@orpc/server 0.0.0-next.3f6c426 → 0.0.0-next.4220427

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.
Files changed (40) hide show
  1. package/dist/{chunk-6A7XHEBH.js → chunk-DNG2IB3R.js} +66 -28
  2. package/dist/{chunk-B2EZJB7X.js → chunk-V3I7RIRY.js} +15 -16
  3. package/dist/chunk-WUOGVGWG.js +1 -0
  4. package/dist/fetch.js +3 -20
  5. package/dist/hono.js +30 -0
  6. package/dist/index.js +36 -12
  7. package/dist/next.js +36 -0
  8. package/dist/node.js +66 -24
  9. package/dist/src/adapters/fetch/index.d.ts +0 -1
  10. package/dist/src/adapters/fetch/orpc-handler.d.ts +8 -8
  11. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +1 -1
  12. package/dist/src/adapters/fetch/types.d.ts +11 -6
  13. package/dist/src/adapters/hono/index.d.ts +3 -0
  14. package/dist/src/adapters/hono/middleware.d.ts +12 -0
  15. package/dist/src/adapters/next/index.d.ts +3 -0
  16. package/dist/src/adapters/next/serve.d.ts +19 -0
  17. package/dist/src/adapters/node/index.d.ts +1 -1
  18. package/dist/src/adapters/node/orpc-handler.d.ts +5 -5
  19. package/dist/src/adapters/node/request-listener.d.ts +28 -0
  20. package/dist/src/adapters/node/types.d.ts +10 -9
  21. package/dist/src/builder.d.ts +10 -9
  22. package/dist/src/error.d.ts +10 -0
  23. package/dist/src/implementer-chainable.d.ts +2 -2
  24. package/dist/src/index.d.ts +2 -2
  25. package/dist/src/lazy-decorated.d.ts +2 -2
  26. package/dist/src/middleware-decorated.d.ts +6 -5
  27. package/dist/src/middleware.d.ts +20 -8
  28. package/dist/src/procedure-builder.d.ts +16 -14
  29. package/dist/src/procedure-client.d.ts +9 -9
  30. package/dist/src/procedure-decorated.d.ts +10 -9
  31. package/dist/src/procedure-implementer.d.ts +12 -11
  32. package/dist/src/procedure.d.ts +33 -13
  33. package/dist/src/router-builder.d.ts +3 -3
  34. package/dist/src/router-client.d.ts +2 -2
  35. package/dist/src/router-implementer.d.ts +2 -2
  36. package/dist/src/router.d.ts +3 -3
  37. package/dist/src/types.d.ts +2 -0
  38. package/package.json +18 -8
  39. package/dist/src/adapters/fetch/composite-handler.d.ts +0 -8
  40. package/dist/src/adapters/node/composite-handler.d.ts +0 -9
@@ -32,6 +32,33 @@ function isProcedure(item) {
32
32
  return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "Procedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "contract" in item["~orpc"] && isContractProcedure(item["~orpc"].contract) && "handler" in item["~orpc"] && typeof item["~orpc"].handler === "function";
33
33
  }
34
34
 
35
+ // src/error.ts
36
+ import { ORPCError } from "@orpc/contract";
37
+ function createORPCErrorConstructorMap(errors) {
38
+ const constructors = {};
39
+ if (!errors) {
40
+ return constructors;
41
+ }
42
+ for (const code in errors) {
43
+ const config = errors[code];
44
+ if (!config) {
45
+ continue;
46
+ }
47
+ const constructor = (...[options]) => {
48
+ return new ORPCError({
49
+ code,
50
+ defined: true,
51
+ status: config.status,
52
+ message: options?.message ?? config.message,
53
+ data: options?.data,
54
+ cause: options?.cause
55
+ });
56
+ };
57
+ constructors[code] = constructor;
58
+ }
59
+ return constructors;
60
+ }
61
+
35
62
  // src/lazy.ts
36
63
  var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
