@7nohe/openapi-react-query-codegen 4.0.0-beta.3 → 4.0.0-beta.5

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 (56) hide show
  1. package/AGENTS.md +8 -4
  2. package/README.md +2 -2
  3. package/dist/client/emitSchemas.mjs +12 -9
  4. package/dist/client/emitSdk.d.mts +2 -0
  5. package/dist/client/emitSdk.mjs +24 -27
  6. package/dist/client/emitTransformers.mjs +21 -72
  7. package/dist/client/emitTypes.d.mts +2 -1
  8. package/dist/client/emitTypes.mjs +5 -5
  9. package/dist/client/index.mjs +13 -5
  10. package/dist/client/naming.d.mts +5 -2
  11. package/dist/client/naming.mjs +7 -3
  12. package/dist/client/operations.d.mts +3 -2
  13. package/dist/client/operations.mjs +16 -48
  14. package/dist/client/runtime/axios.runtime.ts +15 -10
  15. package/dist/client/schema.d.mts +24 -2
  16. package/dist/client/schema.mjs +135 -33
  17. package/dist/client/spec.d.mts +6 -0
  18. package/dist/client/spec.mjs +15 -5
  19. package/dist/common.d.mts +0 -2
  20. package/dist/common.mjs +0 -10
  21. package/dist/createSource.d.mts +1 -1
  22. package/dist/createSource.mjs +1 -3
  23. package/dist/format.d.mts +0 -1
  24. package/dist/format.mjs +0 -20
  25. package/dist/generate.mjs +2 -4
  26. package/dist/{tsmorph → hooks}/buildCommon.d.mts +13 -17
  27. package/dist/hooks/buildCommon.mjs +132 -0
  28. package/dist/{tsmorph → hooks}/buildMutationHooks.d.mts +4 -3
  29. package/dist/hooks/buildMutationHooks.mjs +24 -0
  30. package/dist/{tsmorph → hooks}/buildQueryHooks.d.mts +22 -16
  31. package/dist/{tsmorph → hooks}/buildQueryHooks.mjs +35 -106
  32. package/dist/{tsmorph → hooks}/buildQueryOptions.d.mts +5 -4
  33. package/dist/{tsmorph → hooks}/buildQueryOptions.mjs +9 -35
  34. package/dist/{tsmorph → hooks}/generateFiles.d.mts +1 -1
  35. package/dist/hooks/generateFiles.mjs +122 -0
  36. package/dist/hooks/hookNames.d.mts +69 -0
  37. package/dist/hooks/hookNames.mjs +82 -0
  38. package/dist/hooks/imports.d.mts +46 -0
  39. package/dist/hooks/imports.mjs +115 -0
  40. package/dist/hooks/operationNames.d.mts +15 -0
  41. package/dist/hooks/operationNames.mjs +31 -0
  42. package/dist/hooks/statement.d.mts +43 -0
  43. package/dist/hooks/statement.mjs +63 -0
  44. package/dist/types.d.mts +5 -7
  45. package/package.json +3 -5
  46. package/dist/print.d.mts +0 -5
  47. package/dist/print.mjs +0 -22
  48. package/dist/tsmorph/buildCommon.mjs +0 -183
  49. package/dist/tsmorph/buildMutationHooks.mjs +0 -36
  50. package/dist/tsmorph/generateFiles.mjs +0 -153
  51. package/dist/tsmorph/index.d.mts +0 -5
  52. package/dist/tsmorph/index.mjs +0 -5
  53. package/dist/tsmorph/operationNames.d.mts +0 -5
  54. package/dist/tsmorph/operationNames.mjs +0 -9
  55. package/dist/tsmorph/projectFactory.d.mts +0 -58
  56. package/dist/tsmorph/projectFactory.mjs +0 -169
