@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.
- package/AGENTS.md +8 -4
- package/README.md +2 -2
- 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 +16 -48
- 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/format.d.mts +0 -1
- package/dist/format.mjs +0 -20
- package/dist/generate.mjs +2 -4
- package/dist/{tsmorph → hooks}/buildCommon.d.mts +13 -17
- package/dist/hooks/buildCommon.mjs +132 -0
- package/dist/{tsmorph → hooks}/buildMutationHooks.d.mts +4 -3
- package/dist/hooks/buildMutationHooks.mjs +24 -0
- package/dist/{tsmorph → hooks}/buildQueryHooks.d.mts +22 -16
- package/dist/{tsmorph → hooks}/buildQueryHooks.mjs +35 -106
- package/dist/{tsmorph → hooks}/buildQueryOptions.d.mts +5 -4
- package/dist/{tsmorph → hooks}/buildQueryOptions.mjs +9 -35
- package/dist/{tsmorph → hooks}/generateFiles.d.mts +1 -1
- package/dist/hooks/generateFiles.mjs +122 -0
- package/dist/hooks/hookNames.d.mts +69 -0
- package/dist/hooks/hookNames.mjs +82 -0
- package/dist/hooks/imports.d.mts +46 -0
- package/dist/hooks/imports.mjs +115 -0
- package/dist/hooks/operationNames.d.mts +15 -0
- package/dist/hooks/operationNames.mjs +31 -0
- package/dist/hooks/statement.d.mts +43 -0
- package/dist/hooks/statement.mjs +63 -0
- package/dist/types.d.mts +5 -7
- package/package.json +3 -5
- package/dist/print.d.mts +0 -5
- package/dist/print.mjs +0 -22
- package/dist/tsmorph/buildCommon.mjs +0 -183
- package/dist/tsmorph/buildMutationHooks.mjs +0 -36
- package/dist/tsmorph/generateFiles.mjs +0 -153
- package/dist/tsmorph/index.d.mts +0 -5
- package/dist/tsmorph/index.mjs +0 -5
- package/dist/tsmorph/operationNames.d.mts +0 -5
- package/dist/tsmorph/operationNames.mjs +0 -9
- package/dist/tsmorph/projectFactory.d.mts +0 -58
- package/dist/tsmorph/projectFactory.mjs +0 -169
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { OpenApiRqFiles } from "../constants.mjs";
|
|
2
|
+
import { buildDefaultResponseType, buildInfiniteClientOptionsType, buildInfiniteQueryKeyConst, buildInfiniteQueryKeyFn, buildKeyConst, buildMutationKeyFn, buildMutationResultType, buildQueryKeyFn, buildQueryResultType, } from "./buildCommon.mjs";
|
|
3
|
+
import { buildUseMutationHook } from "./buildMutationHooks.mjs";
|
|
4
|
+
import { buildEnsureQueryDataFn, buildPrefetchFn, buildPrefetchInfiniteQueryFn, buildUseInfiniteQueryHook, buildUseQueryHook, buildUseSuspenseInfiniteQueryHook, buildUseSuspenseQueryHook, } from "./buildQueryHooks.mjs";
|
|
5
|
+
import { buildInfiniteQueryOptionsFn, buildQueryOptionsFn, } from "./buildQueryOptions.mjs";
|
|
6
|
+
import { settleHookNames, } from "./hookNames.mjs";
|
|
7
|
+
import { buildFileImports, printImports, relativeModule } from "./imports.mjs";
|
|
8
|
+
import { printStatements, referencedNames, statementCode, } from "./statement.mjs";
|
|
9
|
+
/** The distinct values of a list, in order of first appearance. */
|
|
10
|
+
function unique(values) {
|
|
11
|
+
return [...new Set(values)];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The content of a generated file: the imports its statements need, then the
|
|
15
|
+
* statements.
|
|
16
|
+
*/
|
|
17
|
+
function createFile(ctx, statements) {
|
|
18
|
+
if (statements.length === 0) {
|
|
19
|
+
// Nothing to export. The bare import keeps the file a module.
|
|
20
|
+
return `import "${relativeModule("../requests/sdk.gen", ctx)}";\n`;
|
|
21
|
+
}
|
|
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")),
|
|
26
|
+
});
|
|
27
|
+
return `${printImports(imports)}\n${printStatements(statements)}`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* The content of index.ts.
|
|
31
|
+
*/
|
|
32
|
+
function indexFile(ctx) {
|
|
33
|
+
return [
|
|
34
|
+
OpenApiRqFiles.common,
|
|
35
|
+
OpenApiRqFiles.queries,
|
|
36
|
+
OpenApiRqFiles.queryOptions,
|
|
37
|
+
]
|
|
38
|
+
.map((name) => `export * from "${relativeModule(`./${name}`, ctx)}";\n`)
|
|
39
|
+
.join("");
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
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.
|
|
44
|
+
*/
|
|
45
|
+
function buildFiles(operations, ctx) {
|
|
46
|
+
const queries = operations.filter((op) => op.httpMethod === "GET");
|
|
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`.
|
|
49
|
+
const paginated = queries.filter((op) => op.isPaginatable);
|
|
50
|
+
const each = (ops, ...builders) => ops.flatMap((op) => builders.map((build) => ({ op, statement: build(op, ctx) })));
|
|
51
|
+
return [
|
|
52
|
+
[
|
|
53
|
+
OpenApiRqFiles.common,
|
|
54
|
+
[
|
|
55
|
+
...each(queries, buildDefaultResponseType, buildQueryResultType, buildKeyConst, buildQueryKeyFn),
|
|
56
|
+
...each(paginated, buildInfiniteClientOptionsType, buildInfiniteQueryKeyConst, buildInfiniteQueryKeyFn),
|
|
57
|
+
...each(mutations, buildMutationResultType, buildKeyConst, buildMutationKeyFn),
|
|
58
|
+
],
|
|
59
|
+
],
|
|
60
|
+
[
|
|
61
|
+
OpenApiRqFiles.queries,
|
|
62
|
+
[
|
|
63
|
+
...each(queries, buildUseQueryHook),
|
|
64
|
+
...each(mutations, buildUseMutationHook),
|
|
65
|
+
],
|
|
66
|
+
],
|
|
67
|
+
[
|
|
68
|
+
OpenApiRqFiles.queryOptions,
|
|
69
|
+
[
|
|
70
|
+
...each(queries, buildQueryOptionsFn),
|
|
71
|
+
...each(paginated, buildInfiniteQueryOptionsFn),
|
|
72
|
+
],
|
|
73
|
+
],
|
|
74
|
+
[
|
|
75
|
+
OpenApiRqFiles.suspense,
|
|
76
|
+
[
|
|
77
|
+
...each(queries, buildUseSuspenseQueryHook),
|
|
78
|
+
...each(paginated, buildUseSuspenseInfiniteQueryHook),
|
|
79
|
+
],
|
|
80
|
+
],
|
|
81
|
+
[
|
|
82
|
+
OpenApiRqFiles.infiniteQueries,
|
|
83
|
+
each(paginated, buildUseInfiniteQueryHook),
|
|
84
|
+
],
|
|
85
|
+
[
|
|
86
|
+
OpenApiRqFiles.prefetch,
|
|
87
|
+
[
|
|
88
|
+
...each(queries, buildPrefetchFn),
|
|
89
|
+
...each(paginated, buildPrefetchInfiniteQueryFn),
|
|
90
|
+
],
|
|
91
|
+
],
|
|
92
|
+
[OpenApiRqFiles.ensureQueryData, each(queries, buildEnsureQueryDataFn)],
|
|
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
|
+
}
|
|
112
|
+
return [
|
|
113
|
+
{
|
|
114
|
+
name: `${OpenApiRqFiles.index}.ts`,
|
|
115
|
+
content: addHeaderComment(indexFile(ctx), ctx.version),
|
|
116
|
+
},
|
|
117
|
+
...files.map(([name, declared]) => ({
|
|
118
|
+
name: `${name}.ts`,
|
|
119
|
+
content: addHeaderComment(createFile(ctx, declared.map(({ statement }) => statement)), ctx.version),
|
|
120
|
+
})),
|
|
121
|
+
];
|
|
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
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { GenerationContext } from "../types.mjs";
|
|
2
|
+
type NamedImport = {
|
|
3
|
+
name: string;
|
|
4
|
+
isTypeOnly?: boolean;
|
|
5
|
+
};
|
|
6
|
+
/** One import declaration of a generated file. */
|
|
7
|
+
export type ImportDeclaration = {
|
|
8
|
+
moduleSpecifier: string;
|
|
9
|
+
/** `import type { … }` */
|
|
10
|
+
isTypeOnly?: boolean;
|
|
11
|
+
/** `import * as Name` */
|
|
12
|
+
namespaceImport?: string;
|
|
13
|
+
namedImports?: NamedImport[];
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* A relative module specifier, with the extension the consuming project's
|
|
17
|
+
* module resolution asks for.
|
|
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[];
|
|
22
|
+
/**
|
|
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.
|
|
25
|
+
*/
|
|
26
|
+
export declare const FIXED_IMPORT_NAMES: readonly string[];
|
|
27
|
+
/**
|
|
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.
|
|
34
|
+
*/
|
|
35
|
+
export declare function buildFileImports(ctx: Pick<GenerationContext, "importExtension">, { services, models, mentioned, }: {
|
|
36
|
+
services: string[];
|
|
37
|
+
models: string[];
|
|
38
|
+
mentioned: ReadonlySet<string>;
|
|
39
|
+
}): ImportDeclaration[];
|
|
40
|
+
/**
|
|
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.
|
|
44
|
+
*/
|
|
45
|
+
export declare function printImports(declarations: ImportDeclaration[]): string;
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A relative module specifier, with the extension the consuming project's
|
|
3
|
+
* module resolution asks for.
|
|
4
|
+
*/
|
|
5
|
+
export function relativeModule(path, ctx) {
|
|
6
|
+
return `${path}${ctx?.importExtension ?? ""}`;
|
|
7
|
+
}
|
|
8
|
+
/** What the builders use from TanStack Query. */
|
|
9
|
+
export const TANSTACK_IMPORTS = [
|
|
10
|
+
{ name: "useQuery" },
|
|
11
|
+
{ name: "useSuspenseQuery" },
|
|
12
|
+
{ name: "useInfiniteQuery" },
|
|
13
|
+
{ name: "useSuspenseInfiniteQuery" },
|
|
14
|
+
{ name: "useMutation" },
|
|
15
|
+
{ name: "queryOptions" },
|
|
16
|
+
{ name: "infiniteQueryOptions" },
|
|
17
|
+
{ name: "QueryClient", isTypeOnly: true },
|
|
18
|
+
{ name: "UseQueryResult", isTypeOnly: true },
|
|
19
|
+
{ name: "UseQueryOptions", isTypeOnly: true },
|
|
20
|
+
{ name: "UseInfiniteQueryOptions", isTypeOnly: true },
|
|
21
|
+
{ name: "UseSuspenseInfiniteQueryOptions", isTypeOnly: true },
|
|
22
|
+
{ name: "UseMutationOptions", isTypeOnly: true },
|
|
23
|
+
{ name: "UseSuspenseQueryOptions", isTypeOnly: true },
|
|
24
|
+
{ name: "InfiniteData", isTypeOnly: true },
|
|
25
|
+
{ name: "FetchQueryOptions", isTypeOnly: true },
|
|
26
|
+
{ name: "FetchInfiniteQueryOptions", isTypeOnly: true },
|
|
27
|
+
{ name: "GetNextPageParamFunction", isTypeOnly: true },
|
|
28
|
+
{ name: "EnsureQueryDataOptions", isTypeOnly: true },
|
|
29
|
+
];
|
|
30
|
+
/**
|
|
31
|
+
* Every fixed name a generated file can import. A name the hooks derive from
|
|
32
|
+
* an operation gives way to these, as the names of the client do.
|
|
33
|
+
*/
|
|
34
|
+
export const FIXED_IMPORT_NAMES = [
|
|
35
|
+
...TANSTACK_IMPORTS.map(({ name }) => name),
|
|
36
|
+
"AxiosError",
|
|
37
|
+
"Options",
|
|
38
|
+
"Common",
|
|
39
|
+
];
|
|
40
|
+
/**
|
|
41
|
+
* The imports of a generated file.
|
|
42
|
+
*
|
|
43
|
+
* `services` and `models` are what the file's statements declare. Everything
|
|
44
|
+
* else is a fixed name the builders write themselves, kept when `mentioned`
|
|
45
|
+
* holds it: the client generator reserves these names, so no model, service or
|
|
46
|
+
* local of the generated code can be called the same.
|
|
47
|
+
*/
|
|
48
|
+
export function buildFileImports(ctx, { services, models, mentioned, }) {
|
|
49
|
+
const isMentioned = ({ name }) => mentioned.has(name);
|
|
50
|
+
const declarations = [
|
|
51
|
+
{
|
|
52
|
+
moduleSpecifier: "@tanstack/react-query",
|
|
53
|
+
namedImports: TANSTACK_IMPORTS.filter(isMentioned),
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
moduleSpecifier: "axios",
|
|
57
|
+
namedImports: [{ name: "AxiosError" }].filter(isMentioned),
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
// `Options` is the one sdk.gen re-exports: it adds `client` and `meta`
|
|
61
|
+
// to the Options of the runtime, and the hooks accept both.
|
|
62
|
+
moduleSpecifier: relativeModule("../requests/sdk.gen", ctx),
|
|
63
|
+
namedImports: [
|
|
64
|
+
...[{ name: "Options", isTypeOnly: true }].filter(isMentioned),
|
|
65
|
+
...services.map((name) => ({ name })),
|
|
66
|
+
],
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
// Type-only, so the output compiles under `verbatimModuleSyntax`.
|
|
70
|
+
moduleSpecifier: relativeModule("../requests/types.gen", ctx),
|
|
71
|
+
isTypeOnly: true,
|
|
72
|
+
namedImports: models.map((name) => ({ name })),
|
|
73
|
+
},
|
|
74
|
+
];
|
|
75
|
+
if (mentioned.has("Common")) {
|
|
76
|
+
declarations.push({
|
|
77
|
+
moduleSpecifier: relativeModule("./common", ctx),
|
|
78
|
+
namespaceImport: "Common",
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
return declarations;
|
|
82
|
+
}
|
|
83
|
+
function compareCaseInsensitive(a, b) {
|
|
84
|
+
const lowerA = a.toLowerCase();
|
|
85
|
+
const lowerB = b.toLowerCase();
|
|
86
|
+
return lowerA < lowerB ? -1 : lowerA > lowerB ? 1 : 0;
|
|
87
|
+
}
|
|
88
|
+
function isRelative(moduleSpecifier) {
|
|
89
|
+
return moduleSpecifier.startsWith(".");
|
|
90
|
+
}
|
|
91
|
+
function printImport(declaration) {
|
|
92
|
+
const from = `from "${declaration.moduleSpecifier}";\n`;
|
|
93
|
+
if (declaration.namespaceImport) {
|
|
94
|
+
return `import * as ${declaration.namespaceImport} ${from}`;
|
|
95
|
+
}
|
|
96
|
+
const names = (declaration.namedImports ?? [])
|
|
97
|
+
.toSorted((a, b) => compareCaseInsensitive(a.name, b.name))
|
|
98
|
+
.map(({ name, isTypeOnly }) => (isTypeOnly ? `type ${name}` : name))
|
|
99
|
+
.join(", ");
|
|
100
|
+
return `import ${declaration.isTypeOnly ? "type " : ""}{ ${names} } ${from}`;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Print an import block: packages before relative modules, and both the
|
|
104
|
+
* modules and the names within a declaration in alphabetical order regardless
|
|
105
|
+
* of case. A declaration without names is left out.
|
|
106
|
+
*/
|
|
107
|
+
export function printImports(declarations) {
|
|
108
|
+
return declarations
|
|
109
|
+
.filter((declaration) => declaration.namespaceImport || declaration.namedImports?.length)
|
|
110
|
+
.toSorted((a, b) => Number(isRelative(a.moduleSpecifier)) -
|
|
111
|
+
Number(isRelative(b.moduleSpecifier)) ||
|
|
112
|
+
compareCaseInsensitive(a.moduleSpecifier, b.moduleSpecifier))
|
|
113
|
+
.map(printImport)
|
|
114
|
+
.join("");
|
|
115
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { GenerationContext, OperationInfo } from "../types.mjs";
|
|
2
|
+
import type { ConstStatement } from "./statement.mjs";
|
|
3
|
+
/** The generated Data type name, or unknown when there is none. */
|
|
4
|
+
export declare function getDataTypeName(op: OperationInfo): string;
|
|
5
|
+
/** The error type of an operation's hooks, wrapped for axios. */
|
|
6
|
+
export declare function getErrorType(op: OperationInfo, ctx: GenerationContext): string;
|
|
7
|
+
/** The model `getDataTypeName` names, for a statement's `models`. */
|
|
8
|
+
export declare function dataModels(op: OperationInfo): string[];
|
|
9
|
+
/** The model `getErrorType` names, for a statement's `models`. */
|
|
10
|
+
export declare function errorModels(op: OperationInfo): string[];
|
|
11
|
+
/**
|
|
12
|
+
* A `const` that calls the operation's SDK function. It carries the JSDoc
|
|
13
|
+
* (description and `@deprecated`) the SDK function has.
|
|
14
|
+
*/
|
|
15
|
+
export declare function operationConst(op: OperationInfo, name: string, initializer: string, models: string[]): ConstStatement;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** The generated Data type name, or unknown when there is none. */
|
|
2
|
+
export function getDataTypeName(op) {
|
|
3
|
+
return op.dataTypeName ?? "unknown";
|
|
4
|
+
}
|
|
5
|
+
/** The error type of an operation's hooks, wrapped for axios. */
|
|
6
|
+
export function getErrorType(op, ctx) {
|
|
7
|
+
const errorType = op.errorTypeName ?? "unknown";
|
|
8
|
+
return ctx.client === "axios" ? `AxiosError<${errorType}>` : errorType;
|
|
9
|
+
}
|
|
10
|
+
/** The model `getDataTypeName` names, for a statement's `models`. */
|
|
11
|
+
export function dataModels(op) {
|
|
12
|
+
return op.dataTypeName ? [op.dataTypeName] : [];
|
|
13
|
+
}
|
|
14
|
+
/** The model `getErrorType` names, for a statement's `models`. */
|
|
15
|
+
export function errorModels(op) {
|
|
16
|
+
return op.errorTypeName ? [op.errorTypeName] : [];
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* A `const` that calls the operation's SDK function. It carries the JSDoc
|
|
20
|
+
* (description and `@deprecated`) the SDK function has.
|
|
21
|
+
*/
|
|
22
|
+
export function operationConst(op, name, initializer, models) {
|
|
23
|
+
return {
|
|
24
|
+
kind: "const",
|
|
25
|
+
jsDoc: op.jsDoc,
|
|
26
|
+
name,
|
|
27
|
+
initializer,
|
|
28
|
+
service: op.methodName,
|
|
29
|
+
models,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/** What a statement's code needs imported from the generated client. */
|
|
2
|
+
type References = {
|
|
3
|
+
/** The SDK function the code calls or takes the type of. */
|
|
4
|
+
service?: string;
|
|
5
|
+
/** The types from `types.gen` the code names. */
|
|
6
|
+
models?: string[];
|
|
7
|
+
};
|
|
8
|
+
/** An exported `const` of a generated file. */
|
|
9
|
+
export type ConstStatement = References & {
|
|
10
|
+
kind: "const";
|
|
11
|
+
name: string;
|
|
12
|
+
/** The expression after `=`. */
|
|
13
|
+
initializer: string;
|
|
14
|
+
/** A complete JSDoc comment to put above the declaration. */
|
|
15
|
+
jsDoc?: string;
|
|
16
|
+
};
|
|
17
|
+
/** An exported type alias of a generated file. */
|
|
18
|
+
export type TypeStatement = References & {
|
|
19
|
+
kind: "type";
|
|
20
|
+
name: string;
|
|
21
|
+
typeParameters?: Array<{
|
|
22
|
+
name: string;
|
|
23
|
+
default: string;
|
|
24
|
+
}>;
|
|
25
|
+
type: string;
|
|
26
|
+
};
|
|
27
|
+
export type Statement = ConstStatement | TypeStatement;
|
|
28
|
+
/** The declaration itself, without its JSDoc. */
|
|
29
|
+
export declare function statementCode(statement: Statement): string;
|
|
30
|
+
/**
|
|
31
|
+
* One statement per line, with a blank line wherever a `const` follows a type
|
|
32
|
+
* alias or the other way round.
|
|
33
|
+
*/
|
|
34
|
+
export declare function printStatements(statements: Statement[]): string;
|
|
35
|
+
/**
|
|
36
|
+
* The identifiers a piece of generated code mentions. Some places hold a name
|
|
37
|
+
* without referring to anything a file could import, and are left out: a
|
|
38
|
+
* comment, a string literal, the name after the dot of a member access, and
|
|
39
|
+
* the key of a property or the name of a parameter (`{ page: 1 }`,
|
|
40
|
+
* `(options?: T)`).
|
|
41
|
+
*/
|
|
42
|
+
export declare function referencedNames(code: string): Set<string>;
|
|
43
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/** The declaration itself, without its JSDoc. */
|
|
2
|
+
export function statementCode(statement) {
|
|
3
|
+
if (statement.kind === "const") {
|
|
4
|
+
return `export const ${statement.name} = ${statement.initializer};`;
|
|
5
|
+
}
|
|
6
|
+
const typeParameters = statement.typeParameters?.length
|
|
7
|
+
? `<${statement.typeParameters
|
|
8
|
+
.map((parameter) => `${parameter.name} = ${parameter.default}`)
|
|
9
|
+
.join(", ")}>`
|
|
10
|
+
: "";
|
|
11
|
+
return `export type ${statement.name}${typeParameters} = ${statement.type};`;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* One statement per line, with a blank line wherever a `const` follows a type
|
|
15
|
+
* alias or the other way round.
|
|
16
|
+
*/
|
|
17
|
+
export function printStatements(statements) {
|
|
18
|
+
let text = "";
|
|
19
|
+
statements.forEach((statement, index) => {
|
|
20
|
+
if (index > 0 && statements[index - 1].kind !== statement.kind) {
|
|
21
|
+
text += "\n";
|
|
22
|
+
}
|
|
23
|
+
if (statement.kind === "const" && statement.jsDoc) {
|
|
24
|
+
text += `${statement.jsDoc}\n`;
|
|
25
|
+
}
|
|
26
|
+
text += `${statementCode(statement)}\n`;
|
|
27
|
+
});
|
|
28
|
+
return text;
|
|
29
|
+
}
|
|
30
|
+
// A comment, a string literal, a spread, a member access, an identifier (the
|
|
31
|
+
// one group), or any other character. The last only matters as what precedes
|
|
32
|
+
// an identifier.
|
|
33
|
+
const TOKEN = /\/\*[\s\S]*?\*\/|\/\/[^\n]*|"(?:[^"\\\n]|\\.)*"|'(?:[^'\\\n]|\\.)*'|`(?:[^`\\]|\\.)*`|\.\.\.|\.\s*[\p{ID_Start}_$][\p{ID_Continue}$]*|([\p{ID_Start}_$][\p{ID_Continue}$]*)|\S/gu;
|
|
34
|
+
/** What precedes the key of a property or the name of a parameter. */
|
|
35
|
+
const KEY_START = new Set(["{", ",", "(", ";"]);
|
|
36
|
+
/** What follows one. */
|
|
37
|
+
const KEY_END = /\s*\??:/y;
|
|
38
|
+
/**
|
|
39
|
+
* The identifiers a piece of generated code mentions. Some places hold a name
|
|
40
|
+
* without referring to anything a file could import, and are left out: a
|
|
41
|
+
* comment, a string literal, the name after the dot of a member access, and
|
|
42
|
+
* the key of a property or the name of a parameter (`{ page: 1 }`,
|
|
43
|
+
* `(options?: T)`).
|
|
44
|
+
*/
|
|
45
|
+
export function referencedNames(code) {
|
|
46
|
+
const names = new Set();
|
|
47
|
+
let previous = "";
|
|
48
|
+
for (const match of code.matchAll(TOKEN)) {
|
|
49
|
+
const [token, identifier] = match;
|
|
50
|
+
if (token.startsWith("/*") || token.startsWith("//")) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (identifier !== undefined) {
|
|
54
|
+
KEY_END.lastIndex = match.index + token.length;
|
|
55
|
+
const isKey = KEY_START.has(previous) && KEY_END.test(code);
|
|
56
|
+
if (!isKey) {
|
|
57
|
+
names.add(identifier);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
previous = token;
|
|
61
|
+
}
|
|
62
|
+
return names;
|
|
63
|
+
}
|
package/dist/types.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* What the hook generators know about an operation. The client generator
|
|
3
|
+
* (`src/client/`) fills it in; it is plain JSON-serializable data.
|
|
4
4
|
*/
|
|
5
5
|
export interface OperationInfo {
|
|
6
|
-
/**
|
|
6
|
+
/** Name of the operation's function in `sdk.gen.ts` (e.g., "findPets") */
|
|
7
7
|
methodName: string;
|
|
8
8
|
/** Capitalized method name (e.g., "FindPets") */
|
|
9
9
|
capitalizedMethodName: string;
|
|
@@ -26,6 +26,8 @@ export interface OperationInfo {
|
|
|
26
26
|
isPaginatable: boolean;
|
|
27
27
|
/** Type of the configured page query parameter */
|
|
28
28
|
pageParamType?: string;
|
|
29
|
+
/** Models `pageParamType` names, which the hooks have to import */
|
|
30
|
+
pageParamModels?: string[];
|
|
29
31
|
/** Resolved primitive kind of the configured page query parameter */
|
|
30
32
|
pageParamTypeKind?: PageParamTypeKind;
|
|
31
33
|
}
|
|
@@ -37,10 +39,6 @@ export type PageParamTypeKind = "string" | "number" | "other";
|
|
|
37
39
|
export interface GenerationContext {
|
|
38
40
|
/** Client type: "fetch" or "axios" */
|
|
39
41
|
client: "fetch" | "axios";
|
|
40
|
-
/** Model type names exported from the models file */
|
|
41
|
-
modelNames: string[];
|
|
42
|
-
/** Service function names exported from the service file */
|
|
43
|
-
serviceNames: string[];
|
|
44
42
|
/** Page param name for infinite queries (e.g., "page") */
|
|
45
43
|
pageParam: string;
|
|
46
44
|
/** Next page param name for infinite queries (e.g., "nextPage") */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@7nohe/openapi-react-query-codegen",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.5",
|
|
4
4
|
"description": "OpenAPI React Query Codegen",
|
|
5
5
|
"bin": {
|
|
6
6
|
"openapi-rq": "dist/cli.mjs"
|
|
@@ -51,18 +51,16 @@
|
|
|
51
51
|
"axios": "1.20.0",
|
|
52
52
|
"commander": "^15.0.0",
|
|
53
53
|
"lefthook": "^2.1.10",
|
|
54
|
-
"ts-morph": "^28.0.0",
|
|
55
54
|
"typescript": "^6.0.3",
|
|
56
55
|
"vite": "^7.3.7",
|
|
57
56
|
"vitest": "^4.1.12"
|
|
58
57
|
},
|
|
59
58
|
"peerDependencies": {
|
|
60
59
|
"@tanstack/react-query": "^5.0.0",
|
|
61
|
-
"commander": "12.x || 13.x || 14.x || 15.x"
|
|
62
|
-
"ts-morph": "28.x"
|
|
60
|
+
"commander": "12.x || 13.x || 14.x || 15.x"
|
|
63
61
|
},
|
|
64
62
|
"engines": {
|
|
65
|
-
"node": ">=
|
|
63
|
+
"node": ">=24.0.0",
|
|
66
64
|
"pnpm": ">=9"
|
|
67
65
|
},
|
|
68
66
|
"scripts": {
|