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

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 (46) hide show
  1. package/dist/{chunk-V3I7RIRY.js → chunk-ESTRJAOX.js} +8 -11
  2. package/dist/chunk-KK4SDLC7.js +320 -0
  3. package/dist/fetch.js +4 -4
  4. package/dist/hono.js +5 -5
  5. package/dist/index.js +248 -402
  6. package/dist/next.js +5 -5
  7. package/dist/node.js +10 -10
  8. package/dist/src/adapters/fetch/orpc-handler.d.ts +4 -4
  9. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +4 -4
  10. package/dist/src/adapters/fetch/types.d.ts +4 -4
  11. package/dist/src/adapters/hono/middleware.d.ts +3 -3
  12. package/dist/src/adapters/next/serve.d.ts +8 -8
  13. package/dist/src/adapters/node/orpc-handler.d.ts +5 -5
  14. package/dist/src/adapters/node/types.d.ts +5 -5
  15. package/dist/src/builder-variants.d.ts +74 -0
  16. package/dist/src/builder.d.ts +52 -31
  17. package/dist/src/config.d.ts +6 -0
  18. package/dist/src/context.d.ts +9 -0
  19. package/dist/src/hidden.d.ts +6 -4
  20. package/dist/src/implementer-procedure.d.ts +30 -0
  21. package/dist/src/implementer-variants.d.ts +17 -0
  22. package/dist/src/implementer.d.ts +28 -0
  23. package/dist/src/index.d.ts +10 -13
  24. package/dist/src/lazy-utils.d.ts +4 -2
  25. package/dist/src/lazy.d.ts +9 -5
  26. package/dist/src/middleware-decorated.d.ts +7 -6
  27. package/dist/src/middleware-utils.d.ts +5 -0
  28. package/dist/src/middleware.d.ts +22 -20
  29. package/dist/src/procedure-client.d.ts +7 -21
  30. package/dist/src/procedure-decorated.d.ts +18 -12
  31. package/dist/src/procedure-utils.d.ts +17 -0
  32. package/dist/src/procedure.d.ts +19 -33
  33. package/dist/src/router-accessible-lazy.d.ts +8 -0
  34. package/dist/src/router-client.d.ts +7 -10
  35. package/dist/src/router.d.ts +25 -12
  36. package/package.json +3 -3
  37. package/dist/chunk-DNG2IB3R.js +0 -227
  38. package/dist/src/error.d.ts +0 -10
  39. package/dist/src/implementer-chainable.d.ts +0 -10
  40. package/dist/src/lazy-decorated.d.ts +0 -10
  41. package/dist/src/procedure-builder.d.ts +0 -24
  42. package/dist/src/procedure-implementer.d.ts +0 -19
  43. package/dist/src/router-builder.d.ts +0 -29
  44. package/dist/src/router-implementer.d.ts +0 -21
  45. package/dist/src/types.d.ts +0 -14
  46. 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-DNG2IB3R.js";
7
+ } from "./chunk-KK4SDLC7.js";
8
8
 
9
9
  // src/adapters/fetch/super-json.ts
10
10
  var super_json_exports = {};
