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