zod-codegen 1.0.2 → 1.1.0
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/CHANGELOG.md +13 -0
- package/CONTRIBUTING.md +1 -1
- package/EXAMPLES.md +704 -0
- package/PERFORMANCE.md +59 -0
- package/README.md +270 -58
- package/dist/src/cli.js +25 -5
- package/dist/src/services/code-generator.service.js +211 -26
- package/dist/src/types/openapi.js +1 -1
- package/dist/tests/unit/code-generator.test.js +219 -0
- package/dist/tests/unit/file-reader.test.js +110 -0
- package/dist/tests/unit/generator.test.js +77 -7
- package/eslint.config.mjs +1 -0
- package/examples/.gitkeep +3 -0
- package/examples/README.md +74 -0
- package/examples/petstore/README.md +121 -0
- package/examples/petstore/authenticated-usage.ts +60 -0
- package/examples/petstore/basic-usage.ts +51 -0
- package/examples/petstore/server-variables-usage.ts +63 -0
- package/examples/petstore/type.ts +217 -0
- package/examples/pokeapi/README.md +105 -0
- package/examples/pokeapi/basic-usage.ts +57 -0
- package/examples/pokeapi/custom-client.ts +56 -0
- package/examples/pokeapi/type.ts +109 -0
- package/package.json +4 -2
- package/samples/pokeapi-openapi.json +212 -0
- package/samples/server-variables-example.yaml +49 -0
- package/src/cli.ts +30 -5
- package/src/services/code-generator.service.ts +641 -57
- package/src/types/openapi.ts +1 -1
- package/tests/unit/code-generator.test.ts +243 -0
- package/tests/unit/file-reader.test.ts +134 -0
- package/tests/unit/generator.test.ts +99 -7
- package/tsconfig.examples.json +17 -0
- package/tsconfig.json +1 -1
package/src/types/openapi.ts
CHANGED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import {describe, expect, it} from 'vitest';
|
|
2
|
+
import {TypeScriptCodeGeneratorService} from '../../src/services/code-generator.service.js';
|
|
3
|
+
import type {OpenApiSpecType} from '../../src/types/openapi.js';
|
|
4
|
+
|
|
5
|
+
describe('TypeScriptCodeGeneratorService', () => {
|
|
6
|
+
let generator: TypeScriptCodeGeneratorService;
|
|
7
|
+
|
|
8
|
+
beforeEach(() => {
|
|
9
|
+
generator = new TypeScriptCodeGeneratorService();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('generate', () => {
|
|
13
|
+
it('should generate code for a minimal OpenAPI spec', () => {
|
|
14
|
+
const spec: OpenApiSpecType = {
|
|
15
|
+
openapi: '3.0.0',
|
|
16
|
+
info: {
|
|
17
|
+
title: 'Test API',
|
|
18
|
+
version: '1.0.0',
|
|
19
|
+
},
|
|
20
|
+
paths: {},
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const code = generator.generate(spec);
|
|
24
|
+
expect(code).toBeTruthy();
|
|
25
|
+
expect(typeof code).toBe('string');
|
|
26
|
+
expect(code).toMatch(/import\s*{\s*z\s*}\s*from\s*['"]zod['"]/);
|
|
27
|
+
expect(code).toContain('export class');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should generate schemas for component schemas', () => {
|
|
31
|
+
const spec: OpenApiSpecType = {
|
|
32
|
+
openapi: '3.0.0',
|
|
33
|
+
info: {
|
|
34
|
+
title: 'Test API',
|
|
35
|
+
version: '1.0.0',
|
|
36
|
+
},
|
|
37
|
+
paths: {},
|
|
38
|
+
components: {
|
|
39
|
+
schemas: {
|
|
40
|
+
User: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
id: {type: 'integer'},
|
|
44
|
+
name: {type: 'string'},
|
|
45
|
+
},
|
|
46
|
+
required: ['id', 'name'],
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const code = generator.generate(spec);
|
|
53
|
+
expect(code).toContain('export const User');
|
|
54
|
+
expect(code).toContain('z.object');
|
|
55
|
+
expect(code).toContain('z.number().int()');
|
|
56
|
+
expect(code).toContain('z.string()');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should generate client methods for paths', () => {
|
|
60
|
+
const spec: OpenApiSpecType = {
|
|
61
|
+
openapi: '3.0.0',
|
|
62
|
+
info: {
|
|
63
|
+
title: 'Test API',
|
|
64
|
+
version: '1.0.0',
|
|
65
|
+
},
|
|
66
|
+
paths: {
|
|
67
|
+
'/users': {
|
|
68
|
+
get: {
|
|
69
|
+
operationId: 'getUsers',
|
|
70
|
+
responses: {
|
|
71
|
+
'200': {
|
|
72
|
+
description: 'Success',
|
|
73
|
+
content: {
|
|
74
|
+
'application/json': {
|
|
75
|
+
schema: {
|
|
76
|
+
type: 'array',
|
|
77
|
+
items: {type: 'string'},
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const code = generator.generate(spec);
|
|
89
|
+
expect(code).toContain('async getUsers');
|
|
90
|
+
expect(code).toContain('#makeRequest');
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('should generate getBaseRequestOptions method', () => {
|
|
94
|
+
const spec: OpenApiSpecType = {
|
|
95
|
+
openapi: '3.0.0',
|
|
96
|
+
info: {
|
|
97
|
+
title: 'Test API',
|
|
98
|
+
version: '1.0.0',
|
|
99
|
+
},
|
|
100
|
+
paths: {},
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const code = generator.generate(spec);
|
|
104
|
+
expect(code).toContain('protected getBaseRequestOptions');
|
|
105
|
+
expect(code).toContain('Partial<Omit<RequestInit');
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('should handle servers configuration', () => {
|
|
109
|
+
const spec: OpenApiSpecType = {
|
|
110
|
+
openapi: '3.0.0',
|
|
111
|
+
info: {
|
|
112
|
+
title: 'Test API',
|
|
113
|
+
version: '1.0.0',
|
|
114
|
+
},
|
|
115
|
+
servers: [{url: 'https://api.example.com'}],
|
|
116
|
+
paths: {},
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
const code = generator.generate(spec);
|
|
120
|
+
expect(code).toContain('defaultBaseUrl');
|
|
121
|
+
expect(code).toContain('https://api.example.com');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should handle complex schemas with references', () => {
|
|
125
|
+
const spec: OpenApiSpecType = {
|
|
126
|
+
openapi: '3.0.0',
|
|
127
|
+
info: {
|
|
128
|
+
title: 'Test API',
|
|
129
|
+
version: '1.0.0',
|
|
130
|
+
},
|
|
131
|
+
paths: {},
|
|
132
|
+
components: {
|
|
133
|
+
schemas: {
|
|
134
|
+
User: {
|
|
135
|
+
type: 'object',
|
|
136
|
+
properties: {
|
|
137
|
+
id: {type: 'integer'},
|
|
138
|
+
profile: {$ref: '#/components/schemas/Profile'},
|
|
139
|
+
},
|
|
140
|
+
required: ['id'],
|
|
141
|
+
},
|
|
142
|
+
Profile: {
|
|
143
|
+
type: 'object',
|
|
144
|
+
properties: {
|
|
145
|
+
name: {type: 'string'},
|
|
146
|
+
email: {type: 'string', format: 'email'},
|
|
147
|
+
},
|
|
148
|
+
required: ['name', 'email'],
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const code = generator.generate(spec);
|
|
155
|
+
expect(code).toContain('export const User');
|
|
156
|
+
expect(code).toContain('export const Profile');
|
|
157
|
+
// Profile should be defined before User (topological sort)
|
|
158
|
+
const profileIndex = code.indexOf('Profile');
|
|
159
|
+
const userIndex = code.indexOf('User');
|
|
160
|
+
expect(profileIndex).toBeLessThan(userIndex);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it('should handle enum types', () => {
|
|
164
|
+
const spec: OpenApiSpecType = {
|
|
165
|
+
openapi: '3.0.0',
|
|
166
|
+
info: {
|
|
167
|
+
title: 'Test API',
|
|
168
|
+
version: '1.0.0',
|
|
169
|
+
},
|
|
170
|
+
paths: {},
|
|
171
|
+
components: {
|
|
172
|
+
schemas: {
|
|
173
|
+
Status: {
|
|
174
|
+
type: 'string',
|
|
175
|
+
enum: ['active', 'inactive', 'pending'],
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const code = generator.generate(spec);
|
|
182
|
+
expect(code).toContain('z.enum');
|
|
183
|
+
expect(code).toContain('active');
|
|
184
|
+
expect(code).toContain('inactive');
|
|
185
|
+
expect(code).toContain('pending');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('should handle array types', () => {
|
|
189
|
+
const spec: OpenApiSpecType = {
|
|
190
|
+
openapi: '3.0.0',
|
|
191
|
+
info: {
|
|
192
|
+
title: 'Test API',
|
|
193
|
+
version: '1.0.0',
|
|
194
|
+
},
|
|
195
|
+
paths: {},
|
|
196
|
+
components: {
|
|
197
|
+
schemas: {
|
|
198
|
+
Tags: {
|
|
199
|
+
type: 'array',
|
|
200
|
+
items: {type: 'string'},
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const code = generator.generate(spec);
|
|
207
|
+
expect(code).toContain('z.array');
|
|
208
|
+
expect(code).toContain('z.string()');
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
describe('buildSchema', () => {
|
|
213
|
+
it('should build schema for string type', () => {
|
|
214
|
+
const schema = {type: 'string'};
|
|
215
|
+
const result = generator.buildSchema(schema, true);
|
|
216
|
+
expect(result).toBeDefined();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should build schema for number type', () => {
|
|
220
|
+
const schema = {type: 'number'};
|
|
221
|
+
const result = generator.buildSchema(schema, true);
|
|
222
|
+
expect(result).toBeDefined();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should build schema for boolean type', () => {
|
|
226
|
+
const schema = {type: 'boolean'};
|
|
227
|
+
const result = generator.buildSchema(schema, true);
|
|
228
|
+
expect(result).toBeDefined();
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('should handle optional fields', () => {
|
|
232
|
+
const schema = {type: 'string'};
|
|
233
|
+
const result = generator.buildSchema(schema, false);
|
|
234
|
+
expect(result).toBeDefined();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('should handle string formats', () => {
|
|
238
|
+
const schema = {type: 'string', format: 'email'};
|
|
239
|
+
const result = generator.buildSchema(schema, true);
|
|
240
|
+
expect(result).toBeDefined();
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
});
|
|
@@ -0,0 +1,134 @@
|
|
|
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
|
+
|
|
7
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
8
|
+
|
|
9
|
+
describe('SyncFileReaderService', () => {
|
|
10
|
+
let reader: SyncFileReaderService;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
reader = new SyncFileReaderService();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('readFile', () => {
|
|
17
|
+
it('should read local JSON files', async () => {
|
|
18
|
+
const content = await reader.readFile(join(__dirname, '../../samples/openapi.json'));
|
|
19
|
+
expect(content).toBeTruthy();
|
|
20
|
+
expect(typeof content).toBe('string');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('should read local YAML files', async () => {
|
|
24
|
+
const content = await reader.readFile(join(__dirname, '../../samples/swagger-petstore.yaml'));
|
|
25
|
+
expect(content).toBeTruthy();
|
|
26
|
+
expect(typeof content).toBe('string');
|
|
27
|
+
expect(content).toContain('openapi:');
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('should handle non-existent files', async () => {
|
|
31
|
+
await expect(reader.readFile('./non-existent-file.json')).rejects.toThrow();
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('should handle URLs', async () => {
|
|
35
|
+
// Mock fetch for URL test
|
|
36
|
+
const originalFetch = global.fetch;
|
|
37
|
+
global.fetch = vi.fn().mockResolvedValue({
|
|
38
|
+
ok: true,
|
|
39
|
+
text: async () => '{"openapi": "3.0.0", "info": {"title": "Test", "version": "1.0.0"}, "paths": {}}',
|
|
40
|
+
} as Response);
|
|
41
|
+
|
|
42
|
+
const content = await reader.readFile('https://example.com/openapi.json');
|
|
43
|
+
expect(content).toBeTruthy();
|
|
44
|
+
|
|
45
|
+
global.fetch = originalFetch;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should handle URL fetch errors with non-ok responses', async () => {
|
|
49
|
+
const originalFetch = global.fetch;
|
|
50
|
+
const mockFetch = vi.fn().mockResolvedValue({
|
|
51
|
+
ok: false,
|
|
52
|
+
status: 404,
|
|
53
|
+
statusText: 'Not Found',
|
|
54
|
+
} as Response);
|
|
55
|
+
global.fetch = mockFetch;
|
|
56
|
+
|
|
57
|
+
await expect(reader.readFile('https://example.com/not-found.json')).rejects.toThrow();
|
|
58
|
+
|
|
59
|
+
// Verify fetch was called
|
|
60
|
+
expect(mockFetch).toHaveBeenCalledWith('https://example.com/not-found.json');
|
|
61
|
+
global.fetch = originalFetch;
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
describe('OpenApiFileParserService', () => {
|
|
67
|
+
let parser: OpenApiFileParserService;
|
|
68
|
+
|
|
69
|
+
beforeEach(() => {
|
|
70
|
+
parser = new OpenApiFileParserService();
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('parse', () => {
|
|
74
|
+
it('should parse valid JSON OpenAPI spec', () => {
|
|
75
|
+
const jsonSpec = JSON.stringify({
|
|
76
|
+
openapi: '3.0.0',
|
|
77
|
+
info: {title: 'Test API', version: '1.0.0'},
|
|
78
|
+
paths: {},
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const result = parser.parse(jsonSpec);
|
|
82
|
+
expect(result).toBeDefined();
|
|
83
|
+
expect(result.openapi).toBe('3.0.0');
|
|
84
|
+
expect(result.info.title).toBe('Test API');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should parse valid YAML OpenAPI spec', () => {
|
|
88
|
+
const yamlSpec = `
|
|
89
|
+
openapi: 3.0.0
|
|
90
|
+
info:
|
|
91
|
+
title: Test API
|
|
92
|
+
version: 1.0.0
|
|
93
|
+
paths: {}
|
|
94
|
+
`;
|
|
95
|
+
|
|
96
|
+
const result = parser.parse(yamlSpec);
|
|
97
|
+
expect(result).toBeDefined();
|
|
98
|
+
expect(result.openapi).toBe('3.0.0');
|
|
99
|
+
expect(result.info.title).toBe('Test API');
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('should parse already parsed objects', () => {
|
|
103
|
+
const spec = {
|
|
104
|
+
openapi: '3.0.0',
|
|
105
|
+
info: {title: 'Test API', version: '1.0.0'},
|
|
106
|
+
paths: {},
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const result = parser.parse(spec);
|
|
110
|
+
expect(result).toBeDefined();
|
|
111
|
+
expect(result.openapi).toBe('3.0.0');
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it('should validate OpenAPI structure', () => {
|
|
115
|
+
const invalidSpec = {
|
|
116
|
+
openapi: '2.0.0', // Wrong version format
|
|
117
|
+
info: {title: 'Test API', version: '1.0.0'},
|
|
118
|
+
paths: {},
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
expect(() => parser.parse(invalidSpec)).toThrow();
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it('should handle missing required fields', () => {
|
|
125
|
+
const invalidSpec = {
|
|
126
|
+
openapi: '3.0.0',
|
|
127
|
+
// Missing info
|
|
128
|
+
paths: {},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
expect(() => parser.parse(invalidSpec)).toThrow();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
@@ -1,36 +1,128 @@
|
|
|
1
|
-
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
|
1
|
+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
|
|
2
2
|
import {Generator} from '../../src/generator.js';
|
|
3
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
|
+
|
|
9
|
+
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
10
|
+
const testOutputDir = join(__dirname, '../../test-output');
|
|
4
11
|
|
|
5
12
|
describe('Generator', () => {
|
|
6
13
|
let generator: Generator;
|
|
7
14
|
let mockReporter: Reporter;
|
|
8
15
|
|
|
9
16
|
beforeEach(() => {
|
|
17
|
+
// Clean up test output directory
|
|
18
|
+
if (existsSync(testOutputDir)) {
|
|
19
|
+
rmSync(testOutputDir, {recursive: true, force: true});
|
|
20
|
+
}
|
|
21
|
+
mkdirSync(testOutputDir, {recursive: true});
|
|
22
|
+
|
|
10
23
|
// Create a mock reporter
|
|
11
24
|
mockReporter = {
|
|
12
|
-
|
|
25
|
+
log: vi.fn(),
|
|
13
26
|
error: vi.fn(),
|
|
14
|
-
warn: vi.fn(),
|
|
15
|
-
success: vi.fn(),
|
|
16
27
|
} as unknown as Reporter;
|
|
28
|
+
});
|
|
17
29
|
|
|
18
|
-
|
|
30
|
+
afterEach(() => {
|
|
31
|
+
// Clean up test output directory
|
|
32
|
+
if (existsSync(testOutputDir)) {
|
|
33
|
+
rmSync(testOutputDir, {recursive: true, force: true});
|
|
34
|
+
}
|
|
19
35
|
});
|
|
20
36
|
|
|
21
37
|
describe('constructor', () => {
|
|
22
38
|
it('should create a new Generator instance', () => {
|
|
39
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
23
40
|
expect(generator).toBeInstanceOf(Generator);
|
|
24
41
|
});
|
|
42
|
+
|
|
43
|
+
it('should initialize with correct parameters', () => {
|
|
44
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
45
|
+
expect(generator).toBeDefined();
|
|
46
|
+
});
|
|
25
47
|
});
|
|
26
48
|
|
|
27
49
|
describe('run', () => {
|
|
28
50
|
it('should be a function', () => {
|
|
51
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
29
52
|
expect(typeof generator.run).toBe('function');
|
|
30
53
|
});
|
|
31
54
|
|
|
32
|
-
it('should
|
|
33
|
-
|
|
55
|
+
it('should generate code from a valid OpenAPI file', async () => {
|
|
56
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
57
|
+
|
|
58
|
+
const exitCode = await generator.run();
|
|
59
|
+
|
|
60
|
+
expect(exitCode).toBe(0);
|
|
61
|
+
expect(mockReporter.log).toHaveBeenCalled();
|
|
62
|
+
expect(mockReporter.error).not.toHaveBeenCalled();
|
|
63
|
+
|
|
64
|
+
// Verify output file was created
|
|
65
|
+
const outputFile = join(testOutputDir, 'type.ts');
|
|
66
|
+
expect(existsSync(outputFile)).toBe(true);
|
|
67
|
+
|
|
68
|
+
// Verify file contains expected content
|
|
69
|
+
const content = readFileSync(outputFile, 'utf-8');
|
|
70
|
+
expect(content).toMatch(/import\s*{\s*z\s*}\s*from\s*['"]zod['"]/);
|
|
71
|
+
expect(content).toContain('export class');
|
|
72
|
+
expect(content).toContain('getBaseRequestOptions');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('should handle invalid file paths gracefully', async () => {
|
|
76
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, './non-existent-file.yaml', testOutputDir);
|
|
77
|
+
|
|
78
|
+
const exitCode = await generator.run();
|
|
79
|
+
|
|
80
|
+
expect(exitCode).toBe(1);
|
|
81
|
+
expect(mockReporter.error).toHaveBeenCalled();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('should handle invalid OpenAPI specifications', async () => {
|
|
85
|
+
// Create a temporary invalid OpenAPI file
|
|
86
|
+
const invalidFile = join(testOutputDir, 'invalid.yaml');
|
|
87
|
+
mkdirSync(testOutputDir, {recursive: true});
|
|
88
|
+
readFileSync; // Ensure we can write
|
|
89
|
+
const {writeFileSync} = await import('node:fs');
|
|
90
|
+
writeFileSync(invalidFile, 'invalid: yaml: content: [unclosed');
|
|
91
|
+
|
|
92
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, invalidFile, testOutputDir);
|
|
93
|
+
|
|
94
|
+
const exitCode = await generator.run();
|
|
95
|
+
|
|
96
|
+
expect(exitCode).toBe(1);
|
|
97
|
+
expect(mockReporter.error).toHaveBeenCalled();
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should generate code with correct structure', async () => {
|
|
101
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
102
|
+
|
|
103
|
+
await generator.run();
|
|
104
|
+
|
|
105
|
+
const outputFile = join(testOutputDir, 'type.ts');
|
|
106
|
+
const content = readFileSync(outputFile, 'utf-8');
|
|
107
|
+
|
|
108
|
+
// Check for key components
|
|
109
|
+
expect(content).toMatch(/import\s*{\s*z\s*}\s*from\s*['"]zod['"]/);
|
|
110
|
+
expect(content).toContain('export const');
|
|
111
|
+
expect(content).toContain('protected getBaseRequestOptions');
|
|
112
|
+
expect(content).toContain('async #makeRequest');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should include header comments in generated file', async () => {
|
|
116
|
+
generator = new Generator('test-app', '1.0.0', mockReporter, './samples/swagger-petstore.yaml', testOutputDir);
|
|
117
|
+
|
|
118
|
+
await generator.run();
|
|
119
|
+
|
|
120
|
+
const outputFile = join(testOutputDir, 'type.ts');
|
|
121
|
+
const content = readFileSync(outputFile, 'utf-8');
|
|
122
|
+
|
|
123
|
+
expect(content).toContain('AUTOGENERATED');
|
|
124
|
+
expect(content).toContain('test-app@1.0.0');
|
|
125
|
+
expect(content).toContain('eslint-disable');
|
|
34
126
|
});
|
|
35
127
|
});
|
|
36
128
|
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"noEmit": true,
|
|
5
|
+
"strict": false,
|
|
6
|
+
"noImplicitAny": false,
|
|
7
|
+
"noImplicitOverride": false,
|
|
8
|
+
"strictNullChecks": false,
|
|
9
|
+
"strictPropertyInitialization": false,
|
|
10
|
+
"noUnusedLocals": false,
|
|
11
|
+
"noUnusedParameters": false,
|
|
12
|
+
"exactOptionalPropertyTypes": false,
|
|
13
|
+
"verbatimModuleSyntax": false
|
|
14
|
+
},
|
|
15
|
+
"include": ["examples/**/*.ts"],
|
|
16
|
+
"exclude": []
|
|
17
|
+
}
|
package/tsconfig.json
CHANGED
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
"useUnknownInCatchVariables": true,
|
|
40
40
|
"verbatimModuleSyntax": true
|
|
41
41
|
},
|
|
42
|
-
"exclude": ["node_modules", "dist", "build"],
|
|
42
|
+
"exclude": ["node_modules", "dist", "build", "examples/**/*.ts"],
|
|
43
43
|
"include": ["src/**/*.ts", "scripts/**/*.ts", "tests/**/*.ts", "vitest.config.ts"]
|
|
44
44
|
}
|