@acrool/rtk-query-codegen-openapi 1.4.0-alpha.1 → 1.4.0-alpha.3
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/bin/cli.mjs +94 -48
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +83 -27
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +83 -27
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/common-types-generator.ts +49 -2
- package/src/generators/rtk-query-generator.ts +44 -20
- package/src/services/unified-code-generator.ts +1 -7
package/package.json
CHANGED
|
@@ -18,12 +18,59 @@ export interface IRequestConfig {
|
|
|
18
18
|
timeout?: number;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
export type IRestFulEndpointsQueryReturn<TVariables> = TVariables extends void ?
|
|
22
|
-
void | {fetchOptions?: IRequestConfig;}:
|
|
21
|
+
export type IRestFulEndpointsQueryReturn<TVariables> = TVariables extends void ?
|
|
22
|
+
void | {fetchOptions?: IRequestConfig;}:
|
|
23
23
|
{
|
|
24
24
|
variables: TVariables;
|
|
25
25
|
fetchOptions?: IRequestConfig;
|
|
26
26
|
};
|
|
27
|
+
|
|
28
|
+
export interface RtkQueryBaseOptions {
|
|
29
|
+
skip?: boolean;
|
|
30
|
+
pollingInterval?: number;
|
|
31
|
+
refetchOnMountOrArgChange?: boolean | number;
|
|
32
|
+
refetchOnFocus?: boolean;
|
|
33
|
+
refetchOnReconnect?: boolean;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface RtkQueryResult<TData> {
|
|
37
|
+
data: TData | undefined;
|
|
38
|
+
currentData: TData | undefined;
|
|
39
|
+
isLoading: boolean;
|
|
40
|
+
isFetching: boolean;
|
|
41
|
+
isSuccess: boolean;
|
|
42
|
+
isError: boolean;
|
|
43
|
+
isUninitialized: boolean;
|
|
44
|
+
error: unknown;
|
|
45
|
+
refetch: () => void;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type SimpleQueryHook<TData, TArg> = (
|
|
49
|
+
arg: TArg,
|
|
50
|
+
options?: RtkQueryBaseOptions
|
|
51
|
+
) => RtkQueryResult<TData>;
|
|
52
|
+
|
|
53
|
+
export type SimpleVoidQueryHook<TData> = (
|
|
54
|
+
options?: RtkQueryBaseOptions
|
|
55
|
+
) => RtkQueryResult<TData>;
|
|
56
|
+
|
|
57
|
+
export type SimpleLazyQueryHook<TData, TArg> = () => readonly [
|
|
58
|
+
(arg: TArg) => void,
|
|
59
|
+
RtkQueryResult<TData>
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
export type UseSimpleMutation<TRes, TArg = void> = () => readonly [
|
|
63
|
+
(arg: TArg) => Promise<TRes>,
|
|
64
|
+
{
|
|
65
|
+
data: TRes | undefined;
|
|
66
|
+
isLoading: boolean;
|
|
67
|
+
isSuccess: boolean;
|
|
68
|
+
isError: boolean;
|
|
69
|
+
isUninitialized: boolean;
|
|
70
|
+
error: unknown;
|
|
71
|
+
reset: () => void;
|
|
72
|
+
}
|
|
73
|
+
];
|
|
27
74
|
`;
|
|
28
75
|
|
|
29
76
|
}
|
|
@@ -116,10 +116,52 @@ ${paramsLines}
|
|
|
116
116
|
`
|
|
117
117
|
: '';
|
|
118
118
|
|
|
119
|
+
// 判斷是否有各種 hook 類型,決定需要導入哪些簡化型別
|
|
120
|
+
const hasVoidQuery = endpointInfos.some(info => info.isQuery && info.isVoidArg);
|
|
121
|
+
const hasArgQuery = endpointInfos.some(info => info.isQuery && !info.isVoidArg);
|
|
122
|
+
const hasLazyQuery = endpointInfos.some(info => info.isQuery) && !!options.useLazyQueries;
|
|
123
|
+
const hasMutation = endpointInfos.some(info => !info.isQuery);
|
|
124
|
+
|
|
125
|
+
const simpleTypeImports: string[] = [];
|
|
126
|
+
if (hasArgQuery) simpleTypeImports.push('SimpleQueryHook');
|
|
127
|
+
if (hasVoidQuery) simpleTypeImports.push('SimpleVoidQueryHook');
|
|
128
|
+
if (hasLazyQuery) simpleTypeImports.push('SimpleLazyQueryHook');
|
|
129
|
+
if (hasMutation) simpleTypeImports.push('UseSimpleMutation');
|
|
130
|
+
|
|
131
|
+
const simpleTypeImportStatement = simpleTypeImports.length > 0
|
|
132
|
+
? `import type { ${simpleTypeImports.join(', ')} } from "../common-types";\n`
|
|
133
|
+
: '';
|
|
134
|
+
|
|
135
|
+
// 生成逐個導出(使用 as 切斷型別推導鏈)
|
|
136
|
+
// tuple 回傳(mutation, lazy query)使用 as unknown as;object 回傳(query)使用 as
|
|
137
|
+
const hookExports = endpointInfos.map(info => {
|
|
138
|
+
const capitalizedOperationName = info.operationName.charAt(0).toUpperCase() + info.operationName.slice(1);
|
|
139
|
+
const argType = info.isVoidArg ? 'void' : `${httpClientTypeName}<${info.argTypeName}>`;
|
|
140
|
+
const lines: string[] = [];
|
|
141
|
+
|
|
142
|
+
if (info.isQuery) {
|
|
143
|
+
const regularHook = `use${capitalizedOperationName}Query`;
|
|
144
|
+
if (info.isVoidArg) {
|
|
145
|
+
lines.push(`export const ${regularHook} = injectedRtkApi.${regularHook} as SimpleVoidQueryHook<${info.responseTypeName}>;`);
|
|
146
|
+
} else {
|
|
147
|
+
lines.push(`export const ${regularHook} = injectedRtkApi.${regularHook} as SimpleQueryHook<${info.responseTypeName}, ${argType}>;`);
|
|
148
|
+
}
|
|
149
|
+
if (options.useLazyQueries) {
|
|
150
|
+
const lazyHook = `useLazy${capitalizedOperationName}Query`;
|
|
151
|
+
lines.push(`export const ${lazyHook} = injectedRtkApi.${lazyHook} as unknown as SimpleLazyQueryHook<${info.responseTypeName}, ${argType}>;`);
|
|
152
|
+
}
|
|
153
|
+
} else {
|
|
154
|
+
const mutationHook = `use${capitalizedOperationName}Mutation`;
|
|
155
|
+
lines.push(`export const ${mutationHook} = injectedRtkApi.${mutationHook} as unknown as UseSimpleMutation<${info.responseTypeName}, ${argType}>;`);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return lines.join('\n');
|
|
159
|
+
}).join('\n');
|
|
160
|
+
|
|
119
161
|
return `/* eslint-disable */
|
|
120
162
|
// [Warning] Generated automatically - do not edit manually
|
|
121
163
|
|
|
122
|
-
${apiImport}${httpClientImport}${tagTypesImport}
|
|
164
|
+
${apiImport}${httpClientImport}${tagTypesImport}${simpleTypeImportStatement}
|
|
123
165
|
${typeImportStatement}
|
|
124
166
|
|
|
125
167
|
|
|
@@ -129,25 +171,7 @@ ${endpoints}
|
|
|
129
171
|
}),
|
|
130
172
|
});
|
|
131
173
|
|
|
132
|
-
|
|
133
|
-
${endpointInfos.map(info => {
|
|
134
|
-
const capitalizedOperationName = info.operationName.charAt(0).toUpperCase() + info.operationName.slice(1);
|
|
135
|
-
if (info.isQuery) {
|
|
136
|
-
// For queries, generate both regular and lazy hooks if useLazyQueries is enabled
|
|
137
|
-
const regularHook = `use${capitalizedOperationName}Query`;
|
|
138
|
-
if (options.useLazyQueries) {
|
|
139
|
-
const lazyHook = `useLazy${capitalizedOperationName}Query`;
|
|
140
|
-
return ` ${regularHook},\n ${lazyHook},`;
|
|
141
|
-
} else {
|
|
142
|
-
return ` ${regularHook},`;
|
|
143
|
-
}
|
|
144
|
-
} else {
|
|
145
|
-
// For mutations, only generate regular hook
|
|
146
|
-
const mutationHook = `use${capitalizedOperationName}Mutation`;
|
|
147
|
-
return ` ${mutationHook},`;
|
|
148
|
-
}
|
|
149
|
-
}).join('\n')}
|
|
150
|
-
} = injectedRtkApi;
|
|
174
|
+
${hookExports}
|
|
151
175
|
|
|
152
176
|
export default injectedRtkApi;
|
|
153
177
|
`;
|
|
@@ -392,13 +392,7 @@ export class UnifiedCodeGenerator {
|
|
|
392
392
|
results.push(...schemaResults);
|
|
393
393
|
}
|
|
394
394
|
|
|
395
|
-
//
|
|
396
|
-
const mainIndexContent = this.generateMainIndex(generatedGroups);
|
|
397
|
-
const mainIndexResult = await this.fileWriterService.writeFile(
|
|
398
|
-
path.join(outputDir, 'index.ts'),
|
|
399
|
-
mainIndexContent
|
|
400
|
-
);
|
|
401
|
-
results.push(mainIndexResult);
|
|
395
|
+
// 主 index.ts 由使用者自行管理,不自動產出
|
|
402
396
|
|
|
403
397
|
} catch (error) {
|
|
404
398
|
errors.push(error as Error);
|