@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/client/index.mjs
CHANGED
|
@@ -30,6 +30,13 @@ function toOperationInfo(operation, ctx, pageParam, emittedTypes) {
|
|
|
30
30
|
const pageParameter = method === "GET"
|
|
31
31
|
? operation.parameters.find((parameter) => parameter.in === "query" && parameter.name === pageParam)
|
|
32
32
|
: undefined;
|
|
33
|
+
const pageParamModels = new Set();
|
|
34
|
+
const pageParamType = pageParameter
|
|
35
|
+
? renderSchema(pageParameter.schema, {
|
|
36
|
+
...ctx,
|
|
37
|
+
onRef: (name) => pageParamModels.add(name),
|
|
38
|
+
})
|
|
39
|
+
: undefined;
|
|
33
40
|
return {
|
|
34
41
|
methodName: operation.functionName,
|
|
35
42
|
capitalizedMethodName: capitalizeFirst(operation.functionName),
|
|
@@ -41,9 +48,8 @@ function toOperationInfo(operation, ctx, pageParam, emittedTypes) {
|
|
|
41
48
|
jsDoc: operation.jsDoc,
|
|
42
49
|
allParamsOptional: optionsAreOptional(operation),
|
|
43
50
|
isPaginatable: pageParameter !== undefined,
|
|
44
|
-
pageParamType
|
|
45
|
-
|
|
46
|
-
: undefined,
|
|
51
|
+
pageParamType,
|
|
52
|
+
pageParamModels: pageParameter ? [...pageParamModels] : undefined,
|
|
47
53
|
pageParamTypeKind: pageParameter
|
|
48
54
|
? schemaPrimitiveKind(ctx.doc, pageParameter.schema)
|
|
49
55
|
: undefined,
|
|
@@ -67,9 +73,11 @@ export async function generateClient(options) {
|
|
|
67
73
|
reservedTypeNames: [...names.values(), ...writableNames.values()],
|
|
68
74
|
});
|
|
69
75
|
const ext = options.importExtension;
|
|
70
|
-
const
|
|
76
|
+
const baseUrl = getBaseUrl(doc);
|
|
77
|
+
const types = emitTypes(operations, ctx, {
|
|
71
78
|
enums: options.enums,
|
|
72
79
|
clientKind: options.client,
|
|
80
|
+
baseUrl,
|
|
73
81
|
});
|
|
74
82
|
const transformers = options.useDateType
|
|
75
83
|
? emitTransformers(operations, ctx)
|
|
@@ -95,7 +103,7 @@ export async function generateClient(options) {
|
|
|
95
103
|
content: emitClientFile({
|
|
96
104
|
kind: options.client,
|
|
97
105
|
ext,
|
|
98
|
-
baseUrl
|
|
106
|
+
baseUrl,
|
|
99
107
|
}),
|
|
100
108
|
},
|
|
101
109
|
{ name: "runtime.gen.ts", content: await readRuntime(options.client) },
|
package/dist/client/naming.d.mts
CHANGED
|
@@ -27,8 +27,11 @@ export declare function toTypeName(input: string): string;
|
|
|
27
27
|
export declare function nameFromPath(method: string, path: string): string;
|
|
28
28
|
/** Object key, quoted only when it is not a valid identifier. */
|
|
29
29
|
export declare function propertyKey(name: string): string;
|
|
30
|
-
/**
|
|
31
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Make `base` unique inside `used` by appending a counter. A name in `taken`
|
|
32
|
+
* is passed over as well; `taken` itself is left as it is.
|
|
33
|
+
*/
|
|
34
|
+
export declare function uniqueName(base: string, used: Set<string>, taken?: ReadonlySet<string>): string;
|
|
32
35
|
/**
|
|
33
36
|
* Name for an enum member generated from its value, in UPPER_SNAKE_CASE
|
|
34
37
|
* (`in-progress` → `IN_PROGRESS`). A number gets a `_` prefix, except when
|
package/dist/client/naming.mjs
CHANGED
|
@@ -75,6 +75,7 @@ const RESERVED_FUNCTION_NAMES = new Set([
|
|
|
75
75
|
"queryKey",
|
|
76
76
|
"queryOptions",
|
|
77
77
|
"signal",
|
|
78
|
+
"undefined",
|
|
78
79
|
"useInfiniteQuery",
|
|
79
80
|
"useMutation",
|
|
80
81
|
"useQuery",
|
|
@@ -146,11 +147,14 @@ export function nameFromPath(method, path) {
|
|
|
146
147
|
export function propertyKey(name) {
|
|
147
148
|
return isValidIdentifier(name) ? name : JSON.stringify(name);
|
|
148
149
|
}
|
|
149
|
-
/**
|
|
150
|
-
|
|
150
|
+
/**
|
|
151
|
+
* Make `base` unique inside `used` by appending a counter. A name in `taken`
|
|
152
|
+
* is passed over as well; `taken` itself is left as it is.
|
|
153
|
+
*/
|
|
154
|
+
export function uniqueName(base, used, taken) {
|
|
151
155
|
let name = base;
|
|
152
156
|
let counter = 2;
|
|
153
|
-
while (used.has(name)) {
|
|
157
|
+
while (used.has(name) || taken?.has(name)) {
|
|
154
158
|
name = `${base}${counter}`;
|
|
155
159
|
counter += 1;
|
|
156
160
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { PageParamTypeKind } from "../types.mjs";
|
|
1
2
|
import { type Doc, type Json } from "./spec.mjs";
|
|
2
3
|
declare const HTTP_METHODS: readonly ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
3
4
|
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
@@ -98,6 +99,6 @@ export declare function classifyResponses(operation: OperationIR): {
|
|
|
98
99
|
responses: ResponseIR[];
|
|
99
100
|
errors: ResponseIR[];
|
|
100
101
|
};
|
|
101
|
-
/**
|
|
102
|
-
export declare function schemaPrimitiveKind(doc: Doc, node: Json):
|
|
102
|
+
/** The one primitive a schema holds, leaving `null` aside. */
|
|
103
|
+
export declare function schemaPrimitiveKind(doc: Doc, node: Json): PageParamTypeKind;
|
|
103
104
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { nameFromPath, toFunctionName, toTypeName, uniqueName, } from "./naming.mjs";
|
|
2
|
-
import { docComment, findReadWriteOnlyKeys } from "./schema.mjs";
|
|
3
|
-
import {
|
|
2
|
+
import { docComment, findReadWriteOnlyKeys, schemaKinds } from "./schema.mjs";
|
|
3
|
+
import { baseMediaType, getByPointer } from "./spec.mjs";
|
|
4
4
|
/**
|
|
5
5
|
* Type names a schema or an operation type cannot take: `ClientOptions` and
|
|
6
6
|
* `Options` are exported next to them, and the rest are what the generated hook
|
|
@@ -71,7 +71,7 @@ export function deref(doc, node, depth = 0) {
|
|
|
71
71
|
return node;
|
|
72
72
|
}
|
|
73
73
|
export function mediaKind(mediaType) {
|
|
74
|
-
const type = mediaType
|
|
74
|
+
const type = baseMediaType(mediaType);
|
|
75
75
|
if (type === "application/json" || type.endsWith("+json"))
|
|
76
76
|
return "json";
|
|
77
77
|
if (type === "multipart/form-data")
|
|
@@ -309,49 +309,12 @@ export function classifyResponses(operation) {
|
|
|
309
309
|
(responses.length === 0 ? responses : errors).push(fallback);
|
|
310
310
|
return { responses, errors };
|
|
311
311
|
}
|
|
312
|
-
/**
|
|
312
|
+
/** The one primitive a schema holds, leaving `null` aside. */
|
|
313
313
|
export function schemaPrimitiveKind(doc, node) {
|
|
314
|
-
const
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
.filter((member) => resolveRef(doc, member)?.type !== "null")
|
|
321
|
-
.map((member) => schemaPrimitiveKind(doc, member)));
|
|
322
|
-
const [only] = kinds;
|
|
323
|
-
return kinds.size === 1 ? only : "other";
|
|
324
|
-
}
|
|
325
|
-
const kinds = new Set();
|
|
326
|
-
const collect = (value) => {
|
|
327
|
-
if (value === null)
|
|
328
|
-
return;
|
|
329
|
-
if (typeof value === "string")
|
|
330
|
-
kinds.add("string");
|
|
331
|
-
else if (typeof value === "number")
|
|
332
|
-
kinds.add("number");
|
|
333
|
-
else
|
|
334
|
-
kinds.add("other");
|
|
335
|
-
};
|
|
336
|
-
if (Array.isArray(schema.enum)) {
|
|
337
|
-
for (const value of schema.enum)
|
|
338
|
-
collect(value);
|
|
339
|
-
}
|
|
340
|
-
else {
|
|
341
|
-
const types = (Array.isArray(schema.type) ? schema.type : [schema.type]).filter((type) => type !== "null");
|
|
342
|
-
for (const type of types) {
|
|
343
|
-
if (type === "string")
|
|
344
|
-
kinds.add("string");
|
|
345
|
-
else if (type === "number" || type === "integer")
|
|
346
|
-
kinds.add("number");
|
|
347
|
-
else
|
|
348
|
-
kinds.add("other");
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
if (kinds.size === 1) {
|
|
352
|
-
const [only] = kinds;
|
|
353
|
-
if (only === "string" || only === "number")
|
|
354
|
-
return only;
|
|
355
|
-
}
|
|
356
|
-
return "other";
|
|
314
|
+
const kinds = schemaKinds(doc, node);
|
|
315
|
+
kinds.delete("null");
|
|
316
|
+
const [only] = kinds;
|
|
317
|
+
return kinds.size === 1 && (only === "string" || only === "number")
|
|
318
|
+
? only
|
|
319
|
+
: "other";
|
|
357
320
|
}
|
|
@@ -443,6 +443,15 @@ async function getAuthToken(
|
|
|
443
443
|
return token;
|
|
444
444
|
}
|
|
445
445
|
|
|
446
|
+
/** The key a header is stored under, however its name is written. */
|
|
447
|
+
function findHeader(
|
|
448
|
+
headers: Record<string, unknown>,
|
|
449
|
+
name: string,
|
|
450
|
+
): string | undefined {
|
|
451
|
+
const lower = name.toLowerCase();
|
|
452
|
+
return Object.keys(headers).find((key) => key.toLowerCase() === lower);
|
|
453
|
+
}
|
|
454
|
+
|
|
446
455
|
/** True when the caller already supplied this API key on the request. */
|
|
447
456
|
function isProvided(
|
|
448
457
|
auth: Auth,
|
|
@@ -452,8 +461,7 @@ function isProvided(
|
|
|
452
461
|
const { name } = auth;
|
|
453
462
|
if (!name) return false;
|
|
454
463
|
if (auth.in === "query") return query?.[name] != null;
|
|
455
|
-
const
|
|
456
|
-
const key = Object.keys(headers).find((item) => item.toLowerCase() === lower);
|
|
464
|
+
const key = findHeader(headers, auth.in === "cookie" ? "Cookie" : name);
|
|
457
465
|
if (!key) return false;
|
|
458
466
|
return (
|
|
459
467
|
auth.in !== "cookie" ||
|
|
@@ -484,8 +492,9 @@ async function applySecurity(
|
|
|
484
492
|
if (auth.in === "query") {
|
|
485
493
|
options.query = { ...options.query, [name]: token };
|
|
486
494
|
} else if (auth.in === "cookie") {
|
|
487
|
-
|
|
488
|
-
|
|
495
|
+
const key = findHeader(headers, "Cookie") ?? "Cookie";
|
|
496
|
+
headers[key] = headers[key]
|
|
497
|
+
? `${headers[key]}; ${name}=${token}`
|
|
489
498
|
: `${name}=${token}`;
|
|
490
499
|
} else {
|
|
491
500
|
headers[name] = token;
|
|
@@ -621,22 +630,18 @@ export const createClient = (config: Config = {}): Client => {
|
|
|
621
630
|
await applySecurity(options, headers);
|
|
622
631
|
|
|
623
632
|
const data = serializeBody(options);
|
|
624
|
-
const hasContentType = Object.keys(headers).some(
|
|
625
|
-
(key) => key.toLowerCase() === "content-type",
|
|
626
|
-
);
|
|
627
633
|
if (
|
|
628
634
|
data !== undefined &&
|
|
629
635
|
options.bodyType === "json" &&
|
|
630
|
-
!
|
|
636
|
+
!findHeader(headers, "Content-Type")
|
|
631
637
|
) {
|
|
632
638
|
headers["Content-Type"] = "application/json";
|
|
633
639
|
}
|
|
634
640
|
|
|
635
641
|
// A header this request removed must not come back from the defaults.
|
|
636
642
|
if (client === instance) {
|
|
637
|
-
const sent = Object.keys(headers).map((key) => key.toLowerCase());
|
|
638
643
|
for (const key of defaultHeaders) {
|
|
639
|
-
if (
|
|
644
|
+
if (findHeader(headers, key) === undefined) headers[key] = null;
|
|
640
645
|
}
|
|
641
646
|
}
|
|
642
647
|
|
package/dist/client/schema.d.mts
CHANGED
|
@@ -13,6 +13,8 @@ export interface SchemaCtx {
|
|
|
13
13
|
writableNames?: Map<string, string>;
|
|
14
14
|
/** Type `date` and `date-time` strings as `Date` (`--useDateType`). */
|
|
15
15
|
dates?: boolean;
|
|
16
|
+
/** Called with the name of every component type the rendered type refers to. */
|
|
17
|
+
onRef?: (typeName: string) => void;
|
|
16
18
|
}
|
|
17
19
|
/** Name of the type generated for `#/components/schemas/<key>`, if any. */
|
|
18
20
|
export declare function componentSchemaKey(ref: string): string | undefined;
|
|
@@ -30,6 +32,26 @@ type Seen = ReadonlySet<string>;
|
|
|
30
32
|
* mapping or else its name, so that a union of subtypes narrows on it.
|
|
31
33
|
*/
|
|
32
34
|
export declare function renderComponentSchema(schema: Json, key: string, ctx: SchemaCtx): string;
|
|
35
|
+
/**
|
|
36
|
+
* Whether a schema that is composed of others (`allOf`, `oneOf`, `anyOf`) also
|
|
37
|
+
* describes an object itself, which its members are then intersected with.
|
|
38
|
+
*/
|
|
39
|
+
export declare function hasObjectKeywords(schema: Json): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* A kind of value, as far as generated code tells values apart. `other` is a
|
|
42
|
+
* `type` this generator does not know, which is none of the rest.
|
|
43
|
+
*/
|
|
44
|
+
export type SchemaKind = "string" | "number" | "boolean" | "null" | "array" | "object" | "other";
|
|
45
|
+
/**
|
|
46
|
+
* The kinds of value a schema allows: every kind when nothing narrows it, and
|
|
47
|
+
* none for `false`. This is the one reading of `type`, `enum`, `const`, the
|
|
48
|
+
* keywords that imply a type and the compositions; whatever asks what a schema
|
|
49
|
+
* can hold (how a parameter is serialized, what a page parameter starts at,
|
|
50
|
+
* how the members of a union are told apart) goes by it.
|
|
51
|
+
*
|
|
52
|
+
* `nullable` adds nothing: no caller tells a value from its absence by it.
|
|
53
|
+
*/
|
|
54
|
+
export declare function schemaKinds(doc: Doc, schema: Json, depth?: number): Set<SchemaKind>;
|
|
33
55
|
/** Render the TypeScript type for an OpenAPI schema object. */
|
|
34
56
|
export declare function renderSchema(schema: Json, ctx: SchemaCtx, indent?: string, seen?: Seen): string;
|
|
35
57
|
/** Whether a string schema holds a date, which `--useDateType` makes a `Date`. */
|
|
@@ -43,8 +65,8 @@ export declare function discriminatorValues(member: Json, discriminator: Json, c
|
|
|
43
65
|
export declare function hasFlag(doc: Doc, property: Json, flag: "readOnly" | "writeOnly"): boolean;
|
|
44
66
|
/**
|
|
45
67
|
* Component schemas that need a separate request-side type: the ones with a
|
|
46
|
-
* `readOnly`/`writeOnly` property, directly
|
|
47
|
-
* reference.
|
|
68
|
+
* `readOnly`/`writeOnly` property, directly, in what they inline, or through
|
|
69
|
+
* the schemas they reference.
|
|
48
70
|
*/
|
|
49
71
|
export declare function findReadWriteOnlyKeys(doc: Doc): Set<string>;
|
|
50
72
|
export {};
|
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,
|