@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/lib/bin/cli.mjs +95 -47
- package/lib/bin/cli.mjs.map +1 -1
- package/lib/index.js +79 -21
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +79 -21
- package/lib/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/generators/common-types-generator.ts +51 -2
- package/src/generators/rtk-query-generator.ts +37 -20
package/lib/index.mjs
CHANGED
|
@@ -474,12 +474,61 @@ export interface IRequestConfig {
|
|
|
474
474
|
timeout?: number;
|
|
475
475
|
}
|
|
476
476
|
|
|
477
|
-
export type IRestFulEndpointsQueryReturn<TVariables> = TVariables extends void ?
|
|
478
|
-
void | {fetchOptions?: IRequestConfig;}:
|
|
477
|
+
export type IRestFulEndpointsQueryReturn<TVariables> = TVariables extends void ?
|
|
478
|
+
void | {fetchOptions?: IRequestConfig;}:
|
|
479
479
|
{
|
|
480
480
|
variables: TVariables;
|
|
481
481
|
fetchOptions?: IRequestConfig;
|
|
482
482
|
};
|
|
483
|
+
|
|
484
|
+
export type UseSimpleQuery<TRes, TArg = void> = (
|
|
485
|
+
arg: TArg,
|
|
486
|
+
options?: {
|
|
487
|
+
skip?: boolean;
|
|
488
|
+
pollingInterval?: number;
|
|
489
|
+
refetchOnMountOrArgChange?: boolean | number;
|
|
490
|
+
refetchOnFocus?: boolean;
|
|
491
|
+
refetchOnReconnect?: boolean;
|
|
492
|
+
}
|
|
493
|
+
) => {
|
|
494
|
+
data: TRes | undefined;
|
|
495
|
+
currentData: TRes | undefined;
|
|
496
|
+
isLoading: boolean;
|
|
497
|
+
isFetching: boolean;
|
|
498
|
+
isSuccess: boolean;
|
|
499
|
+
isError: boolean;
|
|
500
|
+
isUninitialized: boolean;
|
|
501
|
+
error: unknown;
|
|
502
|
+
refetch: () => void;
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
export type UseSimpleMutation<TRes, TArg = void> = () => [
|
|
506
|
+
(arg: TArg) => {unwrap: () => Promise<TRes>} & PromiseLike<{data: TRes} | {error: unknown}>,
|
|
507
|
+
{
|
|
508
|
+
data: TRes | undefined;
|
|
509
|
+
isLoading: boolean;
|
|
510
|
+
isSuccess: boolean;
|
|
511
|
+
isError: boolean;
|
|
512
|
+
isUninitialized: boolean;
|
|
513
|
+
error: unknown;
|
|
514
|
+
reset: () => void;
|
|
515
|
+
}
|
|
516
|
+
];
|
|
517
|
+
|
|
518
|
+
export type UseSimpleLazyQuery<TRes, TArg = void> = () => [
|
|
519
|
+
(arg: TArg, preferCacheValue?: boolean) => void,
|
|
520
|
+
{
|
|
521
|
+
data: TRes | undefined;
|
|
522
|
+
currentData: TRes | undefined;
|
|
523
|
+
isLoading: boolean;
|
|
524
|
+
isFetching: boolean;
|
|
525
|
+
isSuccess: boolean;
|
|
526
|
+
isError: boolean;
|
|
527
|
+
isUninitialized: boolean;
|
|
528
|
+
error: unknown;
|
|
529
|
+
},
|
|
530
|
+
{lastArg: TArg | undefined}
|
|
531
|
+
];
|
|
483
532
|
`;
|
|
484
533
|
}
|
|
485
534
|
|
|
@@ -1092,10 +1141,36 @@ ${paramsLines}
|
|
|
1092
1141
|
const hasTags = endpointInfos.some((info) => info.tags && info.tags.length > 0);
|
|
1093
1142
|
const tagTypesImport = hasTags ? `import {ECacheTagTypes} from "../tagTypes";
|
|
1094
1143
|
` : "";
|
|
1144
|
+
const hasQuery = endpointInfos.some((info) => info.isQuery);
|
|
1145
|
+
const hasLazyQuery = hasQuery && !!options.useLazyQueries;
|
|
1146
|
+
const hasMutation = endpointInfos.some((info) => !info.isQuery);
|
|
1147
|
+
const simpleTypeImports = [];
|
|
1148
|
+
if (hasQuery) simpleTypeImports.push("UseSimpleQuery");
|
|
1149
|
+
if (hasMutation) simpleTypeImports.push("UseSimpleMutation");
|
|
1150
|
+
if (hasLazyQuery) simpleTypeImports.push("UseSimpleLazyQuery");
|
|
1151
|
+
const simpleTypeImportStatement = simpleTypeImports.length > 0 ? `import type { ${simpleTypeImports.join(", ")} } from "../common-types";
|
|
1152
|
+
` : "";
|
|
1153
|
+
const hookExports = endpointInfos.map((info) => {
|
|
1154
|
+
const capitalizedOperationName = info.operationName.charAt(0).toUpperCase() + info.operationName.slice(1);
|
|
1155
|
+
const argType = info.isVoidArg ? "void" : `${httpClientTypeName}<${info.argTypeName}>`;
|
|
1156
|
+
const lines = [];
|
|
1157
|
+
if (info.isQuery) {
|
|
1158
|
+
const regularHook = `use${capitalizedOperationName}Query`;
|
|
1159
|
+
lines.push(`export const ${regularHook} = injectedRtkApi.${regularHook} as UseSimpleQuery<${info.responseTypeName}, ${argType}>;`);
|
|
1160
|
+
if (options.useLazyQueries) {
|
|
1161
|
+
const lazyHook = `useLazy${capitalizedOperationName}Query`;
|
|
1162
|
+
lines.push(`export const ${lazyHook} = injectedRtkApi.${lazyHook} as UseSimpleLazyQuery<${info.responseTypeName}, ${argType}>;`);
|
|
1163
|
+
}
|
|
1164
|
+
} else {
|
|
1165
|
+
const mutationHook = `use${capitalizedOperationName}Mutation`;
|
|
1166
|
+
lines.push(`export const ${mutationHook} = injectedRtkApi.${mutationHook} as UseSimpleMutation<${info.responseTypeName}, ${argType}>;`);
|
|
1167
|
+
}
|
|
1168
|
+
return lines.join("\n");
|
|
1169
|
+
}).join("\n");
|
|
1095
1170
|
return `/* eslint-disable */
|
|
1096
1171
|
// [Warning] Generated automatically - do not edit manually
|
|
1097
1172
|
|
|
1098
|
-
${apiImport}${httpClientImport}${tagTypesImport}
|
|
1173
|
+
${apiImport}${httpClientImport}${tagTypesImport}${simpleTypeImportStatement}
|
|
1099
1174
|
${typeImportStatement}
|
|
1100
1175
|
|
|
1101
1176
|
|
|
@@ -1105,24 +1180,7 @@ ${endpoints}
|
|
|
1105
1180
|
}),
|
|
1106
1181
|
});
|
|
1107
1182
|
|
|
1108
|
-
|
|
1109
|
-
${endpointInfos.map((info) => {
|
|
1110
|
-
const capitalizedOperationName = info.operationName.charAt(0).toUpperCase() + info.operationName.slice(1);
|
|
1111
|
-
if (info.isQuery) {
|
|
1112
|
-
const regularHook = `use${capitalizedOperationName}Query`;
|
|
1113
|
-
if (options.useLazyQueries) {
|
|
1114
|
-
const lazyHook = `useLazy${capitalizedOperationName}Query`;
|
|
1115
|
-
return ` ${regularHook},
|
|
1116
|
-
${lazyHook},`;
|
|
1117
|
-
} else {
|
|
1118
|
-
return ` ${regularHook},`;
|
|
1119
|
-
}
|
|
1120
|
-
} else {
|
|
1121
|
-
const mutationHook = `use${capitalizedOperationName}Mutation`;
|
|
1122
|
-
return ` ${mutationHook},`;
|
|
1123
|
-
}
|
|
1124
|
-
}).join("\n")}
|
|
1125
|
-
} = injectedRtkApi;
|
|
1183
|
+
${hookExports}
|
|
1126
1184
|
|
|
1127
1185
|
export default injectedRtkApi;
|
|
1128
1186
|
`;
|