@@ -0,0 +1,132 @@
1
+ import { buildClientOptionsParam } from "./buildQueryHooks.mjs";
2
+ import { hookNames } from "./hookNames.mjs";
3
+ import { dataModels, getDataTypeName } from "./operationNames.mjs";
4
+ /**
5
+ * Build the default response type alias.
6
+ * Example: export type FindPetsDefaultResponse = Awaited<ReturnType<typeof findPets>>["data"];
7
+ */
8
+ export function buildDefaultResponseType(op) {
9
+ return {
10
+ kind: "type",
11
+ name: hookNames(op).defaultResponse,
12
+ type: `Awaited<ReturnType<typeof ${op.methodName}>>["data"]`,
13
+ service: op.methodName,
14
+ };
15
+ }
16
+ /**
17
+ * Build the query result type alias.
18
+ * Example: export type FindPetsQueryResult<TData = FindPetsDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
19
+ */
20
+ export function buildQueryResultType(op) {
21
+ return {
22
+ kind: "type",
23
+ name: hookNames(op).queryResult,
24
+ typeParameters: [
25
+ { name: "TData", default: hookNames(op).defaultResponse },
26
+ { name: "TError", default: "unknown" },
27
+ ],
28
+ type: "UseQueryResult<TData, TError>",
29
+ };
30
+ }
31
+ /**
32
+ * Build the mutation result type alias.
33
+ * Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet>>;
34
+ */
35
+ export function buildMutationResultType(op) {
36
+ return {
37
+ kind: "type",
38
+ name: hookNames(op).mutationResult,
39
+ type: `Awaited<ReturnType<typeof ${op.methodName}>>`,
40
+ service: op.methodName,
41
+ };
42
+ }
43
+ /**
44
+ * Build the key constant of a query or a mutation.
45
+ * Example: export const useFindPetsKey = "FindPets";
46
+ */
47
+ export function buildKeyConst(op) {
48
+ return {
49
+ kind: "const",
50
+ name: hookNames(op).key,
51
+ initializer: `"${op.capitalizedMethodName}"`,
52
+ };
53
+ }
54
+ /**
55
+ * Build query key function.
56
+ * Example: export const UseFindPetsKeyFn = (clientOptions: Options<FindPetsData, true> = {}, queryKey?: Array<unknown>) =>
57
+ * [useFindPetsKey, ...(queryKey ?? [clientOptions])];
58
+ */
59
+ export function buildQueryKeyFn(op) {
60
+ return {
61
+ kind: "const",
62
+ name: hookNames(op).keyFn,
63
+ initializer: `(${buildClientOptionsParam(op)}, queryKey?: Array<unknown>) => [${hookNames(op).key}, ...(queryKey ?? [clientOptions])]`,
64
+ models: dataModels(op),
65
+ };
66
+ }
67
+ /**
68
+ * Build mutation key function.
69
+ * Example: export const UseAddPetKeyFn = (mutationKey?: Array<unknown>) =>
70
+ * [useAddPetKey, ...(mutationKey ?? [])];
71
+ */
72
+ export function buildMutationKeyFn(op) {
73
+ return {
74
+ kind: "const",
75
+ name: hookNames(op).keyFn,
76
+ initializer: `(mutationKey?: Array<unknown>) => [${hookNames(op).key}, ...(mutationKey ?? [])]`,
77
+ };
78
+ }
79
+ /**
80
+ * Build the client options type for infinite queries.
81
+ * The page parameter is excluded because TanStack Query supplies it via the
82
+ * pageParam mechanism (#140).
83
+ * Example:
84
+ * export type FindPaginatedPetsInfiniteClientOptions = Omit<Options<FindPaginatedPetsData, true>, "query"> &
85
+ * { query?: Omit<NonNullable<FindPaginatedPetsData["query"]>, "page"> };
86
+ */
87
+ export function buildInfiniteClientOptionsType(op, ctx) {
88
+ const dataTypeName = getDataTypeName(op);
89
+ const type = `Omit<Options<${dataTypeName}, true>, "query"> & { query?: Omit<NonNullable<${dataTypeName}["query"]>, "${ctx.pageParam}"> }`;
90
+ return {
91
+ kind: "type",
92
+ name: hookNames(op).infiniteClientOptions,
93
+ type,
94
+ models: dataModels(op),
95
+ };
96
+ }
97
+ /**
98
+ * Build the infinite query key constant.
99
+ * Shares the plain query key as its first segment so a single
100
+ * `invalidateQueries({ queryKey: [useXKey] })` matches both the plain and the
101
+ * infinite cache entries of an operation (#174), while the extra "infinite"
102
+ * segment keeps cached InfiniteData from colliding with plain query data (#140).
103
+ * Example: export const useFindPaginatedPetsInfiniteKey = [useFindPaginatedPetsKey, "infinite"] as const;
104
+ */
105
+ export function buildInfiniteQueryKeyConst(op) {
106
+ return {
107
+ kind: "const",
108
+ name: hookNames(op).infiniteKey,
109
+ initializer: `[${hookNames(op).key}, "infinite"] as const`,
110
+ };
111
+ }
112
+ /**
113
+ * Build the infinite query key function.
114
+ * The custom queryKey argument only replaces the params segment — the
115
+ * hierarchical [opKey, "infinite"] prefix is always preserved so
116
+ * prefix-based invalidation keeps working.
117
+ * Example: export const UseFindPaginatedPetsInfiniteKeyFn = (clientOptions: FindPaginatedPetsInfiniteClientOptions = {}, queryKey?: Array<unknown>) =>
118
+ * [...useFindPaginatedPetsInfiniteKey, ...(queryKey ?? [clientOptions])];
119
+ */
120
+ export function buildInfiniteQueryKeyFn(op) {
121
+ const names = hookNames(op);
122
+ const defaultValue = op.allParamsOptional ? " = {}" : "";
123
+ const params = [
124
+ `clientOptions: ${names.infiniteClientOptions}${defaultValue}`,
125
+ "queryKey?: Array<unknown>",
126
+ ];
127
+ return {
128
+ kind: "const",
129
+ name: names.infiniteKeyFn,
130
+ initializer: `(${params.join(", ")}) => [...${names.infiniteKey}, ...(queryKey ?? [clientOptions])]`,
131
+ };
132
+ }
@@ -1,5 +1,6 @@
1
- import { type VariableStatementStructure } from "ts-morph";
2
- import type { GenerationContext, OperationInfo } from "../types.mjs";
1
+ import type { GenerationContext } from "../types.mjs";
2
+ import { type HookOperation } from "./hookNames.mjs";
3
+ import type { ConstStatement } from "./statement.mjs";
3
4
  /**
4
5
  * Build useMutation hook.
5
6
  * Example:
@@ -12,4 +13,4 @@ import type { GenerationContext, OperationInfo } from "../types.mjs";
12
13
  * ...options
13
14
  * });
14
15
  */
