@7nohe/openapi-react-query-codegen 4.0.0-beta.2 → 4.0.0-beta.4
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/AGENTS.md +4 -4
- package/README.md +3 -2
- package/dist/client/operations.mjs +6 -1
- package/dist/client/tsconfig.d.mts +1 -2
- package/dist/client/tsconfig.mjs +118 -22
- package/dist/createSource.mjs +1 -1
- package/dist/format.d.mts +0 -1
- package/dist/format.mjs +0 -20
- package/dist/generate.mjs +1 -2
- package/dist/{tsmorph → hooks}/buildCommon.d.mts +11 -11
- package/dist/{tsmorph → hooks}/buildCommon.mjs +22 -63
- package/dist/{tsmorph → hooks}/buildMutationHooks.d.mts +2 -2
- package/dist/{tsmorph → hooks}/buildMutationHooks.mjs +5 -12
- package/dist/{tsmorph → hooks}/buildQueryHooks.d.mts +8 -8
- package/dist/{tsmorph → hooks}/buildQueryHooks.mjs +30 -67
- package/dist/{tsmorph → hooks}/buildQueryOptions.d.mts +3 -3
- package/dist/{tsmorph → hooks}/buildQueryOptions.mjs +10 -23
- package/dist/{tsmorph → hooks}/generateFiles.d.mts +1 -1
- package/dist/{tsmorph → hooks}/generateFiles.mjs +29 -33
- package/dist/{tsmorph/projectFactory.d.mts → hooks/imports.d.mts} +29 -16
- package/dist/{tsmorph/projectFactory.mjs → hooks/imports.mjs} +54 -26
- package/dist/{tsmorph → hooks}/index.d.mts +0 -1
- package/dist/{tsmorph → hooks}/index.mjs +0 -1
- package/dist/hooks/statement.d.mts +34 -0
- package/dist/hooks/statement.mjs +58 -0
- package/package.json +3 -6
- /package/dist/{tsmorph → hooks}/operationNames.d.mts +0 -0
- /package/dist/{tsmorph → hooks}/operationNames.mjs +0 -0
package/AGENTS.md
CHANGED
|
@@ -21,7 +21,7 @@ npm install -D @7nohe/openapi-react-query-codegen
|
|
|
21
21
|
npm install @tanstack/react-query
|
|
22
22
|
```
|
|
23
23
|
|
|
24
|
-
`commander
|
|
24
|
+
`commander` is a peer dependency. If your package manager does not install peers automatically, add it as a devDependency. The generated code compiles with TypeScript 5, 6 and 7; the generator itself does not need the `typescript` package. For `--client axios`, also add `axios`. Nothing else is needed at runtime; the client code is generated into `requests/`.
|
|
25
25
|
|
|
26
26
|
Only the scoped `@7nohe/openapi-react-query-codegen` package belongs to this project. The unscoped `openapi-react-query-codegen` on npm is someone else's.
|
|
27
27
|
|
|
@@ -189,13 +189,13 @@ import { check, generate } from "@7nohe/openapi-react-query-codegen";
|
|
|
189
189
|
- After generator changes, run tests and consider regenerating example outputs to manually diff.
|
|
190
190
|
|
|
191
191
|
### Commit & Pull Request Guidelines
|
|
192
|
-
- Commits: clear, descriptive messages (e.g., `fix: align imports for generated queries`, `chore: update
|
|
192
|
+
- Commits: clear, descriptive messages (e.g., `fix: align imports for generated queries`, `chore: update biome config`). Avoid bundling unrelated changes.
|
|
193
193
|
- Pull requests: include summary of changes, affected areas (e.g., codegen output, docs, examples), and test commands run. Link issues when applicable. Add before/after notes or sample generated snippets if behavior changes.
|
|
194
194
|
|
|
195
195
|
### Architecture: IR Boundary
|
|
196
196
|
- `src/client/` is the client generator. It reads the OpenAPI document and writes `requests/` (`types.gen.ts`, `sdk.gen.ts`, `client.gen.ts`, `runtime.gen.ts`, and on request `schemas.gen.ts` and `transformers.gen.ts`). It also returns the normalized operations (`OperationInfo` in `src/types.mts`) for the hook generators, so nothing re-parses the generated SDK.
|
|
197
197
|
- `src/generate.mts` is the only place that turns the client generator's failures into the error codes of `--json` (`mapClientError`).
|
|
198
|
-
- Generation-side modules (`src/
|
|
198
|
+
- Generation-side modules (`src/hooks/build*.mts`, `generateFiles.mts`) consume only `OperationInfo` and `GenerationContext`. Do not teach them about the OpenAPI document or the runtime's internals; extend the IR instead.
|
|
199
199
|
- The generated hooks rely on a small SDK contract: `Options<XData, ThrowOnError>` exported from `sdk.gen`, calls returning `{ data }` and rejecting when `throwOnError` is set, and the `XData` / `XError` type names. Keep it in sync with `src/client/emitSdk.mts` and the runtimes in `src/client/runtime/`.
|
|
200
200
|
- The runtimes (`src/client/runtime/*.runtime.ts`) are plain TypeScript that is copied verbatim into users' projects. Keep them dependency-free (axios aside), strict-mode clean, and free of syntax that needs a newer TypeScript than the generated code supports. `scripts/copy-runtime.mjs` ships them next to the compiled generator.
|
|
201
201
|
|
|
@@ -203,5 +203,5 @@ import { check, generate } from "@7nohe/openapi-react-query-codegen";
|
|
|
203
203
|
- Runtime dependencies are deliberately few (`cross-spawn`, `yaml`). Adding one needs a reason; the generator replaced `@hey-api/openapi-ts` to cut the supply-chain surface.
|
|
204
204
|
|
|
205
205
|
### Agent-Specific Notes
|
|
206
|
-
-
|
|
206
|
+
- The generators build source text as strings. After changing one, run the tests that compile the generated output (`tests/createSource.test.ts`, `tests/client/`) to confirm it is still valid TypeScript.
|
|
207
207
|
- Respect ignore patterns in `biome.json` and avoid checking in `dist/` or example-generated artifacts unless explicitly intended.
|
package/README.md
CHANGED
|
@@ -59,9 +59,10 @@ The client generator is part of this package, so the generated `requests/` direc
|
|
|
59
59
|
|
|
60
60
|
## Requirements
|
|
61
61
|
|
|
62
|
-
- Node.js
|
|
62
|
+
- Node.js 24+
|
|
63
63
|
- `@tanstack/react-query` 5.x (peer dependency)
|
|
64
|
-
- `
|
|
64
|
+
- `commander` 12–15 (peer dependency)
|
|
65
|
+
- TypeScript 5.x, 6.x or 7.x to compile the generated code. It is not a peer dependency: the generator does not use the `typescript` package.
|
|
65
66
|
|
|
66
67
|
## License
|
|
67
68
|
|
|
@@ -5,7 +5,8 @@ import { getByPointer, resolveRef } from "./spec.mjs";
|
|
|
5
5
|
* Type names a schema or an operation type cannot take: `ClientOptions` and
|
|
6
6
|
* `Options` are exported next to them, and the rest are what the generated hook
|
|
7
7
|
* files import or use, where an imported model of the same name would clash
|
|
8
|
-
* with or shadow them.
|
|
8
|
+
* with or shadow them. The last four are the type parameters of the hooks,
|
|
9
|
+
* which would shadow the model instead.
|
|
9
10
|
*/
|
|
10
11
|
const RESERVED_TYPE_NAMES = [
|
|
11
12
|
"ClientOptions",
|
|
@@ -34,6 +35,10 @@ const RESERVED_TYPE_NAMES = [
|
|
|
34
35
|
"Promise",
|
|
35
36
|
"Record",
|
|
36
37
|
"ReturnType",
|
|
38
|
+
"TContext",
|
|
39
|
+
"TData",
|
|
40
|
+
"TError",
|
|
41
|
+
"TQueryKey",
|
|
37
42
|
];
|
|
38
43
|
const HTTP_METHODS = [
|
|
39
44
|
"get",
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import ts from "typescript";
|
|
2
1
|
/**
|
|
3
2
|
* Find the tsconfig that applies to the generated output by walking up from
|
|
4
3
|
* `startDir`. Anchoring the search at the caller's directory finds the
|
|
@@ -10,4 +9,4 @@ export declare function findNearestTsConfigPath(startDir: string): string | unde
|
|
|
10
9
|
* resolution and must not under bundler-style resolution. Read the caller's
|
|
11
10
|
* tsconfig to pick the matching convention.
|
|
12
11
|
*/
|
|
13
|
-
export declare function detectImportExtension(startDir: string
|
|
12
|
+
export declare function detectImportExtension(startDir: string): "" | ".js";
|
package/dist/client/tsconfig.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { readdirSync } from "node:fs";
|
|
1
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
2
3
|
import path from "node:path";
|
|
3
|
-
import ts from "typescript";
|
|
4
4
|
/**
|
|
5
5
|
* Find the tsconfig that applies to the generated output by walking up from
|
|
6
6
|
* `startDir`. Anchoring the search at the caller's directory finds the
|
|
@@ -20,37 +20,133 @@ export function findNearestTsConfigPath(startDir) {
|
|
|
20
20
|
dir = parent;
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
+
/**
|
|
24
|
+
* Parse a tsconfig, which is JSON with comments and trailing commas. Done here
|
|
25
|
+
* and not with the `typescript` package, so that the generator works with
|
|
26
|
+
* whatever TypeScript the project has: version 7 has no API to read a config.
|
|
27
|
+
*/
|
|
28
|
+
function parseJsonWithComments(text) {
|
|
29
|
+
let json = "";
|
|
30
|
+
let index = text.charCodeAt(0) === 0xfeff ? 1 : 0;
|
|
31
|
+
while (index < text.length) {
|
|
32
|
+
const char = text[index];
|
|
33
|
+
const next = text[index + 1];
|
|
34
|
+
if (char === '"') {
|
|
35
|
+
// A string, copied as it is so that nothing inside it is touched.
|
|
36
|
+
let end = index + 1;
|
|
37
|
+
while (end < text.length && text[end] !== '"') {
|
|
38
|
+
end += text[end] === "\\" ? 2 : 1;
|
|
39
|
+
}
|
|
40
|
+
json += text.slice(index, end + 1);
|
|
41
|
+
index = end + 1;
|
|
42
|
+
}
|
|
43
|
+
else if (char === "/" && next === "/") {
|
|
44
|
+
const end = text.indexOf("\n", index);
|
|
45
|
+
index = end === -1 ? text.length : end;
|
|
46
|
+
}
|
|
47
|
+
else if (char === "/" && next === "*") {
|
|
48
|
+
const end = text.indexOf("*/", index + 2);
|
|
49
|
+
index = end === -1 ? text.length : end + 2;
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
json += char;
|
|
53
|
+
index += 1;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// The comments are gone, so a comma before a closing bracket is a trailing
|
|
57
|
+
// one. Strings are skipped as a whole by the first alternative.
|
|
58
|
+
return JSON.parse(json.replace(/("(?:[^"\\]|\\.)*")|,(\s*[}\]])/g, (_match, string, close) => string === undefined ? close : string));
|
|
59
|
+
}
|
|
60
|
+
/** Where an `extends` entry points to, the way TypeScript looks it up. */
|
|
61
|
+
function resolveExtends(target, from) {
|
|
62
|
+
if (target.startsWith(".") || path.isAbsolute(target)) {
|
|
63
|
+
const file = path.resolve(path.dirname(from), target);
|
|
64
|
+
return [file, `${file}.json`].find((candidate) => candidate.endsWith(".json") && existsSync(candidate));
|
|
65
|
+
}
|
|
66
|
+
// A package: its `exports`, or else the file itself or its tsconfig.json.
|
|
67
|
+
const require = createRequire(from);
|
|
68
|
+
for (const candidate of [
|
|
69
|
+
target,
|
|
70
|
+
`${target}.json`,
|
|
71
|
+
`${target}/tsconfig.json`,
|
|
72
|
+
]) {
|
|
73
|
+
try {
|
|
74
|
+
const file = require.resolve(candidate);
|
|
75
|
+
if (file.endsWith(".json"))
|
|
76
|
+
return file;
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
// Not this spelling.
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
/** `module` and `moduleResolution` of a tsconfig, with what it extends. */
|
|
85
|
+
function readModuleOptions(configPath,
|
|
86
|
+
// The files on the way here, so that a circle of `extends` ends.
|
|
87
|
+
chain = new Set()) {
|
|
88
|
+
if (chain.has(configPath))
|
|
89
|
+
return {};
|
|
90
|
+
const config = parseJsonWithComments(readFileSync(configPath, "utf-8"));
|
|
91
|
+
if (!config || typeof config !== "object")
|
|
92
|
+
return {};
|
|
93
|
+
const options = {};
|
|
94
|
+
const apply = (source) => {
|
|
95
|
+
for (const key of ["module", "moduleResolution"]) {
|
|
96
|
+
if (source[key] !== undefined)
|
|
97
|
+
options[key] = source[key];
|
|
98
|
+
}
|
|
99
|
+
};
|
|
100
|
+
// Later entries win over earlier ones, and the file itself over all of them.
|
|
101
|
+
const parents = Array.isArray(config.extends)
|
|
102
|
+
? config.extends
|
|
103
|
+
: [config.extends];
|
|
104
|
+
for (const parent of parents) {
|
|
105
|
+
if (typeof parent !== "string")
|
|
106
|
+
continue;
|
|
107
|
+
const file = resolveExtends(parent, configPath);
|
|
108
|
+
if (file === undefined)
|
|
109
|
+
continue;
|
|
110
|
+
try {
|
|
111
|
+
apply(readModuleOptions(file, new Set(chain).add(configPath)));
|
|
112
|
+
}
|
|
113
|
+
catch {
|
|
114
|
+
// A parent that cannot be read adds nothing.
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const own = config.compilerOptions ?? {};
|
|
118
|
+
apply({
|
|
119
|
+
module: typeof own.module === "string" ? own.module.toLowerCase() : undefined,
|
|
120
|
+
moduleResolution: typeof own.moduleResolution === "string"
|
|
121
|
+
? own.moduleResolution.toLowerCase()
|
|
122
|
+
: undefined,
|
|
123
|
+
});
|
|
124
|
+
return options;
|
|
125
|
+
}
|
|
23
126
|
/**
|
|
24
127
|
* Relative imports must spell out `.js` under Node16/NodeNext module
|
|
25
128
|
* resolution and must not under bundler-style resolution. Read the caller's
|
|
26
129
|
* tsconfig to pick the matching convention.
|
|
27
130
|
*/
|
|
28
|
-
export function detectImportExtension(startDir
|
|
131
|
+
export function detectImportExtension(startDir) {
|
|
29
132
|
const configPath = findNearestTsConfigPath(startDir);
|
|
30
133
|
if (!configPath)
|
|
31
134
|
return "";
|
|
135
|
+
let options;
|
|
32
136
|
try {
|
|
33
|
-
|
|
34
|
-
if (read.error || !read.config)
|
|
35
|
-
return "";
|
|
36
|
-
const parsed = ts.parseJsonConfigFileContent(read.config, host, path.dirname(configPath));
|
|
37
|
-
const { moduleResolution, module } = parsed.options;
|
|
38
|
-
if (moduleResolution !== undefined) {
|
|
39
|
-
return moduleResolution === ts.ModuleResolutionKind.Node16 ||
|
|
40
|
-
moduleResolution === ts.ModuleResolutionKind.NodeNext
|
|
41
|
-
? ".js"
|
|
42
|
-
: "";
|
|
43
|
-
}
|
|
44
|
-
// A range, so that `node18`, `node20` and later ones count as well.
|
|
45
|
-
return module !== undefined &&
|
|
46
|
-
module >= ts.ModuleKind.Node16 &&
|
|
47
|
-
module <= ts.ModuleKind.NodeNext
|
|
48
|
-
? ".js"
|
|
49
|
-
: "";
|
|
137
|
+
options = readModuleOptions(configPath);
|
|
50
138
|
}
|
|
51
139
|
catch {
|
|
52
|
-
//
|
|
53
|
-
//
|
|
140
|
+
// A tsconfig that cannot be read: fall back to the bundler convention
|
|
141
|
+
// rather than failing the whole run.
|
|
54
142
|
return "";
|
|
55
143
|
}
|
|
144
|
+
if (options.moduleResolution !== undefined) {
|
|
145
|
+
return options.moduleResolution === "node16" ||
|
|
146
|
+
options.moduleResolution === "nodenext"
|
|
147
|
+
? ".js"
|
|
148
|
+
: "";
|
|
149
|
+
}
|
|
150
|
+
// `node16`, `node18`, `node20` and later ones resolve the Node way.
|
|
151
|
+
return /^node(1[6-9]|[2-9]\d|next)$/.test(options.module ?? "") ? ".js" : "";
|
|
56
152
|
}
|
package/dist/createSource.mjs
CHANGED
package/dist/format.d.mts
CHANGED
package/dist/format.mjs
CHANGED
|
@@ -1,24 +1,4 @@
|
|
|
1
1
|
import { sync } from "cross-spawn";
|
|
2
|
-
import { IndentationText, NewLineKind, Project, QuoteKind } from "ts-morph";
|
|
3
|
-
export const formatOutput = async (outputPath) => {
|
|
4
|
-
const project = new Project({
|
|
5
|
-
skipAddingFilesFromTsConfig: true,
|
|
6
|
-
manipulationSettings: {
|
|
7
|
-
indentationText: IndentationText.TwoSpaces,
|
|
8
|
-
newLineKind: NewLineKind.LineFeed,
|
|
9
|
-
quoteKind: QuoteKind.Double,
|
|
10
|
-
usePrefixAndSuffixTextForRename: false,
|
|
11
|
-
useTrailingCommas: true,
|
|
12
|
-
},
|
|
13
|
-
});
|
|
14
|
-
const sourceFiles = project.addSourceFilesAtPaths(`${outputPath}/**/*`);
|
|
15
|
-
const tasks = sourceFiles.map((sourceFile) => {
|
|
16
|
-
sourceFile.formatText();
|
|
17
|
-
sourceFile.organizeImports();
|
|
18
|
-
return sourceFile.save();
|
|
19
|
-
});
|
|
20
|
-
await Promise.all(tasks);
|
|
21
|
-
};
|
|
22
2
|
const formatters = {
|
|
23
3
|
biome: {
|
|
24
4
|
args: (path) => ["format", "--write", path],
|
package/dist/generate.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { detectImportExtension, findNearestTsConfigPath, generateClient, SpecErr
|
|
|
2
2
|
import { buildQueriesOutputPath, buildRequestsOutputPath, formatOptions, } from "./common.mjs";
|
|
3
3
|
import { createSource } from "./createSource.mjs";
|
|
4
4
|
import { CodegenError, ErrorCodes, toCodegenError } from "./errors.mjs";
|
|
5
|
-
import {
|
|
5
|
+
import { processOutput } from "./format.mjs";
|
|
6
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. */
|
|
@@ -84,7 +84,6 @@ async function runGenerate(options, version, runtime) {
|
|
|
84
84
|
});
|
|
85
85
|
await print(source, formattedOptions);
|
|
86
86
|
const queriesOutputPath = buildQueriesOutputPath(options.output);
|
|
87
|
-
await formatOutput(queriesOutputPath);
|
|
88
87
|
await processOutput({
|
|
89
88
|
output: queriesOutputPath,
|
|
90
89
|
format: formattedOptions.format,
|
|
@@ -1,42 +1,42 @@
|
|
|
1
|
-
import { type TypeAliasDeclarationStructure, type VariableStatementStructure } from "ts-morph";
|
|
2
1
|
import type { GenerationContext, OperationInfo } from "../types.mjs";
|
|
2
|
+
import type { ConstStatement, TypeStatement } from "./statement.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Build the default response type alias.
|
|
5
5
|
* Example: export type FindPetsDefaultResponse = Awaited<ReturnType<typeof findPets>>["data"];
|
|
6
6
|
*/
|
|
7
|
-
export declare function buildDefaultResponseType(op: OperationInfo):
|
|
7
|
+
export declare function buildDefaultResponseType(op: OperationInfo): TypeStatement;
|
|
8
8
|
/**
|
|
9
9
|
* Build the query result type alias.
|
|
10
10
|
* Example: export type FindPetsQueryResult<TData = FindPetsDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
|
|
11
11
|
*/
|
|
12
|
-
export declare function buildQueryResultType(op: OperationInfo):
|
|
12
|
+
export declare function buildQueryResultType(op: OperationInfo): TypeStatement;
|
|
13
13
|
/**
|
|
14
14
|
* Build the mutation result type alias.
|
|
15
15
|
* Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet>>;
|
|
16
16
|
*/
|
|
17
|
-
export declare function buildMutationResultType(op: OperationInfo):
|
|
17
|
+
export declare function buildMutationResultType(op: OperationInfo): TypeStatement;
|
|
18
18
|
/**
|
|
19
19
|
* Build query key constant.
|
|
20
20
|
* Example: export const useFindPetsKey = "FindPets";
|
|
21
21
|
*/
|
|
22
|
-
export declare function buildQueryKeyConst(op: OperationInfo):
|
|
22
|
+
export declare function buildQueryKeyConst(op: OperationInfo): ConstStatement;
|
|
23
23
|
/**
|
|
24
24
|
* Build mutation key constant.
|
|
25
25
|
* Example: export const useAddPetKey = "AddPet";
|
|
26
26
|
*/
|
|
27
|
-
export declare function buildMutationKeyConst(op: OperationInfo):
|
|
27
|
+
export declare function buildMutationKeyConst(op: OperationInfo): ConstStatement;
|
|
28
28
|
/**
|
|
29
29
|
* Build query key function.
|
|
30
30
|
* Example: export const UseFindPetsKeyFn = (clientOptions: Options<FindPetsData, true> = {}, queryKey?: Array<unknown>) =>
|
|
31
31
|
* [useFindPetsKey, ...(queryKey ?? [clientOptions])];
|
|
32
32
|
*/
|
|
33
|
-
export declare function buildQueryKeyFn(op: OperationInfo):
|
|
33
|
+
export declare function buildQueryKeyFn(op: OperationInfo): ConstStatement;
|
|
34
34
|
/**
|
|
35
35
|
* Build mutation key function.
|
|
36
36
|
* Example: export const UseAddPetKeyFn = (mutationKey?: Array<unknown>) =>
|
|
37
37
|
* [useAddPetKey, ...(mutationKey ?? [])];
|
|
38
38
|
*/
|
|
39
|
-
export declare function buildMutationKeyFn(op: OperationInfo):
|
|
39
|
+
export declare function buildMutationKeyFn(op: OperationInfo): ConstStatement;
|
|
40
40
|
/**
|
|
41
41
|
* Build the client options type for infinite queries.
|
|
42
42
|
* The page parameter is excluded because TanStack Query supplies it via the
|
|
@@ -45,7 +45,7 @@ export declare function buildMutationKeyFn(op: OperationInfo): VariableStatement
|
|
|
45
45
|
* export type FindPaginatedPetsInfiniteClientOptions = Omit<Options<FindPaginatedPetsData, true>, "query"> &
|
|
46
46
|
* { query?: Omit<NonNullable<FindPaginatedPetsData["query"]>, "page"> };
|
|
47
47
|
*/
|
|
48
|
-
export declare function buildInfiniteClientOptionsType(op: OperationInfo, ctx: GenerationContext):
|
|
48
|
+
export declare function buildInfiniteClientOptionsType(op: OperationInfo, ctx: GenerationContext): TypeStatement;
|
|
49
49
|
/**
|
|
50
50
|
* Build the infinite query key constant.
|
|
51
51
|
* Shares the plain query key as its first segment so a single
|
|
@@ -54,7 +54,7 @@ export declare function buildInfiniteClientOptionsType(op: OperationInfo, ctx: G
|
|
|
54
54
|
* segment keeps cached InfiniteData from colliding with plain query data (#140).
|
|
55
55
|
* Example: export const useFindPaginatedPetsInfiniteKey = [useFindPaginatedPetsKey, "infinite"] as const;
|
|
56
56
|
*/
|
|
57
|
-
export declare function buildInfiniteQueryKeyConst(op: OperationInfo):
|
|
57
|
+
export declare function buildInfiniteQueryKeyConst(op: OperationInfo): ConstStatement;
|
|
58
58
|
/**
|
|
59
59
|
* Build the infinite query key function.
|
|
60
60
|
* The custom queryKey argument only replaces the params segment — the
|
|
@@ -63,4 +63,4 @@ export declare function buildInfiniteQueryKeyConst(op: OperationInfo): VariableS
|
|
|
63
63
|
* Example: export const UseFindPaginatedPetsInfiniteKeyFn = (clientOptions: FindPaginatedPetsInfiniteClientOptions = {}, queryKey?: Array<unknown>) =>
|
|
64
64
|
* [...useFindPaginatedPetsInfiniteKey, ...(queryKey ?? [clientOptions])];
|
|
65
65
|
*/
|
|
66
|
-
export declare function buildInfiniteQueryKeyFn(op: OperationInfo):
|
|
66
|
+
export declare function buildInfiniteQueryKeyFn(op: OperationInfo): ConstStatement;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { StructureKind, VariableDeclarationKind, } from "ts-morph";
|
|
2
1
|
import { getDataTypeName } from "./operationNames.mjs";
|
|
3
2
|
/**
|
|
4
3
|
* Build the default response type alias.
|
|
@@ -6,8 +5,7 @@ import { getDataTypeName } from "./operationNames.mjs";
|
|
|
6
5
|
*/
|
|
7
6
|
export function buildDefaultResponseType(op) {
|
|
8
7
|
return {
|
|
9
|
-
kind:
|
|
10
|
-
isExported: true,
|
|
8
|
+
kind: "type",
|
|
11
9
|
name: `${op.capitalizedMethodName}DefaultResponse`,
|
|
12
10
|
type: `Awaited<ReturnType<typeof ${op.methodName}>>["data"]`,
|
|
13
11
|
};
|
|
@@ -18,8 +16,7 @@ export function buildDefaultResponseType(op) {
|
|
|
18
16
|
*/
|
|
19
17
|
export function buildQueryResultType(op) {
|
|
20
18
|
return {
|
|
21
|
-
kind:
|
|
22
|
-
isExported: true,
|
|
19
|
+
kind: "type",
|
|
23
20
|
name: `${op.capitalizedMethodName}QueryResult`,
|
|
24
21
|
typeParameters: [
|
|
25
22
|
{ name: "TData", default: `${op.capitalizedMethodName}DefaultResponse` },
|
|
@@ -34,8 +31,7 @@ export function buildQueryResultType(op) {
|
|
|
34
31
|
*/
|
|
35
32
|
export function buildMutationResultType(op) {
|
|
36
33
|
return {
|
|
37
|
-
kind:
|
|
38
|
-
isExported: true,
|
|
34
|
+
kind: "type",
|
|
39
35
|
name: `${op.capitalizedMethodName}MutationResult`,
|
|
40
36
|
type: `Awaited<ReturnType<typeof ${op.methodName}>>`,
|
|
41
37
|
};
|
|
@@ -46,15 +42,9 @@ export function buildMutationResultType(op) {
|
|
|
46
42
|
*/
|
|
47
43
|
export function buildQueryKeyConst(op) {
|
|
48
44
|
return {
|
|
49
|
-
kind:
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
declarations: [
|
|
53
|
-
{
|
|
54
|
-
name: `use${op.capitalizedMethodName}Key`,
|
|
55
|
-
initializer: `"${op.capitalizedMethodName}"`,
|
|
56
|
-
},
|
|
57
|
-
],
|
|
45
|
+
kind: "const",
|
|
46
|
+
name: `use${op.capitalizedMethodName}Key`,
|
|
47
|
+
initializer: `"${op.capitalizedMethodName}"`,
|
|
58
48
|
};
|
|
59
49
|
}
|
|
60
50
|
/**
|
|
@@ -63,15 +53,9 @@ export function buildQueryKeyConst(op) {
|
|
|
63
53
|
*/
|
|
64
54
|
export function buildMutationKeyConst(op) {
|
|
65
55
|
return {
|
|
66
|
-
kind:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
declarations: [
|
|
70
|
-
{
|
|
71
|
-
name: `use${op.capitalizedMethodName}Key`,
|
|
72
|
-
initializer: `"${op.capitalizedMethodName}"`,
|
|
73
|
-
},
|
|
74
|
-
],
|
|
56
|
+
kind: "const",
|
|
57
|
+
name: `use${op.capitalizedMethodName}Key`,
|
|
58
|
+
initializer: `"${op.capitalizedMethodName}"`,
|
|
75
59
|
};
|
|
76
60
|
}
|
|
77
61
|
/**
|
|
@@ -87,15 +71,9 @@ export function buildQueryKeyFn(op) {
|
|
|
87
71
|
params.push("queryKey?: Array<unknown>");
|
|
88
72
|
const fallbackArray = "[clientOptions]";
|
|
89
73
|
return {
|
|
90
|
-
kind:
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
declarations: [
|
|
94
|
-
{
|
|
95
|
-
name: `Use${op.capitalizedMethodName}KeyFn`,
|
|
96
|
-
initializer: `(${params.join(", ")}) => [use${op.capitalizedMethodName}Key, ...(queryKey ?? ${fallbackArray})]`,
|
|
97
|
-
},
|
|
98
|
-
],
|
|
74
|
+
kind: "const",
|
|
75
|
+
name: `Use${op.capitalizedMethodName}KeyFn`,
|
|
76
|
+
initializer: `(${params.join(", ")}) => [use${op.capitalizedMethodName}Key, ...(queryKey ?? ${fallbackArray})]`,
|
|
99
77
|
};
|
|
100
78
|
}
|
|
101
79
|
/**
|
|
@@ -105,15 +83,9 @@ export function buildQueryKeyFn(op) {
|
|
|
105
83
|
*/
|
|
106
84
|
export function buildMutationKeyFn(op) {
|
|
107
85
|
return {
|
|
108
|
-
kind:
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
declarations: [
|
|
112
|
-
{
|
|
113
|
-
name: `Use${op.capitalizedMethodName}KeyFn`,
|
|
114
|
-
initializer: `(mutationKey?: Array<unknown>) => [use${op.capitalizedMethodName}Key, ...(mutationKey ?? [])]`,
|
|
115
|
-
},
|
|
116
|
-
],
|
|
86
|
+
kind: "const",
|
|
87
|
+
name: `Use${op.capitalizedMethodName}KeyFn`,
|
|
88
|
+
initializer: `(mutationKey?: Array<unknown>) => [use${op.capitalizedMethodName}Key, ...(mutationKey ?? [])]`,
|
|
117
89
|
};
|
|
118
90
|
}
|
|
119
91
|
/**
|
|
@@ -128,8 +100,7 @@ export function buildInfiniteClientOptionsType(op, ctx) {
|
|
|
128
100
|
const dataTypeName = getDataTypeName(op);
|
|
129
101
|
const type = `Omit<Options<${dataTypeName}, true>, "query"> & { query?: Omit<NonNullable<${dataTypeName}["query"]>, "${ctx.pageParam}"> }`;
|
|
130
102
|
return {
|
|
131
|
-
kind:
|
|
132
|
-
isExported: true,
|
|
103
|
+
kind: "type",
|
|
133
104
|
name: `${op.capitalizedMethodName}InfiniteClientOptions`,
|
|
134
105
|
type,
|
|
135
106
|
};
|
|
@@ -144,15 +115,9 @@ export function buildInfiniteClientOptionsType(op, ctx) {
|
|
|
144
115
|
*/
|
|
145
116
|
export function buildInfiniteQueryKeyConst(op) {
|
|
146
117
|
return {
|
|
147
|
-
kind:
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
declarations: [
|
|
151
|
-
{
|
|
152
|
-
name: `use${op.capitalizedMethodName}InfiniteKey`,
|
|
153
|
-
initializer: `[use${op.capitalizedMethodName}Key, "infinite"] as const`,
|
|
154
|
-
},
|
|
155
|
-
],
|
|
118
|
+
kind: "const",
|
|
119
|
+
name: `use${op.capitalizedMethodName}InfiniteKey`,
|
|
120
|
+
initializer: `[use${op.capitalizedMethodName}Key, "infinite"] as const`,
|
|
156
121
|
};
|
|
157
122
|
}
|
|
158
123
|
/**
|
|
@@ -170,14 +135,8 @@ export function buildInfiniteQueryKeyFn(op) {
|
|
|
170
135
|
"queryKey?: Array<unknown>",
|
|
171
136
|
];
|
|
172
137
|
return {
|
|
173
|
-
kind:
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
declarations: [
|
|
177
|
-
{
|
|
178
|
-
name: `Use${op.capitalizedMethodName}InfiniteKeyFn`,
|
|
179
|
-
initializer: `(${params.join(", ")}) => [...use${op.capitalizedMethodName}InfiniteKey, ...(queryKey ?? [clientOptions])]`,
|
|
180
|
-
},
|
|
181
|
-
],
|
|
138
|
+
kind: "const",
|
|
139
|
+
name: `Use${op.capitalizedMethodName}InfiniteKeyFn`,
|
|
140
|
+
initializer: `(${params.join(", ")}) => [...use${op.capitalizedMethodName}InfiniteKey, ...(queryKey ?? [clientOptions])]`,
|
|
182
141
|
};
|
|
183
142
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type VariableStatementStructure } from "ts-morph";
|
|
2
1
|
import type { GenerationContext, OperationInfo } from "../types.mjs";
|
|
2
|
+
import type { ConstStatement } from "./statement.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* Build useMutation hook.
|
|
5
5
|
* Example:
|
|
@@ -12,4 +12,4 @@ import type { GenerationContext, OperationInfo } from "../types.mjs";
|
|
|
12
12
|
* ...options
|
|
13
13
|
* });
|
|
14
14
|
*/
|
|
15
|
-
export declare function buildUseMutationHook(op: OperationInfo, ctx: GenerationContext):
|
|
15
|
+
export declare function buildUseMutationHook(op: OperationInfo, ctx: GenerationContext): ConstStatement;
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { StructureKind, VariableDeclarationKind, } from "ts-morph";
|
|
2
1
|
import { SDK_CALL_ARGS } from "./buildQueryHooks.mjs";
|
|
3
2
|
import { getDataTypeName, getErrorType } from "./operationNames.mjs";
|
|
4
3
|
/**
|
|
@@ -21,16 +20,10 @@ export function buildUseMutationHook(op, ctx) {
|
|
|
21
20
|
const mutationFn = `clientOptions => ${op.methodName}(${SDK_CALL_ARGS}) as unknown as Promise<TData>`;
|
|
22
21
|
const body = `useMutation<TData, TError, ${optionsType}, TContext>({ mutationKey: Common.Use${op.capitalizedMethodName}KeyFn(mutationKey), mutationFn: ${mutationFn}, ...options })`;
|
|
23
22
|
return {
|
|
24
|
-
kind:
|
|
25
|
-
//
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
declarations: [
|
|
30
|
-
{
|
|
31
|
-
name: hookName,
|
|
32
|
-
initializer: `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[], TContext = unknown>(mutationKey?: TQueryKey, options?: Omit<UseMutationOptions<TData, TError, ${optionsType}, TContext>, "mutationKey" | "mutationFn">) => ${body}`,
|
|
33
|
-
},
|
|
34
|
-
],
|
|
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}`,
|
|
35
28
|
};
|
|
36
29
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { type VariableStatementStructure } from "ts-morph";
|
|
2
1
|
import type { GenerationContext, OperationInfo } from "../types.mjs";
|
|
2
|
+
import type { ConstStatement } from "./statement.mjs";
|
|
3
3
|
/**
|
|
4
4
|
* SDK call arguments shared by every generated queryFn/mutationFn.
|
|
5
5
|
* throwOnError: true forces the SDK call to reject on error responses; the
|
|
@@ -79,19 +79,19 @@ export declare function buildOverridableInfiniteOptionsType(optionsTypeName: str
|
|
|
79
79
|
* ...options
|
|
80
80
|
* });
|
|
81
81
|
*/
|
|
82
|
-
export declare function buildUseQueryHook(op: OperationInfo, ctx: GenerationContext):
|
|
82
|
+
export declare function buildUseQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement;
|
|
83
83
|
/**
|
|
84
84
|
* Build useSuspenseQuery hook.
|
|
85
85
|
*/
|
|
86
|
-
export declare function buildUseSuspenseQueryHook(op: OperationInfo, ctx: GenerationContext):
|
|
86
|
+
export declare function buildUseSuspenseQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement;
|
|
87
87
|
/**
|
|
88
88
|
* Build useInfiniteQuery hook.
|
|
89
89
|
*/
|
|
90
|
-
export declare function buildUseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext):
|
|
90
|
+
export declare function buildUseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement | null;
|
|
91
91
|
/**
|
|
92
92
|
* Build useSuspenseInfiniteQuery hook.
|
|
93
93
|
*/
|
|
94
|
-
export declare function buildUseSuspenseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext):
|
|
94
|
+
export declare function buildUseSuspenseInfiniteQueryHook(op: OperationInfo, ctx: GenerationContext): ConstStatement | null;
|
|
95
95
|
/**
|
|
96
96
|
* Build prefetch function.
|
|
97
97
|
* Example:
|
|
@@ -102,7 +102,7 @@ export declare function buildUseSuspenseInfiniteQueryHook(op: OperationInfo, ctx
|
|
|
102
102
|
* ...options
|
|
103
103
|
* });
|
|
104
104
|
*/
|
|
105
|
-
export declare function buildPrefetchFn(op: OperationInfo):
|
|
105
|
+
export declare function buildPrefetchFn(op: OperationInfo): ConstStatement;
|
|
106
106
|
/**
|
|
107
107
|
* Build prefetchInfiniteQuery function for a paginatable operation.
|
|
108
108
|
* Example:
|
|
@@ -115,7 +115,7 @@ export declare function buildPrefetchFn(op: OperationInfo): VariableStatementStr
|
|
|
115
115
|
* ...options
|
|
116
116
|
* });
|
|
117
117
|
*/
|
|
118
|
-
export declare function buildPrefetchInfiniteQueryFn(op: OperationInfo, ctx: GenerationContext):
|
|
118
|
+
export declare function buildPrefetchInfiniteQueryFn(op: OperationInfo, ctx: GenerationContext): ConstStatement | null;
|
|
119
119
|
/**
|
|
120
120
|
* Build ensureQueryData function.
|
|
121
121
|
* Example:
|
|
@@ -126,4 +126,4 @@ export declare function buildPrefetchInfiniteQueryFn(op: OperationInfo, ctx: Gen
|
|
|
126
126
|
* ...options
|
|
127
127
|
* });
|
|
128
128
|
*/
|
|
129
|
-
export declare function buildEnsureQueryDataFn(op: OperationInfo):
|
|
129
|
+
export declare function buildEnsureQueryDataFn(op: OperationInfo): ConstStatement;
|