@orpc/server 0.0.0-next.22ccd98 → 0.0.0-next.23aa4be
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-B2EZJB7X.js → chunk-5IM453DN.js} +16 -18
- package/dist/chunk-WUOGVGWG.js +1 -0
- package/dist/{chunk-6A7XHEBH.js → chunk-ZBJYYEII.js} +70 -32
- package/dist/fetch.js +3 -20
- package/dist/hono.js +30 -0
- package/dist/index.js +135 -106
- package/dist/next.js +36 -0
- package/dist/node.js +66 -24
- package/dist/src/adapters/fetch/index.d.ts +0 -1
- package/dist/src/adapters/fetch/orpc-handler.d.ts +8 -8
- package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +1 -1
- package/dist/src/adapters/fetch/types.d.ts +11 -6
- package/dist/src/adapters/hono/index.d.ts +3 -0
- package/dist/src/adapters/hono/middleware.d.ts +12 -0
- package/dist/src/adapters/next/index.d.ts +3 -0
- package/dist/src/adapters/next/serve.d.ts +19 -0
- package/dist/src/adapters/node/index.d.ts +1 -1
- package/dist/src/adapters/node/orpc-handler.d.ts +5 -5
- package/dist/src/adapters/node/request-listener.d.ts +28 -0
- package/dist/src/adapters/node/types.d.ts +10 -9
- package/dist/src/builder.d.ts +11 -10
- package/dist/src/error.d.ts +10 -0
- package/dist/src/implementer-chainable.d.ts +2 -2
- package/dist/src/index.d.ts +3 -2
- package/dist/src/lazy-decorated.d.ts +3 -6
- package/dist/src/middleware-decorated.d.ts +6 -5
- package/dist/src/middleware.d.ts +20 -8
- package/dist/src/procedure-builder.d.ts +17 -15
- package/dist/src/procedure-client.d.ts +8 -20
- package/dist/src/procedure-decorated.d.ts +22 -10
- package/dist/src/procedure-implementer.d.ts +13 -12
- package/dist/src/procedure-utils.d.ts +17 -0
- package/dist/src/procedure.d.ts +33 -13
- package/dist/src/router-builder.d.ts +4 -4
- package/dist/src/router-client.d.ts +7 -5
- package/dist/src/router-implementer.d.ts +2 -2
- package/dist/src/router.d.ts +4 -4
- package/dist/src/types.d.ts +2 -0
- package/package.json +18 -8
- package/dist/src/adapters/fetch/composite-handler.d.ts +0 -8
- package/dist/src/adapters/node/composite-handler.d.ts +0 -9
package/dist/index.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
import {
|
2
2
|
LAZY_LOADER_SYMBOL,
|
3
3
|
Procedure,
|
4
|
+
createORPCErrorConstructorMap,
|
4
5
|
createProcedureClient,
|
5
6
|
flatLazy,
|
6
7
|
getRouterChild,
|
@@ -9,31 +10,31 @@ import {
|
|
9
10
|
lazy,
|
10
11
|
mergeContext,
|
11
12
|
unlazy
|
12
|
-
} from "./chunk-
|
13
|
+
} from "./chunk-ZBJYYEII.js";
|
13
14
|
|
14
15
|
// src/builder.ts
|
15
16
|
import { ContractProcedure } from "@orpc/contract";
|
16
17
|
|
17
18
|
// src/implementer-chainable.ts
|
18
19
|
import { isContractProcedure } from "@orpc/contract";
|
19
|
-
import { createCallableObject } from "@orpc/shared";
|
20
|
+
import { createCallableObject as createCallableObject2 } from "@orpc/shared";
|
20
21
|
|
21
22
|
// src/middleware-decorated.ts
|
22
23
|
function decorateMiddleware(middleware) {
|
23
24
|
const decorated = middleware;
|
24
25
|
decorated.mapInput = (mapInput) => {
|
25
26
|
const mapped = decorateMiddleware(
|
26
|
-
(input, ...rest) => middleware(mapInput(input), ...rest)
|
27
|
+
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
27
28
|
);
|
28
29
|
return mapped;
|
29
30
|
};
|
30
31
|
decorated.concat = (concatMiddleware, mapInput) => {
|
31
32
|
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
32
|
-
const concatted = decorateMiddleware((
|
33
|
-
const next = async (
|
34
|
-
return mapped(
|
33
|
+
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
34
|
+
const next = async (nextOptions) => {
|
35
|
+
return mapped({ ...options, context: mergeContext(nextOptions.context, options.context) }, input, output, ...rest);
|
35
36
|
};
|
36
|
-
const merged = middleware(
|
37
|
+
const merged = middleware({ ...options, next }, input, output, ...rest);
|
37
38
|
return merged;
|
38
39
|
});
|
39
40
|
return concatted;
|
@@ -43,58 +44,72 @@ function decorateMiddleware(middleware) {
|
|
43
44
|
|
44
45
|
// src/procedure-decorated.ts
|
45
46
|
import { DecoratedContractProcedure } from "@orpc/contract";
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
return
|
56
|
-
...
|
57
|
-
contract: DecoratedContractProcedure.decorate(
|
58
|
-
})
|
59
|
-
}
|
60
|
-
|
61
|
-
return
|
62
|
-
...
|
63
|
-
contract: DecoratedContractProcedure.decorate(
|
64
|
-
})
|
65
|
-
}
|
66
|
-
|
47
|
+
import { createCallableObject } from "@orpc/shared";
|
48
|
+
var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
|
49
|
+
static decorate(procedure) {
|
50
|
+
if (procedure instanceof _DecoratedProcedure) {
|
51
|
+
return procedure;
|
52
|
+
}
|
53
|
+
return new _DecoratedProcedure(procedure["~orpc"]);
|
54
|
+
}
|
55
|
+
prefix(prefix) {
|
56
|
+
return new _DecoratedProcedure({
|
57
|
+
...this["~orpc"],
|
58
|
+
contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).prefix(prefix)
|
59
|
+
});
|
60
|
+
}
|
61
|
+
route(route) {
|
62
|
+
return new _DecoratedProcedure({
|
63
|
+
...this["~orpc"],
|
64
|
+
contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).route(route)
|
65
|
+
});
|
66
|
+
}
|
67
|
+
use(middleware, mapInput) {
|
67
68
|
const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
68
|
-
return
|
69
|
-
...
|
70
|
-
middlewares: [...
|
71
|
-
})
|
72
|
-
}
|
73
|
-
|
74
|
-
return
|
75
|
-
...
|
76
|
-
contract: DecoratedContractProcedure.decorate(
|
77
|
-
})
|
78
|
-
}
|
79
|
-
|
80
|
-
|
69
|
+
return new _DecoratedProcedure({
|
70
|
+
...this["~orpc"],
|
71
|
+
middlewares: [...this["~orpc"].middlewares ?? [], middleware_]
|
72
|
+
});
|
73
|
+
}
|
74
|
+
unshiftTag(...tags) {
|
75
|
+
return new _DecoratedProcedure({
|
76
|
+
...this["~orpc"],
|
77
|
+
contract: DecoratedContractProcedure.decorate(this["~orpc"].contract).unshiftTag(...tags)
|
78
|
+
});
|
79
|
+
}
|
80
|
+
unshiftMiddleware(...middlewares) {
|
81
|
+
const castedMiddlewares = middlewares;
|
82
|
+
if (this["~orpc"].middlewares?.length) {
|
81
83
|
let min = 0;
|
82
|
-
for (let i = 0; i <
|
83
|
-
const index =
|
84
|
+
for (let i = 0; i < this["~orpc"].middlewares.length; i++) {
|
85
|
+
const index = castedMiddlewares.indexOf(this["~orpc"].middlewares[i], min);
|
84
86
|
if (index === -1) {
|
85
|
-
|
87
|
+
castedMiddlewares.push(...this["~orpc"].middlewares.slice(i));
|
86
88
|
break;
|
87
89
|
}
|
88
90
|
min = index + 1;
|
89
91
|
}
|
90
92
|
}
|
91
|
-
return
|
92
|
-
...
|
93
|
-
middlewares
|
94
|
-
})
|
95
|
-
}
|
96
|
-
|
97
|
-
|
93
|
+
return new _DecoratedProcedure({
|
94
|
+
...this["~orpc"],
|
95
|
+
middlewares: castedMiddlewares
|
96
|
+
});
|
97
|
+
}
|
98
|
+
/**
|
99
|
+
* Make this procedure callable (works like a function while still being a procedure).
|
100
|
+
* **Note**: this only takes effect when this method is called at the end of the chain.
|
101
|
+
*/
|
102
|
+
callable(...rest) {
|
103
|
+
return createCallableObject(this, createProcedureClient(this, ...rest));
|
104
|
+
}
|
105
|
+
/**
|
106
|
+
* Make this procedure compatible with server action (the same as .callable, but the type is compatible with server action).
|
107
|
+
* **Note**: this only takes effect when this method is called at the end of the chain.
|
108
|
+
*/
|
109
|
+
actionable(...rest) {
|
110
|
+
return this.callable(...rest);
|
111
|
+
}
|
112
|
+
};
|
98
113
|
|
99
114
|
// src/procedure-implementer.ts
|
100
115
|
var ProcedureImplementer = class _ProcedureImplementer {
|
@@ -111,11 +126,11 @@ var ProcedureImplementer = class _ProcedureImplementer {
|
|
111
126
|
});
|
112
127
|
}
|
113
128
|
handler(handler) {
|
114
|
-
return
|
129
|
+
return new DecoratedProcedure({
|
115
130
|
middlewares: this["~orpc"].middlewares,
|
116
131
|
contract: this["~orpc"].contract,
|
117
132
|
handler
|
118
|
-
})
|
133
|
+
});
|
119
134
|
}
|
120
135
|
};
|
121
136
|
|
@@ -153,31 +168,10 @@ function getLazyRouterPrefix(obj) {
|
|
153
168
|
return obj[LAZY_ROUTER_PREFIX_SYMBOL];
|
154
169
|
}
|
155
170
|
|
156
|
-
// src/lazy-utils.ts
|
157
|
-
function createLazyProcedureFormAnyLazy(lazied) {
|
158
|
-
const lazyProcedure = lazy(async () => {
|
159
|
-
const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
|
160
|
-
if (!isProcedure(maybeProcedure)) {
|
161
|
-
throw new Error(`
|
162
|
-
Expected a lazy<procedure> but got lazy<unknown>.
|
163
|
-
This should be caught by TypeScript compilation.
|
164
|
-
Please report this issue if this makes you feel uncomfortable.
|
165
|
-
`);
|
166
|
-
}
|
167
|
-
return { default: maybeProcedure };
|
168
|
-
});
|
169
|
-
return lazyProcedure;
|
170
|
-
}
|
171
|
-
|
172
171
|
// src/lazy-decorated.ts
|
173
172
|
function decorateLazy(lazied) {
|
174
173
|
const flattenLazy = flatLazy(lazied);
|
175
|
-
const
|
176
|
-
procedure: createLazyProcedureFormAnyLazy(flattenLazy),
|
177
|
-
context: void 0
|
178
|
-
});
|
179
|
-
Object.assign(procedureProcedureClient, flattenLazy);
|
180
|
-
const recursive = new Proxy(procedureProcedureClient, {
|
174
|
+
const recursive = new Proxy(flattenLazy, {
|
181
175
|
get(target, key) {
|
182
176
|
if (typeof key !== "string") {
|
183
177
|
return Reflect.get(target, key);
|
@@ -244,7 +238,7 @@ function adapt(item, options) {
|
|
244
238
|
return adaptedLazy;
|
245
239
|
}
|
246
240
|
if (isProcedure(item)) {
|
247
|
-
let decorated =
|
241
|
+
let decorated = DecoratedProcedure.decorate(item);
|
248
242
|
if (options.tags?.length) {
|
249
243
|
decorated = decorated.unshiftTag(...options.tags);
|
250
244
|
}
|
@@ -312,7 +306,7 @@ function createChainableImplementer(contract, middlewares) {
|
|
312
306
|
if (!next) {
|
313
307
|
return method.bind(routerImplementer);
|
314
308
|
}
|
315
|
-
return
|
309
|
+
return createCallableObject2(next, method.bind(routerImplementer));
|
316
310
|
}
|
317
311
|
});
|
318
312
|
return merged;
|
@@ -346,6 +340,12 @@ var ProcedureBuilder = class _ProcedureBuilder {
|
|
346
340
|
contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).output(schema, example)
|
347
341
|
});
|
348
342
|
}
|
343
|
+
errors(errors) {
|
344
|
+
return new _ProcedureBuilder({
|
345
|
+
...this["~orpc"],
|
346
|
+
contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).errors(errors)
|
347
|
+
});
|
348
|
+
}
|
349
349
|
use(middleware, mapInput) {
|
350
350
|
if (!mapInput) {
|
351
351
|
return new ProcedureImplementer({
|
@@ -359,11 +359,11 @@ var ProcedureBuilder = class _ProcedureBuilder {
|
|
359
359
|
}).use(middleware, mapInput);
|
360
360
|
}
|
361
361
|
handler(handler) {
|
362
|
-
return
|
362
|
+
return new DecoratedProcedure({
|
363
363
|
middlewares: this["~orpc"].middlewares,
|
364
364
|
contract: this["~orpc"].contract,
|
365
365
|
handler
|
366
|
-
})
|
366
|
+
});
|
367
367
|
}
|
368
368
|
};
|
369
369
|
|
@@ -392,7 +392,8 @@ var Builder = class _Builder {
|
|
392
392
|
contract: new ContractProcedure({
|
393
393
|
route,
|
394
394
|
InputSchema: void 0,
|
395
|
-
OutputSchema: void 0
|
395
|
+
OutputSchema: void 0,
|
396
|
+
errorMap: void 0
|
396
397
|
})
|
397
398
|
});
|
398
399
|
}
|
@@ -402,7 +403,8 @@ var Builder = class _Builder {
|
|
402
403
|
contract: new ContractProcedure({
|
403
404
|
OutputSchema: void 0,
|
404
405
|
InputSchema: schema,
|
405
|
-
inputExample: example
|
406
|
+
inputExample: example,
|
407
|
+
errorMap: void 0
|
406
408
|
})
|
407
409
|
});
|
408
410
|
}
|
@@ -412,19 +414,31 @@ var Builder = class _Builder {
|
|
412
414
|
contract: new ContractProcedure({
|
413
415
|
InputSchema: void 0,
|
414
416
|
OutputSchema: schema,
|
415
|
-
outputExample: example
|
417
|
+
outputExample: example,
|
418
|
+
errorMap: void 0
|
419
|
+
})
|
420
|
+
});
|
421
|
+
}
|
422
|
+
errors(errors) {
|
423
|
+
return new ProcedureBuilder({
|
424
|
+
middlewares: this["~orpc"].middlewares,
|
425
|
+
contract: new ContractProcedure({
|
426
|
+
InputSchema: void 0,
|
427
|
+
OutputSchema: void 0,
|
428
|
+
errorMap: errors
|
416
429
|
})
|
417
430
|
});
|
418
431
|
}
|
419
432
|
handler(handler) {
|
420
|
-
return
|
433
|
+
return new DecoratedProcedure({
|
421
434
|
middlewares: this["~orpc"].middlewares,
|
422
435
|
contract: new ContractProcedure({
|
423
436
|
InputSchema: void 0,
|
424
|
-
OutputSchema: void 0
|
437
|
+
OutputSchema: void 0,
|
438
|
+
errorMap: void 0
|
425
439
|
}),
|
426
440
|
handler
|
427
|
-
})
|
441
|
+
});
|
428
442
|
}
|
429
443
|
prefix(prefix) {
|
430
444
|
return new RouterBuilder({
|
@@ -449,36 +463,47 @@ var Builder = class _Builder {
|
|
449
463
|
}
|
450
464
|
};
|
451
465
|
|
466
|
+
// src/procedure-utils.ts
|
467
|
+
function call(procedure, input, ...rest) {
|
468
|
+
return createProcedureClient(procedure, ...rest)(input);
|
469
|
+
}
|
470
|
+
|
471
|
+
// src/lazy-utils.ts
|
472
|
+
function createLazyProcedureFormAnyLazy(lazied) {
|
473
|
+
const lazyProcedure = lazy(async () => {
|
474
|
+
const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
|
475
|
+
if (!isProcedure(maybeProcedure)) {
|
476
|
+
throw new Error(`
|
477
|
+
Expected a lazy<procedure> but got lazy<unknown>.
|
478
|
+
This should be caught by TypeScript compilation.
|
479
|
+
Please report this issue if this makes you feel uncomfortable.
|
480
|
+
`);
|
481
|
+
}
|
482
|
+
return { default: maybeProcedure };
|
483
|
+
});
|
484
|
+
return lazyProcedure;
|
485
|
+
}
|
486
|
+
|
452
487
|
// src/router-client.ts
|
453
|
-
function createRouterClient(
|
454
|
-
if (isProcedure(
|
455
|
-
const caller = createProcedureClient(
|
456
|
-
...options,
|
457
|
-
procedure: options.router,
|
458
|
-
context: options.context,
|
459
|
-
path: options.path
|
460
|
-
});
|
488
|
+
function createRouterClient(router, ...rest) {
|
489
|
+
if (isProcedure(router)) {
|
490
|
+
const caller = createProcedureClient(router, ...rest);
|
461
491
|
return caller;
|
462
492
|
}
|
463
|
-
const procedureCaller = isLazy(
|
464
|
-
...options,
|
465
|
-
procedure: createLazyProcedureFormAnyLazy(options.router),
|
466
|
-
context: options.context,
|
467
|
-
path: options.path
|
468
|
-
}) : {};
|
493
|
+
const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
|
469
494
|
const recursive = new Proxy(procedureCaller, {
|
470
495
|
get(target, key) {
|
471
496
|
if (typeof key !== "string") {
|
472
497
|
return Reflect.get(target, key);
|
473
498
|
}
|
474
|
-
const next = getRouterChild(
|
499
|
+
const next = getRouterChild(router, key);
|
475
500
|
if (!next) {
|
476
501
|
return Reflect.get(target, key);
|
477
502
|
}
|
478
|
-
|
503
|
+
const [options] = rest;
|
504
|
+
return createRouterClient(next, {
|
479
505
|
...options,
|
480
|
-
|
481
|
-
path: [...options.path ?? [], key]
|
506
|
+
path: [...options?.path ?? [], key]
|
482
507
|
});
|
483
508
|
}
|
484
509
|
});
|
@@ -486,35 +511,39 @@ function createRouterClient(options) {
|
|
486
511
|
}
|
487
512
|
|
488
513
|
// src/index.ts
|
489
|
-
import { configGlobal, fallbackToGlobalConfig } from "@orpc/contract";
|
490
|
-
export * from "@orpc/shared/error";
|
514
|
+
import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe } from "@orpc/contract";
|
491
515
|
var os = new Builder({});
|
492
516
|
export {
|
493
517
|
Builder,
|
518
|
+
DecoratedProcedure,
|
494
519
|
LAZY_LOADER_SYMBOL,
|
520
|
+
ORPCError,
|
495
521
|
Procedure,
|
496
522
|
ProcedureBuilder,
|
497
523
|
ProcedureImplementer,
|
498
524
|
RouterBuilder,
|
499
525
|
RouterImplementer,
|
526
|
+
call,
|
500
527
|
configGlobal,
|
501
528
|
createChainableImplementer,
|
529
|
+
createORPCErrorConstructorMap,
|
502
530
|
createProcedureClient,
|
503
531
|
createRouterClient,
|
504
532
|
decorateLazy,
|
505
533
|
decorateMiddleware,
|
506
|
-
decorateProcedure,
|
507
534
|
deepSetLazyRouterPrefix,
|
508
535
|
fallbackToGlobalConfig,
|
509
536
|
flatLazy,
|
510
537
|
getLazyRouterPrefix,
|
511
538
|
getRouterChild,
|
512
539
|
getRouterContract,
|
540
|
+
isDefinedError,
|
513
541
|
isLazy,
|
514
542
|
isProcedure,
|
515
543
|
lazy,
|
516
544
|
mergeContext,
|
517
545
|
os,
|
546
|
+
safe,
|
518
547
|
setRouterContract,
|
519
548
|
unlazy
|
520
549
|
};
|
package/dist/next.js
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
import "./chunk-WUOGVGWG.js";
|
2
|
+
import {
|
3
|
+
ORPCHandler,
|
4
|
+
ORPCPayloadCodec,
|
5
|
+
ORPCProcedureMatcher,
|
6
|
+
super_json_exports
|
7
|
+
} from "./chunk-5IM453DN.js";
|
8
|
+
import "./chunk-ZBJYYEII.js";
|
9
|
+
|
10
|
+
// src/adapters/next/serve.ts
|
11
|
+
import { value } from "@orpc/shared";
|
12
|
+
function serve(handler, ...[options]) {
|
13
|
+
const main = async (req) => {
|
14
|
+
const context = await value(options?.context, req);
|
15
|
+
const { matched, response } = await handler.handle(req, { ...options, context });
|
16
|
+
if (matched) {
|
17
|
+
return response;
|
18
|
+
}
|
19
|
+
return new Response(`Cannot find a matching procedure for ${req.url}`, { status: 404 });
|
20
|
+
};
|
21
|
+
return {
|
22
|
+
GET: main,
|
23
|
+
POST: main,
|
24
|
+
PUT: main,
|
25
|
+
PATCH: main,
|
26
|
+
DELETE: main
|
27
|
+
};
|
28
|
+
}
|
29
|
+
export {
|
30
|
+
ORPCHandler,
|
31
|
+
ORPCPayloadCodec,
|
32
|
+
ORPCProcedureMatcher,
|
33
|
+
super_json_exports as SuperJSON,
|
34
|
+
serve
|
35
|
+
};
|
36
|
+
//# sourceMappingURL=next.js.map
|
package/dist/node.js
CHANGED
@@ -1,45 +1,87 @@
|
|
1
1
|
import {
|
2
2
|
ORPCHandler
|
3
|
-
} from "./chunk-
|
4
|
-
import "./chunk-
|
3
|
+
} from "./chunk-5IM453DN.js";
|
4
|
+
import "./chunk-ZBJYYEII.js";
|
5
5
|
|
6
|
-
// src/adapters/node/
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
// src/adapters/node/request-listener.ts
|
7
|
+
function createRequest(req, res) {
|
8
|
+
const controller = new AbortController();
|
9
|
+
res.on("close", () => {
|
10
|
+
controller.abort();
|
11
|
+
});
|
12
|
+
const method = req.method ?? "GET";
|
13
|
+
const headers = createHeaders(req);
|
14
|
+
const protocol = "encrypted" in req.socket && req.socket.encrypted ? "https:" : "http:";
|
15
|
+
const host = headers.get("Host") ?? "localhost";
|
16
|
+
const url = new URL(req.originalUrl ?? req.url ?? "/", `${protocol}//${host}`);
|
17
|
+
const init = { method, headers, signal: controller.signal };
|
18
|
+
if (method !== "GET" && method !== "HEAD") {
|
19
|
+
init.body = new ReadableStream({
|
20
|
+
start(controller2) {
|
21
|
+
req.on("data", (chunk) => {
|
22
|
+
controller2.enqueue(new Uint8Array(chunk.buffer, chunk.byteOffset, chunk.byteLength));
|
23
|
+
});
|
24
|
+
req.on("end", () => {
|
25
|
+
controller2.close();
|
26
|
+
});
|
27
|
+
}
|
28
|
+
});
|
29
|
+
init.duplex = "half";
|
30
|
+
}
|
31
|
+
return new Request(url, init);
|
32
|
+
}
|
33
|
+
function createHeaders(req) {
|
34
|
+
const headers = new Headers();
|
35
|
+
const rawHeaders = req.rawHeaders;
|
36
|
+
for (let i = 0; i < rawHeaders.length; i += 2) {
|
37
|
+
headers.append(rawHeaders[i], rawHeaders[i + 1]);
|
10
38
|
}
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
39
|
+
return headers;
|
40
|
+
}
|
41
|
+
async function sendResponse(res, response) {
|
42
|
+
const headers = {};
|
43
|
+
for (const [key, value] of response.headers) {
|
44
|
+
if (key in headers) {
|
45
|
+
if (Array.isArray(headers[key])) {
|
46
|
+
headers[key].push(value);
|
47
|
+
} else {
|
48
|
+
headers[key] = [headers[key], value];
|
15
49
|
}
|
50
|
+
} else {
|
51
|
+
headers[key] = value;
|
16
52
|
}
|
17
|
-
res.statusCode = 404;
|
18
|
-
res.end("None of the handlers can handle the request.");
|
19
53
|
}
|
20
|
-
|
54
|
+
res.writeHead(response.status, headers);
|
55
|
+
if (response.body != null && res.req.method !== "HEAD") {
|
56
|
+
for await (const chunk of response.body) {
|
57
|
+
res.write(chunk);
|
58
|
+
}
|
59
|
+
}
|
60
|
+
res.end();
|
61
|
+
}
|
21
62
|
|
22
63
|
// src/adapters/node/orpc-handler.ts
|
23
|
-
import { createRequest, sendResponse } from "@mjackson/node-fetch-server";
|
24
|
-
import { ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE } from "@orpc/shared";
|
25
64
|
var ORPCHandler2 = class {
|
26
65
|
orpcFetchHandler;
|
27
66
|
constructor(router, options) {
|
28
67
|
this.orpcFetchHandler = new ORPCHandler(router, options);
|
29
68
|
}
|
30
|
-
condition(request) {
|
31
|
-
return Boolean(request.headers[ORPC_HANDLER_HEADER]?.includes(ORPC_HANDLER_VALUE));
|
32
|
-
}
|
33
69
|
async handle(req, res, ...[options]) {
|
34
|
-
const request = createRequest(req, res
|
70
|
+
const request = createRequest(req, res);
|
35
71
|
const castedOptions = options ?? {};
|
36
|
-
const
|
37
|
-
|
38
|
-
|
72
|
+
const result = await this.orpcFetchHandler.handle(request, castedOptions);
|
73
|
+
if (result.matched === false) {
|
74
|
+
return { matched: false };
|
75
|
+
}
|
76
|
+
await options?.beforeSend?.(result.response, castedOptions.context);
|
77
|
+
await sendResponse(res, result.response);
|
78
|
+
return { matched: true };
|
39
79
|
}
|
40
80
|
};
|
41
81
|
export {
|
42
|
-
|
43
|
-
|
82
|
+
ORPCHandler2 as ORPCHandler,
|
83
|
+
createHeaders,
|
84
|
+
createRequest,
|
85
|
+
sendResponse
|
44
86
|
};
|
45
87
|
//# sourceMappingURL=node.js.map
|
@@ -1,20 +1,20 @@
|
|
1
1
|
import type { Hooks } from '@orpc/shared';
|
2
2
|
import type { Router } from '../../router';
|
3
|
-
import type { Context
|
4
|
-
import type {
|
3
|
+
import type { Context } from '../../types';
|
4
|
+
import type { FetchHandler, FetchHandleRest, FetchHandleResult } from './types';
|
5
5
|
import { type PublicORPCPayloadCodec } from './orpc-payload-codec';
|
6
6
|
import { type PublicORPCProcedureMatcher } from './orpc-procedure-matcher';
|
7
|
-
export type ORPCHandlerOptions<T extends Context> = Hooks<Request,
|
7
|
+
export type ORPCHandlerOptions<T extends Context> = Hooks<Request, FetchHandleResult, T, {
|
8
|
+
signal?: AbortSignal;
|
9
|
+
}> & {
|
8
10
|
procedureMatcher?: PublicORPCProcedureMatcher;
|
9
11
|
payloadCodec?: PublicORPCPayloadCodec;
|
10
12
|
};
|
11
|
-
export declare class ORPCHandler<T extends Context> implements
|
12
|
-
readonly
|
13
|
-
readonly options?: NoInfer<ORPCHandlerOptions<T>> | undefined;
|
13
|
+
export declare class ORPCHandler<T extends Context> implements FetchHandler<T> {
|
14
|
+
private readonly options?;
|
14
15
|
private readonly procedureMatcher;
|
15
16
|
private readonly payloadCodec;
|
16
17
|
constructor(router: Router<T, any>, options?: NoInfer<ORPCHandlerOptions<T>> | undefined);
|
17
|
-
|
18
|
-
fetch(request: Request, ...[options]: [options: FetchOptions<T>] | (undefined extends T ? [] : never)): Promise<Response>;
|
18
|
+
handle(request: Request, ...[options]: FetchHandleRest<T>): Promise<FetchHandleResult>;
|
19
19
|
}
|
20
20
|
//# sourceMappingURL=orpc-handler.d.ts.map
|
@@ -1,16 +1,21 @@
|
|
1
1
|
import type { HTTPPath } from '@orpc/contract';
|
2
|
-
import type { Context
|
3
|
-
export type
|
2
|
+
import type { Context } from '../../types';
|
3
|
+
export type FetchHandleOptions<T extends Context> = {
|
4
4
|
prefix?: HTTPPath;
|
5
5
|
} & (undefined extends T ? {
|
6
6
|
context?: T;
|
7
7
|
} : {
|
8
8
|
context: T;
|
9
9
|
});
|
10
|
+
export type FetchHandleRest<T extends Context> = [options: FetchHandleOptions<T>] | (undefined extends T ? [] : never);
|
11
|
+
export type FetchHandleResult = {
|
12
|
+
matched: true;
|
13
|
+
response: Response;
|
14
|
+
} | {
|
15
|
+
matched: false;
|
16
|
+
response: undefined;
|
17
|
+
};
|
10
18
|
export interface FetchHandler<T extends Context> {
|
11
|
-
|
12
|
-
}
|
13
|
-
export interface ConditionalFetchHandler<T extends Context> extends FetchHandler<T> {
|
14
|
-
condition: (request: Request) => boolean;
|
19
|
+
handle: (request: Request, ...rest: FetchHandleRest<T>) => Promise<FetchHandleResult>;
|
15
20
|
}
|
16
21
|
//# sourceMappingURL=types.d.ts.map
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { Context as HonoContext, MiddlewareHandler } from 'hono';
|
2
|
+
import type { Context } from '../../types';
|
3
|
+
import type { FetchHandleOptions, FetchHandler } from '../fetch';
|
4
|
+
import { type Value } from '@orpc/shared';
|
5
|
+
export type CreateMiddlewareOptions<T extends Context> = Omit<FetchHandleOptions<T>, 'context'> & (undefined extends T ? {
|
6
|
+
context?: Value<T, [HonoContext]>;
|
7
|
+
} : {
|
8
|
+
context: Value<T, [HonoContext]>;
|
9
|
+
});
|
10
|
+
export type CreateMiddlewareRest<T extends Context> = [options: CreateMiddlewareOptions<T>] | (undefined extends T ? [] : never);
|
11
|
+
export declare function createMiddleware<T extends Context>(handler: FetchHandler<T>, ...[options]: CreateMiddlewareRest<T>): MiddlewareHandler;
|
12
|
+
//# sourceMappingURL=middleware.d.ts.map
|