@temporary-name/server 1.9.3-alpha.592ae1e02e18c5274906b132313b083116248636 → 1.9.3-alpha.5dc8b200530586870ac736830d4584e0333cfd05
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/adapters/aws-lambda/index.d.mts +3 -4
- package/dist/adapters/aws-lambda/index.d.ts +3 -4
- package/dist/adapters/aws-lambda/index.mjs +4 -6
- package/dist/adapters/fetch/index.d.mts +3 -4
- package/dist/adapters/fetch/index.d.ts +3 -4
- package/dist/adapters/fetch/index.mjs +4 -6
- package/dist/adapters/node/index.d.mts +3 -4
- package/dist/adapters/node/index.d.ts +3 -4
- package/dist/adapters/node/index.mjs +4 -6
- package/dist/adapters/standard/index.d.mts +20 -32
- package/dist/adapters/standard/index.d.ts +20 -32
- package/dist/adapters/standard/index.mjs +5 -7
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +71 -339
- package/dist/index.d.ts +71 -339
- package/dist/index.mjs +218 -355
- package/dist/openapi/index.d.mts +1 -1
- package/dist/openapi/index.d.ts +1 -1
- package/dist/openapi/index.mjs +60 -77
- package/dist/plugins/index.d.mts +4 -80
- package/dist/plugins/index.d.ts +4 -80
- package/dist/plugins/index.mjs +17 -189
- package/dist/shared/{server.CQyYNJ1H.d.ts → server.B-meye9-.d.ts} +2 -4
- package/dist/shared/{server.DLsti1Pv.mjs → server.Ba0Z2fNc.mjs} +58 -97
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.DkYpsO6W.d.mts +251 -0
- package/dist/shared/server.DkYpsO6W.d.ts +251 -0
- package/dist/shared/{server.SLLuK6_v.d.ts → server.DwNnHUZP.d.ts} +2 -2
- package/dist/shared/{server.BeuTpcmO.d.mts → server.I-tTl_ce.d.mts} +2 -2
- package/dist/shared/{server.BEHw7Eyx.mjs → server.JtIZ8YG7.mjs} +1 -11
- package/dist/shared/server.miXh-9wo.mjs +416 -0
- package/dist/shared/{server.C1fnTLq0.d.mts → server.vLcMd_kA.d.mts} +2 -4
- package/package.json +10 -21
- package/dist/shared/server.BKSOrA6h.d.mts +0 -192
- package/dist/shared/server.BKSOrA6h.d.ts +0 -192
- package/dist/shared/server.BKh8I1Ny.mjs +0 -239
|
@@ -1,67 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { d as deserialize, s as serialize, a as standardizeHTTPPath } from './server.BEHw7Eyx.mjs';
|
|
1
|
+
import { stringifyJSON, isObject, isORPCErrorStatus, tryDecodeURIComponent, toHttpPath, toArray, intercept, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, setSpanError, ORPCError, toORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { c as createProcedureClient } from './server.miXh-9wo.mjs';
|
|
3
|
+
import { fallbackContractConfig, standardizeHTTPPath } from '@temporary-name/contract';
|
|
4
|
+
import { d as deserialize, b as bracketNotationDeserialize, s as serialize } from './server.JtIZ8YG7.mjs';
|
|
6
5
|
import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@temporary-name/server';
|
|
7
6
|
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
8
7
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
return data;
|
|
29
|
-
}
|
|
30
|
-
const deserializeSearchParams = () => {
|
|
31
|
-
return deserialize(request.url.searchParams);
|
|
32
|
-
};
|
|
8
|
+
async function decode(request, pathParams) {
|
|
9
|
+
return {
|
|
10
|
+
path: pathParams ?? {},
|
|
11
|
+
query: bracketNotationDeserialize(Array.from(request.url.searchParams.entries())),
|
|
12
|
+
headers: request.headers,
|
|
13
|
+
body: deserialize(await request.body()) ?? {}
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function encode(output, procedure) {
|
|
17
|
+
const successStatus = fallbackContractConfig(
|
|
18
|
+
"defaultSuccessStatus",
|
|
19
|
+
procedure["~orpc"].route.successStatus
|
|
20
|
+
);
|
|
21
|
+
const outputStructure = fallbackContractConfig(
|
|
22
|
+
"defaultOutputStructure",
|
|
23
|
+
procedure["~orpc"].route.outputStructure
|
|
24
|
+
);
|
|
25
|
+
if (outputStructure === "compact") {
|
|
33
26
|
return {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
Object.defineProperty(this, "query", { value, writable: true });
|
|
38
|
-
return value;
|
|
39
|
-
},
|
|
40
|
-
set query(value) {
|
|
41
|
-
Object.defineProperty(this, "query", { value, writable: true });
|
|
42
|
-
},
|
|
43
|
-
headers: request.headers,
|
|
44
|
-
body: deserialize(await request.body())
|
|
27
|
+
status: successStatus,
|
|
28
|
+
headers: new Headers(),
|
|
29
|
+
body: serialize(output)
|
|
45
30
|
};
|
|
46
31
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
"defaultSuccessStatus",
|
|
50
|
-
procedure["~orpc"].route.successStatus
|
|
51
|
-
);
|
|
52
|
-
const outputStructure = fallbackContractConfig(
|
|
53
|
-
"defaultOutputStructure",
|
|
54
|
-
procedure["~orpc"].route.outputStructure
|
|
55
|
-
);
|
|
56
|
-
if (outputStructure === "compact") {
|
|
57
|
-
return {
|
|
58
|
-
status: successStatus,
|
|
59
|
-
headers: {},
|
|
60
|
-
body: serialize(output)
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
if (!this.#isDetailedOutput(output)) {
|
|
64
|
-
throw new Error(`
|
|
32
|
+
if (!isDetailedOutput(output)) {
|
|
33
|
+
throw new Error(`
|
|
65
34
|
Invalid "detailed" output structure:
|
|
66
35
|
\u2022 Expected an object with optional properties:
|
|
67
36
|
- status (number 200-399)
|
|
@@ -72,32 +41,31 @@ class StandardOpenAPICodec {
|
|
|
72
41
|
Actual value:
|
|
73
42
|
${stringifyJSON(output)}
|
|
74
43
|
`);
|
|
75
|
-
}
|
|
76
|
-
return {
|
|
77
|
-
status: output.status ?? successStatus,
|
|
78
|
-
headers: output.headers ?? {},
|
|
79
|
-
body: serialize(output.body)
|
|
80
|
-
};
|
|
81
44
|
}
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
45
|
+
return {
|
|
46
|
+
status: output.status ?? successStatus,
|
|
47
|
+
headers: output.headers ?? new Headers(),
|
|
48
|
+
body: serialize(output.body)
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function encodeError(error) {
|
|
52
|
+
return {
|
|
53
|
+
status: error.status,
|
|
54
|
+
headers: new Headers(),
|
|
55
|
+
body: serialize(error.toJSON(), { outputFormat: "plain" })
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
function isDetailedOutput(output) {
|
|
59
|
+
if (!isObject(output)) {
|
|
60
|
+
return false;
|
|
88
61
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
if (output.headers && !isObject(output.headers)) {
|
|
94
|
-
return false;
|
|
95
|
-
}
|
|
96
|
-
if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
return true;
|
|
62
|
+
if (output.headers && !isObject(output.headers)) {
|
|
63
|
+
return false;
|
|
100
64
|
}
|
|
65
|
+
if (output.status !== void 0 && (typeof output.status !== "number" || !Number.isInteger(output.status) || isORPCErrorStatus(output.status))) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
101
69
|
}
|
|
102
70
|
|
|
103
71
|
function resolveFriendlyStandardHandleOptions(options) {
|
|
@@ -121,9 +89,6 @@ class StandardOpenAPIMatcher {
|
|
|
121
89
|
pendingRouters = [];
|
|
122
90
|
init(router, path = []) {
|
|
123
91
|
const laziedOptions = traverseContractProcedures({ router, path }, (traverseOptions) => {
|
|
124
|
-
if (!value(true, traverseOptions)) {
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
92
|
const { path: path2, contract } = traverseOptions;
|
|
128
93
|
const method = fallbackContractConfig("defaultMethod", contract["~orpc"].route.method);
|
|
129
94
|
const httpPath = toRou3Pattern(contract["~orpc"].route.path ?? toHttpPath(path2));
|
|
@@ -204,10 +169,8 @@ class StandardHandler {
|
|
|
204
169
|
clientInterceptors;
|
|
205
170
|
rootInterceptors;
|
|
206
171
|
matcher;
|
|
207
|
-
codec;
|
|
208
172
|
constructor(router, options) {
|
|
209
173
|
this.matcher = new StandardOpenAPIMatcher();
|
|
210
|
-
this.codec = new StandardOpenAPICodec();
|
|
211
174
|
const plugins = new CompositeStandardHandlerPlugin(options.plugins);
|
|
212
175
|
plugins.init(options, router);
|
|
213
176
|
this.interceptors = toArray(options.interceptors);
|
|
@@ -242,15 +205,12 @@ class StandardHandler {
|
|
|
242
205
|
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
243
206
|
span?.setAttribute("rpc.method", match.path.join("."));
|
|
244
207
|
step = "decode_input";
|
|
245
|
-
|
|
246
|
-
{ name: "decode_input" },
|
|
247
|
-
() => this.codec.decode(request2, match.params, match.procedure)
|
|
248
|
-
);
|
|
208
|
+
const input = await runWithSpan({ name: "decode_input" }, () => decode(request2, match.params));
|
|
249
209
|
step = void 0;
|
|
250
|
-
if (isAsyncIteratorObject(input)) {
|
|
251
|
-
input = asyncIteratorWithSpan(
|
|
210
|
+
if (isAsyncIteratorObject(input.body)) {
|
|
211
|
+
input.body = asyncIteratorWithSpan(
|
|
252
212
|
{ name: "consume_event_iterator_input", signal: request2.signal },
|
|
253
|
-
input
|
|
213
|
+
input.body
|
|
254
214
|
);
|
|
255
215
|
}
|
|
256
216
|
const client = createProcedureClient(match.procedure, {
|
|
@@ -260,11 +220,12 @@ class StandardHandler {
|
|
|
260
220
|
});
|
|
261
221
|
step = "call_procedure";
|
|
262
222
|
const output = await client(input, {
|
|
223
|
+
request: request2,
|
|
263
224
|
signal: request2.signal,
|
|
264
|
-
lastEventId:
|
|
225
|
+
lastEventId: request2.headers.get("last-event-id") ?? void 0
|
|
265
226
|
});
|
|
266
227
|
step = void 0;
|
|
267
|
-
const response =
|
|
228
|
+
const response = encode(output, match.procedure);
|
|
268
229
|
return {
|
|
269
230
|
matched: true,
|
|
270
231
|
response
|
|
@@ -279,7 +240,7 @@ class StandardHandler {
|
|
|
279
240
|
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
|
280
241
|
cause: e
|
|
281
242
|
}) : toORPCError(e);
|
|
282
|
-
const response =
|
|
243
|
+
const response = encodeError(error);
|
|
283
244
|
return {
|
|
284
245
|
matched: true,
|
|
285
246
|
response
|
|
@@ -290,4 +251,4 @@ class StandardHandler {
|
|
|
290
251
|
}
|
|
291
252
|
}
|
|
292
253
|
|
|
293
|
-
export { CompositeStandardHandlerPlugin as C, StandardHandler as S,
|
|
254
|
+
export { CompositeStandardHandlerPlugin as C, StandardHandler as S, encodeError as a, StandardOpenAPIMatcher as b, decodeParams as c, decode as d, encode as e, resolveFriendlyStandardHandleOptions as r, toRou3Pattern as t };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { parse, serialize } from 'cookie';
|
|
2
|
+
|
|
3
|
+
function setCookie(headers, name, value, options = {}) {
|
|
4
|
+
if (headers === void 0) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const cookieString = serialize(name, value, {
|
|
8
|
+
path: "/",
|
|
9
|
+
...options
|
|
10
|
+
});
|
|
11
|
+
headers.append("Set-Cookie", cookieString);
|
|
12
|
+
}
|
|
13
|
+
function getCookie(headers, name, options = {}) {
|
|
14
|
+
if (headers === void 0) {
|
|
15
|
+
return void 0;
|
|
16
|
+
}
|
|
17
|
+
const cookieHeader = headers.get("cookie");
|
|
18
|
+
if (cookieHeader === null) {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
return parse(cookieHeader, options)[name];
|
|
22
|
+
}
|
|
23
|
+
function deleteCookie(headers, name, options = {}) {
|
|
24
|
+
return setCookie(headers, name, "", {
|
|
25
|
+
...options,
|
|
26
|
+
maxAge: 0
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { deleteCookie as d, getCookie as g, setCookie as s };
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, Meta, Schemas, ContractProcedureDef, AnyContractRouter, ContractProcedure, InferProcedureClientInputs, InferSchemaOutput, EnhanceRouteOptions, MergedErrorMap, AnyContractProcedure, ContractRouter, AnySchema, ErrorFromErrorMap } from '@temporary-name/contract';
|
|
2
|
+
import { ORPCErrorCode, MaybeOptionalOptions, ORPCErrorOptions, ORPCError, Promisable, StandardLazyRequest, HTTPPath, ClientContext, Interceptor, PromiseWithError, Value, Client } from '@temporary-name/shared';
|
|
3
|
+
|
|
4
|
+
type Context = Record<PropertyKey, any>;
|
|
5
|
+
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
|
6
|
+
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
|
7
|
+
type BuildContextWithAuth<TContext extends Context, TAuthContext> = MergedCurrentContext<TContext, {
|
|
8
|
+
auth: TAuthContext | ('auth' extends keyof TContext ? TContext['auth'] : never);
|
|
9
|
+
}>;
|
|
10
|
+
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
|
11
|
+
|
|
12
|
+
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
|
13
|
+
type ORPCErrorConstructorMapItem<TCode extends ORPCErrorCode, TInData> = (...rest: MaybeOptionalOptions<ORPCErrorConstructorMapItemOptions<TInData>>) => ORPCError<TCode, TInData>;
|
|
14
|
+
type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
15
|
+
[K in keyof T]: K extends ORPCErrorCode ? T[K] extends ErrorMapItem<infer UInputSchema> ? ORPCErrorConstructorMapItem<K, InferSchemaInput<UInputSchema>> : never : never;
|
|
16
|
+
};
|
|
17
|
+
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
|
18
|
+
|
|
19
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
20
|
+
output: TOutput;
|
|
21
|
+
context: TOutContext;
|
|
22
|
+
}>;
|
|
23
|
+
interface MiddlewareNextFn<TOutput> {
|
|
24
|
+
<U extends Context = {}>(options?: {
|
|
25
|
+
context?: U;
|
|
26
|
+
}): MiddlewareResult<U, TOutput>;
|
|
27
|
+
}
|
|
28
|
+
interface MiddlewareOutputFn<TOutput> {
|
|
29
|
+
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
30
|
+
}
|
|
31
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> extends ProcedureHandlerOptions<TInContext, TErrorConstructorMap, TMeta> {
|
|
32
|
+
next: MiddlewareNextFn<TOutput>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A function that represents a middleware.
|
|
36
|
+
*
|
|
37
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
38
|
+
*/
|
|
39
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
40
|
+
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
41
|
+
}
|
|
42
|
+
type AnyMiddleware = Middleware<any, any, any, any, any, any>;
|
|
43
|
+
interface MapInputMiddleware<TInput, TMappedInput> {
|
|
44
|
+
(input: TInput): TMappedInput;
|
|
45
|
+
}
|
|
46
|
+
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
47
|
+
|
|
48
|
+
type DefaultProcedureHandlerOptions = ProcedureHandlerOptions<Context, ORPCErrorConstructorMap<ErrorMap>, Meta>;
|
|
49
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
50
|
+
context: TCurrentContext;
|
|
51
|
+
path: readonly string[];
|
|
52
|
+
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
53
|
+
signal?: AbortSignal;
|
|
54
|
+
request?: StandardLazyRequest;
|
|
55
|
+
lastEventId: string | undefined;
|
|
56
|
+
errors: TErrorConstructorMap;
|
|
57
|
+
}
|
|
58
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput extends {
|
|
59
|
+
path: any;
|
|
60
|
+
query: any;
|
|
61
|
+
body: any;
|
|
62
|
+
}, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
63
|
+
(input: TInput, opt: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
|
|
64
|
+
}
|
|
65
|
+
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TSchemas, TErrorMap, TMeta> {
|
|
66
|
+
__initialContext?: (type: TInitialContext) => unknown;
|
|
67
|
+
middlewares: readonly AnyMiddleware[];
|
|
68
|
+
authConfigs: TypedAuthConfig[];
|
|
69
|
+
inputValidationIndex: number;
|
|
70
|
+
outputValidationIndex: number;
|
|
71
|
+
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* This class represents a procedure.
|
|
75
|
+
*
|
|
76
|
+
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
77
|
+
*/
|
|
78
|
+
declare class Procedure<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
79
|
+
/**
|
|
80
|
+
* This property holds the defined options.
|
|
81
|
+
*/
|
|
82
|
+
'~orpc': ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>;
|
|
83
|
+
constructor(def: ProcedureDef<TInitialContext, TCurrentContext, TSchemas, TErrorMap, TMeta>);
|
|
84
|
+
}
|
|
85
|
+
type AnyProcedure = Procedure<any, any, Schemas, any, any>;
|
|
86
|
+
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
87
|
+
|
|
88
|
+
type ValidatedAuthContext = {};
|
|
89
|
+
interface BasicAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
90
|
+
tokenPrefix?: string;
|
|
91
|
+
validate: (username: string, password: string, options: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<any>, TMeta>) => Promisable<TAuthContext>;
|
|
92
|
+
}
|
|
93
|
+
interface TokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
94
|
+
tokenPrefix?: string;
|
|
95
|
+
validate: (token: string, options: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<any>, TMeta>) => Promisable<TAuthContext>;
|
|
96
|
+
}
|
|
97
|
+
interface NamedTokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> extends TokenAuthConfig<TAuthContext, TCurrentContext, TMeta> {
|
|
98
|
+
name: string;
|
|
99
|
+
}
|
|
100
|
+
type AuthType = 'header' | 'query' | 'cookie' | 'bearer' | 'basic' | 'none';
|
|
101
|
+
type AuthConfig<TAuthType extends AuthType, TAuthContext extends ValidatedAuthContext = ValidatedAuthContext, TCurrentContext extends Context = Context, TMeta extends Meta = Meta> = TAuthType extends 'basic' ? BasicAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'bearer' ? TokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'header' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'query' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'cookie' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'none' ? {} : never;
|
|
102
|
+
type TypedAuthConfig<TAuthType extends AuthType = AuthType> = {
|
|
103
|
+
type: TAuthType;
|
|
104
|
+
} & AuthConfig<TAuthType, object>;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Represents a router, which defines a hierarchical structure of procedures.
|
|
108
|
+
*
|
|
109
|
+
* @info A procedure is a router too.
|
|
110
|
+
* @see {@link https://orpc.unnoq.com/docs/contract-first/define-contract#contract-router Contract Router Docs}
|
|
111
|
+
*/
|
|
112
|
+
type Router<T extends AnyContractRouter, TInitialContext extends Context> = T extends ContractProcedure<infer USchemas, infer UErrorMap, infer UMeta> ? Procedure<TInitialContext, any, USchemas, UErrorMap, UMeta> : {
|
|
113
|
+
[K in keyof T]: T[K] extends AnyContractRouter ? Lazyable<Router<T[K], TInitialContext>> : never;
|
|
114
|
+
};
|
|
115
|
+
type AnyRouter = Router<any, any>;
|
|
116
|
+
type InferRouterInitialContext<T extends AnyRouter> = T extends Router<any, infer UInitialContext> ? UInitialContext : never;
|
|
117
|
+
/**
|
|
118
|
+
* Infer all initial context of the router.
|
|
119
|
+
*
|
|
120
|
+
* @info A procedure is a router too.
|
|
121
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
122
|
+
*/
|
|
123
|
+
type InferRouterInitialContexts<T extends AnyRouter> = T extends Procedure<infer UInitialContext, any, any, any, any> ? UInitialContext : {
|
|
124
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInitialContexts<U> : never;
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* Infer all current context of the router.
|
|
128
|
+
*
|
|
129
|
+
* @info A procedure is a router too.
|
|
130
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
131
|
+
*/
|
|
132
|
+
type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any, infer UCurrentContext, any, any, any> ? UCurrentContext : {
|
|
133
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterCurrentContexts<U> : never;
|
|
134
|
+
};
|
|
135
|
+
/**
|
|
136
|
+
* Infer all router inputs
|
|
137
|
+
*
|
|
138
|
+
* @info A procedure is a router too.
|
|
139
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
140
|
+
*/
|
|
141
|
+
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferProcedureClientInputs<USchemas> : {
|
|
142
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
143
|
+
};
|
|
144
|
+
/**
|
|
145
|
+
* Infer all router outputs
|
|
146
|
+
*
|
|
147
|
+
* @info A procedure is a router too.
|
|
148
|
+
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
149
|
+
*/
|
|
150
|
+
type InferRouterOutputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferSchemaOutput<USchemas['outputSchema']> : {
|
|
151
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterOutputs<U> : never;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
declare function getRouter<T extends Lazyable<AnyRouter | undefined>>(router: T, path: readonly string[]): T extends Lazy<any> ? Lazy<AnyRouter | undefined> : Lazyable<AnyRouter | undefined>;
|
|
155
|
+
type AccessibleLazyRouter<T extends Lazyable<AnyRouter | undefined>> = T extends Lazy<infer U extends AnyRouter | undefined | Lazy<AnyRouter | undefined>> ? AccessibleLazyRouter<U> : T extends AnyProcedure | undefined ? Lazy<T> : Lazy<T> & {
|
|
156
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? AccessibleLazyRouter<T[K]> : never;
|
|
157
|
+
};
|
|
158
|
+
declare function createAccessibleLazyRouter<T extends Lazy<AnyRouter | undefined>>(lazied: T): AccessibleLazyRouter<T>;
|
|
159
|
+
type EnhancedRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap> = T extends Lazy<infer U extends AnyRouter> ? AccessibleLazyRouter<EnhancedRouter<U, TInitialContext, TCurrentContext, TErrorMap>> : T extends (Procedure<infer UInitialContext, infer UCurrentContext, infer USchemas, infer UErrorMap, infer UMeta>) ? Procedure<MergedInitialContext<TInitialContext, UInitialContext, TCurrentContext>, UCurrentContext, USchemas, MergedErrorMap<TErrorMap, UErrorMap>, UMeta> : {
|
|
160
|
+
[K in keyof T]: T[K] extends Lazyable<AnyRouter> ? EnhancedRouter<T[K], TInitialContext, TCurrentContext, TErrorMap> : never;
|
|
161
|
+
};
|
|
162
|
+
interface EnhanceRouterOptions<TErrorMap extends ErrorMap> extends EnhanceRouteOptions {
|
|
163
|
+
middlewares: readonly AnyMiddleware[];
|
|
164
|
+
errorMap: TErrorMap;
|
|
165
|
+
dedupeLeadingMiddlewares: boolean;
|
|
166
|
+
}
|
|
167
|
+
declare function enhanceRouter<T extends Lazyable<AnyRouter>, TInitialContext extends Context, TCurrentContext extends Context, TErrorMap extends ErrorMap>(router: T, options: EnhanceRouterOptions<TErrorMap>): EnhancedRouter<T, TInitialContext, TCurrentContext, TErrorMap>;
|
|
168
|
+
interface TraverseContractProceduresOptions {
|
|
169
|
+
router: AnyContractRouter | AnyRouter;
|
|
170
|
+
path: readonly string[];
|
|
171
|
+
}
|
|
172
|
+
interface TraverseContractProcedureCallbackOptions {
|
|
173
|
+
contract: AnyContractProcedure | AnyProcedure;
|
|
174
|
+
path: readonly string[];
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* @deprecated Use `TraverseContractProcedureCallbackOptions` instead.
|
|
178
|
+
*/
|
|
179
|
+
type ContractProcedureCallbackOptions = TraverseContractProcedureCallbackOptions;
|
|
180
|
+
interface LazyTraverseContractProceduresOptions {
|
|
181
|
+
router: Lazy<AnyRouter>;
|
|
182
|
+
path: readonly string[];
|
|
183
|
+
}
|
|
184
|
+
declare function traverseContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void, lazyOptions?: LazyTraverseContractProceduresOptions[]): LazyTraverseContractProceduresOptions[];
|
|
185
|
+
declare function resolveContractProcedures(options: TraverseContractProceduresOptions, callback: (options: TraverseContractProcedureCallbackOptions) => void): Promise<void>;
|
|
186
|
+
type UnlaziedRouter<T extends AnyRouter> = T extends AnyProcedure ? T : {
|
|
187
|
+
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? UnlaziedRouter<U> : never;
|
|
188
|
+
};
|
|
189
|
+
declare function unlazyRouter<T extends AnyRouter>(router: T): Promise<UnlaziedRouter<T>>;
|
|
190
|
+
|
|
191
|
+
declare const LAZY_SYMBOL: unique symbol;
|
|
192
|
+
interface LazyMeta {
|
|
193
|
+
prefix?: HTTPPath;
|
|
194
|
+
}
|
|
195
|
+
interface Lazy<T> {
|
|
196
|
+
[LAZY_SYMBOL]: {
|
|
197
|
+
loader: () => Promise<{
|
|
198
|
+
default: T;
|
|
199
|
+
}>;
|
|
200
|
+
meta: LazyMeta;
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
type Lazyable<T> = T | Lazy<T>;
|
|
204
|
+
/**
|
|
205
|
+
* @internal
|
|
206
|
+
*/
|
|
207
|
+
declare function lazyInternal<T>(loader: () => Promise<{
|
|
208
|
+
default: T;
|
|
209
|
+
}>, meta?: LazyMeta): Lazy<T>;
|
|
210
|
+
/**
|
|
211
|
+
* Creates a lazy-loaded item.
|
|
212
|
+
*
|
|
213
|
+
* @warning The `prefix` in `meta` only holds metadata and does not apply the prefix to the lazy router, use `os.prefix(...).lazyRoute(...)` instead.
|
|
214
|
+
*/
|
|
215
|
+
declare function lazy<T extends Router<ContractRouter<{}>, any>>(prefix: HTTPPath, loader: () => Promise<{
|
|
216
|
+
default: T;
|
|
217
|
+
}>): EnhancedRouter<Lazy<T>, {}, {}, {}>;
|
|
218
|
+
declare function isLazy(item: unknown): item is Lazy<any>;
|
|
219
|
+
declare function getLazyMeta(lazied: Lazy<any>): LazyMeta;
|
|
220
|
+
declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
|
|
221
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
222
|
+
}>;
|
|
223
|
+
|
|
224
|
+
type ProcedureClient<TClientContext extends ClientContext, TSchemas extends Schemas, TErrorMap extends ErrorMap> = Client<TClientContext, InferProcedureClientInputs<TSchemas>, InferSchemaOutput<TSchemas['outputSchema']>, ErrorFromErrorMap<TErrorMap>>;
|
|
225
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> extends ProcedureHandlerOptions<TInitialContext, ORPCErrorConstructorMap<TErrorMap>, TMeta> {
|
|
226
|
+
input: {
|
|
227
|
+
path: unknown;
|
|
228
|
+
query: unknown;
|
|
229
|
+
body: unknown;
|
|
230
|
+
};
|
|
231
|
+
}
|
|
232
|
+
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
|
233
|
+
/**
|
|
234
|
+
* This is helpful for logging and analytics.
|
|
235
|
+
*/
|
|
236
|
+
path?: readonly string[];
|
|
237
|
+
interceptors?: Interceptor<ProcedureClientInterceptorOptions<TInitialContext, TErrorMap, TMeta>, PromiseWithError<InferSchemaOutput<TOutputSchema>, ErrorFromErrorMap<TErrorMap>>>[];
|
|
238
|
+
} & (Record<never, never> extends TInitialContext ? {
|
|
239
|
+
context?: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
240
|
+
} : {
|
|
241
|
+
context: Value<Promisable<TInitialContext>, [clientContext: TClientContext]>;
|
|
242
|
+
});
|
|
243
|
+
/**
|
|
244
|
+
* Create Server-side client from a procedure.
|
|
245
|
+
*
|
|
246
|
+
* @see {@link https://orpc.unnoq.com/docs/client/server-side Server-side Client Docs}
|
|
247
|
+
*/
|
|
248
|
+
declare function createProcedureClient<TInitialContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TSchemas, TErrorMap, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TSchemas['outputSchema'], TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TSchemas, TErrorMap>;
|
|
249
|
+
|
|
250
|
+
export { enhanceRouter as $, middlewareOutputFn as F, isProcedure as K, Procedure as P, createProcedureClient as Q, getRouter as Y, createAccessibleLazyRouter as _, traverseContractProcedures as a4, resolveContractProcedures as a5, unlazyRouter as a7, mergeCurrentContext as m, createORPCErrorConstructorMap as p, LAZY_SYMBOL as q, lazyInternal as s, lazy as t, isLazy as u, getLazyMeta as v, unlazy as w };
|
|
251
|
+
export type { AuthType as A, BuildContextWithAuth as B, Context as C, MiddlewareOptions as D, EnhanceRouterOptions as E, DefaultProcedureHandlerOptions as G, ProcedureHandlerOptions as H, InferRouterInitialContext as I, ProcedureDef as J, Lazy as L, Middleware as M, ProcedureClientInterceptorOptions as N, ORPCErrorConstructorMap as O, Router as R, InferRouterInitialContexts as S, TypedAuthConfig as T, InferRouterCurrentContexts as U, ValidatedAuthContext as V, InferRouterInputs as W, InferRouterOutputs as X, AccessibleLazyRouter as Z, CreateProcedureClientOptions as a, TraverseContractProceduresOptions as a0, TraverseContractProcedureCallbackOptions as a1, ContractProcedureCallbackOptions as a2, LazyTraverseContractProceduresOptions as a3, UnlaziedRouter as a6, ProcedureClient as b, MergedInitialContext as c, MergedCurrentContext as d, AuthConfig as e, ProcedureHandler as f, EnhancedRouter as g, MapInputMiddleware as h, AnyMiddleware as i, AnyProcedure as j, Lazyable as k, AnyRouter as l, ORPCErrorConstructorMapItemOptions as n, ORPCErrorConstructorMapItem as o, LazyMeta as r, MiddlewareResult as x, MiddlewareNextFn as y, MiddlewareOutputFn as z };
|