@orpc/server 0.0.0-next.c29cb6d → 0.0.0-next.c6c659d
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-MB74GD7C.js +301 -0
- package/dist/chunk-WUOGVGWG.js +1 -0
- package/dist/{chunk-YAVPFNPF.js → chunk-ZBJYYEII.js} +78 -33
- package/dist/fetch.js +10 -281
- package/dist/hono.js +30 -0
- package/dist/index.js +143 -111
- package/dist/next.js +36 -0
- package/dist/node.js +87 -0
- package/dist/src/{fetch → adapters/fetch}/index.d.ts +1 -1
- package/dist/src/adapters/fetch/orpc-handler.d.ts +20 -0
- package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +16 -0
- package/dist/src/{fetch → adapters/fetch}/orpc-procedure-matcher.d.ts +2 -2
- package/dist/src/adapters/fetch/types.d.ts +21 -0
- 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 +5 -0
- package/dist/src/adapters/node/orpc-handler.d.ts +12 -0
- package/dist/src/adapters/node/request-listener.d.ts +28 -0
- package/dist/src/adapters/node/types.d.ts +22 -0
- 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 -1
- 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 +9 -21
- 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 +24 -8
- package/dist/src/fetch/composite-handler.d.ts +0 -8
- package/dist/src/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/fetch/orpc-payload-codec.d.ts +0 -9
- package/dist/src/fetch/types.d.ts +0 -16
- /package/dist/src/{fetch → adapters/fetch}/super-json.d.ts +0 -0
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 {
|
@@ -110,12 +125,12 @@ var ProcedureImplementer = class _ProcedureImplementer {
|
|
110
125
|
middlewares: [...this["~orpc"].middlewares ?? [], mappedMiddleware]
|
111
126
|
});
|
112
127
|
}
|
113
|
-
|
114
|
-
return
|
128
|
+
handler(handler) {
|
129
|
+
return new DecoratedProcedure({
|
115
130
|
middlewares: this["~orpc"].middlewares,
|
116
131
|
contract: this["~orpc"].contract,
|
117
|
-
|
118
|
-
})
|
132
|
+
handler
|
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({
|
@@ -358,12 +358,12 @@ var ProcedureBuilder = class _ProcedureBuilder {
|
|
358
358
|
middlewares: this["~orpc"].middlewares
|
359
359
|
}).use(middleware, mapInput);
|
360
360
|
}
|
361
|
-
|
362
|
-
return
|
361
|
+
handler(handler) {
|
362
|
+
return new DecoratedProcedure({
|
363
363
|
middlewares: this["~orpc"].middlewares,
|
364
364
|
contract: this["~orpc"].contract,
|
365
|
-
|
366
|
-
})
|
365
|
+
handler
|
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
|
416
419
|
})
|
417
420
|
});
|
418
421
|
}
|
419
|
-
|
420
|
-
return
|
422
|
+
errors(errors) {
|
423
|
+
return new ProcedureBuilder({
|
421
424
|
middlewares: this["~orpc"].middlewares,
|
422
425
|
contract: new ContractProcedure({
|
423
426
|
InputSchema: void 0,
|
424
|
-
OutputSchema: void 0
|
427
|
+
OutputSchema: void 0,
|
428
|
+
errorMap: errors
|
429
|
+
})
|
430
|
+
});
|
431
|
+
}
|
432
|
+
handler(handler) {
|
433
|
+
return new DecoratedProcedure({
|
434
|
+
middlewares: this["~orpc"].middlewares,
|
435
|
+
contract: new ContractProcedure({
|
436
|
+
InputSchema: void 0,
|
437
|
+
OutputSchema: void 0,
|
438
|
+
errorMap: void 0
|
425
439
|
}),
|
426
|
-
|
427
|
-
})
|
440
|
+
handler
|
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,32 +511,39 @@ function createRouterClient(options) {
|
|
486
511
|
}
|
487
512
|
|
488
513
|
// src/index.ts
|
489
|
-
|
514
|
+
import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe } from "@orpc/contract";
|
490
515
|
var os = new Builder({});
|
491
516
|
export {
|
492
517
|
Builder,
|
518
|
+
DecoratedProcedure,
|
493
519
|
LAZY_LOADER_SYMBOL,
|
520
|
+
ORPCError,
|
494
521
|
Procedure,
|
495
522
|
ProcedureBuilder,
|
496
523
|
ProcedureImplementer,
|
497
524
|
RouterBuilder,
|
498
525
|
RouterImplementer,
|
526
|
+
call,
|
527
|
+
configGlobal,
|
499
528
|
createChainableImplementer,
|
529
|
+
createORPCErrorConstructorMap,
|
500
530
|
createProcedureClient,
|
501
531
|
createRouterClient,
|
502
532
|
decorateLazy,
|
503
533
|
decorateMiddleware,
|
504
|
-
decorateProcedure,
|
505
534
|
deepSetLazyRouterPrefix,
|
535
|
+
fallbackToGlobalConfig,
|
506
536
|
flatLazy,
|
507
537
|
getLazyRouterPrefix,
|
508
538
|
getRouterChild,
|
509
539
|
getRouterContract,
|
540
|
+
isDefinedError,
|
510
541
|
isLazy,
|
511
542
|
isProcedure,
|
512
543
|
lazy,
|
513
544
|
mergeContext,
|
514
545
|
os,
|
546
|
+
safe,
|
515
547
|
setRouterContract,
|
516
548
|
unlazy
|
517
549
|
};
|
package/dist/next.js
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
import "./chunk-WUOGVGWG.js";
|
2
|
+
import {
|
3
|
+
ORPCPayloadCodec,
|
4
|
+
ORPCProcedureMatcher,
|
5
|
+
RPCHandler,
|
6
|
+
super_json_exports
|
7
|
+
} from "./chunk-MB74GD7C.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
|
+
ORPCPayloadCodec,
|
31
|
+
ORPCProcedureMatcher,
|
32
|
+
RPCHandler,
|
33
|
+
super_json_exports as SuperJSON,
|
34
|
+
serve
|
35
|
+
};
|
36
|
+
//# sourceMappingURL=next.js.map
|
package/dist/node.js
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
import {
|
2
|
+
RPCHandler
|
3
|
+
} from "./chunk-MB74GD7C.js";
|
4
|
+
import "./chunk-ZBJYYEII.js";
|
5
|
+
|
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]);
|
38
|
+
}
|
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];
|
49
|
+
}
|
50
|
+
} else {
|
51
|
+
headers[key] = value;
|
52
|
+
}
|
53
|
+
}
|
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
|
+
}
|
62
|
+
|
63
|
+
// src/adapters/node/orpc-handler.ts
|
64
|
+
var RPCHandler2 = class {
|
65
|
+
orpcFetchHandler;
|
66
|
+
constructor(router, options) {
|
67
|
+
this.orpcFetchHandler = new RPCHandler(router, options);
|
68
|
+
}
|
69
|
+
async handle(req, res, ...[options]) {
|
70
|
+
const request = createRequest(req, res);
|
71
|
+
const castedOptions = options ?? {};
|
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 };
|
79
|
+
}
|
80
|
+
};
|
81
|
+
export {
|
82
|
+
RPCHandler2 as RPCHandler,
|
83
|
+
createHeaders,
|
84
|
+
createRequest,
|
85
|
+
sendResponse
|
86
|
+
};
|
87
|
+
//# sourceMappingURL=node.js.map
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import type { Hooks } from '@orpc/shared';
|
2
|
+
import type { Router } from '../../router';
|
3
|
+
import type { Context } from '../../types';
|
4
|
+
import type { FetchHandler, FetchHandleRest, FetchHandleResult } from './types';
|
5
|
+
import { type PublicORPCPayloadCodec } from './orpc-payload-codec';
|
6
|
+
import { type PublicORPCProcedureMatcher } from './orpc-procedure-matcher';
|
7
|
+
export type RPCHandlerOptions<T extends Context> = Hooks<Request, FetchHandleResult, T, {
|
8
|
+
signal?: AbortSignal;
|
9
|
+
}> & {
|
10
|
+
procedureMatcher?: PublicORPCProcedureMatcher;
|
11
|
+
payloadCodec?: PublicORPCPayloadCodec;
|
12
|
+
};
|
13
|
+
export declare class RPCHandler<T extends Context> implements FetchHandler<T> {
|
14
|
+
private readonly options?;
|
15
|
+
private readonly procedureMatcher;
|
16
|
+
private readonly payloadCodec;
|
17
|
+
constructor(router: Router<T, any>, options?: NoInfer<RPCHandlerOptions<T>> | undefined);
|
18
|
+
handle(request: Request, ...[options]: FetchHandleRest<T>): Promise<FetchHandleResult>;
|
19
|
+
}
|
20
|
+
//# sourceMappingURL=orpc-handler.d.ts.map
|
@@ -0,0 +1,16 @@
|
|
1
|
+
import { type HTTPMethod } from '@orpc/contract';
|
2
|
+
export declare class ORPCPayloadCodec {
|
3
|
+
/**
|
4
|
+
* If method is GET, the payload will be encoded as query string.
|
5
|
+
* If method is GET and payload contain file, the method will be fallback to fallbackMethod. (fallbackMethod = GET will force to use GET method)
|
6
|
+
*/
|
7
|
+
encode(payload: unknown, method?: HTTPMethod, fallbackMethod?: HTTPMethod): {
|
8
|
+
query?: URLSearchParams;
|
9
|
+
body?: FormData | string;
|
10
|
+
headers?: Headers;
|
11
|
+
method: HTTPMethod;
|
12
|
+
};
|
13
|
+
decode(re: Request | Response): Promise<unknown>;
|
14
|
+
}
|
15
|
+
export type PublicORPCPayloadCodec = Pick<ORPCPayloadCodec, keyof ORPCPayloadCodec>;
|
16
|
+
//# sourceMappingURL=orpc-payload-codec.d.ts.map
|
@@ -1,5 +1,5 @@
|
|
1
|
-
import type { ANY_PROCEDURE } from '
|
2
|
-
import { type ANY_ROUTER } from '
|
1
|
+
import type { ANY_PROCEDURE } from '../../procedure';
|
2
|
+
import { type ANY_ROUTER } from '../../router';
|
3
3
|
export declare class ORPCProcedureMatcher {
|
4
4
|
private readonly router;
|
5
5
|
constructor(router: ANY_ROUTER);
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import type { HTTPPath } from '@orpc/contract';
|
2
|
+
import type { Context } from '../../types';
|
3
|
+
export type FetchHandleOptions<T extends Context> = {
|
4
|
+
prefix?: HTTPPath;
|
5
|
+
} & (undefined extends T ? {
|
6
|
+
context?: T;
|
7
|
+
} : {
|
8
|
+
context: T;
|
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
|
+
};
|
18
|
+
export interface FetchHandler<T extends Context> {
|
19
|
+
handle: (request: Request, ...rest: FetchHandleRest<T>) => Promise<FetchHandleResult>;
|
20
|
+
}
|
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
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import type { NextRequest } from 'next/server';
|
2
|
+
import type { Context } from '../../types';
|
3
|
+
import type { FetchHandleOptions, FetchHandler } from '../fetch';
|
4
|
+
import { type Value } from '@orpc/shared';
|
5
|
+
export type ServeOptions<T extends Context> = Omit<FetchHandleOptions<T>, 'context'> & (undefined extends T ? {
|
6
|
+
context?: Value<T, [NextRequest]>;
|
7
|
+
} : {
|
8
|
+
context: Value<T, [NextRequest]>;
|
9
|
+
});
|
10
|
+
export type ServeRest<T extends Context> = [options: ServeOptions<T>] | (undefined extends T ? [] : never);
|
11
|
+
export interface ServeResult {
|
12
|
+
GET: (req: NextRequest) => Promise<Response>;
|
13
|
+
POST: (req: NextRequest) => Promise<Response>;
|
14
|
+
PUT: (req: NextRequest) => Promise<Response>;
|
15
|
+
PATCH: (req: NextRequest) => Promise<Response>;
|
16
|
+
DELETE: (req: NextRequest) => Promise<Response>;
|
17
|
+
}
|
18
|
+
export declare function serve<T extends Context>(handler: FetchHandler<T>, ...[options]: ServeRest<T>): ServeResult;
|
19
|
+
//# sourceMappingURL=serve.d.ts.map
|