37
64
  function lazy(loader) {
@@ -60,8 +87,8 @@ function flatLazy(lazied) {
60
87
  }
61
88
 
62
89
  // src/procedure-client.ts
63
- import { executeWithHooks, value } from "@orpc/shared";
64
- import { ORPCError } from "@orpc/shared/error";
90
+ import { ORPCError as ORPCError2, validateORPCError, ValidationError } from "@orpc/contract";
91
+ import { executeWithHooks, toError, value } from "@orpc/shared";
65
92
  function createProcedureClient(options) {
66
93
  return async (...[input, callerOptions]) => {
67
94
  const path = options.path ?? [];
@@ -74,21 +101,32 @@ function createProcedureClient(options) {
74
101
  };
75
102
  const executeWithValidation = async () => {
76
103
  const validInput = await validateInput(procedure, input);
77
- const output = await executeMiddlewareChain(
78
- procedure,
79
- validInput,
104
+ const output = await executeMiddlewareChain({
80
105
  context,
81
- meta
82
- );
106
+ input: validInput,
107
+ path,
108
+ procedure,
109
+ signal: callerOptions?.signal,
110
+ errors: createORPCErrorConstructorMap(procedure["~orpc"].contract["~orpc"].errorMap)
111
+ });
83
112
  return validateOutput(procedure, output);
84
113
  };
85
- return executeWithHooks({
86
- hooks: options,
87
- input,
88
- context,
89
- meta,
90
- execute: executeWithValidation
91
- });
114
+ try {
115
+ const output = await executeWithHooks({
116
+ hooks: options,
117
+ input,
118
+ context,
119
+ meta,
120
+ execute: executeWithValidation
121
+ });
122
+ return output;
123
+ } catch (e) {
124
+ if (!(e instanceof ORPCError2)) {
125
+ throw toError(e);
126
+ }
127
+ const validated = await validateORPCError(procedure["~orpc"].contract["~orpc"].errorMap, e);
128
+ throw validated;
129
+ }
92
130
  };
93
131
  }
94
132
  async function validateInput(procedure, input) {
@@ -97,10 +135,13 @@ async function validateInput(procedure, input) {
97
135
  return input;
98
136
  const result = await schema["~standard"].validate(input);
99
137
  if (result.issues) {
100
- throw new ORPCError({
138
+ throw new ORPCError2({
101
139
  message: "Input validation failed",
102
140
  code: "BAD_REQUEST",
103
- issues: result.issues
141
+ data: {
142
+ issues: result.issues
143
+ },
144
+ cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
104
145
  });
105
146
  }
106
147
  return result.value;
@@ -111,31 +152,27 @@ async function validateOutput(procedure, output) {
111
152
  return output;
112
153
  const result = await schema["~standard"].validate(output);
113
154
  if (result.issues) {
114
- throw new ORPCError({
155
+ throw new ORPCError2({
115
156
  message: "Output validation failed",
116
157
  code: "INTERNAL_SERVER_ERROR",
117
- issues: result.issues
158
+ cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
118
159
  });
119
160
  }
120
161
  return result.value;
121
162
  }
122
- async function executeMiddlewareChain(procedure, input, context, meta) {
123
- const middlewares = procedure["~orpc"].middlewares ?? [];
163
+ async function executeMiddlewareChain(opt) {
164
+ const middlewares = opt.procedure["~orpc"].middlewares ?? [];
124
165
  let currentMidIndex = 0;
125
- let currentContext = context;
166
+ let currentContext = opt.context;
126
167
  const next = async (nextOptions) => {
127
168
  const mid = middlewares[currentMidIndex];
128
169
  currentMidIndex += 1;
129
170
  currentContext = mergeContext(currentContext, nextOptions.context);
130
171
  if (mid) {
131
- return await mid(input, currentContext, {
132
- ...meta,
133
- next,
134
- output: (output) => ({ output, context: void 0 })
135
- });
172
+ return await mid({ ...opt, context: currentContext, next }, opt.input, (output) => ({ output, context: void 0 }));
136
173
  }
137
174
  const result = {
138
- output: await procedure["~orpc"].handler(input, currentContext, meta),
175
+ output: await opt.procedure["~orpc"].handler({ ...opt, context: currentContext }),
139
176
  context: currentContext
140
177
  };
141
178
  return result;
@@ -178,6 +215,7 @@ export {
178
215
  mergeContext,
179
216
  Procedure,
180
217
  isProcedure,
218
+ createORPCErrorConstructorMap,
181
219
  LAZY_LOADER_SYMBOL,
182
220
  lazy,
183
221
  isLazy,
@@ -186,4 +224,4 @@ export {
186
224
  createProcedureClient,
187
225
  getRouterChild
188
226
  };
189
- //# sourceMappingURL=chunk-6A7XHEBH.js.map
227
+ //# sourceMappingURL=chunk-DNG2IB3R.js.map
@@ -4,7 +4,7 @@ import {
4
4
  getRouterChild,
5
5
  isProcedure,
6
6
  unlazy
7
- } from "./chunk-6A7XHEBH.js";
7
+ } from "./chunk-DNG2IB3R.js";
8
8
 
9
9
  // src/adapters/fetch/super-json.ts
10
10
  var super_json_exports = {};
@@ -130,8 +130,8 @@ function deserialize({
130
130
  }
131
131
 
132
132
  // src/adapters/fetch/orpc-payload-codec.ts
133
+ import { ORPCError } from "@orpc/contract";
133
134
  import { findDeepMatches, set } from "@orpc/shared";
134
- import { ORPCError } from "@orpc/shared/error";
135
135
  var ORPCPayloadCodec = class {
136
136
  /**
137
137
  * If method is GET, the payload will be encoded as query string.
@@ -236,28 +236,24 @@ var ORPCProcedureMatcher = class {
236
236
  };
237
237
 
238
238
  // src/adapters/fetch/orpc-handler.ts
239
- import { executeWithHooks, ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE, trim as trim2 } from "@orpc/shared";
240
- import { ORPCError as ORPCError2 } from "@orpc/shared/error";
239
+ import { ORPCError as ORPCError2 } from "@orpc/contract";
240
+ import { executeWithHooks, trim as trim2 } from "@orpc/shared";
241
241
  var ORPCHandler = class {
242
242
  constructor(router, options) {
243
- this.router = router;
244
243
  this.options = options;
245
244
  this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
246
245
  this.payloadCodec = options?.payloadCodec ?? new ORPCPayloadCodec();
247
246
  }
248
247
  procedureMatcher;
249
248
  payloadCodec;
250
- condition(request) {
251
- return Boolean(request.headers.get(ORPC_HANDLER_HEADER)?.includes(ORPC_HANDLER_VALUE));
252
- }
253
- async fetch(request, ...[options]) {
249
+ async handle(request, ...[options]) {
254
250
  const context = options?.context;
255
251
  const execute = async () => {
256
252
  const url = new URL(request.url);
257
253
  const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
258
254
  const match = await this.procedureMatcher.match(pathname);
259
255
  if (!match) {
260
- throw new ORPCError2({ code: "NOT_FOUND", message: "Not found" });
256
+ return { matched: false, response: void 0 };
261
257
  }
262
258
  const input = await this.payloadCodec.decode(request);
263
259
  const client = createProcedureClient({
@@ -265,20 +261,22 @@ var ORPCHandler = class {
265
261
  procedure: match.procedure,
266
262
  path: match.path
267
263
  });
268
- const output = await client(input, { signal: options?.signal });
264
+ const output = await client(input, { signal: request.signal });
269
265
  const { body, headers } = this.payloadCodec.encode(output);
270
- return new Response(body, { headers });
266
+ const response = new Response(body, { headers });
267
+ return { matched: true, response };
271
268
  };
272
269
  try {
273
- return await executeWithHooks({
270
+ const result = await executeWithHooks({
274
271
  context,
275
272
  execute,
276
273
  input: request,
277
274
  hooks: this.options,
278
275
  meta: {
279
- signal: options?.signal
276
+ signal: request.signal
280
277
  }
281
278
  });
279
+ return result;
282
280
  } catch (e) {
283
281
  const error = e instanceof ORPCError2 ? e : new ORPCError2({
284
282
  code: "INTERNAL_SERVER_ERROR",
@@ -286,10 +284,11 @@ var ORPCHandler = class {
286
284
  cause: e
287
285
  });
288
286
  const { body, headers } = this.payloadCodec.encode(error.toJSON());
289
- return new Response(body, {
287
+ const response = new Response(body, {
290
288
  headers,
291
289
  status: error.status
292
290
  });
291
+ return { matched: true, response };
293
292
  }
294
293
  }
295
294
  };
@@ -300,4 +299,4 @@ export {
300
299
  ORPCProcedureMatcher,
301
300
  ORPCHandler
302
301
  };
303
- //# sourceMappingURL=chunk-B2EZJB7X.js.map
302
+ //# sourceMappingURL=chunk-V3I7RIRY.js.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-WUOGVGWG.js.map
package/dist/fetch.js CHANGED
@@ -1,29 +1,12 @@
1
+ import "./chunk-WUOGVGWG.js";
1
2
  import {
2
3
  ORPCHandler,
3
4
  ORPCPayloadCodec,
4
5
  ORPCProcedureMatcher,
5
6
  super_json_exports
6
- } from "./chunk-B2EZJB7X.js";
7
- import "./chunk-6A7XHEBH.js";
8
-
9
- // src/adapters/fetch/composite-handler.ts
10
- var CompositeHandler = class {
11
- constructor(handlers) {
12
- this.handlers = handlers;
13
- }
14
- async fetch(request, ...opt) {
15
- for (const handler of this.handlers) {
16
- if (handler.condition(request)) {
17
- return handler.fetch(request, ...opt);
18
- }
19
- }
20
- return new Response("None of the handlers can handle the request.", {
21
- status: 404
22
- });
23
- }
24
- };
7
+ } from "./chunk-V3I7RIRY.js";
8
+ import "./chunk-DNG2IB3R.js";
25
9
  export {
26
- CompositeHandler,
27
10
  ORPCHandler,
28
11
  ORPCPayloadCodec,
29
12
  ORPCProcedureMatcher,
package/dist/hono.js ADDED
@@ -0,0 +1,30 @@
1
+ import "./chunk-WUOGVGWG.js";
2
+ import {
3
+ ORPCHandler,
4
+ ORPCPayloadCodec,
5
+ ORPCProcedureMatcher,
6
+ super_json_exports
7
+ } from "./chunk-V3I7RIRY.js";
8
+ import "./chunk-DNG2IB3R.js";
9
+
10
+ // src/adapters/hono/middleware.ts
11
+ import { value } from "@orpc/shared";
12
+ function createMiddleware(handler, ...[options]) {
13
+ return async (c, next) => {
14
+ const context = await value(options?.context, c);
15
+ const { matched, response } = await handler.handle(c.req.raw, { ...options, context });
16
+ if (matched) {
17
+ c.res = response;
18
+ return;
19
+ }
20
+ await next();
21
+ };
22
+ }
23
+ export {
24
+ ORPCHandler,
25
+ ORPCPayloadCodec,
26
+ ORPCProcedureMatcher,
27
+ super_json_exports as SuperJSON,
28
+ createMiddleware
29
+ };
30
+ //# sourceMappingURL=hono.js.map
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,7 +10,7 @@ import {
9
10
  lazy,
10
11
  mergeContext,
11
12
  unlazy
12
- } from "./chunk-6A7XHEBH.js";
13
+ } from "./chunk-DNG2IB3R.js";
13
14
 
14
15
  // src/builder.ts
15
16
  import { ContractProcedure } from "@orpc/contract";
@@ -23,17 +24,17 @@ 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((input, context, meta, ...rest) => {
33
- const next = async (options) => {
34
- return mapped(input, mergeContext(context, options.context), meta, ...rest);
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(input, context, { ...meta, next }, ...rest);
37
+ const merged = middleware({ ...options, next }, input, output, ...rest);
37
38
  return merged;
38
39
  });
39
40
  return concatted;
@@ -346,6 +347,12 @@ var ProcedureBuilder = class _ProcedureBuilder {
346
347
  contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).output(schema, example)
347
348
  });
348
349
  }
350
+ errors(errors) {
351
+ return new _ProcedureBuilder({
352
+ ...this["~orpc"],
353
+ contract: DecoratedContractProcedure2.decorate(this["~orpc"].contract).errors(errors)
354
+ });
355
+ }
349
356
  use(middleware, mapInput) {
350
357
  if (!mapInput) {
351
358
  return new ProcedureImplementer({
@@ -392,7 +399,8 @@ var Builder = class _Builder {
392
399
  contract: new ContractProcedure({
393
400
  route,
394
401
  InputSchema: void 0,
395
- OutputSchema: void 0
402
+ OutputSchema: void 0,
403
+ errorMap: void 0
396
404
  })
397
405
  });
398
406
  }
@@ -402,7 +410,8 @@ var Builder = class _Builder {
402
410
  contract: new ContractProcedure({
403
411
  OutputSchema: void 0,
404
412
  InputSchema: schema,
405
- inputExample: example
413
+ inputExample: example,
414
+ errorMap: void 0
406
415
  })
407
416
  });
408
417
  }
@@ -412,7 +421,18 @@ var Builder = class _Builder {
412
421
  contract: new ContractProcedure({
413
422
  InputSchema: void 0,
414
423
  OutputSchema: schema,
415
- outputExample: example
424
+ outputExample: example,
425
+ errorMap: void 0
426
+ })
427
+ });
428
+ }
429
+ errors(errors) {
430
+ return new ProcedureBuilder({
431
+ middlewares: this["~orpc"].middlewares,
432
+ contract: new ContractProcedure({
433
+ InputSchema: void 0,
434
+ OutputSchema: void 0,
435
+ errorMap: errors
416
436
  })
417
437
  });
418
438
  }
@@ -421,7 +441,8 @@ var Builder = class _Builder {
421
441
  middlewares: this["~orpc"].middlewares,
422
442
  contract: new ContractProcedure({
423
443
  InputSchema: void 0,
424
- OutputSchema: void 0
444
+ OutputSchema: void 0,
445
+ errorMap: void 0
425
446
  }),
426
447
  handler
427
448
  }));
@@ -486,12 +507,12 @@ function createRouterClient(options) {
486
507
  }
487
508
 
488
509
  // src/index.ts
489
- import { configGlobal, fallbackToGlobalConfig } from "@orpc/contract";
490
- export * from "@orpc/shared/error";
510
+ import { configGlobal, fallbackToGlobalConfig, isDefinedError, ORPCError, safe } from "@orpc/contract";
491
511
  var os = new Builder({});
492
512
  export {
493
513
  Builder,
494
514
  LAZY_LOADER_SYMBOL,
515
+ ORPCError,
495
516
  Procedure,
496
517
  ProcedureBuilder,
497
518
  ProcedureImplementer,
@@ -499,6 +520,7 @@ export {
499
520
  RouterImplementer,
500
521
  configGlobal,
501
522
  createChainableImplementer,
523
+ createORPCErrorConstructorMap,
502
524
  createProcedureClient,
503
525
  createRouterClient,
504
526
  decorateLazy,
@@ -510,11 +532,13 @@ export {
510
532
  getLazyRouterPrefix,
511
533
  getRouterChild,
512
534
  getRouterContract,
535
+ isDefinedError,
513
536
  isLazy,
514
537
  isProcedure,
515
538
  lazy,
516
539
  mergeContext,
517
540
  os,
541
+ safe,
518
542
  setRouterContract,
519
543
  unlazy
520
544
  };
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-V3I7RIRY.js";
8
+ import "./chunk-DNG2IB3R.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-B2EZJB7X.js";
4
- import "./chunk-6A7XHEBH.js";
3
+ } from "./chunk-V3I7RIRY.js";
4
+ import "./chunk-DNG2IB3R.js";
5
5
 
6
- // src/adapters/node/composite-handler.ts
7
- var CompositeHandler = class {
8
- constructor(handlers) {
9
- this.handlers = handlers;
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
- async handle(req, res, ...opt) {
12
- for (const handler of this.handlers) {
13
- if (handler.condition(req)) {
14
- return handler.handle(req, res, ...opt);
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, options);
70
+ const request = createRequest(req, res);
35
71
  const castedOptions = options ?? {};
36
- const response = await this.orpcFetchHandler.fetch(request, castedOptions);
37
- await options?.beforeSend?.(response, castedOptions.context);
38
- return await sendResponse(res, response);
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
- CompositeHandler,
43
- ORPCHandler2 as ORPCHandler
82
+ ORPCHandler2 as ORPCHandler,
83
+ createHeaders,
84
+ createRequest,
85
+ sendResponse
44
86
  };
45
87
  //# sourceMappingURL=node.js.map
@@ -1,4 +1,3 @@
1
- export * from './composite-handler';
2
1
  export * from './orpc-handler';
3
2
  export * from './orpc-payload-codec';
4
3
  export * from './orpc-procedure-matcher';
@@ -1,20 +1,20 @@
1
1
  import type { Hooks } from '@orpc/shared';
2
2
  import type { Router } from '../../router';
3
- import type { Context, WithSignal } from '../../types';
4
- import type { ConditionalFetchHandler, FetchOptions } from './types';
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, Response, T, WithSignal> & {
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 ConditionalFetchHandler<T> {
12
- readonly router: Router<T, any>;
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
- condition(request: Request): boolean;
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,4 +1,4 @@
1
- import type { HTTPMethod } from '@orpc/contract';
1
+ import { type HTTPMethod } from '@orpc/contract';
2
2
  export declare class ORPCPayloadCodec {
3
3
  /**
4
4
  * If method is GET, the payload will be encoded as query string.
@@ -1,16 +1,21 @@
1
1
  import type { HTTPPath } from '@orpc/contract';
2
- import type { Context, WithSignal } from '../../types';
3
- export type FetchOptions<T extends Context> = WithSignal & {
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
- fetch: (request: Request, ...opt: [options: FetchOptions<T>] | (undefined extends T ? [] : never)) => Promise<Response>;
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