Replies: 6 comments 19 replies
-
Yes, @McMerph |
Beta Was this translation helpful? Give feedback.
-
These are actually |
Beta Was this translation helpful? Give feedback.
-
Let's break it apart because it seems like some issues are specific to my use cases. Issues 1 and 2 are hard to reproduce. Let's deal with issues 3 and 4 first. Issue 3 in details type GetV1HelloInput = {
name?: string | undefined;
};
... So, it looks like express-zod-api-client-generation-sample/src/client/api.ts
2:7 error {"message":"Consider removing 'undefined' type or '?' specifier, one of them is redundant.","secondaryLocations":[{"column":18,"line":2,"endColumn":27,"endLine":2}]} sonar/no-redundant-optional Is it an issue or expected behaviour? Issue 4 in details type GetV1HelloInput = {
name?: string | undefined;
};
type GetV1HelloResponse = {
status: "success";
data: {
greetings: string;
};
} | {
status: "error";
error: {
message: string;
};
};
export type Path = "/v1/hello";
export type Method = "get" | "post" | "put" | "delete" | "patch";
export type MethodPath = `${Method} ${Path}`;
export interface Input extends Record<MethodPath, any> {
"get /v1/hello": GetV1HelloInput;
}
export interface Response extends Record<MethodPath, any> {
"get /v1/hello": GetV1HelloResponse;
}
export const jsonEndpoints = { "get /v1/hello": true };
export type Provider = <M extends Method, P extends Path>(method: M, path: P, params: Input[`${M} ${P}`]) => Promise<Response[`${M} ${P}`]>;
export type Implementation = (method: Method, path: string, params: Record<string, any>) => Promise<any>;
export class ExpressZodAPIClient {
constructor(protected readonly implementation: Implementation) { }
public readonly provide: Provider = (method, path, params) => this.implementation(method, Object.keys(params).reduce((acc, key) => acc.replace(`:${key}`, params[key]), path), Object.keys(params).reduce((acc, key) => path.indexOf(`:${key}`) >= 0 ? acc : { ...acc, [key]: params[key] }, {}));
} BTW, ESLint complains: 37:14 error Class name `ExpressZodAPIClient` must match one of the following formats: StrictPascalCase @typescript-eslint/naming-convention
39:41 error Functions that return promises must be async @typescript-eslint/promise-function-async
39:67 error Unsafe return of type `Promise<any>` from function with return type `Promise<Response[`${M} ${P}`]>` @typescript-eslint/no-unsafe-return
39:159 error Unsafe argument of type `any` assigned to a parameter of type `string` @typescript-eslint/no-unsafe-argument
39:221 error Use 'includes()' method instead @typescript-eslint/prefer-includes
39:268 error Unsafe assignment of an `any` value @typescript-eslint/no-unsafe-assignment I'd like to exclude |
Beta Was this translation helpful? Give feedback.
-
@McMerph , Regarding "issue 3"The message you get comes from ESLint rule There are different opinions on presence of But anyway, it's probably more like a code style redundancy. |
Beta Was this translation helpful? Give feedback.
-
Regarding "issue 1" I believe is works as expected because according to Index signature is missing in type (only on interfaces, not on type alias) :
In my particular usecase, these interfaces are converted to types after |
Beta Was this translation helpful? Give feedback.
-
The PR for "issue 4" is #974 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
First of all, https://github.com/RobinTail/express-zod-api#generating-a-frontend-client is a great feature! Thanks for this!
But I manually edit the generated code due to:
Record<string, unknown>
and the actual endpoint input/response. So, I just remove theRecord<string, unknown>
part of the intersections.Record<string, unknown>
. So I change it toRecord<PropertyKey, never>
.undefined
type and the specifier?
, e.g.optionalField?: string | undefined;
. So, I remove theundefined
type from those fields, e.g.optional field?: string
.ExpressZodAPIClient
code produces a lot oftsc
errors. That is why I do not use it and remove this part (MethodPath
,jsonEndpoints
,Provider
,Implementation
,ExpressZodAPIClient
) of the generated code manually. I would like to have more options inClient.print()
so that I don't generate code that I don't use.Can we discuss the points above?
Beta Was this translation helpful? Give feedback.
All reactions