15
- export declare function buildUseMutationHook(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure;
16
+ export declare function buildUseMutationHook(op: HookOperation, ctx: GenerationContext): ConstStatement;
@@ -0,0 +1,24 @@
1
+ import { SDK_CALL_ARGS } from "./buildQueryHooks.mjs";
2
+ import { hookNames } from "./hookNames.mjs";
3
+ import { dataModels, errorModels, getDataTypeName, getErrorType, operationConst, } from "./operationNames.mjs";
4
+ /**
5
+ * Build useMutation hook.
6
+ * Example:
7
+ * export const useAddPet = <TData = Common.AddPetMutationResult, TError = AddPetError, TQueryKey extends Array<unknown> = unknown[], TContext = unknown>(
8
+ * mutationKey?: TQueryKey,
9
+ * options?: Omit<UseMutationOptions<TData, TError, Options<AddPetData, true>, TContext>, "mutationKey" | "mutationFn">
10
+ * ) => useMutation<TData, TError, Options<AddPetData, true>, TContext>({
11
+ * mutationKey: Common.UseAddPetKeyFn(mutationKey),
12
+ * mutationFn: clientOptions => addPet(clientOptions) as unknown as Promise<TData>,
13
+ * ...options
14
+ * });
15
+ */
16
+ export function buildUseMutationHook(op, ctx) {
17
+ const hookName = hookNames(op).hook;
18
+ const errorType = getErrorType(op, ctx);
19
+ const dataTypeDefault = `Common.${hookNames(op).mutationResult}`;
20
+ const optionsType = `Options<${getDataTypeName(op)}, true>`;
21
+ const mutationFn = `clientOptions => ${op.methodName}(${SDK_CALL_ARGS}) as unknown as Promise<TData>`;
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)]);
24
+ }
@@ -1,5 +1,6 @@
1
- import { type VariableStatementStructure } from "ts-morph";
2
- import type { GenerationContext, OperationInfo } from "../types.mjs";
1
+ import type { GenerationContext } from "../types.mjs";
2
+ import { type HookOperation } from "./hookNames.mjs";
3
+ import type { ConstStatement } from "./statement.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
@@ -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): VariableStatementStructure;
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): VariableStatementStructure;
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): VariableStatementStructure | 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): VariableStatementStructure | 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): VariableStatementStructure;
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): VariableStatementStr
115
121
  * ...options
