@orpc/server 0.0.0-next.f22c7ec → 0.0.0-next.f56d2b3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. package/dist/{chunk-2HRHHZJD.js → chunk-ESTRJAOX.js} +5 -7
  2. package/dist/chunk-KK4SDLC7.js +320 -0
  3. package/dist/fetch.js +2 -2
  4. package/dist/hono.js +3 -3
  5. package/dist/index.js +205 -405
  6. package/dist/next.js +3 -3
  7. package/dist/node.js +6 -6
  8. package/dist/src/adapters/fetch/orpc-handler.d.ts +1 -1
  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 +2 -2
  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 +50 -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 +5 -3
  20. package/dist/src/implementer-procedure.d.ts +30 -0
  21. package/dist/src/implementer-variants.d.ts +16 -0
  22. package/dist/src/implementer.d.ts +27 -0
  23. package/dist/src/index.d.ts +9 -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 -21
  29. package/dist/src/procedure-client.d.ts +6 -8
  30. package/dist/src/procedure-decorated.d.ts +10 -14
  31. package/dist/src/procedure-utils.d.ts +2 -2
  32. package/dist/src/procedure.d.ts +19 -34
  33. package/dist/src/router-accessible-lazy.d.ts +8 -0
  34. package/dist/src/router-client.d.ts +6 -10
  35. package/dist/src/router.d.ts +25 -12
  36. package/package.json +3 -3
  37. package/dist/chunk-SA7HGGVY.js +0 -242
  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 -7
  41. package/dist/src/procedure-builder.d.ts +0 -24
  42. package/dist/src/procedure-implementer.d.ts +0 -20
  43. package/dist/src/router-builder.d.ts +0 -31
  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-SA7HGGVY.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
  });
@@ -247,7 +246,7 @@ var RPCHandler = 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 ?? "", ""), "/")}`;
@@ -277,8 +276,7 @@ var RPCHandler = class {
277
276
  });
278
277
  return result;
279
278
  } catch (e) {
280
- const error = e instanceof ORPCError2 ? e : new ORPCError2({
281
- code: "INTERNAL_SERVER_ERROR",
279
+ const error = e instanceof ORPCError2 ? e : new ORPCError2("INTERNAL_SERVER_ERROR", {
282
280
  message: "Internal server error",
283
281
  cause: e
284
282
  });
@@ -298,4 +296,4 @@ export {
298
296
  ORPCProcedureMatcher,
299
297
  RPCHandler
300
298
  };
301
- //# sourceMappingURL=chunk-2HRHHZJD.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
@@ -4,8 +4,8 @@ import {
4
4
  ORPCProcedureMatcher,
5
5
  RPCHandler,
6
6
  super_json_exports
7
- } from "./chunk-2HRHHZJD.js";
8
- import "./chunk-SA7HGGVY.js";
7
+ } from "./chunk-ESTRJAOX.js";
8
+ import "./chunk-KK4SDLC7.js";
9
9
  export {
10
10
  ORPCPayloadCodec,
11
11
  ORPCProcedureMatcher,
package/dist/hono.js CHANGED
@@ -4,14 +4,14 @@ import {
4
4
  ORPCProcedureMatcher,
5
5
  RPCHandler,
6
6
  super_json_exports
7
- } from "./chunk-2HRHHZJD.js";
8
- import "./chunk-SA7HGGVY.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;