@acrool/rtk-query-codegen-openapi 0.0.2-test.9 → 0.0.2
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 +4 -8
- package/lib/index.d.ts +4 -8
- package/lib/index.js +113 -377
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +113 -377
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +127 -327
- package/src/generators/react-hooks.ts +2 -2
- package/src/index.ts +9 -152
- package/src/types.ts +1 -9
package/src/index.ts
CHANGED
|
@@ -4,84 +4,10 @@ import path from 'node:path';
|
|
|
4
4
|
import { generateApi } from './generate';
|
|
5
5
|
import type { CommonOptions, ConfigFile, GenerationOptions, OutputFileOptions } from './types';
|
|
6
6
|
import { isValidUrl, prettify } from './utils';
|
|
7
|
-
|
|
8
|
-
export type { OutputFilesConfig, ConfigFile } from './types';
|
|
7
|
+
export type { ConfigFile } from './types';
|
|
9
8
|
|
|
10
9
|
const require = createRequire(__filename);
|
|
11
10
|
|
|
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
|
-
|
|
85
11
|
export async function generateEndpoints(options: GenerationOptions): Promise<string | void> {
|
|
86
12
|
const schemaLocation = options.schemaFile;
|
|
87
13
|
|
|
@@ -94,15 +20,8 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
|
|
|
94
20
|
});
|
|
95
21
|
const { outputFile, prettierConfigFile } = options;
|
|
96
22
|
if (outputFile) {
|
|
97
|
-
const outputPath = path.resolve(process.cwd(), outputFile);
|
|
98
|
-
await ensureDirectoryExists(outputPath);
|
|
99
|
-
|
|
100
|
-
// 確保基礎文件存在
|
|
101
|
-
const outputDir = path.dirname(outputPath);
|
|
102
|
-
await ensureBaseFilesExist(outputDir);
|
|
103
|
-
|
|
104
23
|
fs.writeFileSync(
|
|
105
|
-
|
|
24
|
+
path.resolve(process.cwd(), outputFile),
|
|
106
25
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
107
26
|
);
|
|
108
27
|
} else {
|
|
@@ -110,80 +29,18 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
|
|
|
110
29
|
}
|
|
111
30
|
}
|
|
112
31
|
|
|
113
|
-
|
|
114
32
|
export function parseConfig(fullConfig: ConfigFile) {
|
|
115
33
|
const outFiles: (CommonOptions & OutputFileOptions)[] = [];
|
|
116
34
|
|
|
117
35
|
if ('outputFiles' in fullConfig) {
|
|
118
36
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const patterns = config.groupMatch;
|
|
127
|
-
const filterEndpoint = config.filterEndpoint;
|
|
128
|
-
|
|
129
|
-
const pattern = patterns;
|
|
130
|
-
// 根據路徑自動分類
|
|
131
|
-
const groupedPaths = paths.reduce((acc, path) => {
|
|
132
|
-
const groupName = getGroupNameFromPath(path, pattern);
|
|
133
|
-
if (!acc[groupName]) {
|
|
134
|
-
acc[groupName] = [];
|
|
135
|
-
}
|
|
136
|
-
acc[groupName].push(path);
|
|
137
|
-
return acc;
|
|
138
|
-
}, {} as Record<string, string[]>);
|
|
139
|
-
|
|
140
|
-
// 為每個分類生成配置
|
|
141
|
-
Object.entries(groupedPaths).forEach(([groupName, paths]) => {
|
|
142
|
-
const finalOutputPath = outputPath.replace('$1', groupName);
|
|
143
|
-
|
|
144
|
-
if (filterEndpoint) {
|
|
145
|
-
// 如果有 filterEndpoint,使用基於路徑的篩選函數
|
|
146
|
-
const pathBasedFilter = (operationName: string, operationDefinition: any) => {
|
|
147
|
-
const path = operationDefinition.path;
|
|
148
|
-
|
|
149
|
-
// 檢查路徑是否匹配當前分組
|
|
150
|
-
const pathGroupName = getGroupNameFromPath(path, pattern);
|
|
151
|
-
if (pathGroupName !== groupName) {
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// 使用 filterEndpoint 進行額外篩選
|
|
156
|
-
const endpointFilter = filterEndpoint(groupName);
|
|
157
|
-
if (endpointFilter instanceof RegExp) {
|
|
158
|
-
return endpointFilter.test(operationName);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
return true;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
outFiles.push({
|
|
165
|
-
...commonConfig,
|
|
166
|
-
outputFile: finalOutputPath,
|
|
167
|
-
filterEndpoints: pathBasedFilter,
|
|
168
|
-
});
|
|
169
|
-
} else {
|
|
170
|
-
// 如果沒有 filterEndpoint,只使用路徑分組
|
|
171
|
-
const pathBasedFilter = (operationName: string, operationDefinition: any) => {
|
|
172
|
-
const path = operationDefinition.path;
|
|
173
|
-
|
|
174
|
-
// 檢查路徑是否匹配當前分組
|
|
175
|
-
const pathGroupName = getGroupNameFromPath(path, pattern);
|
|
176
|
-
return pathGroupName === groupName;
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
outFiles.push({
|
|
180
|
-
...commonConfig,
|
|
181
|
-
outputFile: finalOutputPath,
|
|
182
|
-
filterEndpoints: pathBasedFilter,
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
|
|
37
|
+
for (const [outputFile, specificConfig] of Object.entries(outputFiles)) {
|
|
38
|
+
outFiles.push({
|
|
39
|
+
...commonConfig,
|
|
40
|
+
...specificConfig,
|
|
41
|
+
outputFile,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
187
44
|
} else {
|
|
188
45
|
outFiles.push(fullConfig);
|
|
189
46
|
}
|
package/src/types.ts
CHANGED
|
@@ -142,18 +142,10 @@ 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
|
-
|
|
152
145
|
export type ConfigFile =
|
|
153
146
|
| Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>>
|
|
154
147
|
| Id<
|
|
155
148
|
Omit<CommonOptions, 'outputFile'> & {
|
|
156
|
-
|
|
157
|
-
outputFiles: OutputFilesConfig
|
|
149
|
+
outputFiles: { [outputFile: string]: Omit<OutputFileOptions, 'outputFile'> };
|
|
158
150
|
}
|
|
159
151
|
>;
|