@7nohe/openapi-react-query-codegen 4.0.0-beta.4 → 4.0.0-beta.6

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.
Files changed (51) hide show
  1. package/AGENTS.md +23 -1
  2. package/README.md +5 -2
  3. package/dist/agentsMd.d.mts +30 -0
  4. package/dist/agentsMd.mjs +115 -0
  5. package/dist/check.mjs +8 -4
  6. package/dist/cli.d.mts +2 -0
  7. package/dist/client/emitSchemas.mjs +12 -9
  8. package/dist/client/emitSdk.d.mts +2 -0
  9. package/dist/client/emitSdk.mjs +24 -27
  10. package/dist/client/emitTransformers.mjs +21 -72
  11. package/dist/client/emitTypes.d.mts +2 -1
  12. package/dist/client/emitTypes.mjs +5 -5
  13. package/dist/client/index.mjs +13 -5
  14. package/dist/client/naming.d.mts +5 -2
  15. package/dist/client/naming.mjs +7 -3
  16. package/dist/client/operations.d.mts +3 -2
  17. package/dist/client/operations.mjs +10 -47
  18. package/dist/client/runtime/axios.runtime.ts +15 -10
  19. package/dist/client/schema.d.mts +24 -2
  20. package/dist/client/schema.mjs +135 -33
  21. package/dist/client/spec.d.mts +6 -0
  22. package/dist/client/spec.mjs +15 -5
  23. package/dist/common.d.mts +0 -2
  24. package/dist/common.mjs +0 -10
  25. package/dist/createSource.d.mts +1 -1
  26. package/dist/createSource.mjs +1 -3
  27. package/dist/generate.mjs +9 -2
  28. package/dist/hooks/buildCommon.d.mts +12 -16
  29. package/dist/hooks/buildCommon.mjs +25 -35
  30. package/dist/hooks/buildMutationHooks.d.mts +3 -2
  31. package/dist/hooks/buildMutationHooks.mjs +6 -11
  32. package/dist/hooks/buildQueryHooks.d.mts +21 -15
  33. package/dist/hooks/buildQueryHooks.mjs +35 -69
  34. package/dist/hooks/buildQueryOptions.d.mts +4 -3
  35. package/dist/hooks/buildQueryOptions.mjs +9 -22
  36. package/dist/hooks/generateFiles.mjs +54 -81
  37. package/dist/hooks/hookNames.d.mts +69 -0
  38. package/dist/hooks/hookNames.mjs +82 -0
  39. package/dist/hooks/imports.d.mts +27 -52
  40. package/dist/hooks/imports.mjs +82 -164
  41. package/dist/hooks/operationNames.d.mts +10 -0
  42. package/dist/hooks/operationNames.mjs +22 -0
  43. package/dist/hooks/statement.d.mts +15 -6
  44. package/dist/hooks/statement.mjs +19 -14
  45. package/dist/runCli.mjs +1 -0
  46. package/dist/types.d.mts +5 -7
  47. package/package.json +1 -1
  48. package/dist/hooks/index.d.mts +0 -4
  49. package/dist/hooks/index.mjs +0 -4
  50. package/dist/print.d.mts +0 -5
  51. package/dist/print.mjs +0 -22
package/dist/generate.mjs CHANGED
@@ -1,9 +1,9 @@
1
+ import { agentsFilePath, claudeImportHint, writeAgentsBlock, } from "./agentsMd.mjs";
1
2
  import { detectImportExtension, findNearestTsConfigPath, generateClient, SpecError, writeGeneratedFiles, } from "./client/index.mjs";
2
3
  import { buildQueriesOutputPath, buildRequestsOutputPath, formatOptions, } from "./common.mjs";
3
4
  import { createSource } from "./createSource.mjs";
4
5
  import { CodegenError, ErrorCodes, toCodegenError } from "./errors.mjs";
5
6
  import { processOutput } from "./format.mjs";
6
- import { print } from "./print.mjs";
7
7
  export { findNearestTsConfigPath };
8
8
  /** Map a `--client` value (including the v3 `@hey-api/*` aliases) to a kind. */
9
9
  export function resolveClientKind(client) {
@@ -82,13 +82,20 @@ async function runGenerate(options, version, runtime) {
82
82
  omitInitialPageParam: formattedOptions.omitInitialPageParam ?? false,
83
83
  importExtension,
84
84
  });
85
- await print(source, formattedOptions);
86
85
  const queriesOutputPath = buildQueriesOutputPath(options.output);
86
+ await writeGeneratedFiles(queriesOutputPath, source);
87
87
  await processOutput({
88
88
  output: queriesOutputPath,
89
89
  format: formattedOptions.format,
90
90
  lint: formattedOptions.lint,
91
91
  });
92
+ if (options.agentsMd) {
93
+ const file = agentsFilePath(options.agentsMd);
94
+ await writeAgentsBlock(file, options.output);
95
+ const hint = runtime.silent ? undefined : await claudeImportHint(file);
96
+ if (hint)
97
+ console.warn(hint);
98
+ }
92
99
  }
