@trpc/server 10.0.0-alpha.23 → 10.0.0-alpha.26
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/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +1 -1
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +100 -20
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +100 -20
- package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +99 -19
- package/adapters/aws-lambda/index.d.ts +1 -0
- package/adapters/aws-lambda/index.js +1 -0
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.d.ts +1 -0
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.dev.js +25 -0
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.js +7 -0
- package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.prod.js +25 -0
- package/adapters/lambda/dist/trpc-server-adapters-lambda.esm.js +21 -0
- package/dist/TRPCError.d.ts.map +1 -1
- package/dist/adapters/aws-lambda/index.d.ts +14 -0
- package/dist/adapters/aws-lambda/index.d.ts.map +1 -0
- package/dist/adapters/aws-lambda/utils.d.ts +34 -0
- package/dist/adapters/aws-lambda/utils.d.ts.map +1 -0
- package/dist/adapters/express.d.ts +1 -1
- package/dist/adapters/express.d.ts.map +1 -1
- package/dist/adapters/express.js +4 -4
- package/dist/adapters/express.mjs +4 -4
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +1 -1
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +3 -3
- package/dist/adapters/fastify/index.mjs +3 -3
- package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -1
- package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fetch/index.js +3 -3
- package/dist/adapters/fetch/index.mjs +3 -3
- package/dist/adapters/fetch/types.d.ts +1 -1
- package/dist/adapters/fetch/types.d.ts.map +1 -1
- package/dist/adapters/lambda/index.d.ts +12 -0
- package/dist/adapters/lambda/index.d.ts.map +1 -0
- package/dist/adapters/next.d.ts +1 -1
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +4 -4
- package/dist/adapters/next.mjs +4 -4
- package/dist/adapters/node-http/index.js +4 -4
- package/dist/adapters/node-http/index.mjs +4 -4
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/types.d.ts +3 -3
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts +2 -2
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +4 -4
- package/dist/adapters/standalone.mjs +4 -4
- package/dist/adapters/ws.d.ts +3 -2
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +3 -8
- package/dist/adapters/ws.mjs +3 -8
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/initTRPC.d.ts +82 -0
- package/dist/core/initTRPC.d.ts.map +1 -0
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +202 -0
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -0
- package/dist/core/internals/config.d.ts +38 -0
- package/dist/core/internals/config.d.ts.map +1 -0
- package/dist/core/internals/getParseFn.d.ts +9 -0
- package/dist/core/internals/getParseFn.d.ts.map +1 -0
- package/dist/core/internals/internalProcedure.d.ts +61 -0
- package/dist/core/internals/internalProcedure.d.ts.map +1 -0
- package/dist/core/internals/mergeRouters.d.ts +3 -0
- package/dist/core/internals/mergeRouters.d.ts.map +1 -0
- package/dist/core/internals/mergeWithoutOverrides.d.ts +5 -0
- package/dist/core/internals/mergeWithoutOverrides.d.ts.map +1 -0
- package/dist/core/internals/omitPrototype.d.ts +6 -0
- package/dist/core/internals/omitPrototype.d.ts.map +1 -0
- package/dist/core/internals/procedureBuilder.d.ts +71 -0
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -0
- package/dist/core/internals/utils.d.ts +44 -0
- package/dist/core/internals/utils.d.ts.map +1 -0
- package/dist/core/middleware.d.ts +78 -0
- package/dist/core/middleware.d.ts.map +1 -0
- package/dist/core/parser.d.ts +25 -0
- package/dist/core/parser.d.ts.map +1 -0
- package/dist/core/procedure.d.ts +57 -0
- package/dist/core/procedure.d.ts.map +1 -0
- package/dist/core/router.d.ts +110 -0
- package/dist/core/router.d.ts.map +1 -0
- package/dist/core/types.d.ts +17 -0
- package/dist/core/types.d.ts.map +1 -0
- package/dist/{internals → deprecated/internals}/callProcedure.d.ts +4 -1
- package/dist/deprecated/internals/callProcedure.d.ts.map +1 -0
- package/dist/{internals → deprecated/internals}/middlewares.d.ts +16 -1
- package/dist/deprecated/internals/middlewares.d.ts.map +1 -0
- package/dist/{internals → deprecated/internals}/procedure.d.ts +10 -1
- package/dist/deprecated/internals/procedure.d.ts.map +1 -0
- package/dist/deprecated/interop.d.ts +34 -0
- package/dist/deprecated/interop.d.ts.map +1 -0
- package/dist/{router.d.ts → deprecated/router.d.ts} +27 -6
- package/dist/deprecated/router.d.ts.map +1 -0
- package/dist/{envelopes-1deb2fa7.mjs → envelopes-af8c2959.mjs} +12 -1
- package/dist/{envelopes-a953b930.js → envelopes-b6dd0367.js} +11 -1
- package/dist/error/TRPCError.d.ts +11 -0
- package/dist/error/TRPCError.d.ts.map +1 -0
- package/dist/error/formatter.d.ts +33 -0
- package/dist/error/formatter.d.ts.map +1 -0
- package/dist/error/utils.d.ts +5 -0
- package/dist/error/utils.d.ts.map +1 -0
- package/dist/http/index.d.ts +1 -1
- package/dist/http/index.d.ts.map +1 -1
- package/dist/http/internals/types.d.ts +9 -7
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts +1 -1
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/http/{responseMeta.d.ts → types.d.ts} +1 -1
- package/dist/http/types.d.ts.map +1 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +686 -12
- package/dist/index.mjs +688 -16
- package/dist/internals/transformTRPCResponse.d.ts +1 -1
- package/dist/internals/transformTRPCResponse.d.ts.map +1 -1
- package/dist/internals/types.d.ts +23 -0
- package/dist/internals/types.d.ts.map +1 -0
- package/dist/{nodeHTTPRequestHandler-1cdb6383.mjs → nodeHTTPRequestHandler-5ad62229.mjs} +1 -1
- package/dist/{nodeHTTPRequestHandler-848f07b5.js → nodeHTTPRequestHandler-9ddd18f2.js} +1 -1
- package/dist/{resolveHTTPResponse-ec3dccaa.js → resolveHTTPResponse-5932d785.js} +16 -10
- package/dist/{resolveHTTPResponse-54a79e6c.mjs → resolveHTTPResponse-ccb9cfa1.mjs} +15 -9
- package/dist/rpc/envelopes.d.ts +1 -1
- package/dist/rpc/envelopes.d.ts.map +1 -1
- package/dist/rpc/index.js +1 -1
- package/dist/rpc/index.mjs +1 -1
- package/dist/rpc/internals/invert.d.ts +18 -0
- package/dist/rpc/internals/invert.d.ts.map +1 -0
- package/dist/{transformTRPCResponse-333bed6c.js → transformTRPCResponse-0c261655.js} +0 -27
- package/dist/{transformTRPCResponse-e0cba527.mjs → transformTRPCResponse-747b9a68.mjs} +1 -27
- package/dist/transformer.d.ts +14 -0
- package/dist/transformer.d.ts.map +1 -1
- package/dist/types.d.ts +9 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +11 -4
- package/src/TRPCError.ts +1 -1
- package/src/adapters/aws-lambda/index.ts +156 -0
- package/src/adapters/aws-lambda/utils.ts +91 -0
- package/src/adapters/express.ts +1 -1
- package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +2 -2
- package/src/adapters/fetch/fetchRequestHandler.ts +1 -1
- package/src/adapters/fetch/types.ts +1 -1
- package/src/adapters/lambda/index.ts +18 -0
- package/src/adapters/next.ts +1 -1
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +3 -2
- package/src/adapters/node-http/types.ts +5 -3
- package/src/adapters/standalone.ts +1 -1
- package/src/adapters/ws.ts +5 -10
- package/src/core/index.ts +5 -0
- package/src/core/initTRPC.ts +90 -0
- package/src/core/internals/__generated__/mergeRoutersGeneric.ts +306 -0
- package/src/core/internals/config.ts +44 -0
- package/src/core/internals/getParseFn.ts +47 -0
- package/src/core/internals/internalProcedure.ts +265 -0
- package/src/core/internals/mergeRouters.ts +57 -0
- package/src/core/internals/mergeWithoutOverrides.ts +19 -0
- package/src/core/internals/omitPrototype.ts +9 -0
- package/src/core/internals/procedureBuilder.ts +100 -0
- package/src/core/internals/utils.ts +64 -0
- package/src/core/middleware.ts +93 -0
- package/src/core/parser.ts +45 -0
- package/src/core/procedure.ts +72 -0
- package/src/core/router.ts +313 -0
- package/src/core/types.ts +37 -0
- package/src/{internals → deprecated/internals}/callProcedure.ts +4 -1
- package/src/{internals → deprecated/internals}/middlewares.ts +17 -1
- package/src/{internals → deprecated/internals}/procedure.ts +19 -4
- package/src/deprecated/interop.ts +169 -0
- package/src/{router.ts → deprecated/router.ts} +49 -12
- package/src/error/TRPCError.ts +27 -0
- package/src/error/formatter.ts +51 -0
- package/src/error/utils.ts +49 -0
- package/src/http/index.ts +1 -1
- package/src/http/internals/types.ts +9 -11
- package/src/http/resolveHTTPResponse.ts +6 -11
- package/src/http/{responseMeta.ts → types.ts} +0 -0
- package/src/index.ts +2 -1
- package/src/internals/transformTRPCResponse.ts +1 -1
- package/src/internals/types.ts +24 -0
- package/src/rpc/codes.ts +1 -1
- package/src/rpc/envelopes.ts +1 -1
- package/src/rpc/internals/invert.ts +27 -0
- package/src/transformer.ts +28 -0
- package/src/types.ts +12 -0
- package/dist/http/responseMeta.d.ts.map +0 -1
- package/dist/internals/baseHandlerOptions.d.ts +0 -13
- package/dist/internals/baseHandlerOptions.d.ts.map +0 -1
- package/dist/internals/callProcedure.d.ts.map +0 -1
- package/dist/internals/middlewares.d.ts.map +0 -1
- package/dist/internals/onErrorFunction.d.ts +0 -11
- package/dist/internals/onErrorFunction.d.ts.map +0 -1
- package/dist/internals/procedure.d.ts.map +0 -1
- package/dist/router.d.ts.map +0 -1
- package/src/internals/baseHandlerOptions.ts +0 -13
- package/src/internals/onErrorFunction.ts +0 -11
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
export type ParserZodEsque<TInput, TParsedInput> = {
|
|
2
|
+
_input: TInput;
|
|
3
|
+
_output: TParsedInput;
|
|
4
|
+
};
|
|
5
|
+
|
|
6
|
+
export type ParserMyZodEsque<TInput> = {
|
|
7
|
+
parse: (input: any) => TInput;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type ParserSuperstructEsque<TInput> = {
|
|
11
|
+
create: (input: unknown) => TInput;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type ParserCustomValidatorEsque<TInput> = (
|
|
15
|
+
input: unknown,
|
|
16
|
+
) => TInput | Promise<TInput>;
|
|
17
|
+
|
|
18
|
+
export type ParserYupEsque<TInput> = {
|
|
19
|
+
validateSync: (input: unknown) => TInput;
|
|
20
|
+
};
|
|
21
|
+
export type ParserWithoutInput<TInput> =
|
|
22
|
+
| ParserYupEsque<TInput>
|
|
23
|
+
| ParserSuperstructEsque<TInput>
|
|
24
|
+
| ParserCustomValidatorEsque<TInput>
|
|
25
|
+
| ParserMyZodEsque<TInput>;
|
|
26
|
+
|
|
27
|
+
export type ParserWithInputOutput<TInput, TParsedInput> = ParserZodEsque<
|
|
28
|
+
TInput,
|
|
29
|
+
TParsedInput
|
|
30
|
+
>;
|
|
31
|
+
|
|
32
|
+
export type Parser = ParserWithoutInput<any> | ParserWithInputOutput<any, any>;
|
|
33
|
+
|
|
34
|
+
export type inferParser<TParser extends Parser> =
|
|
35
|
+
TParser extends ParserWithInputOutput<infer $TIn, infer $TOut>
|
|
36
|
+
? {
|
|
37
|
+
in: $TIn;
|
|
38
|
+
out: $TOut;
|
|
39
|
+
}
|
|
40
|
+
: TParser extends ParserWithoutInput<infer $InOut>
|
|
41
|
+
? {
|
|
42
|
+
in: $InOut;
|
|
43
|
+
out: $InOut;
|
|
44
|
+
}
|
|
45
|
+
: never;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { UnsetMarker } from './internals/utils';
|
|
2
|
+
|
|
3
|
+
type ClientContext = Record<string, unknown>;
|
|
4
|
+
export interface ProcedureOptions {
|
|
5
|
+
/**
|
|
6
|
+
* Client-side context
|
|
7
|
+
*/
|
|
8
|
+
context?: ClientContext;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
export interface ProcedureParams<
|
|
15
|
+
TContextIn = unknown,
|
|
16
|
+
TContextOut = unknown,
|
|
17
|
+
TInputIn = unknown,
|
|
18
|
+
TInputOut = unknown,
|
|
19
|
+
TOutputIn = unknown,
|
|
20
|
+
TOutputOut = unknown,
|
|
21
|
+
TMeta = unknown,
|
|
22
|
+
> {
|
|
23
|
+
/**
|
|
24
|
+
* @internal
|
|
25
|
+
*/
|
|
26
|
+
_meta: TMeta;
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
_ctx_in: TContextIn;
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
_ctx_out: TContextOut;
|
|
35
|
+
/**
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
_output_in: TOutputIn;
|
|
39
|
+
/**
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
_output_out: TOutputOut;
|
|
43
|
+
/**
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
_input_in: TInputIn;
|
|
47
|
+
/**
|
|
48
|
+
* @internal
|
|
49
|
+
*/
|
|
50
|
+
_input_out: TInputOut;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
export type ProcedureArgs<TParams extends ProcedureParams> =
|
|
57
|
+
TParams['_input_in'] extends UnsetMarker
|
|
58
|
+
? [input?: undefined | void, opts?: ProcedureOptions]
|
|
59
|
+
: undefined extends TParams['_input_in']
|
|
60
|
+
? [input?: TParams['_input_in'] | void, opts?: ProcedureOptions]
|
|
61
|
+
: [input: TParams['_input_in'], opts?: ProcedureOptions];
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @internal
|
|
65
|
+
*/
|
|
66
|
+
export interface Procedure<TParams extends ProcedureParams> {
|
|
67
|
+
(...args: ProcedureArgs<TParams>): Promise<TParams['_output_out']>;
|
|
68
|
+
/**
|
|
69
|
+
* @deprecated use `._def.meta` instead
|
|
70
|
+
*/
|
|
71
|
+
meta?: TParams['_meta'];
|
|
72
|
+
}
|
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
import { TRPCError } from '../TRPCError';
|
|
3
|
+
import {
|
|
4
|
+
DefaultErrorShape,
|
|
5
|
+
ErrorFormatter,
|
|
6
|
+
defaultFormatter,
|
|
7
|
+
} from '../error/formatter';
|
|
8
|
+
import { getHTTPStatusCodeFromError } from '../http/internals/getHTTPStatusCode';
|
|
9
|
+
import { TRPCErrorShape, TRPC_ERROR_CODES_BY_KEY } from '../rpc';
|
|
10
|
+
import { CombinedDataTransformer, defaultTransformer } from '../transformer';
|
|
11
|
+
import { RootConfig } from './internals/config';
|
|
12
|
+
import {
|
|
13
|
+
InternalProcedure,
|
|
14
|
+
InternalProcedureCallOptions,
|
|
15
|
+
} from './internals/internalProcedure';
|
|
16
|
+
import { mergeWithoutOverrides } from './internals/mergeWithoutOverrides';
|
|
17
|
+
import { omitPrototype } from './internals/omitPrototype';
|
|
18
|
+
import { EnsureRecord, ValidateShape } from './internals/utils';
|
|
19
|
+
import { Procedure } from './procedure';
|
|
20
|
+
import { ProcedureType } from './types';
|
|
21
|
+
|
|
22
|
+
// FIXME this should properly use TContext maybe?
|
|
23
|
+
export type ProcedureRecord<_TContext> = Record<string, Procedure<any>>;
|
|
24
|
+
type AnyProcedureRecord = ProcedureRecord<any>;
|
|
25
|
+
export interface ProcedureStructure {
|
|
26
|
+
queries: AnyProcedureRecord;
|
|
27
|
+
mutations: AnyProcedureRecord;
|
|
28
|
+
subscriptions: AnyProcedureRecord;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface RouterParams<
|
|
32
|
+
// FIXME this should use RootConfig
|
|
33
|
+
TContext,
|
|
34
|
+
TErrorShape extends TRPCErrorShape<number>,
|
|
35
|
+
TMeta extends Record<string, unknown>,
|
|
36
|
+
TQueries extends ProcedureRecord<TContext>,
|
|
37
|
+
TMutations extends ProcedureRecord<TContext>,
|
|
38
|
+
TSubscriptions extends ProcedureRecord<TContext>,
|
|
39
|
+
> extends ProcedureStructure {
|
|
40
|
+
/**
|
|
41
|
+
* @internal
|
|
42
|
+
*/
|
|
43
|
+
_ctx: TContext;
|
|
44
|
+
/**
|
|
45
|
+
* @internal
|
|
46
|
+
*/
|
|
47
|
+
_errorShape: TErrorShape;
|
|
48
|
+
/**
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
_meta: TMeta;
|
|
52
|
+
queries: TQueries;
|
|
53
|
+
mutations: TMutations;
|
|
54
|
+
subscriptions: TSubscriptions;
|
|
55
|
+
errorFormatter: ErrorFormatter<TContext, TErrorShape>;
|
|
56
|
+
transformer: CombinedDataTransformer;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type AnyRouterParams<TContext = any> = RouterParams<
|
|
60
|
+
TContext,
|
|
61
|
+
any,
|
|
62
|
+
any,
|
|
63
|
+
any,
|
|
64
|
+
any,
|
|
65
|
+
any
|
|
66
|
+
>;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @internal
|
|
70
|
+
*/
|
|
71
|
+
export type inferHandlerInput<TProcedure extends Procedure<any>> =
|
|
72
|
+
TProcedure extends Procedure<infer TParams>
|
|
73
|
+
? undefined extends TParams['_input_in'] // ? is input optional
|
|
74
|
+
? unknown extends TParams['_input_in'] // ? is input unset
|
|
75
|
+
? [(null | undefined)?] // -> there is no input
|
|
76
|
+
: [(TParams['_input_in'] | null | undefined)?] // -> there is optional input
|
|
77
|
+
: [TParams['_input_in']] // -> input is required
|
|
78
|
+
: [(undefined | null)?]; // -> there is no input
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
type inferHandlerFn<TProcedures extends ProcedureRecord<any>> = <
|
|
84
|
+
TProcedure extends TProcedures[TPath],
|
|
85
|
+
TPath extends keyof TProcedures & string,
|
|
86
|
+
>(
|
|
87
|
+
path: TPath,
|
|
88
|
+
...args: inferHandlerInput<TProcedure>
|
|
89
|
+
) => ReturnType<TProcedure>;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* This only exists b/c of interop mode
|
|
93
|
+
* @internal
|
|
94
|
+
*/
|
|
95
|
+
|
|
96
|
+
type RouterCaller<TParams extends AnyRouterParams> = (ctx: TParams['_ctx']) => {
|
|
97
|
+
query: inferHandlerFn<TParams['queries']>;
|
|
98
|
+
mutation: inferHandlerFn<TParams['mutations']>;
|
|
99
|
+
subscription: inferHandlerFn<TParams['subscriptions']>;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export interface Router<TParams extends AnyRouterParams>
|
|
103
|
+
extends ProcedureStructure {
|
|
104
|
+
_def: RouterParams<
|
|
105
|
+
TParams['_ctx'],
|
|
106
|
+
TParams['_errorShape'],
|
|
107
|
+
TParams['_meta'],
|
|
108
|
+
TParams['queries'],
|
|
109
|
+
TParams['mutations'],
|
|
110
|
+
TParams['subscriptions']
|
|
111
|
+
>;
|
|
112
|
+
queries: TParams['queries'];
|
|
113
|
+
mutations: TParams['mutations'];
|
|
114
|
+
subscriptions: TParams['subscriptions'];
|
|
115
|
+
errorFormatter: TParams['errorFormatter'];
|
|
116
|
+
transformer: TParams['transformer'];
|
|
117
|
+
|
|
118
|
+
createCaller: RouterCaller<TParams>;
|
|
119
|
+
getErrorShape(opts: {
|
|
120
|
+
error: TRPCError;
|
|
121
|
+
type: ProcedureType | 'unknown';
|
|
122
|
+
path: string | undefined;
|
|
123
|
+
input: unknown;
|
|
124
|
+
ctx: undefined | TParams['_ctx'];
|
|
125
|
+
}): TParams['_errorShape'];
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
export type RouterOptions<TContext> = Partial<AnyRouterParams<TContext>>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @internal
|
|
135
|
+
*/
|
|
136
|
+
export type RouterDefaultOptions<TContext> = Pick<
|
|
137
|
+
AnyRouterParams<TContext>,
|
|
138
|
+
'transformer' | 'errorFormatter'
|
|
139
|
+
>;
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
144
|
+
type RouterBuildOptions<TContext> = Pick<
|
|
145
|
+
RouterOptions<TContext>,
|
|
146
|
+
'queries' | 'subscriptions' | 'mutations'
|
|
147
|
+
>;
|
|
148
|
+
|
|
149
|
+
export type AnyRouter = Router<any>;
|
|
150
|
+
|
|
151
|
+
const emptyRouter = {
|
|
152
|
+
_ctx: null as any,
|
|
153
|
+
_errorShape: null as any,
|
|
154
|
+
_meta: null as any,
|
|
155
|
+
queries: {},
|
|
156
|
+
mutations: {},
|
|
157
|
+
subscriptions: {},
|
|
158
|
+
errorFormatter: defaultFormatter,
|
|
159
|
+
transformer: defaultTransformer,
|
|
160
|
+
};
|
|
161
|
+
// type EmptyRouter = typeof emptyRouter;
|
|
162
|
+
|
|
163
|
+
const PROCEDURE_DEFINITION_MAP: Record<
|
|
164
|
+
ProcedureType,
|
|
165
|
+
'queries' | 'mutations' | 'subscriptions'
|
|
166
|
+
> = {
|
|
167
|
+
query: 'queries',
|
|
168
|
+
mutation: 'mutations',
|
|
169
|
+
subscription: 'subscriptions',
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
*
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
export function createRouterFactory<TSettings extends RootConfig>(
|
|
177
|
+
defaults?: RouterDefaultOptions<TSettings['ctx']>,
|
|
178
|
+
) {
|
|
179
|
+
return function createRouterInner<
|
|
180
|
+
TProcedures extends RouterBuildOptions<TSettings['ctx']>,
|
|
181
|
+
>(
|
|
182
|
+
procedures: ValidateShape<
|
|
183
|
+
TProcedures,
|
|
184
|
+
RouterBuildOptions<TSettings['ctx']>
|
|
185
|
+
>,
|
|
186
|
+
): Router<{
|
|
187
|
+
_ctx: TSettings['ctx'];
|
|
188
|
+
_errorShape: TSettings['errorShape'];
|
|
189
|
+
_meta: TSettings['meta'];
|
|
190
|
+
queries: EnsureRecord<TProcedures['queries']>;
|
|
191
|
+
mutations: EnsureRecord<TProcedures['mutations']>;
|
|
192
|
+
subscriptions: EnsureRecord<TProcedures['subscriptions']>;
|
|
193
|
+
errorFormatter: ErrorFormatter<TSettings['ctx'], TSettings['errorShape']>;
|
|
194
|
+
transformer: TSettings['transformer'];
|
|
195
|
+
}> {
|
|
196
|
+
const result = mergeWithoutOverrides<
|
|
197
|
+
RouterDefaultOptions<TSettings['ctx']> &
|
|
198
|
+
RouterBuildOptions<TSettings['ctx']>
|
|
199
|
+
>(
|
|
200
|
+
{
|
|
201
|
+
transformer: defaults?.transformer ?? defaultTransformer,
|
|
202
|
+
errorFormatter: defaults?.errorFormatter ?? defaultFormatter,
|
|
203
|
+
},
|
|
204
|
+
{
|
|
205
|
+
queries: omitPrototype(procedures.queries),
|
|
206
|
+
mutations: omitPrototype(procedures.mutations),
|
|
207
|
+
subscriptions: omitPrototype(procedures.subscriptions),
|
|
208
|
+
},
|
|
209
|
+
);
|
|
210
|
+
|
|
211
|
+
const _def: AnyRouterParams<TSettings['ctx']> = {
|
|
212
|
+
...emptyRouter,
|
|
213
|
+
...result,
|
|
214
|
+
};
|
|
215
|
+
const def = {
|
|
216
|
+
_def,
|
|
217
|
+
..._def,
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
function callProcedure(opts: InternalProcedureCallOptions) {
|
|
221
|
+
const { type, path } = opts;
|
|
222
|
+
const defTarget = PROCEDURE_DEFINITION_MAP[type];
|
|
223
|
+
const defs = def[defTarget];
|
|
224
|
+
|
|
225
|
+
if (!(path in defs)) {
|
|
226
|
+
throw new TRPCError({
|
|
227
|
+
code: 'NOT_FOUND',
|
|
228
|
+
message: `No "${type}"-procedure on path "${path}"`,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
const procedure = defs[path] as InternalProcedure;
|
|
232
|
+
return procedure(opts);
|
|
233
|
+
}
|
|
234
|
+
const router: AnyRouter = {
|
|
235
|
+
...def,
|
|
236
|
+
createCaller(ctx) {
|
|
237
|
+
return {
|
|
238
|
+
query: (path, ...args) =>
|
|
239
|
+
callProcedure({
|
|
240
|
+
path,
|
|
241
|
+
rawInput: args[0],
|
|
242
|
+
ctx,
|
|
243
|
+
type: 'query',
|
|
244
|
+
}) as any,
|
|
245
|
+
mutation: (path, ...args) =>
|
|
246
|
+
callProcedure({
|
|
247
|
+
path,
|
|
248
|
+
rawInput: args[0],
|
|
249
|
+
ctx,
|
|
250
|
+
type: 'mutation',
|
|
251
|
+
}) as any,
|
|
252
|
+
subscription: (path, ...args) =>
|
|
253
|
+
callProcedure({
|
|
254
|
+
path,
|
|
255
|
+
rawInput: args[0],
|
|
256
|
+
ctx,
|
|
257
|
+
type: 'subscription',
|
|
258
|
+
}) as any,
|
|
259
|
+
};
|
|
260
|
+
},
|
|
261
|
+
getErrorShape(opts) {
|
|
262
|
+
const { path, error } = opts;
|
|
263
|
+
const { code } = opts.error;
|
|
264
|
+
const shape: DefaultErrorShape = {
|
|
265
|
+
message: error.message,
|
|
266
|
+
code: TRPC_ERROR_CODES_BY_KEY[code],
|
|
267
|
+
data: {
|
|
268
|
+
code,
|
|
269
|
+
httpStatus: getHTTPStatusCodeFromError(error),
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
if (
|
|
273
|
+
process.env.NODE_ENV !== 'production' &&
|
|
274
|
+
typeof opts.error.stack === 'string'
|
|
275
|
+
) {
|
|
276
|
+
shape.data.stack = opts.error.stack;
|
|
277
|
+
}
|
|
278
|
+
if (typeof path === 'string') {
|
|
279
|
+
shape.data.path = path;
|
|
280
|
+
}
|
|
281
|
+
return this._def.errorFormatter({ ...opts, shape });
|
|
282
|
+
},
|
|
283
|
+
};
|
|
284
|
+
return router as any;
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
type combineProcedureRecords<
|
|
289
|
+
A extends Partial<AnyRouter>,
|
|
290
|
+
B extends Partial<AnyRouter>,
|
|
291
|
+
> = {
|
|
292
|
+
queries: A['queries'] & B['queries'];
|
|
293
|
+
mutations: A['mutations'] & B['mutations'];
|
|
294
|
+
subscriptions: A['subscriptions'] & B['subscriptions'];
|
|
295
|
+
};
|
|
296
|
+
/**
|
|
297
|
+
* @internal
|
|
298
|
+
*/
|
|
299
|
+
export type mergeProcedureRecordsVariadic<
|
|
300
|
+
Routers extends Partial<AnyRouter>[],
|
|
301
|
+
> = Routers extends []
|
|
302
|
+
? {
|
|
303
|
+
queries: {};
|
|
304
|
+
mutations: {};
|
|
305
|
+
subscriptions: {};
|
|
306
|
+
}
|
|
307
|
+
: Routers extends [infer First, ...infer Rest]
|
|
308
|
+
? First extends Partial<AnyRouter>
|
|
309
|
+
? Rest extends Partial<AnyRouter>[]
|
|
310
|
+
? combineProcedureRecords<First, mergeProcedureRecordsVariadic<Rest>>
|
|
311
|
+
: never
|
|
312
|
+
: never
|
|
313
|
+
: never;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { inferObservableValue } from '../observable';
|
|
2
|
+
import { inferAsyncReturnType } from '../types';
|
|
3
|
+
import { Procedure, ProcedureArgs } from './procedure';
|
|
4
|
+
import { AnyRouter, AnyRouterParams, Router } from './router';
|
|
5
|
+
|
|
6
|
+
export type inferRouterParams<TRouter extends AnyRouter> =
|
|
7
|
+
TRouter extends Router<infer TParams>
|
|
8
|
+
? TParams extends AnyRouterParams<any>
|
|
9
|
+
? TParams
|
|
10
|
+
: never
|
|
11
|
+
: never;
|
|
12
|
+
|
|
13
|
+
export type inferRouterContext<TRouter extends AnyRouter> =
|
|
14
|
+
inferRouterParams<TRouter>['_ctx'];
|
|
15
|
+
export type inferRouterError<TRouter extends AnyRouter> =
|
|
16
|
+
inferRouterParams<TRouter>['_errorShape'];
|
|
17
|
+
export type inferRouterMeta<TRouter extends AnyRouter> =
|
|
18
|
+
inferRouterParams<TRouter>['_meta'];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
export type ProcedureType = 'query' | 'mutation' | 'subscription';
|
|
24
|
+
|
|
25
|
+
export type inferHandlerInput<TProcedure extends Procedure<any>> =
|
|
26
|
+
TProcedure extends Procedure<infer TParams> ? ProcedureArgs<TParams> : never;
|
|
27
|
+
|
|
28
|
+
export type inferProcedureInput<TProcedure extends Procedure<any>> =
|
|
29
|
+
inferHandlerInput<TProcedure>[0];
|
|
30
|
+
|
|
31
|
+
export type inferProcedureOutput<TProcedure extends Procedure<any>> =
|
|
32
|
+
inferAsyncReturnType<TProcedure>;
|
|
33
|
+
|
|
34
|
+
export type inferSubscriptionOutput<
|
|
35
|
+
TRouter extends AnyRouter,
|
|
36
|
+
TPath extends keyof TRouter['subscriptions'] & string,
|
|
37
|
+
> = inferObservableValue<inferProcedureOutput<TRouter['subscriptions'][TPath]>>;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { Observable } from '
|
|
1
|
+
import { Observable } from '../../observable';
|
|
2
2
|
import { AnyRouter, ProcedureType } from '../router';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated
|
|
6
|
+
*/
|
|
4
7
|
export async function callProcedure<
|
|
5
8
|
TRouter extends AnyRouter<TContext>,
|
|
6
9
|
TContext,
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
import { TRPCError } from '
|
|
1
|
+
import { TRPCError } from '../../TRPCError';
|
|
2
2
|
import { ProcedureType } from '../router';
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated
|
|
6
|
+
*/
|
|
4
7
|
export const middlewareMarker = 'middlewareMarker' as 'middlewareMarker' & {
|
|
5
8
|
__brand: 'middlewareMarker';
|
|
6
9
|
};
|
|
@@ -14,11 +17,18 @@ interface MiddlewareResultBase<TContext> {
|
|
|
14
17
|
ctx: TContext;
|
|
15
18
|
}
|
|
16
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated
|
|
22
|
+
*/
|
|
17
23
|
interface MiddlewareOKResult<TContext> extends MiddlewareResultBase<TContext> {
|
|
18
24
|
ok: true;
|
|
19
25
|
data: unknown;
|
|
20
26
|
// this could be extended with `input`/`rawInput` later
|
|
21
27
|
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @deprecated
|
|
31
|
+
*/
|
|
22
32
|
interface MiddlewareErrorResult<TContext>
|
|
23
33
|
extends MiddlewareResultBase<TContext> {
|
|
24
34
|
ok: false;
|
|
@@ -26,10 +36,16 @@ interface MiddlewareErrorResult<TContext>
|
|
|
26
36
|
// we could guarantee it's always of this type
|
|
27
37
|
}
|
|
28
38
|
|
|
39
|
+
/**
|
|
40
|
+
* @deprecated
|
|
41
|
+
*/
|
|
29
42
|
export type MiddlewareResult<TContext> =
|
|
30
43
|
| MiddlewareOKResult<TContext>
|
|
31
44
|
| MiddlewareErrorResult<TContext>;
|
|
32
45
|
|
|
46
|
+
/**
|
|
47
|
+
* @deprecated
|
|
48
|
+
*/
|
|
33
49
|
export type MiddlewareFunction<TInputContext, TContext, TMeta> = (opts: {
|
|
34
50
|
ctx: TInputContext;
|
|
35
51
|
type: ProcedureType;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { TRPCError } from '
|
|
3
|
-
import { assertNotBrowser } from '
|
|
2
|
+
import { TRPCError } from '../../TRPCError';
|
|
3
|
+
import { assertNotBrowser } from '../../assertNotBrowser';
|
|
4
|
+
import {
|
|
5
|
+
getCauseFromUnknown,
|
|
6
|
+
getErrorFromUnknown,
|
|
7
|
+
} from '../../internals/errors';
|
|
8
|
+
import { InferLast } from '../../types';
|
|
4
9
|
import { ProcedureType } from '../router';
|
|
5
|
-
import { InferLast } from '../types';
|
|
6
|
-
import { getCauseFromUnknown, getErrorFromUnknown } from './errors';
|
|
7
10
|
import {
|
|
8
11
|
MiddlewareFunction,
|
|
9
12
|
MiddlewareResult,
|
|
@@ -58,6 +61,7 @@ interface ProcedureOptions<TContext, TMeta, TInput, TOutput, TParsedOutput> {
|
|
|
58
61
|
|
|
59
62
|
/**
|
|
60
63
|
* @internal
|
|
64
|
+
* @deprecated
|
|
61
65
|
*/
|
|
62
66
|
export interface ProcedureCallOptions<TContext> {
|
|
63
67
|
ctx: TContext;
|
|
@@ -101,6 +105,7 @@ function getParseFn<T>(procedureParser: ProcedureParser<T>): ParseFn<T> {
|
|
|
101
105
|
|
|
102
106
|
/**
|
|
103
107
|
* @internal
|
|
108
|
+
* @deprecated
|
|
104
109
|
*/
|
|
105
110
|
export class Procedure<
|
|
106
111
|
TInputContext,
|
|
@@ -138,6 +143,16 @@ export class Procedure<
|
|
|
138
143
|
this.meta = opts.meta;
|
|
139
144
|
}
|
|
140
145
|
|
|
146
|
+
public _def() {
|
|
147
|
+
return {
|
|
148
|
+
middlewares: this.middlewares,
|
|
149
|
+
resolver: this.resolver,
|
|
150
|
+
inputParser: this.inputParser,
|
|
151
|
+
outputParser: this.outputParser,
|
|
152
|
+
meta: this.meta,
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
|
|
141
156
|
private async parseInput(rawInput: unknown): Promise<TParsedInput> {
|
|
142
157
|
try {
|
|
143
158
|
return await this.parseInputFn(rawInput);
|