@7nohe/openapi-react-query-codegen 4.0.0-beta.4 → 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 +4 -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 +1 -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/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/client/schema.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { propertyKey, toTypeName } from "./naming.mjs";
|
|
2
|
-
import { getByPointer, refSiblingSchema, resolveRef, } from "./spec.mjs";
|
|
3
|
-
const COMPONENT_SCHEMA_PREFIX = "#/components/schemas/";
|
|
2
|
+
import { baseMediaType, COMPONENT_SCHEMA_PREFIX, decodePointerSegment, getByPointer, refSiblingSchema, resolveRef, } from "./spec.mjs";
|
|
4
3
|
/** Name of the type generated for `#/components/schemas/<key>`, if any. */
|
|
5
4
|
export function componentSchemaKey(ref) {
|
|
6
5
|
if (!ref.startsWith(COMPONENT_SCHEMA_PREFIX))
|
|
@@ -8,7 +7,7 @@ export function componentSchemaKey(ref) {
|
|
|
8
7
|
const rest = ref.slice(COMPONENT_SCHEMA_PREFIX.length);
|
|
9
8
|
if (rest.includes("/"))
|
|
10
9
|
return undefined;
|
|
11
|
-
return
|
|
10
|
+
return decodePointerSegment(rest);
|
|
12
11
|
}
|
|
13
12
|
/** Whether a discriminator mapping target, a `$ref` or a name, is the schema. */
|
|
14
13
|
function isMappingTarget(target, key) {
|
|
@@ -22,6 +21,18 @@ function isMappingTarget(target, key) {
|
|
|
22
21
|
return false;
|
|
23
22
|
}
|
|
24
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* The discriminator values that stand for the schema `key`: the mapping
|
|
26
|
+
* entries whose target it is, or else its own name. Where the schema pins the
|
|
27
|
+
* property to literals of its own, only those remain.
|
|
28
|
+
*/
|
|
29
|
+
function valuesNaming(discriminator, key, pinned, isTarget) {
|
|
30
|
+
const mapped = Object.entries((discriminator.mapping ?? {}))
|
|
31
|
+
.filter(([, target]) => isTarget(target))
|
|
32
|
+
.map(([value]) => value);
|
|
33
|
+
const named = mapped.length > 0 ? mapped : [key];
|
|
34
|
+
return pinned ? named.filter((value) => pinned.includes(value)) : named;
|
|
35
|
+
}
|
|
25
36
|
/** Render a JSDoc block, or an empty string when there is nothing to say. */
|
|
26
37
|
export function docComment(parts, indent = "") {
|
|
27
38
|
const lines = [];
|
|
@@ -172,13 +183,7 @@ export function renderComponentSchema(schema, key, ctx) {
|
|
|
172
183
|
return renderSchema(schema, ctx);
|
|
173
184
|
const property = discriminator.propertyName;
|
|
174
185
|
const pinned = pinnedValues(schema, property, ctx);
|
|
175
|
-
const
|
|
176
|
-
.filter(([, target]) => isMappingTarget(target, key))
|
|
177
|
-
.map(([value]) => value);
|
|
178
|
-
const named = mapped.length > 0 ? mapped : [key];
|
|
179
|
-
const allowed = pinned
|
|
180
|
-
? named.filter((value) => pinned.includes(value))
|
|
181
|
-
: named;
|
|
186
|
+
const allowed = valuesNaming(discriminator, key, pinned, (target) => isMappingTarget(target, key));
|
|
182
187
|
if (allowed.length === 0 || pinned?.length === 1)
|
|
183
188
|
return renderSchema(schema, ctx);
|
|
184
189
|
const members = schema.allOf.map((member) =>
|
|
@@ -194,6 +199,100 @@ export function renderComponentSchema(schema, key, ctx) {
|
|
|
194
199
|
};
|
|
195
200
|
return renderSchema({ ...schema, allOf: [...members, tag] }, ctx);
|
|
196
201
|
}
|
|
202
|
+
/**
|
|
203
|
+
* Whether a schema that is composed of others (`allOf`, `oneOf`, `anyOf`) also
|
|
204
|
+
* describes an object itself, which its members are then intersected with.
|
|
205
|
+
*/
|
|
206
|
+
export function hasObjectKeywords(schema) {
|
|
207
|
+
return Boolean(schema.properties ||
|
|
208
|
+
schema.patternProperties ||
|
|
209
|
+
schema.additionalProperties);
|
|
210
|
+
}
|
|
211
|
+
/** What a schema without a `type` is taken for, going by its keywords. */
|
|
212
|
+
function impliedType(schema) {
|
|
213
|
+
if (schema.properties ||
|
|
214
|
+
schema.patternProperties ||
|
|
215
|
+
schema.additionalProperties !== undefined) {
|
|
216
|
+
return "object";
|
|
217
|
+
}
|
|
218
|
+
return schema.items || schema.prefixItems ? "array" : undefined;
|
|
219
|
+
}
|
|
220
|
+
const ALL_KINDS = [
|
|
221
|
+
"string",
|
|
222
|
+
"number",
|
|
223
|
+
"boolean",
|
|
224
|
+
"null",
|
|
225
|
+
"array",
|
|
226
|
+
"object",
|
|
227
|
+
"other",
|
|
228
|
+
];
|
|
229
|
+
function kindOfType(type) {
|
|
230
|
+
if (type === "integer")
|
|
231
|
+
return "number";
|
|
232
|
+
return ALL_KINDS.find((kind) => kind === type) ?? "other";
|
|
233
|
+
}
|
|
234
|
+
function kindOfValue(value) {
|
|
235
|
+
if (value === null)
|
|
236
|
+
return "null";
|
|
237
|
+
if (Array.isArray(value))
|
|
238
|
+
return "array";
|
|
239
|
+
const type = typeof value;
|
|
240
|
+
return type === "string" ||
|
|
241
|
+
type === "number" ||
|
|
242
|
+
type === "boolean" ||
|
|
243
|
+
type === "object"
|
|
244
|
+
? type
|
|
245
|
+
: "other";
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* The kinds of value a schema allows: every kind when nothing narrows it, and
|
|
249
|
+
* none for `false`. This is the one reading of `type`, `enum`, `const`, the
|
|
250
|
+
* keywords that imply a type and the compositions; whatever asks what a schema
|
|
251
|
+
* can hold (how a parameter is serialized, what a page parameter starts at,
|
|
252
|
+
* how the members of a union are told apart) goes by it.
|
|
253
|
+
*
|
|
254
|
+
* `nullable` adds nothing: no caller tells a value from its absence by it.
|
|
255
|
+
*/
|
|
256
|
+
export function schemaKinds(doc, schema, depth = 0) {
|
|
257
|
+
if (schema === false)
|
|
258
|
+
return new Set();
|
|
259
|
+
if (!schema || typeof schema !== "object" || depth > 16) {
|
|
260
|
+
return new Set(ALL_KINDS);
|
|
261
|
+
}
|
|
262
|
+
if (typeof schema.$ref === "string") {
|
|
263
|
+
return schemaKinds(doc, getByPointer(doc, schema.$ref), depth + 1);
|
|
264
|
+
}
|
|
265
|
+
if (schema.const !== undefined)
|
|
266
|
+
return new Set([kindOfValue(schema.const)]);
|
|
267
|
+
if (Array.isArray(schema.enum)) {
|
|
268
|
+
return new Set(schema.enum.map(kindOfValue));
|
|
269
|
+
}
|
|
270
|
+
let kinds;
|
|
271
|
+
if (Array.isArray(schema.type)) {
|
|
272
|
+
kinds = new Set(schema.type.map(kindOfType));
|
|
273
|
+
}
|
|
274
|
+
else if (typeof schema.type === "string") {
|
|
275
|
+
kinds = new Set([kindOfType(schema.type)]);
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
const implied = impliedType(schema);
|
|
279
|
+
kinds = new Set(implied ? [implied] : ALL_KINDS);
|
|
280
|
+
}
|
|
281
|
+
const narrow = (other) => {
|
|
282
|
+
kinds = new Set([...kinds].filter((kind) => other.has(kind)));
|
|
283
|
+
};
|
|
284
|
+
for (const member of Array.isArray(schema.allOf) ? schema.allOf : []) {
|
|
285
|
+
narrow(schemaKinds(doc, member, depth + 1));
|
|
286
|
+
}
|
|
287
|
+
for (const key of ["oneOf", "anyOf"]) {
|
|
288
|
+
if (!Array.isArray(schema[key]) || schema[key].length === 0)
|
|
289
|
+
continue;
|
|
290
|
+
narrow(new Set(schema[key].flatMap((member) => [
|
|
291
|
+
...schemaKinds(doc, member, depth + 1),
|
|
292
|
+
])));
|
|
293
|
+
}
|
|
294
|
+
return kinds;
|
|
295
|
+
}
|
|
197
296
|
/** Render the TypeScript type for an OpenAPI schema object. */
|
|
198
297
|
export function renderSchema(schema, ctx, indent = "", seen = new Set()) {
|
|
199
298
|
if (schema === true || schema === undefined || schema === null) {
|
|
@@ -232,9 +331,7 @@ export function renderSchema(schema, ctx, indent = "", seen = new Set()) {
|
|
|
232
331
|
.filter((member) => !isRequiredOnly(member))
|
|
233
332
|
.map((member) => renderSchema(member, ctx, indent, seen));
|
|
234
333
|
members.push(...unions);
|
|
235
|
-
if (schema
|
|
236
|
-
schema.patternProperties ||
|
|
237
|
-
schema.additionalProperties) {
|
|
334
|
+
if (hasObjectKeywords(schema)) {
|
|
238
335
|
members.push(renderObject(schema, ctx, indent, seen));
|
|
239
336
|
}
|
|
240
337
|
const required = renderInheritedRequired(schema, ctx, indent, seen);
|
|
@@ -243,7 +340,7 @@ export function renderSchema(schema, ctx, indent = "", seen = new Set()) {
|
|
|
243
340
|
return addNull(intersection(members), schema);
|
|
244
341
|
}
|
|
245
342
|
if (unions.length > 0) {
|
|
246
|
-
const members = schema
|
|
343
|
+
const members = hasObjectKeywords(schema)
|
|
247
344
|
? [...unions, renderObject(schema, ctx, indent, seen)]
|
|
248
345
|
: unions;
|
|
249
346
|
return addNull(intersection(members), schema);
|
|
@@ -281,12 +378,10 @@ export function renderSchema(schema, ctx, indent = "", seen = new Set()) {
|
|
|
281
378
|
type = renderObject(schema, ctx, indent, seen);
|
|
282
379
|
break;
|
|
283
380
|
default:
|
|
284
|
-
if (schema
|
|
285
|
-
schema.patternProperties ||
|
|
286
|
-
schema.additionalProperties !== undefined) {
|
|
381
|
+
if (impliedType(schema) === "object") {
|
|
287
382
|
type = renderObject(schema, ctx, indent, seen);
|
|
288
383
|
}
|
|
289
|
-
else if (schema
|
|
384
|
+
else if (impliedType(schema) === "array") {
|
|
290
385
|
type = renderArray(schema, ctx, indent, seen);
|
|
291
386
|
}
|
|
292
387
|
else if (hasBinaryContent(schema)) {
|
|
@@ -316,7 +411,7 @@ function hasBinaryContent(schema) {
|
|
|
316
411
|
const mediaType = schema.contentMediaType;
|
|
317
412
|
return (typeof mediaType === "string" &&
|
|
318
413
|
schema.contentEncoding === undefined &&
|
|
319
|
-
!TEXT_MEDIA_TYPE.test(mediaType
|
|
414
|
+
!TEXT_MEDIA_TYPE.test(baseMediaType(mediaType)));
|
|
320
415
|
}
|
|
321
416
|
/** Keywords that give a schema a type of its own. */
|
|
322
417
|
const TYPE_KEYWORDS = [
|
|
@@ -413,9 +508,11 @@ function renderRef(ref, ctx, indent, seen) {
|
|
|
413
508
|
if (!Object.hasOwn(schemas, key) && !ctx.names.has(key))
|
|
414
509
|
return "unknown";
|
|
415
510
|
const writable = ctx.writableNames?.get(key);
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
511
|
+
const name = ctx.mode === "write" && writable !== undefined
|
|
512
|
+
? writable
|
|
513
|
+
: (ctx.names.get(key) ?? toTypeName(key));
|
|
514
|
+
ctx.onRef?.(name);
|
|
515
|
+
return name;
|
|
419
516
|
}
|
|
420
517
|
if (seen.has(ref))
|
|
421
518
|
return "unknown";
|
|
@@ -459,16 +556,10 @@ function discriminatorChoice(member, discriminator, ctx) {
|
|
|
459
556
|
if (typeof target?.type === "string" && target.type !== "object") {
|
|
460
557
|
return undefined;
|
|
461
558
|
}
|
|
462
|
-
const values = Object.entries((discriminator.mapping ?? {}))
|
|
463
|
-
.filter(([, target]) => isMappingTarget(target, key) || target.endsWith(`/${key}`))
|
|
464
|
-
.map(([value]) => value);
|
|
465
559
|
// A member that pins the property keeps it, unless it inherits an enum of all
|
|
466
560
|
// the subtypes, which the discriminator then narrows down.
|
|
467
561
|
const pinned = pinnedValues(member, property, ctx);
|
|
468
|
-
const
|
|
469
|
-
const allowed = pinned
|
|
470
|
-
? named.filter((value) => pinned.includes(value))
|
|
471
|
-
: named;
|
|
562
|
+
const allowed = valuesNaming(discriminator, key, pinned, (target) => isMappingTarget(target, key) || target.endsWith(`/${key}`));
|
|
472
563
|
return { allowed, pinned };
|
|
473
564
|
}
|
|
474
565
|
/**
|
|
@@ -591,8 +682,15 @@ function scanReadWriteOnly(doc, schema, found, depth = 0) {
|
|
|
591
682
|
return;
|
|
592
683
|
if (typeof schema.$ref === "string") {
|
|
593
684
|
const key = componentSchemaKey(schema.$ref);
|
|
594
|
-
if (key !== undefined)
|
|
685
|
+
if (key !== undefined) {
|
|
595
686
|
found.refs.add(key);
|
|
687
|
+
}
|
|
688
|
+
else if (!found.inlined.has(schema.$ref)) {
|
|
689
|
+
// Any other reference has no type of its own: `renderRef` writes what
|
|
690
|
+
// it points to in place, so that is part of this schema.
|
|
691
|
+
found.inlined.add(schema.$ref);
|
|
692
|
+
scanReadWriteOnly(doc, getByPointer(doc, schema.$ref), found, depth + 1);
|
|
693
|
+
}
|
|
596
694
|
}
|
|
597
695
|
for (const value of Object.values((schema.properties ?? {}))) {
|
|
598
696
|
if (hasFlag(doc, value, "readOnly") || hasFlag(doc, value, "writeOnly")) {
|
|
@@ -615,15 +713,19 @@ function scanReadWriteOnly(doc, schema, found, depth = 0) {
|
|
|
615
713
|
}
|
|
616
714
|
/**
|
|
617
715
|
* Component schemas that need a separate request-side type: the ones with a
|
|
618
|
-
* `readOnly`/`writeOnly` property, directly
|
|
619
|
-
* reference.
|
|
716
|
+
* `readOnly`/`writeOnly` property, directly, in what they inline, or through
|
|
717
|
+
* the schemas they reference.
|
|
620
718
|
*/
|
|
621
719
|
export function findReadWriteOnlyKeys(doc) {
|
|
622
720
|
const schemas = (doc.components?.schemas ?? {});
|
|
623
721
|
const direct = new Set();
|
|
624
722
|
const references = new Map();
|
|
625
723
|
for (const [key, schema] of Object.entries(schemas)) {
|
|
626
|
-
const found = {
|
|
724
|
+
const found = {
|
|
725
|
+
direct: false,
|
|
726
|
+
refs: new Set(),
|
|
727
|
+
inlined: new Set(),
|
|
728
|
+
};
|
|
627
729
|
scanReadWriteOnly(doc, schema, found);
|
|
628
730
|
if (found.direct)
|
|
629
731
|
direct.add(key);
|
package/dist/client/spec.d.mts
CHANGED
|
@@ -18,6 +18,12 @@ export type LoadedSpec = {
|
|
|
18
18
|
};
|
|
19
19
|
/** Load a specification from a file path, a URL or inline JSON/YAML content. */
|
|
20
20
|
export declare function loadSpec(input: string): Promise<LoadedSpec>;
|
|
21
|
+
/** A media type without its parameters, in lower case. */
|
|
22
|
+
export declare function baseMediaType(mediaType: string): string;
|
|
23
|
+
/** Where the component schemas are, as the start of a `$ref`. */
|
|
24
|
+
export declare const COMPONENT_SCHEMA_PREFIX = "#/components/schemas/";
|
|
25
|
+
/** One segment of a JSON pointer in a `$ref`, as the key it stands for. */
|
|
26
|
+
export declare function decodePointerSegment(segment: string): string;
|
|
21
27
|
/** Resolve a JSON pointer such as `#/components/schemas/Pet`. */
|
|
22
28
|
export declare function getByPointer(root: Json, pointer: string): Json;
|
|
23
29
|
/**
|
package/dist/client/spec.mjs
CHANGED
|
@@ -85,6 +85,18 @@ export async function loadSpec(input) {
|
|
|
85
85
|
}
|
|
86
86
|
return { doc: parseText(input, "inline content"), location: undefined };
|
|
87
87
|
}
|
|
88
|
+
/** A media type without its parameters, in lower case. */
|
|
89
|
+
export function baseMediaType(mediaType) {
|
|
90
|
+
return mediaType.toLowerCase().split(";")[0].trim();
|
|
91
|
+
}
|
|
92
|
+
/** Where the component schemas are, as the start of a `$ref`. */
|
|
93
|
+
export const COMPONENT_SCHEMA_PREFIX = "#/components/schemas/";
|
|
94
|
+
/** One segment of a JSON pointer in a `$ref`, as the key it stands for. */
|
|
95
|
+
export function decodePointerSegment(segment) {
|
|
96
|
+
return decodeURIComponent(segment)
|
|
97
|
+
.replaceAll("~1", "/")
|
|
98
|
+
.replaceAll("~0", "~");
|
|
99
|
+
}
|
|
88
100
|
/** Resolve a JSON pointer such as `#/components/schemas/Pet`. */
|
|
89
101
|
export function getByPointer(root, pointer) {
|
|
90
102
|
const clean = pointer.startsWith("#") ? pointer.slice(1) : pointer;
|
|
@@ -92,9 +104,7 @@ export function getByPointer(root, pointer) {
|
|
|
92
104
|
return root;
|
|
93
105
|
let current = root;
|
|
94
106
|
for (const rawSegment of clean.replace(/^\//, "").split("/")) {
|
|
95
|
-
const segment =
|
|
96
|
-
.replaceAll("~1", "/")
|
|
97
|
-
.replaceAll("~0", "~");
|
|
107
|
+
const segment = decodePointerSegment(rawSegment);
|
|
98
108
|
if (current === null || typeof current !== "object")
|
|
99
109
|
return undefined;
|
|
100
110
|
current = current[segment];
|
|
@@ -229,7 +239,7 @@ function externalSchemaName(location, pointer) {
|
|
|
229
239
|
}
|
|
230
240
|
function componentSchemaRef(name) {
|
|
231
241
|
const segment = name.replaceAll("~", "~0").replaceAll("/", "~1");
|
|
232
|
-
return
|
|
242
|
+
return `${COMPONENT_SCHEMA_PREFIX}${encodeURIComponent(segment)}`;
|
|
233
243
|
}
|
|
234
244
|
/**
|
|
235
245
|
* Bundle every external `$ref` (relative files and URLs) into the root
|
|
@@ -366,7 +376,7 @@ export async function bundleExternalRefs(loaded) {
|
|
|
366
376
|
name =
|
|
367
377
|
hoisted.get(located.key) ??
|
|
368
378
|
hoisted.get(`${location}${componentSchemaRef(target)}`) ??
|
|
369
|
-
hoisted.get(`${location}
|
|
379
|
+
hoisted.get(`${location}${COMPONENT_SCHEMA_PREFIX}${target}`);
|
|
370
380
|
}
|
|
371
381
|
catch {
|
|
372
382
|
// Not a reference that can be resolved, so a schema name.
|
package/dist/common.d.mts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type { PathLike } from "node:fs";
|
|
2
1
|
import type { LimitedUserConfig } from "./cli.mjs";
|
|
3
|
-
export declare function exists(f: PathLike): Promise<boolean>;
|
|
4
2
|
/**
|
|
5
3
|
* Safely parse a value into a number. Checks for NaN and Infinity.
|
|
6
4
|
* Returns NaN if the string is not a valid number.
|
package/dist/common.mjs
CHANGED
|
@@ -1,15 +1,5 @@
|
|
|
1
|
-
import { stat } from "node:fs/promises";
|
|
2
1
|
import path from "node:path";
|
|
3
2
|
import { queriesOutputPath, requestsOutputPath } from "./constants.mjs";
|
|
4
|
-
export async function exists(f) {
|
|
5
|
-
try {
|
|
6
|
-
await stat(f);
|
|
7
|
-
return true;
|
|
8
|
-
}
|
|
9
|
-
catch {
|
|
10
|
-
return false;
|
|
11
|
-
}
|
|
12
|
-
}
|
|
13
3
|
/**
|
|
14
4
|
* Safely parse a value into a number. Checks for NaN and Infinity.
|
|
15
5
|
* Returns NaN if the string is not a valid number.
|
package/dist/createSource.d.mts
CHANGED
|
@@ -4,7 +4,7 @@ import type { GeneratedFile } from "./types.mjs";
|
|
|
4
4
|
* Create the React Query source files for a generated client.
|
|
5
5
|
*/
|
|
6
6
|
export declare const createSource: ({ generated, client, version, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, importExtension, }: {
|
|
7
|
-
generated: Pick<GeneratedClient, "operations"
|
|
7
|
+
generated: Pick<GeneratedClient, "operations">;
|
|
8
8
|
client: ClientKind;
|
|
9
9
|
version: string;
|
|
10
10
|
pageParam: string;
|
package/dist/createSource.mjs
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
|
-
import { generateAllFiles } from "./hooks/
|
|
1
|
+
import { generateAllFiles } from "./hooks/generateFiles.mjs";
|
|
2
2
|
/**
|
|
3
3
|
* Create the React Query source files for a generated client.
|
|
4
4
|
*/
|
|
5
5
|
export const createSource = ({ generated, client, version, pageParam, nextPageParam, initialPageParam, omitInitialPageParam, importExtension = "", }) => {
|
|
6
6
|
const ctx = {
|
|
7
7
|
client,
|
|
8
|
-
modelNames: generated.modelNames,
|
|
9
|
-
serviceNames: generated.serviceNames,
|
|
10
8
|
pageParam,
|
|
11
9
|
nextPageParam,
|
|
12
10
|
initialPageParam,
|
package/dist/generate.mjs
CHANGED
|
@@ -3,7 +3,6 @@ import { buildQueriesOutputPath, buildRequestsOutputPath, formatOptions, } from
|
|
|
3
3
|
import { createSource } from "./createSource.mjs";
|
|
4
4
|
import { CodegenError, ErrorCodes, toCodegenError } from "./errors.mjs";
|
|
5
5
|
import { processOutput } from "./format.mjs";
|
|
6
|
-
import { print } from "./print.mjs";
|
|
7
6
|
export { findNearestTsConfigPath };
|
|
8
7
|
/** Map a `--client` value (including the v3 `@hey-api/*` aliases) to a kind. */
|
|
9
8
|
export function resolveClientKind(client) {
|
|
@@ -82,8 +81,8 @@ async function runGenerate(options, version, runtime) {
|
|
|
82
81
|
omitInitialPageParam: formattedOptions.omitInitialPageParam ?? false,
|
|
83
82
|
importExtension,
|
|
84
83
|
});
|
|
85
|
-
await print(source, formattedOptions);
|
|
86
84
|
const queriesOutputPath = buildQueriesOutputPath(options.output);
|
|
85
|
+
await writeGeneratedFiles(queriesOutputPath, source);
|
|
87
86
|
await processOutput({
|
|
88
87
|
output: queriesOutputPath,
|
|
89
88
|
format: formattedOptions.format,
|
|
@@ -1,42 +1,38 @@
|
|
|
1
|
-
import type { GenerationContext
|
|
1
|
+
import type { GenerationContext } from "../types.mjs";
|
|
2
|
+
import { type HookOperation } from "./hookNames.mjs";
|
|
2
3
|
import type { ConstStatement, TypeStatement } from "./statement.mjs";
|
|
3
4
|
/**
|
|
4
5
|
* Build the default response type alias.
|
|
5
6
|
* Example: export type FindPetsDefaultResponse = Awaited<ReturnType<typeof findPets>>["data"];
|
|
6
7
|
*/
|
|
7
|
-
export declare function buildDefaultResponseType(op:
|
|
8
|
+
export declare function buildDefaultResponseType(op: HookOperation): TypeStatement;
|
|
8
9
|
/**
|
|
9
10
|
* Build the query result type alias.
|
|
10
11
|
* Example: export type FindPetsQueryResult<TData = FindPetsDefaultResponse, TError = unknown> = UseQueryResult<TData, TError>;
|
|
11
12
|
*/
|
|
12
|
-
export declare function buildQueryResultType(op:
|
|
13
|
+
export declare function buildQueryResultType(op: HookOperation): TypeStatement;
|
|
13
14
|
/**
|
|
14
15
|
* Build the mutation result type alias.
|
|
15
16
|
* Example: export type AddPetMutationResult = Awaited<ReturnType<typeof addPet>>;
|
|
16
17
|
*/
|
|
17
|
-
export declare function buildMutationResultType(op:
|
|
18
|
+
export declare function buildMutationResultType(op: HookOperation): TypeStatement;
|
|
18
19
|
/**
|
|
19
|
-
* Build
|
|
20
|
+
* Build the key constant of a query or a mutation.
|
|
20
21
|
* Example: export const useFindPetsKey = "FindPets";
|
|
21
22
|
*/
|
|
22
|
-
export declare function
|
|
23
|
-
/**
|
|
24
|
-
* Build mutation key constant.
|
|
25
|
-
* Example: export const useAddPetKey = "AddPet";
|
|
26
|
-
*/
|
|
27
|
-
export declare function buildMutationKeyConst(op: OperationInfo): ConstStatement;
|
|
23
|
+
export declare function buildKeyConst(op: HookOperation): ConstStatement;
|
|
28
24
|
/**
|
|
29
25
|
* Build query key function.
|
|
30
26
|
* Example: export const UseFindPetsKeyFn = (clientOptions: Options<FindPetsData, true> = {}, queryKey?: Array<unknown>) =>
|
|
31
27
|
* [useFindPetsKey, ...(queryKey ?? [clientOptions])];
|
|
32
28
|
*/
|
|
33
|
-
export declare function buildQueryKeyFn(op:
|
|
29
|
+
export declare function buildQueryKeyFn(op: HookOperation): ConstStatement;
|
|
34
30
|
/**
|
|
35
31
|
* Build mutation key function.
|
|
36
32
|
* Example: export const UseAddPetKeyFn = (mutationKey?: Array<unknown>) =>
|
|
37
33
|
* [useAddPetKey, ...(mutationKey ?? [])];
|
|
38
34
|
*/
|
|
39
|
-
export declare function buildMutationKeyFn(op:
|
|
35
|
+
export declare function buildMutationKeyFn(op: HookOperation): ConstStatement;
|
|
40
36
|
/**
|
|
41
37
|
* Build the client options type for infinite queries.
|
|
42
38
|
* The page parameter is excluded because TanStack Query supplies it via the
|
|
@@ -45,7 +41,7 @@ export declare function buildMutationKeyFn(op: OperationInfo): ConstStatement;
|
|
|
45
41
|
* export type FindPaginatedPetsInfiniteClientOptions = Omit<Options<FindPaginatedPetsData, true>, "query"> &
|
|
46
42
|
* { query?: Omit<NonNullable<FindPaginatedPetsData["query"]>, "page"> };
|
|
47
43
|
*/
|
|
48
|
-
export declare function buildInfiniteClientOptionsType(op:
|
|
44
|
+
export declare function buildInfiniteClientOptionsType(op: HookOperation, ctx: GenerationContext): TypeStatement;
|
|
49
45
|
/**
|
|
50
46
|
* Build the infinite query key constant.
|
|
51
47
|
* Shares the plain query key as its first segment so a single
|
|
@@ -54,7 +50,7 @@ export declare function buildInfiniteClientOptionsType(op: OperationInfo, ctx: G
|
|
|
54
50
|
* segment keeps cached InfiniteData from colliding with plain query data (#140).
|
|
55
51
|
* Example: export const useFindPaginatedPetsInfiniteKey = [useFindPaginatedPetsKey, "infinite"] as const;
|
|
56
52
|
*/
|
|
57
|
-
export declare function buildInfiniteQueryKeyConst(op:
|
|
53
|
+
export declare function buildInfiniteQueryKeyConst(op: HookOperation): ConstStatement;
|
|
58
54
|
/**
|
|
59
55
|
* Build the infinite query key function.
|
|
60
56
|
* The custom queryKey argument only replaces the params segment — the
|
|
@@ -63,4 +59,4 @@ export declare function buildInfiniteQueryKeyConst(op: OperationInfo): ConstStat
|
|
|
63
59
|
* Example: export const UseFindPaginatedPetsInfiniteKeyFn = (clientOptions: FindPaginatedPetsInfiniteClientOptions = {}, queryKey?: Array<unknown>) =>
|
|
64
60
|
* [...useFindPaginatedPetsInfiniteKey, ...(queryKey ?? [clientOptions])];
|
|
65
61
|
*/
|
|
66
|
-
export declare function buildInfiniteQueryKeyFn(op:
|
|
62
|
+
export declare function buildInfiniteQueryKeyFn(op: HookOperation): ConstStatement;
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { buildClientOptionsParam } from "./buildQueryHooks.mjs";
|
|
2
|
+
import { hookNames } from "./hookNames.mjs";
|
|
3
|
+
import { dataModels, getDataTypeName } from "./operationNames.mjs";
|
|
2
4
|
/**
|
|
3
5
|
* Build the default response type alias.
|
|
4
6
|
* Example: export type FindPetsDefaultResponse = Awaited<ReturnType<typeof findPets>>["data"];
|
|
@@ -6,8 +8,9 @@ import { getDataTypeName } from "./operationNames.mjs";
|
|
|
6
8
|
export function buildDefaultResponseType(op) {
|
|
7
9
|
return {
|
|
8
10
|
kind: "type",
|
|
9
|
-
name:
|
|
11
|
+
name: hookNames(op).defaultResponse,
|
|
10
12
|
type: `Awaited<ReturnType<typeof ${op.methodName}>>["data"]`,
|
|
13
|
+
service: op.methodName,
|
|
11
14
|
};
|
|
12
15
|
}
|
|
13
16
|
/**
|
|
@@ -17,9 +20,9 @@ export function buildDefaultResponseType(op) {
|
|
|
17
20
|
export function buildQueryResultType(op) {
|
|
18
21
|
return {
|
|
19
22
|
kind: "type",
|
|
20
|
-
name:
|
|
23
|
+
name: hookNames(op).queryResult,
|
|
21
24
|
typeParameters: [
|
|
22
|
-
{ name: "TData", default:
|
|
25
|
+
{ name: "TData", default: hookNames(op).defaultResponse },
|
|
23
26
|
{ name: "TError", default: "unknown" },
|
|
24
27
|
],
|
|
25
28
|
type: "UseQueryResult<TData, TError>",
|
|
@@ -32,29 +35,19 @@ export function buildQueryResultType(op) {
|
|
|
32
35
|
export function buildMutationResultType(op) {
|
|
33
36
|
return {
|
|
34
37
|
kind: "type",
|
|
35
|
-
name:
|
|
38
|
+
name: hookNames(op).mutationResult,
|
|
36
39
|
type: `Awaited<ReturnType<typeof ${op.methodName}>>`,
|
|
40
|
+
service: op.methodName,
|
|
37
41
|
};
|
|
38
42
|
}
|
|
39
43
|
/**
|
|
40
|
-
* Build
|
|
44
|
+
* Build the key constant of a query or a mutation.
|
|
41
45
|
* Example: export const useFindPetsKey = "FindPets";
|
|
42
46
|
*/
|
|
43
|
-
export function
|
|
47
|
+
export function buildKeyConst(op) {
|
|
44
48
|
return {
|
|
45
49
|
kind: "const",
|
|
46
|
-
name:
|
|
47
|
-
initializer: `"${op.capitalizedMethodName}"`,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
/**
|
|
51
|
-
* Build mutation key constant.
|
|
52
|
-
* Example: export const useAddPetKey = "AddPet";
|
|
53
|
-
*/
|
|
54
|
-
export function buildMutationKeyConst(op) {
|
|
55
|
-
return {
|
|
56
|
-
kind: "const",
|
|
57
|
-
name: `use${op.capitalizedMethodName}Key`,
|
|
50
|
+
name: hookNames(op).key,
|
|
58
51
|
initializer: `"${op.capitalizedMethodName}"`,
|
|
59
52
|
};
|
|
60
53
|
}
|
|
@@ -64,16 +57,11 @@ export function buildMutationKeyConst(op) {
|
|
|
64
57
|
* [useFindPetsKey, ...(queryKey ?? [clientOptions])];
|
|
65
58
|
*/
|
|
66
59
|
export function buildQueryKeyFn(op) {
|
|
67
|
-
const dataTypeName = getDataTypeName(op);
|
|
68
|
-
const params = [];
|
|
69
|
-
const defaultValue = op.allParamsOptional ? " = {}" : "";
|
|
70
|
-
params.push(`clientOptions: Options<${dataTypeName}, true>${defaultValue}`);
|
|
71
|
-
params.push("queryKey?: Array<unknown>");
|
|
72
|
-
const fallbackArray = "[clientOptions]";
|
|
73
60
|
return {
|
|
74
61
|
kind: "const",
|
|
75
|
-
name:
|
|
76
|
-
initializer: `(${
|
|
62
|
+
name: hookNames(op).keyFn,
|
|
63
|
+
initializer: `(${buildClientOptionsParam(op)}, queryKey?: Array<unknown>) => [${hookNames(op).key}, ...(queryKey ?? [clientOptions])]`,
|
|
64
|
+
models: dataModels(op),
|
|
77
65
|
};
|
|
78
66
|
}
|
|
79
67
|
/**
|
|
@@ -84,8 +72,8 @@ export function buildQueryKeyFn(op) {
|
|
|
84
72
|
export function buildMutationKeyFn(op) {
|
|
85
73
|
return {
|
|
86
74
|
kind: "const",
|
|
87
|
-
name:
|
|
88
|
-
initializer: `(mutationKey?: Array<unknown>) => [
|
|
75
|
+
name: hookNames(op).keyFn,
|
|
76
|
+
initializer: `(mutationKey?: Array<unknown>) => [${hookNames(op).key}, ...(mutationKey ?? [])]`,
|
|
89
77
|
};
|
|
90
78
|
}
|
|
91
79
|
/**
|
|
@@ -101,8 +89,9 @@ export function buildInfiniteClientOptionsType(op, ctx) {
|
|
|
101
89
|
const type = `Omit<Options<${dataTypeName}, true>, "query"> & { query?: Omit<NonNullable<${dataTypeName}["query"]>, "${ctx.pageParam}"> }`;
|
|
102
90
|
return {
|
|
103
91
|
kind: "type",
|
|
104
|
-
name:
|
|
92
|
+
name: hookNames(op).infiniteClientOptions,
|
|
105
93
|
type,
|
|
94
|
+
models: dataModels(op),
|
|
106
95
|
};
|
|
107
96
|
}
|
|
108
97
|
/**
|
|
@@ -116,8 +105,8 @@ export function buildInfiniteClientOptionsType(op, ctx) {
|
|
|
116
105
|
export function buildInfiniteQueryKeyConst(op) {
|
|
117
106
|
return {
|
|
118
107
|
kind: "const",
|
|
119
|
-
name:
|
|
120
|
-
initializer: `[
|
|
108
|
+
name: hookNames(op).infiniteKey,
|
|
109
|
+
initializer: `[${hookNames(op).key}, "infinite"] as const`,
|
|
121
110
|
};
|
|
122
111
|
}
|
|
123
112
|
/**
|
|
@@ -129,14 +118,15 @@ export function buildInfiniteQueryKeyConst(op) {
|
|
|
129
118
|
* [...useFindPaginatedPetsInfiniteKey, ...(queryKey ?? [clientOptions])];
|
|
130
119
|
*/
|
|
131
120
|
export function buildInfiniteQueryKeyFn(op) {
|
|
121
|
+
const names = hookNames(op);
|
|
132
122
|
const defaultValue = op.allParamsOptional ? " = {}" : "";
|
|
133
123
|
const params = [
|
|
134
|
-
`clientOptions: ${
|
|
124
|
+
`clientOptions: ${names.infiniteClientOptions}${defaultValue}`,
|
|
135
125
|
"queryKey?: Array<unknown>",
|
|
136
126
|
];
|
|
137
127
|
return {
|
|
138
128
|
kind: "const",
|
|
139
|
-
name:
|
|
140
|
-
initializer: `(${params.join(", ")}) => [
|
|
129
|
+
name: names.infiniteKeyFn,
|
|
130
|
+
initializer: `(${params.join(", ")}) => [...${names.infiniteKey}, ...(queryKey ?? [clientOptions])]`,
|
|
141
131
|
};
|
|
142
132
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { GenerationContext
|
|
1
|
+
import type { GenerationContext } from "../types.mjs";
|
|
2
|
+
import { type HookOperation } from "./hookNames.mjs";
|
|
2
3
|
import type { ConstStatement } from "./statement.mjs";
|
|
3
4
|
/**
|
|
4
5
|
* Build useMutation hook.
|
|
@@ -12,4 +13,4 @@ import type { ConstStatement } from "./statement.mjs";
|
|
|
12
13
|
* ...options
|
|
13
14
|
* });
|
|
14
15
|
*/
|
|
15
|
-
export declare function buildUseMutationHook(op:
|
|
16
|
+
export declare function buildUseMutationHook(op: HookOperation, ctx: GenerationContext): ConstStatement;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { SDK_CALL_ARGS } from "./buildQueryHooks.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { hookNames } from "./hookNames.mjs";
|
|
3
|
+
import { dataModels, errorModels, getDataTypeName, getErrorType, operationConst, } from "./operationNames.mjs";
|
|
3
4
|
/**
|
|
4
5
|
* Build useMutation hook.
|
|
5
6
|
* Example:
|
|
@@ -13,17 +14,11 @@ import { getDataTypeName, getErrorType } from "./operationNames.mjs";
|
|
|
13
14
|
* });
|
|
14
15
|
*/
|
|
15
16
|
export function buildUseMutationHook(op, ctx) {
|
|
16
|
-
const hookName =
|
|
17
|
+
const hookName = hookNames(op).hook;
|
|
17
18
|
const errorType = getErrorType(op, ctx);
|
|
18
|
-
const dataTypeDefault = `Common.${op.
|
|
19
|
+
const dataTypeDefault = `Common.${hookNames(op).mutationResult}`;
|
|
19
20
|
const optionsType = `Options<${getDataTypeName(op)}, true>`;
|
|
20
21
|
const mutationFn = `clientOptions => ${op.methodName}(${SDK_CALL_ARGS}) as unknown as Promise<TData>`;
|
|
21
|
-
const body = `useMutation<TData, TError, ${optionsType}, TContext>({ mutationKey: Common
|
|
22
|
-
return {
|
|
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}`,
|
|
28
|
-
};
|
|
22
|
+
const body = `useMutation<TData, TError, ${optionsType}, TContext>({ mutationKey: Common.${hookNames(op).keyFn}(mutationKey), mutationFn: ${mutationFn}, ...options })`;
|
|
23
|
+
return operationConst(op, hookName, `<TData = ${dataTypeDefault}, TError = ${errorType}, TQueryKey extends Array<unknown> = unknown[], TContext = unknown>(mutationKey?: TQueryKey, options?: Omit<UseMutationOptions<TData, TError, ${optionsType}, TContext>, "mutationKey" | "mutationFn">) => ${body}`, [...dataModels(op), ...errorModels(op)]);
|
|
29
24
|
}
|