@orpc/server 0.0.0-next.22ccd98 → 0.0.0-next.23aa4be

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. package/dist/{chunk-B2EZJB7X.js → chunk-5IM453DN.js} +16 -18
  2. package/dist/chunk-WUOGVGWG.js +1 -0
  3. package/dist/{chunk-6A7XHEBH.js → chunk-ZBJYYEII.js} +70 -32
  4. package/dist/fetch.js +3 -20
  5. package/dist/hono.js +30 -0
  6. package/dist/index.js +135 -106
  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 +11 -10
  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 +3 -2
  25. package/dist/src/lazy-decorated.d.ts +3 -6
  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 +17 -15
  29. package/dist/src/procedure-client.d.ts +8 -20
  30. package/dist/src/procedure-decorated.d.ts +22 -10
  31. package/dist/src/procedure-implementer.d.ts +13 -12
  32. package/dist/src/procedure-utils.d.ts +17 -0
  33. package/dist/src/procedure.d.ts +33 -13
  34. package/dist/src/router-builder.d.ts +4 -4
  35. package/dist/src/router-client.d.ts +7 -5
  36. package/dist/src/router-implementer.d.ts +2 -2
  37. package/dist/src/router.d.ts +4 -4
  38. package/dist/src/types.d.ts +2 -0
  39. package/package.json +18 -8
  40. package/dist/src/adapters/fetch/composite-handler.d.ts +0 -8
  41. package/dist/src/adapters/node/composite-handler.d.ts +0 -9
@@ -4,7 +4,7 @@ import {
4
4
  getRouterChild,
5
5
  isProcedure,
6
6
  unlazy
7
- } from "./chunk-6A7XHEBH.js";
7
+ } from "./chunk-ZBJYYEII.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";
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
- 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
  };
@@ -300,4 +298,4 @@ export {
300
298
  ORPCProcedureMatcher,
301
299
  ORPCHandler
302
300
  };
303
- //# sourceMappingURL=chunk-B2EZJB7X.js.map
301
+ //# sourceMappingURL=chunk-5IM453DN.js.map
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-WUOGVGWG.js.map
@@ -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,13 +87,13 @@ 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";
65
- function createProcedureClient(options) {
90
+ import { ORPCError as ORPCError2, validateORPCError, ValidationError } from "@orpc/contract";
91
+ import { executeWithHooks, toError, value } from "@orpc/shared";
92
+ function createProcedureClient(lazyableProcedure, ...[options]) {
66
93
  return async (...[input, callerOptions]) => {
67
- const path = options.path ?? [];
68
- const { default: procedure } = await unlazy(options.procedure);
69
- const context = await value(options.context);
94
+ const path = options?.path ?? [];
95
+ const { default: procedure } = await unlazy(lazyableProcedure);
96
+ const context = await value(options?.context, callerOptions?.context);
70
97
  const meta = {
71
98
  path,
72
99
  procedure,
@@ -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-ZBJYYEII.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-5IM453DN.js";
8
+ import "./chunk-ZBJYYEII.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-5IM453DN.js";
8
+ import "./chunk-ZBJYYEII.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