@trpc/server 10.0.0-alpha.25 → 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.
Files changed (196) hide show
  1. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +1 -1
  2. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +100 -20
  3. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +100 -20
  4. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +99 -19
  5. package/adapters/aws-lambda/index.d.ts +1 -0
  6. package/adapters/aws-lambda/index.js +1 -0
  7. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.d.ts +1 -0
  8. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.dev.js +25 -0
  9. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.js +7 -0
  10. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.prod.js +25 -0
  11. package/adapters/lambda/dist/trpc-server-adapters-lambda.esm.js +21 -0
  12. package/dist/TRPCError.d.ts.map +1 -1
  13. package/dist/adapters/aws-lambda/index.d.ts +14 -0
  14. package/dist/adapters/aws-lambda/index.d.ts.map +1 -0
  15. package/dist/adapters/aws-lambda/utils.d.ts +34 -0
  16. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -0
  17. package/dist/adapters/express.d.ts +1 -1
  18. package/dist/adapters/express.d.ts.map +1 -1
  19. package/dist/adapters/express.js +4 -4
  20. package/dist/adapters/express.mjs +4 -4
  21. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +1 -1
  22. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  23. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  24. package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  25. package/dist/adapters/fastify/index.js +3 -3
  26. package/dist/adapters/fastify/index.mjs +3 -3
  27. package/dist/adapters/fetch/fetchRequestHandler.d.ts +1 -1
  28. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  29. package/dist/adapters/fetch/index.js +3 -3
  30. package/dist/adapters/fetch/index.mjs +3 -3
  31. package/dist/adapters/fetch/types.d.ts +1 -1
  32. package/dist/adapters/fetch/types.d.ts.map +1 -1
  33. package/dist/adapters/lambda/index.d.ts +12 -0
  34. package/dist/adapters/lambda/index.d.ts.map +1 -0
  35. package/dist/adapters/next.d.ts +1 -1
  36. package/dist/adapters/next.d.ts.map +1 -1
  37. package/dist/adapters/next.js +4 -4
  38. package/dist/adapters/next.mjs +4 -4
  39. package/dist/adapters/node-http/index.js +4 -4
  40. package/dist/adapters/node-http/index.mjs +4 -4
  41. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
  42. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  43. package/dist/adapters/node-http/types.d.ts +3 -2
  44. package/dist/adapters/node-http/types.d.ts.map +1 -1
  45. package/dist/adapters/standalone.d.ts +2 -2
  46. package/dist/adapters/standalone.d.ts.map +1 -1
  47. package/dist/adapters/standalone.js +4 -4
  48. package/dist/adapters/standalone.mjs +4 -4
  49. package/dist/adapters/ws.d.ts +3 -2
  50. package/dist/adapters/ws.d.ts.map +1 -1
  51. package/dist/adapters/ws.js +3 -8
  52. package/dist/adapters/ws.mjs +3 -8
  53. package/dist/core/index.d.ts +5 -0
  54. package/dist/core/index.d.ts.map +1 -0
  55. package/dist/core/initTRPC.d.ts +82 -0
  56. package/dist/core/initTRPC.d.ts.map +1 -0
  57. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +202 -0
  58. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -0
  59. package/dist/core/internals/config.d.ts +38 -0
  60. package/dist/core/internals/config.d.ts.map +1 -0
  61. package/dist/core/internals/getParseFn.d.ts +9 -0
  62. package/dist/core/internals/getParseFn.d.ts.map +1 -0
  63. package/dist/core/internals/internalProcedure.d.ts +61 -0
  64. package/dist/core/internals/internalProcedure.d.ts.map +1 -0
  65. package/dist/core/internals/mergeRouters.d.ts +3 -0
  66. package/dist/core/internals/mergeRouters.d.ts.map +1 -0
  67. package/dist/core/internals/mergeWithoutOverrides.d.ts +5 -0
  68. package/dist/core/internals/mergeWithoutOverrides.d.ts.map +1 -0
  69. package/dist/core/internals/omitPrototype.d.ts +6 -0
  70. package/dist/core/internals/omitPrototype.d.ts.map +1 -0
  71. package/dist/core/internals/procedureBuilder.d.ts +71 -0
  72. package/dist/core/internals/procedureBuilder.d.ts.map +1 -0
  73. package/dist/core/internals/utils.d.ts +44 -0
  74. package/dist/core/internals/utils.d.ts.map +1 -0
  75. package/dist/core/middleware.d.ts +78 -0
  76. package/dist/core/middleware.d.ts.map +1 -0
  77. package/dist/core/parser.d.ts +25 -0
  78. package/dist/core/parser.d.ts.map +1 -0
  79. package/dist/core/procedure.d.ts +57 -0
  80. package/dist/core/procedure.d.ts.map +1 -0
  81. package/dist/core/router.d.ts +110 -0
  82. package/dist/core/router.d.ts.map +1 -0
  83. package/dist/core/types.d.ts +17 -0
  84. package/dist/core/types.d.ts.map +1 -0
  85. package/dist/{internals → deprecated/internals}/callProcedure.d.ts +4 -1
  86. package/dist/deprecated/internals/callProcedure.d.ts.map +1 -0
  87. package/dist/{internals → deprecated/internals}/middlewares.d.ts +16 -1
  88. package/dist/deprecated/internals/middlewares.d.ts.map +1 -0
  89. package/dist/{internals → deprecated/internals}/procedure.d.ts +10 -1
  90. package/dist/deprecated/internals/procedure.d.ts.map +1 -0
  91. package/dist/deprecated/interop.d.ts +34 -0
  92. package/dist/deprecated/interop.d.ts.map +1 -0
  93. package/dist/{router.d.ts → deprecated/router.d.ts} +26 -5
  94. package/dist/deprecated/router.d.ts.map +1 -0
  95. package/dist/{envelopes-1deb2fa7.mjs → envelopes-af8c2959.mjs} +12 -1
  96. package/dist/{envelopes-a953b930.js → envelopes-b6dd0367.js} +11 -1
  97. package/dist/error/TRPCError.d.ts +11 -0
  98. package/dist/error/TRPCError.d.ts.map +1 -0
  99. package/dist/error/formatter.d.ts +33 -0
  100. package/dist/error/formatter.d.ts.map +1 -0
  101. package/dist/error/utils.d.ts +5 -0
  102. package/dist/error/utils.d.ts.map +1 -0
  103. package/dist/http/index.d.ts +1 -1
  104. package/dist/http/index.d.ts.map +1 -1
  105. package/dist/http/internals/types.d.ts +9 -7
  106. package/dist/http/internals/types.d.ts.map +1 -1
  107. package/dist/http/resolveHTTPResponse.d.ts +1 -1
  108. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  109. package/dist/http/{responseMeta.d.ts → types.d.ts} +1 -1
  110. package/dist/http/types.d.ts.map +1 -0
  111. package/dist/index.d.ts +2 -1
  112. package/dist/index.d.ts.map +1 -1
  113. package/dist/index.js +687 -10
  114. package/dist/index.mjs +689 -14
  115. package/dist/internals/transformTRPCResponse.d.ts +1 -1
  116. package/dist/internals/transformTRPCResponse.d.ts.map +1 -1
  117. package/dist/internals/types.d.ts +23 -0
  118. package/dist/internals/types.d.ts.map +1 -0
  119. package/dist/{nodeHTTPRequestHandler-1cdb6383.mjs → nodeHTTPRequestHandler-5ad62229.mjs} +1 -1
  120. package/dist/{nodeHTTPRequestHandler-848f07b5.js → nodeHTTPRequestHandler-9ddd18f2.js} +1 -1
  121. package/dist/{resolveHTTPResponse-ec3dccaa.js → resolveHTTPResponse-5932d785.js} +16 -10
  122. package/dist/{resolveHTTPResponse-54a79e6c.mjs → resolveHTTPResponse-ccb9cfa1.mjs} +15 -9
  123. package/dist/rpc/envelopes.d.ts +1 -1
  124. package/dist/rpc/envelopes.d.ts.map +1 -1
  125. package/dist/rpc/index.js +1 -1
  126. package/dist/rpc/index.mjs +1 -1
  127. package/dist/rpc/internals/invert.d.ts +18 -0
  128. package/dist/rpc/internals/invert.d.ts.map +1 -0
  129. package/dist/{transformTRPCResponse-333bed6c.js → transformTRPCResponse-0c261655.js} +0 -27
  130. package/dist/{transformTRPCResponse-e0cba527.mjs → transformTRPCResponse-747b9a68.mjs} +1 -27
  131. package/dist/transformer.d.ts +14 -0
  132. package/dist/transformer.d.ts.map +1 -1
  133. package/dist/types.d.ts +9 -0
  134. package/dist/types.d.ts.map +1 -1
  135. package/package.json +11 -4
  136. package/src/TRPCError.ts +1 -1
  137. package/src/adapters/aws-lambda/index.ts +156 -0
  138. package/src/adapters/aws-lambda/utils.ts +91 -0
  139. package/src/adapters/express.ts +1 -1
  140. package/src/adapters/fastify/fastifyRequestHandler.ts +1 -1
  141. package/src/adapters/fastify/fastifyTRPCPlugin.ts +2 -2
  142. package/src/adapters/fetch/fetchRequestHandler.ts +1 -1
  143. package/src/adapters/fetch/types.ts +1 -1
  144. package/src/adapters/lambda/index.ts +18 -0
  145. package/src/adapters/next.ts +1 -1
  146. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +3 -2
  147. package/src/adapters/node-http/types.ts +5 -2
  148. package/src/adapters/standalone.ts +1 -1
  149. package/src/adapters/ws.ts +5 -10
  150. package/src/core/index.ts +5 -0
  151. package/src/core/initTRPC.ts +90 -0
  152. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +306 -0
  153. package/src/core/internals/config.ts +44 -0
  154. package/src/core/internals/getParseFn.ts +47 -0
  155. package/src/core/internals/internalProcedure.ts +265 -0
  156. package/src/core/internals/mergeRouters.ts +57 -0
  157. package/src/core/internals/mergeWithoutOverrides.ts +19 -0
  158. package/src/core/internals/omitPrototype.ts +9 -0
  159. package/src/core/internals/procedureBuilder.ts +100 -0
  160. package/src/core/internals/utils.ts +64 -0
  161. package/src/core/middleware.ts +93 -0
  162. package/src/core/parser.ts +45 -0
  163. package/src/core/procedure.ts +72 -0
  164. package/src/core/router.ts +313 -0
  165. package/src/core/types.ts +37 -0
  166. package/src/{internals → deprecated/internals}/callProcedure.ts +4 -1
  167. package/src/{internals → deprecated/internals}/middlewares.ts +17 -1
  168. package/src/{internals → deprecated/internals}/procedure.ts +19 -4
  169. package/src/deprecated/interop.ts +169 -0
  170. package/src/{router.ts → deprecated/router.ts} +47 -12
  171. package/src/error/TRPCError.ts +27 -0
  172. package/src/error/formatter.ts +51 -0
  173. package/src/error/utils.ts +49 -0
  174. package/src/http/index.ts +1 -1
  175. package/src/http/internals/types.ts +9 -11
  176. package/src/http/resolveHTTPResponse.ts +6 -11
  177. package/src/http/{responseMeta.ts → types.ts} +0 -0
  178. package/src/index.ts +2 -1
  179. package/src/internals/transformTRPCResponse.ts +1 -1
  180. package/src/internals/types.ts +24 -0
  181. package/src/rpc/codes.ts +1 -1
  182. package/src/rpc/envelopes.ts +1 -1
  183. package/src/rpc/internals/invert.ts +27 -0
  184. package/src/transformer.ts +28 -0
  185. package/src/types.ts +12 -0
  186. package/dist/http/responseMeta.d.ts.map +0 -1
  187. package/dist/internals/baseHandlerOptions.d.ts +0 -13
  188. package/dist/internals/baseHandlerOptions.d.ts.map +0 -1
  189. package/dist/internals/callProcedure.d.ts.map +0 -1
  190. package/dist/internals/middlewares.d.ts.map +0 -1
  191. package/dist/internals/onErrorFunction.d.ts +0 -11
  192. package/dist/internals/onErrorFunction.d.ts.map +0 -1
  193. package/dist/internals/procedure.d.ts.map +0 -1
  194. package/dist/router.d.ts.map +0 -1
  195. package/src/internals/baseHandlerOptions.ts +0 -13
  196. package/src/internals/onErrorFunction.ts +0 -11
