@acrool/rtk-query-codegen-openapi 0.0.2 → 0.0.6
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/lib/index.d.mts +8 -4
- package/lib/index.d.ts +8 -4
- package/lib/index.js +442 -112
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +442 -112
- package/lib/index.mjs.map +1 -1
- package/package.json +11 -2
- package/src/generate.ts +327 -127
- package/src/generators/react-hooks.ts +2 -2
- package/src/index.ts +250 -9
- package/src/types.ts +9 -1
package/src/index.ts
CHANGED
|
@@ -3,14 +3,177 @@ import { createRequire } from 'node:module';
|
|
|
3
3
|
import path from 'node:path';
|
|
4
4
|
import { generateApi } from './generate';
|
|
5
5
|
import type { CommonOptions, ConfigFile, GenerationOptions, OutputFileOptions } from './types';
|
|
6
|
-
import { isValidUrl, prettify } from './utils';
|
|
7
|
-
|
|
6
|
+
import { isValidUrl, prettify, getV3Doc } from './utils';
|
|
7
|
+
import camelCase from 'lodash.camelcase';
|
|
8
|
+
export type { OutputFilesConfig, ConfigFile } from './types';
|
|
8
9
|
|
|
9
10
|
const require = createRequire(__filename);
|
|
10
11
|
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
// 確保目錄存在的函數
|
|
15
|
+
async function ensureDirectoryExists(filePath: string) {
|
|
16
|
+
const dirname = path.dirname(filePath);
|
|
17
|
+
if (!fs.existsSync(dirname)) {
|
|
18
|
+
await fs.promises.mkdir(dirname, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
// 檢查檔案是否存在的函數
|
|
24
|
+
function fileExists(filePath: string): boolean {
|
|
25
|
+
try {
|
|
26
|
+
return fs.statSync(filePath).isFile();
|
|
27
|
+
} catch {
|
|
28
|
+
return false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// 獲取資料夾名稱並轉換為 API 名稱
|
|
33
|
+
function getApiNameFromDir(dirPath: string): string {
|
|
34
|
+
const dirName = path.basename(dirPath);
|
|
35
|
+
return `${dirName}Api`;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// 確保基礎文件存在的函數
|
|
39
|
+
async function ensureBaseFilesExist(outputDir: string) {
|
|
40
|
+
const enhanceEndpointsPath = path.join(outputDir, 'enhanceEndpoints.ts');
|
|
41
|
+
const indexPath = path.join(outputDir, 'index.ts');
|
|
42
|
+
const apiName = getApiNameFromDir(outputDir);
|
|
43
|
+
|
|
44
|
+
// 如果 enhanceEndpoints.ts 不存在,創建它
|
|
45
|
+
if (!fileExists(enhanceEndpointsPath)) {
|
|
46
|
+
const enhanceEndpointsContent = `import api from './query.generated';
|
|
47
|
+
|
|
48
|
+
const enhancedApi = api.enhanceEndpoints({
|
|
49
|
+
endpoints: {
|
|
50
|
+
},
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
export default enhancedApi;
|
|
54
|
+
`;
|
|
55
|
+
await fs.promises.writeFile(enhanceEndpointsPath, enhanceEndpointsContent, 'utf-8');
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// 如果 index.ts 不存在,創建它
|
|
59
|
+
if (!fileExists(indexPath)) {
|
|
60
|
+
const indexContent = `export * from './query.generated';
|
|
61
|
+
export {default as ${apiName}} from './enhanceEndpoints';
|
|
62
|
+
`;
|
|
63
|
+
await fs.promises.writeFile(indexPath, indexContent, 'utf-8');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
// 從路徑中提取分類名稱
|
|
69
|
+
function getGroupNameFromPath(path: string, pattern: RegExp): string {
|
|
70
|
+
// console.log('pattern', pattern);
|
|
71
|
+
|
|
72
|
+
const match = path.match(pattern);
|
|
73
|
+
// console.log('match', path, match);
|
|
74
|
+
|
|
75
|
+
if (match && match[1]) {
|
|
76
|
+
return camelCase(match[1]);
|
|
77
|
+
}
|
|
78
|
+
return 'common';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
|
|
11
85
|
export async function generateEndpoints(options: GenerationOptions): Promise<string | void> {
|
|
12
86
|
const schemaLocation = options.schemaFile;
|
|
13
87
|
|
|
88
|
+
const schemaAbsPath = isValidUrl(options.schemaFile)
|
|
89
|
+
? options.schemaFile
|
|
90
|
+
: path.resolve(process.cwd(), schemaLocation);
|
|
91
|
+
|
|
92
|
+
// 如果是 URL 且使用 outputFiles 配置,需要特殊處理
|
|
93
|
+
if (isValidUrl(options.schemaFile) && 'outputFiles' in options) {
|
|
94
|
+
const { outputFiles, ...commonConfig } = options as any;
|
|
95
|
+
|
|
96
|
+
// 異步獲取 OpenAPI 文檔
|
|
97
|
+
const openApiDoc = await getV3Doc(options.schemaFile, options.httpResolverOptions);
|
|
98
|
+
const paths = Object.keys(openApiDoc.paths);
|
|
99
|
+
|
|
100
|
+
// 從配置中獲取分類規則
|
|
101
|
+
const outputFilesEntries = Object.entries(outputFiles);
|
|
102
|
+
const [outputPath, config] = outputFilesEntries[0];
|
|
103
|
+
const patterns = (config as any).groupMatch;
|
|
104
|
+
const filterEndpoint = (config as any).filterEndpoint;
|
|
105
|
+
|
|
106
|
+
const pattern = patterns;
|
|
107
|
+
// 根據路徑自動分類
|
|
108
|
+
const groupedPaths = paths.reduce((acc, path) => {
|
|
109
|
+
const groupName = getGroupNameFromPath(path, pattern);
|
|
110
|
+
if (!acc[groupName]) {
|
|
111
|
+
acc[groupName] = [];
|
|
112
|
+
}
|
|
113
|
+
acc[groupName].push(path);
|
|
114
|
+
return acc;
|
|
115
|
+
}, {} as Record<string, string[]>);
|
|
116
|
+
|
|
117
|
+
// 為每個分類生成配置並執行
|
|
118
|
+
for (const [groupName, paths] of Object.entries(groupedPaths)) {
|
|
119
|
+
const finalOutputPath = outputPath.replace('$1', groupName);
|
|
120
|
+
|
|
121
|
+
if (filterEndpoint) {
|
|
122
|
+
// 如果有 filterEndpoint,使用基於路徑的篩選函數
|
|
123
|
+
const pathBasedFilter = (operationName: string, operationDefinition: any) => {
|
|
124
|
+
const path = operationDefinition.path;
|
|
125
|
+
|
|
126
|
+
// 檢查路徑是否匹配當前分組
|
|
127
|
+
const pathGroupName = getGroupNameFromPath(path, pattern);
|
|
128
|
+
if (pathGroupName !== groupName) {
|
|
129
|
+
return false;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 使用 filterEndpoint 進行額外篩選
|
|
133
|
+
const endpointFilter = filterEndpoint(groupName);
|
|
134
|
+
if (endpointFilter instanceof RegExp) {
|
|
135
|
+
return endpointFilter.test(operationName);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return true;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
const groupOptions = {
|
|
142
|
+
...commonConfig,
|
|
143
|
+
outputFile: finalOutputPath,
|
|
144
|
+
filterEndpoints: pathBasedFilter,
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
await generateSingleEndpoint(groupOptions);
|
|
148
|
+
} else {
|
|
149
|
+
// 如果沒有 filterEndpoint,只使用路徑分組
|
|
150
|
+
const pathBasedFilter = (operationName: string, operationDefinition: any) => {
|
|
151
|
+
const path = operationDefinition.path;
|
|
152
|
+
|
|
153
|
+
// 檢查路徑是否匹配當前分組
|
|
154
|
+
const pathGroupName = getGroupNameFromPath(path, pattern);
|
|
155
|
+
return pathGroupName === groupName;
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
const groupOptions = {
|
|
159
|
+
...commonConfig,
|
|
160
|
+
outputFile: finalOutputPath,
|
|
161
|
+
filterEndpoints: pathBasedFilter,
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
await generateSingleEndpoint(groupOptions);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 原有的邏輯處理非 outputFiles 配置或本地文件
|
|
171
|
+
await generateSingleEndpoint(options);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
async function generateSingleEndpoint(options: GenerationOptions): Promise<string | void> {
|
|
175
|
+
const schemaLocation = options.schemaFile;
|
|
176
|
+
|
|
14
177
|
const schemaAbsPath = isValidUrl(options.schemaFile)
|
|
15
178
|
? options.schemaFile
|
|
16
179
|
: path.resolve(process.cwd(), schemaLocation);
|
|
@@ -20,8 +183,15 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
|
|
|
20
183
|
});
|
|
21
184
|
const { outputFile, prettierConfigFile } = options;
|
|
22
185
|
if (outputFile) {
|
|
186
|
+
const outputPath = path.resolve(process.cwd(), outputFile);
|
|
187
|
+
await ensureDirectoryExists(outputPath);
|
|
188
|
+
|
|
189
|
+
// 確保基礎文件存在
|
|
190
|
+
const outputDir = path.dirname(outputPath);
|
|
191
|
+
await ensureBaseFilesExist(outputDir);
|
|
192
|
+
|
|
23
193
|
fs.writeFileSync(
|
|
24
|
-
|
|
194
|
+
outputPath,
|
|
25
195
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
26
196
|
);
|
|
27
197
|
} else {
|
|
@@ -34,13 +204,84 @@ export function parseConfig(fullConfig: ConfigFile) {
|
|
|
34
204
|
|
|
35
205
|
if ('outputFiles' in fullConfig) {
|
|
36
206
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
207
|
+
|
|
208
|
+
// 讀取 OpenAPI 文檔 - 支援 URL 和本地文件
|
|
209
|
+
let openApiDoc: any;
|
|
210
|
+
if (isValidUrl(fullConfig.schemaFile)) {
|
|
211
|
+
// 如果是 URL,直接返回原始配置,讓 generateEndpoints 處理
|
|
212
|
+
outFiles.push(fullConfig as any);
|
|
213
|
+
return outFiles;
|
|
214
|
+
} else {
|
|
215
|
+
// 如果是本地文件,直接讀取
|
|
216
|
+
openApiDoc = JSON.parse(fs.readFileSync(fullConfig.schemaFile, 'utf-8'));
|
|
43
217
|
}
|
|
218
|
+
|
|
219
|
+
const paths = Object.keys(openApiDoc.paths);
|
|
220
|
+
|
|
221
|
+
// 從配置中獲取分類規則
|
|
222
|
+
const outputFilesEntries = Object.entries(outputFiles);
|
|
223
|
+
const [outputPath, config] = outputFilesEntries[0];
|
|
224
|
+
const patterns = (config as any).groupMatch;
|
|
225
|
+
const filterEndpoint = (config as any).filterEndpoint;
|
|
226
|
+
|
|
227
|
+
const pattern = patterns;
|
|
228
|
+
// 根據路徑自動分類
|
|
229
|
+
const groupedPaths = paths.reduce((acc, path) => {
|
|
230
|
+
const groupName = getGroupNameFromPath(path, pattern);
|
|
231
|
+
if (!acc[groupName]) {
|
|
232
|
+
acc[groupName] = [];
|
|
233
|
+
}
|
|
234
|
+
acc[groupName].push(path);
|
|
235
|
+
return acc;
|
|
236
|
+
}, {} as Record<string, string[]>);
|
|
237
|
+
|
|
238
|
+
// 為每個分類生成配置
|
|
239
|
+
Object.entries(groupedPaths).forEach(([groupName, paths]) => {
|
|
240
|
+
const finalOutputPath = outputPath.replace('$1', groupName);
|
|
241
|
+
|
|
242
|
+
if (filterEndpoint) {
|
|
243
|
+
// 如果有 filterEndpoint,使用基於路徑的篩選函數
|
|
244
|
+
const pathBasedFilter = (operationName: string, operationDefinition: any) => {
|
|
245
|
+
const path = operationDefinition.path;
|
|
246
|
+
|
|
247
|
+
// 檢查路徑是否匹配當前分組
|
|
248
|
+
const pathGroupName = getGroupNameFromPath(path, pattern);
|
|
249
|
+
if (pathGroupName !== groupName) {
|
|
250
|
+
return false;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// 使用 filterEndpoint 進行額外篩選
|
|
254
|
+
const endpointFilter = filterEndpoint(groupName);
|
|
255
|
+
if (endpointFilter instanceof RegExp) {
|
|
256
|
+
return endpointFilter.test(operationName);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
return true;
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
outFiles.push({
|
|
263
|
+
...commonConfig,
|
|
264
|
+
outputFile: finalOutputPath,
|
|
265
|
+
filterEndpoints: pathBasedFilter,
|
|
266
|
+
});
|
|
267
|
+
} else {
|
|
268
|
+
// 如果沒有 filterEndpoint,只使用路徑分組
|
|
269
|
+
const pathBasedFilter = (operationName: string, operationDefinition: any) => {
|
|
270
|
+
const path = operationDefinition.path;
|
|
271
|
+
|
|
272
|
+
// 檢查路徑是否匹配當前分組
|
|
273
|
+
const pathGroupName = getGroupNameFromPath(path, pattern);
|
|
274
|
+
return pathGroupName === groupName;
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
outFiles.push({
|
|
278
|
+
...commonConfig,
|
|
279
|
+
outputFile: finalOutputPath,
|
|
280
|
+
filterEndpoints: pathBasedFilter,
|
|
281
|
+
});
|
|
282
|
+
}
|
|
283
|
+
});
|
|
284
|
+
|
|
44
285
|
} else {
|
|
45
286
|
outFiles.push(fullConfig);
|
|
46
287
|
}
|
package/src/types.ts
CHANGED
|
@@ -142,10 +142,18 @@ export type EndpointOverrides = {
|
|
|
142
142
|
parameterFilter: ParameterMatcher;
|
|
143
143
|
}>;
|
|
144
144
|
|
|
145
|
+
export type OutputFilesConfig = {
|
|
146
|
+
[outputFile: string]: {
|
|
147
|
+
groupMatch: RegExp,
|
|
148
|
+
filterEndpoint?: (groupName: string) => RegExp
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
145
152
|
export type ConfigFile =
|
|
146
153
|
| Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>>
|
|
147
154
|
| Id<
|
|
148
155
|
Omit<CommonOptions, 'outputFile'> & {
|
|
149
|
-
outputFiles: { [outputFile: string]: Omit<OutputFileOptions, 'outputFile'> };
|
|
156
|
+
// outputFiles: { [outputFile: string]: Omit<OutputFileOptions, 'outputFile'> };
|
|
157
|
+
outputFiles: OutputFilesConfig
|
|
150
158
|
}
|
|
151
159
|
>;
|