@geekmidas/cli 0.10.0 → 0.13.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.
Files changed (146) hide show
  1. package/README.md +525 -0
  2. package/dist/bundler-B1qy9b-j.cjs +112 -0
  3. package/dist/bundler-B1qy9b-j.cjs.map +1 -0
  4. package/dist/bundler-DskIqW2t.mjs +111 -0
  5. package/dist/bundler-DskIqW2t.mjs.map +1 -0
  6. package/dist/{config-C9aXOHBe.cjs → config-AmInkU7k.cjs} +8 -8
  7. package/dist/config-AmInkU7k.cjs.map +1 -0
  8. package/dist/{config-BrkUalUh.mjs → config-DYULeEv8.mjs} +3 -3
  9. package/dist/config-DYULeEv8.mjs.map +1 -0
  10. package/dist/config.cjs +1 -1
  11. package/dist/config.d.cts +1 -1
  12. package/dist/config.d.mts +1 -1
  13. package/dist/config.mjs +1 -1
  14. package/dist/encryption-C8H-38Yy.mjs +42 -0
  15. package/dist/encryption-C8H-38Yy.mjs.map +1 -0
  16. package/dist/encryption-Dyf_r1h-.cjs +44 -0
  17. package/dist/encryption-Dyf_r1h-.cjs.map +1 -0
  18. package/dist/index.cjs +2123 -179
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.mjs +2141 -192
  21. package/dist/index.mjs.map +1 -1
  22. package/dist/{openapi-CZLI4QTr.mjs → openapi-BfFlOBCG.mjs} +801 -38
  23. package/dist/openapi-BfFlOBCG.mjs.map +1 -0
  24. package/dist/{openapi-BeHLKcwP.cjs → openapi-Bt_1FDpT.cjs} +794 -31
  25. package/dist/openapi-Bt_1FDpT.cjs.map +1 -0
  26. package/dist/{openapi-react-query-o5iMi8tz.cjs → openapi-react-query-B-sNWHFU.cjs} +5 -5
  27. package/dist/openapi-react-query-B-sNWHFU.cjs.map +1 -0
  28. package/dist/{openapi-react-query-CcciaVu5.mjs → openapi-react-query-B6XTeGqS.mjs} +5 -5
  29. package/dist/openapi-react-query-B6XTeGqS.mjs.map +1 -0
  30. package/dist/openapi-react-query.cjs +1 -1
  31. package/dist/openapi-react-query.d.cts.map +1 -1
  32. package/dist/openapi-react-query.d.mts.map +1 -1
  33. package/dist/openapi-react-query.mjs +1 -1
  34. package/dist/openapi.cjs +2 -2
  35. package/dist/openapi.d.cts +1 -1
  36. package/dist/openapi.d.cts.map +1 -1
  37. package/dist/openapi.d.mts +1 -1
  38. package/dist/openapi.d.mts.map +1 -1
  39. package/dist/openapi.mjs +2 -2
  40. package/dist/storage-BOOpAF8N.cjs +5 -0
  41. package/dist/storage-Bj1E26lU.cjs +187 -0
  42. package/dist/storage-Bj1E26lU.cjs.map +1 -0
  43. package/dist/storage-kSxTjkNb.mjs +133 -0
  44. package/dist/storage-kSxTjkNb.mjs.map +1 -0
  45. package/dist/storage-tgZSUnKl.mjs +3 -0
  46. package/dist/{types-b-vwGpqc.d.cts → types-BR0M2v_c.d.mts} +100 -1
  47. package/dist/types-BR0M2v_c.d.mts.map +1 -0
  48. package/dist/{types-DXgiA1sF.d.mts → types-BhkZc-vm.d.cts} +100 -1
  49. package/dist/types-BhkZc-vm.d.cts.map +1 -0
  50. package/examples/cron-example.ts +27 -27
  51. package/examples/env.ts +27 -27
  52. package/examples/function-example.ts +31 -31
  53. package/examples/gkm.config.json +20 -20
  54. package/examples/gkm.config.ts +8 -8
  55. package/examples/gkm.minimal.config.json +5 -5
  56. package/examples/gkm.production.config.json +25 -25
  57. package/examples/logger.ts +2 -2
  58. package/package.json +6 -6
  59. package/src/__tests__/EndpointGenerator.hooks.spec.ts +191 -191
  60. package/src/__tests__/config.spec.ts +55 -55
  61. package/src/__tests__/loadEnvFiles.spec.ts +93 -93
  62. package/src/__tests__/normalizeHooksConfig.spec.ts +58 -58
  63. package/src/__tests__/openapi-react-query.spec.ts +497 -497
  64. package/src/__tests__/openapi.spec.ts +428 -428
  65. package/src/__tests__/test-helpers.ts +76 -76
  66. package/src/auth/__tests__/credentials.spec.ts +204 -0
  67. package/src/auth/__tests__/index.spec.ts +168 -0
  68. package/src/auth/credentials.ts +187 -0
  69. package/src/auth/index.ts +226 -0
  70. package/src/build/__tests__/bundler.spec.ts +444 -0
  71. package/src/build/__tests__/index-new.spec.ts +474 -474
  72. package/src/build/__tests__/manifests.spec.ts +333 -333
  73. package/src/build/bundler.ts +210 -0
  74. package/src/build/endpoint-analyzer.ts +236 -0
  75. package/src/build/handler-templates.ts +1253 -0
  76. package/src/build/index.ts +260 -179
  77. package/src/build/manifests.ts +52 -52
  78. package/src/build/providerResolver.ts +145 -145
  79. package/src/build/types.ts +64 -43
  80. package/src/config.ts +39 -39
  81. package/src/deploy/__tests__/docker.spec.ts +111 -0
  82. package/src/deploy/__tests__/dokploy.spec.ts +245 -0
  83. package/src/deploy/__tests__/init.spec.ts +662 -0
  84. package/src/deploy/docker.ts +128 -0
  85. package/src/deploy/dokploy.ts +204 -0
  86. package/src/deploy/index.ts +136 -0
  87. package/src/deploy/init.ts +484 -0
  88. package/src/deploy/types.ts +48 -0
  89. package/src/dev/__tests__/index.spec.ts +266 -266
  90. package/src/dev/index.ts +647 -601
  91. package/src/docker/__tests__/compose.spec.ts +531 -0
  92. package/src/docker/__tests__/templates.spec.ts +280 -0
  93. package/src/docker/compose.ts +273 -0
  94. package/src/docker/index.ts +230 -0
  95. package/src/docker/templates.ts +446 -0
  96. package/src/generators/CronGenerator.ts +72 -72
  97. package/src/generators/EndpointGenerator.ts +699 -398
  98. package/src/generators/FunctionGenerator.ts +84 -84
  99. package/src/generators/Generator.ts +72 -72
  100. package/src/generators/OpenApiTsGenerator.ts +577 -577
  101. package/src/generators/SubscriberGenerator.ts +124 -124
  102. package/src/generators/__tests__/CronGenerator.spec.ts +433 -433
  103. package/src/generators/__tests__/EndpointGenerator.spec.ts +532 -382
  104. package/src/generators/__tests__/FunctionGenerator.spec.ts +244 -244
  105. package/src/generators/__tests__/SubscriberGenerator.spec.ts +397 -382
  106. package/src/generators/index.ts +4 -4
  107. package/src/index.ts +623 -201
  108. package/src/init/__tests__/generators.spec.ts +334 -334
  109. package/src/init/__tests__/init.spec.ts +332 -332
  110. package/src/init/__tests__/utils.spec.ts +89 -89
  111. package/src/init/generators/config.ts +175 -175
  112. package/src/init/generators/docker.ts +41 -41
  113. package/src/init/generators/env.ts +72 -72
  114. package/src/init/generators/index.ts +1 -1
  115. package/src/init/generators/models.ts +64 -64
  116. package/src/init/generators/monorepo.ts +161 -161
  117. package/src/init/generators/package.ts +71 -71
  118. package/src/init/generators/source.ts +6 -6
  119. package/src/init/index.ts +203 -208
  120. package/src/init/templates/api.ts +115 -115
  121. package/src/init/templates/index.ts +75 -75
  122. package/src/init/templates/minimal.ts +98 -98
  123. package/src/init/templates/serverless.ts +89 -89
  124. package/src/init/templates/worker.ts +98 -98
  125. package/src/init/utils.ts +54 -56
  126. package/src/openapi-react-query.ts +194 -194
  127. package/src/openapi.ts +63 -63
  128. package/src/secrets/__tests__/encryption.spec.ts +226 -0
  129. package/src/secrets/__tests__/generator.spec.ts +319 -0
  130. package/src/secrets/__tests__/index.spec.ts +91 -0
  131. package/src/secrets/__tests__/storage.spec.ts +611 -0
  132. package/src/secrets/encryption.ts +91 -0
  133. package/src/secrets/generator.ts +164 -0
  134. package/src/secrets/index.ts +383 -0
  135. package/src/secrets/storage.ts +192 -0
  136. package/src/secrets/types.ts +53 -0
  137. package/src/types.ts +295 -176
  138. package/tsdown.config.ts +11 -8
  139. package/dist/config-BrkUalUh.mjs.map +0 -1
  140. package/dist/config-C9aXOHBe.cjs.map +0 -1
  141. package/dist/openapi-BeHLKcwP.cjs.map +0 -1
  142. package/dist/openapi-CZLI4QTr.mjs.map +0 -1
  143. package/dist/openapi-react-query-CcciaVu5.mjs.map +0 -1
  144. package/dist/openapi-react-query-o5iMi8tz.cjs.map +0 -1
  145. package/dist/types-DXgiA1sF.d.mts.map +0 -1
  146. package/dist/types-b-vwGpqc.d.cts.map +0 -1
@@ -6,501 +6,501 @@ import { generateReactQueryCommand } from '../openapi-react-query';
6
6
  import { cleanupDir, createTempDir, createTestFile } from './test-helpers';
7
7
 
8
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
- });
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
506
  });