116
122
  * });
117
123
  */
118
- export declare function buildPrefetchInfiniteQueryFn(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure | 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): VariableStatementStructure;
135
+ export declare function buildEnsureQueryDataFn(op: HookOperation): ConstStatement;
@@ -1,6 +1,6 @@
1
- import { StructureKind, VariableDeclarationKind, } from "ts-morph";
2
1
  import { isValidIdentifier, propertyKey } from "../client/naming.mjs";
3
- import { getDataTypeName, getErrorType } from "./operationNames.mjs";
2
+ import { hookNames } from "./hookNames.mjs";
3
+ import { dataModels, errorModels, getDataTypeName, getErrorType, operationConst, } from "./operationNames.mjs";
4
4
  /**
5
5
  * SDK call arguments shared by every generated queryFn/mutationFn.
6
6
  * throwOnError: true forces the SDK call to reject on error responses; the
@@ -28,7 +28,7 @@ export function buildClientOptionsParam(op) {
28
28
  */
29
29
  export function buildInfiniteClientOptionsParam(op) {
30
30
  const defaultValue = op.allParamsOptional ? " = {}" : "";
31
- return `clientOptions: Common.${op.capitalizedMethodName}InfiniteClientOptions${defaultValue}`;
31
+ return `clientOptions: Common.${hookNames(op).infiniteClientOptions}${defaultValue}`;
32
32
  }
33
33
  /**
34
34
  * The type of a single page. TanStack Query instantiates the infinite options
@@ -37,7 +37,14 @@ export function buildInfiniteClientOptionsParam(op) {
37
37
  * page is always present.
38
38
  */
39
39
  export function getPageType(op) {
40
- 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 ?? [])];
41
48
  }
42
49
  /**
43
50
  * Build the paginated SDK call shared by every infinite query builder.
@@ -139,49 +146,25 @@ export function buildOverridableInfiniteOptionsType(optionsTypeName, pageType) {
139
146
  * });
140
147
  */
141
148
  export function buildUseQueryHook(op, ctx) {
142
- const hookName = `use${op.capitalizedMethodName}`;
149
+ const hookName = hookNames(op).hook;
143
150
  const errorType = getErrorType(op, ctx);
144
- const dataTypeDefault = `Common.${op.capitalizedMethodName}DefaultResponse`;
151
+ const dataTypeDefault = `Common.${hookNames(op).defaultResponse}`;
145
152
  const clientOptionsParam = buildClientOptionsParam(op);
146
153
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data as TData) as TData`;
147
- const body = `useQuery<TData, TError>({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions, queryKey), queryFn: ${queryFn}, ...options })`;
148
- return {
149
- kind: StructureKind.VariableStatement,
150
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
151
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
152
- isExported: true,
153
- declarationKind: VariableDeclarationKind.Const,
154
- declarations: [
155
- {
156
- name: hookName,
157
- initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${clientOptionsParam}, queryKey?: TQueryKey, options?: Omit<UseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => ${body}`,
158
- },
159
- ],
160
- };
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)]);
161
156
  }
162
157
  /**
163
158
  * Build useSuspenseQuery hook.
164
159
  */
165
160
  export function buildUseSuspenseQueryHook(op, ctx) {
166
- const hookName = `use${op.capitalizedMethodName}Suspense`;
161
+ const hookName = hookNames(op).suspenseHook;
167
162
  const errorType = getErrorType(op, ctx);
168
- const dataTypeDefault = `NonNullable<Common.${op.capitalizedMethodName}DefaultResponse>`;
163
+ const dataTypeDefault = `NonNullable<Common.${hookNames(op).defaultResponse}>`;
169
164
  const clientOptionsParam = buildClientOptionsParam(op);
170
165
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data as TData) as TData`;
171
- const body = `useSuspenseQuery<TData, TError>({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions, queryKey), queryFn: ${queryFn}, ...options })`;
172
- return {
173
- kind: StructureKind.VariableStatement,
174
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
175
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
176
- isExported: true,
177
- declarationKind: VariableDeclarationKind.Const,
178
- declarations: [
179
- {
180
- name: hookName,
181
- initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${clientOptionsParam}, queryKey?: TQueryKey, options?: Omit<UseSuspenseQueryOptions<TData, TError>, "queryKey" | "queryFn">) => ${body}`,
182
- },
183
- ],
184
- };
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)]);
185
168
  }
