@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/index.js CHANGED
@@ -489,12 +489,61 @@ export interface IRequestConfig {
489
489
  timeout?: number;
490
490
  }
491
491
 
492
- export type IRestFulEndpointsQueryReturn<TVariables> = TVariables extends void ?
493
- void | {fetchOptions?: IRequestConfig;}:
492
+ export type IRestFulEndpointsQueryReturn<TVariables> = TVariables extends void ?
493
+ void | {fetchOptions?: IRequestConfig;}:
494
494
  {
495
495
  variables: TVariables;
496
496
  fetchOptions?: IRequestConfig;
497
497
  };
498
+
499
+ export type UseSimpleQuery<TRes, TArg = void> = (
500
+ arg: TArg,
501
+ options?: {
502
+ skip?: boolean;
503
+ pollingInterval?: number;
504
+ refetchOnMountOrArgChange?: boolean | number;
505
+ refetchOnFocus?: boolean;
506
+ refetchOnReconnect?: boolean;
507
+ }
508
+ ) => {
509
+ data: TRes | undefined;
510
+ currentData: TRes | undefined;
511
+ isLoading: boolean;
512
+ isFetching: boolean;
513
+ isSuccess: boolean;
514
+ isError: boolean;
515
+ isUninitialized: boolean;
516
+ error: unknown;
517
+ refetch: () => void;
518
+ };
519
+
520
+ export type UseSimpleMutation<TRes, TArg = void> = () => [
521
+ (arg: TArg) => {unwrap: () => Promise<TRes>} & PromiseLike<{data: TRes} | {error: unknown}>,
522
+ {
523
+ data: TRes | undefined;
524
+ isLoading: boolean;
525
+ isSuccess: boolean;
526
+ isError: boolean;
527
+ isUninitialized: boolean;
528
+ error: unknown;
529
+ reset: () => void;
530
+ }
531
+ ];
532
+
533
+ export type UseSimpleLazyQuery<TRes, TArg = void> = () => [
534
+ (arg: TArg, preferCacheValue?: boolean) => void,
535
+ {
536
+ data: TRes | undefined;
537
+ currentData: TRes | undefined;
538
+ isLoading: boolean;
539
+ isFetching: boolean;
540
+ isSuccess: boolean;
541
+ isError: boolean;
542
+ isUninitialized: boolean;
543
+ error: unknown;
544
+ },
545
+ {lastArg: TArg | undefined}
546
+ ];
498
547
  `;
499
548
  }
500
549
 
@@ -1107,10 +1156,36 @@ ${paramsLines}
1107
1156
  const hasTags = endpointInfos.some((info) => info.tags && info.tags.length > 0);
1108
1157
  const tagTypesImport = hasTags ? `import {ECacheTagTypes} from "../tagTypes";
1109
1158
  ` : "";
1159
+ const hasQuery = endpointInfos.some((info) => info.isQuery);
1160
+ const hasLazyQuery = hasQuery && !!options.useLazyQueries;
1161
+ const hasMutation = endpointInfos.some((info) => !info.isQuery);
1162
+ const simpleTypeImports = [];
1163
+ if (hasQuery) simpleTypeImports.push("UseSimpleQuery");
1164
+ if (hasMutation) simpleTypeImports.push("UseSimpleMutation");
1165
+ if (hasLazyQuery) simpleTypeImports.push("UseSimpleLazyQuery");
1166
+ const simpleTypeImportStatement = simpleTypeImports.length > 0 ? `import type { ${simpleTypeImports.join(", ")} } from "../common-types";
1167
+ ` : "";
1168
+ const hookExports = endpointInfos.map((info) => {
1169
+ const capitalizedOperationName = info.operationName.charAt(0).toUpperCase() + info.operationName.slice(1);
1170
+ const argType = info.isVoidArg ? "void" : `${httpClientTypeName}<${info.argTypeName}>`;
1171
+ const lines = [];
1172
+ if (info.isQuery) {
1173
+ const regularHook = `use${capitalizedOperationName}Query`;
1174
+ lines.push(`export const ${regularHook} = injectedRtkApi.${regularHook} as UseSimpleQuery<${info.responseTypeName}, ${argType}>;`);
1175
+ if (options.useLazyQueries) {
1176
+ const lazyHook = `useLazy${capitalizedOperationName}Query`;
1177
+ lines.push(`export const ${lazyHook} = injectedRtkApi.${lazyHook} as UseSimpleLazyQuery<${info.responseTypeName}, ${argType}>;`);
1178
+ }
1179
+ } else {
1180
+ const mutationHook = `use${capitalizedOperationName}Mutation`;
1181
+ lines.push(`export const ${mutationHook} = injectedRtkApi.${mutationHook} as UseSimpleMutation<${info.responseTypeName}, ${argType}>;`);
1182
+ }
1183
+ return lines.join("\n");
1184
+ }).join("\n");
1110
1185
  return `/* eslint-disable */
1111
1186
  // [Warning] Generated automatically - do not edit manually
1112
1187
 
1113
- ${apiImport}${httpClientImport}${tagTypesImport}
1188
+ ${apiImport}${httpClientImport}${tagTypesImport}${simpleTypeImportStatement}
1114
1189
  ${typeImportStatement}
1115
1190
 
1116
1191
 
@@ -1120,24 +1195,7 @@ ${endpoints}
1120
1195
  }),
1121
1196
  });
1122
1197
 
1123
- export const {
1124
- ${endpointInfos.map((info) => {
1125
- const capitalizedOperationName = info.operationName.charAt(0).toUpperCase() + info.operationName.slice(1);
1126
- if (info.isQuery) {
1127
- const regularHook = `use${capitalizedOperationName}Query`;
1128
- if (options.useLazyQueries) {
1129
- const lazyHook = `useLazy${capitalizedOperationName}Query`;
1130
- return ` ${regularHook},
1131
- ${lazyHook},`;
1132
- } else {
1133
- return ` ${regularHook},`;
1134
- }
1135
- } else {
1136
- const mutationHook = `use${capitalizedOperationName}Mutation`;
1137
- return ` ${mutationHook},`;
1138
- }
1139
- }).join("\n")}
1140
- } = injectedRtkApi;
1198
+ ${hookExports}
1141
1199
 
1142
1200
  export default injectedRtkApi;
1143
1201
  `;