nuxt-openapi-hyperfetch 0.1.7-alpha.1 → 0.2.7-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/CONTRIBUTING.md +291 -292
- package/INSTRUCTIONS.md +327 -327
- package/LICENSE +202 -202
- package/README.md +231 -227
- package/dist/cli/logger.d.ts +26 -0
- package/dist/cli/logger.js +36 -0
- package/dist/cli/logo.js +5 -5
- package/dist/generators/components/connector-generator/generator.d.ts +12 -0
- package/dist/generators/components/connector-generator/generator.js +116 -0
- package/dist/generators/components/connector-generator/templates.d.ts +18 -0
- package/dist/generators/components/connector-generator/templates.js +222 -0
- package/dist/generators/components/connector-generator/types.d.ts +32 -0
- package/dist/generators/components/connector-generator/types.js +7 -0
- package/dist/generators/components/schema-analyzer/index.d.ts +17 -0
- package/dist/generators/components/schema-analyzer/index.js +20 -0
- package/dist/generators/components/schema-analyzer/intent-detector.d.ts +17 -0
- package/dist/generators/components/schema-analyzer/intent-detector.js +143 -0
- package/dist/generators/components/schema-analyzer/openapi-reader.d.ts +11 -0
- package/dist/generators/components/schema-analyzer/openapi-reader.js +76 -0
- package/dist/generators/components/schema-analyzer/resource-grouper.d.ts +6 -0
- package/dist/generators/components/schema-analyzer/resource-grouper.js +132 -0
- package/dist/generators/components/schema-analyzer/schema-field-mapper.d.ts +35 -0
- package/dist/generators/components/schema-analyzer/schema-field-mapper.js +220 -0
- package/dist/generators/components/schema-analyzer/types.d.ts +156 -0
- package/dist/generators/components/schema-analyzer/types.js +7 -0
- package/dist/generators/nuxt-server/generator.d.ts +2 -1
- package/dist/generators/nuxt-server/generator.js +21 -21
- package/dist/generators/shared/runtime/apiHelpers.d.ts +81 -41
- package/dist/generators/shared/runtime/apiHelpers.js +97 -104
- package/dist/generators/shared/runtime/pagination.d.ts +168 -0
- package/dist/generators/shared/runtime/pagination.js +179 -0
- package/dist/generators/shared/runtime/useDeleteConnector.d.ts +16 -0
- package/dist/generators/shared/runtime/useDeleteConnector.js +93 -0
- package/dist/generators/shared/runtime/useDetailConnector.d.ts +14 -0
- package/dist/generators/shared/runtime/useDetailConnector.js +50 -0
- package/dist/generators/shared/runtime/useFormConnector.d.ts +19 -0
- package/dist/generators/shared/runtime/useFormConnector.js +113 -0
- package/dist/generators/shared/runtime/useListConnector.d.ts +25 -0
- package/dist/generators/shared/runtime/useListConnector.js +125 -0
- package/dist/generators/shared/runtime/zod-error-merger.d.ts +23 -0
- package/dist/generators/shared/runtime/zod-error-merger.js +106 -0
- package/dist/generators/shared/templates/api-callbacks-plugin.js +54 -11
- package/dist/generators/shared/templates/api-pagination-plugin.d.ts +51 -0
- package/dist/generators/shared/templates/api-pagination-plugin.js +152 -0
- package/dist/generators/use-async-data/generator.d.ts +2 -1
- package/dist/generators/use-async-data/generator.js +14 -14
- package/dist/generators/use-async-data/runtime/useApiAsyncData.js +114 -13
- package/dist/generators/use-async-data/runtime/useApiAsyncDataRaw.js +88 -10
- package/dist/generators/use-async-data/templates.js +17 -17
- package/dist/generators/use-fetch/generator.d.ts +2 -1
- package/dist/generators/use-fetch/generator.js +12 -12
- package/dist/generators/use-fetch/runtime/useApiRequest.js +149 -40
- package/dist/generators/use-fetch/templates.js +14 -14
- package/dist/index.js +25 -0
- package/dist/module/index.d.ts +4 -0
- package/dist/module/index.js +93 -0
- package/dist/module/types.d.ts +27 -0
- package/dist/module/types.js +1 -0
- package/docs/API-REFERENCE.md +886 -887
- package/docs/generated-components.md +615 -0
- package/docs/headless-composables-ui.md +569 -0
- package/eslint.config.js +13 -0
- package/package.json +29 -2
- package/src/cli/config.ts +140 -140
- package/src/cli/logger.ts +124 -66
- package/src/cli/logo.ts +25 -25
- 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 -270
- 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 -204
- 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 -169
- package/src/generators/use-fetch/runtime/useApiRequest.ts +354 -234
- package/src/generators/use-fetch/templates.ts +214 -214
- package/src/index.ts +303 -265
- package/src/module/index.ts +133 -0
- package/src/module/types.ts +31 -0
- package/src/generators/tanstack-query/generator.ts +0 -11
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
/**
|
|
3
|
+
* Global API Pagination Plugin
|
|
4
|
+
*
|
|
5
|
+
* ⚠️ IMPORTANT: This file is NEVER regenerated - your changes are safe!
|
|
6
|
+
*
|
|
7
|
+
* Configure the global pagination convention for all paginated API requests.
|
|
8
|
+
* Each composable can override this config locally via `paginationConfig` option.
|
|
9
|
+
*
|
|
10
|
+
* ── HOW IT WORKS ──────────────────────────────────────────────────────────────
|
|
11
|
+
*
|
|
12
|
+
* When you call a composable with `paginated: true`, the wrapper will:
|
|
13
|
+
* 1. Inject page/perPage params into the request (query, body, or headers)
|
|
14
|
+
* 2. After the response, read total/totalPages/currentPage/perPage from the
|
|
15
|
+
* response headers or JSON body, according to `metaSource`
|
|
16
|
+
* 3. Expose `pagination`, `goToPage()`, `nextPage()`, `prevPage()`, `setPerPage()`
|
|
17
|
+
*
|
|
18
|
+
* ── USAGE ─────────────────────────────────────────────────────────────────────
|
|
19
|
+
*
|
|
20
|
+
* // In your page/component:
|
|
21
|
+
* const { data, pagination, goToPage, nextPage, prevPage, setPerPage } =
|
|
22
|
+
* useGetPets(params, { paginated: true })
|
|
23
|
+
*
|
|
24
|
+
* // pagination is a computed ref:
|
|
25
|
+
* pagination.value.currentPage // current page
|
|
26
|
+
* pagination.value.totalPages // total pages
|
|
27
|
+
* pagination.value.total // total items
|
|
28
|
+
* pagination.value.perPage // items per page
|
|
29
|
+
* pagination.value.hasNextPage // boolean
|
|
30
|
+
* pagination.value.hasPrevPage // boolean
|
|
31
|
+
*
|
|
32
|
+
* // Navigation helpers — automatically trigger re-fetch:
|
|
33
|
+
* goToPage(3)
|
|
34
|
+
* nextPage()
|
|
35
|
+
* prevPage()
|
|
36
|
+
* setPerPage(50)
|
|
37
|
+
*
|
|
38
|
+
* ── PRIORITY ──────────────────────────────────────────────────────────────────
|
|
39
|
+
*
|
|
40
|
+
* Per-call paginationConfig > this plugin's global config > built-in defaults
|
|
41
|
+
*
|
|
42
|
+
* Example of per-call override:
|
|
43
|
+
* useGetPets(params, {
|
|
44
|
+
* paginated: true,
|
|
45
|
+
* paginationConfig: {
|
|
46
|
+
* meta: { metaSource: 'headers', fields: { ... } },
|
|
47
|
+
* request: { sendAs: 'query', params: { page: 'p', perPage: 'size' }, defaults: { page: 1, perPage: 10 } }
|
|
48
|
+
* }
|
|
49
|
+
* })
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
export default defineNuxtPlugin(() => {
|
|
53
|
+
// Uncomment and configure ONE of the examples below that matches your backend.
|
|
54
|
+
|
|
55
|
+
// ============================================================================
|
|
56
|
+
// EXAMPLE 1 — Query params + response body (most common REST pattern)
|
|
57
|
+
//
|
|
58
|
+
// Request: GET /pets?page=1&limit=20
|
|
59
|
+
// Response: { data: [...], total: 100, totalPages: 5, currentPage: 1, perPage: 20 }
|
|
60
|
+
// ============================================================================
|
|
61
|
+
// const paginationConfig = {
|
|
62
|
+
// meta: {
|
|
63
|
+
// metaSource: 'body',
|
|
64
|
+
// fields: {
|
|
65
|
+
// total: 'total',
|
|
66
|
+
// totalPages: 'totalPages',
|
|
67
|
+
// currentPage: 'currentPage',
|
|
68
|
+
// perPage: 'perPage',
|
|
69
|
+
// dataKey: 'data', // response.data contains the actual array
|
|
70
|
+
// },
|
|
71
|
+
// },
|
|
72
|
+
// request: {
|
|
73
|
+
// sendAs: 'query',
|
|
74
|
+
// params: { page: 'page', perPage: 'limit' },
|
|
75
|
+
// defaults: { page: 1, perPage: 20 },
|
|
76
|
+
// },
|
|
77
|
+
// };
|
|
78
|
+
|
|
79
|
+
// ============================================================================
|
|
80
|
+
// EXAMPLE 2 — Laravel paginate() convention
|
|
81
|
+
//
|
|
82
|
+
// Request: GET /pets?page=1&per_page=15
|
|
83
|
+
// Response: { data: [...], meta: { total: 100, last_page: 7, current_page: 1, per_page: 15 } }
|
|
84
|
+
// ============================================================================
|
|
85
|
+
// const paginationConfig = {
|
|
86
|
+
// meta: {
|
|
87
|
+
// metaSource: 'body',
|
|
88
|
+
// fields: {
|
|
89
|
+
// total: 'meta.total',
|
|
90
|
+
// totalPages: 'meta.last_page',
|
|
91
|
+
// currentPage: 'meta.current_page',
|
|
92
|
+
// perPage: 'meta.per_page',
|
|
93
|
+
// dataKey: 'data',
|
|
94
|
+
// },
|
|
95
|
+
// },
|
|
96
|
+
// request: {
|
|
97
|
+
// sendAs: 'query',
|
|
98
|
+
// params: { page: 'page', perPage: 'per_page' },
|
|
99
|
+
// defaults: { page: 1, perPage: 15 },
|
|
100
|
+
// },
|
|
101
|
+
// };
|
|
102
|
+
|
|
103
|
+
// ============================================================================
|
|
104
|
+
// EXAMPLE 3 — HTTP response headers convention
|
|
105
|
+
//
|
|
106
|
+
// Request: GET /pets?page=1&limit=20
|
|
107
|
+
// Response headers: X-Total-Count: 100, X-Total-Pages: 5, X-Page: 1, X-Per-Page: 20
|
|
108
|
+
// ============================================================================
|
|
109
|
+
// const paginationConfig = {
|
|
110
|
+
// meta: {
|
|
111
|
+
// metaSource: 'headers',
|
|
112
|
+
// fields: {
|
|
113
|
+
// total: 'X-Total-Count',
|
|
114
|
+
// totalPages: 'X-Total-Pages',
|
|
115
|
+
// currentPage: 'X-Page',
|
|
116
|
+
// perPage: 'X-Per-Page',
|
|
117
|
+
// // No dataKey needed — response body is the array directly
|
|
118
|
+
// },
|
|
119
|
+
// },
|
|
120
|
+
// request: {
|
|
121
|
+
// sendAs: 'query',
|
|
122
|
+
// params: { page: 'page', perPage: 'limit' },
|
|
123
|
+
// defaults: { page: 1, perPage: 20 },
|
|
124
|
+
// },
|
|
125
|
+
// };
|
|
126
|
+
|
|
127
|
+
// ============================================================================
|
|
128
|
+
// EXAMPLE 4 — POST-as-search (body pagination)
|
|
129
|
+
//
|
|
130
|
+
// Request: POST /pets/search body: { filters: {...}, page: 1, pageSize: 20 }
|
|
131
|
+
// Response: { items: [...], total: 100, pages: 5 }
|
|
132
|
+
// ============================================================================
|
|
133
|
+
// const paginationConfig = {
|
|
134
|
+
// meta: {
|
|
135
|
+
// metaSource: 'body',
|
|
136
|
+
// fields: {
|
|
137
|
+
// total: 'total',
|
|
138
|
+
// totalPages: 'pages',
|
|
139
|
+
// currentPage: 'page',
|
|
140
|
+
// perPage: 'pageSize',
|
|
141
|
+
// dataKey: 'items',
|
|
142
|
+
// },
|
|
143
|
+
// },
|
|
144
|
+
// request: {
|
|
145
|
+
// sendAs: 'body',
|
|
146
|
+
// params: { page: 'page', perPage: 'pageSize' },
|
|
147
|
+
// defaults: { page: 1, perPage: 20 },
|
|
148
|
+
// },
|
|
149
|
+
// };
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
provide: {
|
|
153
|
+
// Expose the config so the runtime wrappers can read it.
|
|
154
|
+
// Uncomment this line once you've configured paginationConfig above:
|
|
155
|
+
// getGlobalApiPagination: () => paginationConfig,
|
|
156
|
+
},
|
|
157
|
+
};
|
|
158
|
+
});
|
|
@@ -1,204 +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 {
|
|
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
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
const
|
|
70
|
-
const
|
|
71
|
-
|
|
72
|
-
await fs.
|
|
73
|
-
await fs.ensureDir(
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
//
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
let
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
const
|
|
124
|
-
const
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
const
|
|
139
|
-
const
|
|
140
|
-
const
|
|
141
|
-
const
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
const
|
|
156
|
-
await
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
}
|
|
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
|
+
}
|