186
169
  /**
187
170
  * Build a useInfiniteQuery / useSuspenseInfiniteQuery hook. Both variants
@@ -190,37 +173,22 @@ export function buildUseSuspenseQueryHook(op, ctx) {
190
173
  * default of the suspense variant.
191
174
  */
192
175
  function buildInfiniteHook(op, ctx, suspense) {
193
- if (!op.isPaginatable) {
194
- return null;
195
- }
196
176
  const hookCall = suspense ? "useSuspenseInfiniteQuery" : "useInfiniteQuery";
197
177
  const optionsTypeName = suspense
198
178
  ? "UseSuspenseInfiniteQueryOptions"
199
179
  : "UseInfiniteQueryOptions";
200
180
  const hookName = suspense
201
- ? `use${op.capitalizedMethodName}SuspenseInfinite`
202
- : `use${op.capitalizedMethodName}Infinite`;
181
+ ? hookNames(op).suspenseInfiniteHook
182
+ : hookNames(op).infiniteHook;
203
183
  const errorType = getErrorType(op, ctx);
204
- const baseDataType = `Common.${op.capitalizedMethodName}DefaultResponse`;
184
+ const baseDataType = `Common.${hookNames(op).defaultResponse}`;
205
185
  const dataTypeDefault = suspense
206
186
  ? `InfiniteData<NonNullable<${baseDataType}>>`
207
187
  : `InfiniteData<${baseDataType}>`;
208
188
  const queryFn = buildPagedQueryFn(op, ctx);
209
189
  const infiniteOptions = `initialPageParam: ${formatInitialPageParam(ctx, op)}, getNextPageParam: ${buildGetNextPageParamExpr(ctx, op)}`;
210
- const body = `${hookCall}({ queryKey: Common.Use${op.capitalizedMethodName}InfiniteKeyFn(clientOptions, queryKey), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
211
- return {
212
- kind: StructureKind.VariableStatement,
213
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
214
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
215
- isExported: true,
216
- declarationKind: VariableDeclarationKind.Const,
217
- declarations: [
218
- {
219
- name: hookName,
220
- initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[]>(${buildInfiniteClientOptionsParam(op)}, queryKey?: TQueryKey, options?: ${buildOverridableInfiniteOptionsType(optionsTypeName, getPageType(op))}) => ${body}`,
221
- },
222
- ],
223
- };
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)]);
224
192
  }
225
193
  /**
226
194
  * Build useInfiniteQuery hook.
@@ -245,23 +213,11 @@ export function buildUseSuspenseInfiniteQueryHook(op, ctx) {
245
213
  * });
246
214
  */
247
215
  export function buildPrefetchFn(op) {
248
- const fnName = `prefetchUse${op.capitalizedMethodName}`;
216
+ const fnName = hookNames(op).prefetch;
249
217
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data)`;
250
- const optionsParam = `options?: Omit<FetchQueryOptions<Common.${op.capitalizedMethodName}DefaultResponse>, "queryKey" | "queryFn">`;
251
- const body = `queryClient.prefetchQuery({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions), queryFn: ${queryFn}, ...options })`;
252
- return {
253
- kind: StructureKind.VariableStatement,
254
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
255
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
256
- isExported: true,
257
- declarationKind: VariableDeclarationKind.Const,
258
- declarations: [
259
- {
260
- name: fnName,
261
- initializer: `(queryClient: QueryClient, ${buildClientOptionsParam(op)}, ${optionsParam}) => ${body}`,
262
- },
263
- ],
264
- };
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));
265
221
  }
266
222
  /**
267
223
  * Build prefetchInfiniteQuery function for a paginatable operation.
@@ -276,10 +232,7 @@ export function buildPrefetchFn(op) {
276
232
  * });
277
233
  */
278
234
  export function buildPrefetchInfiniteQueryFn(op, ctx) {
279
- if (!op.isPaginatable) {
280
- return null;
281
- }
282
- const fnName = `prefetchUse${op.capitalizedMethodName}Infinite`;
235
+ const fnName = hookNames(op).prefetchInfinite;
283
236
  const queryFn = buildPagedQueryFn(op, ctx);
284
237
  const infiniteOptions = `initialPageParam: ${formatInitialPageParam(ctx, op)}, getNextPageParam: ${buildGetNextPageParamExpr(ctx, op)}`;
285
238
  // The pagination fields are re-added as optional overrides: `...options` is
@@ -291,20 +244,8 @@ export function buildPrefetchInfiniteQueryFn(op, ctx) {
291
244
  const instantiated = `FetchInfiniteQueryOptions<${pageType}>`;
292
245
  const overrides = `Partial<Pick<${instantiated}, "initialPageParam">> & { getNextPageParam?: GetNextPageParamFunction<unknown, ${pageType}> }`;
293
246
  const optionsParam = `options?: Omit<${instantiated}, "queryKey" | "queryFn" | "initialPageParam" | "getNextPageParam"> & ${overrides}`;
294
- const body = `queryClient.prefetchInfiniteQuery({ queryKey: Common.Use${op.capitalizedMethodName}InfiniteKeyFn(clientOptions), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
295
- return {
296
- kind: StructureKind.VariableStatement,
297
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
298
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
299
- isExported: true,
300
- declarationKind: VariableDeclarationKind.Const,
301
- declarations: [
302
- {
303
- name: fnName,
304
- initializer: `(queryClient: QueryClient, ${buildInfiniteClientOptionsParam(op)}, ${optionsParam}) => ${body}`,
305
- },
306
- ],
307
- };
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));
308
249
  }
