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,170 +1,178 @@
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 { generateComposableFile, generateIndexFile, type GenerateOptions } from './templates.js';
14
- import type { MethodInfo } from './types.js';
15
- import { type Logger, createClackLogger } from '../../cli/logger.js';
16
-
17
- /**
18
- * Main function to generate useFetch composables
19
- */
20
- export async function generateUseFetchComposables(
21
- inputDir: string,
22
- outputDir: string,
23
- options?: GenerateOptions,
24
- logger: Logger = createClackLogger()
25
- ): Promise<void> {
26
- const mainSpinner = logger.spinner();
27
-
28
- // Select parser based on chosen backend
29
- const getApiFiles = options?.backend === 'heyapi' ? getApiFilesHeyApi : getApiFilesOfficial;
30
- const parseApiFile = options?.backend === 'heyapi' ? parseApiFileHeyApi : parseApiFileOfficial;
31
-
32
- // 1. Get all API files
33
- mainSpinner.start('Scanning API files');
34
- const apiFiles = getApiFiles(inputDir);
35
- mainSpinner.stop(`Found ${apiFiles.length} API file(s)`);
36
-
37
- if (apiFiles.length === 0) {
38
- throw new Error('No API files found in the input directory');
39
- }
40
-
41
- // 2. Parse each API file
42
- mainSpinner.start('Parsing API files');
43
- const allMethods: MethodInfo[] = [];
44
-
45
- for (const file of apiFiles) {
46
- const fileName = path.basename(file);
47
- try {
48
- const apiInfo = parseApiFile(file);
49
- allMethods.push(...apiInfo.methods);
50
- } catch (error) {
51
- logger.log.error(`Error parsing ${fileName}: ${String(error)}`);
52
- }
53
- }
54
-
55
- mainSpinner.stop(`Found ${allMethods.length} methods to generate`);
56
-
57
- if (allMethods.length === 0) {
58
- logger.log.warn('No methods found to generate');
59
- return;
60
- }
61
-
62
- // 3. Clean and create output directories
63
- mainSpinner.start('Preparing output directories');
64
- const composablesDir = path.join(outputDir, 'composables');
65
- const runtimeDir = path.join(outputDir, 'runtime');
66
- const sharedRuntimeDir = path.join(path.dirname(outputDir), 'shared', 'runtime');
67
- await fs.emptyDir(composablesDir);
68
- await fs.ensureDir(runtimeDir);
69
- await fs.ensureDir(sharedRuntimeDir);
70
- mainSpinner.stop('Output directories ready');
71
-
72
- // 4. Copy runtime helpers
73
- mainSpinner.start('Copying runtime files');
74
- // Derive __dirname equivalent for ESM
75
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
76
- // When compiled, __dirname points to dist/generators/use-fetch/
77
- // We need to go back to src/ to get the original .ts files
78
-
79
- // Copy useApiRequest.ts
80
- const runtimeSource = path.resolve(
81
- __dirname,
82
- '../../../src/generators/use-fetch/runtime/useApiRequest.ts'
83
- );
84
- const runtimeDest = path.join(runtimeDir, 'useApiRequest.ts');
85
- await fs.copyFile(runtimeSource, runtimeDest);
86
-
87
- // Copy shared apiHelpers.ts
88
- const sharedHelpersSource = path.resolve(
89
- __dirname,
90
- '../../../src/generators/shared/runtime/apiHelpers.ts'
91
- );
92
- const sharedHelpersDest = path.join(sharedRuntimeDir, 'apiHelpers.ts');
93
- await fs.copyFile(sharedHelpersSource, sharedHelpersDest);
94
- mainSpinner.stop('Runtime files copied');
95
-
96
- // 5. Calculate relative import path from composables to APIs
97
- const relativePath = calculateRelativeImportPath(composablesDir, inputDir);
98
-
99
- // 6. Generate each composable
100
- mainSpinner.start('Generating composables');
101
- let successCount = 0;
102
- let errorCount = 0;
103
-
104
- for (const method of allMethods) {
105
- try {
106
- const code = generateComposableFile(method, relativePath, options);
107
- const formattedCode = await formatCode(code, logger);
108
- const fileName = `${method.composableName}.ts`;
109
- const filePath = path.join(composablesDir, fileName);
110
-
111
- await fs.writeFile(filePath, formattedCode, 'utf-8');
112
- successCount++;
113
- } catch (error) {
114
- logger.log.error(`Error generating ${method.composableName}: ${String(error)}`);
115
- errorCount++;
116
- }
117
- }
118
-
119
- // 7. Generate index.ts
120
- const indexCode = generateIndexFile(allMethods.map((m) => m.composableName));
121
- const formattedIndex = await formatCode(indexCode, logger);
122
- await fs.writeFile(path.join(outputDir, 'index.ts'), formattedIndex, 'utf-8');
123
- mainSpinner.stop(`Generated ${successCount} composables`);
124
-
125
- // 8. Summary
126
- if (errorCount > 0) {
127
- logger.log.warn(`Completed with ${errorCount} error(s)`);
128
- }
129
- logger.log.success(`Generated ${successCount} useFetch composable(s) in ${outputDir}`);
130
- }
131
-
132
- /**
133
- * Calculate relative import path from composables to APIs
134
- */
135
- function calculateRelativeImportPath(composablesDir: string, inputDir: string): string {
136
- // Import from the root index.ts which exports apis, models, and runtime
137
- let relativePath = path.relative(composablesDir, inputDir);
138
-
139
- // Convert Windows paths to Unix-style
140
- relativePath = relativePath.replace(/\\/g, '/');
141
-
142
- // Ensure it starts with './' or '../'
143
- if (!relativePath.startsWith('.')) {
144
- relativePath = './' + relativePath;
145
- }
146
-
147
- // Remove .ts extension and trailing /
148
- relativePath = relativePath.replace(/\.ts$/, '').replace(/\/$/, '');
149
-
150
- return relativePath;
151
- }
152
-
153
- /**
154
- * Format code with Prettier
155
- */
156
- async function formatCode(code: string, logger: Logger): Promise<string> {
157
- try {
158
- return await format(code, {
159
- parser: 'typescript',
160
- semi: true,
161
- singleQuote: true,
162
- trailingComma: 'es5',
163
- printWidth: 80,
164
- tabWidth: 2,
165
- });
166
- } catch {
167
- logger.log.warn('Could not format code with Prettier');
168
- return code;
169
- }
170
- }
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 { generateComposableFile, generateIndexFile, type GenerateOptions } from './templates.js';
14
+ import type { MethodInfo } from './types.js';
15
+ import { type Logger, createClackLogger } from '../../cli/logger.js';
16
+
17
+ /**
18
+ * Main function to generate useFetch composables
19
+ */
20
+ export async function generateUseFetchComposables(
21
+ inputDir: string,
22
+ outputDir: string,
23
+ options?: GenerateOptions,
24
+ logger: Logger = createClackLogger()
25
+ ): Promise<void> {
26
+ const mainSpinner = logger.spinner();
27
+
28
+ // Select parser based on chosen backend
29
+ const getApiFiles = options?.backend === 'heyapi' ? getApiFilesHeyApi : getApiFilesOfficial;
30
+ const parseApiFile = options?.backend === 'heyapi' ? parseApiFileHeyApi : parseApiFileOfficial;
31
+
32
+ // 1. Get all API files
33
+ mainSpinner.start('Scanning API files');
34
+ const apiFiles = getApiFiles(inputDir);
35
+ mainSpinner.stop(`Found ${apiFiles.length} API file(s)`);
36
+
37
+ if (apiFiles.length === 0) {
38
+ throw new Error('No API files found in the input directory');
39
+ }
40
+
41
+ // 2. Parse each API file
42
+ mainSpinner.start('Parsing API files');
43
+ const allMethods: MethodInfo[] = [];
44
+
45
+ for (const file of apiFiles) {
46
+ const fileName = path.basename(file);
47
+ try {
48
+ const apiInfo = parseApiFile(file);
49
+ allMethods.push(...apiInfo.methods);
50
+ } catch (error) {
51
+ logger.log.error(`Error parsing ${fileName}: ${String(error)}`);
52
+ }
53
+ }
54
+
55
+ mainSpinner.stop(`Found ${allMethods.length} methods to generate`);
56
+
57
+ if (allMethods.length === 0) {
58
+ logger.log.warn('No methods found to generate');
59
+ return;
60
+ }
61
+
62
+ // 3. Clean and create output directories
63
+ mainSpinner.start('Preparing output directories');
64
+ const composablesDir = path.join(outputDir, 'composables');
65
+ const runtimeDir = path.join(outputDir, 'runtime');
66
+ const sharedRuntimeDir = path.join(path.dirname(outputDir), 'shared', 'runtime');
67
+ await fs.emptyDir(composablesDir);
68
+ await fs.ensureDir(runtimeDir);
69
+ await fs.ensureDir(sharedRuntimeDir);
70
+ mainSpinner.stop('Output directories ready');
71
+
72
+ // 4. Copy runtime helpers
73
+ mainSpinner.start('Copying runtime files');
74
+ // Derive __dirname equivalent for ESM
75
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
76
+ // When compiled, __dirname points to dist/generators/use-fetch/
77
+ // We need to go back to src/ to get the original .ts files
78
+
79
+ // Copy useApiRequest.ts
80
+ const runtimeSource = path.resolve(
81
+ __dirname,
82
+ '../../../src/generators/use-fetch/runtime/useApiRequest.ts'
83
+ );
84
+ const runtimeDest = path.join(runtimeDir, 'useApiRequest.ts');
85
+ await fs.copyFile(runtimeSource, runtimeDest);
86
+
87
+ // Copy shared apiHelpers.ts
88
+ const sharedHelpersSource = path.resolve(
89
+ __dirname,
90
+ '../../../src/generators/shared/runtime/apiHelpers.ts'
91
+ );
92
+ const sharedHelpersDest = path.join(sharedRuntimeDir, 'apiHelpers.ts');
93
+ await fs.copyFile(sharedHelpersSource, sharedHelpersDest);
94
+
95
+ // Copy shared pagination.ts
96
+ const sharedPaginationSource = path.resolve(
97
+ __dirname,
98
+ '../../../src/generators/shared/runtime/pagination.ts'
99
+ );
100
+ const sharedPaginationDest = path.join(sharedRuntimeDir, 'pagination.ts');
101
+ await fs.copyFile(sharedPaginationSource, sharedPaginationDest);
102
+ mainSpinner.stop('Runtime files copied');
103
+
104
+ // 5. Calculate relative import path from composables to APIs
105
+ const relativePath = calculateRelativeImportPath(composablesDir, inputDir);
106
+
107
+ // 6. Generate each composable
108
+ mainSpinner.start('Generating composables');
109
+ let successCount = 0;
110
+ let errorCount = 0;
111
+
112
+ for (const method of allMethods) {
113
+ try {
114
+ const code = generateComposableFile(method, relativePath, options);
115
+ const formattedCode = await formatCode(code, logger);
116
+ const fileName = `${method.composableName}.ts`;
117
+ const filePath = path.join(composablesDir, fileName);
118
+
119
+ await fs.writeFile(filePath, formattedCode, 'utf-8');
120
+ successCount++;
121
+ } catch (error) {
122
+ logger.log.error(`Error generating ${method.composableName}: ${String(error)}`);
123
+ errorCount++;
124
+ }
125
+ }
126
+
127
+ // 7. Generate index.ts
128
+ const indexCode = generateIndexFile(allMethods.map((m) => m.composableName));
129
+ const formattedIndex = await formatCode(indexCode, logger);
130
+ await fs.writeFile(path.join(outputDir, 'index.ts'), formattedIndex, 'utf-8');
131
+ mainSpinner.stop(`Generated ${successCount} composables`);
132
+
133
+ // 8. Summary
134
+ if (errorCount > 0) {
135
+ logger.log.warn(`Completed with ${errorCount} error(s)`);
136
+ }
137
+ logger.log.success(`Generated ${successCount} useFetch composable(s) in ${outputDir}`);
138
+ }
139
+
140
+ /**
141
+ * Calculate relative import path from composables to APIs
142
+ */
143
+ function calculateRelativeImportPath(composablesDir: string, inputDir: string): string {
144
+ // Import from the root index.ts which exports apis, models, and runtime
145
+ let relativePath = path.relative(composablesDir, inputDir);
146
+
147
+ // Convert Windows paths to Unix-style
148
+ relativePath = relativePath.replace(/\\/g, '/');
149
+
150
+ // Ensure it starts with './' or '../'
151
+ if (!relativePath.startsWith('.')) {
152
+ relativePath = './' + relativePath;
153
+ }
154
+
155
+ // Remove .ts extension and trailing /
156
+ relativePath = relativePath.replace(/\.ts$/, '').replace(/\/$/, '');
157
+
158
+ return relativePath;
159
+ }
160
+
161
+ /**
162
+ * Format code with Prettier
163
+ */
164
+ async function formatCode(code: string, logger: Logger): Promise<string> {
165
+ try {
166
+ return await format(code, {
167
+ parser: 'typescript',
168
+ semi: true,
169
+ singleQuote: true,
170
+ trailingComma: 'es5',
171
+ printWidth: 80,
172
+ tabWidth: 2,
173
+ });
174
+ } catch {
175
+ logger.log.warn('Could not format code with Prettier');
176
+ return code;
177
+ }
178
+ }