@orpc/server 0.0.0-next.9adcd05 → 0.0.0-next.9afa7b8
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 +26 -17
- package/dist/adapters/fetch/index.d.mts +54 -10
- package/dist/adapters/fetch/index.d.ts +54 -10
- package/dist/adapters/fetch/index.mjs +104 -8
- package/dist/adapters/node/index.d.mts +56 -21
- package/dist/adapters/node/index.d.ts +56 -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 +631 -66
- package/dist/index.d.ts +631 -66
- package/dist/index.mjs +175 -37
- package/dist/plugins/index.d.mts +143 -18
- package/dist/plugins/index.d.ts +143 -18
- package/dist/plugins/index.mjs +157 -8
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.CN0534_m.d.mts +18 -0
- package/dist/shared/server.CjlA3NKP.d.ts +10 -0
- package/dist/shared/server.CuD15qZB.d.ts +18 -0
- package/dist/shared/server.D5fBlF9j.d.ts +74 -0
- package/dist/shared/{server.BFBhsdJr.mjs → server.DG7Tamti.mjs} +54 -32
- package/dist/shared/{server.CPteJIPP.d.mts → server.DPWk5pjW.d.mts} +71 -22
- package/dist/shared/{server.CPteJIPP.d.ts → server.DPWk5pjW.d.ts} +71 -22
- package/dist/shared/server.DY7OKEoj.d.mts +74 -0
- package/dist/shared/server.DjgtLwKi.d.mts +10 -0
- package/dist/shared/{server.3cSam35R.mjs → server.qf03T-Xn.mjs} +62 -30
- 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,25 +21,34 @@ 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];
|
40
46
|
}
|
41
47
|
|
42
48
|
class Procedure {
|
49
|
+
/**
|
50
|
+
* This property holds the defined options.
|
51
|
+
*/
|
43
52
|
"~orpc";
|
44
53
|
constructor(def) {
|
45
54
|
this["~orpc"] = def;
|
@@ -52,6 +61,10 @@ function isProcedure(item) {
|
|
52
61
|
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
53
62
|
}
|
54
63
|
|
64
|
+
function mergeCurrentContext(context, other) {
|
65
|
+
return { ...context, ...other };
|
66
|
+
}
|
67
|
+
|
55
68
|
function createORPCErrorConstructorMap(errors) {
|
56
69
|
const proxy = new Proxy(errors, {
|
57
70
|
get(target, code) {
|
@@ -117,7 +130,7 @@ function createProcedureClient(lazyableProcedure, ...[options]) {
|
|
117
130
|
);
|
118
131
|
} catch (e) {
|
119
132
|
if (!(e instanceof ORPCError)) {
|
120
|
-
throw
|
133
|
+
throw e;
|
121
134
|
}
|
122
135
|
const validated = await validateORPCError(procedure["~orpc"].errorMap, e);
|
123
136
|
throw validated;
|
@@ -159,28 +172,29 @@ async function executeProcedureInternal(procedure, options) {
|
|
159
172
|
const middlewares = procedure["~orpc"].middlewares;
|
160
173
|
const inputValidationIndex = Math.min(Math.max(0, procedure["~orpc"].inputValidationIndex), middlewares.length);
|
161
174
|
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 };
|
175
|
+
const next = async (index, context, input) => {
|
176
|
+
let currentInput = input;
|
169
177
|
if (index === inputValidationIndex) {
|
170
178
|
currentInput = await validateInput(procedure, currentInput);
|
171
179
|
}
|
172
180
|
const mid = middlewares[index];
|
173
|
-
const
|
181
|
+
const output = mid ? (await mid({
|
182
|
+
...options,
|
183
|
+
context,
|
184
|
+
next: async (...[nextOptions]) => {
|
185
|
+
const nextContext = nextOptions?.context ?? {};
|
186
|
+
return {
|
187
|
+
output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
|
188
|
+
context: nextContext
|
189
|
+
};
|
190
|
+
}
|
191
|
+
}, currentInput, middlewareOutputFn)).output : await procedure["~orpc"].handler({ ...options, context, input: currentInput });
|
174
192
|
if (index === outputValidationIndex) {
|
175
|
-
|
176
|
-
return {
|
177
|
-
...result,
|
178
|
-
output: validatedOutput
|
179
|
-
};
|
193
|
+
return await validateOutput(procedure, output);
|
180
194
|
}
|
181
|
-
return
|
195
|
+
return output;
|
182
196
|
};
|
183
|
-
return (
|
197
|
+
return next(0, options.context, options.input);
|
184
198
|
}
|
185
199
|
|
186
200
|
const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
|
@@ -250,7 +264,7 @@ function enhanceRouter(router, options) {
|
|
250
264
|
return accessible;
|
251
265
|
}
|
252
266
|
if (isProcedure(router)) {
|
253
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares);
|
267
|
+
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, { dedupeLeading: options.dedupeLeadingMiddlewares });
|
254
268
|
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
255
269
|
const enhanced2 = new Procedure({
|
256
270
|
...router["~orpc"],
|
@@ -311,6 +325,18 @@ async function resolveContractProcedures(options, callback) {
|
|
311
325
|
}
|
312
326
|
}
|
313
327
|
}
|
328
|
+
async function unlazyRouter(router) {
|
329
|
+
if (isProcedure(router)) {
|
330
|
+
return router;
|
331
|
+
}
|
332
|
+
const unlazied = {};
|
333
|
+
for (const key in router) {
|
334
|
+
const item = router[key];
|
335
|
+
const { default: unlaziedRouter } = await unlazy(item);
|
336
|
+
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
337
|
+
}
|
338
|
+
return unlazied;
|
339
|
+
}
|
314
340
|
|
315
341
|
function createAssertedLazyProcedure(lazied) {
|
316
342
|
const lazyProcedure = lazy(async () => {
|
@@ -338,8 +364,4 @@ function call(procedure, input, ...rest) {
|
|
338
364
|
return createProcedureClient(procedure, ...rest)(input);
|
339
365
|
}
|
340
366
|
|
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 };
|
367
|
+
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 };
|
@@ -1,19 +1,19 @@
|
|
1
|
-
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
|
2
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
3
|
+
import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
|
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 {
|
@@ -28,6 +28,9 @@ interface Lazy<T> {
|
|
28
28
|
};
|
29
29
|
}
|
30
30
|
type Lazyable<T> = T | Lazy<T>;
|
31
|
+
/**
|
32
|
+
* Create a lazy thing.
|
33
|
+
*/
|
31
34
|
declare function lazy<T>(loader: () => Promise<{
|
32
35
|
default: T;
|
33
36
|
}>, meta?: LazyMeta): Lazy<T>;
|
@@ -51,12 +54,20 @@ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutp
|
|
51
54
|
}
|
52
55
|
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
56
|
__initialContext?: (type: TInitialContext) => unknown;
|
54
|
-
middlewares: AnyMiddleware[];
|
57
|
+
middlewares: readonly AnyMiddleware[];
|
55
58
|
inputValidationIndex: number;
|
56
59
|
outputValidationIndex: number;
|
57
60
|
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
58
61
|
}
|
62
|
+
/**
|
63
|
+
* This class represents a procedure.
|
64
|
+
*
|
65
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
66
|
+
*/
|
59
67
|
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
68
|
+
/**
|
69
|
+
* This property holds the defined options.
|
70
|
+
*/
|
60
71
|
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
61
72
|
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
62
73
|
}
|
@@ -72,8 +83,8 @@ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never>
|
|
72
83
|
} : {
|
73
84
|
context: TOutContext;
|
74
85
|
};
|
75
|
-
interface MiddlewareNextFn<
|
76
|
-
<U extends Context
|
86
|
+
interface MiddlewareNextFn<TOutput> {
|
87
|
+
<U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
|
77
88
|
}
|
78
89
|
interface MiddlewareOutputFn<TOutput> {
|
79
90
|
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
@@ -84,9 +95,14 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
|
|
84
95
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
85
96
|
signal?: AbortSignal;
|
86
97
|
lastEventId: string | undefined;
|
87
|
-
next: MiddlewareNextFn<
|
98
|
+
next: MiddlewareNextFn<TOutput>;
|
88
99
|
errors: TErrorConstructorMap;
|
89
100
|
}
|
101
|
+
/**
|
102
|
+
* A function that represents a middleware.
|
103
|
+
*
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
105
|
+
*/
|
90
106
|
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
91
107
|
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
92
108
|
}
|
@@ -97,47 +113,80 @@ interface MapInputMiddleware<TInput, TMappedInput> {
|
|
97
113
|
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
98
114
|
|
99
115
|
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,
|
116
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
101
117
|
context: TInitialContext;
|
102
|
-
input:
|
118
|
+
input: unknown;
|
103
119
|
errors: ORPCErrorConstructorMap<TErrorMap>;
|
104
120
|
path: readonly string[];
|
105
121
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
106
122
|
signal?: AbortSignal;
|
107
123
|
lastEventId: string | undefined;
|
108
124
|
}
|
109
|
-
|
110
|
-
* Options for creating a procedure caller with comprehensive type safety
|
111
|
-
*/
|
112
|
-
type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
125
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
113
126
|
/**
|
114
127
|
* This is helpful for logging and analytics.
|
115
128
|
*/
|
116
129
|
path?: readonly string[];
|
117
|
-
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext,
|
130
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
|
118
131
|
} & (Record<never, never> extends TInitialContext ? {
|
119
132
|
context?: Value<TInitialContext, [clientContext: TClientContext]>;
|
120
133
|
} : {
|
121
134
|
context: Value<TInitialContext, [clientContext: TClientContext]>;
|
122
135
|
});
|
123
|
-
|
136
|
+
/**
|
137
|
+
* Create Server-side client from a procedure.
|
138
|
+
*
|
139
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
140
|
+
*/
|
141
|
+
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
142
|
|
143
|
+
/**
|
144
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
145
|
+
*
|
146
|
+
* @info A procedure is a router too.
|
147
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
148
|
+
*/
|
125
149
|
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
150
|
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
127
151
|
};
|
128
152
|
type AnyRouter = Router<any, any>;
|
129
153
|
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
154
|
+
/**
|
155
|
+
* Infer all initial context of the router.
|
156
|
+
*
|
157
|
+
* @info A procedure is a router too.
|
158
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
159
|
+
*/
|
130
160
|
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
|
131
161
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
132
162
|
};
|
163
|
+
/**
|
164
|
+
* Infer all current context of the router.
|
165
|
+
*
|
166
|
+
* @info A procedure is a router too.
|
167
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
168
|
+
*/
|
133
169
|
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
|
134
170
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
135
171
|
};
|
172
|
+
/**
|
173
|
+
* Infer all router inputs
|
174
|
+
*
|
175
|
+
* @info A procedure is a router too.
|
176
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
177
|
+
*/
|
136
178
|
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
137
179
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
138
180
|
};
|
181
|
+
/**
|
182
|
+
* Infer all router outputs
|
183
|
+
*
|
184
|
+
* @info A procedure is a router too.
|
185
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
186
|
+
*/
|
139
187
|
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
140
188
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
141
189
|
};
|
142
190
|
|
143
|
-
export {
|
191
|
+
export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
|
192
|
+
export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
|
@@ -1,19 +1,19 @@
|
|
1
|
-
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, ClientContext, Client } from '@orpc/client';
|
2
|
-
import {
|
3
|
-
import {
|
1
|
+
import { ORPCErrorCode, ORPCErrorOptions, ORPCError, HTTPPath, ClientContext, Client } from '@orpc/client';
|
2
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, AnySchema, Meta, ContractProcedureDef, InferSchemaOutput, ErrorFromErrorMap, AnyContractRouter, ContractProcedure } from '@orpc/contract';
|
3
|
+
import { MaybeOptionalOptions, Promisable, Interceptor, Value } from '@orpc/shared';
|
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 {
|
@@ -28,6 +28,9 @@ interface Lazy<T> {
|
|
28
28
|
};
|
29
29
|
}
|
30
30
|
type Lazyable<T> = T | Lazy<T>;
|
31
|
+
/**
|
32
|
+
* Create a lazy thing.
|
33
|
+
*/
|
31
34
|
declare function lazy<T>(loader: () => Promise<{
|
32
35
|
default: T;
|
33
36
|
}>, meta?: LazyMeta): Lazy<T>;
|
@@ -51,12 +54,20 @@ interface ProcedureHandler<TCurrentContext extends Context, TInput, THandlerOutp
|
|
51
54
|
}
|
52
55
|
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
56
|
__initialContext?: (type: TInitialContext) => unknown;
|
54
|
-
middlewares: AnyMiddleware[];
|
57
|
+
middlewares: readonly AnyMiddleware[];
|
55
58
|
inputValidationIndex: number;
|
56
59
|
outputValidationIndex: number;
|
57
60
|
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
58
61
|
}
|
62
|
+
/**
|
63
|
+
* This class represents a procedure.
|
64
|
+
*
|
65
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
66
|
+
*/
|
59
67
|
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
68
|
+
/**
|
69
|
+
* This property holds the defined options.
|
70
|
+
*/
|
60
71
|
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>;
|
61
72
|
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>);
|
62
73
|
}
|
@@ -72,8 +83,8 @@ type MiddlewareNextFnOptions<TOutContext extends Context> = Record<never, never>
|
|
72
83
|
} : {
|
73
84
|
context: TOutContext;
|
74
85
|
};
|
75
|
-
interface MiddlewareNextFn<
|
76
|
-
<U extends Context
|
86
|
+
interface MiddlewareNextFn<TOutput> {
|
87
|
+
<U extends Context = Record<never, never>>(...rest: MaybeOptionalOptions<MiddlewareNextFnOptions<U>>): MiddlewareResult<U, TOutput>;
|
77
88
|
}
|
78
89
|
interface MiddlewareOutputFn<TOutput> {
|
79
90
|
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
@@ -84,9 +95,14 @@ interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstruct
|
|
84
95
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
85
96
|
signal?: AbortSignal;
|
86
97
|
lastEventId: string | undefined;
|
87
|
-
next: MiddlewareNextFn<
|
98
|
+
next: MiddlewareNextFn<TOutput>;
|
88
99
|
errors: TErrorConstructorMap;
|
89
100
|
}
|
101
|
+
/**
|
102
|
+
* A function that represents a middleware.
|
103
|
+
*
|
104
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
105
|
+
*/
|
90
106
|
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
91
107
|
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
92
108
|
}
|
@@ -97,47 +113,80 @@ interface MapInputMiddleware<TInput, TMappedInput> {
|
|
97
113
|
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
98
114
|
|
99
115
|
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,
|
116
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
101
117
|
context: TInitialContext;
|
102
|
-
input:
|
118
|
+
input: unknown;
|
103
119
|
errors: ORPCErrorConstructorMap<TErrorMap>;
|
104
120
|
path: readonly string[];
|
105
121
|
procedure: Procedure<Context, Context, AnySchema, AnySchema, ErrorMap, TMeta>;
|
106
122
|
signal?: AbortSignal;
|
107
123
|
lastEventId: string | undefined;
|
108
124
|
}
|
109
|
-
|
110
|
-
* Options for creating a procedure caller with comprehensive type safety
|
111
|
-
*/
|
112
|
-
type CreateProcedureClientOptions<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
125
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
113
126
|
/**
|
114
127
|
* This is helpful for logging and analytics.
|
115
128
|
*/
|
116
129
|
path?: readonly string[];
|
117
|
-
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext,
|
130
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>[];
|
118
131
|
} & (Record<never, never> extends TInitialContext ? {
|
119
132
|
context?: Value<TInitialContext, [clientContext: TClientContext]>;
|
120
133
|
} : {
|
121
134
|
context: Value<TInitialContext, [clientContext: TClientContext]>;
|
122
135
|
});
|
123
|
-
|
136
|
+
/**
|
137
|
+
* Create Server-side client from a procedure.
|
138
|
+
*
|
139
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
140
|
+
*/
|
141
|
+
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
142
|
|
143
|
+
/**
|
144
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
145
|
+
*
|
146
|
+
* @info A procedure is a router too.
|
147
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
148
|
+
*/
|
125
149
|
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
150
|
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
127
151
|
};
|
128
152
|
type AnyRouter = Router<any, any>;
|
129
153
|
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
154
|
+
/**
|
155
|
+
* Infer all initial context of the router.
|
156
|
+
*
|
157
|
+
* @info A procedure is a router too.
|
158
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
159
|
+
*/
|
130
160
|
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any, any> ? UInitialContext : {
|
131
161
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
132
162
|
};
|
163
|
+
/**
|
164
|
+
* Infer all current context of the router.
|
165
|
+
*
|
166
|
+
* @info A procedure is a router too.
|
167
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
168
|
+
*/
|
133
169
|
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any, any> ? UCurrentContext : {
|
134
170
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
135
171
|
};
|
172
|
+
/**
|
173
|
+
* Infer all router inputs
|
174
|
+
*
|
175
|
+
* @info A procedure is a router too.
|
176
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
177
|
+
*/
|
136
178
|
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer UInputSchema, any, any, any> ? InferSchemaInput<UInputSchema> : {
|
137
179
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
138
180
|
};
|
181
|
+
/**
|
182
|
+
* Infer all router outputs
|
183
|
+
*
|
184
|
+
* @info A procedure is a router too.
|
185
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
186
|
+
*/
|
139
187
|
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, any, infer UOutputSchema, any, any> ? InferSchemaOutput<UOutputSchema> : {
|
140
188
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
141
189
|
};
|
142
190
|
|
143
|
-
export {
|
191
|
+
export { isProcedure as E, createProcedureClient as G, Procedure as P, createORPCErrorConstructorMap as l, mergeCurrentContext as m, LAZY_SYMBOL as n, lazy as p, isLazy as q, getLazyMeta as r, unlazy as u, validateORPCError as v, middlewareOutputFn as z };
|
192
|
+
export type { AnyMiddleware as A, ProcedureHandlerOptions as B, Context as C, ProcedureDef as D, ProcedureClientInterceptorOptions as F, InferRouterInitialContexts as H, InferRouterInitialContext as I, InferRouterCurrentContexts as J, InferRouterInputs as K, Lazyable as L, Middleware as M, InferRouterOutputs as N, ORPCErrorConstructorMap as O, Router as R, MergedInitialContext as a, MergedCurrentContext as b, MapInputMiddleware as c, CreateProcedureClientOptions as d, ProcedureClient as e, AnyRouter as f, Lazy as g, AnyProcedure as h, ProcedureHandler as i, ORPCErrorConstructorMapItemOptions as j, ORPCErrorConstructorMapItem as k, LazyMeta as o, MiddlewareResult as s, MiddlewareNextFnOptions as t, MiddlewareNextFn as w, MiddlewareOutputFn as x, MiddlewareOptions as y };
|
@@ -0,0 +1,74 @@
|
|
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 { C as Context, f as AnyRouter, h as AnyProcedure, F as ProcedureClientInterceptorOptions, R as Router } from './server.DPWk5pjW.mjs';
|
6
|
+
|
7
|
+
interface StandardHandlerPlugin<TContext extends Context> {
|
8
|
+
order?: number;
|
9
|
+
init?(options: StandardHandlerOptions<TContext>): void;
|
10
|
+
}
|
11
|
+
declare class CompositeStandardHandlerPlugin<T extends Context, TPlugin extends StandardHandlerPlugin<T>> implements StandardHandlerPlugin<T> {
|
12
|
+
protected readonly plugins: TPlugin[];
|
13
|
+
constructor(plugins?: readonly TPlugin[]);
|
14
|
+
init(options: StandardHandlerOptions<T>): void;
|
15
|
+
}
|
16
|
+
|
17
|
+
type StandardParams = Record<string, string>;
|
18
|
+
type StandardMatchResult = {
|
19
|
+
path: readonly string[];
|
20
|
+
procedure: AnyProcedure;
|
21
|
+
params?: StandardParams;
|
22
|
+
} | undefined;
|
23
|
+
interface StandardMatcher {
|
24
|
+
init(router: AnyRouter): void;
|
25
|
+
match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
|
26
|
+
}
|
27
|
+
interface StandardCodec {
|
28
|
+
encode(output: unknown, procedure: AnyProcedure): StandardResponse;
|
29
|
+
encodeError(error: ORPCError<any, any>): StandardResponse;
|
30
|
+
decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
|
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, StandardHandler as i };
|
74
|
+
export type { StandardHandlerInterceptorOptions as S, StandardHandlerPlugin as a, StandardHandlerOptions as b, StandardCodec as c, StandardParams as d, StandardMatcher as e, StandardMatchResult as f, StandardHandleOptions as g, StandardHandleResult as h };
|
@@ -0,0 +1,10 @@
|
|
1
|
+
import { C as Context } from './server.DPWk5pjW.mjs';
|
2
|
+
import { g as StandardHandleOptions } from './server.DY7OKEoj.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 };
|