309
250
  /**
310
251
  * Build ensureQueryData function.
@@ -317,21 +258,9 @@ export function buildPrefetchInfiniteQueryFn(op, ctx) {
317
258
  * });
318
259
  */
319
260
  export function buildEnsureQueryDataFn(op) {
320
- const fnName = `ensureUse${op.capitalizedMethodName}Data`;
261
+ const fnName = hookNames(op).ensureData;
321
262
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data)`;
322
- const optionsParam = `options?: Omit<EnsureQueryDataOptions<Common.${op.capitalizedMethodName}DefaultResponse>, "queryKey" | "queryFn">`;
323
- const body = `queryClient.ensureQueryData({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions), queryFn: ${queryFn}, ...options })`;
324
- return {
325
- kind: StructureKind.VariableStatement,
326
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
327
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
328
- isExported: true,
329
- declarationKind: VariableDeclarationKind.Const,
330
- declarations: [
331
- {
332
- name: fnName,
333
- initializer: `(queryClient: QueryClient, ${buildClientOptionsParam(op)}, ${optionsParam}) => ${body}`,
334
- },
335
- ],
336
- };
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));
337
266
  }
@@ -1,5 +1,6 @@
1
- import { type VariableStatementStructure } from "ts-morph";
2
- import type { GenerationContext, OperationInfo } from "../types.mjs";
1
+ import type { GenerationContext } from "../types.mjs";
2
+ import { type HookOperation } from "./hookNames.mjs";
3
+ import type { ConstStatement } from "./statement.mjs";
3
4
  /**
4
5
  * Build a queryOptions factory for a GET operation.
5
6
  * The factory centralizes queryKey and queryFn so they can be reused with
@@ -12,7 +13,7 @@ import type { GenerationContext, OperationInfo } from "../types.mjs";
12
13
  * queryFn: () => findPets({ ...clientOptions, throwOnError: true }).then(response => response.data),
13
14
  * });
14
15
  */
15
- export declare function buildQueryOptionsFn(op: OperationInfo): VariableStatementStructure;
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): VariableStatemen
26
27
  * ...options,
27
28
  * });
28
29
  */
29
- export declare function buildInfiniteQueryOptionsFn(op: OperationInfo, ctx: GenerationContext): VariableStatementStructure | null;
30
+ export declare function buildInfiniteQueryOptionsFn(op: HookOperation, ctx: GenerationContext): ConstStatement;
@@ -1,5 +1,6 @@
1
- import { StructureKind, VariableDeclarationKind, } from "ts-morph";
2
- import { buildClientOptionsParam, buildGetNextPageParamExpr, buildInfiniteClientOptionsParam, buildPagedQueryFn, formatInitialPageParam, getPageType, QUERY_SDK_CALL_ARGS, } from "./buildQueryHooks.mjs";
1
+ import { buildClientOptionsParam, buildGetNextPageParamExpr, buildInfiniteClientOptionsParam, buildPagedQueryFn, formatInitialPageParam, getPageType, pagedModels, QUERY_SDK_CALL_ARGS, } from "./buildQueryHooks.mjs";
2
+ import { hookNames } from "./hookNames.mjs";
3
+ import { dataModels, operationConst } from "./operationNames.mjs";
3
4
  /**
4
5
  * Build a queryOptions factory for a GET operation.
5
6
  * The factory centralizes queryKey and queryFn so they can be reused with
@@ -13,23 +14,11 @@ import { buildClientOptionsParam, buildGetNextPageParamExpr, buildInfiniteClient
13
14
  * });
14
15
  */
15
16
  export function buildQueryOptionsFn(op) {
16
- const fnName = `${op.methodName}Options`;
17
+ const fnName = hookNames(op).options;
17
18
  const clientOptionsParam = buildClientOptionsParam(op);
18
19
  const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data)`;