93
100
  /**
94
101
  * Generate the client and the React Query hooks into `options.output`.
@@ -1,42 +1,38 @@
1
- import type { GenerationContext, OperationInfo } from "../types.mjs";
1
+ import type { GenerationContext } from "../types.mjs";
2
+ import { type HookOperation } from "./hookNames.mjs";
2
3
  import type { ConstStatement, TypeStatement } from "./statement.mjs";
3
4
  /**
4
5
  * Build the default response type alias.
5
6
  * Example: export type FindPetsDefaultResponse = Awaited<ReturnType<typeof findPets>>["data"];
6
7
  */
7
- export declare function buildDefaultResponseType(op: OperationInfo): TypeStatement;
8
+ export declare function buildDefaultResponseType(op: HookOperation): TypeStatement;
8
9
  /**
9
10
  * Build the query result type alias.
10
11
  * Example: export type FindPetsQueryResult<TData = FindPetsDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
11
12
  */
12
- export declare function buildQueryResultType(op: OperationInfo): TypeStatement;
13
+ export declare function buildQueryResultType(op: HookOperation): TypeStatement;
13
14
  /**
14
15
  * Build the mutation result type alias.
15
16
  * Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet>>;
16
17
  */
17
- export declare function buildMutationResultType(op: OperationInfo): TypeStatement;
18
+ export declare function buildMutationResultType(op: HookOperation): TypeStatement;
18
19
  /**
19
- * Build query key constant.
20
+ * Build the key constant of a query or a mutation.
20
21
  * Example: export const useFindPetsKey = "FindPets";
21
22
  */
22
- export declare function buildQueryKeyConst(op: OperationInfo): ConstStatement;
23
- /**
24
- * Build mutation key constant.
25
- * Example: export const useAddPetKey = "AddPet";
26
- */
27
- export declare function buildMutationKeyConst(op: OperationInfo): ConstStatement;
23
+ export declare function buildKeyConst(op: HookOperation): ConstStatement;
28
24
  /**
29
25
  * Build query key function.
30
26
  * Example: export const UseFindPetsKeyFn = (clientOptions: Options<FindPetsData, true> = {}, queryKey?: Array<unknown>) =>
31
27
  * [useFindPetsKey, ...(queryKey ?? [clientOptions])];
32
28
  */
33
- export declare function buildQueryKeyFn(op: OperationInfo): ConstStatement;
29
+ export declare function buildQueryKeyFn(op: HookOperation): ConstStatement;
34
30
  /**
35
31
  * Build mutation key function.
36
32
  * Example: export const UseAddPetKeyFn = (mutationKey?: Array<unknown>) =>
37
33
  * [useAddPetKey, ...(mutationKey ?? [])];
38
34
  */
39
- export declare function buildMutationKeyFn(op: OperationInfo): ConstStatement;
35
+ export declare function buildMutationKeyFn(op: HookOperation): ConstStatement;
40
36
  /**
41
37
  * Build the client options type for infinite queries.
42
38
  * The page parameter is excluded because TanStack Query supplies it via the
@@ -45,7 +41,7 @@ export declare function buildMutationKeyFn(op: OperationInfo): ConstStatement;
45
41
  * export type FindPaginatedPetsInfiniteClientOptions = Omit<Options<FindPaginatedPetsData, true>, "query"> &
46
42
  * { query?: Omit<NonNullable<FindPaginatedPetsData["query"]>, "page"> };
47
43
  */
48
- export declare function buildInfiniteClientOptionsType(op: OperationInfo, ctx: GenerationContext): TypeStatement;
44
+ export declare function buildInfiniteClientOptionsType(op: HookOperation, ctx: GenerationContext): TypeStatement;
49
45
  /**
50
46
  * Build the infinite query key constant.
51
47
  * Shares the plain query key as its first segment so a single
@@ -54,7 +50,7 @@ export declare function buildInfiniteClientOptionsType(op: OperationInfo, ctx: G
54
50
  * segment keeps cached InfiniteData from colliding with plain query data (#140).
55
51
  * Example: export const useFindPaginatedPetsInfiniteKey = [useFindPaginatedPetsKey, "infinite"] as const;
56
52
  */
57
- export declare function buildInfiniteQueryKeyConst(op: OperationInfo): ConstStatement;
53
+ export declare function buildInfiniteQueryKeyConst(op: HookOperation): ConstStatement;
58
54
  /**
59
55
  * Build the infinite query key function.
60
56
  * The custom queryKey argument only replaces the params segment — the
@@ -63,4 +59,4 @@ export declare function buildInfiniteQueryKeyConst(op: OperationInfo): ConstStat
63
59
  * Example: export const UseFindPaginatedPetsInfiniteKeyFn = (clientOptions: FindPaginatedPetsInfiniteClientOptions = {}, queryKey?: Array<unknown>) =>
64
60
  * [...useFindPaginatedPetsInfiniteKey, ...(queryKey ?? [clientOptions])];
65
61
  */
