@orpc/server 0.0.0-next.3f6c426 → 0.0.0-next.43889a7

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 (50) hide show
  1. package/dist/{chunk-B2EZJB7X.js → chunk-26GHKV43.js} +19 -21
  2. package/dist/{chunk-6A7XHEBH.js → chunk-LDIL7OEP.js} +87 -59
  3. package/dist/chunk-WUOGVGWG.js +1 -0
  4. package/dist/fetch.js +5 -22
  5. package/dist/hono.js +30 -0
  6. package/dist/index.js +702 -172
  7. package/dist/next.js +36 -0
  8. package/dist/node.js +71 -29
  9. package/dist/src/adapters/fetch/index.d.ts +0 -1
  10. package/dist/src/adapters/fetch/orpc-handler.d.ts +9 -9
  11. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +1 -1
  12. package/dist/src/adapters/fetch/types.d.ts +12 -7
  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 +8 -8
  19. package/dist/src/adapters/node/request-listener.d.ts +28 -0
  20. package/dist/src/adapters/node/types.d.ts +11 -10
  21. package/dist/src/builder-with-errors-middlewares.d.ts +49 -0
  22. package/dist/src/builder-with-errors.d.ts +49 -0
  23. package/dist/src/builder-with-middlewares.d.ts +49 -0
  24. package/dist/src/builder.d.ts +33 -22
  25. package/dist/src/config.d.ts +6 -0
  26. package/dist/src/context.d.ts +10 -0
  27. package/dist/src/error.d.ts +10 -0
  28. package/dist/src/hidden.d.ts +2 -2
  29. package/dist/src/implementer-chainable.d.ts +11 -5
  30. package/dist/src/index.d.ts +6 -5
  31. package/dist/src/lazy-decorated.d.ts +4 -6
  32. package/dist/src/middleware-decorated.d.ts +6 -5
  33. package/dist/src/middleware.d.ts +29 -13
  34. package/dist/src/procedure-builder-with-input.d.ts +35 -0
  35. package/dist/src/procedure-builder-with-output.d.ts +34 -0
  36. package/dist/src/procedure-builder.d.ts +24 -18
  37. package/dist/src/procedure-client.d.ts +9 -22
  38. package/dist/src/procedure-decorated.d.ts +22 -11
  39. package/dist/src/procedure-implementer.d.ts +18 -13
  40. package/dist/src/procedure-utils.d.ts +17 -0
  41. package/dist/src/procedure.d.ts +37 -13
  42. package/dist/src/router-builder.d.ts +20 -16
  43. package/dist/src/router-client.d.ts +7 -6
  44. package/dist/src/router-implementer.d.ts +12 -10
  45. package/dist/src/router.d.ts +6 -6
  46. package/dist/src/types.d.ts +2 -3
  47. package/package.json +18 -8
  48. package/dist/src/adapters/fetch/composite-handler.d.ts +0 -8
  49. package/dist/src/adapters/node/composite-handler.d.ts +0 -9
  50. package/dist/src/utils.d.ts +0 -3
