@orpc/server 0.0.0-next.8b5a6d6 → 0.0.0-next.8e347ae

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 (52) hide show
  1. package/README.md +120 -0
  2. package/dist/adapters/fetch/index.d.mts +26 -0
  3. package/dist/adapters/fetch/index.d.ts +26 -0
  4. package/dist/adapters/fetch/index.mjs +9 -0
  5. package/dist/adapters/hono/index.d.mts +19 -0
  6. package/dist/adapters/hono/index.d.ts +19 -0
  7. package/dist/adapters/hono/index.mjs +32 -0
  8. package/dist/adapters/next/index.d.mts +26 -0
  9. package/dist/adapters/next/index.d.ts +26 -0
  10. package/dist/adapters/next/index.mjs +29 -0
  11. package/dist/adapters/node/index.d.mts +34 -0
  12. package/dist/adapters/node/index.d.ts +34 -0
  13. package/dist/adapters/node/index.mjs +31 -0
  14. package/dist/adapters/standard/index.d.mts +25 -0
  15. package/dist/adapters/standard/index.d.ts +25 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +269 -0
  18. package/dist/index.d.ts +269 -0
  19. package/dist/index.mjs +343 -0
  20. package/dist/plugins/index.d.mts +31 -0
  21. package/dist/plugins/index.d.ts +31 -0
  22. package/dist/plugins/index.mjs +103 -0
  23. package/dist/shared/server.CM3tWr3C.d.mts +75 -0
  24. package/dist/shared/server.CMrS28Go.mjs +346 -0
  25. package/dist/shared/server.CPteJIPP.d.mts +143 -0
  26. package/dist/shared/server.CPteJIPP.d.ts +143 -0
  27. package/dist/shared/server.CSZRzcSW.mjs +158 -0
  28. package/dist/shared/server.Cq3B6PoL.mjs +28 -0
  29. package/dist/shared/server.DmW25ynm.d.ts +75 -0
  30. package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
  31. package/package.json +41 -18
  32. package/dist/chunk-3JMSDC5L.js +0 -274
  33. package/dist/fetch.js +0 -112
  34. package/dist/index.js +0 -467
  35. package/dist/src/builder.d.ts +0 -53
  36. package/dist/src/fetch/handle.d.ts +0 -7
  37. package/dist/src/fetch/handler.d.ts +0 -3
  38. package/dist/src/fetch/index.d.ts +0 -4
  39. package/dist/src/fetch/types.d.ts +0 -28
  40. package/dist/src/index.d.ts +0 -17
  41. package/dist/src/lazy.d.ts +0 -23
  42. package/dist/src/middleware.d.ts +0 -26
  43. package/dist/src/procedure-builder.d.ts +0 -31
  44. package/dist/src/procedure-caller.d.ts +0 -26
  45. package/dist/src/procedure-implementer.d.ts +0 -22
  46. package/dist/src/procedure.d.ts +0 -32
  47. package/dist/src/router-builder.d.ts +0 -27
  48. package/dist/src/router-caller.d.ts +0 -25
  49. package/dist/src/router-implementer.d.ts +0 -24
  50. package/dist/src/router.d.ts +0 -21
  51. package/dist/src/types.d.ts +0 -14
  52. package/dist/src/utils.d.ts +0 -3
