@7nohe/openapi-react-query-codegen 3.2.1 → 4.0.0-beta.3
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 +22 -19
- package/README.md +5 -4
- package/dist/cli.d.mts +6 -1
- package/dist/client/emitSchemas.d.mts +9 -0
- package/dist/client/emitSchemas.mjs +130 -0
- package/dist/client/emitSdk.d.mts +18 -0
- package/dist/client/emitSdk.mjs +253 -0
- package/dist/client/emitTransformers.d.mts +19 -0
- package/dist/client/emitTransformers.mjs +556 -0
- package/dist/client/emitTypes.d.mts +16 -0
- package/dist/client/emitTypes.mjs +204 -0
- package/dist/client/index.d.mts +44 -0
- package/dist/client/index.mjs +126 -0
- package/dist/client/naming.d.mts +41 -0
- package/dist/client/naming.mjs +175 -0
- package/dist/client/operations.d.mts +103 -0
- package/dist/client/operations.mjs +352 -0
- package/dist/client/runtime/axios.runtime.ts +706 -0
- package/dist/client/runtime/fetch.runtime.ts +795 -0
- package/dist/client/schema.d.mts +50 -0
- package/dist/client/schema.mjs +648 -0
- package/dist/client/spec.d.mts +40 -0
- package/dist/client/spec.mjs +387 -0
- package/dist/client/tsconfig.d.mts +12 -0
- package/dist/client/tsconfig.mjs +152 -0
- package/dist/common.d.mts +0 -38
- package/dist/common.mjs +0 -87
- package/dist/constants.d.mts +0 -2
- package/dist/constants.mjs +0 -2
- package/dist/createSource.d.mts +8 -7
- package/dist/createSource.mjs +14 -17
- package/dist/format.mjs +0 -1
- package/dist/generate.d.mts +9 -9
- package/dist/generate.mjs +45 -107
- package/dist/runCli.mjs +7 -2
- package/dist/tsmorph/buildQueryHooks.mjs +19 -11
- package/dist/tsmorph/generateFiles.mjs +105 -161
- package/dist/tsmorph/operationNames.d.mts +2 -13
- package/dist/tsmorph/operationNames.mjs +4 -24
- package/dist/tsmorph/projectFactory.d.mts +7 -2
- package/dist/tsmorph/projectFactory.mjs +16 -9
- package/dist/types.d.mts +14 -26
- package/package.json +7 -8
- package/dist/parseOperations.d.mts +0 -10
- package/dist/parseOperations.mjs +0 -164
- package/dist/service.d.mts +0 -8
- package/dist/service.mjs +0 -98
- package/dist/util.d.mts +0 -2
- package/dist/util.mjs +0 -16
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type Doc, type Json } from "./spec.mjs";
|
|
2
|
+
declare const HTTP_METHODS: readonly ["get", "put", "post", "delete", "options", "head", "patch", "trace"];
|
|
3
|
+
type HttpMethod = (typeof HTTP_METHODS)[number];
|
|
4
|
+
type ParameterLocation = "path" | "query" | "header";
|
|
5
|
+
export interface ParameterIR {
|
|
6
|
+
name: string;
|
|
7
|
+
in: ParameterLocation;
|
|
8
|
+
required: boolean;
|
|
9
|
+
schema: Json;
|
|
10
|
+
description?: string;
|
|
11
|
+
deprecated?: boolean;
|
|
12
|
+
style?: string;
|
|
13
|
+
explode?: boolean;
|
|
14
|
+
/** Declared with `content` as JSON, so the value is sent as a JSON text. */
|
|
15
|
+
json?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export type BodyKind = "json" | "form-data" | "urlencoded" | "text" | "binary";
|
|
18
|
+
export interface BodyIR {
|
|
19
|
+
required: boolean;
|
|
20
|
+
description?: string;
|
|
21
|
+
mediaType: string;
|
|
22
|
+
kind: BodyKind;
|
|
23
|
+
schema: Json;
|
|
24
|
+
}
|
|
25
|
+
export interface ResponseIR {
|
|
26
|
+
/** `200`, `404`, `2XX` or `default` */
|
|
27
|
+
status: string;
|
|
28
|
+
description?: string;
|
|
29
|
+
schema?: Json;
|
|
30
|
+
/** Every media type the response declares. */
|
|
31
|
+
mediaTypes: string[];
|
|
32
|
+
}
|
|
33
|
+
export interface SecurityIR {
|
|
34
|
+
type: "http" | "apiKey";
|
|
35
|
+
scheme?: "basic" | "bearer";
|
|
36
|
+
in?: "header" | "query" | "cookie";
|
|
37
|
+
name?: string;
|
|
38
|
+
/** The scheme's name, when another scheme would look the same at runtime. */
|
|
39
|
+
key?: string;
|
|
40
|
+
}
|
|
41
|
+
export interface OperationTypeNames {
|
|
42
|
+
data: string;
|
|
43
|
+
errors: string;
|
|
44
|
+
error: string;
|
|
45
|
+
responses: string;
|
|
46
|
+
response: string;
|
|
47
|
+
}
|
|
48
|
+
export interface OperationIR {
|
|
49
|
+
/** Generated function name, e.g. `findPets` */
|
|
50
|
+
functionName: string;
|
|
51
|
+
/**
|
|
52
|
+
* Names of the operation's generated types. Each is the capitalized function
|
|
53
|
+
* name plus a suffix; a name that collides with a schema or another
|
|
54
|
+
* operation's type gets a number appended on its own (`ListRunsResponse2`),
|
|
55
|
+
* so the other names stay what users expect.
|
|
56
|
+
*/
|
|
57
|
+
typeNames: OperationTypeNames;
|
|
58
|
+
method: HttpMethod;
|
|
59
|
+
path: string;
|
|
60
|
+
parameters: ParameterIR[];
|
|
61
|
+
body?: BodyIR;
|
|
62
|
+
responses: ResponseIR[];
|
|
63
|
+
/**
|
|
64
|
+
* Alternative ways to authenticate. Any one of them is enough, and each lists
|
|
65
|
+
* the schemes it needs together.
|
|
66
|
+
*/
|
|
67
|
+
security: SecurityIR[][];
|
|
68
|
+
/** `/** ... *\/` block, or undefined when the operation has no docs */
|
|
69
|
+
jsDoc?: string;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Follow a `$ref` that points outside `components.schemas` (parameters,
|
|
73
|
+
* request bodies, responses, path items …). Schema refs are kept as they are.
|
|
74
|
+
*/
|
|
75
|
+
export declare function deref(doc: Doc, node: Json, depth?: number): Json;
|
|
76
|
+
export declare function mediaKind(mediaType: string): BodyKind;
|
|
77
|
+
/** The media type a body is typed from: JSON when offered, else the first. */
|
|
78
|
+
export declare function pickContent(content: Record<string, Json> | undefined): {
|
|
79
|
+
mediaType: string;
|
|
80
|
+
media: Json;
|
|
81
|
+
} | undefined;
|
|
82
|
+
/** Walk `paths` and normalise every operation. */
|
|
83
|
+
export declare function collectOperations(doc: Doc, options?: {
|
|
84
|
+
useOperationId?: boolean;
|
|
85
|
+
reservedTypeNames?: Iterable<string>;
|
|
86
|
+
}): OperationIR[];
|
|
87
|
+
/** Names of the request-side variants (`PetWritable`) of schemas that need one. */
|
|
88
|
+
export declare function buildWritableNames(doc: Doc, names: Map<string, string>): Map<string, string>;
|
|
89
|
+
/**
|
|
90
|
+
* Type names for `components.requestBodies`. They are claimed after every
|
|
91
|
+
* other name, so a schema or an operation never gives its name up to one.
|
|
92
|
+
*/
|
|
93
|
+
export declare function buildRequestBodyNames(doc: Doc, taken: Iterable<string>): Map<string, string>;
|
|
94
|
+
/** Map every component schema key to its generated type name. */
|
|
95
|
+
export declare function buildSchemaNames(doc: Doc): Map<string, string>;
|
|
96
|
+
/** Split responses into the ones exposed as data and as errors. */
|
|
97
|
+
export declare function classifyResponses(operation: OperationIR): {
|
|
98
|
+
responses: ResponseIR[];
|
|
99
|
+
errors: ResponseIR[];
|
|
100
|
+
};
|
|
101
|
+
/** Resolve a schema (following `$ref` chains) down to its leading primitive. */
|
|
102
|
+
export declare function schemaPrimitiveKind(doc: Doc, node: Json): "string" | "number" | "other";
|
|
103
|
+
export {};
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { nameFromPath, toFunctionName, toTypeName, uniqueName, } from "./naming.mjs";
|
|
2
|
+
import { docComment, findReadWriteOnlyKeys } from "./schema.mjs";
|
|
3
|
+
import { getByPointer, resolveRef } from "./spec.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Type names a schema or an operation type cannot take: `ClientOptions` and
|
|
6
|
+
* `Options` are exported next to them, and the rest are what the generated hook
|
|
7
|
+
* files import or use, where an imported model of the same name would clash
|
|
8
|
+
* with or shadow them.
|
|
9
|
+
*/
|
|
10
|
+
const RESERVED_TYPE_NAMES = [
|
|
11
|
+
"ClientOptions",
|
|
12
|
+
"Options",
|
|
13
|
+
"Common",
|
|
14
|
+
"AxiosError",
|
|
15
|
+
"EnsureQueryDataOptions",
|
|
16
|
+
"FetchInfiniteQueryOptions",
|
|
17
|
+
"FetchQueryOptions",
|
|
18
|
+
"GetNextPageParamFunction",
|
|
19
|
+
"InfiniteData",
|
|
20
|
+
"QueryClient",
|
|
21
|
+
"UseInfiniteQueryOptions",
|
|
22
|
+
"UseMutationOptions",
|
|
23
|
+
"UseMutationResult",
|
|
24
|
+
"UseQueryOptions",
|
|
25
|
+
"UseQueryResult",
|
|
26
|
+
"UseSuspenseInfiniteQueryOptions",
|
|
27
|
+
"UseSuspenseQueryOptions",
|
|
28
|
+
"Array",
|
|
29
|
+
"Awaited",
|
|
30
|
+
"NonNullable",
|
|
31
|
+
"Omit",
|
|
32
|
+
"Partial",
|
|
33
|
+
"Pick",
|
|
34
|
+
"Promise",
|
|
35
|
+
"Record",
|
|
36
|
+
"ReturnType",
|
|
37
|
+
];
|
|
38
|
+
const HTTP_METHODS = [
|
|
39
|
+
"get",
|
|
40
|
+
"put",
|
|
41
|
+
"post",
|
|
42
|
+
"delete",
|
|
43
|
+
"options",
|
|
44
|
+
"head",
|
|
45
|
+
"patch",
|
|
46
|
+
"trace",
|
|
47
|
+
];
|
|
48
|
+
function isHttpMethod(value) {
|
|
49
|
+
return HTTP_METHODS.includes(value);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Follow a `$ref` that points outside `components.schemas` (parameters,
|
|
53
|
+
* request bodies, responses, path items …). Schema refs are kept as they are.
|
|
54
|
+
*/
|
|
55
|
+
export function deref(doc, node, depth = 0) {
|
|
56
|
+
if (node &&
|
|
57
|
+
typeof node === "object" &&
|
|
58
|
+
typeof node.$ref === "string" &&
|
|
59
|
+
depth < 32) {
|
|
60
|
+
const target = getByPointer(doc, node.$ref);
|
|
61
|
+
if (target === undefined) {
|
|
62
|
+
throw new Error(`Unresolvable $ref: ${node.$ref}`);
|
|
63
|
+
}
|
|
64
|
+
return deref(doc, target, depth + 1);
|
|
65
|
+
}
|
|
66
|
+
return node;
|
|
67
|
+
}
|
|
68
|
+
export function mediaKind(mediaType) {
|
|
69
|
+
const type = mediaType.toLowerCase().split(";")[0].trim();
|
|
70
|
+
if (type === "application/json" || type.endsWith("+json"))
|
|
71
|
+
return "json";
|
|
72
|
+
if (type === "multipart/form-data")
|
|
73
|
+
return "form-data";
|
|
74
|
+
if (type === "application/x-www-form-urlencoded")
|
|
75
|
+
return "urlencoded";
|
|
76
|
+
if (type.startsWith("text/"))
|
|
77
|
+
return "text";
|
|
78
|
+
return "binary";
|
|
79
|
+
}
|
|
80
|
+
/** The media type a body is typed from: JSON when offered, else the first. */
|
|
81
|
+
export function pickContent(content) {
|
|
82
|
+
if (!content)
|
|
83
|
+
return undefined;
|
|
84
|
+
const entries = Object.entries(content);
|
|
85
|
+
if (entries.length === 0)
|
|
86
|
+
return undefined;
|
|
87
|
+
const json = entries.find(([type]) => mediaKind(type) === "json");
|
|
88
|
+
const [mediaType, media] = json ?? entries[0];
|
|
89
|
+
return { mediaType, media };
|
|
90
|
+
}
|
|
91
|
+
function buildJsDoc(summary, description, deprecated) {
|
|
92
|
+
const text = description ?? summary;
|
|
93
|
+
const block = docComment({ description: text, deprecated });
|
|
94
|
+
return block ? block.trimEnd() : undefined;
|
|
95
|
+
}
|
|
96
|
+
function mergeParameters(doc, pathItem, operation) {
|
|
97
|
+
const merged = new Map();
|
|
98
|
+
for (const list of [pathItem.parameters, operation.parameters]) {
|
|
99
|
+
if (!Array.isArray(list))
|
|
100
|
+
continue;
|
|
101
|
+
for (const raw of list) {
|
|
102
|
+
const parameter = deref(doc, raw);
|
|
103
|
+
// Nothing can send a cookie parameter: the SDK has no option for it.
|
|
104
|
+
if (!parameter?.name || !parameter.in || parameter.in === "cookie") {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const location = parameter.in;
|
|
108
|
+
const content = parameter.schema
|
|
109
|
+
? undefined
|
|
110
|
+
: pickContent(parameter.content);
|
|
111
|
+
merged.set(`${location}:${parameter.name}`, {
|
|
112
|
+
name: parameter.name,
|
|
113
|
+
in: location,
|
|
114
|
+
required: location === "path" ? true : parameter.required === true,
|
|
115
|
+
schema: parameter.schema ?? content?.media?.schema,
|
|
116
|
+
description: parameter.description,
|
|
117
|
+
deprecated: parameter.deprecated === true,
|
|
118
|
+
style: parameter.style,
|
|
119
|
+
explode: parameter.explode,
|
|
120
|
+
json: content !== undefined && mediaKind(content.mediaType) === "json",
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return [...merged.values()];
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* The scheme as a request carries it. The others (`mutualTLS`, HTTP `digest` and
|
|
128
|
+
* so on) happen outside the SDK, and must not be sent as a bearer token.
|
|
129
|
+
*/
|
|
130
|
+
function toSecurityIR(scheme) {
|
|
131
|
+
switch (scheme?.type) {
|
|
132
|
+
case "apiKey":
|
|
133
|
+
return { type: "apiKey", in: scheme.in, name: scheme.name };
|
|
134
|
+
case "oauth2":
|
|
135
|
+
case "openIdConnect":
|
|
136
|
+
return { type: "http", scheme: "bearer" };
|
|
137
|
+
case "http": {
|
|
138
|
+
const name = String(scheme.scheme).toLowerCase();
|
|
139
|
+
return name === "basic" || name === "bearer"
|
|
140
|
+
? { type: "http", scheme: name }
|
|
141
|
+
: undefined;
|
|
142
|
+
}
|
|
143
|
+
default:
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Schemes that would look the same at runtime, which need their name for an
|
|
149
|
+
* `auth` callback to tell them apart.
|
|
150
|
+
*/
|
|
151
|
+
function findAmbiguousSchemes(doc) {
|
|
152
|
+
const namesByShape = new Map();
|
|
153
|
+
for (const [name, raw] of Object.entries((doc.components?.securitySchemes ?? {}))) {
|
|
154
|
+
const shape = JSON.stringify(toSecurityIR(deref(doc, raw)));
|
|
155
|
+
namesByShape.set(shape, [...(namesByShape.get(shape) ?? []), name]);
|
|
156
|
+
}
|
|
157
|
+
return new Set([...namesByShape.values()].filter((names) => names.length > 1).flat());
|
|
158
|
+
}
|
|
159
|
+
function collectSecurity(doc, requirements, ambiguous) {
|
|
160
|
+
const schemes = doc.components?.securitySchemes ?? {};
|
|
161
|
+
const result = [];
|
|
162
|
+
const seen = new Set();
|
|
163
|
+
for (const requirement of Array.isArray(requirements) ? requirements : []) {
|
|
164
|
+
const group = [];
|
|
165
|
+
for (const name of Object.keys(requirement ?? {})) {
|
|
166
|
+
const entry = toSecurityIR(deref(doc, schemes[name]));
|
|
167
|
+
if (entry)
|
|
168
|
+
group.push(ambiguous.has(name) ? { ...entry, key: name } : entry);
|
|
169
|
+
}
|
|
170
|
+
const id = JSON.stringify(group);
|
|
171
|
+
if (group.length > 0 && !seen.has(id)) {
|
|
172
|
+
seen.add(id);
|
|
173
|
+
result.push(group);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
/** Claim a distinct name for each of the operation's types. */
|
|
179
|
+
function claimTypeNames(stem, used) {
|
|
180
|
+
return {
|
|
181
|
+
data: uniqueName(`${stem}Data`, used),
|
|
182
|
+
errors: uniqueName(`${stem}Errors`, used),
|
|
183
|
+
error: uniqueName(`${stem}Error`, used),
|
|
184
|
+
responses: uniqueName(`${stem}Responses`, used),
|
|
185
|
+
response: uniqueName(`${stem}Response`, used),
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
/** Walk `paths` and normalise every operation. */
|
|
189
|
+
export function collectOperations(doc, options = {}) {
|
|
190
|
+
const useOperationId = options.useOperationId ?? true;
|
|
191
|
+
const operations = [];
|
|
192
|
+
// Functions and types share one set: `index.ts` re-exports both, and
|
|
193
|
+
// digit-leading names (`_1Abc`) look the same in either case.
|
|
194
|
+
const usedNames = new Set([
|
|
195
|
+
...RESERVED_TYPE_NAMES,
|
|
196
|
+
...(options.reservedTypeNames ?? []),
|
|
197
|
+
]);
|
|
198
|
+
const paths = doc.paths ?? {};
|
|
199
|
+
const ambiguousSchemes = findAmbiguousSchemes(doc);
|
|
200
|
+
for (const [pathKey, rawPathItem] of Object.entries(paths)) {
|
|
201
|
+
const pathItem = deref(doc, rawPathItem) ?? {};
|
|
202
|
+
for (const method of Object.keys(pathItem)) {
|
|
203
|
+
if (!isHttpMethod(method))
|
|
204
|
+
continue;
|
|
205
|
+
const operation = pathItem[method];
|
|
206
|
+
if (!operation || typeof operation !== "object")
|
|
207
|
+
continue;
|
|
208
|
+
const baseName = useOperationId && operation.operationId
|
|
209
|
+
? String(operation.operationId)
|
|
210
|
+
: nameFromPath(method, pathKey);
|
|
211
|
+
const functionName = uniqueName(toFunctionName(baseName), usedNames);
|
|
212
|
+
const typeNames = claimTypeNames(toTypeName(functionName), usedNames);
|
|
213
|
+
const parameters = mergeParameters(doc, pathItem, operation);
|
|
214
|
+
let body;
|
|
215
|
+
const requestBody = deref(doc, operation.requestBody);
|
|
216
|
+
const bodyContent = pickContent(requestBody?.content);
|
|
217
|
+
if (requestBody && bodyContent) {
|
|
218
|
+
body = {
|
|
219
|
+
required: requestBody.required === true,
|
|
220
|
+
description: requestBody.description,
|
|
221
|
+
mediaType: bodyContent.mediaType,
|
|
222
|
+
kind: mediaKind(bodyContent.mediaType),
|
|
223
|
+
schema: bodyContent.media?.schema,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
const responses = [];
|
|
227
|
+
for (const [status, rawResponse] of Object.entries((operation.responses ?? {}))) {
|
|
228
|
+
const response = deref(doc, rawResponse) ?? {};
|
|
229
|
+
const content = pickContent(response.content);
|
|
230
|
+
responses.push({
|
|
231
|
+
status: status.toUpperCase() === "DEFAULT"
|
|
232
|
+
? "default"
|
|
233
|
+
: status.toUpperCase(),
|
|
234
|
+
description: response.description,
|
|
235
|
+
schema: content?.media?.schema,
|
|
236
|
+
mediaTypes: Object.keys(response.content ?? {}),
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
const deprecated = operation.deprecated === true;
|
|
240
|
+
operations.push({
|
|
241
|
+
functionName,
|
|
242
|
+
typeNames,
|
|
243
|
+
method,
|
|
244
|
+
path: pathKey,
|
|
245
|
+
parameters,
|
|
246
|
+
body,
|
|
247
|
+
responses,
|
|
248
|
+
security: collectSecurity(doc, operation.security ?? doc.security, ambiguousSchemes),
|
|
249
|
+
jsDoc: buildJsDoc(operation.summary, operation.description, deprecated),
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
return operations;
|
|
254
|
+
}
|
|
255
|
+
/** Names of the request-side variants (`PetWritable`) of schemas that need one. */
|
|
256
|
+
export function buildWritableNames(doc, names) {
|
|
257
|
+
const used = new Set([...RESERVED_TYPE_NAMES, ...names.values()]);
|
|
258
|
+
const writable = new Map();
|
|
259
|
+
for (const key of findReadWriteOnlyKeys(doc)) {
|
|
260
|
+
writable.set(key, uniqueName(`${names.get(key) ?? toTypeName(key)}Writable`, used));
|
|
261
|
+
}
|
|
262
|
+
return writable;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Type names for `components.requestBodies`. They are claimed after every
|
|
266
|
+
* other name, so a schema or an operation never gives its name up to one.
|
|
267
|
+
*/
|
|
268
|
+
export function buildRequestBodyNames(doc, taken) {
|
|
269
|
+
const used = new Set([...RESERVED_TYPE_NAMES, ...taken]);
|
|
270
|
+
const names = new Map();
|
|
271
|
+
for (const key of Object.keys(doc.components?.requestBodies ?? {})) {
|
|
272
|
+
names.set(key, uniqueName(toTypeName(key), used));
|
|
273
|
+
}
|
|
274
|
+
return names;
|
|
275
|
+
}
|
|
276
|
+
/** Map every component schema key to its generated type name. */
|
|
277
|
+
export function buildSchemaNames(doc) {
|
|
278
|
+
const names = new Map();
|
|
279
|
+
const used = new Set(RESERVED_TYPE_NAMES);
|
|
280
|
+
for (const key of Object.keys(doc.components?.schemas ?? {})) {
|
|
281
|
+
names.set(key, uniqueName(toTypeName(key), used));
|
|
282
|
+
}
|
|
283
|
+
return names;
|
|
284
|
+
}
|
|
285
|
+
/** Split responses into the ones exposed as data and as errors. */
|
|
286
|
+
export function classifyResponses(operation) {
|
|
287
|
+
const responses = [];
|
|
288
|
+
const errors = [];
|
|
289
|
+
for (const response of operation.responses) {
|
|
290
|
+
if (response.status === "default")
|
|
291
|
+
continue;
|
|
292
|
+
// 2xx carries the data the caller asked for and 4xx/5xx the error. Other
|
|
293
|
+
// statuses (a 304 on a conditional request, say) are neither, and listing
|
|
294
|
+
// them would only widen `data` and the error type.
|
|
295
|
+
if (/^2/.test(response.status))
|
|
296
|
+
responses.push(response);
|
|
297
|
+
else if (/^[45]/.test(response.status))
|
|
298
|
+
errors.push(response);
|
|
299
|
+
}
|
|
300
|
+
const fallback = operation.responses.find((response) => response.status === "default");
|
|
301
|
+
// `default` stands for whatever is not listed: the data when no 2xx is, and
|
|
302
|
+
// an error otherwise.
|
|
303
|
+
if (fallback)
|
|
304
|
+
(responses.length === 0 ? responses : errors).push(fallback);
|
|
305
|
+
return { responses, errors };
|
|
306
|
+
}
|
|
307
|
+
/** Resolve a schema (following `$ref` chains) down to its leading primitive. */
|
|
308
|
+
export function schemaPrimitiveKind(doc, node) {
|
|
309
|
+
const schema = resolveRef(doc, node);
|
|
310
|
+
if (!schema || typeof schema !== "object")
|
|
311
|
+
return "other";
|
|
312
|
+
const alternatives = schema.anyOf ?? schema.oneOf ?? schema.allOf;
|
|
313
|
+
if (Array.isArray(alternatives)) {
|
|
314
|
+
const kinds = new Set(alternatives
|
|
315
|
+
.filter((member) => resolveRef(doc, member)?.type !== "null")
|
|
316
|
+
.map((member) => schemaPrimitiveKind(doc, member)));
|
|
317
|
+
const [only] = kinds;
|
|
318
|
+
return kinds.size === 1 ? only : "other";
|
|
319
|
+
}
|
|
320
|
+
const kinds = new Set();
|
|
321
|
+
const collect = (value) => {
|
|
322
|
+
if (value === null)
|
|
323
|
+
return;
|
|
324
|
+
if (typeof value === "string")
|
|
325
|
+
kinds.add("string");
|
|
326
|
+
else if (typeof value === "number")
|
|
327
|
+
kinds.add("number");
|
|
328
|
+
else
|
|
329
|
+
kinds.add("other");
|
|
330
|
+
};
|
|
331
|
+
if (Array.isArray(schema.enum)) {
|
|
332
|
+
for (const value of schema.enum)
|
|
333
|
+
collect(value);
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
const types = (Array.isArray(schema.type) ? schema.type : [schema.type]).filter((type) => type !== "null");
|
|
337
|
+
for (const type of types) {
|
|
338
|
+
if (type === "string")
|
|
339
|
+
kinds.add("string");
|
|
340
|
+
else if (type === "number" || type === "integer")
|
|
341
|
+
kinds.add("number");
|
|
342
|
+
else
|
|
343
|
+
kinds.add("other");
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
if (kinds.size === 1) {
|
|
347
|
+
const [only] = kinds;
|
|
348
|
+
if (only === "string" || only === "number")
|
|
349
|
+
return only;
|
|
350
|
+
}
|
|
351
|
+
return "other";
|
|
352
|
+
}
|