zod-codegen 1.4.0 → 1.4.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 +17 -17
- package/.github/workflows/release.yml +8 -8
- package/CHANGELOG.md +11 -0
- package/dist/src/services/code-generator.service.d.ts.map +1 -1
- package/dist/src/services/code-generator.service.js +3 -3
- package/package.json +10 -15
- package/src/services/code-generator.service.ts +4 -7
- package/tests/unit/code-generator.test.ts +3 -3
- package/tests/unit/generator.test.ts +2 -2
- package/tsconfig.json +1 -1
- package/.claude/settings.local.json +0 -43
- package/dist/scripts/update-manifest.d.ts +0 -14
- package/dist/scripts/update-manifest.d.ts.map +0 -1
- package/dist/scripts/update-manifest.js +0 -31
- package/dist/tests/integration/cli.test.d.ts +0 -2
- package/dist/tests/integration/cli.test.d.ts.map +0 -1
- package/dist/tests/integration/cli.test.js +0 -25
- package/dist/tests/unit/code-generator.test.d.ts +0 -2
- package/dist/tests/unit/code-generator.test.d.ts.map +0 -1
- package/dist/tests/unit/code-generator.test.js +0 -459
- package/dist/tests/unit/file-reader.test.d.ts +0 -2
- package/dist/tests/unit/file-reader.test.d.ts.map +0 -1
- package/dist/tests/unit/file-reader.test.js +0 -110
- package/dist/tests/unit/generator.test.d.ts +0 -2
- package/dist/tests/unit/generator.test.d.ts.map +0 -1
- package/dist/tests/unit/generator.test.js +0 -100
- package/dist/tests/unit/naming-convention.test.d.ts +0 -2
- package/dist/tests/unit/naming-convention.test.d.ts.map +0 -1
- package/dist/tests/unit/naming-convention.test.js +0 -231
- package/scripts/republish-versions.sh +0 -94
|
@@ -1,459 +0,0 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it } from 'vitest';
|
|
2
|
-
import { TypeScriptCodeGeneratorService } from '../../src/services/code-generator.service.js';
|
|
3
|
-
describe('TypeScriptCodeGeneratorService', () => {
|
|
4
|
-
let generator;
|
|
5
|
-
beforeEach(() => {
|
|
6
|
-
generator = new TypeScriptCodeGeneratorService();
|
|
7
|
-
});
|
|
8
|
-
describe('naming conventions', () => {
|
|
9
|
-
it('should apply camelCase naming convention', () => {
|
|
10
|
-
const spec = {
|
|
11
|
-
openapi: '3.0.0',
|
|
12
|
-
info: {
|
|
13
|
-
title: 'Test API',
|
|
14
|
-
version: '1.0.0',
|
|
15
|
-
},
|
|
16
|
-
paths: {
|
|
17
|
-
'/users': {
|
|
18
|
-
get: {
|
|
19
|
-
operationId: 'get_user_by_id',
|
|
20
|
-
responses: {
|
|
21
|
-
'200': {
|
|
22
|
-
description: 'Success',
|
|
23
|
-
content: {
|
|
24
|
-
'application/json': {
|
|
25
|
-
schema: { type: 'string' },
|
|
26
|
-
},
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
},
|
|
31
|
-
},
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
const generatorWithConvention = new TypeScriptCodeGeneratorService({
|
|
35
|
-
namingConvention: 'camelCase',
|
|
36
|
-
});
|
|
37
|
-
const code = generatorWithConvention.generate(spec);
|
|
38
|
-
expect(code).toContain('async getUserById');
|
|
39
|
-
expect(code).not.toContain('async get_user_by_id');
|
|
40
|
-
});
|
|
41
|
-
it('should apply PascalCase naming convention', () => {
|
|
42
|
-
const spec = {
|
|
43
|
-
openapi: '3.0.0',
|
|
44
|
-
info: {
|
|
45
|
-
title: 'Test API',
|
|
46
|
-
version: '1.0.0',
|
|
47
|
-
},
|
|
48
|
-
paths: {
|
|
49
|
-
'/users': {
|
|
50
|
-
get: {
|
|
51
|
-
operationId: 'get_user_by_id',
|
|
52
|
-
responses: {
|
|
53
|
-
'200': {
|
|
54
|
-
description: 'Success',
|
|
55
|
-
content: {
|
|
56
|
-
'application/json': {
|
|
57
|
-
schema: { type: 'string' },
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
},
|
|
62
|
-
},
|
|
63
|
-
},
|
|
64
|
-
},
|
|
65
|
-
};
|
|
66
|
-
const generatorWithConvention = new TypeScriptCodeGeneratorService({
|
|
67
|
-
namingConvention: 'PascalCase',
|
|
68
|
-
});
|
|
69
|
-
const code = generatorWithConvention.generate(spec);
|
|
70
|
-
expect(code).toContain('async GetUserById');
|
|
71
|
-
expect(code).not.toContain('async get_user_by_id');
|
|
72
|
-
});
|
|
73
|
-
it('should apply snake_case naming convention', () => {
|
|
74
|
-
const spec = {
|
|
75
|
-
openapi: '3.0.0',
|
|
76
|
-
info: {
|
|
77
|
-
title: 'Test API',
|
|
78
|
-
version: '1.0.0',
|
|
79
|
-
},
|
|
80
|
-
paths: {
|
|
81
|
-
'/users': {
|
|
82
|
-
get: {
|
|
83
|
-
operationId: 'getUserById',
|
|
84
|
-
responses: {
|
|
85
|
-
'200': {
|
|
86
|
-
description: 'Success',
|
|
87
|
-
content: {
|
|
88
|
-
'application/json': {
|
|
89
|
-
schema: { type: 'string' },
|
|
90
|
-
},
|
|
91
|
-
},
|
|
92
|
-
},
|
|
93
|
-
},
|
|
94
|
-
},
|
|
95
|
-
},
|
|
96
|
-
},
|
|
97
|
-
};
|
|
98
|
-
const generatorWithConvention = new TypeScriptCodeGeneratorService({
|
|
99
|
-
namingConvention: 'snake_case',
|
|
100
|
-
});
|
|
101
|
-
const code = generatorWithConvention.generate(spec);
|
|
102
|
-
expect(code).toContain('async get_user_by_id');
|
|
103
|
-
expect(code).not.toContain('async getUserById');
|
|
104
|
-
});
|
|
105
|
-
it('should use custom transformer when provided', () => {
|
|
106
|
-
const spec = {
|
|
107
|
-
openapi: '3.0.0',
|
|
108
|
-
info: {
|
|
109
|
-
title: 'Test API',
|
|
110
|
-
version: '1.0.0',
|
|
111
|
-
},
|
|
112
|
-
paths: {
|
|
113
|
-
'/users/{id}': {
|
|
114
|
-
get: {
|
|
115
|
-
operationId: 'getUserById',
|
|
116
|
-
tags: ['users'],
|
|
117
|
-
summary: 'Get user',
|
|
118
|
-
responses: {
|
|
119
|
-
'200': {
|
|
120
|
-
description: 'Success',
|
|
121
|
-
content: {
|
|
122
|
-
'application/json': {
|
|
123
|
-
schema: { type: 'string' },
|
|
124
|
-
},
|
|
125
|
-
},
|
|
126
|
-
},
|
|
127
|
-
},
|
|
128
|
-
},
|
|
129
|
-
},
|
|
130
|
-
},
|
|
131
|
-
};
|
|
132
|
-
const customTransformer = (details) => {
|
|
133
|
-
return `${details.method.toUpperCase()}_${details.tags?.[0] || 'default'}_${details.operationId}`;
|
|
134
|
-
};
|
|
135
|
-
const generatorWithTransformer = new TypeScriptCodeGeneratorService({
|
|
136
|
-
operationNameTransformer: customTransformer,
|
|
137
|
-
});
|
|
138
|
-
const code = generatorWithTransformer.generate(spec);
|
|
139
|
-
expect(code).toContain('async GET_users_getUserById');
|
|
140
|
-
});
|
|
141
|
-
it('should prioritize custom transformer over naming convention', () => {
|
|
142
|
-
const spec = {
|
|
143
|
-
openapi: '3.0.0',
|
|
144
|
-
info: {
|
|
145
|
-
title: 'Test API',
|
|
146
|
-
version: '1.0.0',
|
|
147
|
-
},
|
|
148
|
-
paths: {
|
|
149
|
-
'/users': {
|
|
150
|
-
get: {
|
|
151
|
-
operationId: 'get_user_by_id',
|
|
152
|
-
responses: {
|
|
153
|
-
'200': {
|
|
154
|
-
description: 'Success',
|
|
155
|
-
content: {
|
|
156
|
-
'application/json': {
|
|
157
|
-
schema: { type: 'string' },
|
|
158
|
-
},
|
|
159
|
-
},
|
|
160
|
-
},
|
|
161
|
-
},
|
|
162
|
-
},
|
|
163
|
-
},
|
|
164
|
-
},
|
|
165
|
-
};
|
|
166
|
-
const customTransformer = () => 'customName';
|
|
167
|
-
const generatorWithBoth = new TypeScriptCodeGeneratorService({
|
|
168
|
-
namingConvention: 'PascalCase',
|
|
169
|
-
operationNameTransformer: customTransformer,
|
|
170
|
-
});
|
|
171
|
-
const code = generatorWithBoth.generate(spec);
|
|
172
|
-
expect(code).toContain('async customName');
|
|
173
|
-
expect(code).not.toContain('GetUserById');
|
|
174
|
-
expect(code).not.toContain('get_user_by_id');
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
describe('generate', () => {
|
|
178
|
-
it('should generate code for a minimal OpenAPI spec', () => {
|
|
179
|
-
const spec = {
|
|
180
|
-
openapi: '3.0.0',
|
|
181
|
-
info: {
|
|
182
|
-
title: 'Test API',
|
|
183
|
-
version: '1.0.0',
|
|
184
|
-
},
|
|
185
|
-
paths: {},
|
|
186
|
-
};
|
|
187
|
-
const code = generator.generate(spec);
|
|
188
|
-
expect(code).toBeTruthy();
|
|
189
|
-
expect(typeof code).toBe('string');
|
|
190
|
-
expect(code).toMatch(/import\s*{\s*z\s*}\s*from\s*['"]zod['"]/);
|
|
191
|
-
expect(code).toContain('export class');
|
|
192
|
-
});
|
|
193
|
-
it('should generate schemas for component schemas', () => {
|
|
194
|
-
const spec = {
|
|
195
|
-
openapi: '3.0.0',
|
|
196
|
-
info: {
|
|
197
|
-
title: 'Test API',
|
|
198
|
-
version: '1.0.0',
|
|
199
|
-
},
|
|
200
|
-
paths: {},
|
|
201
|
-
components: {
|
|
202
|
-
schemas: {
|
|
203
|
-
User: {
|
|
204
|
-
type: 'object',
|
|
205
|
-
properties: {
|
|
206
|
-
id: { type: 'integer' },
|
|
207
|
-
name: { type: 'string' },
|
|
208
|
-
},
|
|
209
|
-
required: ['id', 'name'],
|
|
210
|
-
},
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
};
|
|
214
|
-
const code = generator.generate(spec);
|
|
215
|
-
expect(code).toContain('export const User');
|
|
216
|
-
expect(code).toContain('z.object');
|
|
217
|
-
expect(code).toContain('z.number().int()');
|
|
218
|
-
expect(code).toContain('z.string()');
|
|
219
|
-
});
|
|
220
|
-
it('should generate client methods for paths', () => {
|
|
221
|
-
const spec = {
|
|
222
|
-
openapi: '3.0.0',
|
|
223
|
-
info: {
|
|
224
|
-
title: 'Test API',
|
|
225
|
-
version: '1.0.0',
|
|
226
|
-
},
|
|
227
|
-
paths: {
|
|
228
|
-
'/users': {
|
|
229
|
-
get: {
|
|
230
|
-
operationId: 'getUsers',
|
|
231
|
-
responses: {
|
|
232
|
-
'200': {
|
|
233
|
-
description: 'Success',
|
|
234
|
-
content: {
|
|
235
|
-
'application/json': {
|
|
236
|
-
schema: {
|
|
237
|
-
type: 'array',
|
|
238
|
-
items: { type: 'string' },
|
|
239
|
-
},
|
|
240
|
-
},
|
|
241
|
-
},
|
|
242
|
-
},
|
|
243
|
-
},
|
|
244
|
-
},
|
|
245
|
-
},
|
|
246
|
-
},
|
|
247
|
-
};
|
|
248
|
-
const code = generator.generate(spec);
|
|
249
|
-
expect(code).toContain('async getUsers');
|
|
250
|
-
expect(code).toContain('#makeRequest');
|
|
251
|
-
});
|
|
252
|
-
it('should generate getBaseRequestOptions method', () => {
|
|
253
|
-
const spec = {
|
|
254
|
-
openapi: '3.0.0',
|
|
255
|
-
info: {
|
|
256
|
-
title: 'Test API',
|
|
257
|
-
version: '1.0.0',
|
|
258
|
-
},
|
|
259
|
-
paths: {},
|
|
260
|
-
};
|
|
261
|
-
const code = generator.generate(spec);
|
|
262
|
-
expect(code).toContain('protected getBaseRequestOptions');
|
|
263
|
-
expect(code).toContain('Partial<Omit<RequestInit');
|
|
264
|
-
});
|
|
265
|
-
it('should handle servers configuration', () => {
|
|
266
|
-
const spec = {
|
|
267
|
-
openapi: '3.0.0',
|
|
268
|
-
info: {
|
|
269
|
-
title: 'Test API',
|
|
270
|
-
version: '1.0.0',
|
|
271
|
-
},
|
|
272
|
-
servers: [{ url: 'https://api.example.com' }],
|
|
273
|
-
paths: {},
|
|
274
|
-
};
|
|
275
|
-
const code = generator.generate(spec);
|
|
276
|
-
expect(code).toContain('defaultBaseUrl');
|
|
277
|
-
expect(code).toContain('https://api.example.com');
|
|
278
|
-
});
|
|
279
|
-
it('should handle complex schemas with references', () => {
|
|
280
|
-
const spec = {
|
|
281
|
-
openapi: '3.0.0',
|
|
282
|
-
info: {
|
|
283
|
-
title: 'Test API',
|
|
284
|
-
version: '1.0.0',
|
|
285
|
-
},
|
|
286
|
-
paths: {},
|
|
287
|
-
components: {
|
|
288
|
-
schemas: {
|
|
289
|
-
User: {
|
|
290
|
-
type: 'object',
|
|
291
|
-
properties: {
|
|
292
|
-
id: { type: 'integer' },
|
|
293
|
-
profile: { $ref: '#/components/schemas/Profile' },
|
|
294
|
-
},
|
|
295
|
-
required: ['id'],
|
|
296
|
-
},
|
|
297
|
-
Profile: {
|
|
298
|
-
type: 'object',
|
|
299
|
-
properties: {
|
|
300
|
-
name: { type: 'string' },
|
|
301
|
-
email: { type: 'string', format: 'email' },
|
|
302
|
-
},
|
|
303
|
-
required: ['name', 'email'],
|
|
304
|
-
},
|
|
305
|
-
},
|
|
306
|
-
},
|
|
307
|
-
};
|
|
308
|
-
const code = generator.generate(spec);
|
|
309
|
-
expect(code).toContain('export const User');
|
|
310
|
-
expect(code).toContain('export const Profile');
|
|
311
|
-
// Profile should be defined before User (topological sort)
|
|
312
|
-
const profileIndex = code.indexOf('Profile');
|
|
313
|
-
const userIndex = code.indexOf('User');
|
|
314
|
-
expect(profileIndex).toBeLessThan(userIndex);
|
|
315
|
-
});
|
|
316
|
-
it('should handle enum types', () => {
|
|
317
|
-
const spec = {
|
|
318
|
-
openapi: '3.0.0',
|
|
319
|
-
info: {
|
|
320
|
-
title: 'Test API',
|
|
321
|
-
version: '1.0.0',
|
|
322
|
-
},
|
|
323
|
-
paths: {},
|
|
324
|
-
components: {
|
|
325
|
-
schemas: {
|
|
326
|
-
Status: {
|
|
327
|
-
type: 'string',
|
|
328
|
-
enum: ['active', 'inactive', 'pending'],
|
|
329
|
-
},
|
|
330
|
-
},
|
|
331
|
-
},
|
|
332
|
-
};
|
|
333
|
-
const code = generator.generate(spec);
|
|
334
|
-
expect(code).toContain('z.enum');
|
|
335
|
-
expect(code).toContain('active');
|
|
336
|
-
expect(code).toContain('inactive');
|
|
337
|
-
expect(code).toContain('pending');
|
|
338
|
-
});
|
|
339
|
-
it('should handle numeric enum types with z.union and z.literal', () => {
|
|
340
|
-
const spec = {
|
|
341
|
-
openapi: '3.0.0',
|
|
342
|
-
info: {
|
|
343
|
-
title: 'Test API',
|
|
344
|
-
version: '1.0.0',
|
|
345
|
-
},
|
|
346
|
-
paths: {},
|
|
347
|
-
components: {
|
|
348
|
-
schemas: {
|
|
349
|
-
Status: {
|
|
350
|
-
type: 'integer',
|
|
351
|
-
enum: [-99, 0, 1, 2],
|
|
352
|
-
},
|
|
353
|
-
ExecutionMode: {
|
|
354
|
-
type: 'integer',
|
|
355
|
-
enum: [1, 2],
|
|
356
|
-
},
|
|
357
|
-
},
|
|
358
|
-
},
|
|
359
|
-
};
|
|
360
|
-
const code = generator.generate(spec);
|
|
361
|
-
// Numeric enums should use z.union([z.literal(...), ...])
|
|
362
|
-
expect(code).toContain('z.union');
|
|
363
|
-
expect(code).toContain('z.literal');
|
|
364
|
-
expect(code).toContain('-99');
|
|
365
|
-
expect(code).toContain('0');
|
|
366
|
-
expect(code).toContain('1');
|
|
367
|
-
expect(code).toContain('2');
|
|
368
|
-
// Should not use z.enum for numeric enums
|
|
369
|
-
expect(code).not.toContain('Status: z.enum');
|
|
370
|
-
expect(code).not.toContain('ExecutionMode: z.enum');
|
|
371
|
-
});
|
|
372
|
-
it('should merge baseOptions with request-specific options in #makeRequest', () => {
|
|
373
|
-
const spec = {
|
|
374
|
-
openapi: '3.0.0',
|
|
375
|
-
info: {
|
|
376
|
-
title: 'Test API',
|
|
377
|
-
version: '1.0.0',
|
|
378
|
-
},
|
|
379
|
-
paths: {
|
|
380
|
-
'/test': {
|
|
381
|
-
get: {
|
|
382
|
-
operationId: 'testEndpoint',
|
|
383
|
-
responses: {
|
|
384
|
-
'200': {
|
|
385
|
-
description: 'Success',
|
|
386
|
-
content: {
|
|
387
|
-
'application/json': {
|
|
388
|
-
schema: { type: 'string' },
|
|
389
|
-
},
|
|
390
|
-
},
|
|
391
|
-
},
|
|
392
|
-
},
|
|
393
|
-
},
|
|
394
|
-
},
|
|
395
|
-
},
|
|
396
|
-
};
|
|
397
|
-
const code = generator.generate(spec);
|
|
398
|
-
// Should call getBaseRequestOptions()
|
|
399
|
-
expect(code).toContain('getBaseRequestOptions()');
|
|
400
|
-
// Should merge headers: baseHeaders + Content-Type + request headers
|
|
401
|
-
expect(code).toContain('Object.assign');
|
|
402
|
-
expect(code).toContain('baseHeaders');
|
|
403
|
-
expect(code).toContain('Content-Type');
|
|
404
|
-
// Should merge all options: baseOptions + {method, headers, body}
|
|
405
|
-
expect(code).toMatch(/Object\.assign\s*\(\s*\{\s*\}\s*,\s*baseOptions/);
|
|
406
|
-
expect(code).toContain('method');
|
|
407
|
-
expect(code).toContain('headers');
|
|
408
|
-
expect(code).toContain('body');
|
|
409
|
-
});
|
|
410
|
-
it('should handle array types', () => {
|
|
411
|
-
const spec = {
|
|
412
|
-
openapi: '3.0.0',
|
|
413
|
-
info: {
|
|
414
|
-
title: 'Test API',
|
|
415
|
-
version: '1.0.0',
|
|
416
|
-
},
|
|
417
|
-
paths: {},
|
|
418
|
-
components: {
|
|
419
|
-
schemas: {
|
|
420
|
-
Tags: {
|
|
421
|
-
type: 'array',
|
|
422
|
-
items: { type: 'string' },
|
|
423
|
-
},
|
|
424
|
-
},
|
|
425
|
-
},
|
|
426
|
-
};
|
|
427
|
-
const code = generator.generate(spec);
|
|
428
|
-
expect(code).toContain('z.array');
|
|
429
|
-
expect(code).toContain('z.string()');
|
|
430
|
-
});
|
|
431
|
-
});
|
|
432
|
-
describe('buildSchema', () => {
|
|
433
|
-
it('should build schema for string type', () => {
|
|
434
|
-
const schema = { type: 'string' };
|
|
435
|
-
const result = generator.buildSchema(schema, true);
|
|
436
|
-
expect(result).toBeDefined();
|
|
437
|
-
});
|
|
438
|
-
it('should build schema for number type', () => {
|
|
439
|
-
const schema = { type: 'number' };
|
|
440
|
-
const result = generator.buildSchema(schema, true);
|
|
441
|
-
expect(result).toBeDefined();
|
|
442
|
-
});
|
|
443
|
-
it('should build schema for boolean type', () => {
|
|
444
|
-
const schema = { type: 'boolean' };
|
|
445
|
-
const result = generator.buildSchema(schema, true);
|
|
446
|
-
expect(result).toBeDefined();
|
|
447
|
-
});
|
|
448
|
-
it('should handle optional fields', () => {
|
|
449
|
-
const schema = { type: 'string' };
|
|
450
|
-
const result = generator.buildSchema(schema, false);
|
|
451
|
-
expect(result).toBeDefined();
|
|
452
|
-
});
|
|
453
|
-
it('should handle string formats', () => {
|
|
454
|
-
const schema = { type: 'string', format: 'email' };
|
|
455
|
-
const result = generator.buildSchema(schema, true);
|
|
456
|
-
expect(result).toBeDefined();
|
|
457
|
-
});
|
|
458
|
-
});
|
|
459
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"file-reader.test.d.ts","sourceRoot":"","sources":["../../../tests/unit/file-reader.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { describe, expect, it, vi, beforeEach } from 'vitest';
|
|
2
|
-
import { SyncFileReaderService, OpenApiFileParserService } from '../../src/services/file-reader.service.js';
|
|
3
|
-
import { join } from 'node:path';
|
|
4
|
-
import { fileURLToPath } from 'node:url';
|
|
5
|
-
import { dirname } from 'node:path';
|
|
6
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
7
|
-
describe('SyncFileReaderService', () => {
|
|
8
|
-
let reader;
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
reader = new SyncFileReaderService();
|
|
11
|
-
});
|
|
12
|
-
describe('readFile', () => {
|
|
13
|
-
it('should read local JSON files', async () => {
|
|
14
|
-
const content = await reader.readFile(join(__dirname, '../../samples/openapi.json'));
|
|
15
|
-
expect(content).toBeTruthy();
|
|
16
|
-
expect(typeof content).toBe('string');
|
|
17
|
-
});
|
|
18
|
-
it('should read local YAML files', async () => {
|
|
19
|
-
const content = await reader.readFile(join(__dirname, '../../samples/swagger-petstore.yaml'));
|
|
20
|
-
expect(content).toBeTruthy();
|
|
21
|
-
expect(typeof content).toBe('string');
|
|
22
|
-
expect(content).toContain('openapi:');
|
|
23
|
-
});
|
|
24
|
-
it('should handle non-existent files', async () => {
|
|
25
|
-
await expect(reader.readFile('./non-existent-file.json')).rejects.toThrow();
|
|
26
|
-
});
|
|
27
|
-
it('should handle URLs', async () => {
|
|
28
|
-
// Mock fetch for URL test
|
|
29
|
-
const originalFetch = global.fetch;
|
|
30
|
-
global.fetch = vi.fn().mockResolvedValue({
|
|
31
|
-
ok: true,
|
|
32
|
-
text: async () => '{"openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}}',
|
|
33
|
-
});
|
|
34
|
-
const content = await reader.readFile('https://example.com/openapi.json');
|
|
35
|
-
expect(content).toBeTruthy();
|
|
36
|
-
global.fetch = originalFetch;
|
|
37
|
-
});
|
|
38
|
-
it('should handle URL fetch errors with non-ok responses', async () => {
|
|
39
|
-
const originalFetch = global.fetch;
|
|
40
|
-
const mockFetch = vi.fn().mockResolvedValue({
|
|
41
|
-
ok: false,
|
|
42
|
-
status: 404,
|
|
43
|
-
statusText: 'Not Found',
|
|
44
|
-
});
|
|
45
|
-
global.fetch = mockFetch;
|
|
46
|
-
await expect(reader.readFile('https://example.com/not-found.json')).rejects.toThrow();
|
|
47
|
-
// Verify fetch was called
|
|
48
|
-
expect(mockFetch).toHaveBeenCalledWith('https://example.com/not-found.json');
|
|
49
|
-
global.fetch = originalFetch;
|
|
50
|
-
});
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
describe('OpenApiFileParserService', () => {
|
|
54
|
-
let parser;
|
|
55
|
-
beforeEach(() => {
|
|
56
|
-
parser = new OpenApiFileParserService();
|
|
57
|
-
});
|
|
58
|
-
describe('parse', () => {
|
|
59
|
-
it('should parse valid JSON OpenAPI spec', () => {
|
|
60
|
-
const jsonSpec = JSON.stringify({
|
|
61
|
-
openapi: '3.0.0',
|
|
62
|
-
info: { title: 'Test API', version: '1.0.0' },
|
|
63
|
-
paths: {},
|
|
64
|
-
});
|
|
65
|
-
const result = parser.parse(jsonSpec);
|
|
66
|
-
expect(result).toBeDefined();
|
|
67
|
-
expect(result.openapi).toBe('3.0.0');
|
|
68
|
-
expect(result.info.title).toBe('Test API');
|
|
69
|
-
});
|
|
70
|
-
it('should parse valid YAML OpenAPI spec', () => {
|
|
71
|
-
const yamlSpec = `
|
|
72
|
-
openapi: 3.0.0
|
|
73
|
-
info:
|
|
74
|
-
title: Test API
|
|
75
|
-
version: 1.0.0
|
|
76
|
-
paths: {}
|
|
77
|
-
`;
|
|
78
|
-
const result = parser.parse(yamlSpec);
|
|
79
|
-
expect(result).toBeDefined();
|
|
80
|
-
expect(result.openapi).toBe('3.0.0');
|
|
81
|
-
expect(result.info.title).toBe('Test API');
|
|
82
|
-
});
|
|
83
|
-
it('should parse already parsed objects', () => {
|
|
84
|
-
const spec = {
|
|
85
|
-
openapi: '3.0.0',
|
|
86
|
-
info: { title: 'Test API', version: '1.0.0' },
|
|
87
|
-
paths: {},
|
|
88
|
-
};
|
|
89
|
-
const result = parser.parse(spec);
|
|
90
|
-
expect(result).toBeDefined();
|
|
91
|
-
expect(result.openapi).toBe('3.0.0');
|
|
92
|
-
});
|
|
93
|
-
it('should validate OpenAPI structure', () => {
|
|
94
|
-
const invalidSpec = {
|
|
95
|
-
openapi: '2.0.0', // Wrong version format
|
|
96
|
-
info: { title: 'Test API', version: '1.0.0' },
|
|
97
|
-
paths: {},
|
|
98
|
-
};
|
|
99
|
-
expect(() => parser.parse(invalidSpec)).toThrow();
|
|
100
|
-
});
|
|
101
|
-
it('should handle missing required fields', () => {
|
|
102
|
-
const invalidSpec = {
|
|
103
|
-
openapi: '3.0.0',
|
|
104
|
-
// Missing info
|
|
105
|
-
paths: {},
|
|
106
|
-
};
|
|
107
|
-
expect(() => parser.parse(invalidSpec)).toThrow();
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"generator.test.d.ts","sourceRoot":"","sources":["../../../tests/unit/generator.test.ts"],"names":[],"mappings":""}
|
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
-
import { Generator } from '../../src/generator.js';
|
|
3
|
-
import { Reporter } from '../../src/utils/reporter.js';
|
|
4
|
-
import { readFileSync, existsSync, mkdirSync, rmSync } from 'node:fs';
|
|
5
|
-
import { join } from 'node:path';
|
|
6
|
-
import { fileURLToPath } from 'node:url';
|
|
7
|
-
import { dirname } from 'node:path';
|
|
8
|
-
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
9
|
-
const testOutputDir = join(__dirname, '../../test-output');
|
|
10
|
-
describe('Generator', () => {
|
|
11
|
-
let generator;
|
|
12
|
-
let mockReporter;
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
// Clean up test output directory
|
|
15
|
-
if (existsSync(testOutputDir)) {
|
|
16
|
-
rmSync(testOutputDir, { recursive: true, force: true });
|
|
17
|
-
}
|
|
18
|
-
mkdirSync(testOutputDir, { recursive: true });
|
|
19
|
-
// Create a mock reporter
|
|
20
|
-
mockReporter = {
|
|
21
|
-
log: vi.fn(),
|
|
22
|
-
error: vi.fn(),
|
|
23
|
-
};
|
|
24
|
-
});
|
|
25
|
-
afterEach(() => {
|
|
26
|
-
// Clean up test output directory
|
|
27
|
-
if (existsSync(testOutputDir)) {
|
|
28
|
-
rmSync(testOutputDir, { recursive: true, force: true });
|
|
29
|
-
}
|
|
30
|
-
});
|
|
31
|
-
describe('constructor', () => {
|
|
32
|
-
it('should create a new Generator instance', () => {
|
|
33
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
34
|
-
expect(generator).toBeInstanceOf(Generator);
|
|
35
|
-
});
|
|
36
|
-
it('should initialize with correct parameters', () => {
|
|
37
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
38
|
-
expect(generator).toBeDefined();
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
describe('run', () => {
|
|
42
|
-
it('should be a function', () => {
|
|
43
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
44
|
-
expect(typeof generator.run).toBe('function');
|
|
45
|
-
});
|
|
46
|
-
it('should generate code from a valid OpenAPI file', async () => {
|
|
47
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
48
|
-
const exitCode = await generator.run();
|
|
49
|
-
expect(exitCode).toBe(0);
|
|
50
|
-
expect(mockReporter.log).toHaveBeenCalled();
|
|
51
|
-
expect(mockReporter.error).not.toHaveBeenCalled();
|
|
52
|
-
// Verify output file was created
|
|
53
|
-
const outputFile = join(testOutputDir, 'type.ts');
|
|
54
|
-
expect(existsSync(outputFile)).toBe(true);
|
|
55
|
-
// Verify file contains expected content
|
|
56
|
-
const content = readFileSync(outputFile, 'utf-8');
|
|
57
|
-
expect(content).toMatch(/import\s*{\s*z\s*}\s*from\s*['"]zod['"]/);
|
|
58
|
-
expect(content).toContain('export class');
|
|
59
|
-
expect(content).toContain('getBaseRequestOptions');
|
|
60
|
-
});
|
|
61
|
-
it('should handle invalid file paths gracefully', async () => {
|
|
62
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, './non-existent-file.yaml', testOutputDir);
|
|
63
|
-
const exitCode = await generator.run();
|
|
64
|
-
expect(exitCode).toBe(1);
|
|
65
|
-
expect(mockReporter.error).toHaveBeenCalled();
|
|
66
|
-
});
|
|
67
|
-
it('should handle invalid OpenAPI specifications', async () => {
|
|
68
|
-
// Create a temporary invalid OpenAPI file
|
|
69
|
-
const invalidFile = join(testOutputDir, 'invalid.yaml');
|
|
70
|
-
mkdirSync(testOutputDir, { recursive: true });
|
|
71
|
-
readFileSync; // Ensure we can write
|
|
72
|
-
const { writeFileSync } = await import('node:fs');
|
|
73
|
-
writeFileSync(invalidFile, 'invalid: yaml: content: [unclosed');
|
|
74
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, invalidFile, testOutputDir);
|
|
75
|
-
const exitCode = await generator.run();
|
|
76
|
-
expect(exitCode).toBe(1);
|
|
77
|
-
expect(mockReporter.error).toHaveBeenCalled();
|
|
78
|
-
});
|
|
79
|
-
it('should generate code with correct structure', async () => {
|
|
80
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
81
|
-
await generator.run();
|
|
82
|
-
const outputFile = join(testOutputDir, 'type.ts');
|
|
83
|
-
const content = readFileSync(outputFile, 'utf-8');
|
|
84
|
-
// Check for key components
|
|
85
|
-
expect(content).toMatch(/import\s*{\s*z\s*}\s*from\s*['"]zod['"]/);
|
|
86
|
-
expect(content).toContain('export const');
|
|
87
|
-
expect(content).toContain('protected getBaseRequestOptions');
|
|
88
|
-
expect(content).toContain('async #makeRequest');
|
|
89
|
-
});
|
|
90
|
-
it('should include header comments in generated file', async () => {
|
|
91
|
-
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
92
|
-
await generator.run();
|
|
93
|
-
const outputFile = join(testOutputDir, 'type.ts');
|
|
94
|
-
const content = readFileSync(outputFile, 'utf-8');
|
|
95
|
-
expect(content).toContain('AUTOGENERATED');
|
|
96
|
-
expect(content).toContain('test-app@1.0.0');
|
|
97
|
-
expect(content).toContain('eslint-disable');
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
});
|