@orpc/server 0.33.0 → 0.35.0
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/chunk-CVIWJKJC.js +308 -0
- package/dist/chunk-EYGVJA7A.js +136 -0
- package/dist/{chunk-KK4SDLC7.js → chunk-NOA3GBJQ.js} +69 -9
- package/dist/chunk-OXB4YX67.js +111 -0
- package/dist/fetch.js +12 -9
- package/dist/hono.js +25 -13
- package/dist/index.js +14 -1
- package/dist/next.js +13 -10
- package/dist/node.js +149 -61
- package/dist/plugins.js +11 -0
- package/dist/src/adapters/fetch/index.d.ts +2 -4
- package/dist/src/adapters/fetch/rpc-handler.d.ts +10 -0
- package/dist/src/adapters/fetch/types.d.ts +2 -10
- package/dist/src/adapters/fetch/utils.d.ts +6 -0
- package/dist/src/adapters/hono/middleware.d.ts +3 -2
- package/dist/src/adapters/next/serve.d.ts +3 -2
- package/dist/src/adapters/node/index.d.ts +2 -3
- package/dist/src/adapters/node/rpc-handler.d.ts +10 -0
- package/dist/src/adapters/node/types.d.ts +13 -14
- package/dist/src/adapters/node/utils.d.ts +5 -0
- package/dist/src/adapters/standard/handler.d.ts +47 -0
- package/dist/src/adapters/standard/index.d.ts +7 -0
- package/dist/src/adapters/standard/rpc-codec.d.ts +15 -0
- package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
- package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
- package/dist/src/adapters/standard/rpc-serializer.d.ts +16 -0
- package/dist/src/adapters/standard/types.d.ts +44 -0
- package/dist/src/implementer-variants.d.ts +6 -5
- package/dist/src/implementer.d.ts +7 -6
- package/dist/src/index.d.ts +2 -0
- package/dist/src/plugins/base.d.ts +11 -0
- package/dist/src/plugins/cors.d.ts +18 -0
- package/dist/src/plugins/index.d.ts +4 -0
- package/dist/src/plugins/response-headers.d.ts +10 -0
- package/dist/src/utils.d.ts +24 -0
- package/dist/standard.js +17 -0
- package/package.json +17 -3
- package/dist/chunk-ESTRJAOX.js +0 -299
- package/dist/chunk-WUOGVGWG.js +0 -1
- package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
- package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/adapters/fetch/super-json.d.ts +0 -12
- package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
- package/dist/src/adapters/node/request-listener.d.ts +0 -28
@@ -0,0 +1,308 @@
|
|
1
|
+
import {
|
2
|
+
convertPathToHttpPath,
|
3
|
+
createContractedProcedure,
|
4
|
+
createProcedureClient,
|
5
|
+
eachContractProcedure,
|
6
|
+
getRouterChild,
|
7
|
+
isProcedure,
|
8
|
+
unlazy
|
9
|
+
} from "./chunk-NOA3GBJQ.js";
|
10
|
+
import {
|
11
|
+
CompositePlugin
|
12
|
+
} from "./chunk-OXB4YX67.js";
|
13
|
+
|
14
|
+
// src/adapters/standard/handler.ts
|
15
|
+
import { toORPCError } from "@orpc/contract";
|
16
|
+
import { intercept, trim } from "@orpc/shared";
|
17
|
+
var StandardHandler = class {
|
18
|
+
constructor(router, matcher, codec, options = {}) {
|
19
|
+
this.matcher = matcher;
|
20
|
+
this.codec = codec;
|
21
|
+
this.options = options;
|
22
|
+
this.plugin = new CompositePlugin(options?.plugins);
|
23
|
+
this.plugin.init(this.options);
|
24
|
+
this.matcher.init(router);
|
25
|
+
}
|
26
|
+
plugin;
|
27
|
+
handle(request, ...[options]) {
|
28
|
+
const handleOptions = options ?? {};
|
29
|
+
handleOptions.context ??= {};
|
30
|
+
return intercept(
|
31
|
+
this.options.interceptorsRoot ?? [],
|
32
|
+
{ request, ...handleOptions },
|
33
|
+
async (interceptorRootOptions) => {
|
34
|
+
try {
|
35
|
+
return await intercept(
|
36
|
+
this.options.interceptors ?? [],
|
37
|
+
interceptorRootOptions,
|
38
|
+
async ({ request: request2, context }) => {
|
39
|
+
const method = request2.method;
|
40
|
+
const url = request2.url;
|
41
|
+
const pathname = `/${trim(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
|
42
|
+
const match = await this.matcher.match(method, pathname);
|
43
|
+
if (!match) {
|
44
|
+
return { matched: false, response: void 0 };
|
45
|
+
}
|
46
|
+
const client = createProcedureClient(match.procedure, {
|
47
|
+
context,
|
48
|
+
path: match.path
|
49
|
+
});
|
50
|
+
const input = await this.codec.decode(request2, match.params, match.procedure);
|
51
|
+
const output = await client(input, { signal: request2.signal });
|
52
|
+
const response = this.codec.encode(output, match.procedure);
|
53
|
+
return {
|
54
|
+
matched: true,
|
55
|
+
response
|
56
|
+
};
|
57
|
+
}
|
58
|
+
);
|
59
|
+
} catch (e) {
|
60
|
+
const error = toORPCError(e);
|
61
|
+
const response = this.codec.encodeError(error);
|
62
|
+
return {
|
63
|
+
matched: true,
|
64
|
+
response
|
65
|
+
};
|
66
|
+
}
|
67
|
+
}
|
68
|
+
);
|
69
|
+
}
|
70
|
+
};
|
71
|
+
|
72
|
+
// src/adapters/standard/rpc-serializer.ts
|
73
|
+
import { findDeepMatches, isPlainObject, set } from "@orpc/shared";
|
74
|
+
var RPCSerializer = class {
|
75
|
+
serialize(data) {
|
76
|
+
if (data instanceof Blob) {
|
77
|
+
return data;
|
78
|
+
}
|
79
|
+
const serializedJSON = serializeRPCJson(data);
|
80
|
+
const { maps, values: blobs } = findDeepMatches((v) => v instanceof Blob, serializedJSON.json);
|
81
|
+
if (blobs.length === 0) {
|
82
|
+
return serializedJSON;
|
83
|
+
}
|
84
|
+
const form = new FormData();
|
85
|
+
form.set("data", JSON.stringify(serializedJSON));
|
86
|
+
form.set("maps", JSON.stringify(maps));
|
87
|
+
for (const i in blobs) {
|
88
|
+
form.set(i, blobs[i]);
|
89
|
+
}
|
90
|
+
return form;
|
91
|
+
}
|
92
|
+
deserialize(serialized) {
|
93
|
+
if (serialized instanceof Blob) {
|
94
|
+
return serialized;
|
95
|
+
}
|
96
|
+
if (!(serialized instanceof FormData)) {
|
97
|
+
return deserializeRPCJson(serialized);
|
98
|
+
}
|
99
|
+
const data = JSON.parse(serialized.get("data"));
|
100
|
+
const maps = JSON.parse(serialized.get("maps"));
|
101
|
+
for (const i in maps) {
|
102
|
+
data.json = set(data.json, maps[i], serialized.get(i));
|
103
|
+
}
|
104
|
+
return deserializeRPCJson(data);
|
105
|
+
}
|
106
|
+
};
|
107
|
+
function serializeRPCJson(value, segments = [], meta = []) {
|
108
|
+
if (typeof value === "bigint") {
|
109
|
+
meta.push(["bigint", segments]);
|
110
|
+
return { json: value.toString(), meta };
|
111
|
+
}
|
112
|
+
if (value instanceof Date) {
|
113
|
+
meta.push(["date", segments]);
|
114
|
+
const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
|
115
|
+
return { json: data, meta };
|
116
|
+
}
|
117
|
+
if (Number.isNaN(value)) {
|
118
|
+
meta.push(["nan", segments]);
|
119
|
+
return { json: "NaN", meta };
|
120
|
+
}
|
121
|
+
if (value instanceof RegExp) {
|
122
|
+
meta.push(["regexp", segments]);
|
123
|
+
return { json: value.toString(), meta };
|
124
|
+
}
|
125
|
+
if (value instanceof URL) {
|
126
|
+
meta.push(["url", segments]);
|
127
|
+
return { json: value.toString(), meta };
|
128
|
+
}
|
129
|
+
if (isPlainObject(value)) {
|
130
|
+
const json = {};
|
131
|
+
for (const k in value) {
|
132
|
+
json[k] = serializeRPCJson(value[k], [...segments, k], meta).json;
|
133
|
+
}
|
134
|
+
return { json, meta };
|
135
|
+
}
|
136
|
+
if (Array.isArray(value)) {
|
137
|
+
const json = value.map((v, i) => {
|
138
|
+
if (v === void 0) {
|
139
|
+
meta.push(["undefined", [...segments, i]]);
|
140
|
+
return null;
|
141
|
+
}
|
142
|
+
return serializeRPCJson(v, [...segments, i], meta).json;
|
143
|
+
});
|
144
|
+
return { json, meta };
|
145
|
+
}
|
146
|
+
if (value instanceof Set) {
|
147
|
+
const result = serializeRPCJson(Array.from(value), segments, meta);
|
148
|
+
meta.push(["set", segments]);
|
149
|
+
return result;
|
150
|
+
}
|
151
|
+
if (value instanceof Map) {
|
152
|
+
const result = serializeRPCJson(Array.from(value.entries()), segments, meta);
|
153
|
+
meta.push(["map", segments]);
|
154
|
+
return result;
|
155
|
+
}
|
156
|
+
return { json: value, meta };
|
157
|
+
}
|
158
|
+
function deserializeRPCJson({
|
159
|
+
json,
|
160
|
+
meta
|
161
|
+
}) {
|
162
|
+
if (meta.length === 0) {
|
163
|
+
return json;
|
164
|
+
}
|
165
|
+
const ref = { data: json };
|
166
|
+
for (const [type, segments] of meta) {
|
167
|
+
let currentRef = ref;
|
168
|
+
let preSegment = "data";
|
169
|
+
for (let i = 0; i < segments.length; i++) {
|
170
|
+
currentRef = currentRef[preSegment];
|
171
|
+
preSegment = segments[i];
|
172
|
+
}
|
173
|
+
switch (type) {
|
174
|
+
case "nan":
|
175
|
+
currentRef[preSegment] = Number.NaN;
|
176
|
+
break;
|
177
|
+
case "bigint":
|
178
|
+
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
179
|
+
break;
|
180
|
+
case "date":
|
181
|
+
currentRef[preSegment] = new Date(currentRef[preSegment]);
|
182
|
+
break;
|
183
|
+
case "regexp": {
|
184
|
+
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
185
|
+
currentRef[preSegment] = new RegExp(pattern, flags);
|
186
|
+
break;
|
187
|
+
}
|
188
|
+
case "url":
|
189
|
+
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
190
|
+
break;
|
191
|
+
case "undefined":
|
192
|
+
currentRef[preSegment] = void 0;
|
193
|
+
break;
|
194
|
+
case "map":
|
195
|
+
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
196
|
+
break;
|
197
|
+
case "set":
|
198
|
+
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
199
|
+
break;
|
200
|
+
/* v8 ignore next 3 */
|
201
|
+
default: {
|
202
|
+
const _expected = type;
|
203
|
+
}
|
204
|
+
}
|
205
|
+
}
|
206
|
+
return ref.data;
|
207
|
+
}
|
208
|
+
|
209
|
+
// src/adapters/standard/rpc-codec.ts
|
210
|
+
var RPCCodec = class {
|
211
|
+
serializer;
|
212
|
+
constructor(options = {}) {
|
213
|
+
this.serializer = options.serializer ?? new RPCSerializer();
|
214
|
+
}
|
215
|
+
async decode(request, _params, _procedure) {
|
216
|
+
const serialized = request.method === "GET" ? JSON.parse(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
217
|
+
return this.serializer.deserialize(serialized);
|
218
|
+
}
|
219
|
+
encode(output, _procedure) {
|
220
|
+
return {
|
221
|
+
status: 200,
|
222
|
+
headers: {},
|
223
|
+
body: this.serializer.serialize(output)
|
224
|
+
};
|
225
|
+
}
|
226
|
+
encodeError(error) {
|
227
|
+
return {
|
228
|
+
status: error.status,
|
229
|
+
headers: {},
|
230
|
+
body: this.serializer.serialize(error.toJSON())
|
231
|
+
};
|
232
|
+
}
|
233
|
+
};
|
234
|
+
|
235
|
+
// src/adapters/standard/rpc-matcher.ts
|
236
|
+
var RPCMatcher = class {
|
237
|
+
tree = {};
|
238
|
+
pendingRouters = [];
|
239
|
+
init(router, path = []) {
|
240
|
+
const laziedOptions = eachContractProcedure({
|
241
|
+
router,
|
242
|
+
path
|
243
|
+
}, ({ path: path2, contract }) => {
|
244
|
+
const httpPath = convertPathToHttpPath(path2);
|
245
|
+
if (isProcedure(contract)) {
|
246
|
+
this.tree[httpPath] = {
|
247
|
+
path: path2,
|
248
|
+
contract,
|
249
|
+
procedure: contract,
|
250
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
251
|
+
router
|
252
|
+
};
|
253
|
+
} else {
|
254
|
+
this.tree[httpPath] = {
|
255
|
+
path: path2,
|
256
|
+
contract,
|
257
|
+
procedure: void 0,
|
258
|
+
router
|
259
|
+
};
|
260
|
+
}
|
261
|
+
});
|
262
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
263
|
+
...option,
|
264
|
+
httpPathPrefix: convertPathToHttpPath(option.path)
|
265
|
+
})));
|
266
|
+
}
|
267
|
+
async match(_method, pathname) {
|
268
|
+
if (this.pendingRouters.length) {
|
269
|
+
const newPendingRouters = [];
|
270
|
+
for (const pendingRouter of this.pendingRouters) {
|
271
|
+
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
272
|
+
const { default: router } = await unlazy(pendingRouter.lazied);
|
273
|
+
this.init(router, pendingRouter.path);
|
274
|
+
} else {
|
275
|
+
newPendingRouters.push(pendingRouter);
|
276
|
+
}
|
277
|
+
}
|
278
|
+
this.pendingRouters = newPendingRouters;
|
279
|
+
}
|
280
|
+
const match = this.tree[pathname];
|
281
|
+
if (!match) {
|
282
|
+
return void 0;
|
283
|
+
}
|
284
|
+
if (!match.procedure) {
|
285
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
|
286
|
+
if (!isProcedure(maybeProcedure)) {
|
287
|
+
throw new Error(`
|
288
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
|
289
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
290
|
+
`);
|
291
|
+
}
|
292
|
+
match.procedure = createContractedProcedure(match.contract, maybeProcedure);
|
293
|
+
}
|
294
|
+
return {
|
295
|
+
path: match.path,
|
296
|
+
procedure: match.procedure
|
297
|
+
};
|
298
|
+
}
|
299
|
+
};
|
300
|
+
|
301
|
+
export {
|
302
|
+
StandardHandler,
|
303
|
+
RPCSerializer,
|
304
|
+
serializeRPCJson,
|
305
|
+
RPCCodec,
|
306
|
+
RPCMatcher
|
307
|
+
};
|
308
|
+
//# sourceMappingURL=chunk-CVIWJKJC.js.map
|
@@ -0,0 +1,136 @@
|
|
1
|
+
import {
|
2
|
+
RPCCodec,
|
3
|
+
RPCMatcher,
|
4
|
+
StandardHandler
|
5
|
+
} from "./chunk-CVIWJKJC.js";
|
6
|
+
|
7
|
+
// src/adapters/fetch/utils.ts
|
8
|
+
import { once } from "@orpc/shared";
|
9
|
+
import cd from "content-disposition";
|
10
|
+
function fetchHeadersToStandardHeaders(headers) {
|
11
|
+
const standardHeaders = {};
|
12
|
+
for (const [key, value] of headers) {
|
13
|
+
if (Array.isArray(standardHeaders[key])) {
|
14
|
+
standardHeaders[key].push(value);
|
15
|
+
} else if (standardHeaders[key] !== void 0) {
|
16
|
+
standardHeaders[key] = [standardHeaders[key], value];
|
17
|
+
} else {
|
18
|
+
standardHeaders[key] = value;
|
19
|
+
}
|
20
|
+
}
|
21
|
+
return standardHeaders;
|
22
|
+
}
|
23
|
+
async function fetchReToStandardBody(re) {
|
24
|
+
if (!re.body) {
|
25
|
+
return void 0;
|
26
|
+
}
|
27
|
+
const contentDisposition = re.headers.get("content-disposition");
|
28
|
+
const fileName = contentDisposition ? cd.parse(contentDisposition).parameters.filename : void 0;
|
29
|
+
if (fileName) {
|
30
|
+
const blob2 = await re.blob();
|
31
|
+
return new File([blob2], fileName, {
|
32
|
+
type: blob2.type
|
33
|
+
});
|
34
|
+
}
|
35
|
+
const contentType = re.headers.get("content-type");
|
36
|
+
if (!contentType || contentType.startsWith("application/json")) {
|
37
|
+
const text = await re.text();
|
38
|
+
if (!text) {
|
39
|
+
return void 0;
|
40
|
+
}
|
41
|
+
return JSON.parse(text);
|
42
|
+
}
|
43
|
+
if (contentType.startsWith("multipart/form-data")) {
|
44
|
+
return await re.formData();
|
45
|
+
}
|
46
|
+
if (contentType.startsWith("application/x-www-form-urlencoded")) {
|
47
|
+
return new URLSearchParams(await re.text());
|
48
|
+
}
|
49
|
+
if (contentType.startsWith("text/")) {
|
50
|
+
return await re.text();
|
51
|
+
}
|
52
|
+
const blob = await re.blob();
|
53
|
+
return new File([blob], "blob", {
|
54
|
+
type: blob.type
|
55
|
+
});
|
56
|
+
}
|
57
|
+
function fetchRequestToStandardRequest(request) {
|
58
|
+
const url = new URL(request.url);
|
59
|
+
return {
|
60
|
+
raw: { request },
|
61
|
+
url,
|
62
|
+
signal: request.signal,
|
63
|
+
method: request.method,
|
64
|
+
body: once(() => {
|
65
|
+
return fetchReToStandardBody(request);
|
66
|
+
}),
|
67
|
+
get headers() {
|
68
|
+
const headers = fetchHeadersToStandardHeaders(request.headers);
|
69
|
+
Object.defineProperty(this, "headers", { value: headers, writable: true });
|
70
|
+
return headers;
|
71
|
+
},
|
72
|
+
set headers(value) {
|
73
|
+
Object.defineProperty(this, "headers", { value, writable: true });
|
74
|
+
}
|
75
|
+
};
|
76
|
+
}
|
77
|
+
function standardResponseToFetchHeaders(response) {
|
78
|
+
const fetchHeaders = new Headers();
|
79
|
+
for (const [key, value] of Object.entries(response.headers)) {
|
80
|
+
if (Array.isArray(value)) {
|
81
|
+
for (const v of value) {
|
82
|
+
fetchHeaders.append(key, v);
|
83
|
+
}
|
84
|
+
} else if (value !== void 0) {
|
85
|
+
fetchHeaders.append(key, value);
|
86
|
+
}
|
87
|
+
}
|
88
|
+
if (response.body instanceof Blob && !fetchHeaders.has("content-disposition")) {
|
89
|
+
fetchHeaders.set("content-disposition", cd(response.body instanceof File ? response.body.name : "blob"));
|
90
|
+
} else if (!(response.body instanceof Blob) && !(response.body instanceof URLSearchParams) && !(response.body instanceof FormData) && response.body !== void 0 && !fetchHeaders.has("content-type")) {
|
91
|
+
fetchHeaders.set("content-type", "application/json");
|
92
|
+
}
|
93
|
+
return fetchHeaders;
|
94
|
+
}
|
95
|
+
function standardBodyToFetchBody(body) {
|
96
|
+
if (body instanceof Blob || body instanceof FormData || body instanceof URLSearchParams) {
|
97
|
+
return body;
|
98
|
+
}
|
99
|
+
return JSON.stringify(body);
|
100
|
+
}
|
101
|
+
function standardResponseToFetchResponse(response) {
|
102
|
+
return new Response(standardBodyToFetchBody(response.body), {
|
103
|
+
headers: standardResponseToFetchHeaders(response),
|
104
|
+
status: response.status
|
105
|
+
});
|
106
|
+
}
|
107
|
+
|
108
|
+
// src/adapters/fetch/rpc-handler.ts
|
109
|
+
var RPCHandler = class {
|
110
|
+
standardHandler;
|
111
|
+
constructor(router, options) {
|
112
|
+
const matcher = options?.matcher ?? new RPCMatcher();
|
113
|
+
const codec = options?.codec ?? new RPCCodec();
|
114
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
115
|
+
}
|
116
|
+
async handle(request, ...rest) {
|
117
|
+
const standardRequest = fetchRequestToStandardRequest(request);
|
118
|
+
const result = await this.standardHandler.handle(standardRequest, ...rest);
|
119
|
+
if (!result.matched) {
|
120
|
+
return result;
|
121
|
+
}
|
122
|
+
return {
|
123
|
+
matched: true,
|
124
|
+
response: standardResponseToFetchResponse(result.response)
|
125
|
+
};
|
126
|
+
}
|
127
|
+
};
|
128
|
+
|
129
|
+
export {
|
130
|
+
fetchReToStandardBody,
|
131
|
+
fetchRequestToStandardRequest,
|
132
|
+
standardBodyToFetchBody,
|
133
|
+
standardResponseToFetchResponse,
|
134
|
+
RPCHandler
|
135
|
+
};
|
136
|
+
//# sourceMappingURL=chunk-EYGVJA7A.js.map
|
@@ -1,9 +1,3 @@
|
|
1
|
-
var __defProp = Object.defineProperty;
|
2
|
-
var __export = (target, all) => {
|
3
|
-
for (var name in all)
|
4
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
5
|
-
};
|
6
|
-
|
7
1
|
// src/lazy.ts
|
8
2
|
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
9
3
|
function lazy(loader) {
|
@@ -296,8 +290,70 @@ function createAccessibleLazyRouter(lazied) {
|
|
296
290
|
return recursive;
|
297
291
|
}
|
298
292
|
|
293
|
+
// src/utils.ts
|
294
|
+
import { isContractProcedure as isContractProcedure2 } from "@orpc/contract";
|
295
|
+
function eachContractProcedure(options, callback, laziedOptions = []) {
|
296
|
+
const hiddenContract = getRouterContract(options.router);
|
297
|
+
if (hiddenContract) {
|
298
|
+
return eachContractProcedure(
|
299
|
+
{
|
300
|
+
router: hiddenContract,
|
301
|
+
path: options.path
|
302
|
+
},
|
303
|
+
callback,
|
304
|
+
laziedOptions
|
305
|
+
);
|
306
|
+
}
|
307
|
+
if (isLazy(options.router)) {
|
308
|
+
laziedOptions.push({
|
309
|
+
lazied: options.router,
|
310
|
+
path: options.path
|
311
|
+
});
|
312
|
+
} else if (isContractProcedure2(options.router)) {
|
313
|
+
callback({
|
314
|
+
contract: options.router,
|
315
|
+
path: options.path
|
316
|
+
});
|
317
|
+
} else {
|
318
|
+
for (const key in options.router) {
|
319
|
+
eachContractProcedure(
|
320
|
+
{
|
321
|
+
router: options.router[key],
|
322
|
+
path: [...options.path, key]
|
323
|
+
},
|
324
|
+
callback,
|
325
|
+
laziedOptions
|
326
|
+
);
|
327
|
+
}
|
328
|
+
}
|
329
|
+
return laziedOptions;
|
330
|
+
}
|
331
|
+
async function eachAllContractProcedure(options, callback) {
|
332
|
+
const pending = [options];
|
333
|
+
for (const item of pending) {
|
334
|
+
const lazies = eachContractProcedure(item, callback);
|
335
|
+
for (const lazy2 of lazies) {
|
336
|
+
const { default: router } = await unlazy(lazy2.lazied);
|
337
|
+
pending.push({
|
338
|
+
path: lazy2.path,
|
339
|
+
router
|
340
|
+
});
|
341
|
+
}
|
342
|
+
}
|
343
|
+
}
|
344
|
+
function convertPathToHttpPath(path) {
|
345
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
346
|
+
}
|
347
|
+
function createContractedProcedure(contract, procedure) {
|
348
|
+
return new Procedure({
|
349
|
+
...procedure["~orpc"],
|
350
|
+
errorMap: contract["~orpc"].errorMap,
|
351
|
+
route: contract["~orpc"].route,
|
352
|
+
meta: contract["~orpc"].meta
|
353
|
+
});
|
354
|
+
}
|
355
|
+
|
299
356
|
export {
|
300
|
-
__export,
|
301
357
|
LAZY_LOADER_SYMBOL,
|
302
358
|
lazy,
|
303
359
|
isLazy,
|
@@ -315,6 +371,10 @@ export {
|
|
315
371
|
getLazyRouterPrefix,
|
316
372
|
createAccessibleLazyRouter,
|
317
373
|
adaptRouter,
|
318
|
-
getRouterChild
|
374
|
+
getRouterChild,
|
375
|
+
eachContractProcedure,
|
376
|
+
eachAllContractProcedure,
|
377
|
+
convertPathToHttpPath,
|
378
|
+
createContractedProcedure
|
319
379
|
};
|
320
|
-
//# sourceMappingURL=chunk-
|
380
|
+
//# sourceMappingURL=chunk-NOA3GBJQ.js.map
|
@@ -0,0 +1,111 @@
|
|
1
|
+
// src/plugins/base.ts
|
2
|
+
var CompositePlugin = class {
|
3
|
+
constructor(plugins = []) {
|
4
|
+
this.plugins = plugins;
|
5
|
+
}
|
6
|
+
init(options) {
|
7
|
+
for (const plugin of this.plugins) {
|
8
|
+
plugin.init?.(options);
|
9
|
+
}
|
10
|
+
}
|
11
|
+
};
|
12
|
+
|
13
|
+
// src/plugins/cors.ts
|
14
|
+
import { value } from "@orpc/shared";
|
15
|
+
var CORSPlugin = class {
|
16
|
+
options;
|
17
|
+
constructor(options) {
|
18
|
+
const defaults = {
|
19
|
+
origin: "*",
|
20
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
|
21
|
+
};
|
22
|
+
this.options = {
|
23
|
+
...defaults,
|
24
|
+
...options
|
25
|
+
};
|
26
|
+
}
|
27
|
+
init(options) {
|
28
|
+
options.interceptorsRoot ??= [];
|
29
|
+
options.interceptorsRoot.unshift(async (interceptorOptions) => {
|
30
|
+
if (interceptorOptions.request.method === "OPTIONS") {
|
31
|
+
const resHeaders = {};
|
32
|
+
if (this.options.maxAge !== void 0) {
|
33
|
+
resHeaders["access-control-max-age"] = this.options.maxAge.toString();
|
34
|
+
}
|
35
|
+
if (this.options.allowMethods?.length) {
|
36
|
+
resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
|
37
|
+
}
|
38
|
+
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
|
39
|
+
if (Array.isArray(allowHeaders) && allowHeaders.length) {
|
40
|
+
resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
|
41
|
+
} else if (typeof allowHeaders === "string") {
|
42
|
+
resHeaders["access-control-allow-headers"] = allowHeaders;
|
43
|
+
}
|
44
|
+
return {
|
45
|
+
matched: true,
|
46
|
+
response: {
|
47
|
+
status: 204,
|
48
|
+
headers: resHeaders,
|
49
|
+
body: void 0
|
50
|
+
}
|
51
|
+
};
|
52
|
+
}
|
53
|
+
return interceptorOptions.next();
|
54
|
+
});
|
55
|
+
options.interceptorsRoot.unshift(async (interceptorOptions) => {
|
56
|
+
const result = await interceptorOptions.next();
|
57
|
+
if (!result.matched) {
|
58
|
+
return result;
|
59
|
+
}
|
60
|
+
const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
|
61
|
+
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
62
|
+
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
63
|
+
if (allowedOriginArr.includes(origin)) {
|
64
|
+
result.response.headers["access-control-allow-origin"] = origin;
|
65
|
+
}
|
66
|
+
if (!allowedOriginArr.includes("*")) {
|
67
|
+
result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
|
68
|
+
}
|
69
|
+
if (this.options.credentials) {
|
70
|
+
result.response.headers["access-control-allow-credentials"] = "true";
|
71
|
+
}
|
72
|
+
if (this.options.exposeHeaders?.length) {
|
73
|
+
result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
|
74
|
+
}
|
75
|
+
return result;
|
76
|
+
});
|
77
|
+
}
|
78
|
+
};
|
79
|
+
|
80
|
+
// src/plugins/response-headers.ts
|
81
|
+
var ResponseHeadersPlugin = class {
|
82
|
+
init(options) {
|
83
|
+
options.interceptorsRoot ??= [];
|
84
|
+
options.interceptorsRoot.push(async (interceptorOptions) => {
|
85
|
+
const headers = new Headers();
|
86
|
+
interceptorOptions.context.resHeaders = headers;
|
87
|
+
const result = await interceptorOptions.next();
|
88
|
+
if (!result.matched) {
|
89
|
+
return result;
|
90
|
+
}
|
91
|
+
const responseHeaders = result.response.headers;
|
92
|
+
for (const [key, value2] of headers) {
|
93
|
+
if (Array.isArray(responseHeaders[key])) {
|
94
|
+
responseHeaders[key].push(value2);
|
95
|
+
} else if (responseHeaders[key] !== void 0) {
|
96
|
+
responseHeaders[key] = [responseHeaders[key], value2];
|
97
|
+
} else {
|
98
|
+
responseHeaders[key] = value2;
|
99
|
+
}
|
100
|
+
}
|
101
|
+
return result;
|
102
|
+
});
|
103
|
+
}
|
104
|
+
};
|
105
|
+
|
106
|
+
export {
|
107
|
+
CompositePlugin,
|
108
|
+
CORSPlugin,
|
109
|
+
ResponseHeadersPlugin
|
110
|
+
};
|
111
|
+
//# sourceMappingURL=chunk-OXB4YX67.js.map
|
package/dist/fetch.js
CHANGED
@@ -1,15 +1,18 @@
|
|
1
|
-
import "./chunk-WUOGVGWG.js";
|
2
1
|
import {
|
3
|
-
ORPCPayloadCodec,
|
4
|
-
ORPCProcedureMatcher,
|
5
2
|
RPCHandler,
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
fetchReToStandardBody,
|
4
|
+
fetchRequestToStandardRequest,
|
5
|
+
standardBodyToFetchBody,
|
6
|
+
standardResponseToFetchResponse
|
7
|
+
} from "./chunk-EYGVJA7A.js";
|
8
|
+
import "./chunk-CVIWJKJC.js";
|
9
|
+
import "./chunk-NOA3GBJQ.js";
|
10
|
+
import "./chunk-OXB4YX67.js";
|
9
11
|
export {
|
10
|
-
ORPCPayloadCodec,
|
11
|
-
ORPCProcedureMatcher,
|
12
12
|
RPCHandler,
|
13
|
-
|
13
|
+
fetchReToStandardBody,
|
14
|
+
fetchRequestToStandardRequest,
|
15
|
+
standardBodyToFetchBody,
|
16
|
+
standardResponseToFetchResponse
|
14
17
|
};
|
15
18
|
//# sourceMappingURL=fetch.js.map
|