nuxt-openapi-hyperfetch 0.2.7-alpha.1 → 0.3.0-beta

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 (68) hide show
  1. package/.editorconfig +26 -26
  2. package/.prettierignore +17 -17
  3. package/CONTRIBUTING.md +291 -291
  4. package/INSTRUCTIONS.md +327 -327
  5. package/LICENSE +202 -202
  6. package/README.md +309 -231
  7. package/dist/cli/config.d.ts +9 -2
  8. package/dist/cli/config.js +1 -1
  9. package/dist/cli/logo.js +5 -5
  10. package/dist/cli/messages.d.ts +1 -0
  11. package/dist/cli/messages.js +2 -0
  12. package/dist/cli/prompts.d.ts +5 -0
  13. package/dist/cli/prompts.js +12 -0
  14. package/dist/cli/types.d.ts +1 -1
  15. package/dist/generators/components/connector-generator/templates.js +68 -19
  16. package/dist/generators/shared/runtime/useFormConnector.js +8 -1
  17. package/dist/generators/shared/runtime/useListConnector.js +13 -6
  18. package/dist/generators/use-async-data/generator.js +4 -0
  19. package/dist/generators/use-async-data/runtime/useApiAsyncData.js +4 -4
  20. package/dist/generators/use-async-data/runtime/useApiAsyncDataRaw.js +4 -4
  21. package/dist/generators/use-async-data/templates.js +17 -17
  22. package/dist/generators/use-fetch/generator.js +4 -0
  23. package/dist/generators/use-fetch/templates.js +14 -14
  24. package/dist/index.js +40 -27
  25. package/dist/module/index.js +19 -0
  26. package/dist/module/types.d.ts +7 -0
  27. package/docs/API-REFERENCE.md +886 -886
  28. package/docs/generated-components.md +615 -615
  29. package/docs/headless-composables-ui.md +569 -569
  30. package/eslint.config.js +85 -85
  31. package/package.json +1 -1
  32. package/src/cli/config.ts +147 -140
  33. package/src/cli/logger.ts +124 -124
  34. package/src/cli/logo.ts +25 -25
  35. package/src/cli/messages.ts +4 -0
  36. package/src/cli/prompts.ts +14 -1
  37. package/src/cli/types.ts +50 -50
  38. package/src/generators/components/connector-generator/generator.ts +138 -138
  39. package/src/generators/components/connector-generator/templates.ts +307 -254
  40. package/src/generators/components/connector-generator/types.ts +34 -34
  41. package/src/generators/components/schema-analyzer/index.ts +44 -44
  42. package/src/generators/components/schema-analyzer/intent-detector.ts +187 -187
  43. package/src/generators/components/schema-analyzer/openapi-reader.ts +96 -96
  44. package/src/generators/components/schema-analyzer/resource-grouper.ts +166 -166
  45. package/src/generators/components/schema-analyzer/schema-field-mapper.ts +268 -268
  46. package/src/generators/components/schema-analyzer/types.ts +177 -177
  47. package/src/generators/nuxt-server/generator.ts +272 -272
  48. package/src/generators/shared/runtime/apiHelpers.ts +535 -535
  49. package/src/generators/shared/runtime/pagination.ts +323 -323
  50. package/src/generators/shared/runtime/useDeleteConnector.ts +109 -109
  51. package/src/generators/shared/runtime/useDetailConnector.ts +64 -64
  52. package/src/generators/shared/runtime/useFormConnector.ts +147 -139
  53. package/src/generators/shared/runtime/useListConnector.ts +158 -148
  54. package/src/generators/shared/runtime/zod-error-merger.ts +119 -119
  55. package/src/generators/shared/templates/api-callbacks-plugin.ts +399 -399
  56. package/src/generators/shared/templates/api-pagination-plugin.ts +158 -158
  57. package/src/generators/use-async-data/generator.ts +213 -205
  58. package/src/generators/use-async-data/runtime/useApiAsyncData.ts +329 -329
  59. package/src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts +324 -324
  60. package/src/generators/use-async-data/templates.ts +257 -257
  61. package/src/generators/use-fetch/generator.ts +178 -170
  62. package/src/generators/use-fetch/runtime/useApiRequest.ts +354 -354
  63. package/src/generators/use-fetch/templates.ts +214 -214
  64. package/src/index.ts +306 -303
  65. package/src/module/index.ts +158 -133
  66. package/src/module/types.ts +39 -31
  67. package/dist/generators/tanstack-query/generator.d.ts +0 -5
  68. package/dist/generators/tanstack-query/generator.js +0 -11