19
- const body = `queryOptions({ queryKey: Common.Use${op.capitalizedMethodName}KeyFn(clientOptions, queryKey), queryFn: ${queryFn} })`;
20
- return {
21
- kind: StructureKind.VariableStatement,
22
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
23
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
24
- isExported: true,
25
- declarationKind: VariableDeclarationKind.Const,
26
- declarations: [
27
- {
28
- name: fnName,
29
- initializer: `(${clientOptionsParam}, queryKey?: Array<unknown>) => ${body}`,
30
- },
31
- ],
32
- };
20
+ const body = `queryOptions({ queryKey: Common.${hookNames(op).keyFn}(clientOptions, queryKey), queryFn: ${queryFn} })`;
21
+ return operationConst(op, fnName, `(${clientOptionsParam}, queryKey?: Array<unknown>) => ${body}`, dataModels(op));
33
22
  }
34
23
  /**
35
24
  * Build an infiniteQueryOptions factory for a paginatable GET operation.
@@ -45,28 +34,13 @@ export function buildQueryOptionsFn(op) {
45
34
  * });
46
35
  */
47
36
  export function buildInfiniteQueryOptionsFn(op, ctx) {
48
- if (!op.isPaginatable) {
49
- return null;
50
- }
51
- const fnName = `${op.methodName}InfiniteOptions`;
37
+ const fnName = hookNames(op).infiniteOptions;
52
38
  const queryFn = buildPagedQueryFn(op, ctx);
53
39
  const infiniteOptions = `initialPageParam: ${formatInitialPageParam(ctx, op)}, getNextPageParam: ${buildGetNextPageParamExpr(ctx, op)}`;
54
40
  // Only the pagination fields are overridable here: the factory's return type
55
41
  // is what every downstream consumer infers from, and a wider options type
56
42
  // (select, placeholderData, ...) would make that inference ambiguous (#203).
57
43
  const optionsParam = `options?: Partial<Pick<UseInfiniteQueryOptions<${getPageType(op)}>, "initialPageParam" | "getNextPageParam">>`;
58
- const body = `infiniteQueryOptions({ queryKey: Common.Use${op.capitalizedMethodName}InfiniteKeyFn(clientOptions, queryKey), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
59
- return {
60
- kind: StructureKind.VariableStatement,
61
- // Copy the operation's JSDoc (description and @deprecated) from the SDK function
62
- leadingTrivia: op.jsDoc ? `${op.jsDoc}\n` : undefined,
63
- isExported: true,
64
- declarationKind: VariableDeclarationKind.Const,
65
- declarations: [
66
- {
67
- name: fnName,
68
- initializer: `(${buildInfiniteClientOptionsParam(op)}, queryKey?: Array<unknown>, ${optionsParam}) => ${body}`,
69
- },
70
- ],
71
- };
44
+ const body = `infiniteQueryOptions({ queryKey: Common.${hookNames(op).infiniteKeyFn}(clientOptions, queryKey), queryFn: ${queryFn}, ${infiniteOptions}, ...options })`;
45
+ return operationConst(op, fnName, `(${buildInfiniteClientOptionsParam(op)}, queryKey?: Array<unknown>, ${optionsParam}) => ${body}`, pagedModels(op));
72
46
  }
@@ -1,5 +1,5 @@
1
1
  import type { GeneratedFile, GenerationContext, OperationInfo } from "../types.mjs";
2
2
  /**
3
- * Generate all files using ts-morph.
3
+ * Generate the React Query files for the operations of a specification.
4
4
  */
5
5
  export declare function generateAllFiles(operations: OperationInfo[], ctx: GenerationContext): GeneratedFile[];