Skip to content

Commit a886fe1

Browse files
committed
feat: add option to customize API client constructor name
feat: add apiClientConstructorName to default option values
1 parent 76d86f6 commit a886fe1

File tree

7 files changed

+25
-2
lines changed

7 files changed

+25
-2
lines changed

.changeset/eight-yaks-wait.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-zod-client": minor
3+
---
4+
5+
Parametrized custom name for default api client constructor

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,15 @@ Options:
6464
--no-with-alias With alias as api client methods (default: true)
6565
-a, --with-alias With alias as api client methods (default: true)
6666
--api-client-name <name> when using the default `template.hbs`, allow customizing the `export const {apiClientName}`
67+
--api-client-constructor-name <name> when using the default `template.hbs`, allow customizing the `export const {apiClientName}`
6768
--error-expr <expr> Pass an expression to determine if a response status is an error
6869
--success-expr <expr> Pass an expression to determine which response status is the main success status
6970
--media-type-expr <expr> Pass an expression to determine which response content should be allowed
7071
--export-schemas When true, will export all `#/components/schemas`
7172
--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
7273
--with-deprecated when true, will keep deprecated endpoints in the api output
7374
--with-description when true, will add z.describe(xxx)
74-
--with-docs when true, will add jsdoc comments to generated types
75+
--with-docs when true, will add jsdoc comments to generated types
7576
--group-strategy groups endpoints by a given strategy, possible values are: 'none' | 'tag' | 'method' | 'tag-file' | 'method-file'
7677
--complexity-threshold schema complexity threshold to determine which one (using less than `<` operator) should be assigned to a variable
7778
--default-status when defined as `auto-correct`, will automatically use `default` as fallback for `response` when no status code was declared

lib/src/cli.ts

+5
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ cli.command("<input>", "path/url to OpenAPI/Swagger document as json/yaml")
2727
"--api-client-name <name>",
2828
"when using the default `template.hbs`, allow customizing the `export const {apiClientName}`"
2929
)
30+
.option(
31+
"--api-client-constructor-name <name>",
32+
"when using the default `template.hbs`, allow customizing the `export const {apiClientConstructorName}`"
33+
)
3034
.option("--error-expr <expr>", "Pass an expression to determine if a response status is an error")
3135
.option("--success-expr <expr>", "Pass an expression to determine which response status is the main success status")
3236
.option("--media-type-expr <expr>", "Pass an expression to determine which response content should be allowed")
@@ -79,6 +83,7 @@ cli.command("<input>", "path/url to OpenAPI/Swagger document as json/yaml")
7983
withAlias,
8084
baseUrl: options.baseUrl,
8185
apiClientName: options.apiClientName,
86+
apiClientConstructorName: options.apiClientConstructorName,
8287
isErrorStatus: options.errorExpr,
8388
isMainResponseStatus: options.successExpr,
8489
shouldExportAllSchemas: options.exportSchemas,

lib/src/template-context.ts

+7
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,13 @@ export type TemplateContextOptions = {
272272
* @default "api"
273273
*/
274274
apiClientName?: string;
275+
276+
/**
277+
* when using the default `template.hbs`, allow customizing the `export const {apiClientConstructorName}`
278+
*
279+
* @default "createApiClient"
280+
*/
281+
apiClientConstructorName?: string;
275282
/**
276283
* when defined, will be used to pick which endpoint to use as the main one and set to `ZodiosEndpointDefinition["response"]`
277284
* will use `default` status code as fallback

lib/src/templates/default.hbs

+3-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const endpoints = makeApi([
8282

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

85-
export function createApiClient(baseUrl: string, options?: ZodiosOptions) {
85+
function createApiClient(baseUrl: string, options?: ZodiosOptions) {
8686
return new Zodios(baseUrl, endpoints, options);
8787
}
88+
89+
export { createApiClient as {{#if options.apiClientConstructorName}}{{options.apiClientConstructorName}}{{else}}createApiClient{{/if}} };

playground/src/Playground/Playground.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,7 @@ const optionNameToCliOptionName = {
613613
noWithAlias: "--no-with-alias",
614614
baseUrl: "--base-url",
615615
apiClientName: "--api-client-name",
616+
apiClientConstructorName: "--api-client-constructor-name",
616617
isErrorStatus: "--error-expr",
617618
isMainResponseStatus: "--success-expr",
618619
shouldExportAllSchemas: "--export-schemas",

playground/src/components/OptionsForm.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const schema = z.object({
66
baseUrl: z.string(),
77
noWithAlias: z.boolean(),
88
apiClientName: z.string().default("api"),
9+
apiClientConstructorName: z.string().default("createApiClient"),
910
isMainResponseStatus: z.string(),
1011
isErrorStatus: z.string(),
1112
isMediaTypeAllowed: z.string(),
@@ -24,6 +25,7 @@ export const defaultOptionValues = {
2425
baseUrl: "",
2526
noWithAlias: false,
2627
apiClientName: "api",
28+
apiClientConstructorName: "createApiClient",
2729
isMainResponseStatus: "status >= 200 && status < 300",
2830
isErrorStatus: "!(status >= 200 && status < 300)",
2931
isMediaTypeAllowed: "mediaType === 'application/json'",

0 commit comments

Comments
 (0)