@@ -0,0 +1,158 @@
1
+ import { ORPCError, toORPCError } from '@orpc/client';
2
+ import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
3
+ import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
4
+ import { c as createProcedureClient, t as traverseContractProcedures, a as toHttpPath, i as isProcedure, u as unlazy, g as getRouter, b as createContractedProcedure } from './server.CMrS28Go.mjs';
5
+
6
+ class StandardHandler {
7
+ constructor(router, matcher, codec, options) {
8
+ this.matcher = matcher;
9
+ this.codec = codec;
10
+ this.options = options;
11
+ this.plugin = new CompositePlugin(options.plugins);
12
+ this.plugin.init(this.options);
13
+ this.matcher.init(router);
14
+ }
15
+ plugin;
16
+ handle(request, ...[options]) {
17
+ return intercept(
18
+ this.options.rootInterceptors ?? [],
19
+ {
20
+ request,
21
+ ...options,
22
+ context: options?.context ?? {}
23
+ // context is optional only when all fields are optional so we can safely force it to have a context
24
+ },
25
+ async (interceptorOptions) => {
26
+ let isDecoding = false;
27
+ try {
28
+ return await intercept(
29
+ this.options.interceptors ?? [],
30
+ interceptorOptions,
31
+ async (interceptorOptions2) => {
32
+ const method = interceptorOptions2.request.method;
33
+ const url = interceptorOptions2.request.url;
34
+ const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
35
+ const match = await this.matcher.match(method, pathname);
36
+ if (!match) {
37
+ return { matched: false, response: void 0 };
38
+ }
39
+ const client = createProcedureClient(match.procedure, {
40
+ context: interceptorOptions2.context,
41
+ path: match.path,
42
+ interceptors: this.options.clientInterceptors
43
+ });
44
+ isDecoding = true;
45
+ const input = await this.codec.decode(request, match.params, match.procedure);
46
+ isDecoding = false;
47
+ const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
48
+ const output = await client(input, { signal: request.signal, lastEventId });
49
+ const response = this.codec.encode(output, match.procedure);
50
+ return {
51
+ matched: true,
52
+ response
53
+ };
54
+ }
55
+ );
56
+ } catch (e) {
57
+ const error = isDecoding ? new ORPCError("BAD_REQUEST", {
58
+ message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
59
+ cause: e
60
+ }) : toORPCError(e);
61
+ const response = this.codec.encodeError(error);
62
+ return {
63
+ matched: true,
64
+ response
65
+ };
66
+ }
67
+ }
68
+ );
69
+ }
70
+ }
71
+
72
+ class RPCCodec {
73
+ constructor(serializer) {
74
+ this.serializer = serializer;
75
+ }
76
+ async decode(request, _params, _procedure) {
77
+ const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
78
+ return this.serializer.deserialize(serialized);
79
+ }
80
+ encode(output, _procedure) {
81
+ return {
82
+ status: 200,
83
+ headers: {},
84
+ body: this.serializer.serialize(output)
85
+ };
86
+ }
87
+ encodeError(error) {
88
+ return {
89
+ status: error.status,
90
+ headers: {},
91
+ body: this.serializer.serialize(error.toJSON())
92
+ };
93
+ }
94
+ }
95
+
96
+ class RPCMatcher {
97
+ tree = {};
98
+ pendingRouters = [];
99
+ init(router, path = []) {
100
+ const laziedOptions = traverseContractProcedures({ router, path }, ({ path: path2, contract }) => {
101
+ const httpPath = toHttpPath(path2);
102
+ if (isProcedure(contract)) {
103
+ this.tree[httpPath] = {
104
+ path: path2,
105
+ contract,
106
+ procedure: contract,
107
+ // this mean dev not used contract-first so we can used contract as procedure directly
108
+ router
109
+ };
110
+ } else {
111
+ this.tree[httpPath] = {
112
+ path: path2,
113
+ contract,
114
+ procedure: void 0,
115
+ router
116
+ };
117
+ }
118
+ });
119
+ this.pendingRouters.push(...laziedOptions.map((option) => ({
120
+ ...option,
121
+ httpPathPrefix: toHttpPath(option.path)
122
+ })));
123
+ }
124
+ async match(_method, pathname) {
125
+ if (this.pendingRouters.length) {
126
+ const newPendingRouters = [];
127
+ for (const pendingRouter of this.pendingRouters) {
128
+ if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
129
+ const { default: router } = await unlazy(pendingRouter.router);
130
+ this.init(router, pendingRouter.path);
131
+ } else {
132
+ newPendingRouters.push(pendingRouter);
133
+ }
134
+ }
135
+ this.pendingRouters = newPendingRouters;
136
+ }
137
+ const match = this.tree[pathname];
138
+ if (!match) {
139
+ return void 0;
140
+ }
141
+ if (!match.procedure) {
142
+ const { default: maybeProcedure } = await unlazy(getRouter(match.router, match.path));
143
+ if (!isProcedure(maybeProcedure)) {
144
+ throw new Error(`
145
+ [Contract-First] Missing or invalid implementation for procedure at path: ${toHttpPath(match.path)}.
146
+ Ensure that the procedure is correctly defined and matches the expected contract.
147
+ `);
148
+ }
149
+ match.procedure = createContractedProcedure(maybeProcedure, match.contract);
150
+ }
151
+ return {
152
+ path: match.path,
153
+ procedure: match.procedure
154
+ };
155
+ }
156
+ }
157
+
158
+ export { RPCCodec as R, StandardHandler as S, RPCMatcher as a };
@@ -0,0 +1,28 @@
1
+ import { RPCSerializer } from '@orpc/client/standard';
2
+ import { toStandardLazyRequest, toFetchResponse } from '@orpc/standard-server-fetch';
3
+ import { S as StandardHandler, a as RPCMatcher, R as RPCCodec } from './server.CSZRzcSW.mjs';
4
+
5
+ class RPCHandler {
6
+ standardHandler;
7
+ constructor(router, options = {}) {
8
+ const serializer = new RPCSerializer();
9
+ const matcher = new RPCMatcher();
10
+ const codec = new RPCCodec(serializer);
11
+ this.standardHandler = new StandardHandler(router, matcher, codec, options);
12
+ }
13
+ async handle(request, ...[
14
+ options = {}
15
+ ]) {
16
+ const standardRequest = toStandardLazyRequest(request);
17
+ const result = await this.standardHandler.handle(standardRequest, options);
18
+ if (!result.matched) {
19
+ return result;
20
+ }
21
+ return {
22
+ matched: true,
23
+ response: toFetchResponse(result.response, options)
24
+ };
25
+ }
26
+ }
27
+
28
+ export { RPCHandler as R };
@@ -0,0 +1,75 @@
1
+ import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
2
+ import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
4
+ import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.CPteJIPP.js';
5
+ import { ORPCError } from '@orpc/client';
6
+
7
+ type StandardParams = Record<string, string>;
8
+ type StandardMatchResult = {
9
+ path: readonly string[];
10
+ procedure: AnyProcedure;
11
+ params?: StandardParams;
12
+ } | undefined;
13
+ interface StandardMatcher {
14
+ init(router: AnyRouter): void;
15
+ match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
16
+ }
17
+ interface StandardCodec {
18
+ encode(output: unknown, procedure: AnyProcedure): StandardResponse;
19
+ encodeError(error: ORPCError<any, any>): StandardResponse;
20
+ decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
21
+ }
22
+
23
+ type StandardHandleOptions<T extends Context> = {
24
+ prefix?: HTTPPath;
25
+ } & (Record<never, never> extends T ? {
26
+ context?: T;
27
+ } : {
28
+ context: T;
29
+ });
30
+ type StandardHandleResult = {
31
+ matched: true;
32
+ response: StandardResponse;
33
+ } | {
34
+ matched: false;
35
+ response: undefined;
36
+ };
37
+ type StandardHandlerInterceptorOptions<T extends Context> = StandardHandleOptions<T> & {
38
+ context: T;
39
+ request: StandardLazyRequest;
40
+ };
41
+ interface StandardHandlerOptions<TContext extends Context> {
42
+ plugins?: HandlerPlugin<TContext>[];
43
+ /**
44
+ * Interceptors at the request level, helpful when you want catch errors
45
+ */
46
+ interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
47
+ /**
48
+ * Interceptors at the root level, helpful when you want override the request/response
49
+ */
50
+ rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
51
+ /**
52
+ *
53
+ * Interceptors for procedure client.
54
+ */
55
+ clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
56
+ }
57
+ declare class StandardHandler<T extends Context> {
58
+ private readonly matcher;
59
+ private readonly codec;
60
+ private readonly options;
61
+ private readonly plugin;
62
+ constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
63
+ handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
64
+ }
65
+
66
+ interface HandlerPlugin<TContext extends Context> {
67
+ init?(options: StandardHandlerOptions<TContext>): void;
68
+ }
69
+ declare class CompositePlugin<TContext extends Context> implements HandlerPlugin<TContext> {
70
+ private readonly plugins;
71
+ constructor(plugins?: HandlerPlugin<TContext>[]);
72
+ init(options: StandardHandlerOptions<TContext>): void;
73
+ }
74
+
75
+ export { CompositePlugin as C, type HandlerPlugin as H, type StandardHandleOptions as S, type StandardHandlerInterceptorOptions as a, type StandardHandlerOptions as b, type StandardCodec as c, type StandardParams as d, type StandardMatcher as e, type StandardMatchResult as f, type StandardHandleResult as g, StandardHandler as h };
@@ -0,0 +1,12 @@
1
+ class CompositePlugin {
2
+ constructor(plugins = []) {
3
+ this.plugins = plugins;
4
+ }
5
+ init(options) {
6
+ for (const plugin of this.plugins) {
7
+ plugin.init?.(options);
8
+ }
9
+ }
10
+ }
11
+
12
+ export { CompositePlugin as C };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/server",
3
3
  "type": "module",
