@orpc/server 0.0.0-next.cba521d → 0.0.0-next.cc4cb21
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 +118 -0
- package/dist/chunk-EWOWWNDI.js +182 -0
- package/dist/chunk-MHVECKBC.js +421 -0
- package/dist/chunk-TXHKQO7N.js +120 -0
- package/dist/chunk-XT6IYOIS.js +32 -0
- package/dist/fetch.js +6 -11
- package/dist/hono.js +18 -14
- package/dist/index.js +236 -385
- package/dist/next.js +6 -11
- package/dist/node.js +19 -75
- package/dist/plugins.js +11 -0
- package/dist/src/adapters/fetch/index.d.ts +1 -4
- package/dist/src/adapters/fetch/rpc-handler.d.ts +11 -0
- package/dist/src/adapters/fetch/types.d.ts +4 -11
- package/dist/src/adapters/hono/middleware.d.ts +6 -6
- package/dist/src/adapters/next/serve.d.ts +11 -11
- package/dist/src/adapters/node/index.d.ts +1 -3
- package/dist/src/adapters/node/rpc-handler.d.ts +11 -0
- package/dist/src/adapters/node/types.d.ts +15 -15
- package/dist/src/adapters/standard/handler.d.ts +53 -0
- package/dist/src/adapters/standard/index.d.ts +6 -0
- package/dist/src/adapters/standard/rpc-codec.d.ts +16 -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/types.d.ts +21 -0
- package/dist/src/builder-variants.d.ts +75 -0
- package/dist/src/builder.d.ts +51 -29
- package/dist/src/config.d.ts +6 -0
- package/dist/src/context.d.ts +8 -0
- package/dist/src/error.d.ts +9 -7
- package/dist/src/hidden.d.ts +6 -4
- package/dist/src/implementer-procedure.d.ts +33 -0
- package/dist/src/implementer-variants.d.ts +18 -0
- package/dist/src/implementer.d.ts +29 -0
- package/dist/src/index.d.ts +12 -12
- package/dist/src/lazy-utils.d.ts +4 -2
- package/dist/src/lazy.d.ts +9 -5
- package/dist/src/middleware-decorated.d.ts +7 -5
- package/dist/src/middleware-utils.d.ts +5 -0
- package/dist/src/middleware.d.ts +23 -21
- package/dist/src/plugins/base.d.ts +11 -0
- package/dist/src/plugins/cors.d.ts +19 -0
- package/dist/src/plugins/index.d.ts +4 -0
- package/dist/src/plugins/response-headers.d.ts +10 -0
- package/dist/src/procedure-client.d.ts +23 -12
- package/dist/src/procedure-decorated.d.ts +13 -15
- package/dist/src/procedure-utils.d.ts +6 -4
- package/dist/src/procedure.d.ts +20 -33
- package/dist/src/router-accessible-lazy.d.ts +8 -0
- package/dist/src/router-client.d.ts +8 -22
- package/dist/src/router.d.ts +26 -12
- package/dist/src/utils.d.ts +23 -2
- package/dist/standard.js +13 -0
- package/package.json +20 -3
- package/dist/chunk-3EVCPLVI.js +0 -301
- package/dist/chunk-OUPZ7QGV.js +0 -245
- 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
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/lazy-decorated.d.ts +0 -7
- package/dist/src/procedure-builder.d.ts +0 -24
- package/dist/src/procedure-implementer.d.ts +0 -20
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/types.d.ts +0 -14
@@ -0,0 +1,421 @@
|
|
1
|
+
// src/lazy.ts
|
2
|
+
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
3
|
+
function lazy(loader) {
|
4
|
+
return {
|
5
|
+
[LAZY_LOADER_SYMBOL]: loader
|
6
|
+
};
|
7
|
+
}
|
8
|
+
function isLazy(item) {
|
9
|
+
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
10
|
+
}
|
11
|
+
function unlazy(lazied) {
|
12
|
+
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
13
|
+
}
|
14
|
+
|
15
|
+
// src/procedure.ts
|
16
|
+
import { isContractProcedure } from "@orpc/contract";
|
17
|
+
var Procedure = class {
|
18
|
+
"~orpc";
|
19
|
+
constructor(def) {
|
20
|
+
this["~orpc"] = def;
|
21
|
+
}
|
22
|
+
};
|
23
|
+
function isProcedure(item) {
|
24
|
+
if (item instanceof Procedure) {
|
25
|
+
return true;
|
26
|
+
}
|
27
|
+
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
28
|
+
}
|
29
|
+
|
30
|
+
// src/lazy-utils.ts
|
31
|
+
function flatLazy(lazied) {
|
32
|
+
const flattenLoader = async () => {
|
33
|
+
let current = await unlazy(lazied);
|
34
|
+
while (true) {
|
35
|
+
if (!isLazy(current.default)) {
|
36
|
+
break;
|
37
|
+
}
|
38
|
+
current = await unlazy(current.default);
|
39
|
+
}
|
40
|
+
return current;
|
41
|
+
};
|
42
|
+
return lazy(flattenLoader);
|
43
|
+
}
|
44
|
+
function createLazyProcedureFormAnyLazy(lazied) {
|
45
|
+
const lazyProcedure = lazy(async () => {
|
46
|
+
const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
|
47
|
+
if (!isProcedure(maybeProcedure)) {
|
48
|
+
throw new Error(`
|
49
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
50
|
+
This should be caught by TypeScript compilation.
|
51
|
+
Please report this issue if this makes you feel uncomfortable.
|
52
|
+
`);
|
53
|
+
}
|
54
|
+
return { default: maybeProcedure };
|
55
|
+
});
|
56
|
+
return lazyProcedure;
|
57
|
+
}
|
58
|
+
|
59
|
+
// src/middleware.ts
|
60
|
+
function middlewareOutputFn(output) {
|
61
|
+
return { output, context: {} };
|
62
|
+
}
|
63
|
+
|
64
|
+
// src/procedure-client.ts
|
65
|
+
import { ORPCError as ORPCError2 } from "@orpc/client";
|
66
|
+
import { ValidationError } from "@orpc/contract";
|
67
|
+
import { intercept, toError, value } from "@orpc/shared";
|
68
|
+
|
69
|
+
// src/error.ts
|
70
|
+
import { fallbackORPCErrorStatus, ORPCError } from "@orpc/client";
|
71
|
+
function createORPCErrorConstructorMap(errors) {
|
72
|
+
const proxy = new Proxy(errors, {
|
73
|
+
get(target, code) {
|
74
|
+
if (typeof code !== "string") {
|
75
|
+
return Reflect.get(target, code);
|
76
|
+
}
|
77
|
+
const item = (...[options]) => {
|
78
|
+
const config = errors[code];
|
79
|
+
return new ORPCError(code, {
|
80
|
+
defined: Boolean(config),
|
81
|
+
status: config?.status,
|
82
|
+
message: options?.message ?? config?.message,
|
83
|
+
data: options?.data,
|
84
|
+
cause: options?.cause
|
85
|
+
});
|
86
|
+
};
|
87
|
+
return item;
|
88
|
+
}
|
89
|
+
});
|
90
|
+
return proxy;
|
91
|
+
}
|
92
|
+
async function validateORPCError(map, error) {
|
93
|
+
const { code, status, message, data, cause, defined } = error;
|
94
|
+
const config = map?.[error.code];
|
95
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
96
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
97
|
+
}
|
98
|
+
if (!config.data) {
|
99
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
100
|
+
}
|
101
|
+
const validated = await config.data["~standard"].validate(error.data);
|
102
|
+
if (validated.issues) {
|
103
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
104
|
+
}
|
105
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
106
|
+
}
|
107
|
+
|
108
|
+
// src/procedure-client.ts
|
109
|
+
function createProcedureClient(lazyableProcedure, ...[options]) {
|
110
|
+
return async (...[input, callerOptions]) => {
|
111
|
+
const path = options?.path ?? [];
|
112
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
113
|
+
const clientContext = callerOptions?.context ?? {};
|
114
|
+
const context = await value(options?.context ?? {}, clientContext);
|
115
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
116
|
+
try {
|
117
|
+
return await intercept(
|
118
|
+
options?.interceptors ?? [],
|
119
|
+
{
|
120
|
+
context,
|
121
|
+
input,
|
122
|
+
// input only optional when it undefinable so we can safely cast it
|
123
|
+
errors,
|
124
|
+
path,
|
125
|
+
procedure,
|
126
|
+
signal: callerOptions?.signal,
|
127
|
+
lastEventId: callerOptions?.lastEventId
|
128
|
+
},
|
129
|
+
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
130
|
+
);
|
131
|
+
} catch (e) {
|
132
|
+
if (!(e instanceof ORPCError2)) {
|
133
|
+
throw toError(e);
|
134
|
+
}
|
135
|
+
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
136
|
+
throw validated;
|
137
|
+
}
|
138
|
+
};
|
139
|
+
}
|
140
|
+
async function validateInput(procedure, input) {
|
141
|
+
const schema = procedure["~orpc"].inputSchema;
|
142
|
+
if (!schema) {
|
143
|
+
return input;
|
144
|
+
}
|
145
|
+
const result = await schema["~standard"].validate(input);
|
146
|
+
if (result.issues) {
|
147
|
+
throw new ORPCError2("BAD_REQUEST", {
|
148
|
+
message: "Input validation failed",
|
149
|
+
data: {
|
150
|
+
issues: result.issues
|
151
|
+
},
|
152
|
+
cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
|
153
|
+
});
|
154
|
+
}
|
155
|
+
return result.value;
|
156
|
+
}
|
157
|
+
async function validateOutput(procedure, output) {
|
158
|
+
const schema = procedure["~orpc"].outputSchema;
|
159
|
+
if (!schema) {
|
160
|
+
return output;
|
161
|
+
}
|
162
|
+
const result = await schema["~standard"].validate(output);
|
163
|
+
if (result.issues) {
|
164
|
+
throw new ORPCError2("INTERNAL_SERVER_ERROR", {
|
165
|
+
message: "Output validation failed",
|
166
|
+
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
167
|
+
});
|
168
|
+
}
|
169
|
+
return result.value;
|
170
|
+
}
|
171
|
+
async function executeProcedureInternal(procedure, options) {
|
172
|
+
const middlewares = procedure["~orpc"].middlewares;
|
173
|
+
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
174
|
+
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
175
|
+
let currentIndex = 0;
|
176
|
+
let currentContext = options.context;
|
177
|
+
let currentInput = options.input;
|
178
|
+
const next = async (...[nextOptions]) => {
|
179
|
+
const index = currentIndex;
|
180
|
+
currentIndex += 1;
|
181
|
+
currentContext = { ...currentContext, ...nextOptions?.context };
|
182
|
+
if (index === inputValidationIndex) {
|
183
|
+
currentInput = await validateInput(procedure, currentInput);
|
184
|
+
}
|
185
|
+
const mid = middlewares[index];
|
186
|
+
const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
|
187
|
+
if (index === outputValidationIndex) {
|
188
|
+
const validatedOutput = await validateOutput(procedure, result.output);
|
189
|
+
return {
|
190
|
+
...result,
|
191
|
+
output: validatedOutput
|
192
|
+
};
|
193
|
+
}
|
194
|
+
return result;
|
195
|
+
};
|
196
|
+
return (await next({})).output;
|
197
|
+
}
|
198
|
+
|
199
|
+
// src/hidden.ts
|
200
|
+
var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
|
201
|
+
function setRouterContract(obj, contract) {
|
202
|
+
return new Proxy(obj, {
|
203
|
+
get(target, key) {
|
204
|
+
if (key === ROUTER_CONTRACT_SYMBOL) {
|
205
|
+
return contract;
|
206
|
+
}
|
207
|
+
return Reflect.get(target, key);
|
208
|
+
}
|
209
|
+
});
|
210
|
+
}
|
211
|
+
function getRouterContract(obj) {
|
212
|
+
return obj[ROUTER_CONTRACT_SYMBOL];
|
213
|
+
}
|
214
|
+
var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
|
215
|
+
function deepSetLazyRouterPrefix(router, prefix) {
|
216
|
+
return new Proxy(router, {
|
217
|
+
get(target, key) {
|
218
|
+
if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
|
219
|
+
const val = Reflect.get(target, key);
|
220
|
+
if (isLazy(val)) {
|
221
|
+
return deepSetLazyRouterPrefix(val, prefix);
|
222
|
+
}
|
223
|
+
return val;
|
224
|
+
}
|
225
|
+
return prefix;
|
226
|
+
}
|
227
|
+
});
|
228
|
+
}
|
229
|
+
function getLazyRouterPrefix(obj) {
|
230
|
+
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
231
|
+
}
|
232
|
+
|
233
|
+
// src/router.ts
|
234
|
+
import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
|
235
|
+
|
236
|
+
// src/middleware-utils.ts
|
237
|
+
function dedupeMiddlewares(compare, middlewares) {
|
238
|
+
let min = 0;
|
239
|
+
for (let i = 0; i < middlewares.length; i++) {
|
240
|
+
const index = compare.indexOf(middlewares[i], min);
|
241
|
+
if (index === -1) {
|
242
|
+
return middlewares.slice(i);
|
243
|
+
}
|
244
|
+
min = index + 1;
|
245
|
+
}
|
246
|
+
return [];
|
247
|
+
}
|
248
|
+
function mergeMiddlewares(first, second) {
|
249
|
+
return [...first, ...dedupeMiddlewares(first, second)];
|
250
|
+
}
|
251
|
+
function addMiddleware(middlewares, addition) {
|
252
|
+
return [...middlewares, addition];
|
253
|
+
}
|
254
|
+
|
255
|
+
// src/router.ts
|
256
|
+
function adaptRouter(router, options) {
|
257
|
+
if (isLazy(router)) {
|
258
|
+
const adapted2 = lazy(async () => {
|
259
|
+
const unlaziedRouter = (await unlazy(router)).default;
|
260
|
+
const adapted3 = adaptRouter(unlaziedRouter, options);
|
261
|
+
return { default: adapted3 };
|
262
|
+
});
|
263
|
+
const accessible = createAccessibleLazyRouter(adapted2);
|
264
|
+
const currentPrefix = getLazyRouterPrefix(router);
|
265
|
+
const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
|
266
|
+
if (prefix) {
|
267
|
+
return deepSetLazyRouterPrefix(accessible, prefix);
|
268
|
+
}
|
269
|
+
return accessible;
|
270
|
+
}
|
271
|
+
if (isProcedure(router)) {
|
272
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
273
|
+
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
274
|
+
const adapted2 = new Procedure({
|
275
|
+
...router["~orpc"],
|
276
|
+
route: adaptRoute(router["~orpc"].route, options),
|
277
|
+
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
278
|
+
middlewares: newMiddlewares,
|
279
|
+
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
280
|
+
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
281
|
+
});
|
282
|
+
return adapted2;
|
283
|
+
}
|
284
|
+
const adapted = {};
|
285
|
+
for (const key in router) {
|
286
|
+
adapted[key] = adaptRouter(router[key], options);
|
287
|
+
}
|
288
|
+
return adapted;
|
289
|
+
}
|
290
|
+
function getRouterChild(router, ...path) {
|
291
|
+
let current = router;
|
292
|
+
for (let i = 0; i < path.length; i++) {
|
293
|
+
const segment = path[i];
|
294
|
+
if (!current) {
|
295
|
+
return void 0;
|
296
|
+
}
|
297
|
+
if (isProcedure(current)) {
|
298
|
+
return void 0;
|
299
|
+
}
|
300
|
+
if (!isLazy(current)) {
|
301
|
+
current = current[segment];
|
302
|
+
continue;
|
303
|
+
}
|
304
|
+
const lazied = current;
|
305
|
+
const rest = path.slice(i);
|
306
|
+
const newLazy = lazy(async () => {
|
307
|
+
const unwrapped = await unlazy(lazied);
|
308
|
+
if (!unwrapped.default) {
|
309
|
+
return unwrapped;
|
310
|
+
}
|
311
|
+
const next = getRouterChild(unwrapped.default, ...rest);
|
312
|
+
return { default: next };
|
313
|
+
});
|
314
|
+
return flatLazy(newLazy);
|
315
|
+
}
|
316
|
+
return current;
|
317
|
+
}
|
318
|
+
|
319
|
+
// src/router-accessible-lazy.ts
|
320
|
+
function createAccessibleLazyRouter(lazied) {
|
321
|
+
const flattenLazy = flatLazy(lazied);
|
322
|
+
const recursive = new Proxy(flattenLazy, {
|
323
|
+
get(target, key) {
|
324
|
+
if (typeof key !== "string") {
|
325
|
+
return Reflect.get(target, key);
|
326
|
+
}
|
327
|
+
const next = getRouterChild(flattenLazy, key);
|
328
|
+
return createAccessibleLazyRouter(next);
|
329
|
+
}
|
330
|
+
});
|
331
|
+
return recursive;
|
332
|
+
}
|
333
|
+
|
334
|
+
// src/utils.ts
|
335
|
+
import { isContractProcedure as isContractProcedure2 } from "@orpc/contract";
|
336
|
+
function eachContractProcedure(options, callback, laziedOptions = []) {
|
337
|
+
const hiddenContract = getRouterContract(options.router);
|
338
|
+
if (hiddenContract) {
|
339
|
+
return eachContractProcedure(
|
340
|
+
{
|
341
|
+
router: hiddenContract,
|
342
|
+
path: options.path
|
343
|
+
},
|
344
|
+
callback,
|
345
|
+
laziedOptions
|
346
|
+
);
|
347
|
+
}
|
348
|
+
if (isLazy(options.router)) {
|
349
|
+
laziedOptions.push({
|
350
|
+
lazied: options.router,
|
351
|
+
path: options.path
|
352
|
+
});
|
353
|
+
} else if (isContractProcedure2(options.router)) {
|
354
|
+
callback({
|
355
|
+
contract: options.router,
|
356
|
+
path: options.path
|
357
|
+
});
|
358
|
+
} else {
|
359
|
+
for (const key in options.router) {
|
360
|
+
eachContractProcedure(
|
361
|
+
{
|
362
|
+
router: options.router[key],
|
363
|
+
path: [...options.path, key]
|
364
|
+
},
|
365
|
+
callback,
|
366
|
+
laziedOptions
|
367
|
+
);
|
368
|
+
}
|
369
|
+
}
|
370
|
+
return laziedOptions;
|
371
|
+
}
|
372
|
+
async function eachAllContractProcedure(options, callback) {
|
373
|
+
const pending = [options];
|
374
|
+
for (const item of pending) {
|
375
|
+
const lazies = eachContractProcedure(item, callback);
|
376
|
+
for (const lazy2 of lazies) {
|
377
|
+
const { default: router } = await unlazy(lazy2.lazied);
|
378
|
+
pending.push({
|
379
|
+
path: lazy2.path,
|
380
|
+
router
|
381
|
+
});
|
382
|
+
}
|
383
|
+
}
|
384
|
+
}
|
385
|
+
function convertPathToHttpPath(path) {
|
386
|
+
return `/${path.map(encodeURIComponent).join("/")}`;
|
387
|
+
}
|
388
|
+
function createContractedProcedure(contract, procedure) {
|
389
|
+
return new Procedure({
|
390
|
+
...procedure["~orpc"],
|
391
|
+
errorMap: contract["~orpc"].errorMap,
|
392
|
+
route: contract["~orpc"].route,
|
393
|
+
meta: contract["~orpc"].meta
|
394
|
+
});
|
395
|
+
}
|
396
|
+
|
397
|
+
export {
|
398
|
+
LAZY_LOADER_SYMBOL,
|
399
|
+
lazy,
|
400
|
+
isLazy,
|
401
|
+
unlazy,
|
402
|
+
Procedure,
|
403
|
+
isProcedure,
|
404
|
+
flatLazy,
|
405
|
+
createLazyProcedureFormAnyLazy,
|
406
|
+
addMiddleware,
|
407
|
+
middlewareOutputFn,
|
408
|
+
createProcedureClient,
|
409
|
+
setRouterContract,
|
410
|
+
getRouterContract,
|
411
|
+
deepSetLazyRouterPrefix,
|
412
|
+
getLazyRouterPrefix,
|
413
|
+
createAccessibleLazyRouter,
|
414
|
+
adaptRouter,
|
415
|
+
getRouterChild,
|
416
|
+
eachContractProcedure,
|
417
|
+
eachAllContractProcedure,
|
418
|
+
convertPathToHttpPath,
|
419
|
+
createContractedProcedure
|
420
|
+
};
|
421
|
+
//# sourceMappingURL=chunk-MHVECKBC.js.map
|
@@ -0,0 +1,120 @@
|
|
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: (origin) => origin,
|
20
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
|
21
|
+
};
|
22
|
+
this.options = {
|
23
|
+
...defaults,
|
24
|
+
...options
|
25
|
+
};
|
26
|
+
}
|
27
|
+
init(options) {
|
28
|
+
options.rootInterceptors ??= [];
|
29
|
+
options.rootInterceptors.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.rootInterceptors.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("*")) {
|
64
|
+
result.response.headers["access-control-allow-origin"] = "*";
|
65
|
+
} else {
|
66
|
+
if (allowedOriginArr.includes(origin)) {
|
67
|
+
result.response.headers["access-control-allow-origin"] = origin;
|
68
|
+
}
|
69
|
+
result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
|
70
|
+
}
|
71
|
+
const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
|
72
|
+
const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
|
73
|
+
if (allowedTimingOriginArr.includes("*")) {
|
74
|
+
result.response.headers["timing-allow-origin"] = "*";
|
75
|
+
} else if (allowedTimingOriginArr.includes(origin)) {
|
76
|
+
result.response.headers["timing-allow-origin"] = origin;
|
77
|
+
}
|
78
|
+
if (this.options.credentials) {
|
79
|
+
result.response.headers["access-control-allow-credentials"] = "true";
|
80
|
+
}
|
81
|
+
if (this.options.exposeHeaders?.length) {
|
82
|
+
result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
|
83
|
+
}
|
84
|
+
return result;
|
85
|
+
});
|
86
|
+
}
|
87
|
+
};
|
88
|
+
|
89
|
+
// src/plugins/response-headers.ts
|
90
|
+
var ResponseHeadersPlugin = class {
|
91
|
+
init(options) {
|
92
|
+
options.rootInterceptors ??= [];
|
93
|
+
options.rootInterceptors.push(async (interceptorOptions) => {
|
94
|
+
const headers = new Headers();
|
95
|
+
interceptorOptions.context.resHeaders = headers;
|
96
|
+
const result = await interceptorOptions.next();
|
97
|
+
if (!result.matched) {
|
98
|
+
return result;
|
99
|
+
}
|
100
|
+
const responseHeaders = result.response.headers;
|
101
|
+
for (const [key, value2] of headers) {
|
102
|
+
if (Array.isArray(responseHeaders[key])) {
|
103
|
+
responseHeaders[key].push(value2);
|
104
|
+
} else if (responseHeaders[key] !== void 0) {
|
105
|
+
responseHeaders[key] = [responseHeaders[key], value2];
|
106
|
+
} else {
|
107
|
+
responseHeaders[key] = value2;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
return result;
|
111
|
+
});
|
112
|
+
}
|
113
|
+
};
|
114
|
+
|
115
|
+
export {
|
116
|
+
CompositePlugin,
|
117
|
+
CORSPlugin,
|
118
|
+
ResponseHeadersPlugin
|
119
|
+
};
|
120
|
+
//# sourceMappingURL=chunk-TXHKQO7N.js.map
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import {
|
2
|
+
RPCCodec,
|
3
|
+
RPCMatcher,
|
4
|
+
StandardHandler
|
5
|
+
} from "./chunk-EWOWWNDI.js";
|
6
|
+
|
7
|
+
// src/adapters/fetch/rpc-handler.ts
|
8
|
+
import { toFetchResponse, toStandardRequest } from "@orpc/standard-server-fetch";
|
9
|
+
var RPCHandler = class {
|
10
|
+
standardHandler;
|
11
|
+
constructor(router, options) {
|
12
|
+
const matcher = options?.matcher ?? new RPCMatcher();
|
13
|
+
const codec = options?.codec ?? new RPCCodec();
|
14
|
+
this.standardHandler = new StandardHandler(router, matcher, codec, options);
|
15
|
+
}
|
16
|
+
async handle(request, ...rest) {
|
17
|
+
const standardRequest = toStandardRequest(request);
|
18
|
+
const result = await this.standardHandler.handle(standardRequest, ...rest);
|
19
|
+
if (!result.matched) {
|
20
|
+
return result;
|
21
|
+
}
|
22
|
+
return {
|
23
|
+
matched: true,
|
24
|
+
response: toFetchResponse(result.response)
|
25
|
+
};
|
26
|
+
}
|
27
|
+
};
|
28
|
+
|
29
|
+
export {
|
30
|
+
RPCHandler
|
31
|
+
};
|
32
|
+
//# sourceMappingURL=chunk-XT6IYOIS.js.map
|
package/dist/fetch.js
CHANGED
@@ -1,15 +1,10 @@
|
|
1
|
-
import "./chunk-WUOGVGWG.js";
|
2
1
|
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
import "./chunk-OUPZ7QGV.js";
|
2
|
+
RPCHandler
|
3
|
+
} from "./chunk-XT6IYOIS.js";
|
4
|
+
import "./chunk-EWOWWNDI.js";
|
5
|
+
import "./chunk-MHVECKBC.js";
|
6
|
+
import "./chunk-TXHKQO7N.js";
|
9
7
|
export {
|
10
|
-
|
11
|
-
ORPCProcedureMatcher,
|
12
|
-
RPCHandler,
|
13
|
-
super_json_exports as SuperJSON
|
8
|
+
RPCHandler
|
14
9
|
};
|
15
10
|
//# sourceMappingURL=fetch.js.map
|
package/dist/hono.js
CHANGED
@@ -1,30 +1,34 @@
|
|
1
|
-
import "./chunk-WUOGVGWG.js";
|
2
1
|
import {
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
import "./chunk-OUPZ7QGV.js";
|
2
|
+
RPCHandler
|
3
|
+
} from "./chunk-XT6IYOIS.js";
|
4
|
+
import "./chunk-EWOWWNDI.js";
|
5
|
+
import "./chunk-MHVECKBC.js";
|
6
|
+
import "./chunk-TXHKQO7N.js";
|
9
7
|
|
10
8
|
// src/adapters/hono/middleware.ts
|
11
9
|
import { value } from "@orpc/shared";
|
12
10
|
function createMiddleware(handler, ...[options]) {
|
13
11
|
return async (c, next) => {
|
14
|
-
const
|
15
|
-
const
|
12
|
+
const bodyProps = /* @__PURE__ */ new Set(["arrayBuffer", "blob", "formData", "json", "text"]);
|
13
|
+
const request = c.req.method === "GET" || c.req.method === "HEAD" ? c.req.raw : new Proxy(c.req.raw, {
|
14
|
+
// https://github.com/honojs/middleware/blob/main/packages/trpc-server/src/index.ts#L39
|
15
|
+
get(target, prop) {
|
16
|
+
if (bodyProps.has(prop)) {
|
17
|
+
return () => c.req[prop]();
|
18
|
+
}
|
19
|
+
return Reflect.get(target, prop, target);
|
20
|
+
}
|
21
|
+
});
|
22
|
+
const context = await value(options?.context ?? {}, c);
|
23
|
+
const { matched, response } = await handler.handle(request, { ...options, context });
|
16
24
|
if (matched) {
|
17
|
-
c.
|
18
|
-
return;
|
25
|
+
return c.newResponse(response.body, response);
|
19
26
|
}
|
20
27
|
await next();
|
21
28
|
};
|
22
29
|
}
|
23
30
|
export {
|
24
|
-
ORPCPayloadCodec,
|
25
|
-
ORPCProcedureMatcher,
|
26
31
|
RPCHandler,
|
27
|
-
super_json_exports as SuperJSON,
|
28
32
|
createMiddleware
|
29
33
|
};
|
30
34
|
//# sourceMappingURL=hono.js.map
|