@orpc/server 0.29.0 → 0.31.0

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.
@@ -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,6 +83,11 @@ 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";
@@ -94,30 +96,22 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
94
96
  const path = options?.path ?? [];
95
97
  const { default: procedure } = await unlazy(lazyableProcedure);
96
98
  const context = await value(options?.context, callerOptions?.context);
97
- const meta = {
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,21 +154,29 @@ 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;
166
- let currentContext = opt.context;
157
+ async function executeProcedureInternal(procedure, options) {
158
+ const middlewares = procedure["~orpc"].middlewares;
159
+ const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
160
+ const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
161
+ let currentIndex = 0;
162
+ let currentContext = options.context;
163
+ let currentInput = options.input;
167
164
  const next = async (nextOptions) => {
168
- const mid = middlewares[currentMidIndex];
169
- currentMidIndex += 1;
165
+ const index = currentIndex;
166
+ currentIndex += 1;
170
167
  currentContext = mergeContext(currentContext, nextOptions.context);
171
- if (mid) {
172
- return await mid({ ...opt, context: currentContext, next }, opt.input, (output) => ({ output, context: void 0 }));
168
+ if (index === inputValidationIndex) {
169
+ currentInput = await validateInput(procedure, currentInput);
170
+ }
171
+ const mid = middlewares[index];
172
+ const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
173
+ if (index === outputValidationIndex) {
174
+ const validatedOutput = await validateOutput(procedure, result.output);
175
+ return {
176
+ ...result,
177
+ output: validatedOutput
178
+ };
173
179
  }
174
- const result = {
175
- output: await opt.procedure["~orpc"].handler({ ...opt, context: currentContext }),
176
- context: currentContext
177
- };
178
180
  return result;
179
181
  };
180
182
  return (await next({})).output;
@@ -221,7 +223,8 @@ export {
221
223
  isLazy,
222
224
  unlazy,
223
225
  flatLazy,
226
+ middlewareOutputFn,
224
227
  createProcedureClient,
225
228
  getRouterChild
226
229
  };
227
- //# sourceMappingURL=chunk-XDC42C3C.js.map
230
+ //# sourceMappingURL=chunk-GK2Z6B6W.js.map
@@ -4,7 +4,7 @@ import {
4
4
  getRouterChild,
5
5
  isProcedure,
6
6
  unlazy
7
- } from "./chunk-XDC42C3C.js";
7
+ } from "./chunk-GK2Z6B6W.js";
8
8
 
9
9
  // src/adapters/fetch/super-json.ts
10
10
  var super_json_exports = {};
@@ -298,4 +298,4 @@ export {
298
298
  ORPCProcedureMatcher,
299
299
  RPCHandler
300
300
  };
301
- //# sourceMappingURL=chunk-VY6NXNQT.js.map
301
+ //# sourceMappingURL=chunk-SXUFCJBY.js.map
package/dist/fetch.js CHANGED
@@ -4,8 +4,8 @@ import {
4
4
  ORPCProcedureMatcher,
5
5
  RPCHandler,
6
6
  super_json_exports
7
- } from "./chunk-VY6NXNQT.js";
8
- import "./chunk-XDC42C3C.js";
7
+ } from "./chunk-SXUFCJBY.js";
8
+ import "./chunk-GK2Z6B6W.js";
9
9
  export {
10
10
  ORPCPayloadCodec,
11
11
  ORPCProcedureMatcher,
package/dist/hono.js CHANGED
@@ -4,8 +4,8 @@ import {
4
4
  ORPCProcedureMatcher,
5
5
  RPCHandler,
6
6
  super_json_exports
7
- } from "./chunk-VY6NXNQT.js";
8
- import "./chunk-XDC42C3C.js";
7
+ } from "./chunk-SXUFCJBY.js";
8
+ import "./chunk-GK2Z6B6W.js";
9
9
 
10
10
  // src/adapters/hono/middleware.ts
11
11
  import { value } from "@orpc/shared";