nuxt-openapi-hyperfetch 0.2.7-alpha.1 → 0.2.8-alpha.1

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 (60) 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 +231 -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 +12 -12
  16. package/dist/generators/use-async-data/templates.js +17 -17
  17. package/dist/generators/use-fetch/templates.js +14 -14
  18. package/dist/index.js +39 -27
  19. package/dist/module/index.js +19 -0
  20. package/dist/module/types.d.ts +7 -0
  21. package/docs/API-REFERENCE.md +886 -886
  22. package/docs/generated-components.md +615 -615
  23. package/docs/headless-composables-ui.md +569 -569
  24. package/eslint.config.js +85 -85
  25. package/package.json +1 -1
  26. package/src/cli/config.ts +147 -140
  27. package/src/cli/logger.ts +124 -124
  28. package/src/cli/logo.ts +25 -25
  29. package/src/cli/messages.ts +4 -0
  30. package/src/cli/prompts.ts +14 -1
  31. package/src/cli/types.ts +50 -50
  32. package/src/generators/components/connector-generator/generator.ts +138 -138
  33. package/src/generators/components/connector-generator/templates.ts +254 -254
  34. package/src/generators/components/connector-generator/types.ts +34 -34
  35. package/src/generators/components/schema-analyzer/index.ts +44 -44
  36. package/src/generators/components/schema-analyzer/intent-detector.ts +187 -187
  37. package/src/generators/components/schema-analyzer/openapi-reader.ts +96 -96
  38. package/src/generators/components/schema-analyzer/resource-grouper.ts +166 -166
  39. package/src/generators/components/schema-analyzer/schema-field-mapper.ts +268 -268
  40. package/src/generators/components/schema-analyzer/types.ts +177 -177
  41. package/src/generators/nuxt-server/generator.ts +272 -272
  42. package/src/generators/shared/runtime/apiHelpers.ts +535 -535
  43. package/src/generators/shared/runtime/pagination.ts +323 -323
  44. package/src/generators/shared/runtime/useDeleteConnector.ts +109 -109
  45. package/src/generators/shared/runtime/useDetailConnector.ts +64 -64
  46. package/src/generators/shared/runtime/useFormConnector.ts +139 -139
  47. package/src/generators/shared/runtime/useListConnector.ts +148 -148
  48. package/src/generators/shared/runtime/zod-error-merger.ts +119 -119
  49. package/src/generators/shared/templates/api-callbacks-plugin.ts +399 -399
  50. package/src/generators/shared/templates/api-pagination-plugin.ts +158 -158
  51. package/src/generators/use-async-data/generator.ts +205 -205
  52. package/src/generators/use-async-data/runtime/useApiAsyncData.ts +329 -329
  53. package/src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts +324 -324
  54. package/src/generators/use-async-data/templates.ts +257 -257
  55. package/src/generators/use-fetch/generator.ts +170 -170
  56. package/src/generators/use-fetch/runtime/useApiRequest.ts +354 -354
  57. package/src/generators/use-fetch/templates.ts +214 -214
  58. package/src/index.ts +305 -303
  59. package/src/module/index.ts +158 -133
  60. package/src/module/types.ts +39 -31
@@ -1,205 +1,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
- 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
+ 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
+ }