4
- "version": "0.0.0-next.8b5a6d6",
4
+ "version": "0.0.0-next.8e347ae",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -15,38 +15,61 @@
15
15
  ],
16
16
  "exports": {
17
17
  ".": {
18
- "types": "./dist/src/index.d.ts",
19
- "import": "./dist/index.js",
20
- "default": "./dist/index.js"
18
+ "types": "./dist/index.d.mts",
19
+ "import": "./dist/index.mjs",
20
+ "default": "./dist/index.mjs"
21
+ },
22
+ "./plugins": {
23
+ "types": "./dist/plugins/index.d.mts",
24
+ "import": "./dist/plugins/index.mjs",
25
+ "default": "./dist/plugins/index.mjs"
26
+ },
27
+ "./standard": {
28
+ "types": "./dist/adapters/standard/index.d.mts",
29
+ "import": "./dist/adapters/standard/index.mjs",
30
+ "default": "./dist/adapters/standard/index.mjs"
21
31
  },
22
32
  "./fetch": {
23
- "types": "./dist/src/fetch/index.d.ts",
24
- "import": "./dist/fetch.js",
25
- "default": "./dist/fetch.js"
33
+ "types": "./dist/adapters/fetch/index.d.mts",
34
+ "import": "./dist/adapters/fetch/index.mjs",
35
+ "default": "./dist/adapters/fetch/index.mjs"
36
+ },
37
+ "./hono": {
38
+ "types": "./dist/adapters/hono/index.d.mts",
39
+ "import": "./dist/adapters/hono/index.mjs",
40
+ "default": "./dist/adapters/hono/index.mjs"
41
+ },
42
+ "./next": {
43
+ "types": "./dist/adapters/next/index.d.mts",
44
+ "import": "./dist/adapters/next/index.mjs",
45
+ "default": "./dist/adapters/next/index.mjs"
26
46
  },
27
- "./🔒/*": {
28
- "types": "./dist/src/*.d.ts"
47
+ "./node": {
48
+ "types": "./dist/adapters/node/index.d.mts",
49
+ "import": "./dist/adapters/node/index.mjs",
50
+ "default": "./dist/adapters/node/index.mjs"
29
51
  }
30
52
  },
31
53
  "files": [
32
- "!**/*.map",
33
- "!**/*.tsbuildinfo",
34
54
  "dist"
35
55
  ],
36
56
  "peerDependencies": {
37
- "zod": ">=3.23.0",
38
- "@orpc/zod": "0.0.0-next.8b5a6d6"
57
+ "hono": ">=4.6.0",
58
+ "next": ">=14.0.0"
39
59
  },
40
60
  "dependencies": {
41
- "@orpc/shared": "0.0.0-next.8b5a6d6",
42
- "@orpc/contract": "0.0.0-next.8b5a6d6",
43
- "@orpc/transformer": "0.0.0-next.8b5a6d6"
61
+ "@orpc/contract": "0.0.0-next.8e347ae",
62
+ "@orpc/shared": "0.0.0-next.8e347ae",
63
+ "@orpc/client": "0.0.0-next.8e347ae",
64
+ "@orpc/standard-server": "0.0.0-next.8e347ae",
65
+ "@orpc/standard-server-node": "0.0.0-next.8e347ae",
66
+ "@orpc/standard-server-fetch": "0.0.0-next.8e347ae"
44
67
  },
45
68
  "devDependencies": {
46
- "@orpc/openapi": "0.0.0-next.8b5a6d6"
69
+ "light-my-request": "^6.5.1"
47
70
  },
48
71
  "scripts": {
49
- "build": "tsup --clean --sourcemap --entry.index=src/index.ts --entry.fetch=src/fetch/index.ts --format=esm --onSuccess='tsc -b --noCheck'",
72
+ "build": "unbuild",
50
73
  "build:watch": "pnpm run build --watch",
51
74
  "type:check": "tsc -b"
52
75
  }
@@ -1,274 +0,0 @@
1
- // src/utils.ts
2
- function mergeContext(a, b) {
3
- if (!a)
4
- return b;
5
- if (!b)
6
- return a;
7
- return {
8
- ...a,
9
- ...b
10
- };
11
- }
12
-
13
- // src/middleware.ts
14
- var decoratedMiddlewareSymbol = Symbol("\u{1F512}decoratedMiddleware");
15
- function decorateMiddleware(middleware) {
16
- if (Reflect.get(middleware, decoratedMiddlewareSymbol)) {
17
- return middleware;
18
- }
19
- const concat = (concatMiddleware, mapInput2) => {
20
- const concatMiddleware_ = mapInput2 ? decorateMiddleware(concatMiddleware).mapInput(mapInput2) : concatMiddleware;
21
- return decorateMiddleware(async (input, context, meta, ...rest) => {
22
- const input_ = input;
23
- const context_ = context;
24
- const meta_ = meta;
25
- const next = async (options) => {
26
- return concatMiddleware_(input_, mergeContext(context_, options.context), meta_, ...rest);
27
- };
28
- const m1 = await middleware(input_, context_, {
29
- ...meta_,
30
- next
31
- }, ...rest);
32
- return m1;
33
- });
34
- };
35
- const mapInput = (map) => {
36
- return decorateMiddleware(
37
- (input, ...rest) => middleware(map(input), ...rest)
38
- );
39
- };
40
- return Object.assign(middleware, {
41
- [decoratedMiddlewareSymbol]: true,
42
- concat,
43
- mapInput
44
- });
45
- }
46
-
47
- // src/procedure-caller.ts
48
- import { executeWithHooks, trim, value } from "@orpc/shared";
49
- import { ORPCError } from "@orpc/shared/error";
50
-
51
- // src/procedure.ts
52
- import {
53
- DecoratedContractProcedure,
54
- isContractProcedure
55
- } from "@orpc/contract";
56
- var Procedure = class {
57
- constructor(zz$p) {
58
- this.zz$p = zz$p;
59
- }
60
- };
61
- var DECORATED_PROCEDURE_SYMBOL = Symbol("DECORATED_PROCEDURE");
62
- function decorateProcedure(procedure) {
63
- if (DECORATED_PROCEDURE_SYMBOL in procedure) {
64
- return procedure;
65
- }
66
- return Object.assign(createProcedureCaller({
67
- procedure,
68
- context: void 0
69
- }), {
70
- [DECORATED_PROCEDURE_SYMBOL]: true,
71
- zz$p: procedure.zz$p,
72
- prefix(prefix) {
73
- return decorateProcedure({
74
- zz$p: {
75
- ...procedure.zz$p,
76
- contract: DecoratedContractProcedure.decorate(
77
- procedure.zz$p.contract
78
- ).prefix(prefix)
79
- }
80
- });
81
- },
82
- route(opts) {
83
- return decorateProcedure({
84
- zz$p: {
85
- ...procedure.zz$p,
86
- contract: DecoratedContractProcedure.decorate(
87
- procedure.zz$p.contract
88
- ).route(opts)
89
- }
90
- });
91
- },
92
- use(middleware, mapInput) {
93
- const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
94
- return decorateProcedure({
95
- zz$p: {
96
- ...procedure.zz$p,
97
- middlewares: [middleware_, ...procedure.zz$p.middlewares ?? []]
98
- }
99
- });
100
- }
101
- });
102
- }
103
- function isProcedure(item) {
104
- if (item instanceof Procedure)
105
- return true;
106
- return (typeof item === "object" || typeof item === "function") && item !== null && "zz$p" in item && typeof item.zz$p === "object" && item.zz$p !== null && "contract" in item.zz$p && isContractProcedure(item.zz$p.contract) && "func" in item.zz$p && typeof item.zz$p.func === "function";
107
- }
108
-
109
- // src/procedure-caller.ts
110
- function createProcedureCaller(options) {
111
- const caller = async (...args) => {
112
- const [input, callerOptions] = args;
113
- const path = options.path ?? [];
114
- const procedure = await loadProcedure(options.procedure);
115
- const context = await value(options.context);
116
- const execute = async () => {
117
- const validInput = (() => {
118
- const schema = procedure.zz$p.contract.zz$cp.InputSchema;
119
- if (!schema) {
120
- return input;
121
- }
122
- try {
123
- return schema.parse(input);
124
- } catch (e) {
125
- throw new ORPCError({
126
- message: "Validation input failed",
127
- code: "BAD_REQUEST",
128
- cause: e
129
- });
130
- }
131
- })();
132
- const meta = {
133
- path,
134
- procedure,
135
- signal: callerOptions?.signal
136
- };
137
- const middlewares = procedure.zz$p.middlewares ?? [];
138
- let currentMidIndex = 0;
139
- let currentContext = context;
140
- const next = async (nextOptions) => {
141
- const mid = middlewares[currentMidIndex];
142
- currentMidIndex += 1;
143
- currentContext = mergeContext(currentContext, nextOptions.context);
144
- if (mid) {
145
- return await mid(validInput, currentContext, {
146
- ...meta,
147
- next,
148
- output: (output3) => ({ output: output3, context: void 0 })
149
- });
150
- } else {
151
- return {
152
- output: await await procedure.zz$p.func(validInput, currentContext, meta),
153
- context: currentContext
154
- };
155
- }
156
- };
157
- const output2 = (await next({})).output;
158
- const validOutput = await (async () => {
159
- const schema = procedure.zz$p.contract.zz$cp.OutputSchema;
160
- if (!schema) {
161
- return output2;
162
- }
163
- const result = await schema.safeParseAsync(output2);
164
- if (result.error) {
165
- throw new ORPCError({
166
- message: "Validation output failed",
167
- code: "INTERNAL_SERVER_ERROR",
168
- cause: result.error
169
- });
170
- }
171
- return result.data;
172
- })();
173
- return validOutput;
174
- };
175
- const output = await executeWithHooks({
176
- hooks: options,
177
- input,
178
- context,
179
- meta: {
180
- path,
181
- procedure
182
- },
183
- execute
184
- });
185
- return output;
186
- };
187
- return caller;
188
- }
189
- async function loadProcedure(procedure) {
190
- let loadedProcedure;
191
- if (isLazy(procedure)) {
192
- loadedProcedure = (await loadLazy(procedure)).default;
193
- } else {
194
- loadedProcedure = procedure;
195
- }
196
- if (!isProcedure(loadedProcedure)) {
197
- throw new ORPCError({
198
- code: "NOT_FOUND",
199
- message: "Not found",
200
- cause: new Error(trim(`
201
- This error should be caught by the typescript compiler.
202
- But if you still see this error, it means that you trying to call a lazy router (expected to be a lazy procedure).
203
- `))
204
- });
205
- }
206
- return loadedProcedure;
207
- }
208
-
209
- // src/lazy.ts
210
- var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
211
- function createLazy(loader) {
212
- return {
213
- [LAZY_LOADER_SYMBOL]: loader
214
- };
215
- }
216
- function loadLazy(lazy) {
217
- return lazy[LAZY_LOADER_SYMBOL]();
218
- }
219
- function isLazy(item) {
220
- return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
221
- }
222
- function createFlattenLazy(lazy) {
223
- const flattenLoader = async () => {
224
- let current = await loadLazy(lazy);
225
- while (true) {
226
- if (!isLazy(current.default)) {
227
- break;
228
- }
229
- current = await loadLazy(current.default);
230
- }
231
- return current;
232
- };
233
- const flattenLazy = {
234
- [LAZY_LOADER_SYMBOL]: flattenLoader
235
- };
236
- return flattenLazy;
237
- }
238
- function decorateLazy(lazy) {
239
- const flattenLazy = createFlattenLazy(lazy);
240
- const procedureCaller = createProcedureCaller({
241
- procedure: flattenLazy,
242
- context: void 0
243
- });
244
- Object.assign(procedureCaller, flattenLazy);
245
- const recursive = new Proxy(procedureCaller, {
246
- get(target, key) {
247
- if (typeof key !== "string") {
248
- return Reflect.get(target, key);
249
- }
250
- return decorateLazy(createLazy(async () => {
251
- const current = await loadLazy(flattenLazy);
252
- return { default: current.default[key] };
253
- }));
254
- }
255
- });
256
- return recursive;
257
- }
258
-
259
- export {
260
- mergeContext,
261
- decorateMiddleware,
262
- LAZY_LOADER_SYMBOL,
263
- createLazy,
264
- loadLazy,
265
- isLazy,
266
- createFlattenLazy,
267
- decorateLazy,
268
- createProcedureCaller,
269
- loadProcedure,
270
- Procedure,
271
- decorateProcedure,
272
- isProcedure
273
- };
274
- //# sourceMappingURL=chunk-3JMSDC5L.js.map