@orpc/server 0.28.0 → 0.30.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,7 +4,7 @@ import {
4
4
  getRouterChild,
5
5
  isProcedure,
6
6
  unlazy
7
- } from "./chunk-DNG2IB3R.js";
7
+ } from "./chunk-SA7HGGVY.js";
8
8
 
9
9
  // src/adapters/fetch/super-json.ts
10
10
  var super_json_exports = {};
@@ -238,7 +238,7 @@ var ORPCProcedureMatcher = class {
238
238
  // src/adapters/fetch/orpc-handler.ts
239
239
  import { ORPCError as ORPCError2 } from "@orpc/contract";
240
240
  import { executeWithHooks, trim as trim2 } from "@orpc/shared";
241
- var ORPCHandler = class {
241
+ var RPCHandler = class {
242
242
  constructor(router, options) {
243
243
  this.options = options;
244
244
  this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
@@ -256,9 +256,8 @@ var ORPCHandler = class {
256
256
  return { matched: false, response: void 0 };
257
257
  }
258
258
  const input = await this.payloadCodec.decode(request);
259
- const client = createProcedureClient({
259
+ const client = createProcedureClient(match.procedure, {
260
260
  context,
261
- procedure: match.procedure,
262
261
  path: match.path
263
262
  });
264
263
  const output = await client(input, { signal: request.signal });
@@ -297,6 +296,6 @@ export {
297
296
  super_json_exports,
298
297
  ORPCPayloadCodec,
299
298
  ORPCProcedureMatcher,
300
- ORPCHandler
299
+ RPCHandler
301
300
  };
302
- //# sourceMappingURL=chunk-V3I7RIRY.js.map
301
+ //# sourceMappingURL=chunk-2HRHHZJD.js.map
@@ -36,9 +36,6 @@ function isProcedure(item) {
36
36
  import { ORPCError } from "@orpc/contract";
37
37
  function createORPCErrorConstructorMap(errors) {
38
38
  const constructors = {};
39
- if (!errors) {
40
- return constructors;
41
- }
42
39
  for (const code in errors) {
43
40
  const config = errors[code];
44
41
  if (!config) {
@@ -86,38 +83,35 @@ function flatLazy(lazied) {
86
83
  return lazy(flattenLoader);
87
84
  }
88
85
 
86
+ // src/middleware.ts
87
+ function middlewareOutputFn(output) {
88
+ return { output, context: void 0 };
89
+ }
90
+
89
91
  // src/procedure-client.ts
90
92
  import { ORPCError as ORPCError2, validateORPCError, ValidationError } from "@orpc/contract";
91
93
  import { executeWithHooks, toError, value } from "@orpc/shared";
92
- function createProcedureClient(options) {
94
+ function createProcedureClient(lazyableProcedure, ...[options]) {
93
95
  return async (...[input, callerOptions]) => {
94
- const path = options.path ?? [];
95
- const { default: procedure } = await unlazy(options.procedure);
96
- const context = await value(options.context);
97
- const meta = {
96
+ const path = options?.path ?? [];
97
+ const { default: procedure } = await unlazy(lazyableProcedure);
98
+ const context = await value(options?.context, callerOptions?.context);
99
+ const errors = createORPCErrorConstructorMap(procedure["~orpc"].contract["~orpc"].errorMap);
100
+ const executeOptions = {
101
+ input,
102
+ context,
103
+ errors,
98
104
  path,
99
105
  procedure,
100
106
  signal: callerOptions?.signal
101
107
  };
102
- const executeWithValidation = async () => {
103
- const validInput = await validateInput(procedure, input);
104
- const output = await executeMiddlewareChain({
105
- context,
106
- input: validInput,
107
- path,
108
- procedure,
109
- signal: callerOptions?.signal,
110
- errors: createORPCErrorConstructorMap(procedure["~orpc"].contract["~orpc"].errorMap)
111
- });
112
- return validateOutput(procedure, output);
113
- };
114
108
  try {
115
109
  const output = await executeWithHooks({
116
110
  hooks: options,
117
111
  input,
118
112
  context,
119
- meta,
120
- execute: executeWithValidation
113
+ meta: executeOptions,
114
+ execute: () => executeProcedureInternal(procedure, executeOptions)
121
115
  });
122
116
  return output;
123
117
  } catch (e) {
@@ -160,24 +154,44 @@ async function validateOutput(procedure, output) {
160
154
  }
161
155
  return result.value;
162
156
  }
163
- async function executeMiddlewareChain(opt) {
164
- const middlewares = opt.procedure["~orpc"].middlewares ?? [];
165
- let currentMidIndex = 0;
157
+ function executeMiddlewareChain(middlewares, opt, input) {
158
+ let currentIndex = 0;
166
159
  let currentContext = opt.context;
167
- const next = async (nextOptions) => {
168
- const mid = middlewares[currentMidIndex];
169
- currentMidIndex += 1;
160
+ const executeMiddlewareChain2 = async (nextOptions) => {
161
+ const mid = middlewares[currentIndex];
162
+ currentIndex += 1;
170
163
  currentContext = mergeContext(currentContext, nextOptions.context);
171
164
  if (mid) {
172
- return await mid({ ...opt, context: currentContext, next }, opt.input, (output) => ({ output, context: void 0 }));
165
+ return await mid({ ...opt, context: currentContext, next: executeMiddlewareChain2 }, input, middlewareOutputFn);
173
166
  }
174
- const result = {
175
- output: await opt.procedure["~orpc"].handler({ ...opt, context: currentContext }),
176
- context: currentContext
177
- };
178
- return result;
167
+ return opt.next({ context: currentContext });
168
+ };
169
+ return executeMiddlewareChain2({});
170
+ }
171
+ async function executeProcedureInternal(procedure, options) {
172
+ const executeHandler = async (context, input) => {
173
+ return await procedure["~orpc"].handler({ ...options, context, input });
174
+ };
175
+ const executePostMiddlewares = async (context, input) => {
176
+ const validatedInput = await validateInput(procedure, input);
177
+ const result2 = await executeMiddlewareChain(procedure["~orpc"].postMiddlewares, {
178
+ ...options,
179
+ context,
180
+ next: async ({ context: context2 }) => {
181
+ return middlewareOutputFn(
182
+ await executeHandler(context2, validatedInput)
183
+ );
184
+ }
185
+ }, validatedInput);
186
+ const validatedOutput = await validateOutput(procedure, result2.output);
187
+ return { ...result2, output: validatedOutput };
179
188
  };
180
- return (await next({})).output;
189
+ const result = await executeMiddlewareChain(procedure["~orpc"].preMiddlewares, {
190
+ ...options,
191
+ context: options.context,
192
+ next: ({ context }) => executePostMiddlewares(context, options.input)
193
+ }, options.input);
194
+ return result.output;
181
195
  }
182
196
 
183
197
  // src/router.ts
@@ -221,7 +235,8 @@ export {
221
235
  isLazy,
222
236
  unlazy,
223
237
  flatLazy,
238
+ middlewareOutputFn,
224
239
  createProcedureClient,
225
240
  getRouterChild
226
241
  };
227
- //# sourceMappingURL=chunk-DNG2IB3R.js.map
242
+ //# sourceMappingURL=chunk-SA7HGGVY.js.map
package/dist/fetch.js CHANGED
@@ -1,15 +1,15 @@
1
1
  import "./chunk-WUOGVGWG.js";
2
2
  import {
3
- ORPCHandler,
4
3
  ORPCPayloadCodec,
5
4
  ORPCProcedureMatcher,
5
+ RPCHandler,
6
6
  super_json_exports
7
- } from "./chunk-V3I7RIRY.js";
8
- import "./chunk-DNG2IB3R.js";
7
+ } from "./chunk-2HRHHZJD.js";
8
+ import "./chunk-SA7HGGVY.js";
9
9
  export {
10
- ORPCHandler,
11
10
  ORPCPayloadCodec,
12
11
  ORPCProcedureMatcher,
12
+ RPCHandler,
13
13
  super_json_exports as SuperJSON
14
14
  };
15
15
  //# sourceMappingURL=fetch.js.map
package/dist/hono.js CHANGED
@@ -1,11 +1,11 @@
1
1
  import "./chunk-WUOGVGWG.js";
2
2
  import {
3
- ORPCHandler,
4
3
  ORPCPayloadCodec,
5
4
  ORPCProcedureMatcher,
5
+ RPCHandler,
6
6
  super_json_exports
7
- } from "./chunk-V3I7RIRY.js";
8
- import "./chunk-DNG2IB3R.js";
7
+ } from "./chunk-2HRHHZJD.js";
8
+ import "./chunk-SA7HGGVY.js";
9
9
 
10
10
  // src/adapters/hono/middleware.ts
11
11
  import { value } from "@orpc/shared";
@@ -21,9 +21,9 @@ function createMiddleware(handler, ...[options]) {
21
21
  };
22
22
  }
23
23
  export {
24
- ORPCHandler,
25
24
  ORPCPayloadCodec,
26
25
  ORPCProcedureMatcher,
26
+ RPCHandler,
27
27
  super_json_exports as SuperJSON,
28
28
  createMiddleware
29
29
  };