@@ -46,11 +46,17 @@ const replacerByRuntime = {
46
46
yup : ( line : string ) =>
47
47
line
48
48
. replace ( / y \. I n f e r T y p e < \s * ?t y p e o f ( .* ?) \s * ?> / g, "typeof $1" )
49
- . replace ( new RegExp ( `(${ endpointExport . source } )` + new RegExp ( / ( [ \s \S ] * ? ) ( y \. o b j e c t ) ( \( ) / ) . source , "g" ) , "$1$2(" ) ,
49
+ . replace (
50
+ new RegExp ( `(${ endpointExport . source } )` + new RegExp ( / ( [ \s \S ] * ? ) ( y \. o b j e c t ) ( \( ) / ) . source , "g" ) ,
51
+ "$1$2(" ,
52
+ ) ,
50
53
zod : ( line : string ) =>
51
54
line
52
55
. replace ( / z \. i n f e r < \s * ?t y p e o f ( .* ?) \s * ?> / g, "typeof $1" )
53
- . replace ( new RegExp ( `(${ endpointExport . source } )` + new RegExp ( / ( [ \s \S ] * ? ) ( z \. o b j e c t ) ( \( ) / ) . source , "g" ) , "$1$2(" ) ,
56
+ . replace (
57
+ new RegExp ( `(${ endpointExport . source } )` + new RegExp ( / ( [ \s \S ] * ? ) ( z \. o b j e c t ) ( \( ) / ) . source , "g" ) ,
58
+ "$1$2(" ,
59
+ ) ,
54
60
} ;
55
61
56
62
export const generateFile = ( options : GeneratorOptions ) => {
@@ -64,36 +70,36 @@ export const generateFile = (options: GeneratorOptions) => {
64
70
ctx . runtime === "none"
65
71
? ( file : string ) => file
66
72
: ( file : string ) => {
67
- const model = Codegen . TypeScriptToModel . Generate ( file ) ;
68
- const transformer = runtimeValidationGenerator [ ctx . runtime as Exclude < typeof ctx . runtime , "none" > ] ;
69
- // tmp fix for typebox, there's currently a "// todo" only with Codegen.ModelToTypeBox.Generate
70
- // https://github.com/sinclairzx81/typebox-codegen/blob/44d44d55932371b69f349331b1c8a60f5d760d9e/src/model/model-to-typebox.ts#L31
71
- const generated = ctx . runtime === "typebox" ? Codegen . TypeScriptToTypeBox . Generate ( file ) : transformer ( model ) ;
72
-
73
- let converted = "" ;
74
- const match = generated . match ( / ( c o n s t _ _ E N D P O I N T S _ S T A R T _ _ = ) ( [ \s \S ] * ?) ( e x p o r t t y p e _ _ E N D P O I N T S _ E N D _ _ ) / ) ;
75
- const content = match ?. [ 2 ] ;
76
-
77
- if ( content && ctx . runtime in replacerByRuntime ) {
78
- const before = generated . slice ( 0 , generated . indexOf ( "export type __ENDPOINTS_START" ) ) ;
79
- converted =
80
- before +
81
- replacerByRuntime [ ctx . runtime as keyof typeof replacerByRuntime ] (
82
- content . slice ( content . indexOf ( "export" ) ) ,
83
- ) ;
84
- } else {
85
- converted = generated ;
86
- }
73
+ const model = Codegen . TypeScriptToModel . Generate ( file ) ;
74
+ const transformer = runtimeValidationGenerator [ ctx . runtime as Exclude < typeof ctx . runtime , "none" > ] ;
75
+ // tmp fix for typebox, there's currently a "// todo" only with Codegen.ModelToTypeBox.Generate
76
+ // https://github.com/sinclairzx81/typebox-codegen/blob/44d44d55932371b69f349331b1c8a60f5d760d9e/src/model/model-to-typebox.ts#L31
77
+ const generated = ctx . runtime === "typebox" ? Codegen . TypeScriptToTypeBox . Generate ( file ) : transformer ( model ) ;
78
+
79
+ let converted = "" ;
80
+ const match = generated . match ( / ( c o n s t _ _ E N D P O I N T S _ S T A R T _ _ = ) ( [ \s \S ] * ?) ( e x p o r t t y p e _ _ E N D P O I N T S _ E N D _ _ ) / ) ;
81
+ const content = match ?. [ 2 ] ;
82
+
83
+ if ( content && ctx . runtime in replacerByRuntime ) {
84
+ const before = generated . slice ( 0 , generated . indexOf ( "export type __ENDPOINTS_START" ) ) ;
85
+ converted =
86
+ before +
87
+ replacerByRuntime [ ctx . runtime as keyof typeof replacerByRuntime ] (
88
+ content . slice ( content . indexOf ( "export" ) ) ,
89
+ ) ;
90
+ } else {
91
+ converted = generated ;
92
+ }
87
93
88
- return converted ;
89
- } ;
94
+ return converted ;
95
+ } ;
90
96
91
97
const file = `
92
98
${ transform ( schemaList + endpointSchemaList ) }
93
99
${ apiClient }
94
100
` ;
95
101
96
- return ( file ) ;
102
+ return file ;
97
103
} ;
98
104
99
105
const generateSchemaList = ( { refs, runtime } : GeneratorContext ) => {
@@ -138,36 +144,39 @@ const generateEndpointSchemaList = (ctx: GeneratorContext) => {
138
144
method: "${ endpoint . method . toUpperCase ( ) } ",
139
145
path: "${ endpoint . path } ",
140
146
requestFormat: "${ endpoint . requestFormat } ",
141
- ${ endpoint . meta . hasParameters
142
- ? `parameters: {
147
+ ${
148
+ endpoint . meta . hasParameters
149
+ ? `parameters: {
143
150
${ parameters . query ? `query: ${ parameterObjectToString ( parameters . query ) } ,` : "" }
144
151
${ parameters . path ? `path: ${ parameterObjectToString ( parameters . path ) } ,` : "" }
145
152
${ parameters . header ? `header: ${ parameterObjectToString ( parameters . header ) } ,` : "" }
146
- ${ parameters . body
147
- ? `body: ${ parameterObjectToString (
148
- ctx . runtime === "none"
149
- ? parameters . body . recompute ( ( box ) => {
150
- if ( Box . isReference ( box ) && ! box . params . generics ) {
151
- box . value = `Schemas.${ box . value } ` ;
152
- }
153
- return box ;
154
- } )
155
- : parameters . body ,
156
- ) } ,`
157
- : ""
153
+ ${
154
+ parameters . body
155
+ ? `body: ${ parameterObjectToString (
156
+ ctx . runtime === "none"
157
+ ? parameters . body . recompute ( ( box ) => {
158
+ if ( Box . isReference ( box ) && ! box . params . generics ) {
159
+ box . value = `Schemas.${ box . value } ` ;
160
+ }
161
+ return box ;
162
+ } )
163
+ : parameters . body ,
164
+ ) } ,`
165
+ : ""
158
166
}
159
167
}`
160
- : "parameters: never,"
168
+ : "parameters: never,"
161
169
}
162
- response: ${ ctx . runtime === "none"
163
- ? endpoint . response . recompute ( ( box ) => {
164
- if ( Box . isReference ( box ) && ! box . params . generics && box . value !== "null" ) {
165
- box . value = `Schemas.${ box . value } ` ;
166
- }
167
-
168
- return box ;
169
- } ) . value
170
- : endpoint . response . value
170
+ response: ${
171
+ ctx . runtime === "none"
172
+ ? endpoint . response . recompute ( ( box ) => {
173
+ if ( Box . isReference ( box ) && ! box . params . generics && box . value !== "null" ) {
174
+ box . value = `Schemas.${ box . value } ` ;
175
+ }
176
+
177
+ return box ;
178
+ } ) . value
179
+ : endpoint . response . value
171
180
} ,
172
181
}\n` ;
173
182
} ) ;
@@ -190,14 +199,14 @@ const generateEndpointByMethod = (ctx: GeneratorContext) => {
190
199
// <EndpointByMethod>
191
200
export ${ ctx . runtime === "none" ? "type" : "const" } EndpointByMethod = {
192
201
${ Object . entries ( byMethods )
193
- . map ( ( [ method , list ] ) => {
194
- return `${ method } : {
202
+ . map ( ( [ method , list ] ) => {
203
+ return `${ method } : {
195
204
${ list . map (
196
- ( endpoint ) => `"${ endpoint . path } ": ${ ctx . runtime === "none" ? "Endpoints." : "" } ${ endpoint . meta . alias } ` ,
197
- ) }
205
+ ( endpoint ) => `"${ endpoint . path } ": ${ ctx . runtime === "none" ? "Endpoints." : "" } ${ endpoint . meta . alias } ` ,
206
+ ) }
198
207
}` ;
199
- } )
200
- . join ( ",\n" ) }
208
+ } )
209
+ . join ( ",\n" ) }
201
210
}
202
211
${ ctx . runtime === "none" ? "" : "export type EndpointByMethod = typeof EndpointByMethod;" }
203
212
// </EndpointByMethod>
@@ -264,7 +273,6 @@ type MaybeOptionalArg<T> = RequiredKeys<T> extends never ? [config?: T] : [confi
264
273
// </ApiClientTypes>
265
274
` ;
266
275
267
-
268
276
const apiClient = `
269
277
// <ApiClient>
270
278
export class ApiClient {
@@ -286,22 +294,22 @@ export class ApiClient {
286
294
}
287
295
288
296
${ Object . entries ( byMethods )
289
- . map ( ( [ method , endpointByMethod ] ) => {
290
- const capitalizedMethod = capitalize ( method ) ;
291
- const infer = inferByRuntime [ ctx . runtime ] ;
297
+ . map ( ( [ method , endpointByMethod ] ) => {
298
+ const capitalizedMethod = capitalize ( method ) ;
299
+ const infer = inferByRuntime [ ctx . runtime ] ;
292
300
293
- return endpointByMethod . length
294
- ? `// <ApiClient.${ method } >
301
+ return endpointByMethod . length
302
+ ? `// <ApiClient.${ method } >
295
303
${ method } <Path extends keyof ${ capitalizedMethod } Endpoints, TEndpoint extends ${ capitalizedMethod } Endpoints[Path]>(
296
304
path: Path,
297
305
...params: MaybeOptionalArg<${ match ( ctx . runtime )
298
- . with ( "zod" , "yup" , ( ) => infer ( `TEndpoint["parameters"]` ) )
299
- . with ( "arktype" , "io-ts" , "typebox" , "valibot" , ( ) => infer ( `TEndpoint` ) + `["parameters"]` )
300
- . otherwise ( ( ) => `TEndpoint["parameters"]` ) } >
306
+ . with ( "zod" , "yup" , ( ) => infer ( `TEndpoint["parameters"]` ) )
307
+ . with ( "arktype" , "io-ts" , "typebox" , "valibot" , ( ) => infer ( `TEndpoint` ) + `["parameters"]` )
308
+ . otherwise ( ( ) => `TEndpoint["parameters"]` ) } >
301
309
): Promise<${ match ( ctx . runtime )
302
- . with ( "zod" , "yup" , ( ) => infer ( `TEndpoint["response"]` ) )
303
- . with ( "arktype" , "io-ts" , "typebox" , "valibot" , ( ) => infer ( `TEndpoint` ) + `["response"]` )
304
- . otherwise ( ( ) => `TEndpoint["response"]` ) } > {
310
+ . with ( "zod" , "yup" , ( ) => infer ( `TEndpoint["response"]` ) )
311
+ . with ( "arktype" , "io-ts" , "typebox" , "valibot" , ( ) => infer ( `TEndpoint` ) + `["response"]` )
312
+ . otherwise ( ( ) => `TEndpoint["response"]` ) } > {
305
313
return this.fetcher("${ method } ", this.baseUrl + path, params[0])
306
314
.then(response => this.parseResponse(response))${ match ( ctx . runtime )
307
315
. with ( "zod" , "yup" , ( ) => `as Promise<${ infer ( `TEndpoint["response"]` ) } >` )
@@ -310,9 +318,9 @@ export class ApiClient {
310
318
}
311
319
// </ApiClient.${ method } >
312
320
`
313
- : "" ;
314
- } )
315
- . join ( "\n" ) }
321
+ : "" ;
322
+ } )
323
+ . join ( "\n" ) }
316
324
317
325
// <ApiClient.request>
318
326
/**
@@ -326,9 +334,17 @@ export class ApiClient {
326
334
method: TMethod,
327
335
path: TPath,
328
336
...params: MaybeOptionalArg<${ match ( ctx . runtime )
329
- . with ( "zod" , "yup" , ( ) => inferByRuntime [ ctx . runtime ] ( `TEndpoint extends { parameters: infer Params } ? Params : never` ) )
330
- . with ( "arktype" , "io-ts" , "typebox" , "valibot" , ( ) => inferByRuntime [ ctx . runtime ] ( `TEndpoint` ) + `["parameters"]` )
331
- . otherwise ( ( ) => `TEndpoint extends { parameters: infer Params } ? Params : never` ) } >)
337
+ . with ( "zod" , "yup" , ( ) =>
338
+ inferByRuntime [ ctx . runtime ] ( `TEndpoint extends { parameters: infer Params } ? Params : never` ) ,
339
+ )
340
+ . with (
341
+ "arktype" ,
342
+ "io-ts" ,
343
+ "typebox" ,
344
+ "valibot" ,
345
+ ( ) => inferByRuntime [ ctx . runtime ] ( `TEndpoint` ) + `["parameters"]` ,
346
+ )
347
+ . otherwise ( ( ) => `TEndpoint extends { parameters: infer Params } ? Params : never` ) } >)
332
348
: Promise<Omit<Response, "json"> & {
333
349
/** [MDN Reference](https://developer.mozilla.org/docs/Web/API/Request/json) */
334
350
json: () => Promise<TEndpoint extends { response: infer Res } ? Res : never>;
0 commit comments