@orpc/server 0.0.0-next.9adcd05 → 0.0.0-next.9b13466
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 +14 -1
- package/dist/adapters/fetch/index.d.mts +42 -10
- package/dist/adapters/fetch/index.d.ts +42 -10
- package/dist/adapters/fetch/index.mjs +104 -8
- package/dist/adapters/node/index.d.mts +44 -21
- package/dist/adapters/node/index.d.ts +44 -21
- package/dist/adapters/node/index.mjs +82 -23
- package/dist/adapters/standard/index.d.mts +11 -10
- package/dist/adapters/standard/index.d.ts +11 -10
- package/dist/adapters/standard/index.mjs +6 -4
- package/dist/index.d.mts +89 -62
- package/dist/index.d.ts +89 -62
- package/dist/index.mjs +63 -33
- package/dist/plugins/index.d.mts +111 -18
- package/dist/plugins/index.d.ts +111 -18
- package/dist/plugins/index.mjs +157 -8
- package/dist/shared/server.B5yVxwZh.d.mts +17 -0
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.By38lRwX.d.ts +17 -0
- package/dist/shared/{server.BFBhsdJr.mjs → server.C37gDhSZ.mjs} +51 -32
- package/dist/shared/server.ClO23hLW.d.mts +73 -0
- package/dist/shared/server.Cqam9L0P.d.mts +10 -0
- package/dist/shared/server.CuXY46Yy.d.ts +10 -0
- package/dist/shared/{server.3cSam35R.mjs → server.DFuJLDuo.mjs} +62 -30
- package/dist/shared/{server.CPteJIPP.d.mts → server.DQJX4Gnf.d.mts} +19 -19
- package/dist/shared/{server.CPteJIPP.d.ts → server.DQJX4Gnf.d.ts} +19 -19
- package/dist/shared/server.DsVSuKTu.d.ts +73 -0
- package/package.json +8 -22
- package/dist/adapters/hono/index.d.mts +0 -19
- package/dist/adapters/hono/index.d.ts +0 -19
- package/dist/adapters/hono/index.mjs +0 -32
- package/dist/adapters/next/index.d.mts +0 -26
- package/dist/adapters/next/index.d.ts +0 -26
- package/dist/adapters/next/index.mjs +0 -29
- package/dist/shared/server.BZRSVRDu.d.ts +0 -77
- package/dist/shared/server.CjB_m7jG.mjs +0 -28
- package/dist/shared/server.Q6ZmnTgO.mjs +0 -12
- package/dist/shared/server.iM8li30u.d.mts +0 -77
@@ -1,6 +1,6 @@
|
|
1
1
|
import { isContractProcedure, ValidationError, mergePrefix, mergeErrorMap, enhanceRoute } from '@orpc/contract';
|
2
2
|
import { fallbackORPCErrorStatus, ORPCError } from '@orpc/client';
|
3
|
-
import { value, intercept
|
3
|
+
import { value, intercept } from '@orpc/shared';
|
4
4
|
|
5
5
|
const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
|
6
6
|
function lazy(loader, meta = {}) {
|
@@ -21,19 +21,25 @@ function unlazy(lazied) {
|
|
21
21
|
return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
|
22
22
|
}
|
23
23
|
|
24
|
-
function
|
25
|
-
|
24
|
+
function isStartWithMiddlewares(middlewares, compare) {
|
25
|
+
if (compare.length > middlewares.length) {
|
26
|
+
return false;
|
27
|
+
}
|
26
28
|
for (let i = 0; i < middlewares.length; i++) {
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
if (compare[i] === void 0) {
|
30
|
+
return true;
|
31
|
+
}
|
32
|
+
if (middlewares[i] !== compare[i]) {
|
33
|
+
return false;
|
30
34
|
}
|
31
|
-
min = index + 1;
|
32
35
|
}
|
33
|
-
return
|
36
|
+
return true;
|
34
37
|
}
|
35
|
-
function mergeMiddlewares(first, second) {
|
36
|
-
|
38
|
+
function mergeMiddlewares(first, second, options) {
|
39
|
+
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
40
|
+
return second;
|
41
|
+
}
|
42
|
+
return [...first, ...second];
|
37
43
|
}
|
38
44
|
function addMiddleware(middlewares, addition) {
|
39
45
|
return [...middlewares, addition];
|
@@ -52,6 +58,10 @@ function isProcedure(item) {
|
|
52
58
|
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
53
59
|
}
|
54
60
|
|
61
|
+
function mergeCurrentContext(context, other) {
|
62
|
+
return { ...context, ...other };
|
63
|
+
}
|
64
|
+
|
55
65
|
function createORPCErrorConstructorMap(errors) {
|
56
66
|
const proxy = new Proxy(errors, {
|
57
67
|
get(target, code) {
|
@@ -117,7 +127,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
117
127
|
);
|
118
128
|
} catch (e) {
|
119
129
|
if (!(e instanceof ORPCError)) {
|
120
|
-
throw
|
130
|
+
throw e;
|
121
131
|
}
|
122
132
|
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
123
133
|
throw validated;
|
@@ -159,28 +169,29 @@ async function executeProcedureInternal(procedure, options) {
|
|
159
169
|
const middlewares = procedure["~orpc"].middlewares;
|
160
170
|
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
161
171
|
const outputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].outputValidationIndex), middlewares.length);
|
162
|
-
|
163
|
-
|
164
|
-
let currentInput = options.input;
|
165
|
-
const next = async (...[nextOptions]) => {
|
166
|
-
const index = currentIndex;
|
167
|
-
currentIndex += 1;
|
168
|
-
currentContext = { ...currentContext, ...nextOptions?.context };
|
172
|
+
const next = async (index, context, input) => {
|
173
|
+
let currentInput = input;
|
169
174
|
if (index === inputValidationIndex) {
|
170
175
|
currentInput = await validateInput(procedure, currentInput);
|
171
176
|
}
|
172
177
|
const mid = middlewares[index];
|
173
|
-
const
|
178
|
+
const output = mid ? (await mid({
|
179
|
+
...options,
|
180
|
+
context,
|
181
|
+
next: async (...[nextOptions]) => {
|
182
|
+
const nextContext = nextOptions?.context ?? {};
|
183
|
+
return {
|
184
|
+
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
185
|
+
context: nextContext
|
186
|
+
};
|
187
|
+
}
|
188
|
+
}, currentInput, middlewareOutputFn)).output : await procedure["~orpc"].handler({ ...options, context, input: currentInput });
|
174
189
|
if (index === outputValidationIndex) {
|
175
|
-
|
176
|
-
return {
|
177
|
-
...result,
|
178
|
-
output: validatedOutput
|
179
|
-
};
|
190
|
+
return await validateOutput(procedure, output);
|
180
191
|
}
|
181
|
-
return
|
192
|
+
return output;
|
182
193
|
};
|
183
|
-
return (
|
194
|
+
return next(0, options.context, options.input);
|
184
195
|
}
|
185
196
|
|
186
197
|
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
@@ -250,7 +261,7 @@ function enhanceRouter(router, options) {
|
|
250
261
|
return accessible;
|
251
262
|
}
|
252
263
|
if (isProcedure(router)) {
|
253
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
264
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
|
254
265
|
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
255
266
|
const enhanced2 = new Procedure({
|
256
267
|
...router["~orpc"],
|
@@ -311,6 +322,18 @@ async function resolveContractProcedures(options, callback) {
|
|
311
322
|
}
|
312
323
|
}
|
313
324
|
}
|
325
|
+
async function unlazyRouter(router) {
|
326
|
+
if (isProcedure(router)) {
|
327
|
+
return router;
|
328
|
+
}
|
329
|
+
const unlazied = {};
|
330
|
+
for (const key in router) {
|
331
|
+
const item = router[key];
|
332
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
333
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
334
|
+
}
|
335
|
+
return unlazied;
|
336
|
+
}
|
314
337
|
|
315
338
|
function createAssertedLazyProcedure(lazied) {
|
316
339
|
const lazyProcedure = lazy(async () => {
|
@@ -338,8 +361,4 @@ function call(procedure, input, ...rest) {
|
|
338
361
|
return createProcedureClient(procedure, ...rest)(input);
|
339
362
|
}
|
340
363
|
|
341
|
-
|
342
|
-
return `/${path.map(encodeURIComponent).join("/")}`;
|
343
|
-
}
|
344
|
-
|
345
|
-
export { LAZY_SYMBOL as L, Procedure as P, toHttpPath as a, createContractedProcedure as b, createProcedureClient as c, addMiddleware as d, enhanceRouter as e, isLazy as f, getRouter as g, createAssertedLazyProcedure as h, isProcedure as i, getLazyMeta as j, call as k, lazy as l, middlewareOutputFn as m, getHiddenRouterContract as n, createAccessibleLazyRouter as o, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
|
364
|
+
export { LAZY_SYMBOL as L, Procedure as P, createContractedProcedure as a, addMiddleware as b, createProcedureClient as c, isLazy as d, enhanceRouter as e, createAssertedLazyProcedure as f, getRouter as g, createORPCErrorConstructorMap as h, isProcedure as i, getLazyMeta as j, middlewareOutputFn as k, lazy as l, mergeCurrentContext as m, isStartWithMiddlewares as n, mergeMiddlewares as o, call as p, getHiddenRouterContract as q, createAccessibleLazyRouter as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u, validateORPCError as v, resolveContractProcedures as w, unlazyRouter as x };
|
@@ -0,0 +1,73 @@
|
|
1
|
+
import { HTTPPath, ORPCError } from '@orpc/client';
|
2
|
+
import { Meta, InferSchemaOutput, AnySchema, ErrorFromErrorMap } from '@orpc/contract';
|
3
|
+
import { Interceptor, ThrowableError } from '@orpc/shared';
|
4
|
+
import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
|
5
|
+
import { f as AnyRouter, h as AnyProcedure, C as Context, F as ProcedureClientInterceptorOptions, R as Router } from './server.DQJX4Gnf.mjs';
|
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
|
+
interface StandardHandlerPlugin<TContext extends Context> {
|
24
|
+
order?: number;
|
25
|
+
init?(options: StandardHandlerOptions<TContext>): void;
|
26
|
+
}
|
27
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
28
|
+
protected readonly plugins: TPlugin[];
|
29
|
+
constructor(plugins?: readonly TPlugin[]);
|
30
|
+
init(options: StandardHandlerOptions<T>): void;
|
31
|
+
}
|
32
|
+
|
33
|
+
interface StandardHandleOptions<T extends Context> {
|
34
|
+
prefix?: HTTPPath;
|
35
|
+
context: T;
|
36
|
+
}
|
37
|
+
type StandardHandleResult = {
|
38
|
+
matched: true;
|
39
|
+
response: StandardResponse;
|
40
|
+
} | {
|
41
|
+
matched: false;
|
42
|
+
response: undefined;
|
43
|
+
};
|
44
|
+
interface StandardHandlerInterceptorOptions<T extends Context> extends StandardHandleOptions<T> {
|
45
|
+
request: StandardLazyRequest;
|
46
|
+
}
|
47
|
+
interface StandardHandlerOptions<TContext extends Context> {
|
48
|
+
plugins?: StandardHandlerPlugin<TContext>[];
|
49
|
+
/**
|
50
|
+
* Interceptors at the request level, helpful when you want catch errors
|
51
|
+
*/
|
52
|
+
interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
|
53
|
+
/**
|
54
|
+
* Interceptors at the root level, helpful when you want override the request/response
|
55
|
+
*/
|
56
|
+
rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, ThrowableError>[];
|
57
|
+
/**
|
58
|
+
*
|
59
|
+
* Interceptors for procedure client.
|
60
|
+
*/
|
61
|
+
clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
|
62
|
+
}
|
63
|
+
declare class StandardHandler<T extends Context> {
|
64
|
+
private readonly matcher;
|
65
|
+
private readonly codec;
|
66
|
+
private readonly interceptors;
|
67
|
+
private readonly clientInterceptors;
|
68
|
+
private readonly rootInterceptors;
|
69
|
+
constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
|
70
|
+
handle(request: StandardLazyRequest, options: StandardHandleOptions<T>): Promise<StandardHandleResult>;
|
71
|
+
}
|
72
|
+
|
73
|
+
export { CompositeStandardHandlerPlugin as C, type StandardHandlerInterceptorOptions as S, type StandardHandlerPlugin as a, type StandardHandlerOptions as b, type StandardCodec as c, type StandardParams as d, type StandardMatcher as e, type StandardMatchResult as f, type StandardHandleOptions as g, type StandardHandleResult as h, StandardHandler as i };
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { C as Context } from './server.DQJX4Gnf.mjs';
|
2
|
+
import { g as StandardHandleOptions } from './server.ClO23hLW.mjs';
|
3
|
+
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
5
|
+
context?: T;
|
6
|
+
} : {
|
7
|
+
context: T;
|
8
|
+
});
|
9
|
+
|
10
|
+
export type { FriendlyStandardHandleOptions as F };
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { C as Context } from './server.DQJX4Gnf.js';
|
2
|
+
import { g as StandardHandleOptions } from './server.DsVSuKTu.js';
|
3
|
+
|
4
|
+
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
5
|
+
context?: T;
|
6
|
+
} : {
|
7
|
+
context: T;
|
8
|
+
});
|
9
|
+
|
10
|
+
export type { FriendlyStandardHandleOptions as F };
|
@@ -1,51 +1,68 @@
|
|
1
|
+
import { toHttpPath, StandardRPCJsonSerializer, StandardRPCSerializer } from '@orpc/client/standard';
|
2
|
+
import { toArray, intercept, parseEmptyableJSON } from '@orpc/shared';
|
3
|
+
import '@orpc/standard-server/batch';
|
1
4
|
import { ORPCError, toORPCError } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import {
|
4
|
-
|
5
|
+
import { S as StrictGetMethodPlugin } from './server.BW-nUGgA.mjs';
|
6
|
+
import { c as createProcedureClient, t as traverseContractProcedures, i as isProcedure, u as unlazy, g as getRouter, a as createContractedProcedure } from './server.C37gDhSZ.mjs';
|
7
|
+
|
8
|
+
class CompositeStandardHandlerPlugin {
|
9
|
+
plugins;
|
10
|
+
constructor(plugins = []) {
|
11
|
+
this.plugins = [...plugins].sort((a, b) => (a.order ?? 0) - (b.order ?? 0));
|
12
|
+
}
|
13
|
+
init(options) {
|
14
|
+
for (const plugin of this.plugins) {
|
15
|
+
plugin.init?.(options);
|
16
|
+
}
|
17
|
+
}
|
18
|
+
}
|
5
19
|
|
6
20
|
class StandardHandler {
|
7
21
|
constructor(router, matcher, codec, options) {
|
8
22
|
this.matcher = matcher;
|
9
23
|
this.codec = codec;
|
10
|
-
|
11
|
-
|
12
|
-
this.
|
24
|
+
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
25
|
+
plugins.init(options);
|
26
|
+
this.interceptors = toArray(options.interceptors);
|
27
|
+
this.clientInterceptors = toArray(options.clientInterceptors);
|
28
|
+
this.rootInterceptors = toArray(options.rootInterceptors);
|
13
29
|
this.matcher.init(router);
|
14
30
|
}
|
15
|
-
|
16
|
-
|
31
|
+
interceptors;
|
32
|
+
clientInterceptors;
|
33
|
+
rootInterceptors;
|
34
|
+
async handle(request, options) {
|
35
|
+
const prefix = options.prefix?.replace(/\/$/, "") || void 0;
|
36
|
+
if (prefix && !request.url.pathname.startsWith(`${prefix}/`) && request.url.pathname !== prefix) {
|
37
|
+
return { matched: false, response: void 0 };
|
38
|
+
}
|
17
39
|
return intercept(
|
18
|
-
this.
|
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
|
-
},
|
40
|
+
this.rootInterceptors,
|
41
|
+
{ ...options, request, prefix },
|
25
42
|
async (interceptorOptions) => {
|
26
43
|
let isDecoding = false;
|
27
44
|
try {
|
28
45
|
return await intercept(
|
29
|
-
this.
|
46
|
+
this.interceptors,
|
30
47
|
interceptorOptions,
|
31
|
-
async (
|
32
|
-
const method =
|
33
|
-
const url =
|
34
|
-
const pathname =
|
35
|
-
const match = await this.matcher.match(method, pathname);
|
48
|
+
async ({ request: request2, context, prefix: prefix2 }) => {
|
49
|
+
const method = request2.method;
|
50
|
+
const url = request2.url;
|
51
|
+
const pathname = prefix2 ? url.pathname.replace(prefix2, "") : url.pathname;
|
52
|
+
const match = await this.matcher.match(method, `/${pathname.replace(/^\/|\/$/g, "")}`);
|
36
53
|
if (!match) {
|
37
54
|
return { matched: false, response: void 0 };
|
38
55
|
}
|
39
56
|
const client = createProcedureClient(match.procedure, {
|
40
|
-
context
|
57
|
+
context,
|
41
58
|
path: match.path,
|
42
|
-
interceptors: this.
|
59
|
+
interceptors: this.clientInterceptors
|
43
60
|
});
|
44
61
|
isDecoding = true;
|
45
|
-
const input = await this.codec.decode(
|
62
|
+
const input = await this.codec.decode(request2, match.params, match.procedure);
|
46
63
|
isDecoding = false;
|
47
|
-
const lastEventId = Array.isArray(
|
48
|
-
const output = await client(input, { signal:
|
64
|
+
const lastEventId = Array.isArray(request2.headers["last-event-id"]) ? request2.headers["last-event-id"].at(-1) : request2.headers["last-event-id"];
|
65
|
+
const output = await client(input, { signal: request2.signal, lastEventId });
|
49
66
|
const response = this.codec.encode(output, match.procedure);
|
50
67
|
return {
|
51
68
|
matched: true,
|
@@ -54,7 +71,7 @@ class StandardHandler {
|
|
54
71
|
}
|
55
72
|
);
|
56
73
|
} catch (e) {
|
57
|
-
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
74
|
+
const error = isDecoding && !(e instanceof ORPCError) ? new ORPCError("BAD_REQUEST", {
|
58
75
|
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
59
76
|
cause: e
|
60
77
|
}) : toORPCError(e);
|
@@ -69,7 +86,7 @@ class StandardHandler {
|
|
69
86
|
}
|
70
87
|
}
|
71
88
|
|
72
|
-
class
|
89
|
+
class StandardRPCCodec {
|
73
90
|
constructor(serializer) {
|
74
91
|
this.serializer = serializer;
|
75
92
|
}
|
@@ -93,7 +110,7 @@ class RPCCodec {
|
|
93
110
|
}
|
94
111
|
}
|
95
112
|
|
96
|
-
class
|
113
|
+
class StandardRPCMatcher {
|
97
114
|
tree = {};
|
98
115
|
pendingRouters = [];
|
99
116
|
init(router, path = []) {
|
@@ -155,4 +172,19 @@ class RPCMatcher {
|
|
155
172
|
}
|
156
173
|
}
|
157
174
|
|
158
|
-
|
175
|
+
class StandardRPCHandler extends StandardHandler {
|
176
|
+
constructor(router, options) {
|
177
|
+
options.plugins ??= [];
|
178
|
+
const strictGetMethodPluginEnabled = options.strictGetMethodPluginEnabled ?? true;
|
179
|
+
if (strictGetMethodPluginEnabled) {
|
180
|
+
options.plugins.push(new StrictGetMethodPlugin());
|
181
|
+
}
|
182
|
+
const jsonSerializer = new StandardRPCJsonSerializer(options);
|
183
|
+
const serializer = new StandardRPCSerializer(jsonSerializer);
|
184
|
+
const matcher = new StandardRPCMatcher();
|
185
|
+
const codec = new StandardRPCCodec(serializer);
|
186
|
+
super(router, matcher, codec, options);
|
187
|
+
}
|
188
|
+
}
|
189
|
+
|
190
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, StandardRPCCodec as a, StandardRPCHandler as b, StandardRPCMatcher as c };
|
@@ -1,19 +1,19 @@
|
|
1
|
-
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import { ErrorMap, ErrorMapItem, InferSchemaInput,
|
1
|
+
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
|
2
|
+
import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
|
3
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
4
4
|
|
5
|
-
type Context = Record<
|
6
|
-
type
|
7
|
-
|
8
|
-
|
9
|
-
[K in keyof T]: IsNever<T[K]>;
|
10
|
-
}[keyof T] ? never : unknown;
|
5
|
+
type Context = Record<PropertyKey, any>;
|
6
|
+
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
7
|
+
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
8
|
+
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
11
9
|
|
12
10
|
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
13
11
|
type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
14
12
|
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
15
13
|
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
16
14
|
};
|
15
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
16
|
+
declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
17
17
|
|
18
18
|
declare const LAZY_SYMBOL: unique symbol;
|
19
19
|
interface LazyMeta {
|
@@ -51,7 +51,7 @@ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutp
|
|
51
51
|
}
|
52
52
|
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
53
53
|
__initialContext?: (type: TInitialContext) => unknown;
|
54
|
-
middlewares: AnyMiddleware[];
|
54
|
+
middlewares: readonly AnyMiddleware[];
|
55
55
|
inputValidationIndex: number;
|
56
56
|
outputValidationIndex: number;
|
57
57
|
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
@@ -72,8 +72,8 @@ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never>
|
|
72
72
|
} : {
|
73
73
|
context: TOutContext;
|
74
74
|
};
|
75
|
-
interface MiddlewareNextFn<
|
76
|
-
<U extends Context
|
75
|
+
interface MiddlewareNextFn<TOutput> {
|
76
|
+
<U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
|
77
77
|
}
|
78
78
|
interface MiddlewareOutputFn<TOutput> {
|
79
79
|
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
@@ -84,7 +84,7 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
|
|
84
84
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
85
85
|
signal?: AbortSignal;
|
86
86
|
lastEventId: string | undefined;
|
87
|
-
next: MiddlewareNextFn<
|
87
|
+
next: MiddlewareNextFn<TOutput>;
|
88
88
|
errors: TErrorConstructorMap;
|
89
89
|
}
|
90
90
|
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
@@ -97,9 +97,9 @@ interface MapInputMiddleware<TInput, TMappedInput> {
|
|
97
97
|
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
98
98
|
|
99
99
|
type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
100
|
-
interface ProcedureClientInterceptorOptions<TInitialContext extends Context,
|
100
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
101
101
|
context: TInitialContext;
|
102
|
-
input:
|
102
|
+
input: unknown;
|
103
103
|
errors: ORPCErrorConstructorMap<TErrorMap>;
|
104
104
|
path: readonly string[];
|
105
105
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
@@ -109,18 +109,18 @@ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TIn
|
|
109
109
|
/**
|
110
110
|
* Options for creating a procedure caller with comprehensive type safety
|
111
111
|
*/
|
112
|
-
type CreateProcedureClientOptions<TInitialContext extends Context,
|
112
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
113
113
|
/**
|
114
114
|
* This is helpful for logging and analytics.
|
115
115
|
*/
|
116
116
|
path?: readonly string[];
|
117
|
-
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext,
|
117
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
|
118
118
|
} & (Record<never, never> extends TInitialContext ? {
|
119
119
|
context?: Value<TInitialContext, [clientContext: TClientContext]>;
|
120
120
|
} : {
|
121
121
|
context: Value<TInitialContext, [clientContext: TClientContext]>;
|
122
122
|
});
|
123
|
-
declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext,
|
123
|
+
declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
|
124
124
|
|
125
125
|
type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
|
126
126
|
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
@@ -140,4 +140,4 @@ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any
|
|
140
140
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
141
141
|
};
|
142
142
|
|
143
|
-
export { type
|
143
|
+
export { type AnyMiddleware as A, type ProcedureHandlerOptions as B, type Context as C, type ProcedureDef as D, isProcedure as E, type ProcedureClientInterceptorOptions as F, createProcedureClient as G, type InferRouterInitialContexts as H, type InferRouterInitialContext as I, type InferRouterCurrentContexts as J, type InferRouterInputs as K, type Lazyable as L, type Middleware as M, type InferRouterOutputs as N, type ORPCErrorConstructorMap as O, Procedure as P, type Router as R, type MergedInitialContext as a, type MergedCurrentContext as b, type MapInputMiddleware as c, type CreateProcedureClientOptions as d, type ProcedureClient as e, type AnyRouter as f, type Lazy as g, type AnyProcedure as h, type ProcedureHandler as i, type ORPCErrorConstructorMapItemOptions as j, type ORPCErrorConstructorMapItem as k, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, type LazyMeta as o, lazy as p, isLazy as q, getLazyMeta as r, type MiddlewareResult as s, type MiddlewareNextFnOptions as t, unlazy as u, validateORPCError as v, type MiddlewareNextFn as w, type MiddlewareOutputFn as x, type MiddlewareOptions as y, middlewareOutputFn as z };
|
@@ -1,19 +1,19 @@
|
|
1
|
-
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import { ErrorMap, ErrorMapItem, InferSchemaInput,
|
1
|
+
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
|
2
|
+
import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
|
3
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
4
4
|
|
5
|
-
type Context = Record<
|
6
|
-
type
|
7
|
-
|
8
|
-
|
9
|
-
[K in keyof T]: IsNever<T[K]>;
|
10
|
-
}[keyof T] ? never : unknown;
|
5
|
+
type Context = Record<PropertyKey, any>;
|
6
|
+
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
7
|
+
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
8
|
+
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
11
9
|
|
12
10
|
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
13
11
|
type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
14
12
|
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
15
13
|
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
16
14
|
};
|
15
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
16
|
+
declare function validateORPCError(map: ErrorMap, error: ORPCError<any, any>): Promise<ORPCError<string, unknown>>;
|
17
17
|
|
18
18
|
declare const LAZY_SYMBOL: unique symbol;
|
19
19
|
interface LazyMeta {
|
@@ -51,7 +51,7 @@ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutp
|
|
51
51
|
}
|
52
52
|
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TInputSchema, TOutputSchema, TErrorMap, TMeta> {
|
53
53
|
__initialContext?: (type: TInitialContext) => unknown;
|
54
|
-
middlewares: AnyMiddleware[];
|
54
|
+
middlewares: readonly AnyMiddleware[];
|
55
55
|
inputValidationIndex: number;
|
56
56
|
outputValidationIndex: number;
|
57
57
|
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
@@ -72,8 +72,8 @@ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never>
|
|
72
72
|
} : {
|
73
73
|
context: TOutContext;
|
74
74
|
};
|
75
|
-
interface MiddlewareNextFn<
|
76
|
-
<U extends Context
|
75
|
+
interface MiddlewareNextFn<TOutput> {
|
76
|
+
<U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
|
77
77
|
}
|
78
78
|
interface MiddlewareOutputFn<TOutput> {
|
79
79
|
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
@@ -84,7 +84,7 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
|
|
84
84
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
85
85
|
signal?: AbortSignal;
|
86
86
|
lastEventId: string | undefined;
|
87
|
-
next: MiddlewareNextFn<
|
87
|
+
next: MiddlewareNextFn<TOutput>;
|
88
88
|
errors: TErrorConstructorMap;
|
89
89
|
}
|
90
90
|
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
@@ -97,9 +97,9 @@ interface MapInputMiddleware<TInput, TMappedInput> {
|
|
97
97
|
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
98
98
|
|
99
99
|
type ProcedureClient<TClientContext extends ClientContext, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = Client<TClientContext, InferSchemaInput<TInputSchema>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>;
|
100
|
-
interface ProcedureClientInterceptorOptions<TInitialContext extends Context,
|
100
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
101
101
|
context: TInitialContext;
|
102
|
-
input:
|
102
|
+
input: unknown;
|
103
103
|
errors: ORPCErrorConstructorMap<TErrorMap>;
|
104
104
|
path: readonly string[];
|
105
105
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
@@ -109,18 +109,18 @@ interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TIn
|
|
109
109
|
/**
|
110
110
|
* Options for creating a procedure caller with comprehensive type safety
|
111
111
|
*/
|
112
|
-
type CreateProcedureClientOptions<TInitialContext extends Context,
|
112
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
113
113
|
/**
|
114
114
|
* This is helpful for logging and analytics.
|
115
115
|
*/
|
116
116
|
path?: readonly string[];
|
117
|
-
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext,
|
117
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
|
118
118
|
} & (Record<never, never> extends TInitialContext ? {
|
119
119
|
context?: Value<TInitialContext, [clientContext: TClientContext]>;
|
120
120
|
} : {
|
121
121
|
context: Value<TInitialContext, [clientContext: TClientContext]>;
|
122
122
|
});
|
123
|
-
declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext,
|
123
|
+
declare function createProcedureClient<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap, TMeta>>, ...[options]: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap>;
|
124
124
|
|
125
125
|
type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer UInputSchema, infer UOutputSchema, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, UInputSchema, UOutputSchema, UErrorMap, UMeta> : {
|
126
126
|
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
@@ -140,4 +140,4 @@ type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any
|
|
140
140
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
141
141
|
};
|
142
142
|
|
143
|
-
export { type
|
143
|
+
export { type AnyMiddleware as A, type ProcedureHandlerOptions as B, type Context as C, type ProcedureDef as D, isProcedure as E, type ProcedureClientInterceptorOptions as F, createProcedureClient as G, type InferRouterInitialContexts as H, type InferRouterInitialContext as I, type InferRouterCurrentContexts as J, type InferRouterInputs as K, type Lazyable as L, type Middleware as M, type InferRouterOutputs as N, type ORPCErrorConstructorMap as O, Procedure as P, type Router as R, type MergedInitialContext as a, type MergedCurrentContext as b, type MapInputMiddleware as c, type CreateProcedureClientOptions as d, type ProcedureClient as e, type AnyRouter as f, type Lazy as g, type AnyProcedure as h, type ProcedureHandler as i, type ORPCErrorConstructorMapItemOptions as j, type ORPCErrorConstructorMapItem as k, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, type LazyMeta as o, lazy as p, isLazy as q, getLazyMeta as r, type MiddlewareResult as s, type MiddlewareNextFnOptions as t, unlazy as u, validateORPCError as v, type MiddlewareNextFn as w, type MiddlewareOutputFn as x, type MiddlewareOptions as y, middlewareOutputFn as z };
|