@acrool/rtk-query-codegen-openapi 1.4.0-alpha.1 → 1.4.0-alpha.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@acrool/rtk-query-codegen-openapi",
3
- "version": "1.4.0-alpha.1",
3
+ "version": "1.4.0-alpha.2",
4
4
  "main": "lib/index.js",
5
5
  "types": "lib/index.d.ts",
6
6
  "module": "lib/index.mjs",
@@ -18,12 +18,61 @@ 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 type UseSimpleQuery<TRes, TArg = void> = (
29
+ arg: TArg,
30
+ options?: {
31
+ skip?: boolean;
32
+ pollingInterval?: number;
33
+ refetchOnMountOrArgChange?: boolean | number;
34
+ refetchOnFocus?: boolean;
35
+ refetchOnReconnect?: boolean;
36
+ }
37
+ ) => {
38
+ data: TRes | undefined;
39
+ currentData: TRes | undefined;
40
+ isLoading: boolean;
41
+ isFetching: boolean;
42
+ isSuccess: boolean;
43
+ isError: boolean;
44
+ isUninitialized: boolean;
45
+ error: unknown;
46
+ refetch: () => void;
47
+ };
48
+
49
+ export type UseSimpleMutation<TRes, TArg = void> = () => [
50
+ (arg: TArg) => {unwrap: () => Promise<TRes>} & PromiseLike<{data: TRes} | {error: unknown}>,
51
+ {
52
+ data: TRes | undefined;
53
+ isLoading: boolean;
54
+ isSuccess: boolean;
55
+ isError: boolean;
56
+ isUninitialized: boolean;
57
+ error: unknown;
58
+ reset: () => void;
59
+ }
60
+ ];
61
+
62
+ export type UseSimpleLazyQuery<TRes, TArg = void> = () => [
63
+ (arg: TArg, preferCacheValue?: boolean) => void,
64
+ {
65
+ data: TRes | undefined;
66
+ currentData: TRes | undefined;
67
+ isLoading: boolean;
68
+ isFetching: boolean;
69
+ isSuccess: boolean;
70
+ isError: boolean;
71
+ isUninitialized: boolean;
72
+ error: unknown;
73
+ },
74
+ {lastArg: TArg | undefined}
75
+ ];
27
76
  `;
28
77
 
29
78
  }
@@ -116,10 +116,45 @@ ${paramsLines}
116
116
  `
117
117
  : '';
118
118
 
119
+ // 判斷是否有 query / lazy query / mutation,決定需要導入哪些簡化型別
120
+ const hasQuery = endpointInfos.some(info => info.isQuery);
121
+ const hasLazyQuery = hasQuery && !!options.useLazyQueries;
122
+ const hasMutation = endpointInfos.some(info => !info.isQuery);
123
+
124
+ const simpleTypeImports: string[] = [];
125
+ if (hasQuery) simpleTypeImports.push('UseSimpleQuery');
126
+ if (hasMutation) simpleTypeImports.push('UseSimpleMutation');
127
+ if (hasLazyQuery) simpleTypeImports.push('UseSimpleLazyQuery');
128
+
129
+ const simpleTypeImportStatement = simpleTypeImports.length > 0
130
+ ? `import type { ${simpleTypeImports.join(', ')} } from "../common-types";\n`
131
+ : '';
132
+
133
+ // 生成逐個導出(使用 as 切斷型別推導鏈)
134
+ const hookExports = endpointInfos.map(info => {
135
+ const capitalizedOperationName = info.operationName.charAt(0).toUpperCase() + info.operationName.slice(1);
136
+ const argType = info.isVoidArg ? 'void' : `${httpClientTypeName}<${info.argTypeName}>`;
137
+ const lines: string[] = [];
138
+
139
+ if (info.isQuery) {
140
+ const regularHook = `use${capitalizedOperationName}Query`;
141
+ lines.push(`export const ${regularHook} = injectedRtkApi.${regularHook} as UseSimpleQuery<${info.responseTypeName}, ${argType}>;`);
142
+ if (options.useLazyQueries) {
143
+ const lazyHook = `useLazy${capitalizedOperationName}Query`;
144
+ lines.push(`export const ${lazyHook} = injectedRtkApi.${lazyHook} as UseSimpleLazyQuery<${info.responseTypeName}, ${argType}>;`);
145
+ }
146
+ } else {
147
+ const mutationHook = `use${capitalizedOperationName}Mutation`;
148
+ lines.push(`export const ${mutationHook} = injectedRtkApi.${mutationHook} as UseSimpleMutation<${info.responseTypeName}, ${argType}>;`);
149
+ }
150
+
151
+ return lines.join('\n');
152
+ }).join('\n');
153
+
119
154
  return `/* eslint-disable */
120
155
  // [Warning] Generated automatically - do not edit manually
121
156
 
122
- ${apiImport}${httpClientImport}${tagTypesImport}
157
+ ${apiImport}${httpClientImport}${tagTypesImport}${simpleTypeImportStatement}
123
158
  ${typeImportStatement}
124
159
 
125
160
 
@@ -129,25 +164,7 @@ ${endpoints}
129
164
  }),
130
165
  });
131
166
 
132
- export const {
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;
167
+ ${hookExports}
151
168
 
152
169
  export default injectedRtkApi;
153
170
  `;