@@ -1,205 +1,213 @@
1
- import * as path from 'path';
2
- import fs from 'fs-extra';
3
- import { fileURLToPath } from 'url';
4
- import { format } from 'prettier';
5
- import {
6
- getApiFiles as getApiFilesOfficial,
7
- parseApiFile as parseApiFileOfficial,
8
- } from './parser.js';
9
- import {
10
- getApiFiles as getApiFilesHeyApi,
11
- parseApiFile as parseApiFileHeyApi,
12
- } from '../shared/parsers/heyapi-parser.js';
13
- import {
14
- generateComposableFile,
15
- generateRawComposableFile,
16
- generateIndexFile,
17
- type GenerateOptions,
18
- } from './templates.js';
19
- import type { MethodInfo } from './types.js';
20
- import { type Logger, createClackLogger } from '../../cli/logger.js';
21
-
22
- /**
23
- * Main function to generate useAsyncData composables
24
- */
25
- export async function generateUseAsyncDataComposables(
26
- inputDir: string,
27
- outputDir: string,
28
- options?: GenerateOptions,
29
- logger: Logger = createClackLogger()
30
- ): Promise<void> {
31
- const mainSpinner = logger.spinner();
32
-
33
- // Select parser based on chosen backend
34
- const getApiFiles = options?.backend === 'heyapi' ? getApiFilesHeyApi : getApiFilesOfficial;
35
- const parseApiFile = options?.backend === 'heyapi' ? parseApiFileHeyApi : parseApiFileOfficial;
36
-
37
- // 1. Get all API files
38
- mainSpinner.start('Scanning API files');
39
- const apiFiles = getApiFiles(inputDir);
40
- mainSpinner.stop(`Found ${apiFiles.length} API file(s)`);
41
-
42
- if (apiFiles.length === 0) {
43
- throw new Error('No API files found in the input directory');
44
- }
45
-
46
- // 2. Parse each API file
47
- mainSpinner.start('Parsing API files');
48
- const allMethods: MethodInfo[] = [];
49
-
50
- for (const file of apiFiles) {
51
- const fileName = path.basename(file);
52
- try {
53
- const apiInfo = parseApiFile(file);
54
- allMethods.push(...apiInfo.methods);
55
- } catch (error) {
56
- logger.log.error(`Error parsing ${fileName}: ${String(error)}`);
57
- }
58
- }
59
-
60
- mainSpinner.stop(`Found ${allMethods.length} methods to generate`);
61
-
62
- if (allMethods.length === 0) {
63
- logger.log.warn('No methods found to generate');
64
- return;
65
- }
66
-
67
- // 3. Clean and create output directories
68
- mainSpinner.start('Preparing output directories');
69
- const composablesDir = path.join(outputDir, 'composables');
70
- const runtimeDir = path.join(outputDir, 'runtime');
71
- const sharedRuntimeDir = path.join(path.dirname(outputDir), 'shared', 'runtime');
72
- await fs.emptyDir(composablesDir);
73
- await fs.ensureDir(runtimeDir);
74
- await fs.ensureDir(sharedRuntimeDir);
75
- mainSpinner.stop('Output directories ready');
76
-
77
- // 4. Copy runtime helpers
78
- mainSpinner.start('Copying runtime files');
79
- // Derive __dirname equivalent for ESM
80
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
81
- // When compiled, __dirname points to dist/generators/use-async-data/
82
- // We need to go back to src/ to get the original .ts files
83
-
84
- // Copy useApiAsyncData.ts
85
- const runtimeSource = path.resolve(
86
- __dirname,
87
- '../../../src/generators/use-async-data/runtime/useApiAsyncData.ts'
88
- );
89
- const runtimeDest = path.join(runtimeDir, 'useApiAsyncData.ts');
90
- await fs.copyFile(runtimeSource, runtimeDest);
91
-
92
- // Copy useApiAsyncDataRaw.ts
93
- const runtimeRawSource = path.resolve(
94
- __dirname,
95
- '../../../src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts'
96
- );
97
- const runtimeRawDest = path.join(runtimeDir, 'useApiAsyncDataRaw.ts');
98
- await fs.copyFile(runtimeRawSource, runtimeRawDest);
99
-
100
- // Copy shared apiHelpers.ts
101
- const sharedHelpersSource = path.resolve(
102
- __dirname,
103
- '../../../src/generators/shared/runtime/apiHelpers.ts'
104
- );
105
- const sharedHelpersDest = path.join(sharedRuntimeDir, 'apiHelpers.ts');
106
- await fs.copyFile(sharedHelpersSource, sharedHelpersDest);
107
- mainSpinner.stop('Runtime files copied');
108
-
109
- // 5. Calculate relative import path from composables to APIs
110
- const relativePath = calculateRelativeImportPath(composablesDir, inputDir);
111
-
112
- // 6. Generate each composable (normal + Raw if available)
113
- mainSpinner.start('Generating composables');
114
- let successCount = 0;
115
- let errorCount = 0;
116
- const generatedComposableNames: string[] = [];
117
-
118
- for (const method of allMethods) {
119
- // Generate normal version
120
- try {
121
- const code = generateComposableFile(method, relativePath, options);
122
- const formattedCode = await formatCode(code, logger);
123
- const composableName = method.composableName.replace(/^useFetch/, 'useAsyncData');
124
- const fileName = `${composableName}.ts`;
125
- const filePath = path.join(composablesDir, fileName);
126
-
127
- await fs.writeFile(filePath, formattedCode, 'utf-8');
128
- generatedComposableNames.push(composableName);
129
- successCount++;
130
- } catch (error) {
131
- logger.log.error(`Error generating ${method.composableName}: ${String(error)}`);
132
- errorCount++;
133
- }
134
-
135
- // Generate Raw version if available
136
- if (method.hasRawMethod && method.rawMethodName) {
137
- try {
138
- const code = generateRawComposableFile(method, relativePath, options);
139
- const formattedCode = await formatCode(code, logger);
140
- const composableName = `useAsyncData${method.rawMethodName.replace(/Raw$/, '')}Raw`;
141
- const fileName = `${composableName}.ts`;
142
- const filePath = path.join(composablesDir, fileName);
143
-
144
- await fs.writeFile(filePath, formattedCode, 'utf-8');
145
- generatedComposableNames.push(composableName);
146
- successCount++;
147
- } catch (error) {
148
- logger.log.error(`Error generating ${method.composableName} (Raw): ${String(error)}`);
149
- errorCount++;
150
- }
151
- }
152
- }
153
-
154
- // 7. Generate index.ts
155
- const indexCode = generateIndexFile(generatedComposableNames);
156
- const formattedIndex = await formatCode(indexCode, logger);
157
- await fs.writeFile(path.join(outputDir, 'index.ts'), formattedIndex, 'utf-8');
158
- mainSpinner.stop(`Generated ${successCount} composables`);
159
-
160
- // 8. Summary
161
- if (errorCount > 0) {
162
- logger.log.warn(`Completed with ${errorCount} error(s)`);
163
- }
164
- logger.log.success(`Generated ${successCount} useAsyncData composable(s) in ${outputDir}`);
165
- }
166
-
167
- /**
168
- * Calculate relative import path from composables to APIs
169
- */
170
- function calculateRelativeImportPath(composablesDir: string, inputDir: string): string {
171
- // Import from the root index.ts which exports apis, models, and runtime
172
- let relativePath = path.relative(composablesDir, inputDir);
173
-
174
- // Convert Windows paths to Unix-style
175
- relativePath = relativePath.replace(/\\/g, '/');
176
-
177
- // Ensure it starts with './' or '../'
178
- if (!relativePath.startsWith('.')) {
179
- relativePath = './' + relativePath;
180
- }
181
-
182
- // Remove .ts extension and trailing /
183
- relativePath = relativePath.replace(/\.ts$/, '').replace(/\/$/, '');
184
-
185
- return relativePath;
186
- }
187
-
188
- /**
189
- * Format code with Prettier
190
- */
191
- async function formatCode(code: string, logger: Logger): Promise<string> {
192
- try {
193
- return await format(code, {
194
- parser: 'typescript',
195
- semi: true,
196
- singleQuote: true,
197
- trailingComma: 'es5',
198
- printWidth: 80,
199
- tabWidth: 2,
200
- });
201
- } catch {
202
- logger.log.warn('Could not format code with Prettier');
203
- return code;
204
- }
205
- }
1
+ import * as path from 'path';
2
+ import fs from 'fs-extra';
3
+ import { fileURLToPath } from 'url';
4
+ import { format } from 'prettier';
5
+ import {
6
+ getApiFiles as getApiFilesOfficial,
7
+ parseApiFile as parseApiFileOfficial,
8
+ } from './parser.js';
9
+ import {
10
+ getApiFiles as getApiFilesHeyApi,
11
+ parseApiFile as parseApiFileHeyApi,
12
+ } from '../shared/parsers/heyapi-parser.js';
13
+ import {
14
+ generateComposableFile,
15
+ generateRawComposableFile,
16
+ generateIndexFile,
17
+ type GenerateOptions,
18
+ } from './templates.js';
19
+ import type { MethodInfo } from './types.js';
20
+ import { type Logger, createClackLogger } from '../../cli/logger.js';
21
+
22
+ /**
23
+ * Main function to generate useAsyncData composables
24
+ */
25
+ export async function generateUseAsyncDataComposables(
26
+ inputDir: string,
27
+ outputDir: string,
28
+ options?: GenerateOptions,
29
+ logger: Logger = createClackLogger()
30
+ ): Promise<void> {
31
+ const mainSpinner = logger.spinner();
32
+
33
+ // Select parser based on chosen backend
34
+ const getApiFiles = options?.backend === 'heyapi' ? getApiFilesHeyApi : getApiFilesOfficial;
35
+ const parseApiFile = options?.backend === 'heyapi' ? parseApiFileHeyApi : parseApiFileOfficial;
36
+
37
+ // 1. Get all API files
38
+ mainSpinner.start('Scanning API files');
39
+ const apiFiles = getApiFiles(inputDir);
40
+ mainSpinner.stop(`Found ${apiFiles.length} API file(s)`);
41
+
42
+ if (apiFiles.length === 0) {
43
+ throw new Error('No API files found in the input directory');
44
+ }
45
+
46
+ // 2. Parse each API file
47
+ mainSpinner.start('Parsing API files');
48
+ const allMethods: MethodInfo[] = [];
49
+
50
+ for (const file of apiFiles) {
51
+ const fileName = path.basename(file);
52
+ try {
53
+ const apiInfo = parseApiFile(file);
54
+ allMethods.push(...apiInfo.methods);
55
+ } catch (error) {
56
+ logger.log.error(`Error parsing ${fileName}: ${String(error)}`);
57
+ }
58
+ }
59
+
60
+ mainSpinner.stop(`Found ${allMethods.length} methods to generate`);
61
+
62
+ if (allMethods.length === 0) {
63
+ logger.log.warn('No methods found to generate');
64
+ return;
65
+ }
66
+
67
+ // 3. Clean and create output directories
68
+ mainSpinner.start('Preparing output directories');
69
+ const composablesDir = path.join(outputDir, 'composables');
70
+ const runtimeDir = path.join(outputDir, 'runtime');
71
+ const sharedRuntimeDir = path.join(path.dirname(outputDir), 'shared', 'runtime');
72
+ await fs.emptyDir(composablesDir);
73
+ await fs.ensureDir(runtimeDir);
74
+ await fs.ensureDir(sharedRuntimeDir);
75
+ mainSpinner.stop('Output directories ready');
76
+
77
+ // 4. Copy runtime helpers
78
+ mainSpinner.start('Copying runtime files');
79
+ // Derive __dirname equivalent for ESM
80
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
81
+ // When compiled, __dirname points to dist/generators/use-async-data/
82
+ // We need to go back to src/ to get the original .ts files
83
+
84
+ // Copy useApiAsyncData.ts
85
+ const runtimeSource = path.resolve(
86
+ __dirname,
87
+ '../../../src/generators/use-async-data/runtime/useApiAsyncData.ts'
88
+ );
89
+ const runtimeDest = path.join(runtimeDir, 'useApiAsyncData.ts');
90
+ await fs.copyFile(runtimeSource, runtimeDest);
91
+
92
+ // Copy useApiAsyncDataRaw.ts
93
+ const runtimeRawSource = path.resolve(
94
+ __dirname,
95
+ '../../../src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts'
96
+ );
97
+ const runtimeRawDest = path.join(runtimeDir, 'useApiAsyncDataRaw.ts');
98
+ await fs.copyFile(runtimeRawSource, runtimeRawDest);
99
+
100
+ // Copy shared apiHelpers.ts
101
+ const sharedHelpersSource = path.resolve(
102
+ __dirname,
103
+ '../../../src/generators/shared/runtime/apiHelpers.ts'
104
+ );
105
+ const sharedHelpersDest = path.join(sharedRuntimeDir, 'apiHelpers.ts');
106
+ await fs.copyFile(sharedHelpersSource, sharedHelpersDest);
107
+
108
+ // Copy shared pagination.ts
109
+ const sharedPaginationSource = path.resolve(
110
+ __dirname,
111
+ '../../../src/generators/shared/runtime/pagination.ts'
112
+ );
113
+ const sharedPaginationDest = path.join(sharedRuntimeDir, 'pagination.ts');
114
+ await fs.copyFile(sharedPaginationSource, sharedPaginationDest);
115
+ mainSpinner.stop('Runtime files copied');
116
+
117
+ // 5. Calculate relative import path from composables to APIs
118
+ const relativePath = calculateRelativeImportPath(composablesDir, inputDir);
119
+
120
+ // 6. Generate each composable (normal + Raw if available)
121
+ mainSpinner.start('Generating composables');
122
+ let successCount = 0;
123
+ let errorCount = 0;
124
+ const generatedComposableNames: string[] = [];
125
+
126
+ for (const method of allMethods) {
127
+ // Generate normal version
128
+ try {
129
+ const code = generateComposableFile(method, relativePath, options);
130
+ const formattedCode = await formatCode(code, logger);
131
+ const composableName = method.composableName.replace(/^useFetch/, 'useAsyncData');
132
+ const fileName = `${composableName}.ts`;
133
+ const filePath = path.join(composablesDir, fileName);
134
+
135
+ await fs.writeFile(filePath, formattedCode, 'utf-8');
136
+ generatedComposableNames.push(composableName);
137
+ successCount++;
138
+ } catch (error) {
139
+ logger.log.error(`Error generating ${method.composableName}: ${String(error)}`);
140
+ errorCount++;
141
+ }
142
+
143
+ // Generate Raw version if available
144
+ if (method.hasRawMethod && method.rawMethodName) {
145
+ try {
146
+ const code = generateRawComposableFile(method, relativePath, options);
147
+ const formattedCode = await formatCode(code, logger);
148
+ const composableName = `useAsyncData${method.rawMethodName.replace(/Raw$/, '')}Raw`;
149
+ const fileName = `${composableName}.ts`;
150
+ const filePath = path.join(composablesDir, fileName);
151
+
152
+ await fs.writeFile(filePath, formattedCode, 'utf-8');
153
+ generatedComposableNames.push(composableName);
154
+ successCount++;
155
+ } catch (error) {
156
+ logger.log.error(`Error generating ${method.composableName} (Raw): ${String(error)}`);
157
+ errorCount++;
158
+ }
159
+ }
160
+ }
161
+
162
+ // 7. Generate index.ts
163
+ const indexCode = generateIndexFile(generatedComposableNames);
164
+ const formattedIndex = await formatCode(indexCode, logger);
165
+ await fs.writeFile(path.join(outputDir, 'index.ts'), formattedIndex, 'utf-8');
166
+ mainSpinner.stop(`Generated ${successCount} composables`);
167
+
168
+ // 8. Summary
169
+ if (errorCount > 0) {
170
+ logger.log.warn(`Completed with ${errorCount} error(s)`);
171
+ }
172
+ logger.log.success(`Generated ${successCount} useAsyncData composable(s) in ${outputDir}`);
173
+ }
174
+
175
+ /**
176
+ * Calculate relative import path from composables to APIs
177
+ */
178
+ function calculateRelativeImportPath(composablesDir: string, inputDir: string): string {
179
+ // Import from the root index.ts which exports apis, models, and runtime
180
+ let relativePath = path.relative(composablesDir, inputDir);
181
+
182
+ // Convert Windows paths to Unix-style
183
+ relativePath = relativePath.replace(/\\/g, '/');
184
+
185
+ // Ensure it starts with './' or '../'
186
+ if (!relativePath.startsWith('.')) {
187
+ relativePath = './' + relativePath;
188
+ }
189
+
190
+ // Remove .ts extension and trailing /
191
+ relativePath = relativePath.replace(/\.ts$/, '').replace(/\/$/, '');
192
+
193
+ return relativePath;
194
+ }
195
+
196
+ /**
197
+ * Format code with Prettier
198
+ */
199
+ async function formatCode(code: string, logger: Logger): Promise<string> {
200
+ try {
201
+ return await format(code, {
202
+ parser: 'typescript',
203
+ semi: true,
204
+ singleQuote: true,
205
+ trailingComma: 'es5',
206
+ printWidth: 80,
207
+ tabWidth: 2,
208
+ });
209
+ } catch {
210
+ logger.log.warn('Could not format code with Prettier');
211
+ return code;
212
+ }
213
+ }