66
- export declare function buildInfiniteQueryKeyFn(op: OperationInfo): ConstStatement;
62
+ export declare function buildInfiniteQueryKeyFn(op: HookOperation): ConstStatement;
@@ -1,4 +1,6 @@
1
- import { getDataTypeName } from "./operationNames.mjs";
1
+ import { buildClientOptionsParam } from "./buildQueryHooks.mjs";
2
+ import { hookNames } from "./hookNames.mjs";
3
+ import { dataModels, getDataTypeName } from "./operationNames.mjs";
2
4
  /**
3
5
  * Build the default response type alias.
4
6
  * Example: export type FindPetsDefaultResponse = Awaited<ReturnType<typeof findPets>>["data"];
@@ -6,8 +8,9 @@ import { getDataTypeName } from "./operationNames.mjs";
6
8
  export function buildDefaultResponseType(op) {
7
9
  return {
8
10
  kind: "type",
9
- name: `${op.capitalizedMethodName}DefaultResponse`,
11
+ name: hookNames(op).defaultResponse,
10
12
  type: `Awaited<ReturnType<typeof ${op.methodName}>>["data"]`,
13
+ service: op.methodName,
11
14
  };
12
15
  }
13
16
  /**
@@ -17,9 +20,9 @@ export function buildDefaultResponseType(op) {
17
20
  export function buildQueryResultType(op) {
18
21
  return {
19
22
  kind: "type",
20
- name: `${op.capitalizedMethodName}QueryResult`,
23
+ name: hookNames(op).queryResult,
21
24
  typeParameters: [
22
- { name: "TData", default: `${op.capitalizedMethodName}DefaultResponse` },
25
+ { name: "TData", default: hookNames(op).defaultResponse },
23
26
  { name: "TError", default: "unknown" },
24
27
  ],
25
28
  type: "UseQueryResult<TData, TError>",
@@ -32,29 +35,19 @@ export function buildQueryResultType(op) {
32
35
  export function buildMutationResultType(op) {
33
36
  return {
34
37
  kind: "type",
35
- name: `${op.capitalizedMethodName}MutationResult`,
38
+ name: hookNames(op).mutationResult,
36
39
  type: `Awaited<ReturnType<typeof ${op.methodName}>>`,
40
+ service: op.methodName,
37
41
  };
38
42
  }
39
43
  /**
40
- * Build query key constant.
44
+ * Build the key constant of a query or a mutation.
41
45
  * Example: export const useFindPetsKey = "FindPets";
42
46
  */
