@orpc/openapi 2.0.0-beta.49 → 2.0.0-beta.50
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/dist/adapters/aws-lambda/index.d.ts +3 -15
- package/dist/adapters/aws-lambda/index.js +1 -1
- package/dist/adapters/aws-lambda/openapi-handler.d.ts +8 -9
- package/dist/adapters/aws-lambda/openapi-handler.js +19 -17
- package/dist/adapters/fastify/index.d.ts +2 -1
- package/dist/adapters/fastify/index.js +1 -1
- package/dist/adapters/fastify/openapi-handler.d.ts +8 -9
- package/dist/adapters/fastify/openapi-handler.js +16 -14
- package/dist/adapters/fetch/index.d.ts +3 -2
- package/dist/adapters/fetch/index.js +2 -2
- package/dist/adapters/fetch/openapi-handler.d.ts +8 -9
- package/dist/adapters/fetch/openapi-handler.js +16 -14
- package/dist/adapters/fetch/openapi-link.d.ts +9 -10
- package/dist/adapters/fetch/openapi-link.js +21 -19
- package/dist/adapters/node/index.d.ts +2 -1
- package/dist/adapters/node/index.js +1 -1
- package/dist/adapters/node/openapi-handler.d.ts +8 -9
- package/dist/adapters/node/openapi-handler.js +16 -14
- package/dist/adapters/standard/index.d.ts +4 -3
- package/dist/adapters/standard/index.js +3 -3
- package/dist/adapters/standard/openapi-handler-codec.d.ts +56 -57
- package/dist/adapters/standard/openapi-handler-codec.js +143 -205
- package/dist/adapters/standard/openapi-link-codec.d.ts +55 -59
- package/dist/adapters/standard/openapi-link-codec.js +262 -377
- package/dist/adapters/standard/openapi-matcher.d.ts +26 -26
- package/dist/adapters/standard/openapi-matcher.js +78 -108
- package/dist/adapters/standard/utils.js +28 -44
- package/dist/bracket-notation.d.ts +22 -22
- package/dist/bracket-notation.js +123 -173
- package/dist/client-factory.d.ts +12 -12
- package/dist/client-factory.js +13 -11
- package/dist/constants.d.ts +1 -4
- package/dist/constants.js +5 -4
- package/dist/extensions/route.d.ts +36 -36
- package/dist/extensions/route.js +7 -6
- package/dist/helpers/form-data.d.ts +1 -1
- package/dist/helpers/form-data.js +74 -86
- package/dist/helpers/index.d.ts +2 -1
- package/dist/helpers/index.js +1 -1
- package/dist/index.d.ts +15 -13
- package/dist/index.js +11 -11
- package/dist/meta.d.ts +298 -298
- package/dist/meta.js +54 -72
- package/dist/openapi-generator-components.d.ts +2 -31
- package/dist/openapi-generator-components.js +200 -248
- package/dist/openapi-generator-operation.d.ts +12 -33
- package/dist/openapi-generator-operation.js +369 -437
- package/dist/openapi-generator.d.ts +75 -80
- package/dist/openapi-generator.js +114 -142
- package/dist/openapi-json-serializer.d.ts +65 -65
- package/dist/openapi-json-serializer.js +218 -257
- package/dist/openapi-serializer.d.ts +31 -32
- package/dist/openapi-serializer.js +68 -92
- package/dist/plugins/index.d.ts +2 -1
- package/dist/plugins/index.js +1 -1
- package/dist/plugins/openapi-reference.d.ts +99 -102
- package/dist/plugins/openapi-reference.js +109 -132
- package/dist/router-utils.d.ts +7 -10
- package/dist/router-utils.js +30 -34
- package/dist/types.d.ts +8 -12
- package/dist/utils.d.ts +5 -10
- package/dist/utils.js +39 -42
- package/package.json +12 -12
- package/dist/adapters/standard/utils.d.ts +0 -4
- package/dist/types.js +0 -1
|
@@ -1,469 +1,401 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
1
|
+
import "./constants.js";
|
|
2
|
+
import { isBodylessMethod } from "./utils.js";
|
|
3
|
+
import { getOwn, stringifyJSON, value } from "@orpc/shared";
|
|
4
|
+
import { getAsyncIteratorObjectSchemasDetails } from "@orpc/contract";
|
|
5
|
+
import { combineJsonObjectSchemaEntries, combineJsonSchemasWithComposition, containsJsonFileSchema, extractJsonObjectSchemaAdditionalProperties, extractJsonObjectSchemaEntries, flattenJsonUnionSchema, isJsonFileSchema, isJsonPrimitiveSchema, isUnconstrainedSchema, matchArrayableJsonSchema, resolveJsonSchemaRootLocalRef } from "@orpc/json-schema";
|
|
6
|
+
import { DEFAULT_ERROR_STATUS, DEFAULT_SUCCESS_STATUS } from "@orpc/server";
|
|
7
|
+
// src/openapi-generator-operation.ts
|
|
8
|
+
var OpenAPIGeneratorError = class extends TypeError {};
|
|
9
|
+
function toOpenAPIPath(path, dynamicPathParams) {
|
|
10
|
+
if (!dynamicPathParams?.length) return path;
|
|
11
|
+
let normalized = "";
|
|
12
|
+
let currentIndex = 0;
|
|
13
|
+
for (const param of dynamicPathParams) {
|
|
14
|
+
normalized += path.slice(currentIndex, param.startIndex);
|
|
15
|
+
normalized += `{${param.parameterName}}`;
|
|
16
|
+
currentIndex = param.startIndex + param.segment.length;
|
|
17
|
+
}
|
|
18
|
+
normalized += path.slice(currentIndex);
|
|
19
|
+
return normalized;
|
|
8
20
|
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
35
|
-
return;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
const [schema, optional] = ctx.convertSchemas(def.inputSchemas, 'input');
|
|
39
|
-
if (isUnconstrainedSchema(schema) && !dynamicParams?.length) {
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
|
-
const parts = inputStructure === 'compact'
|
|
43
|
-
? extractCompactRequestParts(schema, optional, method, dynamicParams)
|
|
44
|
-
: extractDetailedRequestParts(schema);
|
|
45
|
-
renderPathParameters(ctx, operation, dynamicParams, parts.paramsEntries, meta?.paramsStyles);
|
|
46
|
-
renderQueryParameters(ctx, operation, parts.queryEntries, meta?.queryStyles);
|
|
47
|
-
renderHeaderParameters(ctx, operation, parts.headersEntries);
|
|
48
|
-
if (parts.bodySchema !== undefined) {
|
|
49
|
-
operation.requestBody = {
|
|
50
|
-
required: parts.bodyOptional ? undefined : true,
|
|
51
|
-
content: toBodyContent(ctx, 'input', parts.bodySchema),
|
|
52
|
-
};
|
|
53
|
-
}
|
|
21
|
+
function buildRequest(ctx, operation, def, meta, dynamicPathParams) {
|
|
22
|
+
const method = meta?.method ?? "POST";
|
|
23
|
+
const inputStructure = meta?.inputStructure ?? "compact";
|
|
24
|
+
const dynamicParams = dynamicPathParams?.map((v) => v.parameterName);
|
|
25
|
+
if (inputStructure === "compact") {
|
|
26
|
+
const iteratorDetails = getAsyncIteratorObjectSchemasDetails(def.inputSchemas);
|
|
27
|
+
if (iteratorDetails) {
|
|
28
|
+
renderPathParameters(ctx, operation, dynamicParams, void 0, void 0);
|
|
29
|
+
operation.requestBody = {
|
|
30
|
+
required: true,
|
|
31
|
+
content: toAsyncIteratorObjectContent(ctx, "input", ctx.convertSchemas(iteratorDetails.yieldSchemas, "input"), ctx.convertSchemas(iteratorDetails.returnSchemas, "input"))
|
|
32
|
+
};
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const [schema, optional] = ctx.convertSchemas(def.inputSchemas, "input");
|
|
37
|
+
if (isUnconstrainedSchema(schema) && !dynamicParams?.length) return;
|
|
38
|
+
const parts = inputStructure === "compact" ? extractCompactRequestParts(schema, optional, method, dynamicParams) : extractDetailedRequestParts(schema);
|
|
39
|
+
renderPathParameters(ctx, operation, dynamicParams, parts.paramsEntries, meta?.paramsStyles);
|
|
40
|
+
renderQueryParameters(ctx, operation, parts.queryEntries, meta?.queryStyles);
|
|
41
|
+
renderHeaderParameters(ctx, operation, parts.headersEntries);
|
|
42
|
+
if (parts.bodySchema !== void 0) operation.requestBody = {
|
|
43
|
+
required: parts.bodyOptional ? void 0 : true,
|
|
44
|
+
content: toBodyContent(ctx, "input", parts.bodySchema)
|
|
45
|
+
};
|
|
54
46
|
}
|
|
55
47
|
function extractCompactRequestParts(schema, optional, method, dynamicParams) {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
? schema
|
|
69
|
-
: restEntries
|
|
70
|
-
? combineJsonObjectSchemaEntries(restEntries, extractJsonObjectSchemaAdditionalProperties(schema))
|
|
71
|
-
: undefined;
|
|
72
|
-
return {
|
|
73
|
-
paramsEntries,
|
|
74
|
-
queryEntries: isBodylessMethod(method) ? restEntries : undefined,
|
|
75
|
-
headersEntries: undefined,
|
|
76
|
-
bodySchema,
|
|
77
|
-
bodyOptional: !dynamicParams?.length
|
|
78
|
-
? optional
|
|
79
|
-
: restEntries?.every(([, , optional]) => optional),
|
|
80
|
-
};
|
|
48
|
+
const entries = extractJsonObjectSchemaEntries(schema);
|
|
49
|
+
if (!entries && isBodylessMethod(method)) throw new OpenAPIGeneratorError(`method is ${method} but the input schema is not an object.\n ${method} sends every input field as a query parameter, so the input must be an object schema.\n Fix: make the input an object, or use a method with a request body (POST, PUT, PATCH, DELETE).`);
|
|
50
|
+
const paramsEntries = entries?.filter(([name]) => dynamicParams?.includes(name));
|
|
51
|
+
const restEntries = entries?.filter(([name]) => !dynamicParams?.includes(name));
|
|
52
|
+
const bodySchema = !!isBodylessMethod(method) ? void 0 : !dynamicParams?.length ? schema : restEntries ? combineJsonObjectSchemaEntries(restEntries, extractJsonObjectSchemaAdditionalProperties(schema)) : void 0;
|
|
53
|
+
return {
|
|
54
|
+
paramsEntries,
|
|
55
|
+
queryEntries: isBodylessMethod(method) ? restEntries : void 0,
|
|
56
|
+
headersEntries: void 0,
|
|
57
|
+
bodySchema,
|
|
58
|
+
bodyOptional: !dynamicParams?.length ? optional : restEntries?.every(([, , optional]) => optional)
|
|
59
|
+
};
|
|
81
60
|
}
|
|
82
61
|
function extractDetailedRequestParts(schema) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
bodySchema: section('body')?.[1],
|
|
94
|
-
bodyOptional: section('body')?.[2],
|
|
95
|
-
};
|
|
62
|
+
const entries = extractJsonObjectSchemaEntries(schema);
|
|
63
|
+
if (!entries) throw new OpenAPIGeneratorError("inputStructure is \"detailed\" but the input schema is not an object.\n Expected an object shaped like: { params?: object, query?: object, headers?: object, body?: unknown }");
|
|
64
|
+
const section = (name) => entries.find(([entryName]) => entryName === name);
|
|
65
|
+
return {
|
|
66
|
+
paramsEntries: extractJsonObjectSchemaEntries(section("params")?.[1] ?? false),
|
|
67
|
+
queryEntries: extractJsonObjectSchemaEntries(section("query")?.[1] ?? false),
|
|
68
|
+
headersEntries: extractJsonObjectSchemaEntries(section("headers")?.[1] ?? false),
|
|
69
|
+
bodySchema: section("body")?.[1],
|
|
70
|
+
bodyOptional: section("body")?.[2]
|
|
71
|
+
};
|
|
96
72
|
}
|
|
97
73
|
function renderPathParameters(ctx, operation, dynamicParams, paramsEntries, paramsStyles) {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const parameter = {
|
|
119
|
-
in: 'path',
|
|
120
|
-
required: true,
|
|
121
|
-
name,
|
|
122
|
-
schema: ctx.registry.toOpenAPISchema(entry[1], 'input'),
|
|
123
|
-
};
|
|
124
|
-
if (style === 'comma-delimited-array' || style === 'comma-delimited-object') {
|
|
125
|
-
parameter.style = 'simple';
|
|
126
|
-
parameter.explode = false;
|
|
127
|
-
}
|
|
128
|
-
else {
|
|
129
|
-
const _expect = style;
|
|
130
|
-
}
|
|
131
|
-
operation.parameters ??= [];
|
|
132
|
-
operation.parameters.push(parameter);
|
|
133
|
-
}
|
|
74
|
+
if (!dynamicParams?.length) return;
|
|
75
|
+
if (!paramsEntries) throw new OpenAPIGeneratorError(`the route declares path params (${dynamicParams.map((p) => `{${p}}`).join(", ")}) but there is no object schema to source them from.\n compact mode: the input schema must be an object containing each param.\n detailed mode: the input's "params" section must be an object containing each param.`);
|
|
76
|
+
for (const name of dynamicParams) {
|
|
77
|
+
const entry = paramsEntries.find(([entryName]) => entryName === name);
|
|
78
|
+
if (!entry) throw new OpenAPIGeneratorError(`dynamic param "{${name}}" is missing from the input schema.\n Route params: ${dynamicParams.map((p) => `{${p}}`).join(", ")}\n Schema keys: ${paramsEntries.map(([n]) => n).join(", ") || "(none)"}`);
|
|
79
|
+
if (entry[2]) throw new OpenAPIGeneratorError(`dynamic param "${name}" is optional in the input schema.\n OpenAPI requires path params to be required. Make "${name}" required.`);
|
|
80
|
+
const style = paramsStyles?.[name];
|
|
81
|
+
const parameter = {
|
|
82
|
+
in: "path",
|
|
83
|
+
required: true,
|
|
84
|
+
name,
|
|
85
|
+
schema: ctx.registry.toOpenAPISchema(entry[1], "input")
|
|
86
|
+
};
|
|
87
|
+
if (style === "comma-delimited-array" || style === "comma-delimited-object") {
|
|
88
|
+
parameter.style = "simple";
|
|
89
|
+
parameter.explode = false;
|
|
90
|
+
}
|
|
91
|
+
operation.parameters ??= [];
|
|
92
|
+
operation.parameters.push(parameter);
|
|
93
|
+
}
|
|
134
94
|
}
|
|
135
95
|
function renderQueryParameters(ctx, operation, queryEntries, queryStyles) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (!flattenJsonUnionSchema(schema).every(isPrimitive)) {
|
|
166
|
-
const arrayable = matchArrayableJsonSchema(schema);
|
|
167
|
-
if (!arrayable || !flattenJsonUnionSchema(arrayable[0]).every(isPrimitive)) {
|
|
168
|
-
parameter.style = 'deepObject';
|
|
169
|
-
parameter.explode = true;
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
else {
|
|
174
|
-
const _expect = style;
|
|
175
|
-
}
|
|
176
|
-
operation.parameters ??= [];
|
|
177
|
-
operation.parameters.push(parameter);
|
|
178
|
-
}
|
|
96
|
+
for (const [name, schema, optional] of queryEntries ?? []) {
|
|
97
|
+
const style = queryStyles?.[name];
|
|
98
|
+
const parameter = {
|
|
99
|
+
in: "query",
|
|
100
|
+
name,
|
|
101
|
+
schema: ctx.registry.toOpenAPISchema(schema, "input"),
|
|
102
|
+
allowEmptyValue: true,
|
|
103
|
+
allowReserved: true
|
|
104
|
+
};
|
|
105
|
+
if (!optional) parameter.required = true;
|
|
106
|
+
if (style === "comma-delimited-array" || style === "comma-delimited-object") parameter.explode = false;
|
|
107
|
+
else if (style === "pipe-delimited-array" || style === "pipe-delimited-object") parameter.style = "pipeDelimited";
|
|
108
|
+
else if (style === "space-delimited-array" || style === "space-delimited-object") parameter.style = "spaceDelimited";
|
|
109
|
+
else if (style === "json") {
|
|
110
|
+
parameter.content = { "application/json": { schema: parameter.schema } };
|
|
111
|
+
delete parameter.schema;
|
|
112
|
+
delete parameter.allowReserved;
|
|
113
|
+
} else if (style === void 0) {
|
|
114
|
+
if (!flattenJsonUnionSchema(schema).every(isPrimitive)) {
|
|
115
|
+
const arrayable = matchArrayableJsonSchema(schema);
|
|
116
|
+
if (!arrayable || !flattenJsonUnionSchema(arrayable[0]).every(isPrimitive)) {
|
|
117
|
+
parameter.style = "deepObject";
|
|
118
|
+
parameter.explode = true;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
operation.parameters ??= [];
|
|
123
|
+
operation.parameters.push(parameter);
|
|
124
|
+
}
|
|
179
125
|
}
|
|
180
126
|
function renderHeaderParameters(ctx, operation, headersEntries) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
127
|
+
for (const [name, schema, optional] of headersEntries ?? []) {
|
|
128
|
+
operation.parameters ??= [];
|
|
129
|
+
operation.parameters.push({
|
|
130
|
+
in: "header",
|
|
131
|
+
name,
|
|
132
|
+
required: optional ? void 0 : true,
|
|
133
|
+
schema: ctx.registry.toOpenAPISchema(schema, "input")
|
|
134
|
+
});
|
|
135
|
+
}
|
|
190
136
|
}
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
schema: ctx.registry.toOpenAPISchema(schema, 'output'),
|
|
229
|
-
};
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
operation.responses ??= {};
|
|
233
|
-
operation.responses[toResponseStatusKey(responseStatus)] = responseObject;
|
|
234
|
-
}
|
|
137
|
+
function buildSuccessResponse(ctx, operation, def, meta) {
|
|
138
|
+
const status = meta?.successStatus ?? DEFAULT_SUCCESS_STATUS;
|
|
139
|
+
const description = meta?.successDescription ?? "OK";
|
|
140
|
+
const outputStructure = meta?.outputStructure ?? "compact";
|
|
141
|
+
if (outputStructure === "compact") {
|
|
142
|
+
const iteratorDetails = getAsyncIteratorObjectSchemasDetails(def.outputSchemas);
|
|
143
|
+
if (iteratorDetails) {
|
|
144
|
+
operation.responses ??= {};
|
|
145
|
+
operation.responses[toResponseStatusKey(status)] = {
|
|
146
|
+
description,
|
|
147
|
+
content: toAsyncIteratorObjectContent(ctx, "output", ctx.convertSchemas(iteratorDetails.yieldSchemas, "output"), ctx.convertSchemas(iteratorDetails.returnSchemas, "output"))
|
|
148
|
+
};
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const [schema] = ctx.convertSchemas(def.outputSchemas, "output");
|
|
153
|
+
if (isUnconstrainedSchema(schema) || outputStructure === "compact") {
|
|
154
|
+
operation.responses ??= {};
|
|
155
|
+
operation.responses[toResponseStatusKey(status)] = {
|
|
156
|
+
description,
|
|
157
|
+
content: toBodyContent(ctx, "output", schema)
|
|
158
|
+
};
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
for (const [responseStatus, parts] of extractDetailedResponseParts(schema, status)) {
|
|
162
|
+
const responseObject = { description: parts.descriptions.length ? parts.descriptions.join(", ") : description };
|
|
163
|
+
if (parts.bodies.length) responseObject.content = toBodyContent(ctx, "output", combineJsonSchemasWithComposition("anyOf", parts.bodies));
|
|
164
|
+
if (parts.headers.length) extractJsonObjectSchemaEntries(combineJsonSchemasWithComposition("anyOf", parts.headers))?.forEach(([name, schema, optional]) => {
|
|
165
|
+
responseObject.headers ??= {};
|
|
166
|
+
responseObject.headers[name] = {
|
|
167
|
+
required: optional ? void 0 : true,
|
|
168
|
+
schema: ctx.registry.toOpenAPISchema(schema, "output")
|
|
169
|
+
};
|
|
170
|
+
});
|
|
171
|
+
operation.responses ??= {};
|
|
172
|
+
operation.responses[toResponseStatusKey(responseStatus)] = responseObject;
|
|
173
|
+
}
|
|
235
174
|
}
|
|
236
175
|
/**
|
|
237
|
-
|
|
238
|
-
|
|
176
|
+
* Formats a numeric HTTP status as a Responses Object key.
|
|
177
|
+
*/
|
|
239
178
|
function toResponseStatusKey(status) {
|
|
240
|
-
|
|
179
|
+
return String(status);
|
|
241
180
|
}
|
|
242
181
|
/**
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
182
|
+
* Flattens a detailed output schema (a union of `{ status?, headers?, body? }` objects)
|
|
183
|
+
* into per-status response pieces.
|
|
184
|
+
*/
|
|
246
185
|
function extractDetailedResponseParts(schema, defaultStatus) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
if (description !== undefined) {
|
|
276
|
-
parts.descriptions.push(description);
|
|
277
|
-
}
|
|
278
|
-
if (headersSchema !== undefined) {
|
|
279
|
-
parts.headers.push(headersSchema);
|
|
280
|
-
}
|
|
281
|
-
if (bodySchema !== undefined) {
|
|
282
|
-
parts.bodies.push(bodySchema);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
return partsByStatus;
|
|
186
|
+
const partsByStatus = /* @__PURE__ */ new Map();
|
|
187
|
+
for (const item of flattenJsonUnionSchema(schema)) {
|
|
188
|
+
const entries = extractJsonObjectSchemaEntries(item);
|
|
189
|
+
if (!entries) throw new OpenAPIGeneratorError("outputStructure is \"detailed\" but the output schema (or one of its union members) is not an object.\n Expected each member shaped like: { status?: number (< 400), headers?: object, body?: unknown }");
|
|
190
|
+
const [, statusSchema, statusOptional = true] = entries.find(([name]) => name === "status") ?? [];
|
|
191
|
+
const statuses = statusSchema === void 0 ? /* @__PURE__ */ new Map() : extractDetailedStatuses(statusSchema);
|
|
192
|
+
if (!statuses) throw new OpenAPIGeneratorError(`invalid "status" field in the detailed output schema.
|
|
193
|
+
Expected: literal integers below 400 (const, enum, or a union of them)
|
|
194
|
+
Received: ${stringifyJSON(statusSchema)}`);
|
|
195
|
+
if (statusOptional && !statuses.has(defaultStatus)) statuses.set(defaultStatus, void 0);
|
|
196
|
+
const headersSchema = entries.find(([name]) => name === "headers")?.[1];
|
|
197
|
+
const bodySchema = entries.find(([name]) => name === "body")?.[1];
|
|
198
|
+
for (const [status, description] of statuses) {
|
|
199
|
+
let parts = partsByStatus.get(status);
|
|
200
|
+
if (!parts) {
|
|
201
|
+
parts = {
|
|
202
|
+
descriptions: [],
|
|
203
|
+
bodies: [],
|
|
204
|
+
headers: []
|
|
205
|
+
};
|
|
206
|
+
partsByStatus.set(status, parts);
|
|
207
|
+
}
|
|
208
|
+
if (description !== void 0) parts.descriptions.push(description);
|
|
209
|
+
if (headersSchema !== void 0) parts.headers.push(headersSchema);
|
|
210
|
+
if (bodySchema !== void 0) parts.bodies.push(bodySchema);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return partsByStatus;
|
|
287
214
|
}
|
|
288
215
|
/**
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
216
|
+
* Collects every status a detailed output `status` schema allows, mapped to its description.
|
|
217
|
+
* Accepts `const`, `enum` (how Effect, arktype, and valibot `picklist` emit literals), or a union of them.
|
|
218
|
+
* Returns `undefined` when any member is not a literal integer below 400.
|
|
219
|
+
*/
|
|
293
220
|
function extractDetailedStatuses(schema) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
return undefined;
|
|
306
|
-
}
|
|
307
|
-
if (!statuses.has(value)) {
|
|
308
|
-
statuses.set(value, member.description);
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
return statuses.size ? statuses : undefined;
|
|
221
|
+
const statuses = /* @__PURE__ */ new Map();
|
|
222
|
+
for (const member of flattenJsonUnionSchema(schema)) {
|
|
223
|
+
if (typeof member !== "object") return;
|
|
224
|
+
const values = member.const !== void 0 ? [member.const] : member.enum;
|
|
225
|
+
if (!values?.length) return;
|
|
226
|
+
for (const value of values) {
|
|
227
|
+
if (!Number.isInteger(value) || value >= 400) return;
|
|
228
|
+
if (!statuses.has(value)) statuses.set(value, member.description);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
return statuses.size ? statuses : void 0;
|
|
313
232
|
}
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
233
|
+
function buildErrorResponse(ctx, operation, def) {
|
|
234
|
+
const definitionsByStatus = /* @__PURE__ */ new Map();
|
|
235
|
+
for (const code of Object.keys(def.errorMap)) {
|
|
236
|
+
const config = def.errorMap[code];
|
|
237
|
+
if (!config) continue;
|
|
238
|
+
const status = getOwn(ctx.errorStatusMap, code) ?? DEFAULT_ERROR_STATUS;
|
|
239
|
+
const [dataJsonSchema, dataOptional] = ctx.convertSchemas(config.data !== void 0 ? [config.data] : void 0, "output");
|
|
240
|
+
let definitions = definitionsByStatus.get(status);
|
|
241
|
+
if (!definitions) {
|
|
242
|
+
definitions = [];
|
|
243
|
+
definitionsByStatus.set(status, definitions);
|
|
244
|
+
}
|
|
245
|
+
definitions.push({
|
|
246
|
+
code,
|
|
247
|
+
dataJsonSchema,
|
|
248
|
+
dataOptional,
|
|
249
|
+
defaultMessage: config.message
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
if (!definitionsByStatus.size) return;
|
|
253
|
+
const undefinedErrorSchema = ctx.registry.register("UndefinedError", {
|
|
254
|
+
title: "UndefinedError",
|
|
255
|
+
type: "object",
|
|
256
|
+
properties: {
|
|
257
|
+
defined: { const: false },
|
|
258
|
+
code: { type: "string" },
|
|
259
|
+
message: { type: "string" },
|
|
260
|
+
data: {}
|
|
261
|
+
},
|
|
262
|
+
required: [
|
|
263
|
+
"defined",
|
|
264
|
+
"code",
|
|
265
|
+
"message"
|
|
266
|
+
]
|
|
267
|
+
});
|
|
268
|
+
for (const [status, definitions] of definitionsByStatus.entries()) {
|
|
269
|
+
const descriptions = definitions.map(({ defaultMessage }) => defaultMessage).filter((m) => m !== void 0);
|
|
270
|
+
const definedErrors = definitions.map((def) => ({
|
|
271
|
+
...def,
|
|
272
|
+
dataJsonSchema: ctx.registry.hoistDefs(def.dataJsonSchema, "output")
|
|
273
|
+
}));
|
|
274
|
+
const defaultSchema = () => combineJsonSchemasWithComposition("oneOf", [...definedErrors.map(({ code, dataJsonSchema, dataOptional }) => {
|
|
275
|
+
return ctx.registry.register(toErrorComponentName(code), {
|
|
276
|
+
title: code,
|
|
277
|
+
...combineJsonObjectSchemaEntries([
|
|
278
|
+
[
|
|
279
|
+
"defined",
|
|
280
|
+
{ const: true },
|
|
281
|
+
false
|
|
282
|
+
],
|
|
283
|
+
[
|
|
284
|
+
"code",
|
|
285
|
+
{ const: code },
|
|
286
|
+
false
|
|
287
|
+
],
|
|
288
|
+
[
|
|
289
|
+
"message",
|
|
290
|
+
{ type: "string" },
|
|
291
|
+
false
|
|
292
|
+
],
|
|
293
|
+
[
|
|
294
|
+
"data",
|
|
295
|
+
dataJsonSchema,
|
|
296
|
+
dataOptional
|
|
297
|
+
]
|
|
298
|
+
])
|
|
299
|
+
});
|
|
300
|
+
}), undefinedErrorSchema]);
|
|
301
|
+
const responseSchema = ctx.errorResponseBodySchema === void 0 ? defaultSchema() : value(ctx.errorResponseBodySchema, definedErrors, status, defaultSchema);
|
|
302
|
+
const responseObject = { description: descriptions.length ? descriptions.join(", ") : status.toString() };
|
|
303
|
+
if (responseSchema !== void 0 && responseSchema !== null) responseObject.content = { [ctx.errorResponseContentType]: { schema: ctx.registry.toOpenAPISchema(responseSchema, "output") } };
|
|
304
|
+
operation.responses ??= {};
|
|
305
|
+
operation.responses[toResponseStatusKey(status)] = responseObject;
|
|
306
|
+
}
|
|
384
307
|
}
|
|
385
308
|
/**
|
|
386
|
-
|
|
387
|
-
|
|
309
|
+
* Converts an error code into a component name, e.g. `BAD_REQUEST` -> `BadRequest`.
|
|
310
|
+
*/
|
|
388
311
|
function toErrorComponentName(code) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const rest = part.slice(1);
|
|
394
|
-
return (part.charAt(0).toUpperCase() + (rest === rest.toUpperCase() ? rest.toLowerCase() : rest));
|
|
395
|
-
})
|
|
396
|
-
.join('');
|
|
397
|
-
return name || 'Error';
|
|
312
|
+
return code.split(/[^a-z0-9]+/i).filter(Boolean).map((part) => {
|
|
313
|
+
const rest = part.slice(1);
|
|
314
|
+
return part.charAt(0).toUpperCase() + (rest === rest.toUpperCase() ? rest.toLowerCase() : rest);
|
|
315
|
+
}).join("") || "Error";
|
|
398
316
|
}
|
|
399
317
|
function toAsyncIteratorObjectContent(ctx, direction, [yieldSchema, yieldOptional], [returnSchema, returnOptional]) {
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
318
|
+
const schema = combineJsonSchemasWithComposition("oneOf", [
|
|
319
|
+
combineJsonObjectSchemaEntries([
|
|
320
|
+
[
|
|
321
|
+
"event",
|
|
322
|
+
{ const: "message" },
|
|
323
|
+
false
|
|
324
|
+
],
|
|
325
|
+
[
|
|
326
|
+
"data",
|
|
327
|
+
yieldSchema,
|
|
328
|
+
yieldOptional
|
|
329
|
+
],
|
|
330
|
+
[
|
|
331
|
+
"id",
|
|
332
|
+
{ type: "string" },
|
|
333
|
+
true
|
|
334
|
+
],
|
|
335
|
+
[
|
|
336
|
+
"retry",
|
|
337
|
+
{ type: "number" },
|
|
338
|
+
true
|
|
339
|
+
]
|
|
340
|
+
]),
|
|
341
|
+
combineJsonObjectSchemaEntries([
|
|
342
|
+
[
|
|
343
|
+
"event",
|
|
344
|
+
{ const: "close" },
|
|
345
|
+
false
|
|
346
|
+
],
|
|
347
|
+
[
|
|
348
|
+
"data",
|
|
349
|
+
returnSchema,
|
|
350
|
+
returnOptional
|
|
351
|
+
],
|
|
352
|
+
[
|
|
353
|
+
"id",
|
|
354
|
+
{ type: "string" },
|
|
355
|
+
true
|
|
356
|
+
],
|
|
357
|
+
[
|
|
358
|
+
"retry",
|
|
359
|
+
{ type: "number" },
|
|
360
|
+
true
|
|
361
|
+
]
|
|
362
|
+
]),
|
|
363
|
+
{
|
|
364
|
+
type: "object",
|
|
365
|
+
properties: {
|
|
366
|
+
event: { const: "error" },
|
|
367
|
+
data: {},
|
|
368
|
+
id: { type: "string" },
|
|
369
|
+
retry: { type: "number" }
|
|
370
|
+
},
|
|
371
|
+
required: ["event"]
|
|
372
|
+
}
|
|
373
|
+
]);
|
|
374
|
+
return { "text/event-stream": { schema: ctx.registry.toOpenAPISchema(schema, direction) } };
|
|
429
375
|
}
|
|
430
376
|
function toBodyContent(ctx, direction, schema) {
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
: schema;
|
|
452
|
-
const hasNestedFiles = containsJsonFileSchema(restSchema);
|
|
453
|
-
const contentType = hasNestedFiles ? 'multipart/form-data' : 'application/json';
|
|
454
|
-
const fileSchemas = fileSchemasByMediaType.get(contentType);
|
|
455
|
-
fileSchemasByMediaType.delete(contentType);
|
|
456
|
-
content[contentType] = {
|
|
457
|
-
schema: ctx.registry.toOpenAPISchema(combineJsonSchemasWithComposition('anyOf', [restSchema, ...(fileSchemas ?? [])]), direction),
|
|
458
|
-
};
|
|
459
|
-
}
|
|
460
|
-
for (const [contentType, schemas] of fileSchemasByMediaType.entries()) {
|
|
461
|
-
content[contentType] = {
|
|
462
|
-
schema: ctx.registry.toOpenAPISchema(combineJsonSchemasWithComposition('anyOf', schemas), direction),
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
return content;
|
|
377
|
+
const fileSchemasByMediaType = /* @__PURE__ */ new Map();
|
|
378
|
+
const rest = flattenJsonUnionSchema(schema).filter((s) => {
|
|
379
|
+
const resolved = resolveJsonSchemaRootLocalRef(s);
|
|
380
|
+
if (!isJsonFileSchema(resolved)) return !isUnconstrainedSchema(s);
|
|
381
|
+
const contentMediaType = resolved.contentMediaType ?? "*/*";
|
|
382
|
+
const schemas = fileSchemasByMediaType.get(contentMediaType);
|
|
383
|
+
if (schemas) schemas.push(s);
|
|
384
|
+
else fileSchemasByMediaType.set(contentMediaType, [s]);
|
|
385
|
+
return false;
|
|
386
|
+
});
|
|
387
|
+
const content = {};
|
|
388
|
+
if (rest.length > 0) {
|
|
389
|
+
const restSchema = fileSchemasByMediaType.size ? combineJsonSchemasWithComposition("anyOf", rest) : schema;
|
|
390
|
+
const contentType = containsJsonFileSchema(restSchema) ? "multipart/form-data" : "application/json";
|
|
391
|
+
const fileSchemas = fileSchemasByMediaType.get(contentType);
|
|
392
|
+
fileSchemasByMediaType.delete(contentType);
|
|
393
|
+
content[contentType] = { schema: ctx.registry.toOpenAPISchema(combineJsonSchemasWithComposition("anyOf", [restSchema, ...fileSchemas ?? []]), direction) };
|
|
394
|
+
}
|
|
395
|
+
for (const [contentType, schemas] of fileSchemasByMediaType.entries()) content[contentType] = { schema: ctx.registry.toOpenAPISchema(combineJsonSchemasWithComposition("anyOf", schemas), direction) };
|
|
396
|
+
return content;
|
|
466
397
|
}
|
|
467
398
|
function isPrimitive(schema) {
|
|
468
|
-
|
|
399
|
+
return isJsonPrimitiveSchema(resolveJsonSchemaRootLocalRef(schema));
|
|
469
400
|
}
|
|
401
|
+
export { OpenAPIGeneratorError, buildErrorResponse, buildRequest, buildSuccessResponse, toOpenAPIPath };
|