@@ -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 '../observable';
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 '../TRPCError';
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 '../TRPCError';
3
- import { assertNotBrowser } from '../assertNotBrowser';
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);
@@ -0,0 +1,169 @@
1
+ import { getParseFnOrPassThrough } from '../core/internals/getParseFn';
2
+ import {
3
+ createInputMiddleware,
4
+ createInternalBuilder,
5
+ createOutputMiddleware,
6
+ } from '../core/internals/internalProcedure';
7
+ import { Procedure as NewProcedure } from '../core/procedure';
8
+ import { Router as NewRouter, createRouterFactory } from '../core/router';
9
+ import {
10
+ AnyRouter as AnyOldRouter,
11
+ Router as OldRouter,
12
+ } from '../deprecated/router';
13
+ import { TRPCErrorShape } from '../rpc';
14
+ import { CombinedDataTransformer } from '../transformer';
15
+ import { Procedure as OldProcedure } from './internals/procedure';
16
+ import { ProcedureRecord } from './router';
17
+
18
+ type AnyOldProcedure = OldProcedure<any, any, any, any, any, any, any, any>;
19
+ type MigrateProcedure<TProcedure extends AnyOldProcedure> =
20
+ TProcedure extends OldProcedure<
21
+ infer TInputContext,
22
+ infer TContext,
23
+ infer TMeta,
24
+ infer TInput,
25
+ infer TParsedInput,
26
+ infer TOutput,
27
+ infer _TParsedOutput,
28
+ infer TFinalInput
29
+ >
30
+ ? NewProcedure<{
31
+ _ctx_in: TInputContext;
32
+ _ctx_out: TContext;
33
+ _meta: TMeta;
34
+ _input_in: TInput;
35
+ _input_out: TParsedInput;
36
+ _output_in: TOutput;
37
+ _output_out: TFinalInput;
38
+ }>
39
+ : never;
40
+
41
+ export type MigrateProcedureRecord<T extends ProcedureRecord<any>> = {
42
+ [K in keyof T]: MigrateProcedure<T[K]>;
43
+ };
44
+
45
+ export type MigrateRouter<
46
+ TInputContext,
47
+ TContext,
48
+ TMeta extends Record<string, any>,
49
+ TQueries extends ProcedureRecord<
50
+ TInputContext,
51
+ TContext,
52
+ any,
53
+ any,
54
+ any,
55
+ any,
56
+ any
57
+ >,
58
+ TMutations extends ProcedureRecord<
59
+ TInputContext,
60
+ TContext,
61
+ any,
62
+ any,
63
+ any,
64
+ any,
65
+ any
66
+ >,
67
+ TSubscriptions extends ProcedureRecord<
68
+ TInputContext,
69
+ TContext,
70
+ unknown,
71
+ unknown,
72
+ any,
73
+ unknown,
74
+ unknown
75
+ >,
76
+ TErrorShape extends TRPCErrorShape<number>,
77
+ > = NewRouter<{
78
+ _ctx: TInputContext;
79
+ _errorShape: TErrorShape;
80
+ _meta: TMeta;
81
+ errorFormatter: never;
82
+ mutations: MigrateProcedureRecord<TMutations>;
83
+ queries: MigrateProcedureRecord<TQueries>;
84
+ subscriptions: MigrateProcedureRecord<TSubscriptions>;
85
+ transformer: CombinedDataTransformer;
86
+ }>;
87
+
88
+ export type MigrateOldRouter<TRouter extends AnyOldRouter> =
89
+ TRouter extends OldRouter<
90
+ infer TInputContext,
91
+ infer TContext,
92
+ infer TMeta,
93
+ infer TQueries,
94
+ infer TMutations,
95
+ infer TSubscriptions,
96
+ infer TErrorShape
97
+ >
98
+ ? MigrateRouter<
99
+ TInputContext,
100
+ TContext,
101
+ TMeta,
102
+ TQueries,
103
+ TMutations,
104
+ TSubscriptions,
105
+ TErrorShape
106
+ >
107
+ : never;
108
+
109
+ function migrateProcedure<TProcedure extends AnyOldProcedure>(
110
+ oldProc: TProcedure,
111
+ ): MigrateProcedure<TProcedure> {
112
+ const def = oldProc._def();
113
+
114
+ const inputParser = getParseFnOrPassThrough(def.inputParser);
115
+ const outputParser = getParseFnOrPassThrough(def.outputParser);
116
+
117
+ const inputMiddleware = createInputMiddleware(inputParser);
118
+
119
+ const builder = createInternalBuilder({
120
+ input: def.inputParser,
121
+ middlewares: [
122
+ ...(def.middlewares as any),
123
+ inputMiddleware,
124
+ createOutputMiddleware(outputParser),
125
+ ],
126
+ meta: def.meta,
127
+ output: def.outputParser,
128
+ });
129
+
130
+ const proc = builder.resolve((opts) => def.resolver(opts as any));
131
+
132
+ return proc as any;
133
+ }
134
+ export function migrateRouter<TOldRouter extends AnyOldRouter>(
135
+ oldRouter: TOldRouter,
136
+ ): MigrateOldRouter<TOldRouter> {
137
+ const errorFormatter = oldRouter._def.errorFormatter;
138
+ const transformer = oldRouter._def.transformer;
139
+
140
+ type ProcRecord = Record<string, NewProcedure<any>>;
141
+
142
+ const queries: ProcRecord = {};
143
+ const mutations: ProcRecord = {};
144
+ const subscriptions: ProcRecord = {};
145
+ for (const [name, procedure] of Object.entries(oldRouter._def.queries)) {
146
+ queries[name] = migrateProcedure(procedure as any);
147
+ }
148
+
149
+ for (const [name, procedure] of Object.entries(oldRouter._def.mutations)) {
150
+ mutations[name] = migrateProcedure(procedure as any);
151
+ }
152
+
153
+ for (const [name, procedure] of Object.entries(
154
+ oldRouter._def.subscriptions,
155
+ )) {
156
+ subscriptions[name] = migrateProcedure(procedure as any);
157
+ }
158
+
159
+ const newRouter = createRouterFactory<any>({
160
+ transformer,
161
+ errorFormatter,
162
+ })({
163
+ mutations,
164
+ queries,
165
+ subscriptions,
166
+ });
167
+
168
+ return newRouter as any;
169
+ }