@@ -206,8 +206,7 @@ var ORPCPayloadCodec = class {
206
206
  const json = await re.json();
207
207
  return deserialize(json);
208
208
  } catch (e) {
209
- throw new ORPCError({
210
- code: "BAD_REQUEST",
209
+ throw new ORPCError("BAD_REQUEST", {
211
210
  message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
212
211
  cause: e
213
212
  });
@@ -238,7 +237,7 @@ var ORPCProcedureMatcher = class {
238
237
  // src/adapters/fetch/orpc-handler.ts
239
238
  import { ORPCError as ORPCError2 } from "@orpc/contract";
240
239
  import { executeWithHooks, trim as trim2 } from "@orpc/shared";
241
- var ORPCHandler = class {
240
+ var RPCHandler = class {
242
241
  constructor(router, options) {
243
242
  this.options = options;
244
243
  this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
@@ -247,7 +246,7 @@ var ORPCHandler = class {
247
246
  procedureMatcher;
248
247
  payloadCodec;
249
248
  async handle(request, ...[options]) {
250
- const context = options?.context;
249
+ const context = options?.context ?? {};
251
250
  const execute = async () => {
252
251
  const url = new URL(request.url);
253
252
  const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
@@ -256,9 +255,8 @@ var ORPCHandler = class {
256
255
  return { matched: false, response: void 0 };
257
256
  }
258
257
  const input = await this.payloadCodec.decode(request);
259
- const client = createProcedureClient({
258
+ const client = createProcedureClient(match.procedure, {
260
259
  context,
261
- procedure: match.procedure,
262
260
  path: match.path
263
261
  });
264
262
  const output = await client(input, { signal: request.signal });
@@ -278,8 +276,7 @@ var ORPCHandler = class {
278
276
  });
279
277
  return result;
280
278
  } catch (e) {
281
- const error = e instanceof ORPCError2 ? e : new ORPCError2({
282
- code: "INTERNAL_SERVER_ERROR",
279
+ const error = e instanceof ORPCError2 ? e : new ORPCError2("INTERNAL_SERVER_ERROR", {
283
280
  message: "Internal server error",
284
281
  cause: e
285
282
  });
@@ -297,6 +294,6 @@ export {
297
294
  super_json_exports,
298
295
  ORPCPayloadCodec,
299
296
  ORPCProcedureMatcher,
300
- ORPCHandler
297
+ RPCHandler
301
298
  };
302
- //# sourceMappingURL=chunk-V3I7RIRY.js.map
299
+ //# sourceMappingURL=chunk-ESTRJAOX.js.map
@@ -0,0 +1,320 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/lazy.ts
8
+ var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
9
+ function lazy(loader) {
10
+ return {
11
+ [LAZY_LOADER_SYMBOL]: loader
12
+ };
13
+ }
14
+ function isLazy(item) {
15
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
16
+ }
17
+ function unlazy(lazied) {
18
+ return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
19
+ }
20
+
21
+ // src/procedure.ts
22
+ import { isContractProcedure } from "@orpc/contract";
23
+ var Procedure = class {
24
+ "~orpc";
25
+ constructor(def) {
26
+ this["~orpc"] = def;
27
+ }
28
+ };
29
+ function isProcedure(item) {
30
+ if (item instanceof Procedure) {
31
+ return true;
32
+ }
33
+ return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
34
+ }
35
+
36
+ // src/lazy-utils.ts
37
+ function flatLazy(lazied) {
38
+ const flattenLoader = async () => {
39
+ let current = await unlazy(lazied);
40
+ while (true) {
41
+ if (!isLazy(current.default)) {
42
+ break;
43
+ }
44
+ current = await unlazy(current.default);
45
+ }
46
+ return current;
47
+ };
48
+ return lazy(flattenLoader);
49
+ }
50
+ function createLazyProcedureFormAnyLazy(lazied) {
51
+ const lazyProcedure = lazy(async () => {
52
+ const { default: maybeProcedure } = await unlazy(flatLazy(lazied));
53
+ if (!isProcedure(maybeProcedure)) {
54
+ throw new Error(`
55
+ Expected a lazy<procedure> but got lazy<unknown>.
56
+ This should be caught by TypeScript compilation.
57
+ Please report this issue if this makes you feel uncomfortable.
58
+ `);
59
+ }
60
+ return { default: maybeProcedure };
61
+ });
62
+ return lazyProcedure;
63
+ }
64
+
65
+ // src/middleware.ts
66
+ function middlewareOutputFn(output) {
67
+ return { output, context: {} };
68
+ }
69
+
70
+ // src/procedure-client.ts
71
+ import { createORPCErrorConstructorMap, ORPCError, validateORPCError, ValidationError } from "@orpc/contract";
72
+ import { executeWithHooks, toError, value } from "@orpc/shared";
73
+ function createProcedureClient(lazyableProcedure, ...[options]) {
74
+ return async (...[input, callerOptions]) => {
75
+ const path = options?.path ?? [];
76
+ const { default: procedure } = await unlazy(lazyableProcedure);
77
+ const context = await value(options?.context ?? {}, callerOptions?.context);
78
+ const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
79
+ const executeOptions = {
80
+ input,
81
+ context,
82
+ errors,
83
+ path,
84
+ procedure,
85
+ signal: callerOptions?.signal
86
+ };
87
+ try {
88
+ const output = await executeWithHooks({
89
+ hooks: options,
90
+ input,
91
+ context,
92
+ meta: executeOptions,
93
+ execute: () => executeProcedureInternal(procedure, executeOptions)
94
+ });
95
+ return output;
96
+ } catch (e) {
97
+ if (!(e instanceof ORPCError)) {
98
+ throw toError(e);
99
+ }
100
+ const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
101
+ throw validated;
102
+ }
103
+ };
104
+ }
105
+ async function validateInput(procedure, input) {
106
+ const schema = procedure["~orpc"].inputSchema;
107
+ if (!schema) {
108
+ return input;
109
+ }
110
+ const result = await schema["~standard"].validate(input);
111
+ if (result.issues) {
112
+ throw new ORPCError("BAD_REQUEST", {
113
+ message: "Input validation failed",
114
+ data: {
115
+ issues: result.issues
116
+ },
117
+ cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
118
+ });
119
+ }
120
+ return result.value;
121
+ }
122
+ async function validateOutput(procedure, output) {
123
+ const schema = procedure["~orpc"].outputSchema;
124
+ if (!schema) {
125
+ return output;
126
+ }
127
+ const result = await schema["~standard"].validate(output);
128
+ if (result.issues) {
129
+ throw new ORPCError("INTERNAL_SERVER_ERROR", {
130
+ message: "Output validation failed",
131
+ cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
132
+ });
133
+ }
134
+ return result.value;
135
+ }
136
+ async function executeProcedureInternal(procedure, options) {
137
+ const middlewares = procedure["~orpc"].middlewares;
138
+ const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
139
+ const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
140
+ let currentIndex = 0;
141
+ let currentContext = options.context;
142
+ let currentInput = options.input;
143
+ const next = async (...[nextOptions]) => {
144
+ const index = currentIndex;
145
+ currentIndex += 1;
146
+ currentContext = { ...currentContext, ...nextOptions?.context };
147
+ if (index === inputValidationIndex) {
148
+ currentInput = await validateInput(procedure, currentInput);
149
+ }
150
+ const mid = middlewares[index];
151
+ const result = mid ? await mid({ ...options, context: currentContext, next }, currentInput, middlewareOutputFn) : { output: await procedure["~orpc"].handler({ ...options, context: currentContext, input: currentInput }), context: currentContext };
152
+ if (index === outputValidationIndex) {
153
+ const validatedOutput = await validateOutput(procedure, result.output);
154
+ return {
155
+ ...result,
156
+ output: validatedOutput
157
+ };
158
+ }
159
+ return result;
160
+ };
161
+ return (await next({})).output;
162
+ }
163
+
164
+ // src/hidden.ts
165
+ var ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_ROUTER_CONTRACT");
166
+ function setRouterContract(obj, contract) {
167
+ return new Proxy(obj, {
168
+ get(target, key) {
169
+ if (key === ROUTER_CONTRACT_SYMBOL) {
170
+ return contract;
171
+ }
172
+ return Reflect.get(target, key);
173
+ }
174
+ });
175
+ }
176
+ function getRouterContract(obj) {
177
+ return obj[ROUTER_CONTRACT_SYMBOL];
178
+ }
179
+ var LAZY_ROUTER_PREFIX_SYMBOL = Symbol("ORPC_LAZY_ROUTER_PREFIX");
180
+ function deepSetLazyRouterPrefix(router, prefix) {
181
+ return new Proxy(router, {
182
+ get(target, key) {
183
+ if (key !== LAZY_ROUTER_PREFIX_SYMBOL) {
184
+ const val = Reflect.get(target, key);
185
+ if (isLazy(val)) {
186
+ return deepSetLazyRouterPrefix(val, prefix);
187
+ }
188
+ return val;
189
+ }
190
+ return prefix;
191
+ }
192
+ });
193
+ }
194
+ function getLazyRouterPrefix(obj) {
195
+ return obj[LAZY_ROUTER_PREFIX_SYMBOL];
196
+ }
197
+
198
+ // src/router.ts
199
+ import { adaptRoute, mergeErrorMap, mergePrefix } from "@orpc/contract";
200
+
201
+ // src/middleware-utils.ts
202
+ function dedupeMiddlewares(compare, middlewares) {
203
+ let min = 0;
204
+ for (let i = 0; i < middlewares.length; i++) {
205
+ const index = compare.indexOf(middlewares[i], min);
206
+ if (index === -1) {
207
+ return middlewares.slice(i);
208
+ }
209
+ min = index + 1;
210
+ }
211
+ return [];
212
+ }
213
+ function mergeMiddlewares(first, second) {
214
+ return [...first, ...dedupeMiddlewares(first, second)];
215
+ }
216
+ function addMiddleware(middlewares, addition) {
217
+ return [...middlewares, addition];
218
+ }
219
+
220
+ // src/router.ts
221
+ function adaptRouter(router, options) {
222
+ if (isLazy(router)) {
223
+ const adapted2 = lazy(async () => {
224
+ const unlaziedRouter = (await unlazy(router)).default;
225
+ const adapted3 = adaptRouter(unlaziedRouter, options);
226
+ return { default: adapted3 };
227
+ });
228
+ const accessible = createAccessibleLazyRouter(adapted2);
229
+ const currentPrefix = getLazyRouterPrefix(router);
230
+ const prefix = currentPrefix ? mergePrefix(options.prefix, currentPrefix) : options.prefix;
231
+ if (prefix) {
232
+ return deepSetLazyRouterPrefix(accessible, prefix);
233
+ }
234
+ return accessible;
235
+ }
236
+ if (isProcedure(router)) {
237
+ const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
238
+ const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
239
+ const adapted2 = new Procedure({
240
+ ...router["~orpc"],
241
+ route: adaptRoute(router["~orpc"].route, options),
242
+ errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
243
+ middlewares: newMiddlewares,
244
+ inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
245
+ outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
246
+ });
247
+ return adapted2;
248
+ }
249
+ const adapted = {};
250
+ for (const key in router) {
251
+ adapted[key] = adaptRouter(router[key], options);
252
+ }
253
+ return adapted;
254
+ }
255
+ function getRouterChild(router, ...path) {
256
+ let current = router;
257
+ for (let i = 0; i < path.length; i++) {
258
+ const segment = path[i];
259
+ if (!current) {
260
+ return void 0;
261
+ }
262
+ if (isProcedure(current)) {
263
+ return void 0;
264
+ }
265
+ if (!isLazy(current)) {
266
+ current = current[segment];
267
+ continue;
268
+ }
269
+ const lazied = current;
270
+ const rest = path.slice(i);
271
+ const newLazy = lazy(async () => {
272
+ const unwrapped = await unlazy(lazied);
273
+ if (!unwrapped.default) {
274
+ return unwrapped;
275
+ }
276
+ const next = getRouterChild(unwrapped.default, ...rest);
277
+ return { default: next };
278
+ });
279
+ return flatLazy(newLazy);
280
+ }
281
+ return current;
282
+ }
283
+
284
+ // src/router-accessible-lazy.ts
285
+ function createAccessibleLazyRouter(lazied) {
286
+ const flattenLazy = flatLazy(lazied);
287
+ const recursive = new Proxy(flattenLazy, {
288
+ get(target, key) {
289
+ if (typeof key !== "string") {
290
+ return Reflect.get(target, key);
291
+ }
292
+ const next = getRouterChild(flattenLazy, key);
293
+ return createAccessibleLazyRouter(next);
294
+ }
295
+ });
296
+ return recursive;
297
+ }
298
+
299
+ export {
300
+ __export,
301
+ LAZY_LOADER_SYMBOL,
302
+ lazy,
303
+ isLazy,
304
+ unlazy,
305
+ Procedure,
306
+ isProcedure,
307
+ flatLazy,
308
+ createLazyProcedureFormAnyLazy,
309
+ addMiddleware,
310
+ middlewareOutputFn,
311
+ createProcedureClient,
312
+ setRouterContract,
313
+ getRouterContract,
314
+ deepSetLazyRouterPrefix,
315
+ getLazyRouterPrefix,
316
+ createAccessibleLazyRouter,
317
+ adaptRouter,
318
+ getRouterChild
319
+ };
320
+ //# sourceMappingURL=chunk-KK4SDLC7.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-ESTRJAOX.js";
8
+ import "./chunk-KK4SDLC7.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,17 +1,17 @@
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-ESTRJAOX.js";
8
+ import "./chunk-KK4SDLC7.js";
9
9
 
10
10
  // src/adapters/hono/middleware.ts
11
11
  import { value } from "@orpc/shared";
12
12
  function createMiddleware(handler, ...[options]) {
13
13
  return async (c, next) => {
14
- const context = await value(options?.context, c);
14
+ const context = await value(options?.context ?? {}, c);
15
15
  const { matched, response } = await handler.handle(c.req.raw, { ...options, context });
16
16
  if (matched) {
17
17
  c.res = response;
@@ -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
  };