@temporary-name/server 1.9.3-alpha.4275e976ddda4d8be107c2cfde9899bdea9a337d → 1.9.3-alpha.50b729ba628b987e14f5bd1004e5a04590fd4b4e
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.mts +12 -6
- package/dist/adapters/aws-lambda/index.d.ts +12 -6
- package/dist/adapters/aws-lambda/index.mjs +12 -4
- package/dist/adapters/fetch/index.d.mts +12 -6
- package/dist/adapters/fetch/index.d.ts +12 -6
- package/dist/adapters/fetch/index.mjs +12 -11
- package/dist/adapters/node/index.d.mts +12 -6
- package/dist/adapters/node/index.d.ts +12 -6
- package/dist/adapters/node/index.mjs +12 -11
- package/dist/adapters/standard/index.d.mts +23 -13
- package/dist/adapters/standard/index.d.ts +23 -13
- package/dist/adapters/standard/index.mjs +8 -100
- package/dist/index.d.mts +44 -368
- package/dist/index.d.ts +44 -368
- package/dist/index.mjs +108 -152
- package/dist/openapi/index.d.mts +220 -0
- package/dist/openapi/index.d.ts +220 -0
- package/dist/openapi/index.mjs +776 -0
- package/dist/plugins/index.d.mts +2 -2
- package/dist/plugins/index.d.ts +2 -2
- package/dist/shared/server.BEHw7Eyx.mjs +247 -0
- package/dist/shared/server.BVxcyR6X.mjs +287 -0
- package/dist/shared/{server.Btxrgkj5.d.ts → server.Bk5r0-2R.d.ts} +7 -24
- package/dist/shared/server.Bs6ka_UE.d.mts +23 -0
- package/dist/shared/{server.C1YnHvvf.d.ts → server.CZNLCQBm.d.mts} +3 -3
- package/dist/shared/{server.C1YnHvvf.d.mts → server.CZNLCQBm.d.ts} +3 -3
- package/dist/shared/{server.Bo94xDTv.d.mts → server.CbLTWfgn.d.mts} +7 -24
- package/dist/shared/server.D2UFMrxf.d.ts +23 -0
- package/dist/shared/{server.BEQrAa3A.mjs → server.DcfsPloY.mjs} +16 -21
- package/package.json +19 -13
- package/dist/shared/server.D6K9uoPI.mjs +0 -35
- package/dist/shared/server.DZ5BIITo.mjs +0 -9
- package/dist/shared/server.X0YaZxSJ.mjs +0 -13
package/dist/plugins/index.d.mts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Value, Promisable, ORPCError } from '@temporary-name/shared';
|
|
2
2
|
import { StandardRequest, StandardHeaders } from '@temporary-name/standard-server';
|
|
3
3
|
import { BatchResponseBodyItem } from '@temporary-name/standard-server/batch';
|
|
4
|
-
import {
|
|
5
|
-
import { C as Context,
|
|
4
|
+
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.CbLTWfgn.mjs';
|
|
5
|
+
import { C as Context, E as ProcedureClientInterceptorOptions } from '../shared/server.CZNLCQBm.mjs';
|
|
6
6
|
import { Meta } from '@temporary-name/contract';
|
|
7
7
|
|
|
8
8
|
interface BatchHandlerOptions<T extends Context> {
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Value, Promisable, ORPCError } from '@temporary-name/shared';
|
|
2
2
|
import { StandardRequest, StandardHeaders } from '@temporary-name/standard-server';
|
|
3
3
|
import { BatchResponseBodyItem } from '@temporary-name/standard-server/batch';
|
|
4
|
-
import {
|
|
5
|
-
import { C as Context,
|
|
4
|
+
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.Bk5r0-2R.js';
|
|
5
|
+
import { C as Context, E as ProcedureClientInterceptorOptions } from '../shared/server.CZNLCQBm.js';
|
|
6
6
|
import { Meta } from '@temporary-name/contract';
|
|
7
7
|
|
|
8
8
|
interface BatchHandlerOptions<T extends Context> {
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
import { isObject, NullProtoObj, isAsyncIteratorObject, isORPCErrorJson, createORPCErrorFromJson, toORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { mapEventIterator, ErrorEvent } from '@temporary-name/standard-server';
|
|
3
|
+
|
|
4
|
+
function bracketNotationSerialize(data, segments = [], result = []) {
|
|
5
|
+
if (Array.isArray(data)) {
|
|
6
|
+
data.forEach((item, i) => {
|
|
7
|
+
bracketNotationSerialize(item, [...segments, i], result);
|
|
8
|
+
});
|
|
9
|
+
} else if (isObject(data)) {
|
|
10
|
+
for (const key in data) {
|
|
11
|
+
bracketNotationSerialize(data[key], [...segments, key], result);
|
|
12
|
+
}
|
|
13
|
+
} else {
|
|
14
|
+
result.push([stringifyPath(segments), data]);
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
function bracketNotationDeserialize(serialized, { maxArrayIndex = 9999 } = {}) {
|
|
19
|
+
if (serialized.length === 0) {
|
|
20
|
+
return {};
|
|
21
|
+
}
|
|
22
|
+
const arrayPushStyles = /* @__PURE__ */ new WeakSet();
|
|
23
|
+
const ref = { value: [] };
|
|
24
|
+
for (const [path, value] of serialized) {
|
|
25
|
+
const segments = parsePath(path);
|
|
26
|
+
let currentRef = ref;
|
|
27
|
+
let nextSegment = "value";
|
|
28
|
+
segments.forEach((segment, i) => {
|
|
29
|
+
if (!Array.isArray(currentRef[nextSegment]) && !isObject(currentRef[nextSegment])) {
|
|
30
|
+
currentRef[nextSegment] = [];
|
|
31
|
+
}
|
|
32
|
+
if (i !== segments.length - 1) {
|
|
33
|
+
if (Array.isArray(currentRef[nextSegment]) && !isValidArrayIndex(segment, maxArrayIndex)) {
|
|
34
|
+
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
35
|
+
arrayPushStyles.delete(currentRef[nextSegment]);
|
|
36
|
+
currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
|
|
37
|
+
} else {
|
|
38
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
if (Array.isArray(currentRef[nextSegment])) {
|
|
43
|
+
if (segment === "") {
|
|
44
|
+
if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
|
|
45
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
46
|
+
}
|
|
47
|
+
} else {
|
|
48
|
+
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
49
|
+
arrayPushStyles.delete(currentRef[nextSegment]);
|
|
50
|
+
currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
|
|
51
|
+
} else if (!isValidArrayIndex(segment, maxArrayIndex)) {
|
|
52
|
+
currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
currentRef = currentRef[nextSegment];
|
|
58
|
+
nextSegment = segment;
|
|
59
|
+
});
|
|
60
|
+
if (Array.isArray(currentRef) && nextSegment === "") {
|
|
61
|
+
arrayPushStyles.add(currentRef);
|
|
62
|
+
currentRef.push(value);
|
|
63
|
+
} else if (nextSegment in currentRef) {
|
|
64
|
+
if (Array.isArray(currentRef[nextSegment])) {
|
|
65
|
+
currentRef[nextSegment].push(value);
|
|
66
|
+
} else {
|
|
67
|
+
currentRef[nextSegment] = [currentRef[nextSegment], value];
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
currentRef[nextSegment] = value;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return ref.value;
|
|
74
|
+
}
|
|
75
|
+
function stringifyPath(segments) {
|
|
76
|
+
return segments.map((segment) => {
|
|
77
|
+
return segment.toString().replace(/[\\[\]]/g, (match) => {
|
|
78
|
+
switch (match) {
|
|
79
|
+
case "\\":
|
|
80
|
+
return "\\\\";
|
|
81
|
+
case "[":
|
|
82
|
+
return "\\[";
|
|
83
|
+
case "]":
|
|
84
|
+
return "\\]";
|
|
85
|
+
/* v8 ignore next 2 */
|
|
86
|
+
default:
|
|
87
|
+
return match;
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
}).reduce((result, segment, i) => {
|
|
91
|
+
if (i === 0) {
|
|
92
|
+
return segment;
|
|
93
|
+
}
|
|
94
|
+
return `${result}[${segment}]`;
|
|
95
|
+
}, "");
|
|
96
|
+
}
|
|
97
|
+
function parsePath(path) {
|
|
98
|
+
const segments = [];
|
|
99
|
+
let inBrackets = false;
|
|
100
|
+
let currentSegment = "";
|
|
101
|
+
let backslashCount = 0;
|
|
102
|
+
for (let i = 0; i < path.length; i++) {
|
|
103
|
+
const char = path[i];
|
|
104
|
+
const nextChar = path[i + 1];
|
|
105
|
+
if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
|
|
106
|
+
if (nextChar === void 0) {
|
|
107
|
+
inBrackets = false;
|
|
108
|
+
}
|
|
109
|
+
segments.push(currentSegment);
|
|
110
|
+
currentSegment = "";
|
|
111
|
+
i++;
|
|
112
|
+
} else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
|
|
113
|
+
inBrackets = true;
|
|
114
|
+
segments.push(currentSegment);
|
|
115
|
+
currentSegment = "";
|
|
116
|
+
} else if (char === "\\") {
|
|
117
|
+
backslashCount++;
|
|
118
|
+
} else {
|
|
119
|
+
currentSegment += "\\".repeat(backslashCount / 2) + char;
|
|
120
|
+
backslashCount = 0;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return inBrackets || segments.length === 0 ? [path] : segments;
|
|
124
|
+
}
|
|
125
|
+
function isValidArrayIndex(value, maxIndex) {
|
|
126
|
+
return /^0$|^[1-9]\d*$/.test(value) && Number(value) <= maxIndex;
|
|
127
|
+
}
|
|
128
|
+
function arrayToObject(array) {
|
|
129
|
+
const obj = new NullProtoObj();
|
|
130
|
+
array.forEach((item, i) => {
|
|
131
|
+
obj[i] = item;
|
|
132
|
+
});
|
|
133
|
+
return obj;
|
|
134
|
+
}
|
|
135
|
+
function pushStyleArrayToObject(array) {
|
|
136
|
+
const obj = new NullProtoObj();
|
|
137
|
+
obj[""] = array.length === 1 ? array[0] : array;
|
|
138
|
+
return obj;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function jsonSerialize(data, hasBlobRef = { value: false }) {
|
|
142
|
+
if (data instanceof Blob) {
|
|
143
|
+
hasBlobRef.value = true;
|
|
144
|
+
return [data, hasBlobRef.value];
|
|
145
|
+
}
|
|
146
|
+
if (data instanceof Set) {
|
|
147
|
+
return jsonSerialize(Array.from(data), hasBlobRef);
|
|
148
|
+
}
|
|
149
|
+
if (data instanceof Map) {
|
|
150
|
+
return jsonSerialize(Array.from(data.entries()), hasBlobRef);
|
|
151
|
+
}
|
|
152
|
+
if (Array.isArray(data)) {
|
|
153
|
+
const json = data.map((v) => v === void 0 ? null : jsonSerialize(v, hasBlobRef)[0]);
|
|
154
|
+
return [json, hasBlobRef.value];
|
|
155
|
+
}
|
|
156
|
+
if (isObject(data)) {
|
|
157
|
+
const json = {};
|
|
158
|
+
for (const k in data) {
|
|
159
|
+
if (k === "toJSON" && typeof data[k] === "function") {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
json[k] = jsonSerialize(data[k], hasBlobRef)[0];
|
|
163
|
+
}
|
|
164
|
+
return [json, hasBlobRef.value];
|
|
165
|
+
}
|
|
166
|
+
if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
|
|
167
|
+
return [data.toString(), hasBlobRef.value];
|
|
168
|
+
}
|
|
169
|
+
if (data instanceof Date) {
|
|
170
|
+
return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
|
|
171
|
+
}
|
|
172
|
+
if (Number.isNaN(data)) {
|
|
173
|
+
return [null, hasBlobRef.value];
|
|
174
|
+
}
|
|
175
|
+
return [data, hasBlobRef.value];
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function serialize(data, options = {}) {
|
|
179
|
+
if (isAsyncIteratorObject(data) && !options.outputFormat) {
|
|
180
|
+
return mapEventIterator(data, {
|
|
181
|
+
value: async (value) => _serialize(value, { outputFormat: "plain" }),
|
|
182
|
+
error: async (e) => {
|
|
183
|
+
return new ErrorEvent({
|
|
184
|
+
data: _serialize(toORPCError(e).toJSON(), { outputFormat: "plain" }),
|
|
185
|
+
cause: e
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
return _serialize(data, options);
|
|
191
|
+
}
|
|
192
|
+
function _serialize(data, options) {
|
|
193
|
+
const [json, hasBlob] = jsonSerialize(data);
|
|
194
|
+
if (options.outputFormat === "plain") {
|
|
195
|
+
return json;
|
|
196
|
+
}
|
|
197
|
+
if (options.outputFormat === "URLSearchParams") {
|
|
198
|
+
const params = new URLSearchParams();
|
|
199
|
+
for (const [path, value] of bracketNotationSerialize(json)) {
|
|
200
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
201
|
+
params.append(path, value.toString());
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return params;
|
|
205
|
+
}
|
|
206
|
+
if (json instanceof Blob || json === void 0 || !hasBlob) {
|
|
207
|
+
return json;
|
|
208
|
+
}
|
|
209
|
+
const form = new FormData();
|
|
210
|
+
for (const [path, value] of bracketNotationSerialize(json)) {
|
|
211
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
212
|
+
form.append(path, value.toString());
|
|
213
|
+
} else if (value instanceof Blob) {
|
|
214
|
+
form.append(path, value);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return form;
|
|
218
|
+
}
|
|
219
|
+
function deserialize(data) {
|
|
220
|
+
if (data instanceof URLSearchParams || data instanceof FormData) {
|
|
221
|
+
return bracketNotationDeserialize(Array.from(data.entries()));
|
|
222
|
+
}
|
|
223
|
+
if (isAsyncIteratorObject(data)) {
|
|
224
|
+
return mapEventIterator(data, {
|
|
225
|
+
value: async (value) => value,
|
|
226
|
+
error: async (e) => {
|
|
227
|
+
if (e instanceof ErrorEvent && isORPCErrorJson(e.data)) {
|
|
228
|
+
return createORPCErrorFromJson(e.data, { cause: e });
|
|
229
|
+
}
|
|
230
|
+
return e;
|
|
231
|
+
}
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
return data;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
function standardizeHTTPPath(path) {
|
|
238
|
+
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
239
|
+
}
|
|
240
|
+
function getDynamicParams(path) {
|
|
241
|
+
return path ? standardizeHTTPPath(path).match(/\/\{[^}]+\}/g)?.map((v) => ({
|
|
242
|
+
raw: v,
|
|
243
|
+
name: v.match(/\{\+?([^}]+)\}/)[1]
|
|
244
|
+
})) : void 0;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export { standardizeHTTPPath as a, deserialize as d, getDynamicParams as g, jsonSerialize as j, serialize as s };
|
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
import { isObject, stringifyJSON, isORPCErrorStatus, tryDecodeURIComponent, value, toHttpPath, toArray, intercept, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, setSpanError, ORPCError, toORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { flattenHeader } from '@temporary-name/standard-server';
|
|
3
|
+
import { c as createProcedureClient } from './server.DcfsPloY.mjs';
|
|
4
|
+
import { fallbackContractConfig } from '@temporary-name/contract';
|
|
5
|
+
import { d as deserialize, s as serialize, a as standardizeHTTPPath } from './server.BEHw7Eyx.mjs';
|
|
6
|
+
import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@temporary-name/server';
|
|
7
|
+
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
8
|
+
|
|
9
|
+
async function decode(request, params, procedure) {
|
|
10
|
+
const inputStructure = fallbackContractConfig(
|
|
11
|
+
"defaultInputStructure",
|
|
12
|
+
procedure["~orpc"].route.inputStructure
|
|
13
|
+
);
|
|
14
|
+
if (inputStructure === "compact") {
|
|
15
|
+
const data = request.method === "GET" ? deserialize(request.url.searchParams) : deserialize(await request.body());
|
|
16
|
+
if (data === void 0) {
|
|
17
|
+
return params;
|
|
18
|
+
}
|
|
19
|
+
if (isObject(data)) {
|
|
20
|
+
return {
|
|
21
|
+
...params,
|
|
22
|
+
...data
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
const deserializeSearchParams = () => {
|
|
28
|
+
return deserialize(request.url.searchParams);
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
params,
|
|
32
|
+
get query() {
|
|
33
|
+
const value = deserializeSearchParams();
|
|
34
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
35
|
+
return value;
|
|
36
|
+
},
|
|
37
|
+
set query(value) {
|
|
38
|
+
Object.defineProperty(this, "query", { value, writable: true });
|
|
39
|
+
},
|
|
40
|
+
headers: request.headers,
|
|
41
|
+
body: deserialize(await request.body())
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
function encode(output, procedure) {
|
|
45
|
+
const successStatus = fallbackContractConfig(
|
|
46
|
+
"defaultSuccessStatus",
|
|
47
|
+
procedure["~orpc"].route.successStatus
|
|
48
|
+
);
|
|
49
|
+
const outputStructure = fallbackContractConfig(
|
|
50
|
+
"defaultOutputStructure",
|
|
51
|
+
procedure["~orpc"].route.outputStructure
|
|
52
|
+
);
|
|
53
|
+
if (outputStructure === "compact") {
|
|
54
|
+
return {
|
|
55
|
+
status: successStatus,
|
|
56
|
+
headers: {},
|
|
57
|
+
body: serialize(output)
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (!isDetailedOutput(output)) {
|
|
61
|
+
throw new Error(`
|
|
62
|
+
Invalid "detailed" output structure:
|
|
63
|
+
\u2022 Expected an object with optional properties:
|
|
64
|
+
- status (number 200-399)
|
|
65
|
+
- headers (Record<string, string | string[]>)
|
|
66
|
+
- body (any)
|
|
67
|
+
\u2022 No extra keys allowed.
|
|
68
|
+
|
|
69
|
+
Actual value:
|
|
70
|
+
${stringifyJSON(output)}
|
|
71
|
+
`);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
status: output.status ?? successStatus,
|
|
75
|
+
headers: output.headers ?? {},
|
|
76
|
+
body: serialize(output.body)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function encodeError(error) {
|
|
80
|
+
return {
|
|
81
|
+
status: error.status,
|
|
82
|
+
headers: {},
|
|
83
|
+
body: serialize(error.toJSON(), { outputFormat: "plain" })
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function isDetailedOutput(output) {
|
|
87
|
+
if (!isObject(output)) {
|
|
88
|
+
return false;
|
|
89
|
+
}
|
|
90
|
+
if (output.headers && !isObject(output.headers)) {
|
|
91
|
+
return false;
|
|
92
|
+
}
|
|
93
|
+
if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return true;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function resolveFriendlyStandardHandleOptions(options) {
|
|
100
|
+
return {
|
|
101
|
+
...options,
|
|
102
|
+
context: options.context ?? {}
|
|
103
|
+
// Context only optional if all fields are optional
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function toRou3Pattern(path) {
|
|
107
|
+
return standardizeHTTPPath(path).replace(/\/\{\+([^}]+)\}/g, "/**:$1").replace(/\/\{([^}]+)\}/g, "/:$1");
|
|
108
|
+
}
|
|
109
|
+
function decodeParams(params) {
|
|
110
|
+
return Object.fromEntries(
|
|
111
|
+
Object.entries(params).map(([key, value]) => [key, tryDecodeURIComponent(value)])
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
class StandardOpenAPIMatcher {
|
|
116
|
+
tree = createRouter();
|
|
117
|
+
pendingRouters = [];
|
|
118
|
+
init(router, path = []) {
|
|
119
|
+
const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
|
|
120
|
+
if (!value(true, traverseOptions)) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
const { path: path2, contract } = traverseOptions;
|
|
124
|
+
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
125
|
+
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
126
|
+
if (isProcedure(contract)) {
|
|
127
|
+
addRoute(this.tree, method, httpPath, {
|
|
128
|
+
path: path2,
|
|
129
|
+
contract,
|
|
130
|
+
procedure: contract,
|
|
131
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
|
132
|
+
router
|
|
133
|
+
});
|
|
134
|
+
} else {
|
|
135
|
+
addRoute(this.tree, method, httpPath, {
|
|
136
|
+
path: path2,
|
|
137
|
+
contract,
|
|
138
|
+
procedure: void 0,
|
|
139
|
+
router
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
this.pendingRouters.push(
|
|
144
|
+
...laziedOptions.map((option) => ({
|
|
145
|
+
...option,
|
|
146
|
+
httpPathPrefix: toHttpPath(option.path),
|
|
147
|
+
laziedPrefix: getLazyMeta(option.router).prefix
|
|
148
|
+
}))
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
async match(method, pathname) {
|
|
152
|
+
if (this.pendingRouters.length) {
|
|
153
|
+
const newPendingRouters = [];
|
|
154
|
+
for (const pendingRouter of this.pendingRouters) {
|
|
155
|
+
if (!pendingRouter.laziedPrefix || pathname.startsWith(pendingRouter.laziedPrefix) || pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
|
156
|
+
const { default: router } = await unlazy(pendingRouter.router);
|
|
157
|
+
this.init(router, pendingRouter.path);
|
|
158
|
+
} else {
|
|
159
|
+
newPendingRouters.push(pendingRouter);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
this.pendingRouters = newPendingRouters;
|
|
163
|
+
}
|
|
164
|
+
const match = findRoute(this.tree, method, pathname);
|
|
165
|
+
if (!match) {
|
|
166
|
+
return void 0;
|
|
167
|
+
}
|
|
168
|
+
if (!match.data.procedure) {
|
|
169
|
+
const { default: maybeProcedure } = await unlazy(getRouter(match.data.router, match.data.path));
|
|
170
|
+
if (!isProcedure(maybeProcedure)) {
|
|
171
|
+
throw new Error(`
|
|
172
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.data.path)}.
|
|
173
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
|
174
|
+
`);
|
|
175
|
+
}
|
|
176
|
+
match.data.procedure = createContractedProcedure(maybeProcedure, match.data.contract);
|
|
177
|
+
}
|
|
178
|
+
return {
|
|
179
|
+
path: match.data.path,
|
|
180
|
+
procedure: match.data.procedure,
|
|
181
|
+
params: match.params ? decodeParams(match.params) : void 0
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
class CompositeStandardHandlerPlugin {
|
|
187
|
+
plugins;
|
|
188
|
+
constructor(plugins = []) {
|
|
189
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
|
190
|
+
}
|
|
191
|
+
init(options, router) {
|
|
192
|
+
for (const plugin of this.plugins) {
|
|
193
|
+
plugin.init?.(options, router);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
class StandardHandler {
|
|
199
|
+
interceptors;
|
|
200
|
+
clientInterceptors;
|
|
201
|
+
rootInterceptors;
|
|
202
|
+
matcher;
|
|
203
|
+
constructor(router, options) {
|
|
204
|
+
this.matcher = new StandardOpenAPIMatcher();
|
|
205
|
+
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
|
206
|
+
plugins.init(options, router);
|
|
207
|
+
this.interceptors = toArray(options.interceptors);
|
|
208
|
+
this.clientInterceptors = toArray(options.clientInterceptors);
|
|
209
|
+
this.rootInterceptors = toArray(options.rootInterceptors);
|
|
210
|
+
this.matcher.init(router);
|
|
211
|
+
}
|
|
212
|
+
async handle(request, options) {
|
|
213
|
+
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
|
214
|
+
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
|
215
|
+
return { matched: false, response: void 0 };
|
|
216
|
+
}
|
|
217
|
+
return intercept(this.rootInterceptors, { ...options, request, prefix }, async (interceptorOptions) => {
|
|
218
|
+
return runWithSpan({ name: `${request.method} ${request.url.pathname}` }, async (span) => {
|
|
219
|
+
let step;
|
|
220
|
+
try {
|
|
221
|
+
return await intercept(
|
|
222
|
+
this.interceptors,
|
|
223
|
+
interceptorOptions,
|
|
224
|
+
async ({ request: request2, context, prefix: prefix2 }) => {
|
|
225
|
+
const method = request2.method;
|
|
226
|
+
const url = request2.url;
|
|
227
|
+
const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
|
|
228
|
+
const match = await runWithSpan(
|
|
229
|
+
{ name: "find_procedure" },
|
|
230
|
+
() => this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`)
|
|
231
|
+
);
|
|
232
|
+
if (!match) {
|
|
233
|
+
return { matched: false, response: void 0 };
|
|
234
|
+
}
|
|
235
|
+
span?.updateName(`${ORPC_NAME}.${match.path.join("/")}`);
|
|
236
|
+
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
237
|
+
span?.setAttribute("rpc.method", match.path.join("."));
|
|
238
|
+
step = "decode_input";
|
|
239
|
+
let input = await runWithSpan(
|
|
240
|
+
{ name: "decode_input" },
|
|
241
|
+
() => decode(request2, match.params, match.procedure)
|
|
242
|
+
);
|
|
243
|
+
step = void 0;
|
|
244
|
+
if (isAsyncIteratorObject(input)) {
|
|
245
|
+
input = asyncIteratorWithSpan(
|
|
246
|
+
{ name: "consume_event_iterator_input", signal: request2.signal },
|
|
247
|
+
input
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
const client = createProcedureClient(match.procedure, {
|
|
251
|
+
context,
|
|
252
|
+
path: match.path,
|
|
253
|
+
interceptors: this.clientInterceptors
|
|
254
|
+
});
|
|
255
|
+
step = "call_procedure";
|
|
256
|
+
const output = await client(input, {
|
|
257
|
+
signal: request2.signal,
|
|
258
|
+
lastEventId: flattenHeader(request2.headers["last-event-id"])
|
|
259
|
+
});
|
|
260
|
+
step = void 0;
|
|
261
|
+
const response = encode(output, match.procedure);
|
|
262
|
+
return {
|
|
263
|
+
matched: true,
|
|
264
|
+
response
|
|
265
|
+
};
|
|
266
|
+
}
|
|
267
|
+
);
|
|
268
|
+
} catch (e) {
|
|
269
|
+
if (step !== "call_procedure") {
|
|
270
|
+
setSpanError(span, e);
|
|
271
|
+
}
|
|
272
|
+
const error = step === "decode_input" && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
|
273
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
|
274
|
+
cause: e
|
|
275
|
+
}) : toORPCError(e);
|
|
276
|
+
const response = encodeError(error);
|
|
277
|
+
return {
|
|
278
|
+
matched: true,
|
|
279
|
+
response
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, encodeError as a, StandardOpenAPIMatcher as b, decodeParams as c, decode as d, encode as e, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Meta } from '@temporary-name/contract';
|
|
2
|
-
import { HTTPPath,
|
|
3
|
-
import {
|
|
4
|
-
import { C as Context, R as Router,
|
|
2
|
+
import { HTTPPath, Interceptor } from '@temporary-name/shared';
|
|
3
|
+
import { StandardLazyRequest, StandardResponse } from '@temporary-name/standard-server';
|
|
4
|
+
import { C as Context, R as Router, E as ProcedureClientInterceptorOptions } from './server.CZNLCQBm.js';
|
|
5
5
|
|
|
6
6
|
interface StandardHandlerPlugin<T extends Context> {
|
|
7
7
|
order?: number;
|
|
@@ -13,22 +13,6 @@ declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends
|
|
|
13
13
|
init(options: StandardHandlerOptions<T>, router: Router<any, T>): void;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
type StandardParams = Record<string, string>;
|
|
17
|
-
type StandardMatchResult = {
|
|
18
|
-
path: readonly string[];
|
|
19
|
-
procedure: AnyProcedure;
|
|
20
|
-
params?: StandardParams;
|
|
21
|
-
} | undefined;
|
|
22
|
-
interface StandardMatcher {
|
|
23
|
-
init(router: AnyRouter): void;
|
|
24
|
-
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
|
25
|
-
}
|
|
26
|
-
interface StandardCodec {
|
|
27
|
-
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
|
28
|
-
encodeError(error: ORPCError<any, any>): StandardResponse;
|
|
29
|
-
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
16
|
interface StandardHandleOptions<T extends Context> {
|
|
33
17
|
prefix?: HTTPPath;
|
|
34
18
|
context: T;
|
|
@@ -60,14 +44,13 @@ interface StandardHandlerOptions<TContext extends Context> {
|
|
|
60
44
|
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, Promise<unknown>>[];
|
|
61
45
|
}
|
|
62
46
|
declare class StandardHandler<T extends Context> {
|
|
63
|
-
private readonly matcher;
|
|
64
|
-
private readonly codec;
|
|
65
47
|
private readonly interceptors;
|
|
66
48
|
private readonly clientInterceptors;
|
|
67
49
|
private readonly rootInterceptors;
|
|
68
|
-
|
|
50
|
+
private readonly matcher;
|
|
51
|
+
constructor(router: Router<any, T>, options: NoInfer<StandardHandlerOptions<T>>);
|
|
69
52
|
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
|
70
53
|
}
|
|
71
54
|
|
|
72
|
-
export { CompositeStandardHandlerPlugin as C, StandardHandler as
|
|
73
|
-
export type {
|
|
55
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as e };
|
|
56
|
+
export type { StandardHandlerInterceptorOptions as S, StandardHandlerPlugin as a, StandardHandlerOptions as b, StandardHandleOptions as c, StandardHandleResult as d };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { HTTPPath } from '@temporary-name/shared';
|
|
2
|
+
import { C as Context } from './server.CZNLCQBm.mjs';
|
|
3
|
+
import { c as StandardHandleOptions } from './server.CbLTWfgn.mjs';
|
|
4
|
+
|
|
5
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
6
|
+
context?: T;
|
|
7
|
+
} : {
|
|
8
|
+
context: T;
|
|
9
|
+
});
|
|
10
|
+
declare function resolveFriendlyStandardHandleOptions<T extends Context>(options: FriendlyStandardHandleOptions<T>): StandardHandleOptions<T>;
|
|
11
|
+
/**
|
|
12
|
+
* {@link https://github.com/unjs/rou3}
|
|
13
|
+
*
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
declare function toRou3Pattern(path: HTTPPath): string;
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
declare function decodeParams(params: Record<string, string>): Record<string, string>;
|
|
21
|
+
|
|
22
|
+
export { decodeParams as d, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|
|
23
|
+
export type { FriendlyStandardHandleOptions as F };
|
|
@@ -71,7 +71,7 @@ declare class Procedure<TInitialContext extends Context, TCurrentContext extends
|
|
|
71
71
|
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
72
72
|
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
73
73
|
}
|
|
74
|
-
type AnyProcedure = Procedure<any, any,
|
|
74
|
+
type AnyProcedure = Procedure<any, any, AnySchema, AnySchema, any, any>;
|
|
75
75
|
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
76
76
|
|
|
77
77
|
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
@@ -188,5 +188,5 @@ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any
|
|
|
188
188
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
189
189
|
};
|
|
190
190
|
|
|
191
|
-
export { isProcedure as
|
|
192
|
-
export type {
|
|
191
|
+
export { isProcedure as D, createProcedureClient as F, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, middlewareOutputFn as y };
|
|
192
|
+
export type { AnyMiddleware as A, ProcedureDef as B, Context as C, ProcedureClientInterceptorOptions as E, InferRouterInitialContexts as G, InferRouterCurrentContexts as H, InferRouterInitialContext as I, InferRouterInputs as J, InferRouterOutputs as K, Lazyable as L, MergedInitialContext as M, ORPCErrorConstructorMap as O, Router as R, CreateProcedureClientOptions as a, ProcedureClient as b, AnyRouter as c, Lazy as d, AnyProcedure as e, Middleware as f, MergedCurrentContext as g, ProcedureHandler as h, MapInputMiddleware as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as v, MiddlewareOutputFn as w, MiddlewareOptions as x, ProcedureHandlerOptions as z };
|
|
@@ -71,7 +71,7 @@ declare class Procedure<TInitialContext extends Context, TCurrentContext extends
|
|
|
71
71
|
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
|
72
72
|
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
|
73
73
|
}
|
|
74
|
-
type AnyProcedure = Procedure<any, any,
|
|
74
|
+
type AnyProcedure = Procedure<any, any, AnySchema, AnySchema, any, any>;
|
|
75
75
|
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
76
76
|
|
|
77
77
|
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
@@ -188,5 +188,5 @@ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any
|
|
|
188
188
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
189
189
|
};
|
|
190
190
|
|
|
191
|
-
export { isProcedure as
|
|
192
|
-
export type {
|
|
191
|
+
export { isProcedure as D, createProcedureClient as F, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, middlewareOutputFn as y };
|
|
192
|
+
export type { AnyMiddleware as A, ProcedureDef as B, Context as C, ProcedureClientInterceptorOptions as E, InferRouterInitialContexts as G, InferRouterCurrentContexts as H, InferRouterInitialContext as I, InferRouterInputs as J, InferRouterOutputs as K, Lazyable as L, MergedInitialContext as M, ORPCErrorConstructorMap as O, Router as R, CreateProcedureClientOptions as a, ProcedureClient as b, AnyRouter as c, Lazy as d, AnyProcedure as e, Middleware as f, MergedCurrentContext as g, ProcedureHandler as h, MapInputMiddleware as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as v, MiddlewareOutputFn as w, MiddlewareOptions as x, ProcedureHandlerOptions as z };
|