@geekmidas/cli 0.0.26 → 0.2.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/FUNCTION_CRON_SUPPORT.md +266 -0
- package/README.md +84 -17
- package/dist/CronGenerator-1PflEYe2.cjs +60 -0
- package/dist/CronGenerator-1PflEYe2.cjs.map +1 -0
- package/dist/CronGenerator-DXRfHQcV.mjs +54 -0
- package/dist/CronGenerator-DXRfHQcV.mjs.map +1 -0
- package/dist/EndpointGenerator-BbGrDiCP.cjs +264 -0
- package/dist/EndpointGenerator-BbGrDiCP.cjs.map +1 -0
- package/dist/EndpointGenerator-BmZ9BxbO.mjs +258 -0
- package/dist/EndpointGenerator-BmZ9BxbO.mjs.map +1 -0
- package/dist/FunctionGenerator-Clw64SwQ.cjs +59 -0
- package/dist/FunctionGenerator-Clw64SwQ.cjs.map +1 -0
- package/dist/FunctionGenerator-DOEB_yPh.mjs +53 -0
- package/dist/FunctionGenerator-DOEB_yPh.mjs.map +1 -0
- package/dist/Generator-CDoEXCDg.cjs +47 -0
- package/dist/Generator-CDoEXCDg.cjs.map +1 -0
- package/dist/Generator-UanJW0_V.mjs +41 -0
- package/dist/Generator-UanJW0_V.mjs.map +1 -0
- package/dist/SubscriberGenerator-BfMZCVNy.cjs +204 -0
- package/dist/SubscriberGenerator-BfMZCVNy.cjs.map +1 -0
- package/dist/SubscriberGenerator-D2u00NI3.mjs +198 -0
- package/dist/SubscriberGenerator-D2u00NI3.mjs.map +1 -0
- package/dist/build/index.cjs +12 -0
- package/dist/build/index.mjs +12 -0
- package/dist/build/manifests.cjs +3 -0
- package/dist/build/manifests.mjs +3 -0
- package/dist/build/providerResolver.cjs +5 -0
- package/dist/build/providerResolver.mjs +3 -0
- package/dist/build/types.cjs +0 -0
- package/dist/build/types.mjs +0 -0
- package/dist/build-BBhlEjf5.cjs +89 -0
- package/dist/build-BBhlEjf5.cjs.map +1 -0
- package/dist/build-kY-lG30Q.mjs +83 -0
- package/dist/build-kY-lG30Q.mjs.map +1 -0
- package/dist/config-D1EpSGk6.cjs +36 -0
- package/dist/config-D1EpSGk6.cjs.map +1 -0
- package/dist/config-U-mdW-7Y.mjs +30 -0
- package/dist/config-U-mdW-7Y.mjs.map +1 -0
- package/dist/config.cjs +1 -1
- package/dist/config.mjs +1 -1
- package/dist/generators/CronGenerator.cjs +4 -0
- package/dist/generators/CronGenerator.mjs +4 -0
- package/dist/generators/EndpointGenerator.cjs +4 -0
- package/dist/generators/EndpointGenerator.mjs +4 -0
- package/dist/generators/FunctionGenerator.cjs +4 -0
- package/dist/generators/FunctionGenerator.mjs +4 -0
- package/dist/generators/Generator.cjs +3 -0
- package/dist/generators/Generator.mjs +3 -0
- package/dist/generators/SubscriberGenerator.cjs +4 -0
- package/dist/generators/SubscriberGenerator.mjs +4 -0
- package/dist/generators/index.cjs +12 -0
- package/dist/generators/index.mjs +8 -0
- package/dist/generators-CEKtVh81.cjs +0 -0
- package/dist/generators-CsLujGXs.mjs +0 -0
- package/dist/index.cjs +71 -25
- package/dist/index.cjs.map +1 -0
- package/dist/index.mjs +71 -25
- package/dist/index.mjs.map +1 -0
- package/dist/manifests-BrJXpHrf.mjs +21 -0
- package/dist/manifests-BrJXpHrf.mjs.map +1 -0
- package/dist/manifests-D0saShvH.cjs +27 -0
- package/dist/manifests-D0saShvH.cjs.map +1 -0
- package/dist/{openapi-CksVdkh2.mjs → openapi-BQx3_JbM.mjs} +8 -6
- package/dist/openapi-BQx3_JbM.mjs.map +1 -0
- package/dist/{openapi-D4QQJUPY.cjs → openapi-CMLr04cz.cjs} +9 -7
- package/dist/openapi-CMLr04cz.cjs.map +1 -0
- package/dist/{openapi-react-query-DpT3XHFC.mjs → openapi-react-query-DbrWwQzb.mjs} +5 -3
- package/dist/openapi-react-query-DbrWwQzb.mjs.map +1 -0
- package/dist/{openapi-react-query-C1JLYUOs.cjs → openapi-react-query-Dvjqx_Eo.cjs} +5 -3
- package/dist/openapi-react-query-Dvjqx_Eo.cjs.map +1 -0
- package/dist/openapi-react-query.cjs +1 -1
- package/dist/openapi-react-query.mjs +1 -1
- package/dist/openapi.cjs +4 -3
- package/dist/openapi.mjs +4 -3
- package/dist/providerResolver-B_TjNF0_.mjs +96 -0
- package/dist/providerResolver-B_TjNF0_.mjs.map +1 -0
- package/dist/providerResolver-DgvzNfP4.cjs +114 -0
- package/dist/providerResolver-DgvzNfP4.cjs.map +1 -0
- package/examples/cron-example.ts +45 -0
- package/examples/function-example.ts +40 -0
- package/examples/gkm.config.json +22 -0
- package/examples/gkm.minimal.config.json +7 -0
- package/examples/gkm.production.config.json +27 -0
- package/examples/logger.ts +1 -1
- package/package.json +38 -14
- package/src/__tests__/config.spec.ts +110 -0
- package/src/__tests__/openapi-react-query.spec.ts +506 -0
- package/src/__tests__/openapi.spec.ts +362 -0
- package/src/__tests__/test-helpers.ts +180 -0
- package/src/build/__tests__/index-new.spec.ts +577 -0
- package/src/build/index.ts +197 -0
- package/src/build/manifests.ts +35 -0
- package/src/build/providerResolver.ts +184 -0
- package/src/build/types.ts +37 -0
- package/src/config.ts +14 -6
- package/src/generators/CronGenerator.ts +98 -0
- package/src/generators/EndpointGenerator.ts +389 -0
- package/src/generators/FunctionGenerator.ts +97 -0
- package/src/generators/Generator.ts +95 -0
- package/src/generators/SubscriberGenerator.ts +271 -0
- package/src/generators/__tests__/CronGenerator.spec.ts +445 -0
- package/src/generators/__tests__/EndpointGenerator.spec.ts +394 -0
- package/src/generators/__tests__/FunctionGenerator.spec.ts +256 -0
- package/src/generators/__tests__/SubscriberGenerator.spec.ts +341 -0
- package/src/generators/index.ts +9 -0
- package/src/index.ts +57 -22
- package/src/openapi-react-query.ts +2 -1
- package/src/openapi.ts +5 -4
- package/src/types.ts +91 -2
- package/dist/build-BTggTCYL.cjs +0 -176
- package/dist/build-Ca4P6_lY.mjs +0 -170
- package/dist/build.cjs +0 -5
- package/dist/build.mjs +0 -5
- package/dist/config-BNqUMsvc.cjs +0 -24
- package/dist/config-BciAdY6_.mjs +0 -18
- package/dist/loadEndpoints-BBIavB9h.cjs +0 -37
- package/dist/loadEndpoints-DAZ53Og2.mjs +0 -31
- package/dist/loadEndpoints.cjs +0 -3
- package/dist/loadEndpoints.mjs +0 -3
- package/src/build.ts +0 -305
- package/src/loadEndpoints.ts +0 -48
|
@@ -0,0 +1,506 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { readFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
import { generateReactQueryCommand } from '../openapi-react-query';
|
|
6
|
+
import { cleanupDir, createTempDir, createTestFile } from './test-helpers';
|
|
7
|
+
|
|
8
|
+
describe('React Query Generation', () => {
|
|
9
|
+
let tempDir: string;
|
|
10
|
+
|
|
11
|
+
beforeEach(async () => {
|
|
12
|
+
tempDir = await createTempDir('react-query-test-');
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
afterEach(async () => {
|
|
16
|
+
await cleanupDir(tempDir);
|
|
17
|
+
vi.restoreAllMocks();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe('generateReactQueryCommand', () => {
|
|
21
|
+
it('should generate React Query hooks from OpenAPI spec', async () => {
|
|
22
|
+
// Create mock OpenAPI spec
|
|
23
|
+
const spec = {
|
|
24
|
+
openapi: '3.0.0',
|
|
25
|
+
info: {
|
|
26
|
+
title: 'Test API',
|
|
27
|
+
version: '1.0.0',
|
|
28
|
+
},
|
|
29
|
+
paths: {
|
|
30
|
+
'/users': {
|
|
31
|
+
get: {
|
|
32
|
+
operationId: 'getUsers',
|
|
33
|
+
responses: {
|
|
34
|
+
'200': {
|
|
35
|
+
description: 'Success',
|
|
36
|
+
content: {
|
|
37
|
+
'application/json': {
|
|
38
|
+
schema: {
|
|
39
|
+
type: 'array',
|
|
40
|
+
items: {
|
|
41
|
+
type: 'object',
|
|
42
|
+
properties: {
|
|
43
|
+
id: { type: 'string' },
|
|
44
|
+
name: { type: 'string' },
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
post: {
|
|
54
|
+
operationId: 'createUser',
|
|
55
|
+
requestBody: {
|
|
56
|
+
content: {
|
|
57
|
+
'application/json': {
|
|
58
|
+
schema: {
|
|
59
|
+
type: 'object',
|
|
60
|
+
properties: {
|
|
61
|
+
name: { type: 'string' },
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
responses: {
|
|
68
|
+
'201': {
|
|
69
|
+
description: 'Created',
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
78
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
79
|
+
|
|
80
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
81
|
+
|
|
82
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
83
|
+
|
|
84
|
+
await generateReactQueryCommand({
|
|
85
|
+
input: inputPath,
|
|
86
|
+
output: outputPath,
|
|
87
|
+
name: 'TestAPI',
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Verify hooks file was created
|
|
91
|
+
expect(existsSync(outputPath)).toBe(true);
|
|
92
|
+
|
|
93
|
+
// Verify content
|
|
94
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
95
|
+
|
|
96
|
+
expect(content).toContain('createTypedQueryClient');
|
|
97
|
+
expect(content).toContain('testapi');
|
|
98
|
+
expect(content).toContain('useGetUsers');
|
|
99
|
+
expect(content).toContain('useCreateUser');
|
|
100
|
+
expect(content).toContain('export');
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should throw error when OpenAPI spec not found', async () => {
|
|
104
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
105
|
+
|
|
106
|
+
await expect(
|
|
107
|
+
generateReactQueryCommand({
|
|
108
|
+
input: join(tempDir, 'nonexistent.json'),
|
|
109
|
+
}),
|
|
110
|
+
).rejects.toThrow(/OpenAPI spec not found/);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should use default input and output paths', async () => {
|
|
114
|
+
// Create spec in default location
|
|
115
|
+
const spec = {
|
|
116
|
+
openapi: '3.0.0',
|
|
117
|
+
info: { title: 'API', version: '1.0.0' },
|
|
118
|
+
paths: {
|
|
119
|
+
'/test': {
|
|
120
|
+
get: {
|
|
121
|
+
operationId: 'test',
|
|
122
|
+
responses: { '200': { description: 'OK' } },
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
129
|
+
|
|
130
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
131
|
+
|
|
132
|
+
await generateReactQueryCommand();
|
|
133
|
+
|
|
134
|
+
// Should create in default location
|
|
135
|
+
const defaultPath = join(tempDir, 'src', 'api', 'hooks.ts');
|
|
136
|
+
expect(existsSync(defaultPath)).toBe(true);
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should generate query hooks for GET requests', async () => {
|
|
140
|
+
const spec = {
|
|
141
|
+
openapi: '3.0.0',
|
|
142
|
+
info: { title: 'API', version: '1.0.0' },
|
|
143
|
+
paths: {
|
|
144
|
+
'/users': {
|
|
145
|
+
get: {
|
|
146
|
+
operationId: 'listUsers',
|
|
147
|
+
responses: { '200': { description: 'Success' } },
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
'/users/{id}': {
|
|
151
|
+
get: {
|
|
152
|
+
operationId: 'getUser',
|
|
153
|
+
parameters: [
|
|
154
|
+
{
|
|
155
|
+
name: 'id',
|
|
156
|
+
in: 'path',
|
|
157
|
+
required: true,
|
|
158
|
+
schema: { type: 'string' },
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
responses: { '200': { description: 'Success' } },
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
},
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
168
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
169
|
+
|
|
170
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
171
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
172
|
+
|
|
173
|
+
await generateReactQueryCommand({
|
|
174
|
+
input: inputPath,
|
|
175
|
+
output: outputPath,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
179
|
+
|
|
180
|
+
expect(content).toContain('useListUsers');
|
|
181
|
+
expect(content).toContain('useGetUser');
|
|
182
|
+
expect(content).toContain('Query Hooks');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('should generate mutation hooks for non-GET requests', async () => {
|
|
186
|
+
const spec = {
|
|
187
|
+
openapi: '3.0.0',
|
|
188
|
+
info: { title: 'API', version: '1.0.0' },
|
|
189
|
+
paths: {
|
|
190
|
+
'/users': {
|
|
191
|
+
post: {
|
|
192
|
+
operationId: 'createUser',
|
|
193
|
+
responses: { '201': { description: 'Created' } },
|
|
194
|
+
},
|
|
195
|
+
},
|
|
196
|
+
'/users/{id}': {
|
|
197
|
+
put: {
|
|
198
|
+
operationId: 'updateUser',
|
|
199
|
+
responses: { '200': { description: 'Updated' } },
|
|
200
|
+
},
|
|
201
|
+
delete: {
|
|
202
|
+
operationId: 'deleteUser',
|
|
203
|
+
responses: { '204': { description: 'Deleted' } },
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
210
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
211
|
+
|
|
212
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
213
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
214
|
+
|
|
215
|
+
await generateReactQueryCommand({
|
|
216
|
+
input: inputPath,
|
|
217
|
+
output: outputPath,
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
221
|
+
|
|
222
|
+
expect(content).toContain('useCreateUser');
|
|
223
|
+
expect(content).toContain('useUpdateUser');
|
|
224
|
+
expect(content).toContain('useDeleteUser');
|
|
225
|
+
expect(content).toContain('Mutation Hooks');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('should create output directory if it does not exist', async () => {
|
|
229
|
+
const spec = {
|
|
230
|
+
openapi: '3.0.0',
|
|
231
|
+
info: { title: 'API', version: '1.0.0' },
|
|
232
|
+
paths: {
|
|
233
|
+
'/test': {
|
|
234
|
+
get: {
|
|
235
|
+
operationId: 'test',
|
|
236
|
+
responses: { '200': { description: 'OK' } },
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
243
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
244
|
+
|
|
245
|
+
const outputPath = join(tempDir, 'nested', 'dir', 'hooks.ts');
|
|
246
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
247
|
+
|
|
248
|
+
await generateReactQueryCommand({
|
|
249
|
+
input: inputPath,
|
|
250
|
+
output: outputPath,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
expect(existsSync(outputPath)).toBe(true);
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
it('should generate TypeScript types file', async () => {
|
|
257
|
+
const spec = {
|
|
258
|
+
openapi: '3.0.0',
|
|
259
|
+
info: { title: 'API', version: '1.0.0' },
|
|
260
|
+
paths: {
|
|
261
|
+
'/test': {
|
|
262
|
+
get: {
|
|
263
|
+
operationId: 'test',
|
|
264
|
+
responses: { '200': { description: 'OK' } },
|
|
265
|
+
},
|
|
266
|
+
},
|
|
267
|
+
},
|
|
268
|
+
};
|
|
269
|
+
|
|
270
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
271
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
272
|
+
|
|
273
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
274
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
275
|
+
|
|
276
|
+
await generateReactQueryCommand({
|
|
277
|
+
input: inputPath,
|
|
278
|
+
output: outputPath,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
// Should generate openapi-types.d.ts
|
|
282
|
+
const typesPath = join(tempDir, 'openapi-types.d.ts');
|
|
283
|
+
expect(existsSync(typesPath)).toBe(true);
|
|
284
|
+
|
|
285
|
+
const typesContent = await readFile(typesPath, 'utf-8');
|
|
286
|
+
expect(typesContent).toContain('export interface paths');
|
|
287
|
+
});
|
|
288
|
+
|
|
289
|
+
it('should handle operations without operationId', async () => {
|
|
290
|
+
const spec = {
|
|
291
|
+
openapi: '3.0.0',
|
|
292
|
+
info: { title: 'API', version: '1.0.0' },
|
|
293
|
+
paths: {
|
|
294
|
+
'/with-id': {
|
|
295
|
+
get: {
|
|
296
|
+
operationId: 'withId',
|
|
297
|
+
responses: { '200': { description: 'OK' } },
|
|
298
|
+
},
|
|
299
|
+
},
|
|
300
|
+
'/without-id': {
|
|
301
|
+
get: {
|
|
302
|
+
// No operationId
|
|
303
|
+
responses: { '200': { description: 'OK' } },
|
|
304
|
+
},
|
|
305
|
+
},
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
|
|
309
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
310
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
311
|
+
|
|
312
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
313
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
314
|
+
|
|
315
|
+
await generateReactQueryCommand({
|
|
316
|
+
input: inputPath,
|
|
317
|
+
output: outputPath,
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
321
|
+
|
|
322
|
+
// Should only generate hook for operation with ID
|
|
323
|
+
expect(content).toContain('useWithId');
|
|
324
|
+
expect(content).not.toContain('useWithoutId');
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
it('should log generation progress', async () => {
|
|
328
|
+
const spec = {
|
|
329
|
+
openapi: '3.0.0',
|
|
330
|
+
info: { title: 'API', version: '1.0.0' },
|
|
331
|
+
paths: {
|
|
332
|
+
'/test': {
|
|
333
|
+
get: {
|
|
334
|
+
operationId: 'test',
|
|
335
|
+
responses: { '200': { description: 'OK' } },
|
|
336
|
+
},
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
342
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
343
|
+
|
|
344
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
345
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
346
|
+
const consoleSpy = vi.spyOn(console, 'log');
|
|
347
|
+
|
|
348
|
+
await generateReactQueryCommand({
|
|
349
|
+
input: inputPath,
|
|
350
|
+
output: outputPath,
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
354
|
+
expect.stringContaining('Generating TypeScript types'),
|
|
355
|
+
);
|
|
356
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
357
|
+
expect.stringContaining('React Query hooks generated'),
|
|
358
|
+
);
|
|
359
|
+
expect(consoleSpy).toHaveBeenCalledWith(
|
|
360
|
+
expect.stringContaining('Generated'),
|
|
361
|
+
);
|
|
362
|
+
expect(consoleSpy).toHaveBeenCalledWith(expect.stringContaining('hooks'));
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
it('should handle invalid JSON in OpenAPI spec', async () => {
|
|
366
|
+
const inputPath = join(tempDir, 'invalid.json');
|
|
367
|
+
await createTestFile(tempDir, 'invalid.json', 'not valid json {[}]');
|
|
368
|
+
|
|
369
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
370
|
+
|
|
371
|
+
await expect(
|
|
372
|
+
generateReactQueryCommand({ input: inputPath }),
|
|
373
|
+
).rejects.toThrow(/React Query generation failed/);
|
|
374
|
+
});
|
|
375
|
+
|
|
376
|
+
it('should use custom API name', async () => {
|
|
377
|
+
const spec = {
|
|
378
|
+
openapi: '3.0.0',
|
|
379
|
+
info: { title: 'API', version: '1.0.0' },
|
|
380
|
+
paths: {
|
|
381
|
+
'/test': {
|
|
382
|
+
get: {
|
|
383
|
+
operationId: 'test',
|
|
384
|
+
responses: { '200': { description: 'OK' } },
|
|
385
|
+
},
|
|
386
|
+
},
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
391
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
392
|
+
|
|
393
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
394
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
395
|
+
|
|
396
|
+
await generateReactQueryCommand({
|
|
397
|
+
input: inputPath,
|
|
398
|
+
output: outputPath,
|
|
399
|
+
name: 'MyCustomAPI',
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
403
|
+
|
|
404
|
+
expect(content).toContain('mycustomapi');
|
|
405
|
+
});
|
|
406
|
+
|
|
407
|
+
it('should generate hooks with parameters', async () => {
|
|
408
|
+
const spec = {
|
|
409
|
+
openapi: '3.0.0',
|
|
410
|
+
info: { title: 'API', version: '1.0.0' },
|
|
411
|
+
paths: {
|
|
412
|
+
'/users/{userId}/posts/{postId}': {
|
|
413
|
+
get: {
|
|
414
|
+
operationId: 'getUserPost',
|
|
415
|
+
parameters: [
|
|
416
|
+
{ name: 'userId', in: 'path', required: true },
|
|
417
|
+
{ name: 'postId', in: 'path', required: true },
|
|
418
|
+
{ name: 'include', in: 'query' },
|
|
419
|
+
],
|
|
420
|
+
responses: { '200': { description: 'OK' } },
|
|
421
|
+
},
|
|
422
|
+
},
|
|
423
|
+
},
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
427
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
428
|
+
|
|
429
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
430
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
431
|
+
|
|
432
|
+
await generateReactQueryCommand({
|
|
433
|
+
input: inputPath,
|
|
434
|
+
output: outputPath,
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
438
|
+
|
|
439
|
+
expect(content).toContain('useGetUserPost');
|
|
440
|
+
// Should generate hook with params
|
|
441
|
+
expect(content).toContain('config');
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
it('should generate type exports', async () => {
|
|
445
|
+
const spec = {
|
|
446
|
+
openapi: '3.0.0',
|
|
447
|
+
info: { title: 'API', version: '1.0.0' },
|
|
448
|
+
paths: {
|
|
449
|
+
'/users': {
|
|
450
|
+
get: {
|
|
451
|
+
operationId: 'getUsers',
|
|
452
|
+
responses: { '200': { description: 'OK' } },
|
|
453
|
+
},
|
|
454
|
+
post: {
|
|
455
|
+
operationId: 'createUser',
|
|
456
|
+
responses: { '201': { description: 'Created' } },
|
|
457
|
+
},
|
|
458
|
+
},
|
|
459
|
+
},
|
|
460
|
+
};
|
|
461
|
+
|
|
462
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
463
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
464
|
+
|
|
465
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
466
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
467
|
+
|
|
468
|
+
await generateReactQueryCommand({
|
|
469
|
+
input: inputPath,
|
|
470
|
+
output: outputPath,
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
474
|
+
|
|
475
|
+
expect(content).toContain('Type exports for convenience');
|
|
476
|
+
expect(content).toContain('GetUsersResponse');
|
|
477
|
+
expect(content).toContain('CreateUserResponse');
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it('should handle spec with no paths', async () => {
|
|
481
|
+
const spec = {
|
|
482
|
+
openapi: '3.0.0',
|
|
483
|
+
info: { title: 'API', version: '1.0.0' },
|
|
484
|
+
paths: {},
|
|
485
|
+
};
|
|
486
|
+
|
|
487
|
+
const inputPath = join(tempDir, 'openapi.json');
|
|
488
|
+
await createTestFile(tempDir, 'openapi.json', JSON.stringify(spec));
|
|
489
|
+
|
|
490
|
+
const outputPath = join(tempDir, 'hooks.ts');
|
|
491
|
+
vi.spyOn(process, 'cwd').mockReturnValue(tempDir);
|
|
492
|
+
|
|
493
|
+
await generateReactQueryCommand({
|
|
494
|
+
input: inputPath,
|
|
495
|
+
output: outputPath,
|
|
496
|
+
});
|
|
497
|
+
|
|
498
|
+
const content = await readFile(outputPath, 'utf-8');
|
|
499
|
+
|
|
500
|
+
// Should still generate base structure
|
|
501
|
+
expect(content).toContain('createTypedQueryClient');
|
|
502
|
+
expect(content).toContain('Query Hooks');
|
|
503
|
+
expect(content).toContain('Mutation Hooks');
|
|
504
|
+
});
|
|
505
|
+
});
|
|
506
|
+
});
|