@orpc/openapi 2.0.0-beta.47 → 2.0.0-beta.49
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/README.md +6 -7
- package/dist/adapters/aws-lambda/index.d.ts +10 -22
- package/dist/adapters/aws-lambda/index.js +1 -0
- package/dist/adapters/aws-lambda/openapi-handler.d.ts +18 -0
- package/dist/adapters/aws-lambda/openapi-handler.js +18 -0
- package/dist/adapters/fastify/index.d.ts +1 -23
- package/dist/adapters/fastify/index.js +1 -0
- package/dist/adapters/fastify/openapi-handler.d.ts +15 -0
- package/dist/adapters/fastify/openapi-handler.js +15 -0
- package/dist/adapters/fetch/index.d.ts +2 -37
- package/dist/adapters/fetch/index.js +2 -0
- package/dist/adapters/fetch/openapi-handler.d.ts +15 -0
- package/dist/adapters/fetch/openapi-handler.js +15 -0
- package/dist/adapters/fetch/openapi-link.d.ts +21 -0
- package/dist/adapters/fetch/openapi-link.js +20 -0
- package/dist/adapters/node/index.d.ts +1 -23
- package/dist/adapters/node/index.js +1 -0
- package/dist/adapters/node/openapi-handler.d.ts +15 -0
- package/dist/adapters/node/openapi-handler.js +15 -0
- package/dist/adapters/standard/index.d.ts +3 -56
- package/dist/adapters/standard/index.js +3 -0
- package/dist/adapters/standard/openapi-handler-codec.d.ts +62 -0
- package/dist/adapters/standard/openapi-handler-codec.js +220 -0
- package/dist/adapters/standard/{index.d.mts → openapi-link-codec.d.ts} +25 -19
- package/dist/adapters/standard/openapi-link-codec.js +405 -0
- package/dist/adapters/standard/openapi-matcher.d.ts +29 -0
- package/dist/adapters/standard/openapi-matcher.js +108 -0
- package/dist/adapters/standard/utils.d.ts +4 -0
- package/dist/adapters/standard/utils.js +47 -0
- package/dist/bracket-notation.d.ts +26 -0
- package/dist/bracket-notation.js +185 -0
- package/dist/client-factory.d.ts +24 -0
- package/dist/client-factory.js +13 -0
- package/dist/constants.d.ts +4 -0
- package/dist/constants.js +4 -0
- package/dist/extensions/route.d.ts +3 -7
- package/dist/extensions/route.js +12 -0
- package/dist/helpers/{index.d.mts → form-data.d.ts} +2 -4
- package/dist/helpers/form-data.js +90 -0
- package/dist/helpers/index.d.ts +1 -59
- package/dist/helpers/index.js +1 -0
- package/dist/index.d.ts +12 -165
- package/dist/index.js +11 -0
- package/dist/{shared/openapi.zGZlCOwp.d.mts → meta.d.ts} +14 -18
- package/dist/meta.js +84 -0
- package/dist/openapi-generator-components.d.ts +31 -0
- package/dist/openapi-generator-components.js +275 -0
- package/dist/openapi-generator-operation.d.ts +34 -0
- package/dist/openapi-generator-operation.js +469 -0
- package/dist/openapi-generator.d.ts +92 -0
- package/dist/openapi-generator.js +147 -0
- package/dist/openapi-json-serializer.d.ts +79 -0
- package/dist/openapi-json-serializer.js +260 -0
- package/dist/openapi-serializer.d.ts +43 -0
- package/dist/openapi-serializer.js +93 -0
- package/dist/plugins/index.d.ts +1 -121
- package/dist/plugins/index.js +1 -0
- package/dist/plugins/{index.d.mts → openapi-reference.d.ts} +15 -21
- package/dist/plugins/openapi-reference.js +187 -0
- package/dist/router-utils.d.ts +20 -0
- package/dist/router-utils.js +36 -0
- package/dist/types.d.ts +31 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +14 -0
- package/dist/utils.js +47 -0
- package/package.json +69 -71
- package/dist/adapters/aws-lambda/index.d.mts +0 -27
- package/dist/adapters/aws-lambda/index.mjs +0 -21
- package/dist/adapters/fastify/index.d.mts +0 -23
- package/dist/adapters/fastify/index.mjs +0 -21
- package/dist/adapters/fetch/index.d.mts +0 -37
- package/dist/adapters/fetch/index.mjs +0 -34
- package/dist/adapters/node/index.d.mts +0 -23
- package/dist/adapters/node/index.mjs +0 -21
- package/dist/adapters/standard/index.mjs +0 -12
- package/dist/extensions/route.d.mts +0 -41
- package/dist/extensions/route.mjs +0 -14
- package/dist/helpers/index.mjs +0 -39
- package/dist/index.d.mts +0 -166
- package/dist/index.mjs +0 -780
- package/dist/plugins/index.mjs +0 -167
- package/dist/shared/openapi.B9PQzqBn.mjs +0 -49
- package/dist/shared/openapi.CQkXu1nY.mjs +0 -366
- package/dist/shared/openapi.Covi8zVE.d.mts +0 -33
- package/dist/shared/openapi.Covi8zVE.d.ts +0 -33
- package/dist/shared/openapi.DfmCR1pB.d.mts +0 -94
- package/dist/shared/openapi.DtEVcxuW.mjs +0 -149
- package/dist/shared/openapi.E3ZBiweM.mjs +0 -353
- package/dist/shared/openapi.Y4K_-I5W.d.ts +0 -94
- package/dist/shared/openapi.YMRD3b4R.d.mts +0 -157
- package/dist/shared/openapi.YMRD3b4R.d.ts +0 -157
- package/dist/shared/openapi.g7ucL05o.mjs +0 -410
- package/dist/shared/openapi.zGZlCOwp.d.ts +0 -329
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { COMMON_ERROR_STATUS_MAP } from '@orpc/client';
|
|
2
|
+
import { DEFAULT_ERROR_STATUS, DEFAULT_SUCCESS_STATUS } from '@orpc/server';
|
|
3
|
+
import { getOwn, isPlainObject, isNonPrimitive, NullProtoObj, parseEmptyableJSON, stringifyJSON, } from '@orpc/shared';
|
|
4
|
+
import { parseStandardUrl } from '@standard-server/core';
|
|
5
|
+
import { DEFAULT_OPENAPI_INPUT_STRUCTURE, DEFAULT_OPENAPI_OUTPUT_STRUCTURE, } from '../../constants.js';
|
|
6
|
+
import { getOpenAPIMeta } from '../../meta.js';
|
|
7
|
+
import { OpenAPISerializer } from '../../openapi-serializer.js';
|
|
8
|
+
import { isBodylessMethod } from '../../utils.js';
|
|
9
|
+
import { OpenAPIMatcher } from './openapi-matcher.js';
|
|
10
|
+
import { serializeHeaders } from './utils.js';
|
|
11
|
+
export class OpenAPIHandlerCodecCore {
|
|
12
|
+
serializer;
|
|
13
|
+
errorStatusMap;
|
|
14
|
+
errorResponseBodyEncoder;
|
|
15
|
+
errorResponseContentType;
|
|
16
|
+
constructor(options = {}) {
|
|
17
|
+
this.serializer = options.serializer ?? new OpenAPISerializer();
|
|
18
|
+
this.errorStatusMap = options.errorStatusMap ?? COMMON_ERROR_STATUS_MAP;
|
|
19
|
+
this.errorResponseBodyEncoder = options.errorResponseBodyEncoder ?? ((e) => e.toJSON());
|
|
20
|
+
this.errorResponseContentType = options.errorResponseContentType;
|
|
21
|
+
}
|
|
22
|
+
async decodeInput(matched, request) {
|
|
23
|
+
const meta = getOpenAPIMeta(matched.procedure);
|
|
24
|
+
const inputStructure = meta?.inputStructure ?? DEFAULT_OPENAPI_INPUT_STRUCTURE;
|
|
25
|
+
const params = this.deserializeParams(matched.params, meta?.paramsStyles);
|
|
26
|
+
if (inputStructure === 'compact') {
|
|
27
|
+
const data = isBodylessMethod(request.method)
|
|
28
|
+
? this.deserializeQuery(request.url, meta?.queryStyles)
|
|
29
|
+
: this.serializer.deserialize(await request.resolveBody(meta?.requestBodyHint));
|
|
30
|
+
if (data === undefined) {
|
|
31
|
+
return params;
|
|
32
|
+
}
|
|
33
|
+
if (!params || Object.keys(params).length < 1) {
|
|
34
|
+
return data;
|
|
35
|
+
}
|
|
36
|
+
// Non-object data (primitive, array, Blob, ReadableStream, ...) cannot be merged with params.
|
|
37
|
+
// Prefer params to stay consistent with the OpenAPI generator, which only describes path params here.
|
|
38
|
+
if (!isPlainObject(data)) {
|
|
39
|
+
return params;
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
...data,
|
|
43
|
+
...params,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
params,
|
|
48
|
+
query: this.deserializeQuery(request.url, meta?.queryStyles),
|
|
49
|
+
headers: request.headers,
|
|
50
|
+
body: this.serializer.deserialize(await request.resolveBody(meta?.requestBodyHint)),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* @throws {TypeError} If `outputStructure` is "detailed" and the output doesn't match the expected structure.
|
|
55
|
+
*/
|
|
56
|
+
encodeOutput(output, procedure, path) {
|
|
57
|
+
const meta = getOpenAPIMeta(procedure);
|
|
58
|
+
const successStatus = meta?.successStatus ?? DEFAULT_SUCCESS_STATUS;
|
|
59
|
+
const outputStructure = meta?.outputStructure ?? DEFAULT_OPENAPI_OUTPUT_STRUCTURE;
|
|
60
|
+
if (outputStructure === 'compact') {
|
|
61
|
+
return {
|
|
62
|
+
status: successStatus,
|
|
63
|
+
headers: {},
|
|
64
|
+
body: this.serializer.serialize(output),
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (!isValidDetailedOutput(output)) {
|
|
68
|
+
throw new TypeError(`
|
|
69
|
+
Invalid "detailed" output structure returned by procedure (${path.join('.')}):
|
|
70
|
+
• Expected an object with optional properties:
|
|
71
|
+
- status (number <400)
|
|
72
|
+
- headers (object)
|
|
73
|
+
- body (any)
|
|
74
|
+
• No extra keys allowed.
|
|
75
|
+
|
|
76
|
+
Actual value:
|
|
77
|
+
${stringifyJSON(output)}
|
|
78
|
+
`);
|
|
79
|
+
}
|
|
80
|
+
return {
|
|
81
|
+
status: output.status ?? successStatus,
|
|
82
|
+
headers: output.headers !== undefined ? serializeHeaders(output.headers, this.serializer) : {},
|
|
83
|
+
body: this.serializer.serialize(output.body),
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
encodeError(error) {
|
|
87
|
+
const status = getOwn(this.errorStatusMap, error.code) ?? DEFAULT_ERROR_STATUS;
|
|
88
|
+
return {
|
|
89
|
+
status,
|
|
90
|
+
headers: this.errorResponseContentType !== undefined
|
|
91
|
+
? { 'content-type': this.errorResponseContentType }
|
|
92
|
+
: {},
|
|
93
|
+
body: this.serializer.serialize(this.errorResponseBodyEncoder(error)),
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
deserializeQuery(url, styles) {
|
|
97
|
+
const [, search] = parseStandardUrl(url);
|
|
98
|
+
const searchParams = new URLSearchParams(search);
|
|
99
|
+
const parsed = this.serializer.deserialize(searchParams);
|
|
100
|
+
if (!styles || !isPlainObject(parsed)) {
|
|
101
|
+
return parsed;
|
|
102
|
+
}
|
|
103
|
+
Object.entries(styles).forEach(([key, hint]) => {
|
|
104
|
+
if (hint === undefined) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const values = searchParams.getAll(key);
|
|
108
|
+
let parsedValue;
|
|
109
|
+
if (hint === 'primitive') {
|
|
110
|
+
parsedValue = values.at(-1);
|
|
111
|
+
}
|
|
112
|
+
else if (hint === 'array') {
|
|
113
|
+
parsedValue = values;
|
|
114
|
+
}
|
|
115
|
+
else if (hint === 'comma-delimited-array') {
|
|
116
|
+
parsedValue = decodeDelimitedArray(values.at(-1), ',');
|
|
117
|
+
}
|
|
118
|
+
else if (hint === 'comma-delimited-object') {
|
|
119
|
+
parsedValue = decodeDelimitedObject(values.at(-1), ',');
|
|
120
|
+
}
|
|
121
|
+
else if (hint === 'space-delimited-array') {
|
|
122
|
+
parsedValue = decodeDelimitedArray(values.at(-1), ' ');
|
|
123
|
+
}
|
|
124
|
+
else if (hint === 'space-delimited-object') {
|
|
125
|
+
parsedValue = decodeDelimitedObject(values.at(-1), ' ');
|
|
126
|
+
}
|
|
127
|
+
else if (hint === 'pipe-delimited-array') {
|
|
128
|
+
parsedValue = decodeDelimitedArray(values.at(-1), '|');
|
|
129
|
+
}
|
|
130
|
+
else if (hint === 'pipe-delimited-object') {
|
|
131
|
+
parsedValue = decodeDelimitedObject(values.at(-1), '|');
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
const _expect = hint;
|
|
135
|
+
const last = values.at(-1);
|
|
136
|
+
try {
|
|
137
|
+
parsedValue = parseEmptyableJSON(last);
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
parsedValue = last;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
parsed[key] = this.serializer.deserialize(parsedValue);
|
|
144
|
+
});
|
|
145
|
+
return parsed;
|
|
146
|
+
}
|
|
147
|
+
deserializeParams(params, styles) {
|
|
148
|
+
if (!params || !styles) {
|
|
149
|
+
return params;
|
|
150
|
+
}
|
|
151
|
+
const parsed = { ...params };
|
|
152
|
+
Object.entries(styles).forEach(([key, hint]) => {
|
|
153
|
+
const value = getOwn(params, key);
|
|
154
|
+
if (hint === undefined || hint === 'primitive' || value === undefined) {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
if (hint === 'comma-delimited-array') {
|
|
158
|
+
parsed[key] = decodeDelimitedArray(value, ',');
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
const _expect = hint;
|
|
162
|
+
parsed[key] = decodeDelimitedObject(value, ',');
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return parsed;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
export class OpenAPIHandlerCodec extends OpenAPIHandlerCodecCore {
|
|
169
|
+
matcher;
|
|
170
|
+
constructor(router, options = {}) {
|
|
171
|
+
super(options);
|
|
172
|
+
this.matcher = new OpenAPIMatcher(router, options);
|
|
173
|
+
}
|
|
174
|
+
async resolveProcedure(request, options) {
|
|
175
|
+
const [pathname] = parseStandardUrl(request.url);
|
|
176
|
+
const matched = await this.matcher.match(request.method, pathname, options.prefix);
|
|
177
|
+
if (!matched) {
|
|
178
|
+
return undefined;
|
|
179
|
+
}
|
|
180
|
+
return {
|
|
181
|
+
procedure: matched.procedure,
|
|
182
|
+
path: matched.path,
|
|
183
|
+
decodeInput: () => this.decodeInput(matched, request),
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function isValidDetailedOutput(output) {
|
|
188
|
+
if (!isNonPrimitive(output)) {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
if (Object.keys(output).some((key) => key !== 'status' && key !== 'headers' && key !== 'body')) {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
if (output.status !== undefined &&
|
|
195
|
+
(typeof output.status !== 'number' || !Number.isInteger(output.status) || output.status > 399)) {
|
|
196
|
+
return false;
|
|
197
|
+
}
|
|
198
|
+
if (output.headers !== undefined && !isNonPrimitive(output.headers)) {
|
|
199
|
+
return false;
|
|
200
|
+
}
|
|
201
|
+
return true;
|
|
202
|
+
}
|
|
203
|
+
function decodeDelimitedArray(value, delimiter) {
|
|
204
|
+
if (value === undefined) {
|
|
205
|
+
return undefined;
|
|
206
|
+
}
|
|
207
|
+
return value.split(delimiter);
|
|
208
|
+
}
|
|
209
|
+
function decodeDelimitedObject(value, delimiter) {
|
|
210
|
+
if (value === undefined) {
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
const obj = new NullProtoObj(); // Prevent Prototype Pollution with NullProtoObj
|
|
214
|
+
const parts = value.split(delimiter);
|
|
215
|
+
for (let i = 0; i < parts.length; i += 2) {
|
|
216
|
+
const key = parts[i];
|
|
217
|
+
obj[key] = parts[i + 1];
|
|
218
|
+
}
|
|
219
|
+
return obj;
|
|
220
|
+
}
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
import '@orpc/server';
|
|
9
|
-
import '@orpc/server/standard';
|
|
10
|
-
|
|
11
|
-
interface OpenAPILinkCodecOptions<T extends ClientContext> {
|
|
1
|
+
import type { ClientContext, ClientOptions } from '@orpc/client';
|
|
2
|
+
import type { StandardLinkCodec, StandardLinkCodecDecodedResponse } from '@orpc/client/standard';
|
|
3
|
+
import type { RouterContract } from '@orpc/contract';
|
|
4
|
+
import type { Promisable, Public, Value } from '@orpc/shared';
|
|
5
|
+
import type { StandardHeaders, StandardLazyResponse, StandardRequest, StandardUrl } from '@standard-server/core';
|
|
6
|
+
import { OpenAPISerializer } from '../../openapi-serializer.ts';
|
|
7
|
+
export interface OpenAPILinkCodecOptions<T extends ClientContext> {
|
|
12
8
|
/**
|
|
13
9
|
* Base URL for all requests, without origin. Should match the OpenAPI handler mount path.
|
|
14
10
|
*
|
|
@@ -19,16 +15,24 @@ interface OpenAPILinkCodecOptions<T extends ClientContext> {
|
|
|
19
15
|
/**
|
|
20
16
|
* Inject headers into the request.
|
|
21
17
|
*/
|
|
22
|
-
headers?: Value<Promisable<StandardHeaders | Headers>, [
|
|
18
|
+
headers?: Value<Promisable<StandardHeaders | Headers>, [
|
|
19
|
+
options: ClientOptions<T>,
|
|
20
|
+
path: string[],
|
|
21
|
+
input: unknown
|
|
22
|
+
]> | undefined;
|
|
23
23
|
/**
|
|
24
24
|
* Override the default OpenAPI serializer.
|
|
25
25
|
*/
|
|
26
26
|
serializer?: Public<OpenAPISerializer> | undefined;
|
|
27
27
|
/**
|
|
28
|
-
* Customize how an error response body is converted into
|
|
29
|
-
*
|
|
28
|
+
* Customize how an error response body is converted into the error thrown by the client.
|
|
29
|
+
*
|
|
30
|
+
* @remarks
|
|
31
|
+
* - Return `defaultError()` to keep the default decoding (a `MalformedResponseError` for bodies that are not ORPC errors)
|
|
32
|
+
*
|
|
33
|
+
* @default ((body, response, defaultError) => defaultError())
|
|
30
34
|
*/
|
|
31
|
-
errorResponseBodyDecoder?: ((deserializedBody: unknown, response: StandardLazyResponse) =>
|
|
35
|
+
errorResponseBodyDecoder?: ((deserializedBody: unknown, response: StandardLazyResponse, defaultError: () => Error) => Error) | undefined;
|
|
32
36
|
/**
|
|
33
37
|
* Parse error responses with this `content-type` as JSON, ignoring case and parameters such as `charset`.
|
|
34
38
|
* Should match the handler's `errorResponseContentType`.
|
|
@@ -37,7 +41,7 @@ interface OpenAPILinkCodecOptions<T extends ClientContext> {
|
|
|
37
41
|
*/
|
|
38
42
|
errorResponseContentType?: string | undefined;
|
|
39
43
|
}
|
|
40
|
-
declare class OpenAPILinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
44
|
+
export declare class OpenAPILinkCodec<T extends ClientContext> implements StandardLinkCodec<T> {
|
|
41
45
|
private readonly router;
|
|
42
46
|
private readonly baseUrl;
|
|
43
47
|
private readonly headers;
|
|
@@ -47,10 +51,12 @@ declare class OpenAPILinkCodec<T extends ClientContext> implements StandardLinkC
|
|
|
47
51
|
constructor(router: RouterContract, options?: OpenAPILinkCodecOptions<T>);
|
|
48
52
|
encodeInput(input: unknown, path: string[], options: ClientOptions<T>): Promise<StandardRequest>;
|
|
49
53
|
private encodePathParam;
|
|
54
|
+
/**
|
|
55
|
+
* Serializes a single path/query value, like header values, so body-level
|
|
56
|
+
* serialize defaults (e.g. `asFormData`) never apply to URLs.
|
|
57
|
+
*/
|
|
58
|
+
private serializeURLValue;
|
|
50
59
|
private serializeQueryString;
|
|
51
60
|
decodeResponse(response: StandardLazyResponse, path: string[], _options: ClientOptions<T>): Promise<StandardLinkCodecDecodedResponse>;
|
|
52
61
|
private resolveProcedure;
|
|
53
62
|
}
|
|
54
|
-
|
|
55
|
-
export { OpenAPILinkCodec };
|
|
56
|
-
export type { OpenAPILinkCodecOptions };
|