@orpc/server 0.0.0-next.cba521d → 0.0.0-next.cc8802c
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 +118 -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-3EVCPLVI.js +0 -301
- package/dist/chunk-OUPZ7QGV.js +0 -245
- package/dist/chunk-WUOGVGWG.js +0 -1
- package/dist/fetch.js +0 -15
- package/dist/hono.js +0 -30
- package/dist/index.js +0 -562
- 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 -25
- package/dist/src/lazy-decorated.d.ts +0 -7
- 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 -36
- package/dist/src/procedure-builder.d.ts +0 -24
- package/dist/src/procedure-client.d.ts +0 -22
- package/dist/src/procedure-decorated.d.ts +0 -26
- package/dist/src/procedure-implementer.d.ts +0 -20
- package/dist/src/procedure-utils.d.ts +0 -17
- package/dist/src/procedure.d.ts +0 -44
- package/dist/src/router-builder.d.ts +0 -29
- package/dist/src/router-client.d.ts +0 -26
- 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.cc8802c",
|
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/
|
57
|
-
"@orpc/
|
61
|
+
"@orpc/client": "0.0.0-next.cc8802c",
|
62
|
+
"@orpc/contract": "0.0.0-next.cc8802c",
|
63
|
+
"@orpc/shared": "0.0.0-next.cc8802c",
|
64
|
+
"@orpc/standard-server-node": "0.0.0-next.cc8802c",
|
65
|
+
"@orpc/standard-server-fetch": "0.0.0-next.cc8802c",
|
66
|
+
"@orpc/standard-server": "0.0.0-next.cc8802c"
|
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-3EVCPLVI.js
DELETED
@@ -1,301 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
__export,
|
3
|
-
createProcedureClient,
|
4
|
-
getRouterChild,
|
5
|
-
isProcedure,
|
6
|
-
unlazy
|
7
|
-
} from "./chunk-OUPZ7QGV.js";
|
8
|
-
|
9
|
-
// src/adapters/fetch/super-json.ts
|
10
|
-
var super_json_exports = {};
|
11
|
-
__export(super_json_exports, {
|
12
|
-
deserialize: () => deserialize,
|
13
|
-
serialize: () => serialize
|
14
|
-
});
|
15
|
-
|
16
|
-
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
|
17
|
-
function getType(payload) {
|
18
|
-
return Object.prototype.toString.call(payload).slice(8, -1);
|
19
|
-
}
|
20
|
-
|
21
|
-
// ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
|
22
|
-
function isPlainObject(payload) {
|
23
|
-
if (getType(payload) !== "Object")
|
24
|
-
return false;
|
25
|
-
const prototype = Object.getPrototypeOf(payload);
|
26
|
-
return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
|
27
|
-
}
|
28
|
-
|
29
|
-
// src/adapters/fetch/super-json.ts
|
30
|
-
function serialize(value, segments = [], meta = []) {
|
31
|
-
if (typeof value === "bigint") {
|
32
|
-
meta.push(["bigint", segments]);
|
33
|
-
return { data: value.toString(), meta };
|
34
|
-
}
|
35
|
-
if (value instanceof Date) {
|
36
|
-
meta.push(["date", segments]);
|
37
|
-
const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
|
38
|
-
return { data, meta };
|
39
|
-
}
|
40
|
-
if (Number.isNaN(value)) {
|
41
|
-
meta.push(["nan", segments]);
|
42
|
-
return { data: "NaN", meta };
|
43
|
-
}
|
44
|
-
if (value instanceof RegExp) {
|
45
|
-
meta.push(["regexp", segments]);
|
46
|
-
return { data: value.toString(), meta };
|
47
|
-
}
|
48
|
-
if (value instanceof URL) {
|
49
|
-
meta.push(["url", segments]);
|
50
|
-
return { data: value.toString(), meta };
|
51
|
-
}
|
52
|
-
if (isPlainObject(value)) {
|
53
|
-
const data = {};
|
54
|
-
for (const k in value) {
|
55
|
-
data[k] = serialize(value[k], [...segments, k], meta).data;
|
56
|
-
}
|
57
|
-
return { data, meta };
|
58
|
-
}
|
59
|
-
if (Array.isArray(value)) {
|
60
|
-
const data = value.map((v, i) => {
|
61
|
-
if (v === void 0) {
|
62
|
-
meta.push(["undefined", [...segments, i]]);
|
63
|
-
return null;
|
64
|
-
}
|
65
|
-
return serialize(v, [...segments, i], meta).data;
|
66
|
-
});
|
67
|
-
return { data, meta };
|
68
|
-
}
|
69
|
-
if (value instanceof Set) {
|
70
|
-
const result = serialize(Array.from(value), segments, meta);
|
71
|
-
meta.push(["set", segments]);
|
72
|
-
return result;
|
73
|
-
}
|
74
|
-
if (value instanceof Map) {
|
75
|
-
const result = serialize(Array.from(value.entries()), segments, meta);
|
76
|
-
meta.push(["map", segments]);
|
77
|
-
return result;
|
78
|
-
}
|
79
|
-
return { data: value, meta };
|
80
|
-
}
|
81
|
-
function deserialize({
|
82
|
-
data,
|
83
|
-
meta
|
84
|
-
}) {
|
85
|
-
if (meta.length === 0) {
|
86
|
-
return data;
|
87
|
-
}
|
88
|
-
const ref = { data };
|
89
|
-
for (const [type, segments] of meta) {
|
90
|
-
let currentRef = ref;
|
91
|
-
let preSegment = "data";
|
92
|
-
for (let i = 0; i < segments.length; i++) {
|
93
|
-
currentRef = currentRef[preSegment];
|
94
|
-
preSegment = segments[i];
|
95
|
-
}
|
96
|
-
switch (type) {
|
97
|
-
case "nan":
|
98
|
-
currentRef[preSegment] = Number.NaN;
|
99
|
-
break;
|
100
|
-
case "bigint":
|
101
|
-
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
102
|
-
break;
|
103
|
-
case "date":
|
104
|
-
currentRef[preSegment] = new Date(currentRef[preSegment]);
|
105
|
-
break;
|
106
|
-
case "regexp": {
|
107
|
-
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
108
|
-
currentRef[preSegment] = new RegExp(pattern, flags);
|
109
|
-
break;
|
110
|
-
}
|
111
|
-
case "url":
|
112
|
-
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
113
|
-
break;
|
114
|
-
case "undefined":
|
115
|
-
currentRef[preSegment] = void 0;
|
116
|
-
break;
|
117
|
-
case "map":
|
118
|
-
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
119
|
-
break;
|
120
|
-
case "set":
|
121
|
-
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
122
|
-
break;
|
123
|
-
/* v8 ignore next 3 */
|
124
|
-
default: {
|
125
|
-
const _expected = type;
|
126
|
-
}
|
127
|
-
}
|
128
|
-
}
|
129
|
-
return ref.data;
|
130
|
-
}
|
131
|
-
|
132
|
-
// src/adapters/fetch/orpc-payload-codec.ts
|
133
|
-
import { ORPCError } from "@orpc/contract";
|
134
|
-
import { findDeepMatches, set } from "@orpc/shared";
|
135
|
-
var ORPCPayloadCodec = class {
|
136
|
-
/**
|
137
|
-
* If method is GET, the payload will be encoded as query string.
|
138
|
-
* If method is GET and payload contain file, the method will be fallback to fallbackMethod. (fallbackMethod = GET will force to use GET method)
|
139
|
-
*/
|
140
|
-
encode(payload, method = "POST", fallbackMethod = "POST") {
|
141
|
-
const { data, meta } = serialize(payload);
|
142
|
-
const { maps, values } = findDeepMatches((v) => v instanceof Blob, data);
|
143
|
-
if (method === "GET" && (values.length === 0 || fallbackMethod === "GET")) {
|
144
|
-
const query = new URLSearchParams({
|
145
|
-
data: JSON.stringify(data),
|
146
|
-
meta: JSON.stringify(meta)
|
147
|
-
});
|
148
|
-
return {
|
149
|
-
query,
|
150
|
-
method: "GET"
|
151
|
-
};
|
152
|
-
}
|
153
|
-
const nonGETMethod = method === "GET" ? fallbackMethod : method;
|
154
|
-
if (values.length > 0) {
|
155
|
-
const form = new FormData();
|
156
|
-
if (data !== void 0) {
|
157
|
-
form.append("data", JSON.stringify(data));
|
158
|
-
}
|
159
|
-
form.append("meta", JSON.stringify(meta));
|
160
|
-
form.append("maps", JSON.stringify(maps));
|
161
|
-
for (const i in values) {
|
162
|
-
const value = values[i];
|
163
|
-
form.append(i, value);
|
164
|
-
}
|
165
|
-
return {
|
166
|
-
body: form,
|
167
|
-
method: nonGETMethod
|
168
|
-
};
|
169
|
-
}
|
170
|
-
return {
|
171
|
-
body: JSON.stringify({ data, meta }),
|
172
|
-
headers: new Headers({
|
173
|
-
"content-type": "application/json"
|
174
|
-
}),
|
175
|
-
method: nonGETMethod
|
176
|
-
};
|
177
|
-
}
|
178
|
-
async decode(re) {
|
179
|
-
try {
|
180
|
-
if ("method" in re && re.method === "GET") {
|
181
|
-
const url = new URL(re.url);
|
182
|
-
const query = url.searchParams;
|
183
|
-
const data = JSON.parse(query.getAll("data").at(-1));
|
184
|
-
const meta = JSON.parse(query.getAll("meta").at(-1));
|
185
|
-
return deserialize({
|
186
|
-
data,
|
187
|
-
meta
|
188
|
-
});
|
189
|
-
}
|
190
|
-
if (re.headers.get("content-type")?.startsWith("multipart/form-data")) {
|
191
|
-
const form = await re.formData();
|
192
|
-
const rawData = form.get("data");
|
193
|
-
const rawMeta = form.get("meta");
|
194
|
-
const rawMaps = form.get("maps");
|
195
|
-
let data = JSON.parse(rawData);
|
196
|
-
const meta = JSON.parse(rawMeta);
|
197
|
-
const maps = JSON.parse(rawMaps);
|
198
|
-
for (const i in maps) {
|
199
|
-
data = set(data, maps[i], form.get(i));
|
200
|
-
}
|
201
|
-
return deserialize({
|
202
|
-
data,
|
203
|
-
meta
|
204
|
-
});
|
205
|
-
}
|
206
|
-
const json = await re.json();
|
207
|
-
return deserialize(json);
|
208
|
-
} catch (e) {
|
209
|
-
throw new ORPCError({
|
210
|
-
code: "BAD_REQUEST",
|
211
|
-
message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
|
212
|
-
cause: e
|
213
|
-
});
|
214
|
-
}
|
215
|
-
}
|
216
|
-
};
|
217
|
-
|
218
|
-
// src/adapters/fetch/orpc-procedure-matcher.ts
|
219
|
-
import { trim } from "@orpc/shared";
|
220
|
-
var ORPCProcedureMatcher = class {
|
221
|
-
constructor(router) {
|
222
|
-
this.router = router;
|
223
|
-
}
|
224
|
-
async match(pathname) {
|
225
|
-
const path = trim(pathname, "/").split("/").map(decodeURIComponent);
|
226
|
-
const match = getRouterChild(this.router, ...path);
|
227
|
-
const { default: maybeProcedure } = await unlazy(match);
|
228
|
-
if (!isProcedure(maybeProcedure)) {
|
229
|
-
return void 0;
|
230
|
-
}
|
231
|
-
return {
|
232
|
-
procedure: maybeProcedure,
|
233
|
-
path
|
234
|
-
};
|
235
|
-
}
|
236
|
-
};
|
237
|
-
|
238
|
-
// src/adapters/fetch/orpc-handler.ts
|
239
|
-
import { ORPCError as ORPCError2 } from "@orpc/contract";
|
240
|
-
import { executeWithHooks, trim as trim2 } from "@orpc/shared";
|
241
|
-
var RPCHandler = class {
|
242
|
-
constructor(router, options) {
|
243
|
-
this.options = options;
|
244
|
-
this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
|
245
|
-
this.payloadCodec = options?.payloadCodec ?? new ORPCPayloadCodec();
|
246
|
-
}
|
247
|
-
procedureMatcher;
|
248
|
-
payloadCodec;
|
249
|
-
async handle(request, ...[options]) {
|
250
|
-
const context = options?.context;
|
251
|
-
const execute = async () => {
|
252
|
-
const url = new URL(request.url);
|
253
|
-
const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
|
254
|
-
const match = await this.procedureMatcher.match(pathname);
|
255
|
-
if (!match) {
|
256
|
-
return { matched: false, response: void 0 };
|
257
|
-
}
|
258
|
-
const input = await this.payloadCodec.decode(request);
|
259
|
-
const client = createProcedureClient(match.procedure, {
|
260
|
-
context,
|
261
|
-
path: match.path
|
262
|
-
});
|
263
|
-
const output = await client(input, { signal: request.signal });
|
264
|
-
const { body, headers } = this.payloadCodec.encode(output);
|
265
|
-
const response = new Response(body, { headers });
|
266
|
-
return { matched: true, response };
|
267
|
-
};
|
268
|
-
try {
|
269
|
-
const result = await executeWithHooks({
|
270
|
-
context,
|
271
|
-
execute,
|
272
|
-
input: request,
|
273
|
-
hooks: this.options,
|
274
|
-
meta: {
|
275
|
-
signal: request.signal
|
276
|
-
}
|
277
|
-
});
|
278
|
-
return result;
|
279
|
-
} catch (e) {
|
280
|
-
const error = e instanceof ORPCError2 ? e : new ORPCError2({
|
281
|
-
code: "INTERNAL_SERVER_ERROR",
|
282
|
-
message: "Internal server error",
|
283
|
-
cause: e
|
284
|
-
});
|
285
|
-
const { body, headers } = this.payloadCodec.encode(error.toJSON());
|
286
|
-
const response = new Response(body, {
|
287
|
-
headers,
|
288
|
-
status: error.status
|
289
|
-
});
|
290
|
-
return { matched: true, response };
|
291
|
-
}
|
292
|
-
}
|
293
|
-
};
|
294
|
-
|
295
|
-
export {
|
296
|
-
super_json_exports,
|
297
|
-
ORPCPayloadCodec,
|
298
|
-
ORPCProcedureMatcher,
|
299
|
-
RPCHandler
|
300
|
-
};
|
301
|
-
//# sourceMappingURL=chunk-3EVCPLVI.js.map
|