@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
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { AnyORPCError } from '@orpc/client';
|
|
2
|
-
import { AnyProcedure, AnyRouter, Context } from '@orpc/server';
|
|
3
|
-
import { StandardHandlerCodec, ResolvedStandardHandlerHandleOptions, StandardHandlerCodecResolvedProcedure } from '@orpc/server/standard';
|
|
4
|
-
import { Value, Public, Promisable } from '@orpc/shared';
|
|
5
|
-
import { StandardLazyRequest, StandardResponse } from '@standard-server/core';
|
|
6
|
-
import { AnyProcedureContract } from '@orpc/contract';
|
|
7
|
-
import { O as OpenAPISerializer } from './openapi.YMRD3b4R.mjs';
|
|
8
|
-
|
|
9
|
-
interface OpenAPIMatcherOptions {
|
|
10
|
-
/**
|
|
11
|
-
* Filter which procedures are exposed for matching. Return `false` to exclude.
|
|
12
|
-
*
|
|
13
|
-
* @default true
|
|
14
|
-
*/
|
|
15
|
-
filter?: Value<boolean, [contract: AnyProcedureContract | AnyProcedure, path: string[]]> | undefined;
|
|
16
|
-
}
|
|
17
|
-
declare class OpenAPIMatcher {
|
|
18
|
-
private readonly filter;
|
|
19
|
-
private readonly rootRouter;
|
|
20
|
-
private readonly tree;
|
|
21
|
-
private readonly pendingLazyRouters;
|
|
22
|
-
constructor(router: AnyRouter, options?: OpenAPIMatcherOptions);
|
|
23
|
-
private index;
|
|
24
|
-
match(method: string, pathname: `/${string}`, prefix: `/${string}` | undefined): Promise<{
|
|
25
|
-
path: string[];
|
|
26
|
-
procedure: AnyProcedure;
|
|
27
|
-
params?: Record<string, string> | undefined;
|
|
28
|
-
} | undefined>;
|
|
29
|
-
private findMatch;
|
|
30
|
-
private resolvePendingLazyRouters;
|
|
31
|
-
private loadPendingLazyRouters;
|
|
32
|
-
private loadPendingLazyRouter;
|
|
33
|
-
private indexPendingLazyRouter;
|
|
34
|
-
private resolveProcedure;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface OpenAPIHandlerCodecCoreOptions<_T extends Context> {
|
|
38
|
-
/**
|
|
39
|
-
* Override the default OpenAPI serializer.
|
|
40
|
-
*/
|
|
41
|
-
serializer?: Public<OpenAPISerializer> | undefined;
|
|
42
|
-
/**
|
|
43
|
-
* Mapping ORPCError Code -> HTTP Status Code
|
|
44
|
-
* The status code should be in the `4xx` or `5xx` range (must be greater than or equal to `400`).
|
|
45
|
-
* Codes missing from the map fall back to `DEFAULT_ERROR_STATUS` (400).
|
|
46
|
-
*
|
|
47
|
-
* @default COMMON_ERROR_STATUS_MAP
|
|
48
|
-
*/
|
|
49
|
-
errorStatusMap?: Record<string, number> | undefined;
|
|
50
|
-
/**
|
|
51
|
-
* Customize how an ORPC error is serialized into a response body.
|
|
52
|
-
* Use this if your API needs a different error output structure.
|
|
53
|
-
*
|
|
54
|
-
* @remarks
|
|
55
|
-
* - Return `null | undefined` to fallback to default behavior
|
|
56
|
-
*
|
|
57
|
-
* @default ((e) => e.toJSON())
|
|
58
|
-
*/
|
|
59
|
-
errorResponseBodyEncoder?: ((error: AnyORPCError) => unknown) | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* The `content-type` of error responses, e.g. `application/problem+json` for RFC 9457 Problem Details.
|
|
62
|
-
*
|
|
63
|
-
* @default 'application/json'
|
|
64
|
-
*/
|
|
65
|
-
errorResponseContentType?: string | undefined;
|
|
66
|
-
}
|
|
67
|
-
declare class OpenAPIHandlerCodecCore<T extends Context> {
|
|
68
|
-
private readonly serializer;
|
|
69
|
-
private readonly errorStatusMap;
|
|
70
|
-
private readonly errorResponseBodyEncoder;
|
|
71
|
-
private readonly errorResponseContentType;
|
|
72
|
-
constructor(options?: OpenAPIHandlerCodecCoreOptions<T>);
|
|
73
|
-
decodeInput(matched: {
|
|
74
|
-
procedure: AnyProcedure;
|
|
75
|
-
params?: Record<string, string> | undefined;
|
|
76
|
-
}, request: StandardLazyRequest): Promise<unknown>;
|
|
77
|
-
/**
|
|
78
|
-
* @throws {TypeError} If `outputStructure` is "detailed" and the output doesn't match the expected structure.
|
|
79
|
-
*/
|
|
80
|
-
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[]): Promisable<StandardResponse>;
|
|
81
|
-
encodeError(error: AnyORPCError): Promisable<StandardResponse>;
|
|
82
|
-
private deserializeQuery;
|
|
83
|
-
private deserializeParams;
|
|
84
|
-
}
|
|
85
|
-
interface OpenAPIHandlerCodecOptions<T extends Context> extends OpenAPIHandlerCodecCoreOptions<T>, OpenAPIMatcherOptions {
|
|
86
|
-
}
|
|
87
|
-
declare class OpenAPIHandlerCodec<T extends Context> extends OpenAPIHandlerCodecCore<T> implements StandardHandlerCodec<T> {
|
|
88
|
-
private readonly matcher;
|
|
89
|
-
constructor(router: AnyRouter, options?: OpenAPIHandlerCodecOptions<T>);
|
|
90
|
-
resolveProcedure(request: StandardLazyRequest, options: ResolvedStandardHandlerHandleOptions<T>): Promise<StandardHandlerCodecResolvedProcedure | undefined>;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export { OpenAPIHandlerCodec as a, OpenAPIHandlerCodecCore as b, OpenAPIMatcher as d };
|
|
94
|
-
export type { OpenAPIHandlerCodecOptions as O, OpenAPIHandlerCodecCoreOptions as c, OpenAPIMatcherOptions as e };
|
|
@@ -1,149 +0,0 @@
|
|
|
1
|
-
import { isPlainObject, NullProtoObj, getOwn, setOwn } from '@orpc/shared';
|
|
2
|
-
|
|
3
|
-
class BracketNotationSerializer {
|
|
4
|
-
maxDeserializingEmptySlots;
|
|
5
|
-
constructor(options = {}) {
|
|
6
|
-
this.maxDeserializingEmptySlots = options.maxDeserializingEmptySlots ?? 1e3;
|
|
7
|
-
}
|
|
8
|
-
serialize(data) {
|
|
9
|
-
const result = [];
|
|
10
|
-
this.internalSerialize(data, "", true, result);
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
13
|
-
internalSerialize(data, path, isRoot, result) {
|
|
14
|
-
if (Array.isArray(data)) {
|
|
15
|
-
data.forEach((item, i) => {
|
|
16
|
-
this.internalSerialize(item, isRoot ? i.toString() : `${path}[${i}]`, false, result);
|
|
17
|
-
});
|
|
18
|
-
} else if (isPlainObject(data)) {
|
|
19
|
-
for (const key of Object.keys(data)) {
|
|
20
|
-
this.internalSerialize(data[key], isRoot ? key : `${path}[${key}]`, false, result);
|
|
21
|
-
}
|
|
22
|
-
} else {
|
|
23
|
-
result.push([path, data]);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
deserialize(serialized) {
|
|
27
|
-
let arrayPushStyles;
|
|
28
|
-
const root = new NullProtoObj();
|
|
29
|
-
let emptySlotsLeft = this.maxDeserializingEmptySlots;
|
|
30
|
-
for (const [path, value] of serialized) {
|
|
31
|
-
const segments = this.parsePath(path);
|
|
32
|
-
let currentRef = root;
|
|
33
|
-
let nextSegment = segments[0];
|
|
34
|
-
for (let i = 1; i < segments.length; i++) {
|
|
35
|
-
const segment = segments[i];
|
|
36
|
-
const isLast = i === segments.length - 1;
|
|
37
|
-
const existing = getOwn(currentRef, nextSegment);
|
|
38
|
-
let child = existing;
|
|
39
|
-
let key = segment;
|
|
40
|
-
if (Array.isArray(child) || !isPlainObject(child)) {
|
|
41
|
-
const isArray = Array.isArray(child);
|
|
42
|
-
const isPushStyle = isArray && !!arrayPushStyles?.has(child);
|
|
43
|
-
const length = isArray ? child.length : 0;
|
|
44
|
-
const index = internalToArrayIndex(segment);
|
|
45
|
-
const emptySlots = index === void 0 ? 0 : Math.max(0, index - length);
|
|
46
|
-
const canBeArray = segment === "" ? isLast && (isPushStyle || length === 0) : index !== void 0 && !(isLast && isPushStyle) && emptySlots <= emptySlotsLeft;
|
|
47
|
-
if (canBeArray) {
|
|
48
|
-
emptySlotsLeft -= emptySlots;
|
|
49
|
-
child = isArray ? child : [];
|
|
50
|
-
key = index ?? segment;
|
|
51
|
-
} else if (isArray) {
|
|
52
|
-
arrayPushStyles?.delete(child);
|
|
53
|
-
child = isPushStyle ? internalPushStyleArrayToObject(child) : internalArrayToObject(child);
|
|
54
|
-
} else {
|
|
55
|
-
child = new NullProtoObj();
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
if (child !== existing) {
|
|
59
|
-
internalSetOwn(currentRef, nextSegment, child);
|
|
60
|
-
}
|
|
61
|
-
currentRef = child;
|
|
62
|
-
nextSegment = key;
|
|
63
|
-
}
|
|
64
|
-
if (Array.isArray(currentRef) && nextSegment === "") {
|
|
65
|
-
arrayPushStyles ??= /* @__PURE__ */ new WeakSet();
|
|
66
|
-
arrayPushStyles.add(currentRef);
|
|
67
|
-
currentRef.push(value);
|
|
68
|
-
} else if (Object.hasOwn(currentRef, nextSegment)) {
|
|
69
|
-
const current = currentRef[nextSegment];
|
|
70
|
-
if (Array.isArray(current)) {
|
|
71
|
-
current.push(value);
|
|
72
|
-
} else {
|
|
73
|
-
internalSetOwn(currentRef, nextSegment, [current, value]);
|
|
74
|
-
}
|
|
75
|
-
} else {
|
|
76
|
-
internalSetOwn(currentRef, nextSegment, value);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return root;
|
|
80
|
-
}
|
|
81
|
-
stringifyPath(segments) {
|
|
82
|
-
if (segments.length === 0) {
|
|
83
|
-
return "";
|
|
84
|
-
}
|
|
85
|
-
let result = segments[0].toString();
|
|
86
|
-
for (let i = 1; i < segments.length; i++) {
|
|
87
|
-
result += `[${segments[i]}]`;
|
|
88
|
-
}
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
parsePath(path) {
|
|
92
|
-
const start = path.indexOf("[");
|
|
93
|
-
if (start === -1 || path[path.length - 1] !== "]") {
|
|
94
|
-
return [path];
|
|
95
|
-
}
|
|
96
|
-
const segments = [path.slice(0, start)];
|
|
97
|
-
let from = start + 1;
|
|
98
|
-
let to = path.indexOf("][", from);
|
|
99
|
-
while (to !== -1) {
|
|
100
|
-
segments.push(path.slice(from, to));
|
|
101
|
-
from = to + 2;
|
|
102
|
-
to = path.indexOf("][", from);
|
|
103
|
-
}
|
|
104
|
-
segments.push(path.slice(from, -1));
|
|
105
|
-
return segments;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
const MAX_ARRAY_INDEX = 2 ** 32 - 2;
|
|
109
|
-
function internalToArrayIndex(key) {
|
|
110
|
-
if (key.length === 0 || key.length > 10) {
|
|
111
|
-
return void 0;
|
|
112
|
-
}
|
|
113
|
-
let index = key.charCodeAt(0) - 48;
|
|
114
|
-
if (index < 0 || index > 9 || index === 0 && key.length > 1) {
|
|
115
|
-
return void 0;
|
|
116
|
-
}
|
|
117
|
-
for (let i = 1; i < key.length; i++) {
|
|
118
|
-
const digit = key.charCodeAt(i) - 48;
|
|
119
|
-
if (digit < 0 || digit > 9) {
|
|
120
|
-
return void 0;
|
|
121
|
-
}
|
|
122
|
-
index = index * 10 + digit;
|
|
123
|
-
}
|
|
124
|
-
return index <= MAX_ARRAY_INDEX ? index : void 0;
|
|
125
|
-
}
|
|
126
|
-
function internalArrayToObject(array) {
|
|
127
|
-
const obj = new NullProtoObj();
|
|
128
|
-
array.forEach((item, i) => {
|
|
129
|
-
obj[i] = item;
|
|
130
|
-
});
|
|
131
|
-
return obj;
|
|
132
|
-
}
|
|
133
|
-
function internalPushStyleArrayToObject(array) {
|
|
134
|
-
const obj = new NullProtoObj();
|
|
135
|
-
obj[""] = array.length === 1 ? array[0] : array;
|
|
136
|
-
return obj;
|
|
137
|
-
}
|
|
138
|
-
function internalSetOwn(container, key, value) {
|
|
139
|
-
if (typeof key === "string" && container instanceof NullProtoObj) {
|
|
140
|
-
const index = internalToArrayIndex(key);
|
|
141
|
-
if (index !== void 0 && index !== 0 && !Object.hasOwn(container, index - 1) && !Object.hasOwn(container, MAX_ARRAY_INDEX)) {
|
|
142
|
-
container[MAX_ARRAY_INDEX] = void 0;
|
|
143
|
-
delete container[MAX_ARRAY_INDEX];
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
setOwn(container, key, value);
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export { BracketNotationSerializer as B };
|
|
@@ -1,353 +0,0 @@
|
|
|
1
|
-
import { wrapAsyncIteratorPreservingEventMeta, toORPCError, isORPCErrorJson, createORPCErrorFromJson } from '@orpc/client';
|
|
2
|
-
import { isPlainObject, NullProtoObj, copyOnWrite, isAsyncIteratorObject } from '@orpc/shared';
|
|
3
|
-
import { ErrorEvent } from '@standard-server/core';
|
|
4
|
-
import { B as BracketNotationSerializer } from './openapi.DtEVcxuW.mjs';
|
|
5
|
-
|
|
6
|
-
const DEFAULT_OPENAPI_METHOD = "POST";
|
|
7
|
-
const DEFAULT_OPENAPI_SUCCESS_DESCRIPTION = "OK";
|
|
8
|
-
const DEFAULT_OPENAPI_INPUT_STRUCTURE = "compact";
|
|
9
|
-
const DEFAULT_OPENAPI_OUTPUT_STRUCTURE = "compact";
|
|
10
|
-
|
|
11
|
-
const DEFAULT_OPEN_API_JSON_SERIALIZER_HANDLERS = {
|
|
12
|
-
undefined: {
|
|
13
|
-
condition(data) {
|
|
14
|
-
return data === void 0;
|
|
15
|
-
},
|
|
16
|
-
serialize() {
|
|
17
|
-
return null;
|
|
18
|
-
},
|
|
19
|
-
isTerminal: true
|
|
20
|
-
},
|
|
21
|
-
bigint: {
|
|
22
|
-
condition(data) {
|
|
23
|
-
return typeof data === "bigint";
|
|
24
|
-
},
|
|
25
|
-
serialize(data) {
|
|
26
|
-
return data.toString();
|
|
27
|
-
},
|
|
28
|
-
isTerminal: true
|
|
29
|
-
},
|
|
30
|
-
date: {
|
|
31
|
-
condition(data) {
|
|
32
|
-
return data instanceof Date;
|
|
33
|
-
},
|
|
34
|
-
serialize(data) {
|
|
35
|
-
if (Number.isNaN(data.getTime())) {
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
return data.toISOString();
|
|
39
|
-
},
|
|
40
|
-
isTerminal: true
|
|
41
|
-
},
|
|
42
|
-
nan: {
|
|
43
|
-
condition(data) {
|
|
44
|
-
return typeof data === "number" && Number.isNaN(data);
|
|
45
|
-
},
|
|
46
|
-
serialize() {
|
|
47
|
-
return null;
|
|
48
|
-
},
|
|
49
|
-
isTerminal: true
|
|
50
|
-
},
|
|
51
|
-
infinity: {
|
|
52
|
-
condition(data) {
|
|
53
|
-
return data === Number.POSITIVE_INFINITY || data === Number.NEGATIVE_INFINITY;
|
|
54
|
-
},
|
|
55
|
-
serialize() {
|
|
56
|
-
return null;
|
|
57
|
-
},
|
|
58
|
-
isTerminal: true
|
|
59
|
-
},
|
|
60
|
-
url: {
|
|
61
|
-
condition(data) {
|
|
62
|
-
return data instanceof URL;
|
|
63
|
-
},
|
|
64
|
-
serialize(data) {
|
|
65
|
-
return data.toString();
|
|
66
|
-
},
|
|
67
|
-
isTerminal: true
|
|
68
|
-
},
|
|
69
|
-
set: {
|
|
70
|
-
condition(data) {
|
|
71
|
-
return data instanceof Set;
|
|
72
|
-
},
|
|
73
|
-
serialize(data) {
|
|
74
|
-
return Array.from(data);
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
map: {
|
|
78
|
-
condition(data) {
|
|
79
|
-
return data instanceof Map;
|
|
80
|
-
},
|
|
81
|
-
serialize(data) {
|
|
82
|
-
return Array.from(data.entries());
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
class OpenAPIJsonSerializer {
|
|
87
|
-
inlineBuiltInHandlers;
|
|
88
|
-
handlerEntries;
|
|
89
|
-
omitUndefinedProperties;
|
|
90
|
-
constructor(options = {}) {
|
|
91
|
-
this.omitUndefinedProperties = options.omitUndefinedProperties !== false;
|
|
92
|
-
const customHandlers = options.handlers;
|
|
93
|
-
if (customHandlers === void 0) {
|
|
94
|
-
this.inlineBuiltInHandlers = true;
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
let inlineBuiltInHandlers = true;
|
|
98
|
-
let handlerEntries = [];
|
|
99
|
-
for (const key of Object.keys(customHandlers)) {
|
|
100
|
-
const handler = customHandlers[key];
|
|
101
|
-
if (inlineBuiltInHandlers && key in DEFAULT_OPEN_API_JSON_SERIALIZER_HANDLERS) {
|
|
102
|
-
inlineBuiltInHandlers = false;
|
|
103
|
-
break;
|
|
104
|
-
}
|
|
105
|
-
if (handler !== void 0) {
|
|
106
|
-
handlerEntries.push(handler);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
if (!inlineBuiltInHandlers) {
|
|
110
|
-
handlerEntries = [];
|
|
111
|
-
for (const handler of Object.values({ ...DEFAULT_OPEN_API_JSON_SERIALIZER_HANDLERS, ...customHandlers })) {
|
|
112
|
-
if (handler !== void 0) {
|
|
113
|
-
handlerEntries.push(handler);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
this.inlineBuiltInHandlers = inlineBuiltInHandlers;
|
|
118
|
-
this.handlerEntries = handlerEntries;
|
|
119
|
-
}
|
|
120
|
-
serializeJson(data) {
|
|
121
|
-
const maps = [];
|
|
122
|
-
const blobs = [];
|
|
123
|
-
const json = this.serializeValue(data, [], maps, blobs);
|
|
124
|
-
return { json, maps, blobs };
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* `segments` is a shared mutable stack (push/pop while walking),
|
|
128
|
-
* so it must be copied before being stored in `maps`.
|
|
129
|
-
*/
|
|
130
|
-
serializeValue(data, segments, maps, blobs) {
|
|
131
|
-
if (this.inlineBuiltInHandlers) {
|
|
132
|
-
switch (typeof data) {
|
|
133
|
-
case "string":
|
|
134
|
-
case "boolean":
|
|
135
|
-
return data;
|
|
136
|
-
case "number":
|
|
137
|
-
return Number.isFinite(data) ? data : null;
|
|
138
|
-
case "undefined":
|
|
139
|
-
return null;
|
|
140
|
-
case "bigint":
|
|
141
|
-
return data.toString();
|
|
142
|
-
case "object": {
|
|
143
|
-
if (data === null) {
|
|
144
|
-
return data;
|
|
145
|
-
}
|
|
146
|
-
if (data instanceof Date) {
|
|
147
|
-
return Number.isNaN(data.getTime()) ? null : data.toISOString();
|
|
148
|
-
}
|
|
149
|
-
if (data instanceof URL) {
|
|
150
|
-
return data.toString();
|
|
151
|
-
}
|
|
152
|
-
if (data instanceof Set) {
|
|
153
|
-
return this.serializeValue(Array.from(data), segments, maps, blobs);
|
|
154
|
-
}
|
|
155
|
-
if (data instanceof Map) {
|
|
156
|
-
return this.serializeValue(Array.from(data.entries()), segments, maps, blobs);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
const handlerEntries = this.handlerEntries;
|
|
162
|
-
if (handlerEntries) {
|
|
163
|
-
for (let i = 0; i < handlerEntries.length; i++) {
|
|
164
|
-
const handler = handlerEntries[i];
|
|
165
|
-
if (handler.condition(data)) {
|
|
166
|
-
const serialized = handler.serialize(data);
|
|
167
|
-
if (handler.isTerminal) {
|
|
168
|
-
if (serialized instanceof Blob) {
|
|
169
|
-
maps.push(segments.slice());
|
|
170
|
-
blobs.push(serialized);
|
|
171
|
-
}
|
|
172
|
-
return serialized;
|
|
173
|
-
}
|
|
174
|
-
return this.serializeValue(serialized, segments, maps, blobs);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
if (data instanceof Blob) {
|
|
179
|
-
maps.push(segments.slice());
|
|
180
|
-
blobs.push(data);
|
|
181
|
-
return data;
|
|
182
|
-
}
|
|
183
|
-
if (Array.isArray(data)) {
|
|
184
|
-
const json = [];
|
|
185
|
-
for (let i = 0; i < data.length; i++) {
|
|
186
|
-
segments.push(i);
|
|
187
|
-
json.push(this.serializeValue(data[i], segments, maps, blobs));
|
|
188
|
-
segments.pop();
|
|
189
|
-
}
|
|
190
|
-
return json;
|
|
191
|
-
}
|
|
192
|
-
if (isPlainObject(data)) {
|
|
193
|
-
const json = new NullProtoObj();
|
|
194
|
-
for (const k of Object.keys(data)) {
|
|
195
|
-
const v = data[k];
|
|
196
|
-
if (k === "toJSON" && typeof v === "function") {
|
|
197
|
-
continue;
|
|
198
|
-
}
|
|
199
|
-
if (v === void 0 && this.omitUndefinedProperties) {
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
segments.push(k);
|
|
203
|
-
json[k] = this.serializeValue(v, segments, maps, blobs);
|
|
204
|
-
segments.pop();
|
|
205
|
-
}
|
|
206
|
-
return json;
|
|
207
|
-
}
|
|
208
|
-
return data;
|
|
209
|
-
}
|
|
210
|
-
deserializeJson(serialized) {
|
|
211
|
-
const ref = { json: serialized.json };
|
|
212
|
-
if (serialized.blobs?.length) {
|
|
213
|
-
for (let i = 0; i < serialized.maps.length; i++) {
|
|
214
|
-
const blob = serialized.blobs[i];
|
|
215
|
-
if (!(blob instanceof Blob)) {
|
|
216
|
-
throw new TypeError(`Invalid OpenAPI serialized data: blob ${i} is not a Blob.`);
|
|
217
|
-
}
|
|
218
|
-
const segments = serialized.maps[i];
|
|
219
|
-
let original = serialized;
|
|
220
|
-
let currentRef = ref;
|
|
221
|
-
let preSegment = "json";
|
|
222
|
-
for (let j = 0; j < segments.length; j++) {
|
|
223
|
-
original = original[preSegment];
|
|
224
|
-
currentRef = copyOnWrite(currentRef, preSegment, original);
|
|
225
|
-
preSegment = segments[j];
|
|
226
|
-
if (!Object.hasOwn(currentRef, preSegment)) {
|
|
227
|
-
throw new TypeError(`Invalid OpenAPI serialized data: segment "${preSegment}" does not exist.`);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
currentRef[preSegment] = blob;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
return ref.json;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
class OpenAPISerializer extends OpenAPIJsonSerializer {
|
|
238
|
-
bracketNotation;
|
|
239
|
-
defaultSerializeOptions;
|
|
240
|
-
constructor({ bracketNotation, serialize, ...options } = {}) {
|
|
241
|
-
super(options);
|
|
242
|
-
this.bracketNotation = new BracketNotationSerializer(bracketNotation);
|
|
243
|
-
this.defaultSerializeOptions = serialize;
|
|
244
|
-
}
|
|
245
|
-
serialize(data, options = {}) {
|
|
246
|
-
if (!options.asFormData) {
|
|
247
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
248
|
-
return data;
|
|
249
|
-
}
|
|
250
|
-
if (isAsyncIteratorObject(data)) {
|
|
251
|
-
return wrapAsyncIteratorPreservingEventMeta(data, {
|
|
252
|
-
mapResult: (result) => {
|
|
253
|
-
if (result.value === void 0) {
|
|
254
|
-
return result;
|
|
255
|
-
}
|
|
256
|
-
return { done: result.done, value: this.serializePayload(result.value, false, false) };
|
|
257
|
-
},
|
|
258
|
-
mapError: (e) => new ErrorEvent(
|
|
259
|
-
this.serializePayload(toORPCError(e).toJSON(), false, false),
|
|
260
|
-
{ cause: e }
|
|
261
|
-
)
|
|
262
|
-
});
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
const useFormDataForBlobFields = options.useFormDataForBlobFields ?? this.defaultSerializeOptions?.useFormDataForBlobFields ?? true;
|
|
266
|
-
const asFormData = options.asFormData ?? this.defaultSerializeOptions?.asFormData ?? false;
|
|
267
|
-
return this.serializePayload(data, useFormDataForBlobFields, asFormData);
|
|
268
|
-
}
|
|
269
|
-
serializePayload(value, useFormDataForBlobFields, asFormData) {
|
|
270
|
-
const { json, blobs } = this.serializeJson(value);
|
|
271
|
-
if (!asFormData && (json instanceof Blob || json === void 0 || !blobs?.length || !useFormDataForBlobFields)) {
|
|
272
|
-
return json;
|
|
273
|
-
}
|
|
274
|
-
const form = new FormData();
|
|
275
|
-
for (const [path, value2] of this.bracketNotation.serialize(json)) {
|
|
276
|
-
if (value2 instanceof Blob) {
|
|
277
|
-
form.append(path, value2);
|
|
278
|
-
} else if (value2 !== void 0 && value2 !== null) {
|
|
279
|
-
form.append(path, String(value2));
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
return form;
|
|
283
|
-
}
|
|
284
|
-
deserialize(data) {
|
|
285
|
-
if (data === void 0 || data instanceof ReadableStream || data instanceof Blob) {
|
|
286
|
-
return data;
|
|
287
|
-
}
|
|
288
|
-
if (isAsyncIteratorObject(data)) {
|
|
289
|
-
return wrapAsyncIteratorPreservingEventMeta(data, {
|
|
290
|
-
mapResult: (result) => {
|
|
291
|
-
if (result.value === void 0) {
|
|
292
|
-
return result;
|
|
293
|
-
}
|
|
294
|
-
return { done: result.done, value: this.deserializeJson({ json: result.value }) };
|
|
295
|
-
},
|
|
296
|
-
mapError: (e) => {
|
|
297
|
-
if (e instanceof ErrorEvent) {
|
|
298
|
-
const deserialized = this.deserializeJson({ json: e.data });
|
|
299
|
-
if (isORPCErrorJson(deserialized)) {
|
|
300
|
-
return createORPCErrorFromJson(deserialized, { cause: e });
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
return e;
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
}
|
|
307
|
-
if (data instanceof URLSearchParams || data instanceof FormData) {
|
|
308
|
-
data = this.bracketNotation.deserialize(Array.from(data.entries()));
|
|
309
|
-
}
|
|
310
|
-
return this.deserializeJson({ json: data });
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
function isBodylessMethod(method) {
|
|
315
|
-
return method === "GET" || method === "HEAD";
|
|
316
|
-
}
|
|
317
|
-
const PARAMETER_NAME_REGEX = /^[\w-]+$/;
|
|
318
|
-
function getDynamicPathParams(path) {
|
|
319
|
-
if (!path.includes("{")) {
|
|
320
|
-
return void 0;
|
|
321
|
-
}
|
|
322
|
-
const len = path.length;
|
|
323
|
-
let params;
|
|
324
|
-
let index = 1;
|
|
325
|
-
while (index < len) {
|
|
326
|
-
const segmentStart = index;
|
|
327
|
-
const slashPos = path.indexOf("/", index);
|
|
328
|
-
const segmentEnd = slashPos === -1 ? len : slashPos;
|
|
329
|
-
if (segmentEnd > segmentStart && path.charCodeAt(segmentStart) === 123 && path.charCodeAt(segmentEnd - 1) === 125) {
|
|
330
|
-
let parameterStart = segmentStart + 1;
|
|
331
|
-
let allowsSlash = false;
|
|
332
|
-
const operator = path.charCodeAt(parameterStart);
|
|
333
|
-
if (operator === 43) {
|
|
334
|
-
allowsSlash = true;
|
|
335
|
-
parameterStart++;
|
|
336
|
-
}
|
|
337
|
-
const parameterName = path.slice(parameterStart, segmentEnd - 1);
|
|
338
|
-
if (PARAMETER_NAME_REGEX.test(parameterName)) {
|
|
339
|
-
params ??= [];
|
|
340
|
-
params.push({
|
|
341
|
-
segment: path.slice(segmentStart, segmentEnd),
|
|
342
|
-
startIndex: segmentStart,
|
|
343
|
-
parameterName,
|
|
344
|
-
allowsSlash
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
index = segmentEnd + 1;
|
|
349
|
-
}
|
|
350
|
-
return params;
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
export { DEFAULT_OPENAPI_METHOD as D, OpenAPISerializer as O, DEFAULT_OPENAPI_INPUT_STRUCTURE as a, DEFAULT_OPENAPI_OUTPUT_STRUCTURE as b, DEFAULT_OPENAPI_SUCCESS_DESCRIPTION as c, OpenAPIJsonSerializer as d, getDynamicPathParams as g, isBodylessMethod as i };
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { AnyORPCError } from '@orpc/client';
|
|
2
|
-
import { AnyProcedure, AnyRouter, Context } from '@orpc/server';
|
|
3
|
-
import { StandardHandlerCodec, ResolvedStandardHandlerHandleOptions, StandardHandlerCodecResolvedProcedure } from '@orpc/server/standard';
|
|
4
|
-
import { Value, Public, Promisable } from '@orpc/shared';
|
|
5
|
-
import { StandardLazyRequest, StandardResponse } from '@standard-server/core';
|
|
6
|
-
import { AnyProcedureContract } from '@orpc/contract';
|
|
7
|
-
import { O as OpenAPISerializer } from './openapi.YMRD3b4R.js';
|
|
8
|
-
|
|
9
|
-
interface OpenAPIMatcherOptions {
|
|
10
|
-
/**
|
|
11
|
-
* Filter which procedures are exposed for matching. Return `false` to exclude.
|
|
12
|
-
*
|
|
13
|
-
* @default true
|
|
14
|
-
*/
|
|
15
|
-
filter?: Value<boolean, [contract: AnyProcedureContract | AnyProcedure, path: string[]]> | undefined;
|
|
16
|
-
}
|
|
17
|
-
declare class OpenAPIMatcher {
|
|
18
|
-
private readonly filter;
|
|
19
|
-
private readonly rootRouter;
|
|
20
|
-
private readonly tree;
|
|
21
|
-
private readonly pendingLazyRouters;
|
|
22
|
-
constructor(router: AnyRouter, options?: OpenAPIMatcherOptions);
|
|
23
|
-
private index;
|
|
24
|
-
match(method: string, pathname: `/${string}`, prefix: `/${string}` | undefined): Promise<{
|
|
25
|
-
path: string[];
|
|
26
|
-
procedure: AnyProcedure;
|
|
27
|
-
params?: Record<string, string> | undefined;
|
|
28
|
-
} | undefined>;
|
|
29
|
-
private findMatch;
|
|
30
|
-
private resolvePendingLazyRouters;
|
|
31
|
-
private loadPendingLazyRouters;
|
|
32
|
-
private loadPendingLazyRouter;
|
|
33
|
-
private indexPendingLazyRouter;
|
|
34
|
-
private resolveProcedure;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
interface OpenAPIHandlerCodecCoreOptions<_T extends Context> {
|
|
38
|
-
/**
|
|
39
|
-
* Override the default OpenAPI serializer.
|
|
40
|
-
*/
|
|
41
|
-
serializer?: Public<OpenAPISerializer> | undefined;
|
|
42
|
-
/**
|
|
43
|
-
* Mapping ORPCError Code -> HTTP Status Code
|
|
44
|
-
* The status code should be in the `4xx` or `5xx` range (must be greater than or equal to `400`).
|
|
45
|
-
* Codes missing from the map fall back to `DEFAULT_ERROR_STATUS` (400).
|
|
46
|
-
*
|
|
47
|
-
* @default COMMON_ERROR_STATUS_MAP
|
|
48
|
-
*/
|
|
49
|
-
errorStatusMap?: Record<string, number> | undefined;
|
|
50
|
-
/**
|
|
51
|
-
* Customize how an ORPC error is serialized into a response body.
|
|
52
|
-
* Use this if your API needs a different error output structure.
|
|
53
|
-
*
|
|
54
|
-
* @remarks
|
|
55
|
-
* - Return `null | undefined` to fallback to default behavior
|
|
56
|
-
*
|
|
57
|
-
* @default ((e) => e.toJSON())
|
|
58
|
-
*/
|
|
59
|
-
errorResponseBodyEncoder?: ((error: AnyORPCError) => unknown) | undefined;
|
|
60
|
-
/**
|
|
61
|
-
* The `content-type` of error responses, e.g. `application/problem+json` for RFC 9457 Problem Details.
|
|
62
|
-
*
|
|
63
|
-
* @default 'application/json'
|
|
64
|
-
*/
|
|
65
|
-
errorResponseContentType?: string | undefined;
|
|
66
|
-
}
|
|
67
|
-
declare class OpenAPIHandlerCodecCore<T extends Context> {
|
|
68
|
-
private readonly serializer;
|
|
69
|
-
private readonly errorStatusMap;
|
|
70
|
-
private readonly errorResponseBodyEncoder;
|
|
71
|
-
private readonly errorResponseContentType;
|
|
72
|
-
constructor(options?: OpenAPIHandlerCodecCoreOptions<T>);
|
|
73
|
-
decodeInput(matched: {
|
|
74
|
-
procedure: AnyProcedure;
|
|
75
|
-
params?: Record<string, string> | undefined;
|
|
76
|
-
}, request: StandardLazyRequest): Promise<unknown>;
|
|
77
|
-
/**
|
|
78
|
-
* @throws {TypeError} If `outputStructure` is "detailed" and the output doesn't match the expected structure.
|
|
79
|
-
*/
|
|
80
|
-
encodeOutput(output: unknown, procedure: AnyProcedure, path: string[]): Promisable<StandardResponse>;
|
|
81
|
-
encodeError(error: AnyORPCError): Promisable<StandardResponse>;
|
|
82
|
-
private deserializeQuery;
|
|
83
|
-
private deserializeParams;
|
|
84
|
-
}
|
|
85
|
-
interface OpenAPIHandlerCodecOptions<T extends Context> extends OpenAPIHandlerCodecCoreOptions<T>, OpenAPIMatcherOptions {
|
|
86
|
-
}
|
|
87
|
-
declare class OpenAPIHandlerCodec<T extends Context> extends OpenAPIHandlerCodecCore<T> implements StandardHandlerCodec<T> {
|
|
88
|
-
private readonly matcher;
|
|
89
|
-
constructor(router: AnyRouter, options?: OpenAPIHandlerCodecOptions<T>);
|
|
90
|
-
resolveProcedure(request: StandardLazyRequest, options: ResolvedStandardHandlerHandleOptions<T>): Promise<StandardHandlerCodecResolvedProcedure | undefined>;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export { OpenAPIHandlerCodec as a, OpenAPIHandlerCodecCore as b, OpenAPIMatcher as d };
|
|
94
|
-
export type { OpenAPIHandlerCodecOptions as O, OpenAPIHandlerCodecCoreOptions as c, OpenAPIMatcherOptions as e };
|