@orpc/server 0.0.0-next.9b3a030 → 0.0.0-next.9b9ade5
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.
- package/README.md +119 -0
- package/dist/adapters/fetch/index.d.mts +26 -0
- package/dist/adapters/fetch/index.d.ts +26 -0
- package/dist/adapters/fetch/index.mjs +9 -0
- package/dist/adapters/hono/index.d.mts +19 -0
- package/dist/adapters/hono/index.d.ts +19 -0
- package/dist/adapters/hono/index.mjs +32 -0
- package/dist/adapters/next/index.d.mts +26 -0
- package/dist/adapters/next/index.d.ts +26 -0
- package/dist/adapters/next/index.mjs +29 -0
- package/dist/adapters/node/index.d.mts +34 -0
- package/dist/adapters/node/index.d.ts +34 -0
- package/dist/adapters/node/index.mjs +31 -0
- package/dist/adapters/standard/index.d.mts +25 -0
- package/dist/adapters/standard/index.d.ts +25 -0
- package/dist/adapters/standard/index.mjs +6 -0
- package/dist/index.d.mts +269 -0
- package/dist/index.d.ts +269 -0
- package/dist/index.mjs +343 -0
- package/dist/plugins/index.d.mts +31 -0
- package/dist/plugins/index.d.ts +31 -0
- package/dist/plugins/index.mjs +103 -0
- package/dist/shared/server.CM3tWr3C.d.mts +75 -0
- package/dist/shared/server.CMrS28Go.mjs +346 -0
- package/dist/shared/server.CPteJIPP.d.mts +143 -0
- package/dist/shared/server.CPteJIPP.d.ts +143 -0
- package/dist/shared/server.CSZRzcSW.mjs +158 -0
- package/dist/shared/server.Cq3B6PoL.mjs +28 -0
- package/dist/shared/server.DmW25ynm.d.ts +75 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/package.json +36 -24
- package/dist/chunk-DNG2IB3R.js +0 -227
- package/dist/chunk-V3I7RIRY.js +0 -302
- package/dist/chunk-WUOGVGWG.js +0 -1
- package/dist/fetch.js +0 -15
- package/dist/hono.js +0 -30
- package/dist/index.js +0 -545
- package/dist/next.js +0 -36
- package/dist/node.js +0 -87
- package/dist/src/adapters/fetch/index.d.ts +0 -6
- package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
- package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
- package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
- package/dist/src/adapters/fetch/super-json.d.ts +0 -12
- package/dist/src/adapters/fetch/types.d.ts +0 -21
- package/dist/src/adapters/hono/index.d.ts +0 -3
- package/dist/src/adapters/hono/middleware.d.ts +0 -12
- package/dist/src/adapters/next/index.d.ts +0 -3
- package/dist/src/adapters/next/serve.d.ts +0 -19
- package/dist/src/adapters/node/index.d.ts +0 -5
- package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
- package/dist/src/adapters/node/request-listener.d.ts +0 -28
- package/dist/src/adapters/node/types.d.ts +0 -22
- package/dist/src/builder.d.ts +0 -36
- package/dist/src/error.d.ts +0 -10
- package/dist/src/hidden.d.ts +0 -6
- package/dist/src/implementer-chainable.d.ts +0 -10
- package/dist/src/index.d.ts +0 -24
- package/dist/src/lazy-decorated.d.ts +0 -10
- package/dist/src/lazy-utils.d.ts +0 -4
- package/dist/src/lazy.d.ts +0 -18
- package/dist/src/middleware-decorated.d.ts +0 -9
- package/dist/src/middleware.d.ts +0 -35
- package/dist/src/procedure-builder.d.ts +0 -24
- package/dist/src/procedure-client.d.ts +0 -34
- package/dist/src/procedure-decorated.d.ts +0 -15
- package/dist/src/procedure-implementer.d.ts +0 -19
- package/dist/src/procedure.d.ts +0 -43
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-client.d.ts +0 -25
- package/dist/src/router-implementer.d.ts +0 -21
- package/dist/src/router.d.ts +0 -16
- package/dist/src/types.d.ts +0 -14
- 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 };
|
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.
|
4
|
+
"version": "0.0.0-next.9b9ade5",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -15,37 +15,42 @@
|
|
15
15
|
],
|
16
16
|
"exports": {
|
17
17
|
".": {
|
18
|
-
"types": "./dist/
|
19
|
-
"import": "./dist/index.
|
20
|
-
"default": "./dist/index.
|
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/
|
24
|
-
"import": "./dist/fetch.
|
25
|
-
"default": "./dist/fetch.
|
33
|
+
"types": "./dist/adapters/fetch/index.d.mts",
|
34
|
+
"import": "./dist/adapters/fetch/index.mjs",
|
35
|
+
"default": "./dist/adapters/fetch/index.mjs"
|
26
36
|
},
|
27
37
|
"./hono": {
|
28
|
-
"types": "./dist/
|
29
|
-
"import": "./dist/hono.
|
30
|
-
"default": "./dist/hono.
|
38
|
+
"types": "./dist/adapters/hono/index.d.mts",
|
39
|
+
"import": "./dist/adapters/hono/index.mjs",
|
40
|
+
"default": "./dist/adapters/hono/index.mjs"
|
31
41
|
},
|
32
42
|
"./next": {
|
33
|
-
"types": "./dist/
|
34
|
-
"import": "./dist/next.
|
35
|
-
"default": "./dist/next.
|
43
|
+
"types": "./dist/adapters/next/index.d.mts",
|
44
|
+
"import": "./dist/adapters/next/index.mjs",
|
45
|
+
"default": "./dist/adapters/next/index.mjs"
|
36
46
|
},
|
37
47
|
"./node": {
|
38
|
-
"types": "./dist/
|
39
|
-
"import": "./dist/node.
|
40
|
-
"default": "./dist/node.
|
41
|
-
},
|
42
|
-
"./🔒/*": {
|
43
|
-
"types": "./dist/src/*.d.ts"
|
48
|
+
"types": "./dist/adapters/node/index.d.mts",
|
49
|
+
"import": "./dist/adapters/node/index.mjs",
|
50
|
+
"default": "./dist/adapters/node/index.mjs"
|
44
51
|
}
|
45
52
|
},
|
46
53
|
"files": [
|
47
|
-
"!**/*.map",
|
48
|
-
"!**/*.tsbuildinfo",
|
49
54
|
"dist"
|
50
55
|
],
|
51
56
|
"peerDependencies": {
|
@@ -53,11 +58,18 @@
|
|
53
58
|
"next": ">=14.0.0"
|
54
59
|
},
|
55
60
|
"dependencies": {
|
56
|
-
"@orpc/contract": "0.0.0-next.
|
57
|
-
"@orpc/
|
61
|
+
"@orpc/contract": "0.0.0-next.9b9ade5",
|
62
|
+
"@orpc/client": "0.0.0-next.9b9ade5",
|
63
|
+
"@orpc/shared": "0.0.0-next.9b9ade5",
|
64
|
+
"@orpc/standard-server": "0.0.0-next.9b9ade5",
|
65
|
+
"@orpc/standard-server-fetch": "0.0.0-next.9b9ade5",
|
66
|
+
"@orpc/standard-server-node": "0.0.0-next.9b9ade5"
|
67
|
+
},
|
68
|
+
"devDependencies": {
|
69
|
+
"light-my-request": "^6.5.1"
|
58
70
|
},
|
59
71
|
"scripts": {
|
60
|
-
"build": "
|
72
|
+
"build": "unbuild",
|
61
73
|
"build:watch": "pnpm run build --watch",
|
62
74
|
"type:check": "tsc -b"
|
63
75
|
}
|
package/dist/chunk-DNG2IB3R.js
DELETED
@@ -1,227 +0,0 @@
|
|
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/utils.ts
|
8
|
-
function mergeContext(a, b) {
|
9
|
-
if (!a)
|
10
|
-
return b;
|
11
|
-
if (!b)
|
12
|
-
return a;
|
13
|
-
return {
|
14
|
-
...a,
|
15
|
-
...b
|
16
|
-
};
|
17
|
-
}
|
18
|
-
|
19
|
-
// src/procedure.ts
|
20
|
-
import { isContractProcedure } from "@orpc/contract";
|
21
|
-
var Procedure = class {
|
22
|
-
"~type" = "Procedure";
|
23
|
-
"~orpc";
|
24
|
-
constructor(def) {
|
25
|
-
this["~orpc"] = def;
|
26
|
-
}
|
27
|
-
};
|
28
|
-
function isProcedure(item) {
|
29
|
-
if (item instanceof Procedure) {
|
30
|
-
return true;
|
31
|
-
}
|
32
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "Procedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "contract" in item["~orpc"] && isContractProcedure(item["~orpc"].contract) && "handler" in item["~orpc"] && typeof item["~orpc"].handler === "function";
|
33
|
-
}
|
34
|
-
|
35
|
-
// src/error.ts
|
36
|
-
import { ORPCError } from "@orpc/contract";
|
37
|
-
function createORPCErrorConstructorMap(errors) {
|
38
|
-
const constructors = {};
|
39
|
-
if (!errors) {
|
40
|
-
return constructors;
|
41
|
-
}
|
42
|
-
for (const code in errors) {
|
43
|
-
const config = errors[code];
|
44
|
-
if (!config) {
|
45
|
-
continue;
|
46
|
-
}
|
47
|
-
const constructor = (...[options]) => {
|
48
|
-
return new ORPCError({
|
49
|
-
code,
|
50
|
-
defined: true,
|
51
|
-
status: config.status,
|
52
|
-
message: options?.message ?? config.message,
|
53
|
-
data: options?.data,
|
54
|
-
cause: options?.cause
|
55
|
-
});
|
56
|
-
};
|
57
|
-
constructors[code] = constructor;
|
58
|
-
}
|
59
|
-
return constructors;
|
60
|
-
}
|
61
|
-
|
62
|
-
// src/lazy.ts
|
63
|
-
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
64
|
-
function lazy(loader) {
|
65
|
-
return {
|
66
|
-
[LAZY_LOADER_SYMBOL]: loader
|
67
|
-
};
|
68
|
-
}
|
69
|
-
function isLazy(item) {
|
70
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
71
|
-
}
|
72
|
-
function unlazy(lazied) {
|
73
|
-
return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
|
74
|
-
}
|
75
|
-
function flatLazy(lazied) {
|
76
|
-
const flattenLoader = async () => {
|
77
|
-
let current = await unlazy(lazied);
|
78
|
-
while (true) {
|
79
|
-
if (!isLazy(current.default)) {
|
80
|
-
break;
|
81
|
-
}
|
82
|
-
current = await unlazy(current.default);
|
83
|
-
}
|
84
|
-
return current;
|
85
|
-
};
|
86
|
-
return lazy(flattenLoader);
|
87
|
-
}
|
88
|
-
|
89
|
-
// src/procedure-client.ts
|
90
|
-
import { ORPCError as ORPCError2, validateORPCError, ValidationError } from "@orpc/contract";
|
91
|
-
import { executeWithHooks, toError, value } from "@orpc/shared";
|
92
|
-
function createProcedureClient(options) {
|
93
|
-
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 = {
|
98
|
-
path,
|
99
|
-
procedure,
|
100
|
-
signal: callerOptions?.signal
|
101
|
-
};
|
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
|
-
try {
|
115
|
-
const output = await executeWithHooks({
|
116
|
-
hooks: options,
|
117
|
-
input,
|
118
|
-
context,
|
119
|
-
meta,
|
120
|
-
execute: executeWithValidation
|
121
|
-
});
|
122
|
-
return output;
|
123
|
-
} catch (e) {
|
124
|
-
if (!(e instanceof ORPCError2)) {
|
125
|
-
throw toError(e);
|
126
|
-
}
|
127
|
-
const validated = await validateORPCError(procedure["~orpc"].contract["~orpc"].errorMap, e);
|
128
|
-
throw validated;
|
129
|
-
}
|
130
|
-
};
|
131
|
-
}
|
132
|
-
async function validateInput(procedure, input) {
|
133
|
-
const schema = procedure["~orpc"].contract["~orpc"].InputSchema;
|
134
|
-
if (!schema)
|
135
|
-
return input;
|
136
|
-
const result = await schema["~standard"].validate(input);
|
137
|
-
if (result.issues) {
|
138
|
-
throw new ORPCError2({
|
139
|
-
message: "Input validation failed",
|
140
|
-
code: "BAD_REQUEST",
|
141
|
-
data: {
|
142
|
-
issues: result.issues
|
143
|
-
},
|
144
|
-
cause: new ValidationError({ message: "Input validation failed", issues: result.issues })
|
145
|
-
});
|
146
|
-
}
|
147
|
-
return result.value;
|
148
|
-
}
|
149
|
-
async function validateOutput(procedure, output) {
|
150
|
-
const schema = procedure["~orpc"].contract["~orpc"].OutputSchema;
|
151
|
-
if (!schema)
|
152
|
-
return output;
|
153
|
-
const result = await schema["~standard"].validate(output);
|
154
|
-
if (result.issues) {
|
155
|
-
throw new ORPCError2({
|
156
|
-
message: "Output validation failed",
|
157
|
-
code: "INTERNAL_SERVER_ERROR",
|
158
|
-
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
159
|
-
});
|
160
|
-
}
|
161
|
-
return result.value;
|
162
|
-
}
|
163
|
-
async function executeMiddlewareChain(opt) {
|
164
|
-
const middlewares = opt.procedure["~orpc"].middlewares ?? [];
|
165
|
-
let currentMidIndex = 0;
|
166
|
-
let currentContext = opt.context;
|
167
|
-
const next = async (nextOptions) => {
|
168
|
-
const mid = middlewares[currentMidIndex];
|
169
|
-
currentMidIndex += 1;
|
170
|
-
currentContext = mergeContext(currentContext, nextOptions.context);
|
171
|
-
if (mid) {
|
172
|
-
return await mid({ ...opt, context: currentContext, next }, opt.input, (output) => ({ output, context: void 0 }));
|
173
|
-
}
|
174
|
-
const result = {
|
175
|
-
output: await opt.procedure["~orpc"].handler({ ...opt, context: currentContext }),
|
176
|
-
context: currentContext
|
177
|
-
};
|
178
|
-
return result;
|
179
|
-
};
|
180
|
-
return (await next({})).output;
|
181
|
-
}
|
182
|
-
|
183
|
-
// src/router.ts
|
184
|
-
function getRouterChild(router, ...path) {
|
185
|
-
let current = router;
|
186
|
-
for (let i = 0; i < path.length; i++) {
|
187
|
-
const segment = path[i];
|
188
|
-
if (!current) {
|
189
|
-
return void 0;
|
190
|
-
}
|
191
|
-
if (isProcedure(current)) {
|
192
|
-
return void 0;
|
193
|
-
}
|
194
|
-
if (!isLazy(current)) {
|
195
|
-
current = current[segment];
|
196
|
-
continue;
|
197
|
-
}
|
198
|
-
const lazied = current;
|
199
|
-
const rest = path.slice(i);
|
200
|
-
const newLazy = lazy(async () => {
|
201
|
-
const unwrapped = await unlazy(lazied);
|
202
|
-
if (!unwrapped.default) {
|
203
|
-
return unwrapped;
|
204
|
-
}
|
205
|
-
const next = getRouterChild(unwrapped.default, ...rest);
|
206
|
-
return { default: next };
|
207
|
-
});
|
208
|
-
return flatLazy(newLazy);
|
209
|
-
}
|
210
|
-
return current;
|
211
|
-
}
|
212
|
-
|
213
|
-
export {
|
214
|
-
__export,
|
215
|
-
mergeContext,
|
216
|
-
Procedure,
|
217
|
-
isProcedure,
|
218
|
-
createORPCErrorConstructorMap,
|
219
|
-
LAZY_LOADER_SYMBOL,
|
220
|
-
lazy,
|
221
|
-
isLazy,
|
222
|
-
unlazy,
|
223
|
-
flatLazy,
|
224
|
-
createProcedureClient,
|
225
|
-
getRouterChild
|
226
|
-
};
|
227
|
-
//# sourceMappingURL=chunk-DNG2IB3R.js.map
|