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.
- package/.editorconfig +26 -26
- package/.prettierignore +17 -17
- package/CONTRIBUTING.md +291 -291
- package/INSTRUCTIONS.md +327 -327
- package/LICENSE +202 -202
- package/README.md +231 -231
- package/dist/cli/config.d.ts +9 -2
- package/dist/cli/config.js +1 -1
- package/dist/cli/logo.js +5 -5
- package/dist/cli/messages.d.ts +1 -0
- package/dist/cli/messages.js +2 -0
- package/dist/cli/prompts.d.ts +5 -0
- package/dist/cli/prompts.js +12 -0
- package/dist/cli/types.d.ts +1 -1
- package/dist/generators/components/connector-generator/templates.js +12 -12
- package/dist/generators/use-async-data/templates.js +17 -17
- package/dist/generators/use-fetch/templates.js +14 -14
- package/dist/index.js +39 -27
- package/dist/module/index.js +19 -0
- package/dist/module/types.d.ts +7 -0
- package/docs/API-REFERENCE.md +886 -886
- package/docs/generated-components.md +615 -615
- package/docs/headless-composables-ui.md +569 -569
- package/eslint.config.js +85 -85
- package/package.json +1 -1
- package/src/cli/config.ts +147 -140
- package/src/cli/logger.ts +124 -124
- package/src/cli/logo.ts +25 -25
- package/src/cli/messages.ts +4 -0
- package/src/cli/prompts.ts +14 -1
- package/src/cli/types.ts +50 -50
- package/src/generators/components/connector-generator/generator.ts +138 -138
- package/src/generators/components/connector-generator/templates.ts +254 -254
- package/src/generators/components/connector-generator/types.ts +34 -34
- package/src/generators/components/schema-analyzer/index.ts +44 -44
- package/src/generators/components/schema-analyzer/intent-detector.ts +187 -187
- package/src/generators/components/schema-analyzer/openapi-reader.ts +96 -96
- package/src/generators/components/schema-analyzer/resource-grouper.ts +166 -166
- package/src/generators/components/schema-analyzer/schema-field-mapper.ts +268 -268
- package/src/generators/components/schema-analyzer/types.ts +177 -177
- package/src/generators/nuxt-server/generator.ts +272 -272
- package/src/generators/shared/runtime/apiHelpers.ts +535 -535
- package/src/generators/shared/runtime/pagination.ts +323 -323
- package/src/generators/shared/runtime/useDeleteConnector.ts +109 -109
- package/src/generators/shared/runtime/useDetailConnector.ts +64 -64
- package/src/generators/shared/runtime/useFormConnector.ts +139 -139
- package/src/generators/shared/runtime/useListConnector.ts +148 -148
- package/src/generators/shared/runtime/zod-error-merger.ts +119 -119
- package/src/generators/shared/templates/api-callbacks-plugin.ts +399 -399
- package/src/generators/shared/templates/api-pagination-plugin.ts +158 -158
- package/src/generators/use-async-data/generator.ts +205 -205
- package/src/generators/use-async-data/runtime/useApiAsyncData.ts +329 -329
- package/src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts +324 -324
- package/src/generators/use-async-data/templates.ts +257 -257
- package/src/generators/use-fetch/generator.ts +170 -170
- package/src/generators/use-fetch/runtime/useApiRequest.ts +354 -354
- package/src/generators/use-fetch/templates.ts +214 -214
- package/src/index.ts +305 -303
- package/src/module/index.ts +158 -133
- package/src/module/types.ts +39 -31
|
@@ -1,170 +1,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
|
-
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
|
+
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
|
+
}
|