zod-codegen 1.5.1 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +6 -0
- package/.github/workflows/release.yml +3 -3
- package/CHANGELOG.md +29 -0
- package/CONTRIBUTING.md +1 -1
- package/EXAMPLES.md +91 -12
- package/README.md +11 -4
- package/dist/scripts/add-js-extensions.d.ts +2 -0
- package/dist/scripts/add-js-extensions.d.ts.map +1 -0
- package/dist/scripts/add-js-extensions.js +66 -0
- package/dist/scripts/update-manifest.d.ts +14 -0
- package/dist/scripts/update-manifest.d.ts.map +1 -0
- package/dist/scripts/update-manifest.js +33 -0
- package/dist/src/assets/manifest.json +1 -1
- package/dist/src/cli.js +3 -3
- package/dist/src/generator.d.ts +46 -4
- package/dist/src/generator.d.ts.map +1 -1
- package/dist/src/generator.js +43 -1
- package/dist/src/interfaces/code-generator.d.ts +1 -1
- package/dist/src/interfaces/code-generator.d.ts.map +1 -1
- package/dist/src/services/code-generator.service.d.ts +5 -3
- package/dist/src/services/code-generator.service.d.ts.map +1 -1
- package/dist/src/services/code-generator.service.js +69 -1
- package/dist/src/services/file-reader.service.d.ts +2 -2
- package/dist/src/services/file-reader.service.d.ts.map +1 -1
- package/dist/src/services/file-writer.service.d.ts +1 -1
- package/dist/src/services/file-writer.service.d.ts.map +1 -1
- package/dist/src/services/import-builder.service.d.ts +1 -1
- package/dist/src/services/import-builder.service.d.ts.map +1 -1
- package/dist/src/services/type-builder.service.d.ts +1 -1
- package/dist/src/services/type-builder.service.d.ts.map +1 -1
- package/dist/src/types/generator-options.d.ts +1 -1
- package/dist/src/types/generator-options.d.ts.map +1 -1
- package/dist/src/utils/error-handler.d.ts +3 -2
- package/dist/src/utils/error-handler.d.ts.map +1 -1
- package/dist/src/utils/error-handler.js +4 -4
- package/dist/src/utils/reporter.d.ts +3 -2
- package/dist/src/utils/reporter.d.ts.map +1 -1
- package/dist/src/utils/reporter.js +7 -5
- package/dist/src/utils/signal-handler.d.ts +3 -2
- package/dist/src/utils/signal-handler.d.ts.map +1 -1
- package/dist/src/utils/signal-handler.js +4 -4
- package/examples/README.md +10 -1
- package/examples/petstore/README.md +6 -6
- package/examples/petstore/authenticated-usage.ts +1 -1
- package/examples/petstore/basic-usage.ts +1 -1
- package/examples/petstore/retry-handler-usage.ts +173 -0
- package/examples/petstore/server-variables-usage.ts +1 -1
- package/examples/petstore/type.ts +68 -47
- package/examples/pokeapi/README.md +3 -3
- package/examples/pokeapi/basic-usage.ts +1 -1
- package/examples/pokeapi/custom-client.ts +1 -1
- package/generated/type.ts +323 -0
- package/package.json +4 -7
- package/scripts/add-js-extensions.ts +79 -0
- package/scripts/update-manifest.ts +4 -2
- package/src/assets/manifest.json +1 -1
- package/src/cli.ts +7 -7
- package/src/generator.ts +51 -9
- package/src/interfaces/code-generator.ts +1 -1
- package/src/services/code-generator.service.ts +114 -8
- package/src/services/file-reader.service.ts +3 -3
- package/src/services/file-writer.service.ts +1 -1
- package/src/services/import-builder.service.ts +1 -1
- package/src/services/type-builder.service.ts +1 -1
- package/src/types/generator-options.ts +1 -1
- package/src/utils/error-handler.ts +6 -5
- package/src/utils/reporter.ts +6 -3
- package/src/utils/signal-handler.ts +10 -8
- package/tests/integration/cli-comprehensive.test.ts +123 -0
- package/tests/integration/cli.test.ts +2 -2
- package/tests/integration/error-scenarios.test.ts +240 -0
- package/tests/integration/snapshots.test.ts +131 -0
- package/tests/unit/code-generator-edge-cases.test.ts +551 -0
- package/tests/unit/code-generator.test.ts +385 -2
- package/tests/unit/file-reader.test.ts +16 -1
- package/tests/unit/generator.test.ts +19 -2
- package/tests/unit/naming-convention.test.ts +30 -1
- package/tests/unit/reporter.test.ts +63 -0
- package/tests/unit/type-builder.test.ts +131 -0
- package/tsconfig.json +3 -3
- package/dist/src/http/fetch-client.d.ts +0 -15
- package/dist/src/http/fetch-client.d.ts.map +0 -1
- package/dist/src/http/fetch-client.js +0 -140
- package/dist/src/polyfills/fetch.d.ts +0 -5
- package/dist/src/polyfills/fetch.d.ts.map +0 -1
- package/dist/src/polyfills/fetch.js +0 -18
- package/dist/src/types/http.d.ts +0 -25
- package/dist/src/types/http.d.ts.map +0 -1
- package/dist/src/types/http.js +0 -10
- package/dist/src/utils/manifest.d.ts +0 -8
- package/dist/src/utils/manifest.d.ts.map +0 -1
- package/dist/src/utils/manifest.js +0 -9
- package/dist/src/utils/tty.d.ts +0 -2
- package/dist/src/utils/tty.d.ts.map +0 -1
- package/dist/src/utils/tty.js +0 -3
- package/src/http/fetch-client.ts +0 -181
- package/src/polyfills/fetch.ts +0 -26
- package/src/types/http.ts +0 -35
- package/src/utils/manifest.ts +0 -17
- package/src/utils/tty.ts +0 -3
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
|
2
|
-
// Built with zod-codegen@1.
|
|
3
|
-
// Latest edit:
|
|
2
|
+
// Built with zod-codegen@1.5.0
|
|
3
|
+
// Latest edit: Thu, 08 Jan 2026 19:31:58 GMT
|
|
4
4
|
// Source file: ./samples/swagger-petstore.yaml
|
|
5
5
|
/* eslint-disable */
|
|
6
6
|
// @ts-nocheck
|
|
@@ -13,7 +13,11 @@ export const Order = z.object({
|
|
|
13
13
|
id: z.number().int().optional(),
|
|
14
14
|
petId: z.number().int().optional(),
|
|
15
15
|
quantity: z.number().int().optional(),
|
|
16
|
-
shipDate: z.
|
|
16
|
+
shipDate: z.iso
|
|
17
|
+
.datetime({
|
|
18
|
+
local: true,
|
|
19
|
+
})
|
|
20
|
+
.optional(),
|
|
17
21
|
status: z.enum(['placed', 'approved', 'delivered']).optional(),
|
|
18
22
|
complete: z.boolean().optional(),
|
|
19
23
|
});
|
|
@@ -59,6 +63,14 @@ export const ApiResponse = z.object({
|
|
|
59
63
|
type: z.string().optional(),
|
|
60
64
|
message: z.string().optional(),
|
|
61
65
|
});
|
|
66
|
+
export type Order = z.infer<typeof Order>;
|
|
67
|
+
export type Address = z.infer<typeof Address>;
|
|
68
|
+
export type Customer = z.infer<typeof Customer>;
|
|
69
|
+
export type Category = z.infer<typeof Category>;
|
|
70
|
+
export type User = z.infer<typeof User>;
|
|
71
|
+
export type Tag = z.infer<typeof Tag>;
|
|
72
|
+
export type Pet = z.infer<typeof Pet>;
|
|
73
|
+
export type ApiResponse = z.infer<typeof ApiResponse>;
|
|
62
74
|
export const serverConfigurations = [
|
|
63
75
|
{
|
|
64
76
|
url: 'https://petstore3.swagger.io/api/v3',
|
|
@@ -91,7 +103,7 @@ function resolveServerUrl(serverIndex?: number | undefined, serverVariables?: Re
|
|
|
91
103
|
}
|
|
92
104
|
|
|
93
105
|
// Client class
|
|
94
|
-
export class SwaggerPetstoreOpenAPI30 {
|
|
106
|
+
export default class SwaggerPetstoreOpenAPI30 {
|
|
95
107
|
readonly #baseUrl: string;
|
|
96
108
|
constructor(options: ClientOptions) {
|
|
97
109
|
const resolvedUrl =
|
|
@@ -101,7 +113,15 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
101
113
|
protected getBaseRequestOptions(): Partial<Omit<RequestInit, 'method' | 'body'>> {
|
|
102
114
|
return {};
|
|
103
115
|
}
|
|
104
|
-
async
|
|
116
|
+
protected async handleResponse<T>(
|
|
117
|
+
response: Response,
|
|
118
|
+
method: string,
|
|
119
|
+
path: string,
|
|
120
|
+
options: _params___Record_string__string___number___boolean___data___unknown__contentType___string__headers___Record_string__string__,
|
|
121
|
+
): Promise<Response> {
|
|
122
|
+
return response;
|
|
123
|
+
}
|
|
124
|
+
protected async makeRequest<T>(
|
|
105
125
|
method: string,
|
|
106
126
|
path: string,
|
|
107
127
|
options: _params___Record_string__string___number___boolean___data___unknown__contentType___string__headers___Record_string__string__ = {},
|
|
@@ -140,18 +160,19 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
140
160
|
})()
|
|
141
161
|
: JSON.stringify(options.data)
|
|
142
162
|
: null;
|
|
143
|
-
const
|
|
163
|
+
const rawResponse = await fetch(url, Object.assign({}, baseOptions, {method, headers: headers, body: body}));
|
|
164
|
+
const response = await this.handleResponse<T>(rawResponse, method, path, options);
|
|
144
165
|
if (!response.ok) throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
145
166
|
return await response.json();
|
|
146
167
|
}
|
|
147
168
|
/**
|
|
148
169
|
* Add a new pet to the store
|
|
149
170
|
* @param body Create a new pet in the store
|
|
150
|
-
* @returns {
|
|
171
|
+
* @returns {Pet}
|
|
151
172
|
*/
|
|
152
|
-
async addPet(body: Pet): Promise<
|
|
173
|
+
async addPet(body: Pet): Promise<Pet> {
|
|
153
174
|
return Pet.parse(
|
|
154
|
-
await this
|
|
175
|
+
await this.makeRequest('POST', '/pet', {data: body, contentType: 'application/x-www-form-urlencoded'}),
|
|
155
176
|
);
|
|
156
177
|
}
|
|
157
178
|
/**
|
|
@@ -159,11 +180,11 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
159
180
|
*
|
|
160
181
|
* Update an existing pet by Id
|
|
161
182
|
* @param body Update an existent pet in the store
|
|
162
|
-
* @returns {
|
|
183
|
+
* @returns {Pet}
|
|
163
184
|
*/
|
|
164
|
-
async updatePet(body: Pet): Promise<
|
|
185
|
+
async updatePet(body: Pet): Promise<Pet> {
|
|
165
186
|
return Pet.parse(
|
|
166
|
-
await this
|
|
187
|
+
await this.makeRequest('PUT', '/pet', {data: body, contentType: 'application/x-www-form-urlencoded'}),
|
|
167
188
|
);
|
|
168
189
|
}
|
|
169
190
|
/**
|
|
@@ -172,10 +193,10 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
172
193
|
* Multiple status values can be provided with comma separated strings
|
|
173
194
|
*
|
|
174
195
|
* @param status Status values that need to be considered for filter
|
|
175
|
-
* @returns {
|
|
196
|
+
* @returns {Pet[]}
|
|
176
197
|
*/
|
|
177
|
-
async findPetsByStatus(status?: string): Promise<
|
|
178
|
-
return await this
|
|
198
|
+
async findPetsByStatus(status?: string): Promise<Pet[]> {
|
|
199
|
+
return await this.makeRequest('GET', '/pet/findByStatus', {params: {status: status}});
|
|
179
200
|
}
|
|
180
201
|
/**
|
|
181
202
|
* Finds Pets by tags
|
|
@@ -183,10 +204,10 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
183
204
|
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
184
205
|
*
|
|
185
206
|
* @param tags Tags to filter by
|
|
186
|
-
* @returns {
|
|
207
|
+
* @returns {Pet[]}
|
|
187
208
|
*/
|
|
188
|
-
async findPetsByTags(tags?: string[]): Promise<
|
|
189
|
-
return await this
|
|
209
|
+
async findPetsByTags(tags?: string[]): Promise<Pet[]> {
|
|
210
|
+
return await this.makeRequest('GET', '/pet/findByTags', {params: {tags: tags}});
|
|
190
211
|
}
|
|
191
212
|
/**
|
|
192
213
|
* Find pet by ID
|
|
@@ -194,10 +215,10 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
194
215
|
* Returns a single pet
|
|
195
216
|
*
|
|
196
217
|
* @param petId ID of pet to return
|
|
197
|
-
* @returns {
|
|
218
|
+
* @returns {Pet}
|
|
198
219
|
*/
|
|
199
|
-
async getPetById(petId: number): Promise<
|
|
200
|
-
return Pet.parse(await this
|
|
220
|
+
async getPetById(petId: number): Promise<Pet> {
|
|
221
|
+
return Pet.parse(await this.makeRequest('GET', `/pet/${petId}`, {}));
|
|
201
222
|
}
|
|
202
223
|
/**
|
|
203
224
|
* Updates a pet in the store with form data
|
|
@@ -208,7 +229,7 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
208
229
|
* @returns {void}
|
|
209
230
|
*/
|
|
210
231
|
async updatePetWithForm(petId: number, name?: string, status?: string): Promise<void> {
|
|
211
|
-
return await this
|
|
232
|
+
return await this.makeRequest('POST', `/pet/${petId}`, {params: {name: name, status: status}});
|
|
212
233
|
}
|
|
213
234
|
/**
|
|
214
235
|
* Deletes a pet
|
|
@@ -220,7 +241,7 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
220
241
|
* @returns {void}
|
|
221
242
|
*/
|
|
222
243
|
async deletePet(petId: number): Promise<void> {
|
|
223
|
-
return await this
|
|
244
|
+
return await this.makeRequest('DELETE', `/pet/${petId}`, {});
|
|
224
245
|
}
|
|
225
246
|
/**
|
|
226
247
|
* uploads an image
|
|
@@ -228,11 +249,11 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
228
249
|
* @param petId ID of pet to update
|
|
229
250
|
* @param additionalMetadata Additional Metadata
|
|
230
251
|
* @param body
|
|
231
|
-
* @returns {
|
|
252
|
+
* @returns {ApiResponse}
|
|
232
253
|
*/
|
|
233
|
-
async uploadFile(petId: number, additionalMetadata?: string): Promise<
|
|
254
|
+
async uploadFile(petId: number, additionalMetadata?: string): Promise<ApiResponse> {
|
|
234
255
|
return ApiResponse.parse(
|
|
235
|
-
await this
|
|
256
|
+
await this.makeRequest('POST', `/pet/${petId}/uploadImage`, {params: {additionalMetadata: additionalMetadata}}),
|
|
236
257
|
);
|
|
237
258
|
}
|
|
238
259
|
/**
|
|
@@ -242,18 +263,18 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
242
263
|
* @returns {Record<string, unknown>}
|
|
243
264
|
*/
|
|
244
265
|
async getInventory(): Promise<Record<string, unknown>> {
|
|
245
|
-
return await this
|
|
266
|
+
return await this.makeRequest('GET', '/store/inventory', {});
|
|
246
267
|
}
|
|
247
268
|
/**
|
|
248
269
|
* Place an order for a pet
|
|
249
270
|
*
|
|
250
271
|
* Place a new order in the store
|
|
251
272
|
* @param body
|
|
252
|
-
* @returns {
|
|
273
|
+
* @returns {Order}
|
|
253
274
|
*/
|
|
254
|
-
async placeOrder(body?: Order): Promise<
|
|
275
|
+
async placeOrder(body?: Order): Promise<Order> {
|
|
255
276
|
return Order.parse(
|
|
256
|
-
await this
|
|
277
|
+
await this.makeRequest('POST', '/store/order', {data: body, contentType: 'application/x-www-form-urlencoded'}),
|
|
257
278
|
);
|
|
258
279
|
}
|
|
259
280
|
/**
|
|
@@ -262,10 +283,10 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
262
283
|
* For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions.
|
|
263
284
|
*
|
|
264
285
|
* @param orderId ID of order that needs to be fetched
|
|
265
|
-
* @returns {
|
|
286
|
+
* @returns {Order}
|
|
266
287
|
*/
|
|
267
|
-
async getOrderById(orderId: number): Promise<
|
|
268
|
-
return Order.parse(await this
|
|
288
|
+
async getOrderById(orderId: number): Promise<Order> {
|
|
289
|
+
return Order.parse(await this.makeRequest('GET', `/store/order/${orderId}`, {}));
|
|
269
290
|
}
|
|
270
291
|
/**
|
|
271
292
|
* Delete purchase order by ID
|
|
@@ -276,27 +297,27 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
276
297
|
* @returns {void}
|
|
277
298
|
*/
|
|
278
299
|
async deleteOrder(orderId: number): Promise<void> {
|
|
279
|
-
return await this
|
|
300
|
+
return await this.makeRequest('DELETE', `/store/order/${orderId}`, {});
|
|
280
301
|
}
|
|
281
302
|
/**
|
|
282
303
|
* Create user
|
|
283
304
|
*
|
|
284
305
|
* This can only be done by the logged in user.
|
|
285
306
|
* @param body Created user object
|
|
286
|
-
* @returns {
|
|
307
|
+
* @returns {User}
|
|
287
308
|
*/
|
|
288
|
-
async createUser(body?: User): Promise<
|
|
309
|
+
async createUser(body?: User): Promise<User> {
|
|
289
310
|
return User.parse(
|
|
290
|
-
await this
|
|
311
|
+
await this.makeRequest('POST', '/user', {data: body, contentType: 'application/x-www-form-urlencoded'}),
|
|
291
312
|
);
|
|
292
313
|
}
|
|
293
314
|
/**
|
|
294
315
|
* Creates list of users with given input array
|
|
295
316
|
* @param body
|
|
296
|
-
* @returns {
|
|
317
|
+
* @returns {User}
|
|
297
318
|
*/
|
|
298
|
-
async createUsersWithListInput(body?: User[]): Promise<
|
|
299
|
-
return User.parse(await this
|
|
319
|
+
async createUsersWithListInput(body?: User[]): Promise<User> {
|
|
320
|
+
return User.parse(await this.makeRequest('POST', '/user/createWithList', {data: body}));
|
|
300
321
|
}
|
|
301
322
|
/**
|
|
302
323
|
* Logs user into the system
|
|
@@ -306,23 +327,23 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
306
327
|
* @returns {string}
|
|
307
328
|
*/
|
|
308
329
|
async loginUser(username?: string, password?: string): Promise<string> {
|
|
309
|
-
return await this
|
|
330
|
+
return await this.makeRequest('GET', '/user/login', {params: {username: username, password: password}});
|
|
310
331
|
}
|
|
311
332
|
/**
|
|
312
333
|
* Logs out current logged in user session
|
|
313
334
|
* @returns {void}
|
|
314
335
|
*/
|
|
315
336
|
async logoutUser(): Promise<void> {
|
|
316
|
-
return await this
|
|
337
|
+
return await this.makeRequest('GET', '/user/logout', {});
|
|
317
338
|
}
|
|
318
339
|
/**
|
|
319
340
|
* Get user by user name
|
|
320
341
|
*
|
|
321
342
|
* @param username The name that needs to be fetched. Use user1 for testing.
|
|
322
|
-
* @returns {
|
|
343
|
+
* @returns {User}
|
|
323
344
|
*/
|
|
324
|
-
async getUserByName(username: string): Promise<
|
|
325
|
-
return User.parse(await this
|
|
345
|
+
async getUserByName(username: string): Promise<User> {
|
|
346
|
+
return User.parse(await this.makeRequest('GET', `/user/${username}`, {}));
|
|
326
347
|
}
|
|
327
348
|
/**
|
|
328
349
|
* Update user
|
|
@@ -334,7 +355,7 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
334
355
|
* @returns {void}
|
|
335
356
|
*/
|
|
336
357
|
async updateUser(username: string, body?: User): Promise<void> {
|
|
337
|
-
return await this
|
|
358
|
+
return await this.makeRequest('PUT', `/user/${username}`, {
|
|
338
359
|
data: body,
|
|
339
360
|
contentType: 'application/x-www-form-urlencoded',
|
|
340
361
|
});
|
|
@@ -348,6 +369,6 @@ export class SwaggerPetstoreOpenAPI30 {
|
|
|
348
369
|
* @returns {void}
|
|
349
370
|
*/
|
|
350
371
|
async deleteUser(username: string): Promise<void> {
|
|
351
|
-
return await this
|
|
372
|
+
return await this.makeRequest('DELETE', `/user/${username}`, {});
|
|
352
373
|
}
|
|
353
374
|
}
|
|
@@ -15,7 +15,7 @@ zod-codegen --input ./samples/pokeapi-openapi.json --output ./examples/pokeapi
|
|
|
15
15
|
## Basic Usage
|
|
16
16
|
|
|
17
17
|
```typescript
|
|
18
|
-
import {PokAPI, defaultBaseUrl} from './type
|
|
18
|
+
import {PokAPI, defaultBaseUrl} from './type';
|
|
19
19
|
|
|
20
20
|
// Create a client instance
|
|
21
21
|
const client = new PokAPI(defaultBaseUrl);
|
|
@@ -30,7 +30,7 @@ console.log('Pokemon:', pokemon);
|
|
|
30
30
|
See [basic-usage.ts](./basic-usage.ts) for a complete example:
|
|
31
31
|
|
|
32
32
|
```typescript
|
|
33
|
-
import {PokAPI, defaultBaseUrl} from './type
|
|
33
|
+
import {PokAPI, defaultBaseUrl} from './type';
|
|
34
34
|
|
|
35
35
|
async function getPokemonInfo() {
|
|
36
36
|
const client = new PokAPI(defaultBaseUrl);
|
|
@@ -73,7 +73,7 @@ npx ts-node examples/pokeapi/basic-usage.ts
|
|
|
73
73
|
See [custom-client.ts](./custom-client.ts) for a complete example:
|
|
74
74
|
|
|
75
75
|
```typescript
|
|
76
|
-
import {PokAPI, defaultBaseUrl} from './type
|
|
76
|
+
import {PokAPI, defaultBaseUrl} from './type';
|
|
77
77
|
|
|
78
78
|
class CustomPokeAPI extends PokAPI {
|
|
79
79
|
protected getBaseRequestOptions(): Partial<Omit<RequestInit, 'method' | 'body'>> {
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* Run with: npx ts-node examples/pokeapi/custom-client.ts
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import {PokAPI, defaultBaseUrl} from './type
|
|
7
|
+
import {PokAPI, defaultBaseUrl} from './type';
|
|
8
8
|
|
|
9
9
|
class CustomPokeAPIClient extends PokAPI {
|
|
10
10
|
protected getBaseRequestOptions(): Partial<Omit<RequestInit, 'method' | 'body'>> {
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
|
|
2
|
+
// Built with zod-codegen@1.6.1
|
|
3
|
+
// Latest edit: Fri, 09 Jan 2026 16:41:29 GMT
|
|
4
|
+
// Source file: ./samples/swagger-petstore.yaml
|
|
5
|
+
/* eslint-disable */
|
|
6
|
+
// @ts-nocheck
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
// Imports
|
|
10
|
+
;
|
|
11
|
+
import { z } from 'zod';
|
|
12
|
+
|
|
13
|
+
// Components schemas
|
|
14
|
+
;
|
|
15
|
+
export const Order = z.object({
|
|
16
|
+
id: z.number().int().optional(),
|
|
17
|
+
petId: z.number().int().optional(),
|
|
18
|
+
quantity: z.number().int().optional(),
|
|
19
|
+
shipDate: z.iso.datetime({
|
|
20
|
+
local: true
|
|
21
|
+
}).optional(),
|
|
22
|
+
status: z.enum(['placed', 'approved', 'delivered']).optional(),
|
|
23
|
+
complete: z.boolean().optional()
|
|
24
|
+
});
|
|
25
|
+
export const Address = z.object({
|
|
26
|
+
street: z.string().optional(),
|
|
27
|
+
city: z.string().optional(),
|
|
28
|
+
state: z.string().optional(),
|
|
29
|
+
zip: z.string().optional()
|
|
30
|
+
});
|
|
31
|
+
export const Customer = z.object({
|
|
32
|
+
id: z.number().int().optional(),
|
|
33
|
+
username: z.string().optional(),
|
|
34
|
+
address: z.array(Address).optional()
|
|
35
|
+
});
|
|
36
|
+
export const Category = z.object({
|
|
37
|
+
id: z.number().int().optional(),
|
|
38
|
+
name: z.string().optional()
|
|
39
|
+
});
|
|
40
|
+
export const User = z.object({
|
|
41
|
+
id: z.number().int().optional(),
|
|
42
|
+
username: z.string().optional(),
|
|
43
|
+
firstName: z.string().optional(),
|
|
44
|
+
lastName: z.string().optional(),
|
|
45
|
+
email: z.string().optional(),
|
|
46
|
+
password: z.string().optional(),
|
|
47
|
+
phone: z.string().optional(),
|
|
48
|
+
userStatus: z.number().int().optional()
|
|
49
|
+
});
|
|
50
|
+
export const Tag = z.object({
|
|
51
|
+
id: z.number().int().optional(),
|
|
52
|
+
name: z.string().optional()
|
|
53
|
+
});
|
|
54
|
+
export const Pet = z.object({
|
|
55
|
+
id: z.number().int().optional(),
|
|
56
|
+
name: z.string(),
|
|
57
|
+
category: Category.optional(),
|
|
58
|
+
photoUrls: z.array(z.string()),
|
|
59
|
+
tags: z.array(Tag).optional(),
|
|
60
|
+
status: z.enum(['available', 'pending', 'sold']).optional()
|
|
61
|
+
});
|
|
62
|
+
export const ApiResponse = z.object({
|
|
63
|
+
code: z.number().int().optional(),
|
|
64
|
+
type: z.string().optional(),
|
|
65
|
+
message: z.string().optional()
|
|
66
|
+
});
|
|
67
|
+
export type Order = z.infer<typeof Order>;
|
|
68
|
+
export type Address = z.infer<typeof Address>;
|
|
69
|
+
export type Customer = z.infer<typeof Customer>;
|
|
70
|
+
export type Category = z.infer<typeof Category>;
|
|
71
|
+
export type User = z.infer<typeof User>;
|
|
72
|
+
export type Tag = z.infer<typeof Tag>;
|
|
73
|
+
export type Pet = z.infer<typeof Pet>;
|
|
74
|
+
export type ApiResponse = z.infer<typeof ApiResponse>;
|
|
75
|
+
export const serverConfigurations = [{
|
|
76
|
+
url: 'https://petstore3.swagger.io/api/v3'
|
|
77
|
+
}];
|
|
78
|
+
export const defaultBaseUrl = 'https://petstore3.swagger.io/api/v3';
|
|
79
|
+
export type ClientOptions = {
|
|
80
|
+
baseUrl?: string;
|
|
81
|
+
serverIndex?: number;
|
|
82
|
+
serverVariables?: Record<string, string>;
|
|
83
|
+
};
|
|
84
|
+
function resolveServerUrl(serverIndex?: number | undefined, serverVariables?: Record<string, string> = {}): string {
|
|
85
|
+
const configs = [{
|
|
86
|
+
url: 'https://petstore3.swagger.io/api/v3'
|
|
87
|
+
}];
|
|
88
|
+
const idx = serverIndex ?? 0;
|
|
89
|
+
if (idx < configs.length) {
|
|
90
|
+
const config = configs[idx];
|
|
91
|
+
let url = config.url;
|
|
92
|
+
if (config.variables && serverVariables) {
|
|
93
|
+
for (const [key, value] of Object.entries(serverVariables)) {
|
|
94
|
+
url = url.replace(new RegExp("\\{" + key + "\\}", "g"), value);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return url;
|
|
98
|
+
}
|
|
99
|
+
return 'https://petstore3.swagger.io/api/v3';
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// Client class
|
|
103
|
+
;
|
|
104
|
+
export default class SwaggerPetstoreOpenAPI30 {
|
|
105
|
+
readonly #baseUrl: string;
|
|
106
|
+
constructor(options: ClientOptions) {
|
|
107
|
+
const resolvedUrl = options.baseUrl !== null ? options.baseUrl : resolveServerUrl(options.serverIndex, options.serverVariables);
|
|
108
|
+
this.#baseUrl = resolvedUrl;
|
|
109
|
+
}
|
|
110
|
+
protected getBaseRequestOptions(): Partial<Omit<RequestInit, 'method' | 'body'>> {
|
|
111
|
+
return {};
|
|
112
|
+
}
|
|
113
|
+
protected async handleResponse<T>(response: Response, method: string, path: string, options: _params___Record_string__string___number___boolean___data___unknown__contentType___string__headers___Record_string__string__): Promise<Response> {
|
|
114
|
+
return response;
|
|
115
|
+
}
|
|
116
|
+
protected async makeRequest<T>(method: string, path: string, options: _params___Record_string__string___number___boolean___data___unknown__contentType___string__headers___Record_string__string__ = {}): Promise<T> {
|
|
117
|
+
const baseUrl = `${this.#baseUrl}${path}`;
|
|
118
|
+
const url = options.params && Object.keys(options.params).length > 0 ? (() => { Object.entries(options.params).forEach(([key, value]) => { new URL(baseUrl).searchParams.set(key, String(value)); }); return new URL(baseUrl).toString(); })() : new URL(baseUrl).toString();
|
|
119
|
+
const baseOptions = this.getBaseRequestOptions();
|
|
120
|
+
const contentType = options.contentType === 'application/x-www-form-urlencoded' ? 'application/x-www-form-urlencoded' : 'application/json';
|
|
121
|
+
const baseHeaders = baseOptions.headers !== undefined ? baseOptions.headers : {};
|
|
122
|
+
const headers = Object.assign({}, baseHeaders, { 'Content-Type': contentType }, options.headers !== undefined ? options.headers : {});
|
|
123
|
+
const body = options.data !== undefined ? options.contentType === 'application/x-www-form-urlencoded' ? (() => { const params = new URLSearchParams(); Object.entries(options.data).forEach(([key, value]) => { params.set(key, String(value)); }); return params.toString(); })() : JSON.stringify(options.data) : null;
|
|
124
|
+
const rawResponse = await fetch(url, Object.assign({}, baseOptions, { method, headers: headers, body: body }));
|
|
125
|
+
const response = await this.handleResponse<T>(rawResponse, method, path, options);
|
|
126
|
+
if (!response.ok)
|
|
127
|
+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
|
|
128
|
+
return await response.json();
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Add a new pet to the store
|
|
132
|
+
* @param body Create a new pet in the store
|
|
133
|
+
* @returns {Pet}
|
|
134
|
+
*/
|
|
135
|
+
async addPet(body: Pet): Promise<Pet> {
|
|
136
|
+
return Pet.parse(await this.makeRequest('POST', '/pet', { data: body, contentType: 'application/x-www-form-urlencoded' }));
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Update an existing pet
|
|
140
|
+
*
|
|
141
|
+
* Update an existing pet by Id
|
|
142
|
+
* @param body Update an existent pet in the store
|
|
143
|
+
* @returns {Pet}
|
|
144
|
+
*/
|
|
145
|
+
async updatePet(body: Pet): Promise<Pet> {
|
|
146
|
+
return Pet.parse(await this.makeRequest('PUT', '/pet', { data: body, contentType: 'application/x-www-form-urlencoded' }));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Finds Pets by status
|
|
150
|
+
*
|
|
151
|
+
* Multiple status values can be provided with comma separated strings
|
|
152
|
+
*
|
|
153
|
+
* @param status Status values that need to be considered for filter
|
|
154
|
+
* @returns {Pet[]}
|
|
155
|
+
*/
|
|
156
|
+
async findPetsByStatus(status?: string): Promise<Pet[]> {
|
|
157
|
+
return await this.makeRequest('GET', '/pet/findByStatus', { params: { 'status': status } });
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Finds Pets by tags
|
|
161
|
+
*
|
|
162
|
+
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
163
|
+
*
|
|
164
|
+
* @param tags Tags to filter by
|
|
165
|
+
* @returns {Pet[]}
|
|
166
|
+
*/
|
|
167
|
+
async findPetsByTags(tags?: string[]): Promise<Pet[]> {
|
|
168
|
+
return await this.makeRequest('GET', '/pet/findByTags', { params: { 'tags': tags } });
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Find pet by ID
|
|
172
|
+
*
|
|
173
|
+
* Returns a single pet
|
|
174
|
+
*
|
|
175
|
+
* @param petId ID of pet to return
|
|
176
|
+
* @returns {Pet}
|
|
177
|
+
*/
|
|
178
|
+
async getPetById(petId: number): Promise<Pet> {
|
|
179
|
+
return Pet.parse(await this.makeRequest('GET', `/pet/${petId}`, {}));
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Updates a pet in the store with form data
|
|
183
|
+
*
|
|
184
|
+
* @param petId ID of pet that needs to be updated
|
|
185
|
+
* @param name Name of pet that needs to be updated
|
|
186
|
+
* @param status Status of pet that needs to be updated
|
|
187
|
+
* @returns {void}
|
|
188
|
+
*/
|
|
189
|
+
async updatePetWithForm(petId: number, name?: string, status?: string): Promise<void> {
|
|
190
|
+
return await this.makeRequest('POST', `/pet/${petId}`, { params: { 'name': name, 'status': status } });
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Deletes a pet
|
|
194
|
+
*
|
|
195
|
+
* delete a pet
|
|
196
|
+
*
|
|
197
|
+
* @param api_key
|
|
198
|
+
* @param petId Pet id to delete
|
|
199
|
+
* @returns {void}
|
|
200
|
+
*/
|
|
201
|
+
async deletePet(petId: number): Promise<void> {
|
|
202
|
+
return await this.makeRequest('DELETE', `/pet/${petId}`, {});
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* uploads an image
|
|
206
|
+
*
|
|
207
|
+
* @param petId ID of pet to update
|
|
208
|
+
* @param additionalMetadata Additional Metadata
|
|
209
|
+
* @param body
|
|
210
|
+
* @returns {ApiResponse}
|
|
211
|
+
*/
|
|
212
|
+
async uploadFile(petId: number, additionalMetadata?: string): Promise<ApiResponse> {
|
|
213
|
+
return ApiResponse.parse(await this.makeRequest('POST', `/pet/${petId}/uploadImage`, { params: { 'additionalMetadata': additionalMetadata } }));
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Returns pet inventories by status
|
|
217
|
+
*
|
|
218
|
+
* Returns a map of status codes to quantities
|
|
219
|
+
* @returns {Record<string, unknown>}
|
|
220
|
+
*/
|
|
221
|
+
async getInventory(): Promise<Record<string, unknown>> {
|
|
222
|
+
return await this.makeRequest('GET', '/store/inventory', {});
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Place an order for a pet
|
|
226
|
+
*
|
|
227
|
+
* Place a new order in the store
|
|
228
|
+
* @param body
|
|
229
|
+
* @returns {Order}
|
|
230
|
+
*/
|
|
231
|
+
async placeOrder(body?: Order): Promise<Order> {
|
|
232
|
+
return Order.parse(await this.makeRequest('POST', '/store/order', { data: body, contentType: 'application/x-www-form-urlencoded' }));
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Find purchase order by ID
|
|
236
|
+
*
|
|
237
|
+
* For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions.
|
|
238
|
+
*
|
|
239
|
+
* @param orderId ID of order that needs to be fetched
|
|
240
|
+
* @returns {Order}
|
|
241
|
+
*/
|
|
242
|
+
async getOrderById(orderId: number): Promise<Order> {
|
|
243
|
+
return Order.parse(await this.makeRequest('GET', `/store/order/${orderId}`, {}));
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Delete purchase order by ID
|
|
247
|
+
*
|
|
248
|
+
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
|
249
|
+
*
|
|
250
|
+
* @param orderId ID of the order that needs to be deleted
|
|
251
|
+
* @returns {void}
|
|
252
|
+
*/
|
|
253
|
+
async deleteOrder(orderId: number): Promise<void> {
|
|
254
|
+
return await this.makeRequest('DELETE', `/store/order/${orderId}`, {});
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Create user
|
|
258
|
+
*
|
|
259
|
+
* This can only be done by the logged in user.
|
|
260
|
+
* @param body Created user object
|
|
261
|
+
* @returns {User}
|
|
262
|
+
*/
|
|
263
|
+
async createUser(body?: User): Promise<User> {
|
|
264
|
+
return User.parse(await this.makeRequest('POST', '/user', { data: body, contentType: 'application/x-www-form-urlencoded' }));
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Creates list of users with given input array
|
|
268
|
+
* @param body
|
|
269
|
+
* @returns {User}
|
|
270
|
+
*/
|
|
271
|
+
async createUsersWithListInput(body?: User[]): Promise<User> {
|
|
272
|
+
return User.parse(await this.makeRequest('POST', '/user/createWithList', { data: body }));
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Logs user into the system
|
|
276
|
+
*
|
|
277
|
+
* @param username The user name for login
|
|
278
|
+
* @param password The password for login in clear text
|
|
279
|
+
* @returns {string}
|
|
280
|
+
*/
|
|
281
|
+
async loginUser(username?: string, password?: string): Promise<string> {
|
|
282
|
+
return await this.makeRequest('GET', '/user/login', { params: { 'username': username, 'password': password } });
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Logs out current logged in user session
|
|
286
|
+
* @returns {void}
|
|
287
|
+
*/
|
|
288
|
+
async logoutUser(): Promise<void> {
|
|
289
|
+
return await this.makeRequest('GET', '/user/logout', {});
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Get user by user name
|
|
293
|
+
*
|
|
294
|
+
* @param username The name that needs to be fetched. Use user1 for testing.
|
|
295
|
+
* @returns {User}
|
|
296
|
+
*/
|
|
297
|
+
async getUserByName(username: string): Promise<User> {
|
|
298
|
+
return User.parse(await this.makeRequest('GET', `/user/${username}`, {}));
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Update user
|
|
302
|
+
*
|
|
303
|
+
* This can only be done by the logged in user.
|
|
304
|
+
*
|
|
305
|
+
* @param username name that need to be deleted
|
|
306
|
+
* @param body Update an existent user in the store
|
|
307
|
+
* @returns {void}
|
|
308
|
+
*/
|
|
309
|
+
async updateUser(username: string, body?: User): Promise<void> {
|
|
310
|
+
return await this.makeRequest('PUT', `/user/${username}`, { data: body, contentType: 'application/x-www-form-urlencoded' });
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Delete user
|
|
314
|
+
*
|
|
315
|
+
* This can only be done by the logged in user.
|
|
316
|
+
*
|
|
317
|
+
* @param username The name that needs to be deleted
|
|
318
|
+
* @returns {void}
|
|
319
|
+
*/
|
|
320
|
+
async deleteUser(username: string): Promise<void> {
|
|
321
|
+
return await this.makeRequest('DELETE', `/user/${username}`, {});
|
|
322
|
+
}
|
|
323
|
+
}
|