@orpc/openapi 2.0.0-beta.47 → 2.0.0-beta.48
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 +2 -3
- package/dist/adapters/aws-lambda/index.d.mts +14 -26
- package/dist/adapters/aws-lambda/index.mjs +1 -21
- package/dist/adapters/aws-lambda/openapi-handler.d.mts +17 -0
- package/dist/adapters/aws-lambda/openapi-handler.mjs +11 -0
- package/dist/adapters/fastify/index.d.mts +1 -23
- package/dist/adapters/fastify/index.mjs +1 -21
- package/dist/adapters/fastify/openapi-handler.d.mts +14 -0
- package/dist/adapters/fastify/openapi-handler.mjs +11 -0
- package/dist/adapters/fetch/index.d.mts +2 -37
- package/dist/adapters/fetch/index.mjs +2 -34
- package/dist/adapters/fetch/openapi-handler.d.mts +14 -0
- package/dist/adapters/fetch/openapi-handler.mjs +11 -0
- package/dist/adapters/fetch/openapi-link.d.mts +15 -0
- package/dist/adapters/fetch/openapi-link.mjs +11 -0
- package/dist/adapters/node/index.d.mts +1 -23
- package/dist/adapters/node/index.mjs +1 -21
- package/dist/adapters/node/openapi-handler.d.mts +14 -0
- package/dist/adapters/node/openapi-handler.mjs +11 -0
- package/dist/adapters/standard/index.d.mts +3 -56
- package/dist/adapters/standard/index.mjs +3 -12
- package/dist/adapters/standard/openapi-handler-codec.d.mts +61 -0
- package/dist/adapters/standard/openapi-handler-codec.mjs +203 -0
- package/dist/adapters/standard/openapi-link-codec.d.mts +53 -0
- package/dist/adapters/standard/openapi-link-codec.mjs +356 -0
- package/dist/adapters/standard/openapi-matcher.d.mts +29 -0
- package/dist/adapters/standard/openapi-matcher.mjs +105 -0
- package/dist/adapters/standard/utils.d.mts +4 -0
- package/dist/adapters/standard/utils.mjs +36 -0
- package/dist/bracket-notation.d.mts +26 -0
- package/dist/bracket-notation.mjs +156 -0
- package/dist/client-factory.d.mts +24 -0
- package/dist/client-factory.mjs +5 -0
- package/dist/constants.d.mts +4 -0
- package/dist/constants.mjs +4 -0
- package/dist/extensions/route.d.mts +74 -39
- package/dist/extensions/route.mjs +8 -10
- package/dist/helpers/form-data.d.mts +57 -0
- package/dist/helpers/form-data.mjs +36 -0
- package/dist/helpers/index.d.mts +1 -59
- package/dist/helpers/index.mjs +1 -39
- package/dist/index.d.mts +13 -166
- package/dist/index.mjs +11 -780
- package/dist/meta.d.mts +345 -0
- package/dist/meta.mjs +56 -0
- package/dist/openapi-generator-components.d.mts +24 -0
- package/dist/openapi-generator-components.mjs +227 -0
- package/dist/openapi-generator-operation.d.mts +29 -0
- package/dist/openapi-generator-operation.mjs +447 -0
- package/dist/openapi-generator.d.mts +87 -0
- package/dist/openapi-generator.mjs +115 -0
- package/dist/openapi-json-serializer.d.mts +86 -0
- package/dist/openapi-json-serializer.mjs +229 -0
- package/dist/openapi-serializer.d.mts +43 -0
- package/dist/openapi-serializer.mjs +87 -0
- package/dist/plugins/index.d.mts +1 -121
- package/dist/plugins/index.mjs +1 -167
- package/dist/plugins/openapi-reference.d.mts +118 -0
- package/dist/plugins/openapi-reference.mjs +157 -0
- package/dist/router-utils.d.mts +18 -0
- package/dist/router-utils.mjs +29 -0
- package/dist/types.d.mts +27 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +14 -0
- package/dist/utils.mjs +40 -0
- package/package.json +15 -15
- package/dist/adapters/aws-lambda/index.d.ts +0 -27
- package/dist/adapters/fastify/index.d.ts +0 -23
- package/dist/adapters/fetch/index.d.ts +0 -37
- package/dist/adapters/node/index.d.ts +0 -23
- package/dist/adapters/standard/index.d.ts +0 -56
- package/dist/extensions/route.d.ts +0 -41
- package/dist/helpers/index.d.ts +0 -59
- package/dist/index.d.ts +0 -166
- package/dist/plugins/index.d.ts +0 -121
- 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.mts +0 -329
- package/dist/shared/openapi.zGZlCOwp.d.ts +0 -329
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import { StandardBody } from '@standard-server/core';
|
|
2
|
-
import { Segment } from '@orpc/shared';
|
|
3
|
-
|
|
4
|
-
type BracketNotationSerializeResult = [string, unknown][];
|
|
5
|
-
interface BracketNotationSerializerOptions {
|
|
6
|
-
/**
|
|
7
|
-
* Maximum number of empty slots that explicit array indexes may leave in total during deserialization
|
|
8
|
-
* (e.g., `a[0]=x&a[3]=y` leaves 2). An index that would exceed it turns its array into an object
|
|
9
|
-
* instead (e.g., `?arr[5000]=x` becomes `{ arr: { 5000: 'x' } }`).
|
|
10
|
-
*
|
|
11
|
-
* Empty slots take memory (a lone `?arr[999]=x` takes ~12KB in V8), and code iterating an array walks
|
|
12
|
-
* every one of them, so this keeps both in line with the input.
|
|
13
|
-
*
|
|
14
|
-
* NOTE: Append-style notation (e.g., `arr[]`) never leaves empty slots.
|
|
15
|
-
*
|
|
16
|
-
* @default 1_000
|
|
17
|
-
*/
|
|
18
|
-
maxDeserializingEmptySlots?: number | undefined;
|
|
19
|
-
}
|
|
20
|
-
declare class BracketNotationSerializer {
|
|
21
|
-
private readonly maxDeserializingEmptySlots;
|
|
22
|
-
constructor(options?: BracketNotationSerializerOptions);
|
|
23
|
-
serialize(data: unknown): BracketNotationSerializeResult;
|
|
24
|
-
private internalSerialize;
|
|
25
|
-
deserialize(serialized: BracketNotationSerializeResult): Record<string, unknown>;
|
|
26
|
-
stringifyPath(segments: readonly Segment[]): string;
|
|
27
|
-
parsePath(path: string): string[];
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
type OpenAPIJsonSerialization = {
|
|
31
|
-
json: unknown;
|
|
32
|
-
maps?: undefined;
|
|
33
|
-
blobs?: undefined;
|
|
34
|
-
} | {
|
|
35
|
-
json: unknown;
|
|
36
|
-
maps: Segment[][];
|
|
37
|
-
blobs: Blob[];
|
|
38
|
-
};
|
|
39
|
-
interface OpenAPIJsonSerializerHandler {
|
|
40
|
-
condition(value: unknown): boolean;
|
|
41
|
-
serialize(value: any): unknown;
|
|
42
|
-
/**
|
|
43
|
-
* If false, the result of this serializer will not be further processed by other serializers,
|
|
44
|
-
* even if it matches their conditions and treat it as final serialized value.
|
|
45
|
-
* This can be useful for serializers that return primitive values, which should not be further processed.
|
|
46
|
-
* to improve performance and avoid potential issues with other serializers.
|
|
47
|
-
*
|
|
48
|
-
* @default false
|
|
49
|
-
*/
|
|
50
|
-
isTerminal?: boolean | undefined;
|
|
51
|
-
}
|
|
52
|
-
interface OpenAPIJsonSerializerOptions {
|
|
53
|
-
/**
|
|
54
|
-
* Extend or override the built-in type handlers used during serialization.
|
|
55
|
-
*
|
|
56
|
-
* Each key is a unique type identifier (e.g. `"date"`, `"bigint"`) and maps to a handler
|
|
57
|
-
* that defines how to detect and serialize values of that type.
|
|
58
|
-
*
|
|
59
|
-
* **Extending:** Add new keys to support custom types:
|
|
60
|
-
* ```ts
|
|
61
|
-
* handlers: {
|
|
62
|
-
* buffer: {
|
|
63
|
-
* condition: (v) => v instanceof Buffer,
|
|
64
|
-
* serialize: (v: Buffer) => v.toString('base64'),
|
|
65
|
-
* isTerminal: true,
|
|
66
|
-
* }
|
|
67
|
-
* }
|
|
68
|
-
* ```
|
|
69
|
-
*
|
|
70
|
-
* **Overriding:** Use an existing key to replace a built-in handler:
|
|
71
|
-
* ```ts
|
|
72
|
-
* handlers: {
|
|
73
|
-
* date: {
|
|
74
|
-
* condition: (v) => v instanceof Date,
|
|
75
|
-
* serialize: (v: Date) => v.getTime(),
|
|
76
|
-
* isTerminal: true,
|
|
77
|
-
* }
|
|
78
|
-
* }
|
|
79
|
-
* ```
|
|
80
|
-
*
|
|
81
|
-
* **Disabling:** Set a key to `undefined` to remove a built-in handler:
|
|
82
|
-
* ```ts
|
|
83
|
-
* handlers: { url: undefined }
|
|
84
|
-
* ```
|
|
85
|
-
*
|
|
86
|
-
* Built-in type keys: `undefined`, `bigint`, `date`, `nan`, `infinity`, `url`, `set`, `map`.
|
|
87
|
-
*/
|
|
88
|
-
handlers?: Record<string, undefined | OpenAPIJsonSerializerHandler> | undefined;
|
|
89
|
-
/**
|
|
90
|
-
* If true, properties with undefined values will be omitted during serialization.
|
|
91
|
-
*
|
|
92
|
-
* @default true
|
|
93
|
-
*/
|
|
94
|
-
omitUndefinedProperties?: boolean | undefined;
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* The JSON layer behind `OpenAPISerializer`: one-way serialization of types like `Date`, `BigInt`,
|
|
98
|
-
* and `Set` into JSON-friendly values, for places that only accept JSON.
|
|
99
|
-
*
|
|
100
|
-
* @see {@link https://orpc.dev/docs/openapi/serializer#openapi-json-serializer | OpenAPI Serializer - OpenAPI JSON Serializer}
|
|
101
|
-
*/
|
|
102
|
-
declare class OpenAPIJsonSerializer {
|
|
103
|
-
private readonly inlineBuiltInHandlers;
|
|
104
|
-
private readonly handlerEntries;
|
|
105
|
-
private readonly omitUndefinedProperties;
|
|
106
|
-
constructor(options?: OpenAPIJsonSerializerOptions);
|
|
107
|
-
serializeJson(data: unknown): OpenAPIJsonSerialization;
|
|
108
|
-
/**
|
|
109
|
-
* `segments` is a shared mutable stack (push/pop while walking),
|
|
110
|
-
* so it must be copied before being stored in `maps`.
|
|
111
|
-
*/
|
|
112
|
-
private serializeValue;
|
|
113
|
-
deserializeJson(serialized: OpenAPIJsonSerialization): unknown;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
interface OpenAPISerializerSerializeOptions {
|
|
117
|
-
/**
|
|
118
|
-
* Use FormData for serialization when nested blobs are present.
|
|
119
|
-
* Does not apply to root-level Blob values.
|
|
120
|
-
*
|
|
121
|
-
* @default true
|
|
122
|
-
*/
|
|
123
|
-
useFormDataForBlobFields?: boolean | undefined;
|
|
124
|
-
/**
|
|
125
|
-
* When enabled, the serialized output is always returned as a FormData instance using bracket notation.
|
|
126
|
-
*
|
|
127
|
-
* @default false
|
|
128
|
-
*/
|
|
129
|
-
asFormData?: boolean | undefined;
|
|
130
|
-
}
|
|
131
|
-
interface OpenAPISerializerOptions extends OpenAPIJsonSerializerOptions {
|
|
132
|
-
/**
|
|
133
|
-
* Options for bracket notation serializer, like maxDeserializingEmptySlots
|
|
134
|
-
*/
|
|
135
|
-
bracketNotation?: BracketNotationSerializerOptions | undefined;
|
|
136
|
-
/**
|
|
137
|
-
* Default options for serialize method
|
|
138
|
-
*/
|
|
139
|
-
serialize?: OpenAPISerializerSerializeOptions | undefined;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Handles one-way serialization of oRPC payloads into JSON-friendly formats,
|
|
143
|
-
* partially supporting complex data types beyond plain JSON such as `Date`, `BigInt`, and `Set`.
|
|
144
|
-
*
|
|
145
|
-
* @see {@link https://orpc.dev/docs/openapi/serializer | OpenAPI Serializer}
|
|
146
|
-
*/
|
|
147
|
-
declare class OpenAPISerializer extends OpenAPIJsonSerializer {
|
|
148
|
-
private readonly bracketNotation;
|
|
149
|
-
private readonly defaultSerializeOptions;
|
|
150
|
-
constructor({ bracketNotation, serialize, ...options }?: OpenAPISerializerOptions);
|
|
151
|
-
serialize(data: unknown, options?: OpenAPISerializerSerializeOptions): StandardBody;
|
|
152
|
-
private serializePayload;
|
|
153
|
-
deserialize(data: StandardBody): unknown;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export { OpenAPISerializer as O, OpenAPIJsonSerializer as a, BracketNotationSerializer as b };
|
|
157
|
-
export type { BracketNotationSerializeResult as B, BracketNotationSerializerOptions as c, OpenAPIJsonSerialization as d, OpenAPIJsonSerializerHandler as e, OpenAPIJsonSerializerOptions as f, OpenAPISerializerOptions as g, OpenAPISerializerSerializeOptions as h };
|
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
import { COMMON_ERROR_STATUS_MAP } from '@orpc/client';
|
|
2
|
-
import { walkProcedureContractsSync, unlazy, resolveContractProcedure, Procedure, DEFAULT_SUCCESS_STATUS, DEFAULT_ERROR_STATUS } from '@orpc/server';
|
|
3
|
-
import { value, pathToHttpPath, mergeHttpPath, safeDecodeURIComponent, safeEncodeURIComponent, setOwn, NullProtoObj, isTypescriptObject, isPlainObject, stringifyJSON, getOwn, parseEmptyableJSON } from '@orpc/shared';
|
|
4
|
-
import { parseStandardUrl } from '@standard-server/core';
|
|
5
|
-
import { D as DEFAULT_OPENAPI_METHOD, g as getDynamicPathParams, O as OpenAPISerializer, a as DEFAULT_OPENAPI_INPUT_STRUCTURE, i as isBodylessMethod, b as DEFAULT_OPENAPI_OUTPUT_STRUCTURE } from './openapi.E3ZBiweM.mjs';
|
|
6
|
-
import { g as getOpenAPIMeta } from './openapi.B9PQzqBn.mjs';
|
|
7
|
-
import { createRouter, addRoute, findRoute, findAllRoutes, routeToRegExp } from 'rou3';
|
|
8
|
-
|
|
9
|
-
const ROU3_RAW_CHARS = String.raw`\w!$&'()*+,\-.:;=@[\\\]|~`;
|
|
10
|
-
const NORMALIZABLE_PATHNAME_REGEX = new RegExp(`[^${ROU3_RAW_CHARS}/]`);
|
|
11
|
-
const ROU3_ENCODED_SEGMENT_TEXT_REGEX = new RegExp(`[^${ROU3_RAW_CHARS}]+`, "g");
|
|
12
|
-
class OpenAPIMatcher {
|
|
13
|
-
filter;
|
|
14
|
-
rootRouter;
|
|
15
|
-
tree = createRouter();
|
|
16
|
-
pendingLazyRouters = /* @__PURE__ */ new Set();
|
|
17
|
-
constructor(router, options = {}) {
|
|
18
|
-
this.filter = options.filter ?? true;
|
|
19
|
-
this.rootRouter = router;
|
|
20
|
-
this.index(router);
|
|
21
|
-
}
|
|
22
|
-
index(router, path = []) {
|
|
23
|
-
const routes = [];
|
|
24
|
-
const lazyResults = walkProcedureContractsSync(router, (contract, path2) => {
|
|
25
|
-
if (!value(this.filter, contract, path2)) {
|
|
26
|
-
return;
|
|
27
|
-
}
|
|
28
|
-
const meta = getOpenAPIMeta(contract);
|
|
29
|
-
const method = meta?.method ?? DEFAULT_OPENAPI_METHOD;
|
|
30
|
-
const postHttpPath = meta?.path ?? pathToHttpPath(path2);
|
|
31
|
-
const openapiPath = meta?.prefix ? mergeHttpPath(meta.prefix, postHttpPath) : postHttpPath;
|
|
32
|
-
const { pattern, ...route } = toRou3Route(openapiPath);
|
|
33
|
-
routes.push({
|
|
34
|
-
method,
|
|
35
|
-
pattern,
|
|
36
|
-
entry: {
|
|
37
|
-
path: path2,
|
|
38
|
-
contract,
|
|
39
|
-
procedure: contract instanceof Procedure ? contract : void 0,
|
|
40
|
-
...route
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
}, path);
|
|
44
|
-
const pendingLazyRouters = lazyResults.map((result) => {
|
|
45
|
-
const prefix = getOpenAPIMeta(result.router)?.prefix;
|
|
46
|
-
return {
|
|
47
|
-
...result,
|
|
48
|
-
matcher: prefix ? toRou3PrefixMatcher(prefix) : void 0
|
|
49
|
-
};
|
|
50
|
-
});
|
|
51
|
-
for (const { method, pattern, entry } of routes) {
|
|
52
|
-
addRoute(this.tree, method, pattern, entry);
|
|
53
|
-
}
|
|
54
|
-
for (const pending of pendingLazyRouters) {
|
|
55
|
-
this.pendingLazyRouters.add(pending);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
async match(method, pathname, prefix) {
|
|
59
|
-
if (prefix) {
|
|
60
|
-
if (!pathname.startsWith(prefix)) {
|
|
61
|
-
return void 0;
|
|
62
|
-
}
|
|
63
|
-
const charAfterPrefix = pathname[prefix.length];
|
|
64
|
-
if (charAfterPrefix === "/") {
|
|
65
|
-
pathname = pathname.slice(prefix.length);
|
|
66
|
-
} else if (charAfterPrefix === void 0) {
|
|
67
|
-
pathname = "/";
|
|
68
|
-
} else if (prefix[prefix.length - 1] === "/") {
|
|
69
|
-
pathname = pathname.slice(prefix.length - 1);
|
|
70
|
-
} else {
|
|
71
|
-
return void 0;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
pathname = normalizeRou3Path(pathname);
|
|
75
|
-
const loading = this.resolvePendingLazyRouters(pathname);
|
|
76
|
-
if (loading !== void 0) {
|
|
77
|
-
await loading;
|
|
78
|
-
}
|
|
79
|
-
const match = this.findMatch(method, pathname);
|
|
80
|
-
if (match === void 0) {
|
|
81
|
-
return void 0;
|
|
82
|
-
}
|
|
83
|
-
const entry = match.data;
|
|
84
|
-
return {
|
|
85
|
-
path: entry.path,
|
|
86
|
-
procedure: entry.procedure ?? await this.resolveProcedure(entry),
|
|
87
|
-
params: entry.params && decodeParams(entry.params, match.params)
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
findMatch(method, pathname) {
|
|
91
|
-
const match = findRoute(this.tree, method, pathname);
|
|
92
|
-
if (match === void 0 || !hasEmptyCatchAll(match)) {
|
|
93
|
-
return match;
|
|
94
|
-
}
|
|
95
|
-
return findAllRoutes(this.tree, method, pathname).reverse().find((match2) => !hasEmptyCatchAll(match2));
|
|
96
|
-
}
|
|
97
|
-
resolvePendingLazyRouters(pathname) {
|
|
98
|
-
for (const pending of this.pendingLazyRouters) {
|
|
99
|
-
if (pending.matcher === void 0 || pending.matcher.test(pathname)) {
|
|
100
|
-
return this.loadPendingLazyRouters(pathname);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
async loadPendingLazyRouters(pathname) {
|
|
105
|
-
for (const pending of this.pendingLazyRouters) {
|
|
106
|
-
if (pending.matcher === void 0 || pending.matcher.test(pathname)) {
|
|
107
|
-
await this.loadPendingLazyRouter(pending);
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
loadPendingLazyRouter(pending) {
|
|
112
|
-
if (pending.loading === void 0) {
|
|
113
|
-
pending.loading = this.indexPendingLazyRouter(pending).catch((error) => {
|
|
114
|
-
pending.loading = void 0;
|
|
115
|
-
throw error;
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
return pending.loading;
|
|
119
|
-
}
|
|
120
|
-
async indexPendingLazyRouter(pending) {
|
|
121
|
-
const { default: router } = await unlazy(pending.router);
|
|
122
|
-
this.index(router, pending.path);
|
|
123
|
-
this.pendingLazyRouters.delete(pending);
|
|
124
|
-
}
|
|
125
|
-
async resolveProcedure(entry) {
|
|
126
|
-
entry.procedure = await resolveContractProcedure(this.rootRouter, entry.contract, entry.path);
|
|
127
|
-
return entry.procedure;
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
function toRou3Route(path) {
|
|
131
|
-
const dynamicParams = getDynamicPathParams(path);
|
|
132
|
-
if (!dynamicParams) {
|
|
133
|
-
return { pattern: toRou3Literal(path) };
|
|
134
|
-
}
|
|
135
|
-
let pattern = "";
|
|
136
|
-
let literalStart = 0;
|
|
137
|
-
let catchAllKey;
|
|
138
|
-
const params = [];
|
|
139
|
-
for (const param of dynamicParams) {
|
|
140
|
-
const key = `p${params.length}`;
|
|
141
|
-
params.push([key, param.parameterName]);
|
|
142
|
-
pattern += toRou3Literal(path.slice(literalStart, param.startIndex));
|
|
143
|
-
literalStart = param.startIndex + param.segment.length;
|
|
144
|
-
if (param.allowsSlash) {
|
|
145
|
-
if (catchAllKey !== void 0) {
|
|
146
|
-
throw new TypeError(`OpenAPI path "${path}" has more than one catch-all param ({+name}), but only one is supported per path.`);
|
|
147
|
-
}
|
|
148
|
-
catchAllKey = key;
|
|
149
|
-
pattern += `:${key}(.*)`;
|
|
150
|
-
} else {
|
|
151
|
-
pattern += `:${key}`;
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
pattern += toRou3Literal(path.slice(literalStart));
|
|
155
|
-
return { pattern, params, catchAllKey };
|
|
156
|
-
}
|
|
157
|
-
const ROU3_SYNTAX_REGEX = /[\\:*?+(){}]|(?<![^/])\.(?=\.?(?:\/|$))/g;
|
|
158
|
-
function toRou3Literal(text) {
|
|
159
|
-
return normalizeRou3Path(text).replace(ROU3_SYNTAX_REGEX, "\\$&");
|
|
160
|
-
}
|
|
161
|
-
function normalizeRou3Path(path) {
|
|
162
|
-
if (!NORMALIZABLE_PATHNAME_REGEX.test(path)) {
|
|
163
|
-
return path;
|
|
164
|
-
}
|
|
165
|
-
return path.split("/").map((segment) => NORMALIZABLE_PATHNAME_REGEX.test(segment) ? safeDecodeURIComponent(segment).replace(ROU3_ENCODED_SEGMENT_TEXT_REGEX, safeEncodeURIComponent) : segment).join("/");
|
|
166
|
-
}
|
|
167
|
-
function toRou3PrefixMatcher(prefix) {
|
|
168
|
-
const catchAll = getDynamicPathParams(prefix)?.find((param) => param.allowsSlash);
|
|
169
|
-
const head = catchAll ? prefix.slice(0, catchAll.startIndex) : prefix;
|
|
170
|
-
return routeToRegExp(mergeHttpPath(toRou3Route(head).pattern, "/**"));
|
|
171
|
-
}
|
|
172
|
-
function hasEmptyCatchAll(match) {
|
|
173
|
-
return match.data.catchAllKey !== void 0 && !match.params?.[match.data.catchAllKey];
|
|
174
|
-
}
|
|
175
|
-
function decodeParams(params, values) {
|
|
176
|
-
const decoded = {};
|
|
177
|
-
for (const [rou3Key, name] of params) {
|
|
178
|
-
setOwn(decoded, name, safeDecodeURIComponent(values[rou3Key]));
|
|
179
|
-
}
|
|
180
|
-
return decoded;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
function serializeHeaders(headers, serializer) {
|
|
184
|
-
const result = new NullProtoObj();
|
|
185
|
-
for (const [key, value] of Object.entries(headers)) {
|
|
186
|
-
if (Array.isArray(value)) {
|
|
187
|
-
const lines = [];
|
|
188
|
-
for (const item of value) {
|
|
189
|
-
const line2 = serializeHeaderValue(item, serializer);
|
|
190
|
-
if (line2 !== void 0) {
|
|
191
|
-
lines.push(line2);
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
result[key] = lines;
|
|
195
|
-
continue;
|
|
196
|
-
}
|
|
197
|
-
const line = serializeHeaderValue(value, serializer);
|
|
198
|
-
if (line !== void 0) {
|
|
199
|
-
result[key] = line;
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return result;
|
|
203
|
-
}
|
|
204
|
-
function serializeHeaderValue(value, serializer) {
|
|
205
|
-
const serialized = serializer.serializeJson(value).json;
|
|
206
|
-
if (Array.isArray(serialized)) {
|
|
207
|
-
return serialized.filter((item) => item !== void 0 && item !== null).map(String).join(",");
|
|
208
|
-
}
|
|
209
|
-
if (isTypescriptObject(serialized)) {
|
|
210
|
-
return Object.entries(serialized).filter(([, val]) => val !== void 0 && val !== null).map(([key, val]) => `${String(key)},${String(val)}`).join(",");
|
|
211
|
-
}
|
|
212
|
-
if (serialized !== void 0 && serialized !== null) {
|
|
213
|
-
return String(serialized);
|
|
214
|
-
}
|
|
215
|
-
return void 0;
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
class OpenAPIHandlerCodecCore {
|
|
219
|
-
serializer;
|
|
220
|
-
errorStatusMap;
|
|
221
|
-
errorResponseBodyEncoder;
|
|
222
|
-
errorResponseContentType;
|
|
223
|
-
constructor(options = {}) {
|
|
224
|
-
this.serializer = options.serializer ?? new OpenAPISerializer();
|
|
225
|
-
this.errorStatusMap = options.errorStatusMap ?? COMMON_ERROR_STATUS_MAP;
|
|
226
|
-
this.errorResponseBodyEncoder = options.errorResponseBodyEncoder;
|
|
227
|
-
this.errorResponseContentType = options.errorResponseContentType;
|
|
228
|
-
}
|
|
229
|
-
async decodeInput(matched, request) {
|
|
230
|
-
const meta = getOpenAPIMeta(matched.procedure);
|
|
231
|
-
const inputStructure = meta?.inputStructure ?? DEFAULT_OPENAPI_INPUT_STRUCTURE;
|
|
232
|
-
const params = this.deserializeParams(matched.params, meta?.paramsStyles);
|
|
233
|
-
if (inputStructure === "compact") {
|
|
234
|
-
const data = isBodylessMethod(request.method) ? this.deserializeQuery(request.url, meta?.queryStyles) : this.serializer.deserialize(await request.resolveBody(meta?.requestBodyHint));
|
|
235
|
-
if (data === void 0) {
|
|
236
|
-
return params;
|
|
237
|
-
}
|
|
238
|
-
if (!params || Object.keys(params).length < 1) {
|
|
239
|
-
return data;
|
|
240
|
-
}
|
|
241
|
-
if (!isPlainObject(data)) {
|
|
242
|
-
return params;
|
|
243
|
-
}
|
|
244
|
-
return {
|
|
245
|
-
...data,
|
|
246
|
-
...params
|
|
247
|
-
};
|
|
248
|
-
}
|
|
249
|
-
return {
|
|
250
|
-
params,
|
|
251
|
-
query: this.deserializeQuery(request.url, meta?.queryStyles),
|
|
252
|
-
headers: request.headers,
|
|
253
|
-
body: this.serializer.deserialize(await request.resolveBody(meta?.requestBodyHint))
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* @throws {TypeError} If `outputStructure` is "detailed" and the output doesn't match the expected structure.
|
|
258
|
-
*/
|
|
259
|
-
encodeOutput(output, procedure, path) {
|
|
260
|
-
const meta = getOpenAPIMeta(procedure);
|
|
261
|
-
const successStatus = meta?.successStatus ?? DEFAULT_SUCCESS_STATUS;
|
|
262
|
-
const outputStructure = meta?.outputStructure ?? DEFAULT_OPENAPI_OUTPUT_STRUCTURE;
|
|
263
|
-
if (outputStructure === "compact") {
|
|
264
|
-
return {
|
|
265
|
-
status: successStatus,
|
|
266
|
-
headers: {},
|
|
267
|
-
body: this.serializer.serialize(output)
|
|
268
|
-
};
|
|
269
|
-
}
|
|
270
|
-
if (!isValidDetailedOutput(output)) {
|
|
271
|
-
throw new TypeError(`
|
|
272
|
-
Invalid "detailed" output structure returned by procedure (${path.join(".")}):
|
|
273
|
-
\u2022 Expected an object with optional properties:
|
|
274
|
-
- status (number <400)
|
|
275
|
-
- headers (object)
|
|
276
|
-
- body (any)
|
|
277
|
-
\u2022 No extra keys allowed.
|
|
278
|
-
|
|
279
|
-
Actual value:
|
|
280
|
-
${stringifyJSON(output)}
|
|
281
|
-
`);
|
|
282
|
-
}
|
|
283
|
-
return {
|
|
284
|
-
status: output.status ?? successStatus,
|
|
285
|
-
headers: output.headers !== void 0 ? serializeHeaders(output.headers, this.serializer) : {},
|
|
286
|
-
body: this.serializer.serialize(output.body)
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
encodeError(error) {
|
|
290
|
-
const status = getOwn(this.errorStatusMap, error.code) ?? DEFAULT_ERROR_STATUS;
|
|
291
|
-
return {
|
|
292
|
-
status,
|
|
293
|
-
headers: this.errorResponseContentType !== void 0 ? { "content-type": this.errorResponseContentType } : {},
|
|
294
|
-
body: this.serializer.serialize(this.errorResponseBodyEncoder?.(error) ?? error.toJSON())
|
|
295
|
-
};
|
|
296
|
-
}
|
|
297
|
-
deserializeQuery(url, styles) {
|
|
298
|
-
const [, search] = parseStandardUrl(url);
|
|
299
|
-
const searchParams = new URLSearchParams(search);
|
|
300
|
-
const parsed = this.serializer.deserialize(searchParams);
|
|
301
|
-
if (!styles || !isPlainObject(parsed)) {
|
|
302
|
-
return parsed;
|
|
303
|
-
}
|
|
304
|
-
Object.entries(styles).forEach(([key, hint]) => {
|
|
305
|
-
if (hint === void 0) {
|
|
306
|
-
return;
|
|
307
|
-
}
|
|
308
|
-
const values = searchParams.getAll(key);
|
|
309
|
-
let parsedValue;
|
|
310
|
-
if (hint === "primitive") {
|
|
311
|
-
parsedValue = values.at(-1);
|
|
312
|
-
} else if (hint === "array") {
|
|
313
|
-
parsedValue = values;
|
|
314
|
-
} else if (hint === "comma-delimited-array") {
|
|
315
|
-
parsedValue = decodeDelimitedArray(values.at(-1), ",");
|
|
316
|
-
} else if (hint === "comma-delimited-object") {
|
|
317
|
-
parsedValue = decodeDelimitedObject(values.at(-1), ",");
|
|
318
|
-
} else if (hint === "space-delimited-array") {
|
|
319
|
-
parsedValue = decodeDelimitedArray(values.at(-1), " ");
|
|
320
|
-
} else if (hint === "space-delimited-object") {
|
|
321
|
-
parsedValue = decodeDelimitedObject(values.at(-1), " ");
|
|
322
|
-
} else if (hint === "pipe-delimited-array") {
|
|
323
|
-
parsedValue = decodeDelimitedArray(values.at(-1), "|");
|
|
324
|
-
} else if (hint === "pipe-delimited-object") {
|
|
325
|
-
parsedValue = decodeDelimitedObject(values.at(-1), "|");
|
|
326
|
-
} else {
|
|
327
|
-
const last = values.at(-1);
|
|
328
|
-
try {
|
|
329
|
-
parsedValue = parseEmptyableJSON(last);
|
|
330
|
-
} catch {
|
|
331
|
-
parsedValue = last;
|
|
332
|
-
}
|
|
333
|
-
}
|
|
334
|
-
parsed[key] = this.serializer.deserialize(parsedValue);
|
|
335
|
-
});
|
|
336
|
-
return parsed;
|
|
337
|
-
}
|
|
338
|
-
deserializeParams(params, styles) {
|
|
339
|
-
if (!params || !styles) {
|
|
340
|
-
return params;
|
|
341
|
-
}
|
|
342
|
-
const parsed = { ...params };
|
|
343
|
-
Object.entries(styles).forEach(([key, hint]) => {
|
|
344
|
-
const value = getOwn(params, key);
|
|
345
|
-
if (hint === void 0 || hint === "primitive" || value === void 0) {
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
if (hint === "comma-delimited-array") {
|
|
349
|
-
parsed[key] = decodeDelimitedArray(value, ",");
|
|
350
|
-
} else {
|
|
351
|
-
parsed[key] = decodeDelimitedObject(value, ",");
|
|
352
|
-
}
|
|
353
|
-
});
|
|
354
|
-
return parsed;
|
|
355
|
-
}
|
|
356
|
-
}
|
|
357
|
-
class OpenAPIHandlerCodec extends OpenAPIHandlerCodecCore {
|
|
358
|
-
matcher;
|
|
359
|
-
constructor(router, options = {}) {
|
|
360
|
-
super(options);
|
|
361
|
-
this.matcher = new OpenAPIMatcher(router, options);
|
|
362
|
-
}
|
|
363
|
-
async resolveProcedure(request, options) {
|
|
364
|
-
const [pathname] = parseStandardUrl(request.url);
|
|
365
|
-
const matched = await this.matcher.match(request.method, pathname, options.prefix);
|
|
366
|
-
if (!matched) {
|
|
367
|
-
return void 0;
|
|
368
|
-
}
|
|
369
|
-
return {
|
|
370
|
-
procedure: matched.procedure,
|
|
371
|
-
path: matched.path,
|
|
372
|
-
decodeInput: () => this.decodeInput(matched, request)
|
|
373
|
-
};
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
function isValidDetailedOutput(output) {
|
|
377
|
-
if (!isTypescriptObject(output)) {
|
|
378
|
-
return false;
|
|
379
|
-
}
|
|
380
|
-
if (Object.keys(output).some((key) => key !== "status" && key !== "headers" && key !== "body")) {
|
|
381
|
-
return false;
|
|
382
|
-
}
|
|
383
|
-
if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || output.status > 399)) {
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
if (output.headers !== void 0 && !isTypescriptObject(output.headers)) {
|
|
387
|
-
return false;
|
|
388
|
-
}
|
|
389
|
-
return true;
|
|
390
|
-
}
|
|
391
|
-
function decodeDelimitedArray(value, delimiter) {
|
|
392
|
-
if (value === void 0) {
|
|
393
|
-
return void 0;
|
|
394
|
-
}
|
|
395
|
-
return value.split(delimiter);
|
|
396
|
-
}
|
|
397
|
-
function decodeDelimitedObject(value, delimiter) {
|
|
398
|
-
if (value === void 0) {
|
|
399
|
-
return void 0;
|
|
400
|
-
}
|
|
401
|
-
const obj = new NullProtoObj();
|
|
402
|
-
const parts = value.split(delimiter);
|
|
403
|
-
for (let i = 0; i < parts.length; i += 2) {
|
|
404
|
-
const key = parts[i];
|
|
405
|
-
obj[key] = parts[i + 1];
|
|
406
|
-
}
|
|
407
|
-
return obj;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
export { OpenAPIHandlerCodec as O, OpenAPIHandlerCodecCore as a, OpenAPIMatcher as b, serializeHeaders as s };
|