@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
package/dist/hooks/imports.mjs
CHANGED
|
@@ -5,141 +5,80 @@
|
|
|
5
5
|
export function relativeModule(path, ctx) {
|
|
6
6
|
return `${path}${ctx?.importExtension ?? ""}`;
|
|
7
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
|
+
];
|
|
8
30
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* base client Options lacks; hooks must accept those properties.
|
|
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.
|
|
12
33
|
*/
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Build import structure for TanStack Query.
|
|
21
|
-
*/
|
|
22
|
-
export function buildQueryImport() {
|
|
23
|
-
return {
|
|
24
|
-
moduleSpecifier: "@tanstack/react-query",
|
|
25
|
-
namedImports: [
|
|
26
|
-
{ name: "QueryClient", isTypeOnly: true },
|
|
27
|
-
{ name: "useQuery" },
|
|
28
|
-
{ name: "useSuspenseQuery" },
|
|
29
|
-
{ name: "useInfiniteQuery" },
|
|
30
|
-
{ name: "useSuspenseInfiniteQuery" },
|
|
31
|
-
{ name: "useMutation" },
|
|
32
|
-
{ name: "UseQueryResult", isTypeOnly: true },
|
|
33
|
-
{ name: "UseQueryOptions", isTypeOnly: true },
|
|
34
|
-
{ name: "UseInfiniteQueryOptions", isTypeOnly: true },
|
|
35
|
-
{ name: "UseSuspenseInfiniteQueryOptions", isTypeOnly: true },
|
|
36
|
-
{ name: "UseMutationOptions", isTypeOnly: true },
|
|
37
|
-
{ name: "UseMutationResult", isTypeOnly: true },
|
|
38
|
-
{ name: "UseSuspenseQueryOptions", isTypeOnly: true },
|
|
39
|
-
{ name: "InfiniteData", isTypeOnly: true },
|
|
40
|
-
{ name: "FetchQueryOptions", isTypeOnly: true },
|
|
41
|
-
{ name: "FetchInfiniteQueryOptions", isTypeOnly: true },
|
|
42
|
-
{ name: "GetNextPageParamFunction", isTypeOnly: true },
|
|
43
|
-
{ name: "EnsureQueryDataOptions", isTypeOnly: true },
|
|
44
|
-
],
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Build import structure for the queryOptions/infiniteQueryOptions helpers.
|
|
49
|
-
*/
|
|
50
|
-
export function buildQueryOptionsImport() {
|
|
51
|
-
return {
|
|
52
|
-
moduleSpecifier: "@tanstack/react-query",
|
|
53
|
-
namedImports: [
|
|
54
|
-
{ name: "queryOptions" },
|
|
55
|
-
{ name: "infiniteQueryOptions" },
|
|
56
|
-
{ name: "UseInfiniteQueryOptions", isTypeOnly: true },
|
|
57
|
-
],
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Build import structure for services.
|
|
62
|
-
*/
|
|
63
|
-
export function buildServiceImport(ctx) {
|
|
64
|
-
return {
|
|
65
|
-
moduleSpecifier: relativeModule("../requests/sdk.gen", ctx),
|
|
66
|
-
namedImports: ctx.serviceNames.map((name) => ({ name })),
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
/**
|
|
70
|
-
* Build import structure for models.
|
|
71
|
-
* Emitted as a type-only import so generated code compiles under
|
|
72
|
-
* `verbatimModuleSyntax` (enabled by default in recent Vite templates).
|
|
73
|
-
*/
|
|
74
|
-
export function buildModelImport(ctx) {
|
|
75
|
-
if (ctx.modelNames.length === 0) {
|
|
76
|
-
return null;
|
|
77
|
-
}
|
|
78
|
-
return {
|
|
79
|
-
moduleSpecifier: relativeModule("../requests/types.gen", ctx),
|
|
80
|
-
isTypeOnly: true,
|
|
81
|
-
namedImports: ctx.modelNames.map((name) => ({ name })),
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
/**
|
|
85
|
-
* Build import structure for axios error type.
|
|
86
|
-
*/
|
|
87
|
-
export function buildAxiosErrorImport() {
|
|
88
|
-
return {
|
|
89
|
-
moduleSpecifier: "axios",
|
|
90
|
-
namedImports: [{ name: "AxiosError" }],
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
/**
|
|
94
|
-
* Build import for Common namespace.
|
|
95
|
-
*/
|
|
96
|
-
export function buildCommonImport(ctx) {
|
|
97
|
-
return {
|
|
98
|
-
moduleSpecifier: relativeModule("./common", ctx),
|
|
99
|
-
namespaceImport: "Common",
|
|
100
|
-
};
|
|
101
|
-
}
|
|
34
|
+
export const FIXED_IMPORT_NAMES = [
|
|
35
|
+
...TANSTACK_IMPORTS.map(({ name }) => name),
|
|
36
|
+
"AxiosError",
|
|
37
|
+
"Options",
|
|
38
|
+
"Common",
|
|
39
|
+
];
|
|
102
40
|
/**
|
|
103
|
-
*
|
|
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.
|
|
104
47
|
*/
|
|
105
|
-
export function
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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
|
+
},
|
|
110
74
|
];
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
imports.push(buildAxiosErrorImport());
|
|
75
|
+
if (mentioned.has("Common")) {
|
|
76
|
+
declarations.push({
|
|
77
|
+
moduleSpecifier: relativeModule("./common", ctx),
|
|
78
|
+
namespaceImport: "Common",
|
|
79
|
+
});
|
|
117
80
|
}
|
|
118
|
-
return
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Build all imports needed for hook files (queries, suspense, infinite).
|
|
122
|
-
*/
|
|
123
|
-
export function buildHookFileImports(ctx) {
|
|
124
|
-
return [buildCommonImport(ctx), ...buildCommonFileImports(ctx)];
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Build all imports needed for the queryOptions file.
|
|
128
|
-
* Narrower than the hook file imports: queryOptions only needs the
|
|
129
|
-
* queryOptions/infiniteQueryOptions helpers, not the TanStack hooks.
|
|
130
|
-
*/
|
|
131
|
-
export function buildQueryOptionsFileImports(ctx) {
|
|
132
|
-
const imports = [
|
|
133
|
-
buildCommonImport(ctx),
|
|
134
|
-
buildQueryOptionsImport(),
|
|
135
|
-
buildClientImport(ctx),
|
|
136
|
-
buildServiceImport(ctx),
|
|
137
|
-
];
|
|
138
|
-
const modelImport = buildModelImport(ctx);
|
|
139
|
-
if (modelImport) {
|
|
140
|
-
imports.push(modelImport);
|
|
141
|
-
}
|
|
142
|
-
return imports;
|
|
81
|
+
return declarations;
|
|
143
82
|
}
|
|
144
83
|
function compareCaseInsensitive(a, b) {
|
|
145
84
|
const lowerA = a.toLowerCase();
|
|
@@ -149,49 +88,28 @@ function compareCaseInsensitive(a, b) {
|
|
|
149
88
|
function isRelative(moduleSpecifier) {
|
|
150
89
|
return moduleSpecifier.startsWith(".");
|
|
151
90
|
}
|
|
152
|
-
function printImport(declaration
|
|
91
|
+
function printImport(declaration) {
|
|
153
92
|
const from = `from "${declaration.moduleSpecifier}";\n`;
|
|
154
93
|
if (declaration.namespaceImport) {
|
|
155
94
|
return `import * as ${declaration.namespaceImport} ${from}`;
|
|
156
95
|
}
|
|
157
|
-
const
|
|
158
|
-
.
|
|
96
|
+
const names = (declaration.namedImports ?? [])
|
|
97
|
+
.toSorted((a, b) => compareCaseInsensitive(a.name, b.name))
|
|
159
98
|
.map(({ name, isTypeOnly }) => (isTypeOnly ? `type ${name}` : name))
|
|
160
99
|
.join(", ");
|
|
161
|
-
return `import ${declaration.isTypeOnly ? "type " : ""}{ ${
|
|
100
|
+
return `import ${declaration.isTypeOnly ? "type " : ""}{ ${names} } ${from}`;
|
|
162
101
|
}
|
|
163
102
|
/**
|
|
164
|
-
* Print
|
|
165
|
-
*
|
|
166
|
-
*
|
|
167
|
-
* regardless of case. A declaration left without names is dropped.
|
|
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.
|
|
168
106
|
*/
|
|
169
|
-
export function printImports(declarations
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
].join("|");
|
|
177
|
-
let target = merged.get(key);
|
|
178
|
-
if (!target) {
|
|
179
|
-
target = { declaration, names: new Map() };
|
|
180
|
-
merged.set(key, target);
|
|
181
|
-
}
|
|
182
|
-
for (const named of declaration.namedImports ?? []) {
|
|
183
|
-
if (used.has(named.name)) {
|
|
184
|
-
target.names.set(named.name, named);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return [...merged.values()]
|
|
189
|
-
.filter(({ declaration, names }) => declaration.namespaceImport
|
|
190
|
-
? used.has(declaration.namespaceImport)
|
|
191
|
-
: names.size > 0)
|
|
192
|
-
.sort((a, b) => Number(isRelative(a.declaration.moduleSpecifier)) -
|
|
193
|
-
Number(isRelative(b.declaration.moduleSpecifier)) ||
|
|
194
|
-
compareCaseInsensitive(a.declaration.moduleSpecifier, b.declaration.moduleSpecifier))
|
|
195
|
-
.map(({ declaration, names }) => printImport(declaration, [...names.values()]))
|
|
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)
|
|
196
114
|
.join("");
|
|
197
115
|
}
|
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
import type { GenerationContext, OperationInfo } from "../types.mjs";
|
|
2
|
+
import type { ConstStatement } from "./statement.mjs";
|
|
2
3
|
/** The generated Data type name, or unknown when there is none. */
|
|
3
4
|
export declare function getDataTypeName(op: OperationInfo): string;
|
|
4
5
|
/** The error type of an operation's hooks, wrapped for axios. */
|
|
5
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;
|
|
@@ -7,3 +7,25 @@ export function getErrorType(op, ctx) {
|
|
|
7
7
|
const errorType = op.errorTypeName ?? "unknown";
|
|
8
8
|
return ctx.client === "axios" ? `AxiosError<${errorType}>` : errorType;
|
|
9
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
|
+
}
|
|
@@ -1,5 +1,12 @@
|
|
|
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
|
+
};
|
|
1
8
|
/** An exported `const` of a generated file. */
|
|
2
|
-
export type ConstStatement = {
|
|
9
|
+
export type ConstStatement = References & {
|
|
3
10
|
kind: "const";
|
|
4
11
|
name: string;
|
|
5
12
|
/** The expression after `=`. */
|
|
@@ -8,7 +15,7 @@ export type ConstStatement = {
|
|
|
8
15
|
jsDoc?: string;
|
|
9
16
|
};
|
|
10
17
|
/** An exported type alias of a generated file. */
|
|
11
|
-
export type TypeStatement = {
|
|
18
|
+
export type TypeStatement = References & {
|
|
12
19
|
kind: "type";
|
|
13
20
|
name: string;
|
|
14
21
|
typeParameters?: Array<{
|
|
@@ -26,9 +33,11 @@ export declare function statementCode(statement: Statement): string;
|
|
|
26
33
|
*/
|
|
27
34
|
export declare function printStatements(statements: Statement[]): string;
|
|
28
35
|
/**
|
|
29
|
-
* The identifiers a piece of generated code
|
|
30
|
-
*
|
|
31
|
-
* a string literal, the name after the dot of a member access, and
|
|
32
|
-
* a property or the name of a parameter (`{ page: 1 }`,
|
|
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)`).
|
|
33
41
|
*/
|
|
34
42
|
export declare function referencedNames(code: string): Set<string>;
|
|
43
|
+
export {};
|
package/dist/hooks/statement.mjs
CHANGED
|
@@ -27,29 +27,34 @@ export function printStatements(statements) {
|
|
|
27
27
|
});
|
|
28
28
|
return text;
|
|
29
29
|
}
|
|
30
|
-
// A string literal, a spread, a member access, an identifier
|
|
31
|
-
// character. The last
|
|
32
|
-
|
|
33
|
-
const
|
|
34
|
-
/** What
|
|
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. */
|
|
35
37
|
const KEY_END = /\s*\??:/y;
|
|
36
38
|
/**
|
|
37
|
-
* The identifiers a piece of generated code
|
|
38
|
-
*
|
|
39
|
-
* a string literal, the name after the dot of a member access, and
|
|
40
|
-
* a property or the name of a parameter (`{ page: 1 }`,
|
|
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)`).
|
|
41
44
|
*/
|
|
42
45
|
export function referencedNames(code) {
|
|
43
46
|
const names = new Set();
|
|
44
47
|
let previous = "";
|
|
45
48
|
for (const match of code.matchAll(TOKEN)) {
|
|
46
|
-
const [token] = match;
|
|
47
|
-
if (
|
|
49
|
+
const [token, identifier] = match;
|
|
50
|
+
if (token.startsWith("/*") || token.startsWith("//")) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (identifier !== undefined) {
|
|
48
54
|
KEY_END.lastIndex = match.index + token.length;
|
|
49
|
-
const isKey = (previous
|
|
50
|
-
KEY_END.test(code);
|
|
55
|
+
const isKey = KEY_START.has(previous) && KEY_END.test(code);
|
|
51
56
|
if (!isKey) {
|
|
52
|
-
names.add(
|
|
57
|
+
names.add(identifier);
|
|
53
58
|
}
|
|
54
59
|
}
|
|
55
60
|
previous = token;
|
package/dist/runCli.mjs
CHANGED
|
@@ -45,6 +45,7 @@ export function buildProgram(version, io, { json }) {
|
|
|
45
45
|
.option("--nextPageParam <value>", "Name of the response parameter used for next page", "nextPage")
|
|
46
46
|
.option("--initialPageParam <value>", "Initial page value to query", "1")
|
|
47
47
|
.option("--omitInitialPageParam", "Send no initial page parameter at all (overrides --initialPageParam)")
|
|
48
|
+
.option("--agentsMd [file]", "Also keep a short guide for coding agents (how to look operations up in the output) in AGENTS.md, or in the file named")
|
|
48
49
|
.option("--check", "Do not update the output; exit 1 and list the files that differ if the existing output is out of date")
|
|
49
50
|
.option("--json", "Print errors to stderr as a single line of JSON ({ code, message, details })");
|
|
50
51
|
}
|
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
package/dist/hooks/index.d.mts
DELETED
package/dist/hooks/index.mjs
DELETED
package/dist/print.d.mts
DELETED
package/dist/print.mjs
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { mkdir, writeFile } from "node:fs/promises";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
import { buildQueriesOutputPath, exists } from "./common.mjs";
|
|
4
|
-
async function printGeneratedTS(result, options) {
|
|
5
|
-
const dir = buildQueriesOutputPath(options.output);
|
|
6
|
-
const dirExists = await exists(dir);
|
|
7
|
-
if (!dirExists) {
|
|
8
|
-
await mkdir(dir, { recursive: true });
|
|
9
|
-
}
|
|
10
|
-
await writeFile(path.join(dir, result.name), result.content);
|
|
11
|
-
}
|
|
12
|
-
export async function print(results, options) {
|
|
13
|
-
const outputPath = options.output;
|
|
14
|
-
const dirExists = await exists(outputPath);
|
|
15
|
-
if (!dirExists) {
|
|
16
|
-
await mkdir(outputPath);
|
|
17
|
-
}
|
|
18
|
-
const promises = results.map(async (result) => {
|
|
19
|
-
await printGeneratedTS(result, options);
|
|
20
|
-
});
|
|
21
|
-
await Promise.all(promises);
|
|
22
|
-
}
|