@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.
- package/AGENTS.md +23 -1
- package/README.md +5 -2
- package/dist/agentsMd.d.mts +30 -0
- package/dist/agentsMd.mjs +115 -0
- package/dist/check.mjs +8 -4
- package/dist/cli.d.mts +2 -0
- package/dist/client/emitSchemas.mjs +12 -9
- package/dist/client/emitSdk.d.mts +2 -0
- package/dist/client/emitSdk.mjs +24 -27
- package/dist/client/emitTransformers.mjs +21 -72
- package/dist/client/emitTypes.d.mts +2 -1
- package/dist/client/emitTypes.mjs +5 -5
- package/dist/client/index.mjs +13 -5
- package/dist/client/naming.d.mts +5 -2
- package/dist/client/naming.mjs +7 -3
- package/dist/client/operations.d.mts +3 -2
- package/dist/client/operations.mjs +10 -47
- package/dist/client/runtime/axios.runtime.ts +15 -10
- package/dist/client/schema.d.mts +24 -2
- package/dist/client/schema.mjs +135 -33
- package/dist/client/spec.d.mts +6 -0
- package/dist/client/spec.mjs +15 -5
- package/dist/common.d.mts +0 -2
- package/dist/common.mjs +0 -10
- package/dist/createSource.d.mts +1 -1
- package/dist/createSource.mjs +1 -3
- package/dist/generate.mjs +9 -2
- package/dist/hooks/buildCommon.d.mts +12 -16
- package/dist/hooks/buildCommon.mjs +25 -35
- package/dist/hooks/buildMutationHooks.d.mts +3 -2
- package/dist/hooks/buildMutationHooks.mjs +6 -11
- package/dist/hooks/buildQueryHooks.d.mts +21 -15
- package/dist/hooks/buildQueryHooks.mjs +35 -69
- package/dist/hooks/buildQueryOptions.d.mts +4 -3
- package/dist/hooks/buildQueryOptions.mjs +9 -22
- package/dist/hooks/generateFiles.mjs +54 -81
- package/dist/hooks/hookNames.d.mts +69 -0
- package/dist/hooks/hookNames.mjs +82 -0
- package/dist/hooks/imports.d.mts +27 -52
- package/dist/hooks/imports.mjs +82 -164
- package/dist/hooks/operationNames.d.mts +10 -0
- package/dist/hooks/operationNames.mjs +22 -0
- package/dist/hooks/statement.d.mts +15 -6
- package/dist/hooks/statement.mjs +19 -14
- package/dist/runCli.mjs +1 -0
- package/dist/types.d.mts +5 -7
- package/package.json +1 -1
- package/dist/hooks/index.d.mts +0 -4
- package/dist/hooks/index.mjs +0 -4
- package/dist/print.d.mts +0 -5
- package/dist/print.mjs +0 -22
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
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";
|
|
2
4
|
/**
|
|
3
5
|
* Build a queryOptions factory for a GET operation.
|
|
4
6
|
* The factory centralizes queryKey and queryFn so they can be reused with
|
|
@@ -12,17 +14,11 @@ import { buildClientOptionsParam, buildGetNextPageParamExpr, buildInfiniteClient
|
|
|
12
14
|
* });
|
|
13
15
|
*/
|
|
14
16
|
export function buildQueryOptionsFn(op) {
|
|
15
|
-
const fnName =
|
|
17
|
+
const fnName = hookNames(op).options;
|
|
16
18
|
const clientOptionsParam = buildClientOptionsParam(op);
|
|
17
19
|
const queryFn = `({ signal }) => ${op.methodName}(${QUERY_SDK_CALL_ARGS}).then(response => response.data)`;
|
|
18
|
-
const body = `queryOptions({ queryKey: Common
|
|
19
|
-
return {
|
|
20
|
-
kind: "const",
|
|
21
|
-
// The operation's JSDoc (description and @deprecated), as on the SDK function
|
|
22
|
-
jsDoc: op.jsDoc,
|
|
23
|
-
name: fnName,
|
|
24
|
-
initializer: `(${clientOptionsParam}, queryKey?: Array<unknown>) => ${body}`,
|
|
25
|
-
};
|
|
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));
|
|
26
22
|
}
|
|
27
23
|
/**
|
|
28
24
|
* Build an infiniteQueryOptions factory for a paginatable GET operation.
|
|
@@ -38,22 +34,13 @@ export function buildQueryOptionsFn(op) {
|
|
|
38
34
|
* });
|
|
39
35
|
*/
|
|
40
36
|
export function buildInfiniteQueryOptionsFn(op, ctx) {
|
|
41
|
-
|
|
42
|
-
return null;
|
|
43
|
-
}
|
|
44
|
-
const fnName = `${op.methodName}InfiniteOptions`;
|
|
37
|
+
const fnName = hookNames(op).infiniteOptions;
|
|
45
38
|
const queryFn = buildPagedQueryFn(op, ctx);
|
|
46
39
|
const infiniteOptions = `initialPageParam: ${formatInitialPageParam(ctx, op)}, getNextPageParam: ${buildGetNextPageParamExpr(ctx, op)}`;
|
|
47
40
|
// Only the pagination fields are overridable here: the factory's return type
|
|
48
41
|
// is what every downstream consumer infers from, and a wider options type
|
|
49
42
|
// (select, placeholderData, ...) would make that inference ambiguous (#203).
|
|
50
43
|
const optionsParam = `options?: Partial<Pick<UseInfiniteQueryOptions<${getPageType(op)}>, "initialPageParam" | "getNextPageParam">>`;
|
|
51
|
-
const body = `infiniteQueryOptions({ queryKey: Common
|
|
52
|
-
return {
|
|
53
|
-
kind: "const",
|
|
54
|
-
// The operation's JSDoc (description and @deprecated), as on the SDK function
|
|
55
|
-
jsDoc: op.jsDoc,
|
|
56
|
-
name: fnName,
|
|
57
|
-
initializer: `(${buildInfiniteClientOptionsParam(op)}, queryKey?: Array<unknown>, ${optionsParam}) => ${body}`,
|
|
58
|
-
};
|
|
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));
|
|
59
46
|
}
|
|
@@ -1,43 +1,30 @@
|
|
|
1
1
|
import { OpenApiRqFiles } from "../constants.mjs";
|
|
2
|
-
import { buildDefaultResponseType, buildInfiniteClientOptionsType, buildInfiniteQueryKeyConst, buildInfiniteQueryKeyFn,
|
|
2
|
+
import { buildDefaultResponseType, buildInfiniteClientOptionsType, buildInfiniteQueryKeyConst, buildInfiniteQueryKeyFn, buildKeyConst, buildMutationKeyFn, buildMutationResultType, buildQueryKeyFn, buildQueryResultType, } from "./buildCommon.mjs";
|
|
3
3
|
import { buildUseMutationHook } from "./buildMutationHooks.mjs";
|
|
4
4
|
import { buildEnsureQueryDataFn, buildPrefetchFn, buildPrefetchInfiniteQueryFn, buildUseInfiniteQueryHook, buildUseQueryHook, buildUseSuspenseInfiniteQueryHook, buildUseSuspenseQueryHook, } from "./buildQueryHooks.mjs";
|
|
5
5
|
import { buildInfiniteQueryOptionsFn, buildQueryOptionsFn, } from "./buildQueryOptions.mjs";
|
|
6
|
-
import {
|
|
6
|
+
import { settleHookNames, } from "./hookNames.mjs";
|
|
7
|
+
import { buildFileImports, printImports, relativeModule } from "./imports.mjs";
|
|
7
8
|
import { printStatements, referencedNames, statementCode, } from "./statement.mjs";
|
|
8
|
-
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* every infinite-query builder returns null for non-paginatable operations,
|
|
12
|
-
* which is how the paginatable subset gets selected.
|
|
13
|
-
*/
|
|
14
|
-
function collectStatements(operations, build) {
|
|
15
|
-
const statements = [];
|
|
16
|
-
for (const op of operations) {
|
|
17
|
-
const statement = build(op);
|
|
18
|
-
if (statement) {
|
|
19
|
-
statements.push(statement);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
return statements;
|
|
9
|
+
/** The distinct values of a list, in order of first appearance. */
|
|
10
|
+
function unique(values) {
|
|
11
|
+
return [...new Set(values)];
|
|
23
12
|
}
|
|
24
13
|
/**
|
|
25
14
|
* The content of a generated file: the imports its statements need, then the
|
|
26
|
-
* statements.
|
|
27
|
-
* models its code mentions are imported.
|
|
15
|
+
* statements.
|
|
28
16
|
*/
|
|
29
|
-
function createFile(ctx,
|
|
17
|
+
function createFile(ctx, statements) {
|
|
30
18
|
if (statements.length === 0) {
|
|
31
19
|
// Nothing to export. The bare import keeps the file a module.
|
|
32
20
|
return `import "${relativeModule("../requests/sdk.gen", ctx)}";\n`;
|
|
33
21
|
}
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
serviceNames: operations.map((op) => op.methodName),
|
|
22
|
+
const imports = buildFileImports(ctx, {
|
|
23
|
+
services: unique(statements.flatMap((statement) => statement.service ?? [])),
|
|
24
|
+
models: unique(statements.flatMap((statement) => statement.models ?? [])),
|
|
25
|
+
mentioned: referencedNames(statements.map(statementCode).join("\n")),
|
|
39
26
|
});
|
|
40
|
-
return `${printImports(imports
|
|
27
|
+
return `${printImports(imports)}\n${printStatements(statements)}`;
|
|
41
28
|
}
|
|
42
29
|
/**
|
|
43
30
|
* The content of index.ts.
|
|
@@ -51,99 +38,85 @@ function indexFile(ctx) {
|
|
|
51
38
|
.map((name) => `export * from "${relativeModule(`./${name}`, ctx)}";\n`)
|
|
52
39
|
.join("");
|
|
53
40
|
}
|
|
54
|
-
function commonStatements(queries, mutations, ctx) {
|
|
55
|
-
const statements = [];
|
|
56
|
-
for (const op of queries) {
|
|
57
|
-
statements.push(buildDefaultResponseType(op), buildQueryResultType(op), buildQueryKeyConst(op), buildQueryKeyFn(op));
|
|
58
|
-
}
|
|
59
|
-
// Dedicated infinite query types and keys for paginatable operations
|
|
60
|
-
for (const op of queries.filter((query) => query.isPaginatable)) {
|
|
61
|
-
statements.push(buildInfiniteClientOptionsType(op, ctx), buildInfiniteQueryKeyConst(op), buildInfiniteQueryKeyFn(op));
|
|
62
|
-
}
|
|
63
|
-
for (const op of mutations) {
|
|
64
|
-
statements.push(buildMutationResultType(op), buildMutationKeyConst(op), buildMutationKeyFn(op));
|
|
65
|
-
}
|
|
66
|
-
return statements;
|
|
67
|
-
}
|
|
68
41
|
/**
|
|
69
|
-
*
|
|
42
|
+
* The statements of every file but `index.ts`, in the order they are printed:
|
|
43
|
+
* for each operation of a kind, what each builder makes of it.
|
|
70
44
|
*/
|
|
71
|
-
function
|
|
72
|
-
const comment = `// generated with @7nohe/openapi-react-query-codegen@${version} \n\n`;
|
|
73
|
-
return comment + content;
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Generate the React Query files for the operations of a specification.
|
|
77
|
-
*/
|
|
78
|
-
export function generateAllFiles(operations, ctx) {
|
|
45
|
+
function buildFiles(operations, ctx) {
|
|
79
46
|
const queries = operations.filter((op) => op.httpMethod === "GET");
|
|
80
47
|
const mutations = operations.filter((op) => ["POST", "PUT", "PATCH", "DELETE"].includes(op.httpMethod));
|
|
48
|
+
// The infinite variants: their own types and keys in `common.ts`.
|
|
81
49
|
const paginated = queries.filter((op) => op.isPaginatable);
|
|
82
|
-
const
|
|
83
|
-
|
|
50
|
+
const each = (ops, ...builders) => ops.flatMap((op) => builders.map((build) => ({ op, statement: build(op, ctx) })));
|
|
51
|
+
return [
|
|
84
52
|
[
|
|
85
53
|
OpenApiRqFiles.common,
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
54
|
+
[
|
|
55
|
+
...each(queries, buildDefaultResponseType, buildQueryResultType, buildKeyConst, buildQueryKeyFn),
|
|
56
|
+
...each(paginated, buildInfiniteClientOptionsType, buildInfiniteQueryKeyConst, buildInfiniteQueryKeyFn),
|
|
57
|
+
...each(mutations, buildMutationResultType, buildKeyConst, buildMutationKeyFn),
|
|
58
|
+
],
|
|
89
59
|
],
|
|
90
60
|
[
|
|
91
61
|
OpenApiRqFiles.queries,
|
|
92
|
-
buildHookFileImports,
|
|
93
|
-
all,
|
|
94
62
|
[
|
|
95
|
-
...queries
|
|
96
|
-
...mutations
|
|
63
|
+
...each(queries, buildUseQueryHook),
|
|
64
|
+
...each(mutations, buildUseMutationHook),
|
|
97
65
|
],
|
|
98
66
|
],
|
|
99
67
|
[
|
|
100
68
|
OpenApiRqFiles.queryOptions,
|
|
101
|
-
buildQueryOptionsFileImports,
|
|
102
|
-
queries,
|
|
103
69
|
[
|
|
104
|
-
...queries
|
|
105
|
-
...
|
|
70
|
+
...each(queries, buildQueryOptionsFn),
|
|
71
|
+
...each(paginated, buildInfiniteQueryOptionsFn),
|
|
106
72
|
],
|
|
107
73
|
],
|
|
108
74
|
[
|
|
109
75
|
OpenApiRqFiles.suspense,
|
|
110
|
-
buildHookFileImports,
|
|
111
|
-
queries,
|
|
112
76
|
[
|
|
113
|
-
...queries
|
|
114
|
-
...
|
|
77
|
+
...each(queries, buildUseSuspenseQueryHook),
|
|
78
|
+
...each(paginated, buildUseSuspenseInfiniteQueryHook),
|
|
115
79
|
],
|
|
116
80
|
],
|
|
117
81
|
[
|
|
118
82
|
OpenApiRqFiles.infiniteQueries,
|
|
119
|
-
|
|
120
|
-
paginated,
|
|
121
|
-
collectStatements(queries, (op) => buildUseInfiniteQueryHook(op, ctx)),
|
|
83
|
+
each(paginated, buildUseInfiniteQueryHook),
|
|
122
84
|
],
|
|
123
85
|
[
|
|
124
86
|
OpenApiRqFiles.prefetch,
|
|
125
|
-
buildHookFileImports,
|
|
126
|
-
queries,
|
|
127
87
|
[
|
|
128
|
-
...queries
|
|
129
|
-
...
|
|
88
|
+
...each(queries, buildPrefetchFn),
|
|
89
|
+
...each(paginated, buildPrefetchInfiniteQueryFn),
|
|
130
90
|
],
|
|
131
91
|
],
|
|
132
|
-
[
|
|
133
|
-
OpenApiRqFiles.ensureQueryData,
|
|
134
|
-
buildHookFileImports,
|
|
135
|
-
queries,
|
|
136
|
-
queries.map((op) => buildEnsureQueryDataFn(op)),
|
|
137
|
-
],
|
|
92
|
+
[OpenApiRqFiles.ensureQueryData, each(queries, buildEnsureQueryDataFn)],
|
|
138
93
|
];
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Add the generated header comment to file content.
|
|
97
|
+
*/
|
|
98
|
+
function addHeaderComment(content, version) {
|
|
99
|
+
const comment = `// generated with @7nohe/openapi-react-query-codegen@${version} \n\n`;
|
|
100
|
+
return comment + content;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Generate the React Query files for the operations of a specification.
|
|
104
|
+
*/
|
|
105
|
+
export function generateAllFiles(operations, ctx) {
|
|
106
|
+
let files = buildFiles(operations, ctx);
|
|
107
|
+
// Built again only when a name the hooks derive is already taken.
|
|
108
|
+
const names = settleHookNames(files);
|
|
109
|
+
if (names) {
|
|
110
|
+
files = buildFiles(operations.map((op) => ({ ...op, names: names.get(op) })), ctx);
|
|
111
|
+
}
|
|
139
112
|
return [
|
|
140
113
|
{
|
|
141
114
|
name: `${OpenApiRqFiles.index}.ts`,
|
|
142
115
|
content: addHeaderComment(indexFile(ctx), ctx.version),
|
|
143
116
|
},
|
|
144
|
-
...files.map(([name,
|
|
117
|
+
...files.map(([name, declared]) => ({
|
|
145
118
|
name: `${name}.ts`,
|
|
146
|
-
content: addHeaderComment(createFile(ctx,
|
|
119
|
+
content: addHeaderComment(createFile(ctx, declared.map(({ statement }) => statement)), ctx.version),
|
|
147
120
|
})),
|
|
148
121
|
];
|
|
149
122
|
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import type { OperationInfo } from "../types.mjs";
|
|
2
|
+
import type { Statement } from "./statement.mjs";
|
|
3
|
+
/** What the hook files declare for one operation, each under its name. */
|
|
4
|
+
export interface HookNames {
|
|
5
|
+
/** `FindPetsDefaultResponse` */
|
|
6
|
+
defaultResponse: string;
|
|
7
|
+
/** `FindPetsQueryResult` */
|
|
8
|
+
queryResult: string;
|
|
9
|
+
/** `AddPetMutationResult` */
|
|
10
|
+
mutationResult: string;
|
|
11
|
+
/** `useFindPetsKey` */
|
|
12
|
+
key: string;
|
|
13
|
+
/** `UseFindPetsKeyFn` */
|
|
14
|
+
keyFn: string;
|
|
15
|
+
/** `FindPetsInfiniteClientOptions` */
|
|
16
|
+
infiniteClientOptions: string;
|
|
17
|
+
/** `useFindPetsInfiniteKey` */
|
|
18
|
+
infiniteKey: string;
|
|
19
|
+
/** `UseFindPetsInfiniteKeyFn` */
|
|
20
|
+
infiniteKeyFn: string;
|
|
21
|
+
/** `useFindPets`, for a query and for a mutation */
|
|
22
|
+
hook: string;
|
|
23
|
+
/** `useFindPetsSuspense` */
|
|
24
|
+
suspenseHook: string;
|
|
25
|
+
/** `useFindPetsInfinite` */
|
|
26
|
+
infiniteHook: string;
|
|
27
|
+
/** `useFindPetsSuspenseInfinite` */
|
|
28
|
+
suspenseInfiniteHook: string;
|
|
29
|
+
/** `findPetsOptions` */
|
|
30
|
+
options: string;
|
|
31
|
+
/** `findPetsInfiniteOptions` */
|
|
32
|
+
infiniteOptions: string;
|
|
33
|
+
/** `prefetchUseFindPets` */
|
|
34
|
+
prefetch: string;
|
|
35
|
+
/** `prefetchUseFindPetsInfinite` */
|
|
36
|
+
prefetchInfinite: string;
|
|
37
|
+
/** `ensureUseFindPetsData` */
|
|
38
|
+
ensureData: string;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* An operation as the builders take it. `names` is set only when one of the
|
|
42
|
+
* names had to give way to something else in its file.
|
|
43
|
+
*/
|
|
44
|
+
export type HookOperation = OperationInfo & {
|
|
45
|
+
names?: HookNames;
|
|
46
|
+
};
|
|
47
|
+
/** The names the hook files declare for an operation. */
|
|
48
|
+
export declare function hookNames(op: HookOperation): HookNames;
|
|
49
|
+
/** A statement of a generated file, and the operation it was built for. */
|
|
50
|
+
export type Declared = {
|
|
51
|
+
op: HookOperation;
|
|
52
|
+
statement: Statement;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Find the names that cannot stay as they are spelled, given the files built
|
|
56
|
+
* with those names, and give each a number.
|
|
57
|
+
*
|
|
58
|
+
* A name has to differ from what its file imports (an SDK function, a model, a
|
|
59
|
+
* fixed name such as `useQuery`) and from the other names declared in the
|
|
60
|
+
* file, or in any file `index.ts` re-exports next to it. The specification's
|
|
61
|
+
* names always stay: `getUserOptions` is the operation of that name, and the
|
|
62
|
+
* options of `getUser` become `getUserOptions2`. Between two derived names the
|
|
63
|
+
* later one gives way, and `common.ts` comes last, so that a hook keeps its
|
|
64
|
+
* name before a key does (`useGetApiKey` is the hook of `getApiKey`, not the
|
|
65
|
+
* key of `getApi`).
|
|
66
|
+
*
|
|
67
|
+
* Returns nothing when every name can stay, which is the usual case.
|
|
68
|
+
*/
|
|
69
|
+
export declare function settleHookNames(files: ReadonlyArray<readonly [name: string, declared: Declared[]]>): Map<HookOperation, HookNames> | undefined;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { uniqueName } from "../client/naming.mjs";
|
|
2
|
+
import { OpenApiRqFiles } from "../constants.mjs";
|
|
3
|
+
import { FIXED_IMPORT_NAMES } from "./imports.mjs";
|
|
4
|
+
/** The names as they are spelled from the name of the operation. */
|
|
5
|
+
function defaultHookNames(op) {
|
|
6
|
+
const name = op.methodName;
|
|
7
|
+
const Name = op.capitalizedMethodName;
|
|
8
|
+
return {
|
|
9
|
+
defaultResponse: `${Name}DefaultResponse`,
|
|
10
|
+
queryResult: `${Name}QueryResult`,
|
|
11
|
+
mutationResult: `${Name}MutationResult`,
|
|
12
|
+
key: `use${Name}Key`,
|
|
13
|
+
keyFn: `Use${Name}KeyFn`,
|
|
14
|
+
infiniteClientOptions: `${Name}InfiniteClientOptions`,
|
|
15
|
+
infiniteKey: `use${Name}InfiniteKey`,
|
|
16
|
+
infiniteKeyFn: `Use${Name}InfiniteKeyFn`,
|
|
17
|
+
hook: `use${Name}`,
|
|
18
|
+
suspenseHook: `use${Name}Suspense`,
|
|
19
|
+
infiniteHook: `use${Name}Infinite`,
|
|
20
|
+
suspenseInfiniteHook: `use${Name}SuspenseInfinite`,
|
|
21
|
+
options: `${name}Options`,
|
|
22
|
+
infiniteOptions: `${name}InfiniteOptions`,
|
|
23
|
+
prefetch: `prefetchUse${Name}`,
|
|
24
|
+
prefetchInfinite: `prefetchUse${Name}Infinite`,
|
|
25
|
+
ensureData: `ensureUse${Name}Data`,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
/** The names the hook files declare for an operation. */
|
|
29
|
+
export function hookNames(op) {
|
|
30
|
+
return op.names ?? defaultHookNames(op);
|
|
31
|
+
}
|
|
32
|
+
/** The files `index.ts` re-exports, which therefore share their names. */
|
|
33
|
+
const INDEX_FILES = [
|
|
34
|
+
OpenApiRqFiles.common,
|
|
35
|
+
OpenApiRqFiles.queries,
|
|
36
|
+
OpenApiRqFiles.queryOptions,
|
|
37
|
+
];
|
|
38
|
+
/**
|
|
39
|
+
* Find the names that cannot stay as they are spelled, given the files built
|
|
40
|
+
* with those names, and give each a number.
|
|
41
|
+
*
|
|
42
|
+
* A name has to differ from what its file imports (an SDK function, a model, a
|
|
43
|
+
* fixed name such as `useQuery`) and from the other names declared in the
|
|
44
|
+
* file, or in any file `index.ts` re-exports next to it. The specification's
|
|
45
|
+
* names always stay: `getUserOptions` is the operation of that name, and the
|
|
46
|
+
* options of `getUser` become `getUserOptions2`. Between two derived names the
|
|
47
|
+
* later one gives way, and `common.ts` comes last, so that a hook keeps its
|
|
48
|
+
* name before a key does (`useGetApiKey` is the hook of `getApiKey`, not the
|
|
49
|
+
* key of `getApi`).
|
|
50
|
+
*
|
|
51
|
+
* Returns nothing when every name can stay, which is the usual case.
|
|
52
|
+
*/
|
|
53
|
+
export function settleHookNames(files) {
|
|
54
|
+
const settled = new Map();
|
|
55
|
+
const exported = new Set();
|
|
56
|
+
const inOrder = [
|
|
57
|
+
...files.filter(([name]) => name !== OpenApiRqFiles.common),
|
|
58
|
+
...files.filter(([name]) => name === OpenApiRqFiles.common),
|
|
59
|
+
];
|
|
60
|
+
for (const [file, declared] of inOrder) {
|
|
61
|
+
const imported = new Set(FIXED_IMPORT_NAMES);
|
|
62
|
+
for (const { statement } of declared) {
|
|
63
|
+
if (statement.service)
|
|
64
|
+
imported.add(statement.service);
|
|
65
|
+
for (const model of statement.models ?? [])
|
|
66
|
+
imported.add(model);
|
|
67
|
+
}
|
|
68
|
+
const local = INDEX_FILES.includes(file) ? exported : new Set();
|
|
69
|
+
for (const { op, statement } of declared) {
|
|
70
|
+
const name = uniqueName(statement.name, local, imported);
|
|
71
|
+
if (name === statement.name)
|
|
72
|
+
continue;
|
|
73
|
+
const names = settled.get(op) ?? { ...hookNames(op) };
|
|
74
|
+
for (const key of Object.keys(names)) {
|
|
75
|
+
if (names[key] === statement.name)
|
|
76
|
+
names[key] = name;
|
|
77
|
+
}
|
|
78
|
+
settled.set(op, names);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return settled.size > 0 ? settled : undefined;
|
|
82
|
+
}
|
package/dist/hooks/imports.d.mts
CHANGED
|
@@ -1,71 +1,46 @@
|
|
|
1
1
|
import type { GenerationContext } from "../types.mjs";
|
|
2
|
-
|
|
2
|
+
type NamedImport = {
|
|
3
|
+
name: string;
|
|
4
|
+
isTypeOnly?: boolean;
|
|
5
|
+
};
|
|
6
|
+
/** One import declaration of a generated file. */
|
|
3
7
|
export type ImportDeclaration = {
|
|
4
8
|
moduleSpecifier: string;
|
|
5
9
|
/** `import type { … }` */
|
|
6
10
|
isTypeOnly?: boolean;
|
|
7
11
|
/** `import * as Name` */
|
|
8
12
|
namespaceImport?: string;
|
|
9
|
-
namedImports?:
|
|
10
|
-
name: string;
|
|
11
|
-
isTypeOnly?: boolean;
|
|
12
|
-
}>;
|
|
13
|
+
namedImports?: NamedImport[];
|
|
13
14
|
};
|
|
14
15
|
/**
|
|
15
16
|
* A relative module specifier, with the extension the consuming project's
|
|
16
17
|
* module resolution asks for.
|
|
17
18
|
*/
|
|
18
19
|
export declare function relativeModule(path: string, ctx?: Pick<GenerationContext, "importExtension">): string;
|
|
20
|
+
/** What the builders use from TanStack Query. */
|
|
21
|
+
export declare const TANSTACK_IMPORTS: NamedImport[];
|
|
19
22
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* base client Options lacks; hooks must accept those properties.
|
|
23
|
-
*/
|
|
24
|
-
export declare function buildClientImport(ctx: GenerationContext): ImportDeclaration;
|
|
25
|
-
/**
|
|
26
|
-
* Build import structure for TanStack Query.
|
|
27
|
-
*/
|
|
28
|
-
export declare function buildQueryImport(): ImportDeclaration;
|
|
29
|
-
/**
|
|
30
|
-
* Build import structure for the queryOptions/infiniteQueryOptions helpers.
|
|
31
|
-
*/
|
|
32
|
-
export declare function buildQueryOptionsImport(): ImportDeclaration;
|
|
33
|
-
/**
|
|
34
|
-
* Build import structure for services.
|
|
35
|
-
*/
|
|
36
|
-
export declare function buildServiceImport(ctx: GenerationContext): ImportDeclaration;
|
|
37
|
-
/**
|
|
38
|
-
* Build import structure for models.
|
|
39
|
-
* Emitted as a type-only import so generated code compiles under
|
|
40
|
-
* `verbatimModuleSyntax` (enabled by default in recent Vite templates).
|
|
41
|
-
*/
|
|
42
|
-
export declare function buildModelImport(ctx: GenerationContext): ImportDeclaration | null;
|
|
43
|
-
/**
|
|
44
|
-
* Build import structure for axios error type.
|
|
45
|
-
*/
|
|
46
|
-
export declare function buildAxiosErrorImport(): ImportDeclaration;
|
|
47
|
-
/**
|
|
48
|
-
* Build import for Common namespace.
|
|
49
|
-
*/
|
|
50
|
-
export declare function buildCommonImport(ctx?: Pick<GenerationContext, "importExtension">): ImportDeclaration;
|
|
51
|
-
/**
|
|
52
|
-
* Build all imports needed for the common file.
|
|
53
|
-
*/
|
|
54
|
-
export declare function buildCommonFileImports(ctx: GenerationContext): ImportDeclaration[];
|
|
55
|
-
/**
|
|
56
|
-
* Build all imports needed for hook files (queries, suspense, infinite).
|
|
23
|
+
* Every fixed name a generated file can import. A name the hooks derive from
|
|
24
|
+
* an operation gives way to these, as the names of the client do.
|
|
57
25
|
*/
|
|
58
|
-
export declare
|
|
26
|
+
export declare const FIXED_IMPORT_NAMES: readonly string[];
|
|
59
27
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
28
|
+
* The imports of a generated file.
|
|
29
|
+
*
|
|
30
|
+
* `services` and `models` are what the file's statements declare. Everything
|
|
31
|
+
* else is a fixed name the builders write themselves, kept when `mentioned`
|
|
32
|
+
* holds it: the client generator reserves these names, so no model, service or
|
|
33
|
+
* local of the generated code can be called the same.
|
|
63
34
|
*/
|
|
64
|
-
export declare function
|
|
35
|
+
export declare function buildFileImports(ctx: Pick<GenerationContext, "importExtension">, { services, models, mentioned, }: {
|
|
36
|
+
services: string[];
|
|
37
|
+
models: string[];
|
|
38
|
+
mentioned: ReadonlySet<string>;
|
|
39
|
+
}): ImportDeclaration[];
|
|
65
40
|
/**
|
|
66
|
-
* Print
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
* regardless of case. A declaration left without names is dropped.
|
|
41
|
+
* Print an import block: packages before relative modules, and both the
|
|
42
|
+
* modules and the names within a declaration in alphabetical order regardless
|
|
43
|
+
* of case. A declaration without names is left out.
|
|
70
44
|
*/
|
|
71
|
-
export declare function printImports(declarations: ImportDeclaration[]
|
|
45
|
+
export declare function printImports(declarations: ImportDeclaration[]): string;
|
|
46
|
+
export {};
|