@acrool/rtk-query-codegen-openapi 0.0.2-test.5 → 0.0.2-test.7
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/README.md +1 -0
- package/lib/index.d.mts +9 -4
- package/lib/index.d.ts +9 -4
- package/lib/index.js +220 -24
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +220 -24
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generate.ts +162 -23
- package/src/generators/react-hooks.ts +2 -2
- package/src/index.ts +120 -8
- package/src/types.ts +10 -1
|
@@ -20,7 +20,7 @@ type CreateBindingParams = {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
const createBinding = ({
|
|
23
|
-
operationDefinition: { verb, path
|
|
23
|
+
operationDefinition: { verb, path },
|
|
24
24
|
overrides,
|
|
25
25
|
isLazy = false,
|
|
26
26
|
}: CreateBindingParams) =>
|
|
@@ -28,7 +28,7 @@ const createBinding = ({
|
|
|
28
28
|
undefined,
|
|
29
29
|
undefined,
|
|
30
30
|
factory.createIdentifier(
|
|
31
|
-
`use${isLazy ? 'Lazy' : ''}${capitalize(getOperationName(verb, path,
|
|
31
|
+
`use${isLazy ? 'Lazy' : ''}${capitalize(getOperationName(verb, path, undefined))}${
|
|
32
32
|
isQuery(verb, overrides) ? 'Query' : 'Mutation'
|
|
33
33
|
}`
|
|
34
34
|
),
|
package/src/index.ts
CHANGED
|
@@ -4,10 +4,84 @@ 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
|
-
|
|
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
|
|
|
@@ -20,8 +94,15 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
|
|
|
20
94
|
});
|
|
21
95
|
const { outputFile, prettierConfigFile } = options;
|
|
22
96
|
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
|
+
|
|
23
104
|
fs.writeFileSync(
|
|
24
|
-
|
|
105
|
+
outputPath,
|
|
25
106
|
await prettify(outputFile, sourceCode, prettierConfigFile)
|
|
26
107
|
);
|
|
27
108
|
} else {
|
|
@@ -29,18 +110,49 @@ export async function generateEndpoints(options: GenerationOptions): Promise<str
|
|
|
29
110
|
}
|
|
30
111
|
}
|
|
31
112
|
|
|
113
|
+
|
|
32
114
|
export function parseConfig(fullConfig: ConfigFile) {
|
|
33
115
|
const outFiles: (CommonOptions & OutputFileOptions)[] = [];
|
|
34
116
|
|
|
35
117
|
if ('outputFiles' in fullConfig) {
|
|
36
118
|
const { outputFiles, ...commonConfig } = fullConfig;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
119
|
+
|
|
120
|
+
// 讀取 OpenAPI 文檔
|
|
121
|
+
const openApiDoc = JSON.parse(fs.readFileSync(fullConfig.schemaFile, 'utf-8'));
|
|
122
|
+
const paths = Object.keys(openApiDoc.paths);
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
// 從配置中獲取分類規則
|
|
126
|
+
const [outputPath, config] = Object.entries(outputFiles)[0];
|
|
127
|
+
const patterns = config.groupMatch;
|
|
128
|
+
|
|
129
|
+
const filterEndpoint = config.filterEndpoint;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
const pattern = patterns;
|
|
133
|
+
// 根據路徑自動分類
|
|
134
|
+
const groupedPaths = paths.reduce((acc, path) => {
|
|
135
|
+
|
|
136
|
+
const groupName = getGroupNameFromPath(path, pattern);
|
|
137
|
+
if (!acc[groupName]) {
|
|
138
|
+
acc[groupName] = [];
|
|
139
|
+
}
|
|
140
|
+
acc[groupName].push(path);
|
|
141
|
+
return acc;
|
|
142
|
+
}, {} as Record<string, string[]>);
|
|
143
|
+
|
|
144
|
+
// 為每個分類生成配置
|
|
145
|
+
Object.entries(groupedPaths).forEach(([groupName, paths]) => {
|
|
146
|
+
const finalOutputPath = outputPath.replace('$1', groupName);
|
|
147
|
+
|
|
148
|
+
const filterEndpoints = filterEndpoint(groupName);
|
|
149
|
+
outFiles.push({
|
|
150
|
+
...commonConfig,
|
|
151
|
+
outputFile: finalOutputPath,
|
|
152
|
+
filterEndpoints: [filterEndpoints],
|
|
153
|
+
});
|
|
42
154
|
});
|
|
43
|
-
|
|
155
|
+
|
|
44
156
|
} else {
|
|
45
157
|
outFiles.push(fullConfig);
|
|
46
158
|
}
|
package/src/types.ts
CHANGED
|
@@ -132,6 +132,7 @@ export interface OutputFileOptions extends Partial<CommonOptions> {
|
|
|
132
132
|
* If passed as true it will generate TS enums instead of union of strings
|
|
133
133
|
*/
|
|
134
134
|
useEnumType?: boolean;
|
|
135
|
+
sharedTypesFile?: string;
|
|
135
136
|
}
|
|
136
137
|
|
|
137
138
|
export type EndpointOverrides = {
|
|
@@ -141,10 +142,18 @@ export type EndpointOverrides = {
|
|
|
141
142
|
parameterFilter: ParameterMatcher;
|
|
142
143
|
}>;
|
|
143
144
|
|
|
145
|
+
export type OutputFilesConfig = {
|
|
146
|
+
[outputFile: string]: {
|
|
147
|
+
groupMatch: RegExp,
|
|
148
|
+
filterEndpoint: (groupName: string) => RegExp
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
144
152
|
export type ConfigFile =
|
|
145
153
|
| Id<Require<CommonOptions & OutputFileOptions, 'outputFile'>>
|
|
146
154
|
| Id<
|
|
147
155
|
Omit<CommonOptions, 'outputFile'> & {
|
|
148
|
-
outputFiles: { [outputFile: string]: Omit<OutputFileOptions, 'outputFile'> };
|
|
156
|
+
// outputFiles: { [outputFile: string]: Omit<OutputFileOptions, 'outputFile'> };
|
|
157
|
+
outputFiles: OutputFilesConfig
|
|
149
158
|
}
|
|
150
159
|
>;
|