nuxt-openapi-hyperfetch 0.1.8-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 -0
- package/docs/headless-composables-ui.md +569 -0
- package/eslint.config.js +85 -85
- package/package.json +8 -2
- 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 -0
- package/src/generators/components/connector-generator/templates.ts +254 -0
- package/src/generators/components/connector-generator/types.ts +34 -0
- package/src/generators/components/schema-analyzer/index.ts +44 -0
- package/src/generators/components/schema-analyzer/intent-detector.ts +187 -0
- package/src/generators/components/schema-analyzer/openapi-reader.ts +96 -0
- package/src/generators/components/schema-analyzer/resource-grouper.ts +166 -0
- package/src/generators/components/schema-analyzer/schema-field-mapper.ts +268 -0
- package/src/generators/components/schema-analyzer/types.ts +177 -0
- package/src/generators/nuxt-server/generator.ts +272 -272
- package/src/generators/shared/runtime/apiHelpers.ts +535 -507
- package/src/generators/shared/runtime/pagination.ts +323 -0
- package/src/generators/shared/runtime/useDeleteConnector.ts +109 -0
- package/src/generators/shared/runtime/useDetailConnector.ts +64 -0
- package/src/generators/shared/runtime/useFormConnector.ts +139 -0
- package/src/generators/shared/runtime/useListConnector.ts +148 -0
- package/src/generators/shared/runtime/zod-error-merger.ts +119 -0
- package/src/generators/shared/templates/api-callbacks-plugin.ts +399 -352
- package/src/generators/shared/templates/api-pagination-plugin.ts +158 -0
- package/src/generators/use-async-data/generator.ts +205 -205
- package/src/generators/use-async-data/runtime/useApiAsyncData.ts +329 -229
- package/src/generators/use-async-data/runtime/useApiAsyncDataRaw.ts +324 -245
- 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 -234
- package/src/generators/use-fetch/templates.ts +214 -214
- package/src/index.ts +305 -265
- package/src/module/index.ts +158 -133
- package/src/module/types.ts +39 -31
package/src/cli/logger.ts
CHANGED
|
@@ -1,124 +1,124 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Centralized logging utilities using @clack/prompts
|
|
3
|
-
* Re-exports @clack for easy use across the project
|
|
4
|
-
*/
|
|
5
|
-
import * as p from '@clack/prompts';
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Re-export @clack/prompts for consistent usage
|
|
9
|
-
*/
|
|
10
|
-
export { p };
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Create and manage a spinner for long operations
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* const spinner = createSpinner();
|
|
17
|
-
* spinner.start('Processing files');
|
|
18
|
-
* // do work
|
|
19
|
-
* spinner.stop('Files processed');
|
|
20
|
-
*/
|
|
21
|
-
export function createSpinner() {
|
|
22
|
-
return p.spinner();
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Log a success message (replaces console.log with ✓)
|
|
27
|
-
*/
|
|
28
|
-
export function logSuccess(message: string) {
|
|
29
|
-
p.log.success(message);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Log an error message
|
|
34
|
-
*/
|
|
35
|
-
export function logError(message: string) {
|
|
36
|
-
p.log.error(message);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Log a warning message
|
|
41
|
-
*/
|
|
42
|
-
export function logWarning(message: string) {
|
|
43
|
-
p.log.warn(message);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Log an info message
|
|
48
|
-
*/
|
|
49
|
-
export function logInfo(message: string) {
|
|
50
|
-
p.log.info(message);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Display a note/box with multiple lines of information
|
|
55
|
-
* Great for "Next steps" sections
|
|
56
|
-
*/
|
|
57
|
-
export function logNote(message: string, title?: string) {
|
|
58
|
-
p.note(message, title);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Display section separator
|
|
63
|
-
*/
|
|
64
|
-
export function logStep(message: string) {
|
|
65
|
-
p.log.step(message);
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// ---------------------------------------------------------------------------
|
|
69
|
-
// Logger abstraction — allows generators to run in both CLI and Nuxt module
|
|
70
|
-
// ---------------------------------------------------------------------------
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Abstract logger interface used by generators.
|
|
74
|
-
* Decouples generator output from @clack/prompts so the same generators work
|
|
75
|
-
* in the interactive CLI and in the Nuxt module (where clack is irrelevant).
|
|
76
|
-
*/
|
|
77
|
-
export interface Logger {
|
|
78
|
-
spinner(): { start(msg: string): void; stop(msg: string): void };
|
|
79
|
-
log: {
|
|
80
|
-
warn(msg: string): void;
|
|
81
|
-
info(msg: string): void;
|
|
82
|
-
success(msg: string): void;
|
|
83
|
-
error(msg: string): void;
|
|
84
|
-
};
|
|
85
|
-
note(msg: string, title?: string): void;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Creates a Logger backed by @clack/prompts — used by the CLI.
|
|
90
|
-
*/
|
|
91
|
-
export function createClackLogger(): Logger {
|
|
92
|
-
return {
|
|
93
|
-
spinner: () => p.spinner(),
|
|
94
|
-
log: {
|
|
95
|
-
warn: (msg) => p.log.warn(msg),
|
|
96
|
-
info: (msg) => p.log.info(msg),
|
|
97
|
-
success: (msg) => p.log.success(msg),
|
|
98
|
-
error: (msg) => p.log.error(msg),
|
|
99
|
-
},
|
|
100
|
-
note: (msg, title) => p.note(msg, title),
|
|
101
|
-
};
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Creates a Logger backed by console — used by the Nuxt module.
|
|
106
|
-
*/
|
|
107
|
-
export function createConsoleLogger(): Logger {
|
|
108
|
-
const prefix = '[nuxt-openapi-hyperfetch]';
|
|
109
|
-
return {
|
|
110
|
-
spinner() {
|
|
111
|
-
return {
|
|
112
|
-
start: (msg: string) => console.log(`${prefix} ⏳ ${msg}`),
|
|
113
|
-
stop: (msg: string) => console.log(`${prefix} ✓ ${msg}`),
|
|
114
|
-
};
|
|
115
|
-
},
|
|
116
|
-
log: {
|
|
117
|
-
warn: (msg) => console.warn(`${prefix} ⚠ ${msg}`),
|
|
118
|
-
info: (msg) => console.info(`${prefix} ℹ ${msg}`),
|
|
119
|
-
success: (msg) => console.log(`${prefix} ✓ ${msg}`),
|
|
120
|
-
error: (msg) => console.error(`${prefix} ✗ ${msg}`),
|
|
121
|
-
},
|
|
122
|
-
note: (msg, title) => console.log(title ? `\n${prefix} ${title}:\n${msg}\n` : `\n${msg}\n`),
|
|
123
|
-
};
|
|
124
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Centralized logging utilities using @clack/prompts
|
|
3
|
+
* Re-exports @clack for easy use across the project
|
|
4
|
+
*/
|
|
5
|
+
import * as p from '@clack/prompts';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Re-export @clack/prompts for consistent usage
|
|
9
|
+
*/
|
|
10
|
+
export { p };
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Create and manage a spinner for long operations
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* const spinner = createSpinner();
|
|
17
|
+
* spinner.start('Processing files');
|
|
18
|
+
* // do work
|
|
19
|
+
* spinner.stop('Files processed');
|
|
20
|
+
*/
|
|
21
|
+
export function createSpinner() {
|
|
22
|
+
return p.spinner();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Log a success message (replaces console.log with ✓)
|
|
27
|
+
*/
|
|
28
|
+
export function logSuccess(message: string) {
|
|
29
|
+
p.log.success(message);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Log an error message
|
|
34
|
+
*/
|
|
35
|
+
export function logError(message: string) {
|
|
36
|
+
p.log.error(message);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Log a warning message
|
|
41
|
+
*/
|
|
42
|
+
export function logWarning(message: string) {
|
|
43
|
+
p.log.warn(message);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Log an info message
|
|
48
|
+
*/
|
|
49
|
+
export function logInfo(message: string) {
|
|
50
|
+
p.log.info(message);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Display a note/box with multiple lines of information
|
|
55
|
+
* Great for "Next steps" sections
|
|
56
|
+
*/
|
|
57
|
+
export function logNote(message: string, title?: string) {
|
|
58
|
+
p.note(message, title);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Display section separator
|
|
63
|
+
*/
|
|
64
|
+
export function logStep(message: string) {
|
|
65
|
+
p.log.step(message);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// Logger abstraction — allows generators to run in both CLI and Nuxt module
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Abstract logger interface used by generators.
|
|
74
|
+
* Decouples generator output from @clack/prompts so the same generators work
|
|
75
|
+
* in the interactive CLI and in the Nuxt module (where clack is irrelevant).
|
|
76
|
+
*/
|
|
77
|
+
export interface Logger {
|
|
78
|
+
spinner(): { start(msg: string): void; stop(msg: string): void };
|
|
79
|
+
log: {
|
|
80
|
+
warn(msg: string): void;
|
|
81
|
+
info(msg: string): void;
|
|
82
|
+
success(msg: string): void;
|
|
83
|
+
error(msg: string): void;
|
|
84
|
+
};
|
|
85
|
+
note(msg: string, title?: string): void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Creates a Logger backed by @clack/prompts — used by the CLI.
|
|
90
|
+
*/
|
|
91
|
+
export function createClackLogger(): Logger {
|
|
92
|
+
return {
|
|
93
|
+
spinner: () => p.spinner(),
|
|
94
|
+
log: {
|
|
95
|
+
warn: (msg) => p.log.warn(msg),
|
|
96
|
+
info: (msg) => p.log.info(msg),
|
|
97
|
+
success: (msg) => p.log.success(msg),
|
|
98
|
+
error: (msg) => p.log.error(msg),
|
|
99
|
+
},
|
|
100
|
+
note: (msg, title) => p.note(msg, title),
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Creates a Logger backed by console — used by the Nuxt module.
|
|
106
|
+
*/
|
|
107
|
+
export function createConsoleLogger(): Logger {
|
|
108
|
+
const prefix = '[nuxt-openapi-hyperfetch]';
|
|
109
|
+
return {
|
|
110
|
+
spinner() {
|
|
111
|
+
return {
|
|
112
|
+
start: (msg: string) => console.log(`${prefix} ⏳ ${msg}`),
|
|
113
|
+
stop: (msg: string) => console.log(`${prefix} ✓ ${msg}`),
|
|
114
|
+
};
|
|
115
|
+
},
|
|
116
|
+
log: {
|
|
117
|
+
warn: (msg) => console.warn(`${prefix} ⚠ ${msg}`),
|
|
118
|
+
info: (msg) => console.info(`${prefix} ℹ ${msg}`),
|
|
119
|
+
success: (msg) => console.log(`${prefix} ✓ ${msg}`),
|
|
120
|
+
error: (msg) => console.error(`${prefix} ✗ ${msg}`),
|
|
121
|
+
},
|
|
122
|
+
note: (msg, title) => console.log(title ? `\n${prefix} ${title}:\n${msg}\n` : `\n${msg}\n`),
|
|
123
|
+
};
|
|
124
|
+
}
|
package/src/cli/logo.ts
CHANGED
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import gradient from 'gradient-string';
|
|
2
|
-
|
|
3
|
-
const NUXT_LOGO = `███╗ ██╗██╗ ██╗██╗ ██╗████████╗
|
|
4
|
-
████╗ ██║██║ ██║╚██╗██╔╝╚══██╔══╝
|
|
5
|
-
██╔██╗ ██║██║ ██║ ╚███╔╝ ██║
|
|
6
|
-
██║╚██╗██║██║ ██║ ██╔██╗ ██║
|
|
7
|
-
██║ ╚████║╚██████╔╝██╔╝ ██╗ ██║
|
|
8
|
-
╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝`;
|
|
9
|
-
|
|
10
|
-
const SUBTITLE = 'Nuxt OpenAPI Generator - useFetch, useAsyncData & Nuxt server';
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Display the Nuxt logo with gradient colors
|
|
14
|
-
* - Green gradient for Nuxt logo (official Nuxt color #00DC82)
|
|
15
|
-
* - Blue gradient for Swagger subtitle
|
|
16
|
-
*/
|
|
17
|
-
export function displayLogo(): void {
|
|
18
|
-
const nuxtGradient = gradient('#00DC82', '#00E090');
|
|
19
|
-
const swaggerGradient = gradient('#3B82F6', '#0EA5E9');
|
|
20
|
-
|
|
21
|
-
console.log('\n');
|
|
22
|
-
console.log(nuxtGradient(NUXT_LOGO));
|
|
23
|
-
console.log(swaggerGradient(SUBTITLE));
|
|
24
|
-
console.log('\n');
|
|
25
|
-
}
|
|
1
|
+
import gradient from 'gradient-string';
|
|
2
|
+
|
|
3
|
+
const NUXT_LOGO = `███╗ ██╗██╗ ██╗██╗ ██╗████████╗
|
|
4
|
+
████╗ ██║██║ ██║╚██╗██╔╝╚══██╔══╝
|
|
5
|
+
██╔██╗ ██║██║ ██║ ╚███╔╝ ██║
|
|
6
|
+
██║╚██╗██║██║ ██║ ██╔██╗ ██║
|
|
7
|
+
██║ ╚████║╚██████╔╝██╔╝ ██╗ ██║
|
|
8
|
+
╚═╝ ╚═══╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝`;
|
|
9
|
+
|
|
10
|
+
const SUBTITLE = 'Nuxt OpenAPI Generator - useFetch, useAsyncData & Nuxt server';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Display the Nuxt logo with gradient colors
|
|
14
|
+
* - Green gradient for Nuxt logo (official Nuxt color #00DC82)
|
|
15
|
+
* - Blue gradient for Swagger subtitle
|
|
16
|
+
*/
|
|
17
|
+
export function displayLogo(): void {
|
|
18
|
+
const nuxtGradient = gradient('#00DC82', '#00E090');
|
|
19
|
+
const swaggerGradient = gradient('#3B82F6', '#0EA5E9');
|
|
20
|
+
|
|
21
|
+
console.log('\n');
|
|
22
|
+
console.log(nuxtGradient(NUXT_LOGO));
|
|
23
|
+
console.log(swaggerGradient(SUBTITLE));
|
|
24
|
+
console.log('\n');
|
|
25
|
+
}
|
package/src/cli/messages.ts
CHANGED
|
@@ -67,6 +67,10 @@ export const CHOICES = {
|
|
|
67
67
|
},
|
|
68
68
|
],
|
|
69
69
|
|
|
70
|
+
// Connectors prompt (shown only when useAsyncData is selected)
|
|
71
|
+
connectorsPrompt:
|
|
72
|
+
'Generate headless UI connectors? (tables, pagination, forms & delete logic built on top of useAsyncData)',
|
|
73
|
+
|
|
70
74
|
// Server path options
|
|
71
75
|
serverPaths: [
|
|
72
76
|
{
|
package/src/cli/prompts.ts
CHANGED
|
@@ -96,7 +96,7 @@ export async function promptComposablesSelection(): Promise<ComposablesSelection
|
|
|
96
96
|
});
|
|
97
97
|
checkCancellation(result);
|
|
98
98
|
|
|
99
|
-
return { composables: result as
|
|
99
|
+
return { composables: result as ('useFetch' | 'useAsyncData' | 'nuxtServer')[] };
|
|
100
100
|
}
|
|
101
101
|
|
|
102
102
|
/**
|
|
@@ -141,3 +141,16 @@ export async function promptBffConfig(): Promise<BffConfig> {
|
|
|
141
141
|
|
|
142
142
|
return { enableBff: result as boolean };
|
|
143
143
|
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Ask whether to generate headless UI connectors on top of useAsyncData.
|
|
147
|
+
* Only called when useAsyncData was selected and no config value is present.
|
|
148
|
+
*/
|
|
149
|
+
export async function promptConnectors(): Promise<boolean> {
|
|
150
|
+
const result = await p.confirm({
|
|
151
|
+
message: CHOICES.connectorsPrompt,
|
|
152
|
+
initialValue: false,
|
|
153
|
+
});
|
|
154
|
+
checkCancellation(result);
|
|
155
|
+
return result as boolean;
|
|
156
|
+
}
|
package/src/cli/types.ts
CHANGED
|
@@ -1,50 +1,50 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* TypeScript types for CLI prompts and responses
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Generator backend selector (internal)
|
|
7
|
-
*/
|
|
8
|
-
export type GeneratorBackend = 'official' | 'heyapi';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* Generator engine value for nxh.config — user-facing alias
|
|
12
|
-
* 'openapi' maps to the official Java-based OpenAPI Generator
|
|
13
|
-
* 'heyapi' maps to @hey-api/openapi-ts (Node.js native)
|
|
14
|
-
*/
|
|
15
|
-
export type ConfigGenerator = 'openapi' | 'heyapi';
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Initial input and output paths
|
|
19
|
-
*/
|
|
20
|
-
export interface InitialInputs {
|
|
21
|
-
inputPath: string;
|
|
22
|
-
outputPath: string;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Composables selection (checkbox response)
|
|
27
|
-
*/
|
|
28
|
-
export interface ComposablesSelection {
|
|
29
|
-
composables: Array<'useFetch' | 'useAsyncData' | 'nuxtServer'>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Server route path configuration
|
|
34
|
-
*/
|
|
35
|
-
export interface ServerRouteConfig {
|
|
36
|
-
serverPath: string;
|
|
37
|
-
customPath?: string;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* BFF (Backend for Frontend) configuration
|
|
42
|
-
*/
|
|
43
|
-
export interface BffConfig {
|
|
44
|
-
enableBff: boolean;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Valid composable types
|
|
49
|
-
*/
|
|
50
|
-
export type ComposableType = 'useFetch' | 'useAsyncData' | 'nuxtServer';
|
|
1
|
+
/**
|
|
2
|
+
* TypeScript types for CLI prompts and responses
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generator backend selector (internal)
|
|
7
|
+
*/
|
|
8
|
+
export type GeneratorBackend = 'official' | 'heyapi';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Generator engine value for nxh.config — user-facing alias
|
|
12
|
+
* 'openapi' maps to the official Java-based OpenAPI Generator
|
|
13
|
+
* 'heyapi' maps to @hey-api/openapi-ts (Node.js native)
|
|
14
|
+
*/
|
|
15
|
+
export type ConfigGenerator = 'openapi' | 'heyapi';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Initial input and output paths
|
|
19
|
+
*/
|
|
20
|
+
export interface InitialInputs {
|
|
21
|
+
inputPath: string;
|
|
22
|
+
outputPath: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Composables selection (checkbox response)
|
|
27
|
+
*/
|
|
28
|
+
export interface ComposablesSelection {
|
|
29
|
+
composables: Array<'useFetch' | 'useAsyncData' | 'nuxtServer'>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Server route path configuration
|
|
34
|
+
*/
|
|
35
|
+
export interface ServerRouteConfig {
|
|
36
|
+
serverPath: string;
|
|
37
|
+
customPath?: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* BFF (Backend for Frontend) configuration
|
|
42
|
+
*/
|
|
43
|
+
export interface BffConfig {
|
|
44
|
+
enableBff: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Valid composable types
|
|
49
|
+
*/
|
|
50
|
+
export type ComposableType = 'useFetch' | 'useAsyncData' | 'nuxtServer' | 'connectors';
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import * as path from 'path';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { format } from 'prettier';
|
|
5
|
+
import { analyzeSpec } from '../schema-analyzer/index.js';
|
|
6
|
+
import {
|
|
7
|
+
generateConnectorFile,
|
|
8
|
+
connectorFileName,
|
|
9
|
+
generateConnectorIndexFile,
|
|
10
|
+
} from './templates.js';
|
|
11
|
+
import type { ConnectorGeneratorOptions } from './types.js';
|
|
12
|
+
import { type Logger, createClackLogger } from '../../../cli/logger.js';
|
|
13
|
+
|
|
14
|
+
// Runtime files that must be copied to the user's project
|
|
15
|
+
const RUNTIME_FILES = [
|
|
16
|
+
'useListConnector.ts',
|
|
17
|
+
'useDetailConnector.ts',
|
|
18
|
+
'useFormConnector.ts',
|
|
19
|
+
'useDeleteConnector.ts',
|
|
20
|
+
'zod-error-merger.ts',
|
|
21
|
+
] as const;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Format TypeScript source with Prettier.
|
|
25
|
+
* Falls back to unformatted code on error.
|
|
26
|
+
*/
|
|
27
|
+
async function formatCode(code: string, logger: Logger): Promise<string> {
|
|
28
|
+
try {
|
|
29
|
+
return await format(code, { parser: 'typescript' });
|
|
30
|
+
} catch (error) {
|
|
31
|
+
logger.log.warn(`Prettier formatting failed: ${String(error)}`);
|
|
32
|
+
return code;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Generate headless connector composables from an OpenAPI spec.
|
|
38
|
+
*
|
|
39
|
+
* Steps:
|
|
40
|
+
* 1. Analyze the spec → ResourceMap (Schema Analyzer)
|
|
41
|
+
* 2. For each resource: generate connector source, format, write
|
|
42
|
+
* 3. Write an index barrel file
|
|
43
|
+
* 4. Copy runtime helpers to the user's project
|
|
44
|
+
*/
|
|
45
|
+
export async function generateConnectors(
|
|
46
|
+
options: ConnectorGeneratorOptions,
|
|
47
|
+
logger: Logger = createClackLogger()
|
|
48
|
+
): Promise<void> {
|
|
49
|
+
const spinner = logger.spinner();
|
|
50
|
+
|
|
51
|
+
const outputDir = path.resolve(options.outputDir);
|
|
52
|
+
const composablesRelDir = options.composablesRelDir ?? '../use-async-data';
|
|
53
|
+
const runtimeRelDir = options.runtimeRelDir ?? '../runtime';
|
|
54
|
+
|
|
55
|
+
// ── 1. Analyze spec ───────────────────────────────────────────────────────
|
|
56
|
+
spinner.start('Analyzing OpenAPI spec');
|
|
57
|
+
const resourceMap = analyzeSpec(options.inputSpec);
|
|
58
|
+
spinner.stop(`Found ${resourceMap.size} resource(s)`);
|
|
59
|
+
|
|
60
|
+
if (resourceMap.size === 0) {
|
|
61
|
+
logger.log.warn('No resources found in spec — nothing to generate');
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// ── 2. Prepare output directory ───────────────────────────────────────────
|
|
66
|
+
// emptyDir (not ensureDir) so stale connectors from previous runs are removed.
|
|
67
|
+
// If the resource got renamed in the spec the old file would otherwise linger.
|
|
68
|
+
spinner.start('Preparing output directory');
|
|
69
|
+
await fs.emptyDir(outputDir);
|
|
70
|
+
spinner.stop('Output directory ready');
|
|
71
|
+
|
|
72
|
+
// ── 3. Generate connector files ───────────────────────────────────────────
|
|
73
|
+
spinner.start('Generating connector composables');
|
|
74
|
+
let successCount = 0;
|
|
75
|
+
let errorCount = 0;
|
|
76
|
+
const generatedNames: string[] = [];
|
|
77
|
+
|
|
78
|
+
for (const resource of resourceMap.values()) {
|
|
79
|
+
try {
|
|
80
|
+
const code = generateConnectorFile(resource, composablesRelDir);
|
|
81
|
+
const formatted = await formatCode(code, logger);
|
|
82
|
+
const fileName = connectorFileName(resource.composableName);
|
|
83
|
+
const filePath = path.join(outputDir, fileName);
|
|
84
|
+
|
|
85
|
+
await fs.writeFile(filePath, formatted, 'utf-8');
|
|
86
|
+
generatedNames.push(resource.composableName);
|
|
87
|
+
successCount++;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
logger.log.error(`Error generating ${resource.composableName}: ${String(error)}`);
|
|
90
|
+
errorCount++;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
spinner.stop(`Generated ${successCount} connector(s)`);
|
|
95
|
+
|
|
96
|
+
// ── 4. Write barrel index ─────────────────────────────────────────────────
|
|
97
|
+
if (generatedNames.length > 0) {
|
|
98
|
+
try {
|
|
99
|
+
const indexCode = generateConnectorIndexFile(generatedNames);
|
|
100
|
+
const formattedIndex = await formatCode(indexCode, logger);
|
|
101
|
+
await fs.writeFile(path.join(outputDir, 'index.ts'), formattedIndex, 'utf-8');
|
|
102
|
+
} catch (error) {
|
|
103
|
+
logger.log.warn(`Could not write connector index: ${String(error)}`);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ── 5. Copy runtime helpers ───────────────────────────────────────────────
|
|
108
|
+
// Runtime files (useListConnector, useFormConnector …) live in src/ and must
|
|
109
|
+
// be physical .ts files in the user's project so Nuxt/Vite can type-check them.
|
|
110
|
+
//
|
|
111
|
+
// Path resolution trick:
|
|
112
|
+
// • During development (ts-node / tsx): __dirname ≈ src/generators/components/connector-generator/
|
|
113
|
+
// • After `tsc` build: __dirname ≈ dist/generators/components/connector-generator/
|
|
114
|
+
//
|
|
115
|
+
// In both cases we need to land in src/generators/shared/runtime/, so we step
|
|
116
|
+
// up 4 levels and then re-enter src/ explicitly. This works because the repo
|
|
117
|
+
// always keeps src/ alongside dist/ in the published package (see "files" in package.json).
|
|
118
|
+
spinner.start('Copying runtime files');
|
|
119
|
+
const runtimeDir = path.resolve(outputDir, runtimeRelDir);
|
|
120
|
+
await fs.ensureDir(runtimeDir); // ensureDir (not emptyDir) — other runtime files may exist there
|
|
121
|
+
|
|
122
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
123
|
+
const runtimeSrcDir = path.resolve(__dirname, '../../../../src/generators/shared/runtime');
|
|
124
|
+
|
|
125
|
+
for (const file of RUNTIME_FILES) {
|
|
126
|
+
const src = path.join(runtimeSrcDir, file);
|
|
127
|
+
const dest = path.join(runtimeDir, file);
|
|
128
|
+
await fs.copyFile(src, dest);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
spinner.stop('Runtime files copied');
|
|
132
|
+
|
|
133
|
+
// ── 6. Summary ────────────────────────────────────────────────────────────
|
|
134
|
+
if (errorCount > 0) {
|
|
135
|
+
logger.log.warn(`Completed with ${errorCount} error(s)`);
|
|
136
|
+
}
|
|
137
|
+
logger.log.success(`Generated ${successCount} connector(s) in ${outputDir}`);
|
|
138
|
+
}
|