@@ -4,7 +4,7 @@ import {
4
4
  getRouterChild,
5
5
  isProcedure,
6
6
  unlazy
7
- } from "./chunk-6A7XHEBH.js";
7
+ } from "./chunk-LDIL7OEP.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,49 +236,46 @@ 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";
241
- var ORPCHandler = class {
239
+ import { ORPCError as ORPCError2 } from "@orpc/contract";
240
+ import { executeWithHooks, trim as trim2 } from "@orpc/shared";
241
+ var RPCHandler = 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]) {
254
- const context = options?.context;
249
+ async handle(request, ...[options]) {
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
- const client = createProcedureClient({
259
+ const client = createProcedureClient(match.procedure, {
264
260
  context,
265
- procedure: match.procedure,
266
261
  path: match.path
267
262
  });
268
- const output = await client(input, { signal: options?.signal });
263
+ const output = await client(input, { signal: request.signal });
269
264
  const { body, headers } = this.payloadCodec.encode(output);
270
- return new Response(body, { headers });
265
+ const response = new Response(body, { headers });
266
+ return { matched: true, response };
271
267
  };
272
268
  try {
273
- return await executeWithHooks({
269
+ const result = await executeWithHooks({
274
270
  context,
275
271
  execute,
276
272
  input: request,
277
273
  hooks: this.options,
278
274
  meta: {
279
- signal: options?.signal
275
+ signal: request.signal
280
276
  }
281
277
  });
278
+ return result;
282
279
  } catch (e) {
283
280
  const error = e instanceof ORPCError2 ? e : new ORPCError2({
284
281
  code: "INTERNAL_SERVER_ERROR",
@@ -286,10 +283,11 @@ var ORPCHandler = class {
286
283
  cause: e
287
284
  });
288
285
  const { body, headers } = this.payloadCodec.encode(error.toJSON());
289
- return new Response(body, {
286
+ const response = new Response(body, {
290
287
  headers,
291
288
  status: error.status
292
289
  });
290
+ return { matched: true, response };
293
291
  }
294
292
  }
295
293
  };
@@ -298,6 +296,6 @@ export {
298
296
  super_json_exports,
299
297
  ORPCPayloadCodec,
300
298
  ORPCProcedureMatcher,
301
- ORPCHandler
299
+ RPCHandler
302
300
  };
303
- //# sourceMappingURL=chunk-B2EZJB7X.js.map
301
+ //# sourceMappingURL=chunk-26GHKV43.js.map
@@ -4,18 +4,6 @@ var __export = (target, all) => {
4
4
  __defProp(target, name, { get: all[name], enumerable: true });
5
5
  };
6
6
 
7
- // src/utils.ts
8
- function mergeContext(a, b) {
9
- if (!a)
10
- return b;
11
- if (!b)
12
- return a;
13
- return {
14
- ...a,
15
- ...b
16
- };
17
- }
18
-
19
7
  // src/procedure.ts
20
8
  import { isContractProcedure } from "@orpc/contract";
21
9
  var Procedure = class {
@@ -32,6 +20,30 @@ function isProcedure(item) {
32
20
  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
21
  }
34
22
 
23
+ // src/error.ts
24
+ import { ORPCError } from "@orpc/contract";
25
+ function createORPCErrorConstructorMap(errors) {
26
+ const constructors = {};
27
+ for (const code in errors) {
28
+ const config = errors[code];
29
+ if (!config) {
30
+ continue;
31
+ }
32
+ const constructor = (...[options]) => {
33
+ return new ORPCError({
34
+ code,
35
+ defined: true,
36
+ status: config.status,
37
+ message: options?.message ?? config.message,
38
+ data: options?.data,
39
+ cause: options?.cause
40
+ });
41
+ };
42
+ constructors[code] = constructor;
43
+ }
44
+ return constructors;
45
+ }
46
+
35
47
  // src/lazy.ts
36
48
  var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
37
49
  function lazy(loader) {
@@ -59,36 +71,44 @@ function flatLazy(lazied) {
59
71
  return lazy(flattenLoader);
60
72
  }
61
73
 
74
+ // src/middleware.ts
75
+ function middlewareOutputFn(output) {
76
+ return { output, context: {} };
77
+ }
78
+
62
79
  // src/procedure-client.ts
63
- import { executeWithHooks, value } from "@orpc/shared";
64
- import { ORPCError } from "@orpc/shared/error";
65
- function createProcedureClient(options) {
80
+ import { ORPCError as ORPCError2, validateORPCError, ValidationError } from "@orpc/contract";
81
+ import { executeWithHooks, toError, value } from "@orpc/shared";
82
+ function createProcedureClient(lazyableProcedure, ...[options]) {
66
83
  return async (...[input, callerOptions]) => {
67
- const path = options.path ?? [];
68
- const { default: procedure } = await unlazy(options.procedure);
69
- const context = await value(options.context);
70
- const meta = {
84
+ const path = options?.path ?? [];
85
+ const { default: procedure } = await unlazy(lazyableProcedure);
86
+ const context = await value(options?.context ?? {}, callerOptions?.context);
87
+ const errors = createORPCErrorConstructorMap(procedure["~orpc"].contract["~orpc"].errorMap);
88
+ const executeOptions = {
89
+ input,
90
+ context,
91
+ errors,
71
92
  path,
72
93
  procedure,
73
94
  signal: callerOptions?.signal
74
95
  };
75
- const executeWithValidation = async () => {
76
- const validInput = await validateInput(procedure, input);
77
- const output = await executeMiddlewareChain(
78
- procedure,
79
- validInput,
96
+ try {
97
+ const output = await executeWithHooks({
98
+ hooks: options,
99
+ input,
80
100
  context,
81
- meta
82
- );
83
- return validateOutput(procedure, output);
84
- };
85
- return executeWithHooks({
86
- hooks: options,
87
- input,
88
- context,
89
- meta,
90
- execute: executeWithValidation
91
- });
101
+ meta: executeOptions,
102
+ execute: () => executeProcedureInternal(procedure, executeOptions)
103
+ });
104
+ return output;
105
+ } catch (e) {
106
+ if (!(e instanceof ORPCError2)) {
107
+ throw toError(e);
108
+ }
109
+ const validated = await validateORPCError(procedure["~orpc"].contract["~orpc"].errorMap, e);
110
+ throw validated;
111
+ }
92
112
  };
93
113
  }
94
114
  async function validateInput(procedure, input) {
@@ -97,10 +117,13 @@ async function validateInput(procedure, input) {
97
117
  return input;
98
118
  const result = await schema["~standard"].validate(input);
99
119
  if (result.issues) {
100
- throw new ORPCError({
120
+ throw new ORPCError2({
101
121
  message: "Input validation failed",
102
122
  code: "BAD_REQUEST",
103
- issues: result.issues
123
+ data: {
124
+ issues: result.issues
125
+ },
126
+ cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
104
127
  });
105
128
  }
106
129
  return result.value;
@@ -111,33 +134,37 @@ async function validateOutput(procedure, output) {
111
134
  return output;
112
135
  const result = await schema["~standard"].validate(output);
113
136
  if (result.issues) {
114
- throw new ORPCError({
137
+ throw new ORPCError2({
115
138
  message: "Output validation failed",
116
139
  code: "INTERNAL_SERVER_ERROR",
117
- issues: result.issues
140
+ cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
118
141
  });
119
142
  }
120
143
  return result.value;
121
144
  }
122
- async function executeMiddlewareChain(procedure, input, context, meta) {
123
- const middlewares = procedure["~orpc"].middlewares ?? [];
124
- let currentMidIndex = 0;
125
- let currentContext = context;
126
- const next = async (nextOptions) => {
127
- const mid = middlewares[currentMidIndex];
128
- currentMidIndex += 1;
129
- currentContext = mergeContext(currentContext, nextOptions.context);
130
- if (mid) {
131
- return await mid(input, currentContext, {
132
- ...meta,
133
- next,
134
- output: (output) => ({ output, context: void 0 })
135
- });
145
+ async function executeProcedureInternal(procedure, options) {
146
+ const middlewares = procedure["~orpc"].middlewares;
147
+ const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
148
+ const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
149
+ let currentIndex = 0;
150
+ let currentContext = options.context;
151
+ let currentInput = options.input;
152
+ const next = async (...[nextOptions]) => {
153
+ const index = currentIndex;
154
+ currentIndex += 1;
155
+ currentContext = { ...currentContext, ...nextOptions?.context };
156
+ if (index === inputValidationIndex) {
157
+ currentInput = await validateInput(procedure, currentInput);
158
+ }
159
+ const mid = middlewares[index];
160
+ const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
161
+ if (index === outputValidationIndex) {
162
+ const validatedOutput = await validateOutput(procedure, result.output);
163
+ return {
164
+ ...result,
165
+ output: validatedOutput
166
+ };
136
167
  }
137
- const result = {
138
- output: await procedure["~orpc"].handler(input, currentContext, meta),
139
- context: currentContext
140
- };
141
168
  return result;
142
169
  };
143
170
  return (await next({})).output;
@@ -175,15 +202,16 @@ function getRouterChild(router, ...path) {
175
202
 
176
203
  export {
177
204
  __export,
178
- mergeContext,
179
205
  Procedure,
180
206
  isProcedure,
207
+ createORPCErrorConstructorMap,
181
208
  LAZY_LOADER_SYMBOL,
182
209
  lazy,
183
210
  isLazy,
184
211
  unlazy,
185
212
  flatLazy,
213
+ middlewareOutputFn,
186
214
  createProcedureClient,
187
215
  getRouterChild
188
216
  };
189
- //# sourceMappingURL=chunk-6A7XHEBH.js.map
217
+ //# sourceMappingURL=chunk-LDIL7OEP.js.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-WUOGVGWG.js.map
package/dist/fetch.js CHANGED
@@ -1,32 +1,15 @@
1
+ import "./chunk-WUOGVGWG.js";
1
2
  import {
2
- ORPCHandler,
3
3
  ORPCPayloadCodec,
4
4
  ORPCProcedureMatcher,
5
+ RPCHandler,
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-26GHKV43.js";
8
+ import "./chunk-LDIL7OEP.js";
25
9
  export {
26
- CompositeHandler,
27
- ORPCHandler,
28
10
  ORPCPayloadCodec,
29
11
  ORPCProcedureMatcher,
12
+ RPCHandler,
30
13
  super_json_exports as SuperJSON
31
14
  };
32
15
  //# sourceMappingURL=fetch.js.map
package/dist/hono.js ADDED
@@ -0,0 +1,30 @@
1
+ import "./chunk-WUOGVGWG.js";
2
+ import {
3
+ ORPCPayloadCodec,
4
+ ORPCProcedureMatcher,
5
+ RPCHandler,
6
+ super_json_exports
7
+ } from "./chunk-26GHKV43.js";
8
+ import "./chunk-LDIL7OEP.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
+ ORPCPayloadCodec,
25
+ ORPCProcedureMatcher,
26
+ RPCHandler,
27
+ super_json_exports as SuperJSON,
28
+ createMiddleware
29
+ };
30
+ //# sourceMappingURL=hono.js.map