Skip to content

feat: add option to customize API client constructor name #320

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-yaks-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-zod-client": minor
---

Parametrized custom name for default api client constructor
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ Options:
--no-with-alias With alias as api client methods (default: true)
-a, --with-alias With alias as api client methods (default: true)
--api-client-name <name> when using the default `template.hbs`, allow customizing the `export const {apiClientName}`
--api-client-constructor-name <name> when using the default `template.hbs`, allow customizing the `export const {apiClientName}`
--error-expr <expr> Pass an expression to determine if a response status is an error
--success-expr <expr> Pass an expression to determine which response status is the main success status
--media-type-expr <expr> Pass an expression to determine which response content should be allowed
--export-schemas When true, will export all `#/components/schemas`
--implicit-required When true, will make all properties of an object required by default (rather than the current opposite), unless an explicitly `required` array is set
--with-deprecated when true, will keep deprecated endpoints in the api output
--with-description when true, will add z.describe(xxx)
--with-docs when true, will add jsdoc comments to generated types
--with-docs when true, will add jsdoc comments to generated types
--group-strategy groups endpoints by a given strategy, possible values are: 'none' | 'tag' | 'method' | 'tag-file' | 'method-file'
--complexity-threshold schema complexity threshold to determine which one (using less than `<` operator) should be assigned to a variable
--default-status when defined as `auto-correct`, will automatically use `default` as fallback for `response` when no status code was declared
Expand Down
5 changes: 5 additions & 0 deletions lib/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ cli.command("<input>", "path/url to OpenAPI/Swagger document as json/yaml")
"--api-client-name <name>",
"when using the default `template.hbs`, allow customizing the `export const {apiClientName}`"
)
.option(
"--api-client-constructor-name <name>",
"when using the default `template.hbs`, allow customizing the `export const {apiClientConstructorName}`"
)
.option("--error-expr <expr>", "Pass an expression to determine if a response status is an error")
.option("--success-expr <expr>", "Pass an expression to determine which response status is the main success status")
.option("--media-type-expr <expr>", "Pass an expression to determine which response content should be allowed")
Expand Down Expand Up @@ -79,6 +83,7 @@ cli.command("<input>", "path/url to OpenAPI/Swagger document as json/yaml")
withAlias,
baseUrl: options.baseUrl,
apiClientName: options.apiClientName,
apiClientConstructorName: options.apiClientConstructorName,
isErrorStatus: options.errorExpr,
isMainResponseStatus: options.successExpr,
shouldExportAllSchemas: options.exportSchemas,
Expand Down
7 changes: 7 additions & 0 deletions lib/src/template-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,13 @@ export type TemplateContextOptions = {
* @default "api"
*/
apiClientName?: string;

/**
* when using the default `template.hbs`, allow customizing the `export const {apiClientConstructorName}`
*
* @default "createApiClient"
*/
apiClientConstructorName?: string;
/**
* when defined, will be used to pick which endpoint to use as the main one and set to `ZodiosEndpointDefinition["response"]`
* will use `default` status code as fallback
Expand Down
8 changes: 8 additions & 0 deletions lib/src/templates/default.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ const endpoints = makeApi([

export const {{options.apiClientName}} = new Zodios({{#if options.baseUrl}}"{{options.baseUrl}}", {{/if}}endpoints);

{{#if options.apiClientConstructorName}}
function createApiClient(baseUrl: string, options?: ZodiosOptions) {
return new Zodios(baseUrl, endpoints, options);
}

export { createApiClient as {{options.apiClientConstructorName}} };
{{else}}
export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
return new Zodios(baseUrl, endpoints, options);
}
{{/if}}
1 change: 1 addition & 0 deletions playground/src/Playground/Playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ const optionNameToCliOptionName = {
noWithAlias: "--no-with-alias",
baseUrl: "--base-url",
apiClientName: "--api-client-name",
apiClientConstructorName: "--api-client-constructor-name",
isErrorStatus: "--error-expr",
isMainResponseStatus: "--success-expr",
shouldExportAllSchemas: "--export-schemas",
Expand Down
2 changes: 2 additions & 0 deletions playground/src/components/OptionsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const schema = z.object({
baseUrl: z.string(),
noWithAlias: z.boolean(),
apiClientName: z.string().default("api"),
apiClientConstructorName: z.string().default("createApiClient"),
isMainResponseStatus: z.string(),
isErrorStatus: z.string(),
isMediaTypeAllowed: z.string(),
Expand All @@ -24,6 +25,7 @@ export const defaultOptionValues = {
baseUrl: "",
noWithAlias: false,
apiClientName: "api",
apiClientConstructorName: "createApiClient",
isMainResponseStatus: "status >= 200 && status < 300",
isErrorStatus: "!(status >= 200 && status < 300)",
isMediaTypeAllowed: "mediaType === 'application/json'",
Expand Down
Loading