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,166 +1,166 @@
|
|
|
1
|
-
import { pascalCase, camelCase } from 'change-case';
|
|
2
|
-
import type { EndpointInfo, OpenApiSpec, ResourceInfo, ResourceMap } from './types.js';
|
|
3
|
-
import { extractEndpoints } from './intent-detector.js';
|
|
4
|
-
import {
|
|
5
|
-
mapFieldsFromSchema,
|
|
6
|
-
buildZodSchema,
|
|
7
|
-
mapColumnsFromSchema,
|
|
8
|
-
} from './schema-field-mapper.js';
|
|
9
|
-
|
|
10
|
-
// ─── Naming helpers ───────────────────────────────────────────────────────────
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Derive a plural connector composable name from a tag.
|
|
14
|
-
* 'pet' → 'usePetsConnector'
|
|
15
|
-
* 'store' → 'useStoreConnector' (already ends in e, avoid double-s)
|
|
16
|
-
*/
|
|
17
|
-
function toConnectorName(tag: string): string {
|
|
18
|
-
const pascal = pascalCase(tag);
|
|
19
|
-
// Simple English plural: if ends in 's', 'x', 'z', 'ch', 'sh' → +es
|
|
20
|
-
// otherwise → +s. Good enough for API resource names.
|
|
21
|
-
const plural = /(?:s|x|z|ch|sh)$/i.test(pascal) ? `${pascal}es` : `${pascal}s`;
|
|
22
|
-
return `use${plural}Connector`;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Primary grouping key for an endpoint: first tag, or path prefix as fallback.
|
|
27
|
-
* '/pets/{id}' → 'pets'
|
|
28
|
-
*/
|
|
29
|
-
function tagOrPrefix(endpoint: EndpointInfo): string {
|
|
30
|
-
if (endpoint.tags.length > 0) {
|
|
31
|
-
return endpoint.tags[0];
|
|
32
|
-
}
|
|
33
|
-
// Path prefix: first non-empty segment, lower-cased
|
|
34
|
-
const segment = endpoint.path.split('/').find((s) => s && !s.startsWith('{'));
|
|
35
|
-
return segment ?? 'unknown';
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// ─── Pick the "best" endpoint for each intent ─────────────────────────────────
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* When a resource has multiple endpoints with the same intent, pick the
|
|
42
|
-
* simplest one (fewest path params, then shortest path).
|
|
43
|
-
*
|
|
44
|
-
* Example: if both GET /pets and GET /users/{id}/pets detect as 'list',
|
|
45
|
-
* we prefer GET /pets (0 path params, shorter path).
|
|
46
|
-
*/
|
|
47
|
-
function pickBest(endpoints: EndpointInfo[]): EndpointInfo {
|
|
48
|
-
return endpoints.sort(
|
|
49
|
-
(a, b) => a.pathParams.length - b.pathParams.length || a.path.length - b.path.length
|
|
50
|
-
)[0];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// ─── Main grouper ─────────────────────────────────────────────────────────────
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Parse the entire OpenAPI spec and produce one ResourceInfo per resource.
|
|
57
|
-
* A resource is a group of endpoints that share the same tag (or path prefix).
|
|
58
|
-
*/
|
|
59
|
-
export function buildResourceMap(spec: OpenApiSpec): ResourceMap {
|
|
60
|
-
// 1. Collect all endpoints
|
|
61
|
-
const allEndpoints: EndpointInfo[] = [];
|
|
62
|
-
|
|
63
|
-
for (const [path, pathItem] of Object.entries(spec.paths)) {
|
|
64
|
-
// pathItem is already $ref-resolved at this point
|
|
65
|
-
const endpoints = extractEndpoints(
|
|
66
|
-
path,
|
|
67
|
-
pathItem as unknown as Record<string, import('./types.js').OpenApiOperation>
|
|
68
|
-
);
|
|
69
|
-
allEndpoints.push(...endpoints);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
// 2. Group by tag / prefix
|
|
73
|
-
const groups = new Map<string, EndpointInfo[]>();
|
|
74
|
-
for (const ep of allEndpoints) {
|
|
75
|
-
const key = tagOrPrefix(ep);
|
|
76
|
-
if (!groups.has(key)) {
|
|
77
|
-
groups.set(key, []);
|
|
78
|
-
}
|
|
79
|
-
groups.get(key)!.push(ep);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
// 3. Build one ResourceInfo per group
|
|
83
|
-
const resourceMap: ResourceMap = new Map();
|
|
84
|
-
|
|
85
|
-
for (const [tag, endpoints] of groups) {
|
|
86
|
-
const byIntent = groupByIntent(endpoints);
|
|
87
|
-
|
|
88
|
-
const listEp = byIntent.list ? pickBest(byIntent.list) : undefined;
|
|
89
|
-
const detailEp = byIntent.detail ? pickBest(byIntent.detail) : undefined;
|
|
90
|
-
const createEp = byIntent.create ? pickBest(byIntent.create) : undefined;
|
|
91
|
-
const updateEp = byIntent.update ? pickBest(byIntent.update) : undefined;
|
|
92
|
-
const deleteEp = byIntent.delete ? pickBest(byIntent.delete) : undefined;
|
|
93
|
-
|
|
94
|
-
// Infer columns from list > detail response schema
|
|
95
|
-
const schemaForColumns = listEp?.responseSchema ?? detailEp?.responseSchema;
|
|
96
|
-
const columns = schemaForColumns ? mapColumnsFromSchema(schemaForColumns) : [];
|
|
97
|
-
|
|
98
|
-
// Form fields + Zod schemas
|
|
99
|
-
const createFields = createEp?.requestBodySchema
|
|
100
|
-
? mapFieldsFromSchema(createEp.requestBodySchema)
|
|
101
|
-
: undefined;
|
|
102
|
-
const updateFields = updateEp?.requestBodySchema
|
|
103
|
-
? mapFieldsFromSchema(updateEp.requestBodySchema)
|
|
104
|
-
: undefined;
|
|
105
|
-
|
|
106
|
-
const createZod = createEp?.requestBodySchema
|
|
107
|
-
? buildZodSchema(createEp.requestBodySchema)
|
|
108
|
-
: undefined;
|
|
109
|
-
const updateZod = updateEp?.requestBodySchema
|
|
110
|
-
? buildZodSchema(updateEp.requestBodySchema)
|
|
111
|
-
: undefined;
|
|
112
|
-
|
|
113
|
-
const resourceName = pascalCase(tag);
|
|
114
|
-
|
|
115
|
-
const info: ResourceInfo = {
|
|
116
|
-
name: resourceName,
|
|
117
|
-
tag,
|
|
118
|
-
composableName: toConnectorName(tag),
|
|
119
|
-
endpoints,
|
|
120
|
-
listEndpoint: listEp,
|
|
121
|
-
detailEndpoint: detailEp,
|
|
122
|
-
createEndpoint: createEp,
|
|
123
|
-
updateEndpoint: updateEp,
|
|
124
|
-
deleteEndpoint: deleteEp,
|
|
125
|
-
columns,
|
|
126
|
-
formFields: {
|
|
127
|
-
...(createFields ? { create: createFields } : {}),
|
|
128
|
-
...(updateFields ? { update: updateFields } : {}),
|
|
129
|
-
},
|
|
130
|
-
zodSchemas: {
|
|
131
|
-
...(createZod ? { create: createZod } : {}),
|
|
132
|
-
...(updateZod ? { update: updateZod } : {}),
|
|
133
|
-
},
|
|
134
|
-
};
|
|
135
|
-
|
|
136
|
-
// Map key uses camelCase of the tag (e.g. 'petStore') to be a valid JS identifier.
|
|
137
|
-
// resource.name uses PascalCase ('PetStore') for use in type/class names.
|
|
138
|
-
// resource.tag preserves the original casing from the spec ('petStore' or 'pet_store').
|
|
139
|
-
resourceMap.set(camelCase(tag), info);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return resourceMap;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
// ─── Helper ───────────────────────────────────────────────────────────────────
|
|
146
|
-
|
|
147
|
-
type IntentGroups = Partial<Record<EndpointInfo['intent'], EndpointInfo[]>>;
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* Group endpoints by their detected intent.
|
|
151
|
-
* Endpoints with intent 'unknown' (e.g. custom actions like POST /pets/{id}/upload)
|
|
152
|
-
* are silently skipped — they do not map to a standard CRUD connector.
|
|
153
|
-
*/
|
|
154
|
-
function groupByIntent(endpoints: EndpointInfo[]): IntentGroups {
|
|
155
|
-
const result: IntentGroups = {};
|
|
156
|
-
for (const ep of endpoints) {
|
|
157
|
-
if (ep.intent === 'unknown') {
|
|
158
|
-
continue;
|
|
159
|
-
}
|
|
160
|
-
if (!result[ep.intent]) {
|
|
161
|
-
result[ep.intent] = [];
|
|
162
|
-
}
|
|
163
|
-
result[ep.intent]!.push(ep);
|
|
164
|
-
}
|
|
165
|
-
return result;
|
|
166
|
-
}
|
|
1
|
+
import { pascalCase, camelCase } from 'change-case';
|
|
2
|
+
import type { EndpointInfo, OpenApiSpec, ResourceInfo, ResourceMap } from './types.js';
|
|
3
|
+
import { extractEndpoints } from './intent-detector.js';
|
|
4
|
+
import {
|
|
5
|
+
mapFieldsFromSchema,
|
|
6
|
+
buildZodSchema,
|
|
7
|
+
mapColumnsFromSchema,
|
|
8
|
+
} from './schema-field-mapper.js';
|
|
9
|
+
|
|
10
|
+
// ─── Naming helpers ───────────────────────────────────────────────────────────
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Derive a plural connector composable name from a tag.
|
|
14
|
+
* 'pet' → 'usePetsConnector'
|
|
15
|
+
* 'store' → 'useStoreConnector' (already ends in e, avoid double-s)
|
|
16
|
+
*/
|
|
17
|
+
function toConnectorName(tag: string): string {
|
|
18
|
+
const pascal = pascalCase(tag);
|
|
19
|
+
// Simple English plural: if ends in 's', 'x', 'z', 'ch', 'sh' → +es
|
|
20
|
+
// otherwise → +s. Good enough for API resource names.
|
|
21
|
+
const plural = /(?:s|x|z|ch|sh)$/i.test(pascal) ? `${pascal}es` : `${pascal}s`;
|
|
22
|
+
return `use${plural}Connector`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Primary grouping key for an endpoint: first tag, or path prefix as fallback.
|
|
27
|
+
* '/pets/{id}' → 'pets'
|
|
28
|
+
*/
|
|
29
|
+
function tagOrPrefix(endpoint: EndpointInfo): string {
|
|
30
|
+
if (endpoint.tags.length > 0) {
|
|
31
|
+
return endpoint.tags[0];
|
|
32
|
+
}
|
|
33
|
+
// Path prefix: first non-empty segment, lower-cased
|
|
34
|
+
const segment = endpoint.path.split('/').find((s) => s && !s.startsWith('{'));
|
|
35
|
+
return segment ?? 'unknown';
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ─── Pick the "best" endpoint for each intent ─────────────────────────────────
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* When a resource has multiple endpoints with the same intent, pick the
|
|
42
|
+
* simplest one (fewest path params, then shortest path).
|
|
43
|
+
*
|
|
44
|
+
* Example: if both GET /pets and GET /users/{id}/pets detect as 'list',
|
|
45
|
+
* we prefer GET /pets (0 path params, shorter path).
|
|
46
|
+
*/
|
|
47
|
+
function pickBest(endpoints: EndpointInfo[]): EndpointInfo {
|
|
48
|
+
return endpoints.sort(
|
|
49
|
+
(a, b) => a.pathParams.length - b.pathParams.length || a.path.length - b.path.length
|
|
50
|
+
)[0];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ─── Main grouper ─────────────────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Parse the entire OpenAPI spec and produce one ResourceInfo per resource.
|
|
57
|
+
* A resource is a group of endpoints that share the same tag (or path prefix).
|
|
58
|
+
*/
|
|
59
|
+
export function buildResourceMap(spec: OpenApiSpec): ResourceMap {
|
|
60
|
+
// 1. Collect all endpoints
|
|
61
|
+
const allEndpoints: EndpointInfo[] = [];
|
|
62
|
+
|
|
63
|
+
for (const [path, pathItem] of Object.entries(spec.paths)) {
|
|
64
|
+
// pathItem is already $ref-resolved at this point
|
|
65
|
+
const endpoints = extractEndpoints(
|
|
66
|
+
path,
|
|
67
|
+
pathItem as unknown as Record<string, import('./types.js').OpenApiOperation>
|
|
68
|
+
);
|
|
69
|
+
allEndpoints.push(...endpoints);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// 2. Group by tag / prefix
|
|
73
|
+
const groups = new Map<string, EndpointInfo[]>();
|
|
74
|
+
for (const ep of allEndpoints) {
|
|
75
|
+
const key = tagOrPrefix(ep);
|
|
76
|
+
if (!groups.has(key)) {
|
|
77
|
+
groups.set(key, []);
|
|
78
|
+
}
|
|
79
|
+
groups.get(key)!.push(ep);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// 3. Build one ResourceInfo per group
|
|
83
|
+
const resourceMap: ResourceMap = new Map();
|
|
84
|
+
|
|
85
|
+
for (const [tag, endpoints] of groups) {
|
|
86
|
+
const byIntent = groupByIntent(endpoints);
|
|
87
|
+
|
|
88
|
+
const listEp = byIntent.list ? pickBest(byIntent.list) : undefined;
|
|
89
|
+
const detailEp = byIntent.detail ? pickBest(byIntent.detail) : undefined;
|
|
90
|
+
const createEp = byIntent.create ? pickBest(byIntent.create) : undefined;
|
|
91
|
+
const updateEp = byIntent.update ? pickBest(byIntent.update) : undefined;
|
|
92
|
+
const deleteEp = byIntent.delete ? pickBest(byIntent.delete) : undefined;
|
|
93
|
+
|
|
94
|
+
// Infer columns from list > detail response schema
|
|
95
|
+
const schemaForColumns = listEp?.responseSchema ?? detailEp?.responseSchema;
|
|
96
|
+
const columns = schemaForColumns ? mapColumnsFromSchema(schemaForColumns) : [];
|
|
97
|
+
|
|
98
|
+
// Form fields + Zod schemas
|
|
99
|
+
const createFields = createEp?.requestBodySchema
|
|
100
|
+
? mapFieldsFromSchema(createEp.requestBodySchema)
|
|
101
|
+
: undefined;
|
|
102
|
+
const updateFields = updateEp?.requestBodySchema
|
|
103
|
+
? mapFieldsFromSchema(updateEp.requestBodySchema)
|
|
104
|
+
: undefined;
|
|
105
|
+
|
|
106
|
+
const createZod = createEp?.requestBodySchema
|
|
107
|
+
? buildZodSchema(createEp.requestBodySchema)
|
|
108
|
+
: undefined;
|
|
109
|
+
const updateZod = updateEp?.requestBodySchema
|
|
110
|
+
? buildZodSchema(updateEp.requestBodySchema)
|
|
111
|
+
: undefined;
|
|
112
|
+
|
|
113
|
+
const resourceName = pascalCase(tag);
|
|
114
|
+
|
|
115
|
+
const info: ResourceInfo = {
|
|
116
|
+
name: resourceName,
|
|
117
|
+
tag,
|
|
118
|
+
composableName: toConnectorName(tag),
|
|
119
|
+
endpoints,
|
|
120
|
+
listEndpoint: listEp,
|
|
121
|
+
detailEndpoint: detailEp,
|
|
122
|
+
createEndpoint: createEp,
|
|
123
|
+
updateEndpoint: updateEp,
|
|
124
|
+
deleteEndpoint: deleteEp,
|
|
125
|
+
columns,
|
|
126
|
+
formFields: {
|
|
127
|
+
...(createFields ? { create: createFields } : {}),
|
|
128
|
+
...(updateFields ? { update: updateFields } : {}),
|
|
129
|
+
},
|
|
130
|
+
zodSchemas: {
|
|
131
|
+
...(createZod ? { create: createZod } : {}),
|
|
132
|
+
...(updateZod ? { update: updateZod } : {}),
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
// Map key uses camelCase of the tag (e.g. 'petStore') to be a valid JS identifier.
|
|
137
|
+
// resource.name uses PascalCase ('PetStore') for use in type/class names.
|
|
138
|
+
// resource.tag preserves the original casing from the spec ('petStore' or 'pet_store').
|
|
139
|
+
resourceMap.set(camelCase(tag), info);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return resourceMap;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ─── Helper ───────────────────────────────────────────────────────────────────
|
|
146
|
+
|
|
147
|
+
type IntentGroups = Partial<Record<EndpointInfo['intent'], EndpointInfo[]>>;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Group endpoints by their detected intent.
|
|
151
|
+
* Endpoints with intent 'unknown' (e.g. custom actions like POST /pets/{id}/upload)
|
|
152
|
+
* are silently skipped — they do not map to a standard CRUD connector.
|
|
153
|
+
*/
|
|
154
|
+
function groupByIntent(endpoints: EndpointInfo[]): IntentGroups {
|
|
155
|
+
const result: IntentGroups = {};
|
|
156
|
+
for (const ep of endpoints) {
|
|
157
|
+
if (ep.intent === 'unknown') {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (!result[ep.intent]) {
|
|
161
|
+
result[ep.intent] = [];
|
|
162
|
+
}
|
|
163
|
+
result[ep.intent]!.push(ep);
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
}
|