@orpc/server 0.0.0-next.6affcc9 → 0.0.0-next.6c13765
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/dist/{chunk-L4ESXQX7.js → chunk-47YYO5JS.js} +2 -2
- package/dist/chunk-77FU7QSO.js +181 -0
- package/dist/{chunk-XXBE6CYD.js → chunk-MHVECKBC.js} +47 -5
- package/dist/{chunk-XI6WGCB3.js → chunk-WQNNSBXW.js} +6 -11
- package/dist/fetch.js +4 -4
- package/dist/hono.js +4 -4
- package/dist/index.js +3 -2
- package/dist/next.js +4 -4
- package/dist/node.js +3 -3
- package/dist/plugins.js +1 -1
- package/dist/src/adapters/standard/handler.d.ts +9 -7
- package/dist/src/adapters/standard/index.d.ts +0 -1
- package/dist/src/adapters/standard/rpc-codec.d.ts +2 -2
- package/dist/src/adapters/standard/types.d.ts +2 -1
- package/dist/src/builder-variants.d.ts +2 -1
- package/dist/src/builder.d.ts +2 -1
- package/dist/src/error.d.ts +12 -0
- package/dist/src/implementer-procedure.d.ts +3 -1
- package/dist/src/implementer-variants.d.ts +2 -1
- package/dist/src/implementer.d.ts +2 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/middleware-decorated.d.ts +2 -1
- package/dist/src/middleware.d.ts +2 -1
- package/dist/src/plugins/base.d.ts +1 -3
- package/dist/src/procedure-client.d.ts +3 -1
- package/dist/src/procedure-decorated.d.ts +3 -1
- package/dist/src/procedure-utils.d.ts +2 -1
- package/dist/src/procedure.d.ts +2 -1
- package/dist/src/router-client.d.ts +3 -2
- package/dist/standard.js +5 -9
- package/package.json +4 -3
- package/dist/chunk-NXEANHUK.js +0 -351
- package/dist/src/adapters/standard/rpc-serializer.d.ts +0 -22
@@ -2,7 +2,7 @@ import {
|
|
2
2
|
RPCCodec,
|
3
3
|
RPCMatcher,
|
4
4
|
StandardHandler
|
5
|
-
} from "./chunk-
|
5
|
+
} from "./chunk-77FU7QSO.js";
|
6
6
|
|
7
7
|
// src/adapters/fetch/rpc-handler.ts
|
8
8
|
import { toFetchResponse, toStandardRequest } from "@orpc/server-standard-fetch";
|
@@ -29,4 +29,4 @@ var RPCHandler = class {
|
|
29
29
|
export {
|
30
30
|
RPCHandler
|
31
31
|
};
|
32
|
-
//# sourceMappingURL=chunk-
|
32
|
+
//# sourceMappingURL=chunk-47YYO5JS.js.map
|
@@ -0,0 +1,181 @@
|
|
1
|
+
import {
|
2
|
+
convertPathToHttpPath,
|
3
|
+
createContractedProcedure,
|
4
|
+
createProcedureClient,
|
5
|
+
eachContractProcedure,
|
6
|
+
getRouterChild,
|
7
|
+
isProcedure,
|
8
|
+
unlazy
|
9
|
+
} from "./chunk-MHVECKBC.js";
|
10
|
+
import {
|
11
|
+
CompositePlugin
|
12
|
+
} from "./chunk-WQNNSBXW.js";
|
13
|
+
|
14
|
+
// src/adapters/standard/handler.ts
|
15
|
+
import { ORPCError, toORPCError } from "@orpc/client";
|
16
|
+
import { intercept, trim } from "@orpc/shared";
|
17
|
+
var StandardHandler = class {
|
18
|
+
constructor(router, matcher, codec, options = {}) {
|
19
|
+
this.matcher = matcher;
|
20
|
+
this.codec = codec;
|
21
|
+
this.options = options;
|
22
|
+
this.plugin = new CompositePlugin(options.plugins);
|
23
|
+
this.plugin.init(this.options);
|
24
|
+
this.matcher.init(router);
|
25
|
+
}
|
26
|
+
plugin;
|
27
|
+
handle(request, ...[options]) {
|
28
|
+
return intercept(
|
29
|
+
this.options.rootInterceptors ?? [],
|
30
|
+
{
|
31
|
+
request,
|
32
|
+
...options,
|
33
|
+
context: options?.context ?? {}
|
34
|
+
// context is optional only when all fields are optional so we can safely force it to have a context
|
35
|
+
},
|
36
|
+
async (interceptorOptions) => {
|
37
|
+
let isDecoding = false;
|
38
|
+
try {
|
39
|
+
return await intercept(
|
40
|
+
this.options.interceptors ?? [],
|
41
|
+
interceptorOptions,
|
42
|
+
async (interceptorOptions2) => {
|
43
|
+
const method = interceptorOptions2.request.method;
|
44
|
+
const url = interceptorOptions2.request.url;
|
45
|
+
const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
|
46
|
+
const match = await this.matcher.match(method, pathname);
|
47
|
+
if (!match) {
|
48
|
+
return { matched: false, response: void 0 };
|
49
|
+
}
|
50
|
+
const client = createProcedureClient(match.procedure, {
|
51
|
+
context: interceptorOptions2.context,
|
52
|
+
path: match.path,
|
53
|
+
interceptors: this.options.clientInterceptors
|
54
|
+
});
|
55
|
+
isDecoding = true;
|
56
|
+
const input = await this.codec.decode(request, match.params, match.procedure);
|
57
|
+
isDecoding = false;
|
58
|
+
const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
|
59
|
+
const output = await client(input, { signal: request.signal, lastEventId });
|
60
|
+
const response = this.codec.encode(output, match.procedure);
|
61
|
+
return {
|
62
|
+
matched: true,
|
63
|
+
response
|
64
|
+
};
|
65
|
+
}
|
66
|
+
);
|
67
|
+
} catch (e) {
|
68
|
+
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
69
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
70
|
+
cause: e
|
71
|
+
}) : toORPCError(e);
|
72
|
+
const response = this.codec.encodeError(error);
|
73
|
+
return {
|
74
|
+
matched: true,
|
75
|
+
response
|
76
|
+
};
|
77
|
+
}
|
78
|
+
}
|
79
|
+
);
|
80
|
+
}
|
81
|
+
};
|
82
|
+
|
83
|
+
// src/adapters/standard/rpc-codec.ts
|
84
|
+
import { RPCSerializer } from "@orpc/client/rpc";
|
85
|
+
var RPCCodec = class {
|
86
|
+
serializer;
|
87
|
+
constructor(options = {}) {
|
88
|
+
this.serializer = options.serializer ?? new RPCSerializer();
|
89
|
+
}
|
90
|
+
async decode(request, _params, _procedure) {
|
91
|
+
const serialized = request.method === "GET" ? JSON.parse(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
92
|
+
return this.serializer.deserialize(serialized);
|
93
|
+
}
|
94
|
+
encode(output, _procedure) {
|
95
|
+
return {
|
96
|
+
status: 200,
|
97
|
+
headers: {},
|
98
|
+
body: this.serializer.serialize(output)
|
99
|
+
};
|
100
|
+
}
|
101
|
+
encodeError(error) {
|
102
|
+
return {
|
103
|
+
status: error.status,
|
104
|
+
headers: {},
|
105
|
+
body: this.serializer.serialize(error.toJSON())
|
106
|
+
};
|
107
|
+
}
|
108
|
+
};
|
109
|
+
|
110
|
+
// src/adapters/standard/rpc-matcher.ts
|
111
|
+
var RPCMatcher = class {
|
112
|
+
tree = {};
|
113
|
+
pendingRouters = [];
|
114
|
+
init(router, path = []) {
|
115
|
+
const laziedOptions = eachContractProcedure({
|
116
|
+
router,
|
117
|
+
path
|
118
|
+
}, ({ path: path2, contract }) => {
|
119
|
+
const httpPath = convertPathToHttpPath(path2);
|
120
|
+
if (isProcedure(contract)) {
|
121
|
+
this.tree[httpPath] = {
|
122
|
+
path: path2,
|
123
|
+
contract,
|
124
|
+
procedure: contract,
|
125
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
126
|
+
router
|
127
|
+
};
|
128
|
+
} else {
|
129
|
+
this.tree[httpPath] = {
|
130
|
+
path: path2,
|
131
|
+
contract,
|
132
|
+
procedure: void 0,
|
133
|
+
router
|
134
|
+
};
|
135
|
+
}
|
136
|
+
});
|
137
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
138
|
+
...option,
|
139
|
+
httpPathPrefix: convertPathToHttpPath(option.path)
|
140
|
+
})));
|
141
|
+
}
|
142
|
+
async match(_method, pathname) {
|
143
|
+
if (this.pendingRouters.length) {
|
144
|
+
const newPendingRouters = [];
|
145
|
+
for (const pendingRouter of this.pendingRouters) {
|
146
|
+
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
147
|
+
const { default: router } = await unlazy(pendingRouter.lazied);
|
148
|
+
this.init(router, pendingRouter.path);
|
149
|
+
} else {
|
150
|
+
newPendingRouters.push(pendingRouter);
|
151
|
+
}
|
152
|
+
}
|
153
|
+
this.pendingRouters = newPendingRouters;
|
154
|
+
}
|
155
|
+
const match = this.tree[pathname];
|
156
|
+
if (!match) {
|
157
|
+
return void 0;
|
158
|
+
}
|
159
|
+
if (!match.procedure) {
|
160
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
|
161
|
+
if (!isProcedure(maybeProcedure)) {
|
162
|
+
throw new Error(`
|
163
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
|
164
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
165
|
+
`);
|
166
|
+
}
|
167
|
+
match.procedure = createContractedProcedure(match.contract, maybeProcedure);
|
168
|
+
}
|
169
|
+
return {
|
170
|
+
path: match.path,
|
171
|
+
procedure: match.procedure
|
172
|
+
};
|
173
|
+
}
|
174
|
+
};
|
175
|
+
|
176
|
+
export {
|
177
|
+
StandardHandler,
|
178
|
+
RPCCodec,
|
179
|
+
RPCMatcher
|
180
|
+
};
|
181
|
+
//# sourceMappingURL=chunk-77FU7QSO.js.map
|
@@ -62,8 +62,50 @@ function middlewareOutputFn(output) {
|
|
62
62
|
}
|
63
63
|
|
64
64
|
// src/procedure-client.ts
|
65
|
-
import {
|
65
|
+
import { ORPCError as ORPCError2 } from "@orpc/client";
|
66
|
+
import { ValidationError } from "@orpc/contract";
|
66
67
|
import { intercept, toError, value } from "@orpc/shared";
|
68
|
+
|
69
|
+
// src/error.ts
|
70
|
+
import { fallbackORPCErrorStatus, ORPCError } from "@orpc/client";
|
71
|
+
function createORPCErrorConstructorMap(errors) {
|
72
|
+
const proxy = new Proxy(errors, {
|
73
|
+
get(target, code) {
|
74
|
+
if (typeof code !== "string") {
|
75
|
+
return Reflect.get(target, code);
|
76
|
+
}
|
77
|
+
const item = (...[options]) => {
|
78
|
+
const config = errors[code];
|
79
|
+
return new ORPCError(code, {
|
80
|
+
defined: Boolean(config),
|
81
|
+
status: config?.status,
|
82
|
+
message: options?.message ?? config?.message,
|
83
|
+
data: options?.data,
|
84
|
+
cause: options?.cause
|
85
|
+
});
|
86
|
+
};
|
87
|
+
return item;
|
88
|
+
}
|
89
|
+
});
|
90
|
+
return proxy;
|
91
|
+
}
|
92
|
+
async function validateORPCError(map, error) {
|
93
|
+
const { code, status, message, data, cause, defined } = error;
|
94
|
+
const config = map?.[error.code];
|
95
|
+
if (!config || fallbackORPCErrorStatus(error.code, config.status) !== error.status) {
|
96
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
97
|
+
}
|
98
|
+
if (!config.data) {
|
99
|
+
return defined ? error : new ORPCError(code, { defined: true, status, message, data, cause });
|
100
|
+
}
|
101
|
+
const validated = await config.data["~standard"].validate(error.data);
|
102
|
+
if (validated.issues) {
|
103
|
+
return defined ? new ORPCError(code, { defined: false, status, message, data, cause }) : error;
|
104
|
+
}
|
105
|
+
return new ORPCError(code, { defined: true, status, message, data: validated.value, cause });
|
106
|
+
}
|
107
|
+
|
108
|
+
// src/procedure-client.ts
|
67
109
|
function createProcedureClient(lazyableProcedure, ...[options]) {
|
68
110
|
return async (...[input, callerOptions]) => {
|
69
111
|
const path = options?.path ?? [];
|
@@ -87,7 +129,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
87
129
|
(interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
|
88
130
|
);
|
89
131
|
} catch (e) {
|
90
|
-
if (!(e instanceof
|
132
|
+
if (!(e instanceof ORPCError2)) {
|
91
133
|
throw toError(e);
|
92
134
|
}
|
93
135
|
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
@@ -102,7 +144,7 @@ async function validateInput(procedure, input) {
|
|
102
144
|
}
|
103
145
|
const result = await schema["~standard"].validate(input);
|
104
146
|
if (result.issues) {
|
105
|
-
throw new
|
147
|
+
throw new ORPCError2("BAD_REQUEST", {
|
106
148
|
message: "Input validation failed",
|
107
149
|
data: {
|
108
150
|
issues: result.issues
|
@@ -119,7 +161,7 @@ async function validateOutput(procedure, output) {
|
|
119
161
|
}
|
120
162
|
const result = await schema["~standard"].validate(output);
|
121
163
|
if (result.issues) {
|
122
|
-
throw new
|
164
|
+
throw new ORPCError2("INTERNAL_SERVER_ERROR", {
|
123
165
|
message: "Output validation failed",
|
124
166
|
cause: new ValidationError({ message: "Output validation failed", issues: result.issues })
|
125
167
|
});
|
@@ -376,4 +418,4 @@ export {
|
|
376
418
|
convertPathToHttpPath,
|
377
419
|
createContractedProcedure
|
378
420
|
};
|
379
|
-
//# sourceMappingURL=chunk-
|
421
|
+
//# sourceMappingURL=chunk-MHVECKBC.js.map
|
@@ -8,11 +8,6 @@ var CompositePlugin = class {
|
|
8
8
|
plugin.init?.(options);
|
9
9
|
}
|
10
10
|
}
|
11
|
-
beforeCreateProcedureClient(clientOptions, interceptorOptions) {
|
12
|
-
for (const plugin of this.plugins) {
|
13
|
-
plugin.beforeCreateProcedureClient?.(clientOptions, interceptorOptions);
|
14
|
-
}
|
15
|
-
}
|
16
11
|
};
|
17
12
|
|
18
13
|
// src/plugins/cors.ts
|
@@ -30,8 +25,8 @@ var CORSPlugin = class {
|
|
30
25
|
};
|
31
26
|
}
|
32
27
|
init(options) {
|
33
|
-
options.
|
34
|
-
options.
|
28
|
+
options.rootInterceptors ??= [];
|
29
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
35
30
|
if (interceptorOptions.request.method === "OPTIONS") {
|
36
31
|
const resHeaders = {};
|
37
32
|
if (this.options.maxAge !== void 0) {
|
@@ -57,7 +52,7 @@ var CORSPlugin = class {
|
|
57
52
|
}
|
58
53
|
return interceptorOptions.next();
|
59
54
|
});
|
60
|
-
options.
|
55
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
61
56
|
const result = await interceptorOptions.next();
|
62
57
|
if (!result.matched) {
|
63
58
|
return result;
|
@@ -94,8 +89,8 @@ var CORSPlugin = class {
|
|
94
89
|
// src/plugins/response-headers.ts
|
95
90
|
var ResponseHeadersPlugin = class {
|
96
91
|
init(options) {
|
97
|
-
options.
|
98
|
-
options.
|
92
|
+
options.rootInterceptors ??= [];
|
93
|
+
options.rootInterceptors.push(async (interceptorOptions) => {
|
99
94
|
const headers = new Headers();
|
100
95
|
interceptorOptions.context.resHeaders = headers;
|
101
96
|
const result = await interceptorOptions.next();
|
@@ -122,4 +117,4 @@ export {
|
|
122
117
|
CORSPlugin,
|
123
118
|
ResponseHeadersPlugin
|
124
119
|
};
|
125
|
-
//# sourceMappingURL=chunk-
|
120
|
+
//# sourceMappingURL=chunk-WQNNSBXW.js.map
|
package/dist/fetch.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import {
|
2
2
|
RPCHandler
|
3
|
-
} from "./chunk-
|
4
|
-
import "./chunk-
|
5
|
-
import "./chunk-
|
6
|
-
import "./chunk-
|
3
|
+
} from "./chunk-47YYO5JS.js";
|
4
|
+
import "./chunk-77FU7QSO.js";
|
5
|
+
import "./chunk-MHVECKBC.js";
|
6
|
+
import "./chunk-WQNNSBXW.js";
|
7
7
|
export {
|
8
8
|
RPCHandler
|
9
9
|
};
|
package/dist/hono.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import {
|
2
2
|
RPCHandler
|
3
|
-
} from "./chunk-
|
4
|
-
import "./chunk-
|
5
|
-
import "./chunk-
|
6
|
-
import "./chunk-
|
3
|
+
} from "./chunk-47YYO5JS.js";
|
4
|
+
import "./chunk-77FU7QSO.js";
|
5
|
+
import "./chunk-MHVECKBC.js";
|
6
|
+
import "./chunk-WQNNSBXW.js";
|
7
7
|
|
8
8
|
// src/adapters/hono/middleware.ts
|
9
9
|
import { value } from "@orpc/shared";
|
package/dist/index.js
CHANGED
@@ -21,7 +21,7 @@ import {
|
|
21
21
|
middlewareOutputFn,
|
22
22
|
setRouterContract,
|
23
23
|
unlazy
|
24
|
-
} from "./chunk-
|
24
|
+
} from "./chunk-MHVECKBC.js";
|
25
25
|
|
26
26
|
// src/builder.ts
|
27
27
|
import { mergeErrorMap as mergeErrorMap2, mergeMeta as mergeMeta2, mergePrefix, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
|
@@ -361,7 +361,8 @@ function createRouterClient(router, ...rest) {
|
|
361
361
|
}
|
362
362
|
|
363
363
|
// src/index.ts
|
364
|
-
import {
|
364
|
+
import { isDefinedError, ORPCError, safe } from "@orpc/client";
|
365
|
+
import { eventIterator, type, ValidationError } from "@orpc/contract";
|
365
366
|
import { getEventMeta, withEventMeta } from "@orpc/server-standard";
|
366
367
|
import { onError, onFinish, onStart, onSuccess } from "@orpc/shared";
|
367
368
|
export {
|
package/dist/next.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
import {
|
2
2
|
RPCHandler
|
3
|
-
} from "./chunk-
|
4
|
-
import "./chunk-
|
5
|
-
import "./chunk-
|
6
|
-
import "./chunk-
|
3
|
+
} from "./chunk-47YYO5JS.js";
|
4
|
+
import "./chunk-77FU7QSO.js";
|
5
|
+
import "./chunk-MHVECKBC.js";
|
6
|
+
import "./chunk-WQNNSBXW.js";
|
7
7
|
|
8
8
|
// src/adapters/next/serve.ts
|
9
9
|
import { value } from "@orpc/shared";
|
package/dist/node.js
CHANGED
@@ -2,9 +2,9 @@ import {
|
|
2
2
|
RPCCodec,
|
3
3
|
RPCMatcher,
|
4
4
|
StandardHandler
|
5
|
-
} from "./chunk-
|
6
|
-
import "./chunk-
|
7
|
-
import "./chunk-
|
5
|
+
} from "./chunk-77FU7QSO.js";
|
6
|
+
import "./chunk-MHVECKBC.js";
|
7
|
+
import "./chunk-WQNNSBXW.js";
|
8
8
|
|
9
9
|
// src/adapters/node/rpc-handler.ts
|
10
10
|
import { sendStandardResponse, toStandardRequest } from "@orpc/server-standard-node";
|
package/dist/plugins.js
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
import type {
|
1
|
+
import type { ErrorFromErrorMap, HTTPPath, Meta, Schema, SchemaOutput } from '@orpc/contract';
|
2
2
|
import type { StandardRequest, StandardResponse } from '@orpc/server-standard';
|
3
3
|
import type { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
|
4
4
|
import type { Context } from '../../context';
|
5
5
|
import type { Plugin } from '../../plugins';
|
6
|
-
import type {
|
6
|
+
import type { ProcedureClientInterceptorOptions } from '../../procedure-client';
|
7
7
|
import type { Router } from '../../router';
|
8
8
|
import type { StandardCodec, StandardMatcher } from './types';
|
9
9
|
export type StandardHandleOptions<T extends Context> = {
|
@@ -26,9 +26,6 @@ export type StandardHandleResult = {
|
|
26
26
|
export type StandardHandlerInterceptorOptions<TContext extends Context> = WellStandardHandleOptions<TContext> & {
|
27
27
|
request: StandardRequest;
|
28
28
|
};
|
29
|
-
export type WellCreateProcedureClientOptions<TContext extends Context> = CreateProcedureClientOptions<TContext, Schema, Schema, unknown, ErrorMap, Meta, Record<never, never>> & {
|
30
|
-
context: TContext;
|
31
|
-
};
|
32
29
|
export interface StandardHandlerOptions<TContext extends Context> {
|
33
30
|
plugins?: Plugin<TContext>[];
|
34
31
|
/**
|
@@ -36,9 +33,14 @@ export interface StandardHandlerOptions<TContext extends Context> {
|
|
36
33
|
*/
|
37
34
|
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
|
38
35
|
/**
|
39
|
-
* Interceptors at the root level, helpful when you want override the response
|
36
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
37
|
+
*/
|
38
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
|
39
|
+
/**
|
40
|
+
*
|
41
|
+
* Interceptors for procedure client.
|
40
42
|
*/
|
41
|
-
|
43
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Schema, Record<never, never>, Meta>, SchemaOutput<Schema, unknown>, ErrorFromErrorMap<Record<never, never>>>[];
|
42
44
|
}
|
43
45
|
export declare class StandardHandler<T extends Context> {
|
44
46
|
private readonly matcher;
|
@@ -1,8 +1,8 @@
|
|
1
|
-
import type { ORPCError } from '@orpc/
|
1
|
+
import type { ORPCError } from '@orpc/client';
|
2
2
|
import type { StandardRequest, StandardResponse } from '@orpc/server-standard';
|
3
3
|
import type { AnyProcedure } from '../../procedure';
|
4
4
|
import type { StandardCodec, StandardParams } from './types';
|
5
|
-
import { RPCSerializer } from '
|
5
|
+
import { RPCSerializer } from '@orpc/client/rpc';
|
6
6
|
export interface StandardCodecOptions {
|
7
7
|
serializer?: RPCSerializer;
|
8
8
|
}
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import type {
|
1
|
+
import type { ORPCError } from '@orpc/client';
|
2
|
+
import type { HTTPPath } from '@orpc/contract';
|
2
3
|
import type { StandardRequest, StandardResponse } from '@orpc/server-standard';
|
3
4
|
import type { AnyProcedure } from '../../procedure';
|
4
5
|
import type { AnyRouter } from '../../router';
|
@@ -1,6 +1,7 @@
|
|
1
|
-
import type { ContractRouter, ErrorMap, HTTPPath, MergedErrorMap, Meta,
|
1
|
+
import type { ContractRouter, ErrorMap, HTTPPath, MergedErrorMap, Meta, Route, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
2
|
import type { BuilderDef } from './builder';
|
3
3
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
4
|
+
import type { ORPCErrorConstructorMap } from './error';
|
4
5
|
import type { FlattenLazy } from './lazy-utils';
|
5
6
|
import type { MapInputMiddleware, Middleware } from './middleware';
|
6
7
|
import type { ProcedureHandler } from './procedure';
|
package/dist/src/builder.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
import type { ContractProcedureDef, ContractRouter, ErrorMap, HTTPPath, MergedErrorMap, Meta,
|
1
|
+
import type { ContractProcedureDef, ContractRouter, ErrorMap, HTTPPath, MergedErrorMap, Meta, Route, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
2
|
import type { BuilderWithMiddlewares, ProcedureBuilder, ProcedureBuilderWithInput, ProcedureBuilderWithOutput, RouterBuilder } from './builder-variants';
|
3
3
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
4
|
+
import type { ORPCErrorConstructorMap } from './error';
|
4
5
|
import type { FlattenLazy } from './lazy-utils';
|
5
6
|
import type { AnyMiddleware, MapInputMiddleware, Middleware } from './middleware';
|
6
7
|
import type { DecoratedMiddleware } from './middleware-decorated';
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import type { ORPCErrorCode, ORPCErrorOptions } from '@orpc/client';
|
2
|
+
import type { ErrorMap, ErrorMapItem, SchemaInput } from '@orpc/contract';
|
3
|
+
import type { MaybeOptionalOptions } from '@orpc/shared';
|
4
|
+
import { ORPCError } from '@orpc/client';
|
5
|
+
export type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
6
|
+
export type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
7
|
+
export type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
8
|
+
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, SchemaInput<UInputSchema>> : never : never;
|
9
|
+
};
|
10
|
+
export declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
11
|
+
export declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
12
|
+
//# sourceMappingURL=error.d.ts.map
|
@@ -1,7 +1,9 @@
|
|
1
|
-
import type { ClientContext, ClientRest
|
1
|
+
import type { ClientContext, ClientRest } from '@orpc/client';
|
2
|
+
import type { ErrorMap, Meta, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
3
|
import type { MaybeOptionalOptions } from '@orpc/shared';
|
3
4
|
import type { BuilderDef } from './builder';
|
4
5
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
6
|
+
import type { ORPCErrorConstructorMap } from './error';
|
5
7
|
import type { MapInputMiddleware, Middleware } from './middleware';
|
6
8
|
import type { Procedure, ProcedureHandler } from './procedure';
|
7
9
|
import type { CreateProcedureClientOptions, ProcedureClient } from './procedure-client';
|
@@ -1,5 +1,6 @@
|
|
1
|
-
import type { AnyContractRouter, ContractProcedure, ContractRouterToErrorMap, ContractRouterToMeta
|
1
|
+
import type { AnyContractRouter, ContractProcedure, ContractRouterToErrorMap, ContractRouterToMeta } from '@orpc/contract';
|
2
2
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
3
|
+
import type { ORPCErrorConstructorMap } from './error';
|
3
4
|
import type { ProcedureImplementer } from './implementer-procedure';
|
4
5
|
import type { FlattenLazy } from './lazy-utils';
|
5
6
|
import type { Middleware } from './middleware';
|
@@ -1,5 +1,6 @@
|
|
1
|
-
import type { AnyContractRouter, ContractProcedure, ContractRouterToErrorMap, ContractRouterToMeta
|
1
|
+
import type { AnyContractRouter, ContractProcedure, ContractRouterToErrorMap, ContractRouterToMeta } from '@orpc/contract';
|
2
2
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
3
|
+
import type { ORPCErrorConstructorMap } from './error';
|
3
4
|
import type { ProcedureImplementer } from './implementer-procedure';
|
4
5
|
import type { ImplementerInternalWithMiddlewares } from './implementer-variants';
|
5
6
|
import type { AnyMiddleware, Middleware } from './middleware';
|
package/dist/src/index.d.ts
CHANGED
@@ -18,7 +18,8 @@ export * from './router';
|
|
18
18
|
export * from './router-accessible-lazy';
|
19
19
|
export * from './router-client';
|
20
20
|
export * from './utils';
|
21
|
-
export {
|
21
|
+
export { isDefinedError, ORPCError, safe } from '@orpc/client';
|
22
|
+
export { eventIterator, type, ValidationError } from '@orpc/contract';
|
22
23
|
export { getEventMeta, withEventMeta } from '@orpc/server-standard';
|
23
24
|
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
24
25
|
//# sourceMappingURL=index.d.ts.map
|
@@ -1,5 +1,6 @@
|
|
1
|
-
import type { Meta
|
1
|
+
import type { Meta } from '@orpc/contract';
|
2
2
|
import type { Context, MergedContext } from './context';
|
3
|
+
import type { ORPCErrorConstructorMap } from './error';
|
3
4
|
import type { MapInputMiddleware, Middleware } from './middleware';
|
4
5
|
export interface DecoratedMiddleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> extends Middleware<TInContext, TOutContext, TInput, TOutput, TErrorConstructorMap, TMeta> {
|
5
6
|
concat<UOutContext extends Context, UInput>(middleware: Middleware<TInContext & TOutContext, UOutContext, UInput & TInput, TOutput, TErrorConstructorMap, TMeta>): DecoratedMiddleware<TInContext, MergedContext<TOutContext, UOutContext>, UInput & TInput, TOutput, TErrorConstructorMap, TMeta>;
|
package/dist/src/middleware.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
import type { ErrorMap, Meta,
|
1
|
+
import type { ErrorMap, Meta, Schema } from '@orpc/contract';
|
2
2
|
import type { MaybeOptionalOptions, Promisable } from '@orpc/shared';
|
3
3
|
import type { Context } from './context';
|
4
|
+
import type { ORPCErrorConstructorMap } from './error';
|
4
5
|
import type { Procedure } from './procedure';
|
5
6
|
export type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
6
7
|
output: TOutput;
|
@@ -1,13 +1,11 @@
|
|
1
|
-
import type {
|
1
|
+
import type { StandardHandlerOptions } from '../adapters/standard';
|
2
2
|
import type { Context } from '../context';
|
3
3
|
export interface Plugin<TContext extends Context> {
|
4
4
|
init?(options: StandardHandlerOptions<TContext>): void;
|
5
|
-
beforeCreateProcedureClient?(clientOptions: WellCreateProcedureClientOptions<TContext>, interceptorOptions: StandardHandlerInterceptorOptions<TContext>): void;
|
6
5
|
}
|
7
6
|
export declare class CompositePlugin<TContext extends Context> implements Plugin<TContext> {
|
8
7
|
private readonly plugins;
|
9
8
|
constructor(plugins?: Plugin<TContext>[]);
|
10
9
|
init(options: StandardHandlerOptions<TContext>): void;
|
11
|
-
beforeCreateProcedureClient(clientOptions: WellCreateProcedureClientOptions<TContext>, interceptorOptions: StandardHandlerInterceptorOptions<TContext>): void;
|
12
10
|
}
|
13
11
|
//# sourceMappingURL=base.d.ts.map
|
@@ -1,8 +1,10 @@
|
|
1
|
-
import type { Client, ClientContext
|
1
|
+
import type { Client, ClientContext } from '@orpc/client';
|
2
2
|
import type { Interceptor, MaybeOptionalOptions, Value } from '@orpc/shared';
|
3
3
|
import type { Context } from './context';
|
4
4
|
import type { Lazyable } from './lazy';
|
5
5
|
import type { Procedure } from './procedure';
|
6
|
+
import { type ErrorFromErrorMap, type ErrorMap, type Meta, type Schema, type SchemaInput, type SchemaOutput } from '@orpc/contract';
|
7
|
+
import { type ORPCErrorConstructorMap } from './error';
|
6
8
|
export type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends Schema, TOutputSchema extends Schema, THandlerOutput extends SchemaInput<TOutputSchema>, TErrorMap extends ErrorMap> = Client<TClientContext, SchemaInput<TInputSchema>, SchemaOutput<TOutputSchema, THandlerOutput>, ErrorFromErrorMap<TErrorMap>>;
|
7
9
|
export interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TInputSchema extends Schema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
8
10
|
context: TInitialContext;
|
@@ -1,6 +1,8 @@
|
|
1
|
-
import type { ClientContext, ClientRest
|
1
|
+
import type { ClientContext, ClientRest } from '@orpc/client';
|
2
|
+
import type { ErrorMap, MergedErrorMap, Meta, Route, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
3
|
import type { MaybeOptionalOptions } from '@orpc/shared';
|
3
4
|
import type { ConflictContextGuard, Context, MergedContext } from './context';
|
5
|
+
import type { ORPCErrorConstructorMap } from './error';
|
4
6
|
import type { MapInputMiddleware, Middleware } from './middleware';
|
5
7
|
import type { CreateProcedureClientOptions, ProcedureClient } from './procedure-client';
|
6
8
|
import { Procedure } from './procedure';
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import type { ClientPromiseResult
|
1
|
+
import type { ClientPromiseResult } from '@orpc/client';
|
2
|
+
import type { ErrorFromErrorMap, ErrorMap, Meta, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
3
|
import type { MaybeOptionalOptions } from '@orpc/shared';
|
3
4
|
import type { Context } from './context';
|
4
5
|
import type { Lazyable } from './lazy';
|
package/dist/src/procedure.d.ts
CHANGED
@@ -1,6 +1,7 @@
|
|
1
|
-
import type { ContractProcedureDef, ErrorMap, Meta,
|
1
|
+
import type { ContractProcedureDef, ErrorMap, Meta, Schema, SchemaInput, SchemaOutput } from '@orpc/contract';
|
2
2
|
import type { Promisable } from '@orpc/shared';
|
3
3
|
import type { Context } from './context';
|
4
|
+
import type { ORPCErrorConstructorMap } from './error';
|
4
5
|
import type { AnyMiddleware } from './middleware';
|
5
6
|
export interface ProcedureHandlerOptions<TCurrentContext extends Context, TInput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
6
7
|
context: TCurrentContext;
|
@@ -1,10 +1,11 @@
|
|
1
|
-
import type { ClientContext
|
1
|
+
import type { ClientContext } from '@orpc/client';
|
2
|
+
import type { ErrorMap, Meta } from '@orpc/contract';
|
2
3
|
import type { MaybeOptionalOptions } from '@orpc/shared';
|
3
4
|
import type { Lazy } from './lazy';
|
4
5
|
import type { Procedure } from './procedure';
|
5
6
|
import type { CreateProcedureClientOptions, ProcedureClient } from './procedure-client';
|
6
7
|
import type { AnyRouter, InferRouterInitialContext } from './router';
|
7
|
-
export type RouterClient<TRouter extends AnyRouter, TClientContext extends ClientContext
|
8
|
+
export type RouterClient<TRouter extends AnyRouter, TClientContext extends ClientContext = Record<never, never>> = TRouter extends Lazy<infer U extends AnyRouter> ? RouterClient<U, TClientContext> : TRouter extends Procedure<any, any, infer UInputSchema, infer UOutputSchema, infer UFuncOutput, infer UErrorMap, any> ? ProcedureClient<TClientContext, UInputSchema, UOutputSchema, UFuncOutput, UErrorMap> : {
|
8
9
|
[K in keyof TRouter]: TRouter[K] extends AnyRouter ? RouterClient<TRouter[K], TClientContext> : never;
|
9
10
|
};
|
10
11
|
export declare function createRouterClient<TRouter extends AnyRouter, TClientContext extends ClientContext>(router: TRouter | Lazy<undefined>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<InferRouterInitialContext<TRouter>, undefined, undefined, unknown, ErrorMap, Meta, TClientContext>>): RouterClient<TRouter, TClientContext>;
|
package/dist/standard.js
CHANGED
@@ -1,17 +1,13 @@
|
|
1
1
|
import {
|
2
2
|
RPCCodec,
|
3
3
|
RPCMatcher,
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
import "./chunk-XXBE6CYD.js";
|
9
|
-
import "./chunk-XI6WGCB3.js";
|
4
|
+
StandardHandler
|
5
|
+
} from "./chunk-77FU7QSO.js";
|
6
|
+
import "./chunk-MHVECKBC.js";
|
7
|
+
import "./chunk-WQNNSBXW.js";
|
10
8
|
export {
|
11
9
|
RPCCodec,
|
12
10
|
RPCMatcher,
|
13
|
-
|
14
|
-
StandardHandler,
|
15
|
-
serializeRPCJson
|
11
|
+
StandardHandler
|
16
12
|
};
|
17
13
|
//# sourceMappingURL=standard.js.map
|
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.6c13765",
|
5
5
|
"license": "MIT",
|
6
6
|
"homepage": "https://orpc.unnoq.com",
|
7
7
|
"repository": {
|
@@ -66,8 +66,9 @@
|
|
66
66
|
"@orpc/server-standard": "^0.4.0",
|
67
67
|
"@orpc/server-standard-fetch": "^0.4.0",
|
68
68
|
"@orpc/server-standard-node": "^0.4.0",
|
69
|
-
"@orpc/
|
70
|
-
"@orpc/
|
69
|
+
"@orpc/client": "0.0.0-next.6c13765",
|
70
|
+
"@orpc/contract": "0.0.0-next.6c13765",
|
71
|
+
"@orpc/shared": "0.0.0-next.6c13765"
|
71
72
|
},
|
72
73
|
"devDependencies": {
|
73
74
|
"light-my-request": "^6.5.1"
|
package/dist/chunk-NXEANHUK.js
DELETED
@@ -1,351 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
convertPathToHttpPath,
|
3
|
-
createContractedProcedure,
|
4
|
-
createProcedureClient,
|
5
|
-
eachContractProcedure,
|
6
|
-
getRouterChild,
|
7
|
-
isProcedure,
|
8
|
-
unlazy
|
9
|
-
} from "./chunk-XXBE6CYD.js";
|
10
|
-
import {
|
11
|
-
CompositePlugin
|
12
|
-
} from "./chunk-XI6WGCB3.js";
|
13
|
-
|
14
|
-
// src/adapters/standard/handler.ts
|
15
|
-
import { ORPCError, toORPCError } from "@orpc/contract";
|
16
|
-
import { intercept, trim } from "@orpc/shared";
|
17
|
-
var StandardHandler = class {
|
18
|
-
constructor(router, matcher, codec, options = {}) {
|
19
|
-
this.matcher = matcher;
|
20
|
-
this.codec = codec;
|
21
|
-
this.options = options;
|
22
|
-
this.plugin = new CompositePlugin(options?.plugins);
|
23
|
-
this.plugin.init(this.options);
|
24
|
-
this.matcher.init(router);
|
25
|
-
}
|
26
|
-
plugin;
|
27
|
-
handle(request, ...[options]) {
|
28
|
-
return intercept(
|
29
|
-
this.options.interceptorsRoot ?? [],
|
30
|
-
{
|
31
|
-
request,
|
32
|
-
...options,
|
33
|
-
context: options?.context ?? {}
|
34
|
-
// context is optional only when all fields are optional so we can safely force it to have a context
|
35
|
-
},
|
36
|
-
async (interceptorOptions) => {
|
37
|
-
let isDecoding = false;
|
38
|
-
try {
|
39
|
-
return await intercept(
|
40
|
-
this.options.interceptors ?? [],
|
41
|
-
interceptorOptions,
|
42
|
-
async (interceptorOptions2) => {
|
43
|
-
const method = interceptorOptions2.request.method;
|
44
|
-
const url = interceptorOptions2.request.url;
|
45
|
-
const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
|
46
|
-
const match = await this.matcher.match(method, pathname);
|
47
|
-
if (!match) {
|
48
|
-
return { matched: false, response: void 0 };
|
49
|
-
}
|
50
|
-
const clientOptions = {
|
51
|
-
context: interceptorOptions2.context,
|
52
|
-
path: match.path
|
53
|
-
};
|
54
|
-
this.plugin.beforeCreateProcedureClient(clientOptions, interceptorOptions2);
|
55
|
-
const client = createProcedureClient(match.procedure, clientOptions);
|
56
|
-
isDecoding = true;
|
57
|
-
const input = await this.codec.decode(request, match.params, match.procedure);
|
58
|
-
isDecoding = false;
|
59
|
-
const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
|
60
|
-
const output = await client(input, { signal: request.signal, lastEventId });
|
61
|
-
const response = this.codec.encode(output, match.procedure);
|
62
|
-
return {
|
63
|
-
matched: true,
|
64
|
-
response
|
65
|
-
};
|
66
|
-
}
|
67
|
-
);
|
68
|
-
} catch (e) {
|
69
|
-
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
70
|
-
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
71
|
-
cause: e
|
72
|
-
}) : toORPCError(e);
|
73
|
-
const response = this.codec.encodeError(error);
|
74
|
-
return {
|
75
|
-
matched: true,
|
76
|
-
response
|
77
|
-
};
|
78
|
-
}
|
79
|
-
}
|
80
|
-
);
|
81
|
-
}
|
82
|
-
};
|
83
|
-
|
84
|
-
// src/adapters/standard/rpc-serializer.ts
|
85
|
-
import { mapEventIterator, ORPCError as ORPCError2, toORPCError as toORPCError2 } from "@orpc/contract";
|
86
|
-
import { ErrorEvent, isAsyncIteratorObject } from "@orpc/server-standard";
|
87
|
-
import { findDeepMatches, isObject, set } from "@orpc/shared";
|
88
|
-
var RPCSerializer = class {
|
89
|
-
serialize(data) {
|
90
|
-
if (isAsyncIteratorObject(data)) {
|
91
|
-
return mapEventIterator(data, {
|
92
|
-
value: async (value) => serializeRPCJson(value),
|
93
|
-
error: async (e) => {
|
94
|
-
if (e instanceof ErrorEvent) {
|
95
|
-
return new ErrorEvent({
|
96
|
-
data: serializeRPCJson(e.data),
|
97
|
-
cause: e
|
98
|
-
});
|
99
|
-
}
|
100
|
-
return new ErrorEvent({
|
101
|
-
data: serializeRPCJson(toORPCError2(e).toJSON()),
|
102
|
-
cause: e
|
103
|
-
});
|
104
|
-
}
|
105
|
-
});
|
106
|
-
}
|
107
|
-
const serializedJSON = serializeRPCJson(data);
|
108
|
-
const { maps, values: blobs } = findDeepMatches((v) => v instanceof Blob, serializedJSON.json);
|
109
|
-
if (blobs.length === 0) {
|
110
|
-
return serializedJSON;
|
111
|
-
}
|
112
|
-
const form = new FormData();
|
113
|
-
form.set("data", JSON.stringify(serializedJSON));
|
114
|
-
form.set("maps", JSON.stringify(maps));
|
115
|
-
for (const i in blobs) {
|
116
|
-
form.set(i, blobs[i]);
|
117
|
-
}
|
118
|
-
return form;
|
119
|
-
}
|
120
|
-
deserialize(serialized) {
|
121
|
-
if (isAsyncIteratorObject(serialized)) {
|
122
|
-
return mapEventIterator(serialized, {
|
123
|
-
value: async (value) => deserializeRPCJson(value),
|
124
|
-
error: async (e) => {
|
125
|
-
if (!(e instanceof ErrorEvent)) {
|
126
|
-
return e;
|
127
|
-
}
|
128
|
-
const deserialized = deserializeRPCJson(e.data);
|
129
|
-
if (ORPCError2.isValidJSON(deserialized)) {
|
130
|
-
return ORPCError2.fromJSON(deserialized, { cause: e });
|
131
|
-
}
|
132
|
-
return new ErrorEvent({
|
133
|
-
data: deserialized,
|
134
|
-
cause: e
|
135
|
-
});
|
136
|
-
}
|
137
|
-
});
|
138
|
-
}
|
139
|
-
if (!(serialized instanceof FormData)) {
|
140
|
-
return deserializeRPCJson(serialized);
|
141
|
-
}
|
142
|
-
const data = JSON.parse(serialized.get("data"));
|
143
|
-
const maps = JSON.parse(serialized.get("maps"));
|
144
|
-
for (const i in maps) {
|
145
|
-
data.json = set(data.json, maps[i], serialized.get(i));
|
146
|
-
}
|
147
|
-
return deserializeRPCJson(data);
|
148
|
-
}
|
149
|
-
};
|
150
|
-
function serializeRPCJson(value, segments = [], meta = []) {
|
151
|
-
if (typeof value === "bigint") {
|
152
|
-
meta.push(["bigint", segments]);
|
153
|
-
return { json: value.toString(), meta };
|
154
|
-
}
|
155
|
-
if (value instanceof Date) {
|
156
|
-
meta.push(["date", segments]);
|
157
|
-
const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
|
158
|
-
return { json: data, meta };
|
159
|
-
}
|
160
|
-
if (Number.isNaN(value)) {
|
161
|
-
meta.push(["nan", segments]);
|
162
|
-
return { json: "NaN", meta };
|
163
|
-
}
|
164
|
-
if (value instanceof RegExp) {
|
165
|
-
meta.push(["regexp", segments]);
|
166
|
-
return { json: value.toString(), meta };
|
167
|
-
}
|
168
|
-
if (value instanceof URL) {
|
169
|
-
meta.push(["url", segments]);
|
170
|
-
return { json: value.toString(), meta };
|
171
|
-
}
|
172
|
-
if (isObject(value)) {
|
173
|
-
const json = {};
|
174
|
-
for (const k in value) {
|
175
|
-
json[k] = serializeRPCJson(value[k], [...segments, k], meta).json;
|
176
|
-
}
|
177
|
-
return { json, meta };
|
178
|
-
}
|
179
|
-
if (Array.isArray(value)) {
|
180
|
-
const json = value.map((v, i) => {
|
181
|
-
if (v === void 0) {
|
182
|
-
meta.push(["undefined", [...segments, i]]);
|
183
|
-
return null;
|
184
|
-
}
|
185
|
-
return serializeRPCJson(v, [...segments, i], meta).json;
|
186
|
-
});
|
187
|
-
return { json, meta };
|
188
|
-
}
|
189
|
-
if (value instanceof Set) {
|
190
|
-
const result = serializeRPCJson(Array.from(value), segments, meta);
|
191
|
-
meta.push(["set", segments]);
|
192
|
-
return result;
|
193
|
-
}
|
194
|
-
if (value instanceof Map) {
|
195
|
-
const result = serializeRPCJson(Array.from(value.entries()), segments, meta);
|
196
|
-
meta.push(["map", segments]);
|
197
|
-
return result;
|
198
|
-
}
|
199
|
-
return { json: value, meta };
|
200
|
-
}
|
201
|
-
function deserializeRPCJson({
|
202
|
-
json,
|
203
|
-
meta
|
204
|
-
}) {
|
205
|
-
if (meta.length === 0) {
|
206
|
-
return json;
|
207
|
-
}
|
208
|
-
const ref = { data: json };
|
209
|
-
for (const [type, segments] of meta) {
|
210
|
-
let currentRef = ref;
|
211
|
-
let preSegment = "data";
|
212
|
-
for (let i = 0; i < segments.length; i++) {
|
213
|
-
currentRef = currentRef[preSegment];
|
214
|
-
preSegment = segments[i];
|
215
|
-
}
|
216
|
-
switch (type) {
|
217
|
-
case "nan":
|
218
|
-
currentRef[preSegment] = Number.NaN;
|
219
|
-
break;
|
220
|
-
case "bigint":
|
221
|
-
currentRef[preSegment] = BigInt(currentRef[preSegment]);
|
222
|
-
break;
|
223
|
-
case "date":
|
224
|
-
currentRef[preSegment] = new Date(currentRef[preSegment]);
|
225
|
-
break;
|
226
|
-
case "regexp": {
|
227
|
-
const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
|
228
|
-
currentRef[preSegment] = new RegExp(pattern, flags);
|
229
|
-
break;
|
230
|
-
}
|
231
|
-
case "url":
|
232
|
-
currentRef[preSegment] = new URL(currentRef[preSegment]);
|
233
|
-
break;
|
234
|
-
case "undefined":
|
235
|
-
currentRef[preSegment] = void 0;
|
236
|
-
break;
|
237
|
-
case "map":
|
238
|
-
currentRef[preSegment] = new Map(currentRef[preSegment]);
|
239
|
-
break;
|
240
|
-
case "set":
|
241
|
-
currentRef[preSegment] = new Set(currentRef[preSegment]);
|
242
|
-
break;
|
243
|
-
/* v8 ignore next 3 */
|
244
|
-
default: {
|
245
|
-
const _expected = type;
|
246
|
-
}
|
247
|
-
}
|
248
|
-
}
|
249
|
-
return ref.data;
|
250
|
-
}
|
251
|
-
|
252
|
-
// src/adapters/standard/rpc-codec.ts
|
253
|
-
var RPCCodec = class {
|
254
|
-
serializer;
|
255
|
-
constructor(options = {}) {
|
256
|
-
this.serializer = options.serializer ?? new RPCSerializer();
|
257
|
-
}
|
258
|
-
async decode(request, _params, _procedure) {
|
259
|
-
const serialized = request.method === "GET" ? JSON.parse(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
260
|
-
return this.serializer.deserialize(serialized);
|
261
|
-
}
|
262
|
-
encode(output, _procedure) {
|
263
|
-
return {
|
264
|
-
status: 200,
|
265
|
-
headers: {},
|
266
|
-
body: this.serializer.serialize(output)
|
267
|
-
};
|
268
|
-
}
|
269
|
-
encodeError(error) {
|
270
|
-
return {
|
271
|
-
status: error.status,
|
272
|
-
headers: {},
|
273
|
-
body: this.serializer.serialize(error.toJSON())
|
274
|
-
};
|
275
|
-
}
|
276
|
-
};
|
277
|
-
|
278
|
-
// src/adapters/standard/rpc-matcher.ts
|
279
|
-
var RPCMatcher = class {
|
280
|
-
tree = {};
|
281
|
-
pendingRouters = [];
|
282
|
-
init(router, path = []) {
|
283
|
-
const laziedOptions = eachContractProcedure({
|
284
|
-
router,
|
285
|
-
path
|
286
|
-
}, ({ path: path2, contract }) => {
|
287
|
-
const httpPath = convertPathToHttpPath(path2);
|
288
|
-
if (isProcedure(contract)) {
|
289
|
-
this.tree[httpPath] = {
|
290
|
-
path: path2,
|
291
|
-
contract,
|
292
|
-
procedure: contract,
|
293
|
-
// this mean dev not used contract-first so we can used contract as procedure directly
|
294
|
-
router
|
295
|
-
};
|
296
|
-
} else {
|
297
|
-
this.tree[httpPath] = {
|
298
|
-
path: path2,
|
299
|
-
contract,
|
300
|
-
procedure: void 0,
|
301
|
-
router
|
302
|
-
};
|
303
|
-
}
|
304
|
-
});
|
305
|
-
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
306
|
-
...option,
|
307
|
-
httpPathPrefix: convertPathToHttpPath(option.path)
|
308
|
-
})));
|
309
|
-
}
|
310
|
-
async match(_method, pathname) {
|
311
|
-
if (this.pendingRouters.length) {
|
312
|
-
const newPendingRouters = [];
|
313
|
-
for (const pendingRouter of this.pendingRouters) {
|
314
|
-
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
315
|
-
const { default: router } = await unlazy(pendingRouter.lazied);
|
316
|
-
this.init(router, pendingRouter.path);
|
317
|
-
} else {
|
318
|
-
newPendingRouters.push(pendingRouter);
|
319
|
-
}
|
320
|
-
}
|
321
|
-
this.pendingRouters = newPendingRouters;
|
322
|
-
}
|
323
|
-
const match = this.tree[pathname];
|
324
|
-
if (!match) {
|
325
|
-
return void 0;
|
326
|
-
}
|
327
|
-
if (!match.procedure) {
|
328
|
-
const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
|
329
|
-
if (!isProcedure(maybeProcedure)) {
|
330
|
-
throw new Error(`
|
331
|
-
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
|
332
|
-
Ensure that the procedure is correctly defined and matches the expected contract.
|
333
|
-
`);
|
334
|
-
}
|
335
|
-
match.procedure = createContractedProcedure(match.contract, maybeProcedure);
|
336
|
-
}
|
337
|
-
return {
|
338
|
-
path: match.path,
|
339
|
-
procedure: match.procedure
|
340
|
-
};
|
341
|
-
}
|
342
|
-
};
|
343
|
-
|
344
|
-
export {
|
345
|
-
StandardHandler,
|
346
|
-
RPCSerializer,
|
347
|
-
serializeRPCJson,
|
348
|
-
RPCCodec,
|
349
|
-
RPCMatcher
|
350
|
-
};
|
351
|
-
//# sourceMappingURL=chunk-NXEANHUK.js.map
|
@@ -1,22 +0,0 @@
|
|
1
|
-
import type { Segment } from '@orpc/shared';
|
2
|
-
export type RPCSerializedJsonMeta = ['bigint' | 'date' | 'nan' | 'undefined' | 'set' | 'map' | 'regexp' | 'url', Segment[]][];
|
3
|
-
export type RPCSerialized = {
|
4
|
-
json: unknown;
|
5
|
-
meta: RPCSerializedJsonMeta;
|
6
|
-
} | FormData | AsyncIteratorObject<{
|
7
|
-
json: unknown;
|
8
|
-
meta: RPCSerializedJsonMeta;
|
9
|
-
}, {
|
10
|
-
json: unknown;
|
11
|
-
meta: RPCSerializedJsonMeta;
|
12
|
-
}, void>;
|
13
|
-
export type RPCSerializedFormDataMaps = Segment[][];
|
14
|
-
export declare class RPCSerializer {
|
15
|
-
serialize(data: unknown): RPCSerialized;
|
16
|
-
deserialize(serialized: RPCSerialized): unknown;
|
17
|
-
}
|
18
|
-
export declare function serializeRPCJson(value: unknown, segments?: Segment[], meta?: RPCSerializedJsonMeta): {
|
19
|
-
json: unknown;
|
20
|
-
meta: RPCSerializedJsonMeta;
|
21
|
-
};
|
22
|
-
//# sourceMappingURL=rpc-serializer.d.ts.map
|