43
- export function buildQueryKeyConst(op) {
47
+ export function buildKeyConst(op) {
44
48
  return {
45
49
  kind: "const",
46
- name: `use${op.capitalizedMethodName}Key`,
47
- initializer: `"${op.capitalizedMethodName}"`,
48
- };
49
- }
50
- /**
51
- * Build mutation key constant.
52
- * Example: export const useAddPetKey = "AddPet";
53
- */
54
- export function buildMutationKeyConst(op) {
55
- return {
56
- kind: "const",
57
- name: `use${op.capitalizedMethodName}Key`,
50
+ name: hookNames(op).key,
58
51
  initializer: `"${op.capitalizedMethodName}"`,
59
52
  };
60
53
  }
@@ -64,16 +57,11 @@ export function buildMutationKeyConst(op) {
64
57
  * [useFindPetsKey, ...(queryKey ?? [clientOptions])];
65
58
  */
66
59
  export function buildQueryKeyFn(op) {
67
- const dataTypeName = getDataTypeName(op);
68
- const params = [];
69
- const defaultValue = op.allParamsOptional ? " = {}" : "";
70
- params.push(`clientOptions: Options<${dataTypeName}, true>${defaultValue}`);
71
- params.push("queryKey?: Array<unknown>");
72
- const fallbackArray = "[clientOptions]";
73
60
  return {
74
61
  kind: "const",
75
- name: `Use${op.capitalizedMethodName}KeyFn`,
76
- initializer: `(${params.join(", ")}) => [use${op.capitalizedMethodName}Key, ...(queryKey ?? ${fallbackArray})]`,
62
+ name: hookNames(op).keyFn,
63
+ initializer: `(${buildClientOptionsParam(op)}, queryKey?: Array<unknown>) => [${hookNames(op).key}, ...(queryKey ?? [clientOptions])]`,
64
+ models: dataModels(op),
77
65
  };
78
66
  }
79
67
  /**
@@ -84,8 +72,8 @@ export function buildQueryKeyFn(op) {
84
72
  export function buildMutationKeyFn(op) {
85
73
  return {
86
74
  kind: "const",
87
- name: `Use${op.capitalizedMethodName}KeyFn`,
88
- initializer: `(mutationKey?: Array<unknown>) => [use${op.capitalizedMethodName}Key, ...(mutationKey ?? [])]`,
75
+ name: hookNames(op).keyFn,
76
+ initializer: `(mutationKey?: Array<unknown>) => [${hookNames(op).key}, ...(mutationKey ?? [])]`,
89
77
  };
90
78
  }
91
79
  /**
@@ -101,8 +89,9 @@ export function buildInfiniteClientOptionsType(op, ctx) {
101
89
  const type = `Omit<Options<${dataTypeName}, true>, "query"> & { query?: Omit<NonNullable<${dataTypeName}["query"]>, "${ctx.pageParam}"> }`;
102
90
  return {
103
91
  kind: "type",
104
- name: `${op.capitalizedMethodName}InfiniteClientOptions`,
92
+ name: hookNames(op).infiniteClientOptions,
105
93
  type,
94
+ models: dataModels(op),
106
95
  };
107
96
  }
108
97
  /**
@@ -116,8 +105,8 @@ export function buildInfiniteClientOptionsType(op, ctx) {
116
105
  export function buildInfiniteQueryKeyConst(op) {
117
106
  return {
118
107
  kind: "const",
119
- name: `use${op.capitalizedMethodName}InfiniteKey`,
120
- initializer: `[use${op.capitalizedMethodName}Key, "infinite"] as const`,
108
+ name: hookNames(op).infiniteKey,
109
+ initializer: `[${hookNames(op).key}, "infinite"] as const`,
121
110
  };
122
111
  }
123
112
  /**
@@ -129,14 +118,15 @@ export function buildInfiniteQueryKeyConst(op) {
129
118
  * [...useFindPaginatedPetsInfiniteKey, ...(queryKey ?? [clientOptions])];
130
119
  */
131
120
  export function buildInfiniteQueryKeyFn(op) {
121
+ const names = hookNames(op);
132
122
  const defaultValue = op.allParamsOptional ? " = {}" : "";
133
123
  const params = [
134
- `clientOptions: ${op.capitalizedMethodName}InfiniteClientOptions${defaultValue}`,
124
+ `clientOptions: ${names.infiniteClientOptions}${defaultValue}`,
135
125
  "queryKey?: Array<unknown>",
136
126
  ];
137
127
  return {
138
128
  kind: "const",
139
- name: `Use${op.capitalizedMethodName}InfiniteKeyFn`,
140
- initializer: `(${params.join(", ")}) => [...use${op.capitalizedMethodName}InfiniteKey, ...(queryKey ?? [clientOptions])]`,
129
+ name: names.infiniteKeyFn,
130
+ initializer: `(${params.join(", ")}) => [...${names.infiniteKey}, ...(queryKey ?? [clientOptions])]`,
141
131
  };
142
132
  }
@@ -1,4 +1,5 @@
1
- import type { GenerationContext, OperationInfo } from "../types.mjs";
1
+ import type { GenerationContext } from "../types.mjs";
2
+ import { type HookOperation } from "./hookNames.mjs";
2
3
  import type { ConstStatement } from "./statement.mjs";
3
4
  /**
4
5
  * Build useMutation hook.
@@ -12,4 +13,4 @@ import type { ConstStatement } from "./statement.mjs";
12
13
  * ...options
13
14
  * });
14
15
  */
15
- export declare function buildUseMutationHook(op: OperationInfo, ctx: GenerationContext): ConstStatement;
16
+ export declare function buildUseMutationHook(op: HookOperation, ctx: GenerationContext): ConstStatement;
@@ -1,5 +1,6 @@
1
1
  import { SDK_CALL_ARGS } from "./buildQueryHooks.mjs";
2
- import { getDataTypeName, getErrorType } from "./operationNames.mjs";
2
+ import { hookNames } from "./hookNames.mjs";
3
+ import { dataModels, errorModels, getDataTypeName, getErrorType, operationConst, } from "./operationNames.mjs";
3
4
  /**
4
5
  * Build useMutation hook.
5
6
  * Example:
@@ -13,17 +14,11 @@ import { getDataTypeName, getErrorType } from "./operationNames.mjs";
13
14
  * });
14
15
  */
15
16
  export function buildUseMutationHook(op, ctx) {
16
- const hookName = `use${op.capitalizedMethodName}`;
17
+ const hookName = hookNames(op).hook;
17
18
  const errorType = getErrorType(op, ctx);
18
- const dataTypeDefault = `Common.${op.capitalizedMethodName}MutationResult`;
19
+ const dataTypeDefault = `Common.${hookNames(op).mutationResult}`;
19
20
  const optionsType = `Options<${getDataTypeName(op)}, true>`;
20
21
  const mutationFn = `clientOptions => ${op.methodName}(${SDK_CALL_ARGS}) as unknown as Promise<TData>`;
21
- const body = `useMutation<TData, TError, ${optionsType}, TContext>({ mutationKey: Common.Use${op.capitalizedMethodName}KeyFn(mutationKey), mutationFn: ${mutationFn}, ...options })`;
22
- return {
23
- kind: "const",
24
- // The operation's JSDoc (description and @deprecated), as on the SDK function
25
- jsDoc: op.jsDoc,
26
- name: hookName,
27
- initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[], TContext = unknown>(mutationKey?: TQueryKey, options?: Omit<UseMutationOptions<TData, TError, ${optionsType}, TContext>, "mutationKey" | "mutationFn">) => ${body}`,
28
- };
22
+ const body = `useMutation<TData, TError, ${optionsType}, TContext>({ mutationKey: Common.${hookNames(op).keyFn}(mutationKey), mutationFn: ${mutationFn}, ...options })`;
23
+ return operationConst(op, hookName, `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[], TContext = unknown>(mutationKey?: TQueryKey, options?: Omit<UseMutationOptions<TData, TError, ${optionsType}, TContext>, "mutationKey" | "mutationFn">) => ${body}`, [...dataModels(op), ...errorModels(op)]);
29
24
  }
@@ -1,4 +1,5 @@
1
- import type { GenerationContext, OperationInfo } from "../types.mjs";
1
+ import type { GenerationContext } from "../types.mjs";
2
+ import { type HookOperation } from "./hookNames.mjs";
2
3
  import type { ConstStatement } from "./statement.mjs";
3
4
  /**
4
5
  * SDK call arguments shared by every generated queryFn/mutationFn.
@@ -10,24 +11,29 @@ export declare const SDK_CALL_ARGS = "{ ...clientOptions, throwOnError: true }";
10
11
  /** SDK call arguments for TanStack query functions with cancellation. */
11
12
  export declare const QUERY_SDK_CALL_ARGS = "{ ...clientOptions, signal, throwOnError: true }";
12
13
  /** Resolve the OpenAPI page parameter type, preserving older numeric output. */
13
- export declare function getPageParamType(op: OperationInfo): string;
14
+ export declare function getPageParamType(op: HookOperation): string;
14
15
  /**
15
16
  * Build the client options parameter string.
16
17
  */
17
- export declare function buildClientOptionsParam(op: OperationInfo): string;
18
+ export declare function buildClientOptionsParam(op: HookOperation): string;
18
19
  /**
19
20
  * Build the clientOptions parameter typed with the page-less infinite
20
21
  * options type — the page parameter is supplied by TanStack Query's
21
22
  * pageParam mechanism.
22
23
  */
23
- export declare function buildInfiniteClientOptionsParam(op: OperationInfo): string;
24
+ export declare function buildInfiniteClientOptionsParam(op: HookOperation): string;
24
25
  /**
25
26
  * The type of a single page. TanStack Query instantiates the infinite options
26
27
  * with this as TQueryFnData, so it is what `getNextPageParam` receives as
27
28
  * `lastPage` (#203). NonNullable because `throwOnError: true` means a resolved
28
29
  * page is always present.
29
30
  */
30
- export declare function getPageType(op: OperationInfo): string;
31
+ export declare function getPageType(op: HookOperation): string;
32
+ /**
33
+ * The models a paginated query names: the Data type in `buildPagedQueryFn`,
34
+ * and whatever the page parameter's type refers to.
35
+ */
36
+ export declare function pagedModels(op: HookOperation): string[];
31
37
  /**
32
38
  * Build the paginated SDK call shared by every infinite query builder.
33
39
  * The resolved response is narrowed to the page type so it matches the
@@ -35,13 +41,13 @@ export declare function getPageType(op: OperationInfo): string;
35
41
  * a cast rather than `!` because generated code is linted downstream, and a
36
42
  * non-null assertion trips biome's noNonNullAssertion.
37
43
  */
38
- export declare function buildPagedQueryFn(op: OperationInfo, ctx: GenerationContext): string;
44
+ export declare function buildPagedQueryFn(op: HookOperation, ctx: GenerationContext): string;
39
45
  /**
40
46
  * Format the initialPageParam literal. Emits `undefined` when the caller opted
41
47
  * to omit it (#177); otherwise a numeric literal when possible so the inferred
42
48
  * pageParam type matches what getNextPageParam returns.
43
49
  */
44
- export declare function formatInitialPageParam(ctx: GenerationContext, op?: OperationInfo): string;
50
+ export declare function formatInitialPageParam(ctx: GenerationContext, op?: HookOperation): string;
45
51
  /**
46
52
  * Build the nested type for getNextPageParam.
47
53
  * E.g., "meta.next" becomes "{ meta: { next: number } }"
@@ -52,7 +58,7 @@ export declare function buildNestedNextPageType(nextPageParam: string, pageParam
52
58
  * not every TanStack entry point contextually types it (prefetchInfiniteQuery
53
59
  * does not, which would fail noImplicitAny).
54
60
  */
55
- export declare function buildGetNextPageParamExpr(ctx: GenerationContext, op?: OperationInfo): string;
61
+ export declare function buildGetNextPageParamExpr(ctx: GenerationContext, op?: HookOperation): string;
56
62
  /**
57
63
  * Build an options type where the pagination fields TanStack Query marks as
58
64
  * required become optional overrides: the generator supplies them, and
@@ -79,19 +85,19 @@ export declare function buildOverridableInfiniteOptionsType(optionsTypeName: str
79
85
  * ...options
80
86
  * });
81
87
  */
82
- export declare function buildUseQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement;
88
+ export declare function buildUseQueryHook(op: HookOperation, ctx: GenerationContext): ConstStatement;
83
89
  /**
84
90
  * Build useSuspenseQuery hook.
85
91
  */
86
- export declare function buildUseSuspenseQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement;
92
+ export declare function buildUseSuspenseQueryHook(op: HookOperation, ctx: GenerationContext): ConstStatement;
87
93
  /**
88
94
  * Build useInfiniteQuery hook.
89
95
  */
90
- export declare function buildUseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement | null;
96
+ export declare function buildUseInfiniteQueryHook(op: HookOperation, ctx: GenerationContext): ConstStatement;
91
97
  /**
92
98
  * Build useSuspenseInfiniteQuery hook.
93
99
  */
94
- export declare function buildUseSuspenseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement | null;
100
+ export declare function buildUseSuspenseInfiniteQueryHook(op: HookOperation, ctx: GenerationContext): ConstStatement;
95
101
  /**
96
102
  * Build prefetch function.
97
103
  * Example:
@@ -102,7 +108,7 @@ export declare function buildUseSuspenseInfiniteQueryHook(op: OperationInfo, ctx
102
108
  * ...options
103
109
  * });
104
110
  */
105
- export declare function buildPrefetchFn(op: OperationInfo): ConstStatement;
111
+ export declare function buildPrefetchFn(op: HookOperation): ConstStatement;
106
112
  /**
107
113
  * Build prefetchInfiniteQuery function for a paginatable operation.
108
114
  * Example:
@@ -115,7 +121,7 @@ export declare function buildPrefetchFn(op: OperationInfo): ConstStatement;
115
121
  * ...options
116
122
  * });
117
123
  */
118
- export declare function buildPrefetchInfiniteQueryFn(op: OperationInfo, ctx: GenerationContext): ConstStatement | null;
124
+ export declare function buildPrefetchInfiniteQueryFn(op: HookOperation, ctx: GenerationContext): ConstStatement;
119
125
  /**
120
126
  * Build ensureQueryData function.
121
127
  * Example:
@@ -126,4 +132,4 @@ export declare function buildPrefetchInfiniteQueryFn(op: OperationInfo, ctx: Gen
126
132
  * ...options
127
133
  * });
128
134
  */
129
- export declare function buildEnsureQueryDataFn(op: OperationInfo): ConstStatement;
135
+ export declare function buildEnsureQueryDataFn(op: HookOperation): ConstStatement;
@@ -1,5 +1,6 @@
1
1
  import { isValidIdentifier, propertyKey } from "../client/naming.mjs";
2
- import { getDataTypeName, getErrorType } from "./operationNames.mjs";
2
+ import { hookNames } from "./hookNames.mjs";
3
+ import { dataModels, errorModels, getDataTypeName, getErrorType, operationConst, } from "./operationNames.mjs";
3
4
  /**
4
5
  * SDK call arguments shared by every generated queryFn/mutationFn.
5
6
  * throwOnError: true forces the SDK call to reject on error responses; the
@@ -27,7 +28,7 @@ export function buildClientOptionsParam(op) {
27
28
  */
28
29
  export function buildInfiniteClientOptionsParam(op) {
29
30
  const defaultValue = op.allParamsOptional ? " = {}" : "";
30
- return `clientOptions: Common.${op.capitalizedMethodName}InfiniteClientOptions${defaultValue}`;
31
+ return `clientOptions: Common.${hookNames(op).infiniteClientOptions}${defaultValue}`;
31
32
  }
32
33
  /**
33
34
  * The type of a single page. TanStack Query instantiates the infinite options
@@ -36,7 +37,14 @@ export function buildInfiniteClientOptionsParam(op) {
36
37
  * page is always present.
37
38
  */
38
39
  export function getPageType(op) {
39
- return `NonNullable<Common.${op.capitalizedMethodName}DefaultResponse>`;
40
+ return `NonNullable<Common.${hookNames(op).defaultResponse}>`;
41
+ }
42
+ /**
43
+ * The models a paginated query names: the Data type in `buildPagedQueryFn`,
44
+ * and whatever the page parameter's type refers to.
45
+ */
46
+ export function pagedModels(op) {
47
+ return [...dataModels(op), ...(op.pageParamModels ?? [])];
40
48
  }
41
49
  /**
42
50
  * Build the paginated SDK call shared by every infinite query builder.
@@ -138,37 +146,25 @@ export function buildOverridableInfiniteOptionsType(optionsTypeName, pageType) {
138
146
  * });
139
147
  */
140
148
  export function buildUseQueryHook(op, ctx) {
141
- const hookName = `use${op.capitalizedMethodName}`;
149
+ const hookName = hookNames(op).hook;
142
150
  const errorType = getErrorType(op, ctx);
143
- const dataTypeDefault = `Common.${op.capitalizedMethodName}DefaultResponse`;
151
+ const dataTypeDefault = `Common.${hookNames(op).defaultResponse}`;
144
152
  const clientOptionsParam = buildClientOptionsParam(op);
145
153
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data as TData) as TData`;
146
- const body = `useQuery<TData, TError>({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions, queryKey), queryFn: ${queryFn}, ...options })`;
147
- return {
148
- kind: "const",
149
- // The operation's JSDoc (description and @deprecated), as on the SDK function
150
- jsDoc: op.jsDoc,
151
- name: hookName,
152
- initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${clientOptionsParam}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => ${body}`,
153
- };
154
+ const body = `useQuery<TData, TError>({ queryKey: Common.${hookNames(op).keyFn}(clientOptions, queryKey), queryFn: ${queryFn}, ...options })`;
155
+ return operationConst(op, hookName, `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${clientOptionsParam}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => ${body}`, [...dataModels(op), ...errorModels(op)]);
154
156
  }
155
157
  /**
156
158
  * Build useSuspenseQuery hook.
157
159
  */
158
160
  export function buildUseSuspenseQueryHook(op, ctx) {
159
- const hookName = `use${op.capitalizedMethodName}Suspense`;
161
+ const hookName = hookNames(op).suspenseHook;
160
162
  const errorType = getErrorType(op, ctx);
161
- const dataTypeDefault = `NonNullable<Common.${op.capitalizedMethodName}DefaultResponse>`;
163
+ const dataTypeDefault = `NonNullable<Common.${hookNames(op).defaultResponse}>`;
162
164
  const clientOptionsParam = buildClientOptionsParam(op);
163
165
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data as TData) as TData`;
164
- const body = `useSuspenseQuery<TData, TError>({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions, queryKey), queryFn: ${queryFn}, ...options })`;
165
- return {
166
- kind: "const",
167
- // The operation's JSDoc (description and @deprecated), as on the SDK function
168
- jsDoc: op.jsDoc,
169
- name: hookName,
170
- initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${clientOptionsParam}, queryKey?: TQueryKey, options?: Omit<UseSuspenseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => ${body}`,
171
- };
166
+ const body = `useSuspenseQuery<TData, TError>({ queryKey: Common.${hookNames(op).keyFn}(clientOptions, queryKey), queryFn: ${queryFn}, ...options })`;
167
+ return operationConst(op, hookName, `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${clientOptionsParam}, queryKey?: TQueryKey, options?: Omit<UseSuspenseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => ${body}`, [...dataModels(op), ...errorModels(op)]);
172
168
  }
173
169
  /**
174
170
  * Build a useInfiniteQuery / useSuspenseInfiniteQuery hook. Both variants
@@ -177,31 +173,22 @@ export function buildUseSuspenseQueryHook(op, ctx) {
177
173
  * default of the suspense variant.
178
174
  */
179
175
  function buildInfiniteHook(op, ctx, suspense) {
180
- if (!op.isPaginatable) {
181
- return null;
182
- }
183
176
  const hookCall = suspense ? "useSuspenseInfiniteQuery" : "useInfiniteQuery";
184
177
  const optionsTypeName = suspense
185
178
  ? "UseSuspenseInfiniteQueryOptions"
186
179
  : "UseInfiniteQueryOptions";
187
180
  const hookName = suspense
188
- ? `use${op.capitalizedMethodName}SuspenseInfinite`
189
- : `use${op.capitalizedMethodName}Infinite`;
181
+ ? hookNames(op).suspenseInfiniteHook
182
+ : hookNames(op).infiniteHook;
190
183
  const errorType = getErrorType(op, ctx);
191
- const baseDataType = `Common.${op.capitalizedMethodName}DefaultResponse`;
184
+ const baseDataType = `Common.${hookNames(op).defaultResponse}`;
192
185
  const dataTypeDefault = suspense
193
186
  ? `InfiniteData<NonNullable<${baseDataType}>>`
194
187
  : `InfiniteData<${baseDataType}>`;
195
188
  const queryFn = buildPagedQueryFn(op, ctx);
196
189
  const infiniteOptions = `initialPageParam: ${formatInitialPageParam(ctx, op)}, getNextPageParam: ${buildGetNextPageParamExpr(ctx, op)}`;
197
- const body = `${hookCall}({ queryKey: Common.Use${op.capitalizedMethodName}InfiniteKeyFn(clientOptions, queryKey), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
198
- return {
199
- kind: "const",
200
- // The operation's JSDoc (description and @deprecated), as on the SDK function
201
- jsDoc: op.jsDoc,
202
- name: hookName,
203
- initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${buildInfiniteClientOptionsParam(op)}, queryKey?: TQueryKey, options?: ${buildOverridableInfiniteOptionsType(optionsTypeName, getPageType(op))}) => ${body}`,
204
- };
190
+ const body = `${hookCall}({ queryKey: Common.${hookNames(op).infiniteKeyFn}(clientOptions, queryKey), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
191
+ return operationConst(op, hookName, `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${buildInfiniteClientOptionsParam(op)}, queryKey?: TQueryKey, options?: ${buildOverridableInfiniteOptionsType(optionsTypeName, getPageType(op))}) => ${body}`, [...pagedModels(op), ...errorModels(op)]);
205
192
  }
206
193
  /**
207
194
  * Build useInfiniteQuery hook.
@@ -226,17 +213,11 @@ export function buildUseSuspenseInfiniteQueryHook(op, ctx) {
226
213
  * });
227
214
  */
228
215
  export function buildPrefetchFn(op) {
229
- const fnName = `prefetchUse${op.capitalizedMethodName}`;
216
+ const fnName = hookNames(op).prefetch;
230
217
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data)`;
231
- const optionsParam = `options?: Omit<FetchQueryOptions<Common.${op.capitalizedMethodName}DefaultResponse>, "queryKey" | "queryFn">`;
232
- const body = `queryClient.prefetchQuery({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions), queryFn: ${queryFn}, ...options })`;
233
- return {
234
- kind: "const",
235
- // The operation's JSDoc (description and @deprecated), as on the SDK function
236
- jsDoc: op.jsDoc,
237
- name: fnName,
238
- initializer: `(queryClient: QueryClient, ${buildClientOptionsParam(op)}, ${optionsParam}) => ${body}`,
239
- };
218
+ const optionsParam = `options?: Omit<FetchQueryOptions<Common.${hookNames(op).defaultResponse}>, "queryKey" | "queryFn">`;
219
+ const body = `queryClient.prefetchQuery({ queryKey: Common.${hookNames(op).keyFn}(clientOptions), queryFn: ${queryFn}, ...options })`;
220
+ return operationConst(op, fnName, `(queryClient: QueryClient, ${buildClientOptionsParam(op)}, ${optionsParam}) => ${body}`, dataModels(op));
240
221
  }
241
222
  /**
242
223
  * Build prefetchInfiniteQuery function for a paginatable operation.
@@ -251,10 +232,7 @@ export function buildPrefetchFn(op) {
251
232
  * });
252
233
  */
253
234
  export function buildPrefetchInfiniteQueryFn(op, ctx) {
254
- if (!op.isPaginatable) {
255
- return null;
256
- }
257
- const fnName = `prefetchUse${op.capitalizedMethodName}Infinite`;
235
+ const fnName = hookNames(op).prefetchInfinite;
258
236
  const queryFn = buildPagedQueryFn(op, ctx);
259
237
  const infiniteOptions = `initialPageParam: ${formatInitialPageParam(ctx, op)}, getNextPageParam: ${buildGetNextPageParamExpr(ctx, op)}`;
260
238
  // The pagination fields are re-added as optional overrides: `...options` is
@@ -266,14 +244,8 @@ export function buildPrefetchInfiniteQueryFn(op, ctx) {
266
244
  const instantiated = `FetchInfiniteQueryOptions<${pageType}>`;
267
245
  const overrides = `Partial<Pick<${instantiated}, "initialPageParam">> & { getNextPageParam?: GetNextPageParamFunction<unknown, ${pageType}> }`;
268
246
  const optionsParam = `options?: Omit<${instantiated}, "queryKey" | "queryFn" | "initialPageParam" | "getNextPageParam"> & ${overrides}`;
269
- const body = `queryClient.prefetchInfiniteQuery({ queryKey: Common.Use${op.capitalizedMethodName}InfiniteKeyFn(clientOptions), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
270
- return {
271
- kind: "const",
272
- // The operation's JSDoc (description and @deprecated), as on the SDK function
273
- jsDoc: op.jsDoc,
274
- name: fnName,
275
- initializer: `(queryClient: QueryClient, ${buildInfiniteClientOptionsParam(op)}, ${optionsParam}) => ${body}`,
276
- };
247
+ const body = `queryClient.prefetchInfiniteQuery({ queryKey: Common.${hookNames(op).infiniteKeyFn}(clientOptions), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
248
+ return operationConst(op, fnName, `(queryClient: QueryClient, ${buildInfiniteClientOptionsParam(op)}, ${optionsParam}) => ${body}`, pagedModels(op));
277
249
  }
278
250
  /**
279
251
  * Build ensureQueryData function.
@@ -286,15 +258,9 @@ export function buildPrefetchInfiniteQueryFn(op, ctx) {
286
258
  * });
287
259
  */
288
260
  export function buildEnsureQueryDataFn(op) {
289
- const fnName = `ensureUse${op.capitalizedMethodName}Data`;
261
+ const fnName = hookNames(op).ensureData;
290
262
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data)`;
291
- const optionsParam = `options?: Omit<EnsureQueryDataOptions<Common.${op.capitalizedMethodName}DefaultResponse>, "queryKey" | "queryFn">`;
292
- const body = `queryClient.ensureQueryData({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions), queryFn: ${queryFn}, ...options })`;
293
- return {
294
- kind: "const",
295
- // The operation's JSDoc (description and @deprecated), as on the SDK function
296
- jsDoc: op.jsDoc,
297
- name: fnName,
298
- initializer: `(queryClient: QueryClient, ${buildClientOptionsParam(op)}, ${optionsParam}) => ${body}`,
299
- };
263
+ const optionsParam = `options?: Omit<EnsureQueryDataOptions<Common.${hookNames(op).defaultResponse}>, "queryKey" | "queryFn">`;
264
+ const body = `queryClient.ensureQueryData({ queryKey: Common.${hookNames(op).keyFn}(clientOptions), queryFn: ${queryFn}, ...options })`;
265
+ return operationConst(op, fnName, `(queryClient: QueryClient, ${buildClientOptionsParam(op)}, ${optionsParam}) => ${body}`, dataModels(op));
300
266
  }
@@ -1,4 +1,5 @@
1
- import type { GenerationContext, OperationInfo } from "../types.mjs";
1
+ import type { GenerationContext } from "../types.mjs";
2
+ import { type HookOperation } from "./hookNames.mjs";
2
3
  import type { ConstStatement } from "./statement.mjs";
3
4
  /**
4
5
  * Build a queryOptions factory for a GET operation.
@@ -12,7 +13,7 @@ import type { ConstStatement } from "./statement.mjs";
12
13
  * queryFn: () => findPets({ ...clientOptions, throwOnError: true }).then(response => response.data),
13
14
  * });
14
15
  */
15
- export declare function buildQueryOptionsFn(op: OperationInfo): ConstStatement;
16
+ export declare function buildQueryOptionsFn(op: HookOperation): ConstStatement;
16
17
  /**
17
18
  * Build an infiniteQueryOptions factory for a paginatable GET operation.
18
19
  * Uses the dedicated infinite query key and page-less options type.
@@ -26,4 +27,4 @@ export declare function buildQueryOptionsFn(op: OperationInfo): ConstStatement;
26
27
  * ...options,
27
28
  * });
28
29
  */
29
- export declare function buildInfiniteQueryOptionsFn(op: OperationInfo, ctx: GenerationContext): ConstStatement | null;
30
+ export declare function buildInfiniteQueryOptionsFn(op: HookOperation, ctx: GenerationContext): ConstStatement;