@orpc/server 0.0.0-next.9b3a030 → 0.0.0-next.a2e4a58

Sign up to get free protection for your applications and to get access to all the features.
Files changed (35) hide show
  1. package/dist/{chunk-DNG2IB3R.js → chunk-GK2Z6B6W.js} +38 -35
  2. package/dist/{chunk-V3I7RIRY.js → chunk-SXUFCJBY.js} +5 -6
  3. package/dist/fetch.js +4 -4
  4. package/dist/hono.js +4 -4
  5. package/dist/index.js +672 -174
  6. package/dist/next.js +4 -4
  7. package/dist/node.js +6 -6
  8. package/dist/src/adapters/fetch/orpc-handler.d.ts +3 -3
  9. package/dist/src/adapters/node/orpc-handler.d.ts +3 -3
  10. package/dist/src/builder-with-errors-middlewares.d.ts +51 -0
  11. package/dist/src/builder-with-errors.d.ts +52 -0
  12. package/dist/src/builder-with-middlewares.d.ts +48 -0
  13. package/dist/src/builder.d.ts +36 -23
  14. package/dist/src/config.d.ts +6 -0
  15. package/dist/src/context.d.ts +11 -0
  16. package/dist/src/error.d.ts +1 -1
  17. package/dist/src/hidden.d.ts +2 -2
  18. package/dist/src/implementer-chainable.d.ts +7 -3
  19. package/dist/src/index.d.ts +4 -2
  20. package/dist/src/lazy-decorated.d.ts +3 -6
  21. package/dist/src/middleware-decorated.d.ts +2 -1
  22. package/dist/src/middleware.d.ts +1 -0
  23. package/dist/src/procedure-builder-with-input.d.ts +34 -0
  24. package/dist/src/procedure-builder-with-output.d.ts +33 -0
  25. package/dist/src/procedure-builder.d.ts +21 -18
  26. package/dist/src/procedure-client.d.ts +7 -19
  27. package/dist/src/procedure-decorated.d.ts +21 -10
  28. package/dist/src/procedure-implementer.d.ts +7 -4
  29. package/dist/src/procedure-utils.d.ts +17 -0
  30. package/dist/src/procedure.d.ts +4 -2
  31. package/dist/src/router-builder.d.ts +19 -15
  32. package/dist/src/router-client.d.ts +7 -6
  33. package/dist/src/router-implementer.d.ts +7 -6
  34. package/dist/src/router.d.ts +3 -3
  35. package/package.json +3 -3
@@ -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,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-DNG2IB3R.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-DNG2IB3R.js";
7
+ } from "./chunk-GK2Z6B6W.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-SXUFCJBY.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-SXUFCJBY.js";
8
+ import "./chunk-GK2Z6B6W.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-SXUFCJBY.js";
8
+ import "./chunk-GK2Z6B6W.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
  };