@trpc/server 10.43.0 → 11.0.0-next.91

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 (164) hide show
  1. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  2. package/dist/adapters/aws-lambda/index.js +6 -4
  3. package/dist/adapters/aws-lambda/index.mjs +6 -4
  4. package/dist/adapters/aws-lambda/utils.d.ts +3 -1
  5. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  6. package/dist/adapters/express.js +4 -3
  7. package/dist/adapters/express.mjs +4 -3
  8. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  9. package/dist/adapters/fastify/index.js +8 -5
  10. package/dist/adapters/fastify/index.mjs +8 -5
  11. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  12. package/dist/adapters/fetch/index.js +6 -4
  13. package/dist/adapters/fetch/index.mjs +6 -4
  14. package/dist/adapters/fetch/types.d.ts +2 -1
  15. package/dist/adapters/fetch/types.d.ts.map +1 -1
  16. package/dist/adapters/next.js +4 -3
  17. package/dist/adapters/next.mjs +4 -3
  18. package/dist/adapters/node-http/index.js +4 -3
  19. package/dist/adapters/node-http/index.mjs +4 -3
  20. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  21. package/dist/adapters/node-http/types.d.ts +2 -1
  22. package/dist/adapters/node-http/types.d.ts.map +1 -1
  23. package/dist/adapters/standalone.d.ts +1 -6
  24. package/dist/adapters/standalone.d.ts.map +1 -1
  25. package/dist/adapters/standalone.js +8 -19
  26. package/dist/adapters/standalone.mjs +8 -19
  27. package/dist/adapters/ws.d.ts +25 -4
  28. package/dist/adapters/ws.d.ts.map +1 -1
  29. package/dist/adapters/ws.js +67 -14
  30. package/dist/adapters/ws.mjs +58 -6
  31. package/dist/bundle-analysis.json +910 -0
  32. package/dist/config-5de7eae5.js +10 -0
  33. package/dist/config-60fc2667.js +7 -0
  34. package/dist/config-ab4c1c6b.mjs +5 -0
  35. package/dist/core/index.d.ts +1 -1
  36. package/dist/core/index.d.ts.map +1 -1
  37. package/dist/core/initTRPC.d.ts +1 -1
  38. package/dist/core/internals/builderTypes.d.ts +11 -0
  39. package/dist/core/internals/builderTypes.d.ts.map +1 -0
  40. package/dist/core/internals/getParseFn.d.ts +0 -5
  41. package/dist/core/internals/getParseFn.d.ts.map +1 -1
  42. package/dist/core/internals/mergeRouters.d.ts +0 -3
  43. package/dist/core/internals/mergeRouters.d.ts.map +1 -1
  44. package/dist/core/internals/procedureBuilder.d.ts +24 -27
  45. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  46. package/dist/core/internals/utils.d.ts +7 -8
  47. package/dist/core/internals/utils.d.ts.map +1 -1
  48. package/dist/core/middleware.d.ts +16 -15
  49. package/dist/core/middleware.d.ts.map +1 -1
  50. package/dist/core/procedure.d.ts +41 -42
  51. package/dist/core/procedure.d.ts.map +1 -1
  52. package/dist/core/router.d.ts +9 -56
  53. package/dist/core/router.d.ts.map +1 -1
  54. package/dist/core/types.d.ts +7 -12
  55. package/dist/core/types.d.ts.map +1 -1
  56. package/dist/http/index.js +3 -2
  57. package/dist/http/index.mjs +3 -2
  58. package/dist/http/resolveHTTPResponse.d.ts +3 -3
  59. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  60. package/dist/http/types.d.ts +22 -1
  61. package/dist/http/types.d.ts.map +1 -1
  62. package/dist/index.d.ts +10 -2
  63. package/dist/index.d.ts.map +1 -1
  64. package/dist/index.js +24 -811
  65. package/dist/index.mjs +15 -802
  66. package/dist/{nodeHTTPRequestHandler-cf01c646.mjs → nodeHTTPRequestHandler-0986c5a1.mjs} +8 -3
  67. package/dist/{nodeHTTPRequestHandler-b7d358fe.js → nodeHTTPRequestHandler-18f3370e.js} +8 -3
  68. package/dist/{nodeHTTPRequestHandler-9781faee.js → nodeHTTPRequestHandler-388f5fce.js} +8 -3
  69. package/dist/{resolveHTTPResponse-68c8befb.mjs → resolveHTTPResponse-55c648bd.mjs} +14 -12
  70. package/dist/{resolveHTTPResponse-79011e44.js → resolveHTTPResponse-5def1b0c.js} +15 -13
  71. package/dist/{resolveHTTPResponse-051b3a40.js → resolveHTTPResponse-8d474952.js} +12 -12
  72. package/dist/{config-3ab6b85e.js → router-3c22fe56.js} +9 -46
  73. package/dist/{config-4c0f8e88.mjs → router-5274e533.mjs} +10 -45
  74. package/dist/{config-f356f2fd.js → router-a1635eab.js} +9 -44
  75. package/dist/rpc/envelopes.d.ts +1 -5
  76. package/dist/rpc/envelopes.d.ts.map +1 -1
  77. package/dist/rpc/index.d.ts +0 -1
  78. package/dist/rpc/index.d.ts.map +1 -1
  79. package/dist/rpc/index.js +0 -2
  80. package/dist/rpc/index.mjs +0 -1
  81. package/dist/shared/getErrorShape.d.ts +1 -1
  82. package/dist/shared/getErrorShape.d.ts.map +1 -1
  83. package/dist/shared/index.d.ts +1 -0
  84. package/dist/shared/index.d.ts.map +1 -1
  85. package/dist/shared/jsonify.d.ts +3 -2
  86. package/dist/shared/jsonify.d.ts.map +1 -1
  87. package/dist/shared/transformTRPCResponse.d.ts +1 -1
  88. package/dist/shared/transformTRPCResponse.d.ts.map +1 -1
  89. package/dist/shared/types.d.ts +6 -0
  90. package/dist/shared/types.d.ts.map +1 -0
  91. package/dist/types.d.ts +5 -0
  92. package/dist/types.d.ts.map +1 -1
  93. package/dist/unstableInternalsExport-10cc2bae.mjs +353 -0
  94. package/dist/unstableInternalsExport-a064a7dd.js +349 -0
  95. package/dist/unstableInternalsExport-bf8496ea.js +363 -0
  96. package/dist/unstableInternalsExport.d.ts +11 -0
  97. package/dist/unstableInternalsExport.d.ts.map +1 -0
  98. package/dist/unstableInternalsExport.js +16 -0
  99. package/dist/unstableInternalsExport.mjs +5 -0
  100. package/package.json +9 -3
  101. package/src/adapters/aws-lambda/index.ts +11 -7
  102. package/src/adapters/aws-lambda/utils.ts +3 -0
  103. package/src/adapters/fastify/fastifyRequestHandler.ts +9 -5
  104. package/src/adapters/fetch/fetchRequestHandler.ts +9 -3
  105. package/src/adapters/fetch/types.ts +2 -1
  106. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +15 -4
  107. package/src/adapters/node-http/types.ts +2 -1
  108. package/src/adapters/standalone.ts +4 -22
  109. package/src/adapters/ws.ts +110 -14
  110. package/src/core/index.ts +1 -0
  111. package/src/core/internals/builderTypes.ts +11 -0
  112. package/src/core/internals/getParseFn.ts +0 -13
  113. package/src/core/internals/mergeRouters.ts +0 -4
  114. package/src/core/internals/procedureBuilder.ts +50 -70
  115. package/src/core/internals/utils.ts +11 -8
  116. package/src/core/middleware.ts +37 -31
  117. package/src/core/procedure.ts +46 -54
  118. package/src/core/router.ts +19 -117
  119. package/src/core/types.ts +11 -20
  120. package/src/http/resolveHTTPResponse.ts +17 -14
  121. package/src/http/types.ts +24 -1
  122. package/src/index.ts +16 -2
  123. package/src/rpc/envelopes.ts +1 -6
  124. package/src/rpc/index.ts +0 -1
  125. package/src/shared/getErrorShape.ts +2 -1
  126. package/src/shared/index.ts +2 -0
  127. package/src/shared/jsonify.ts +9 -5
  128. package/src/shared/transformTRPCResponse.ts +1 -1
  129. package/src/shared/types.ts +9 -0
  130. package/src/types.ts +8 -0
  131. package/src/unstableInternalsExport.ts +10 -0
  132. package/unstableInternalsExport/index.d.ts +1 -0
  133. package/unstableInternalsExport/index.js +1 -0
  134. package/adapters/zodFileSchema/index.d.ts +0 -1
  135. package/adapters/zodFileSchema/index.js +0 -1
  136. package/dist/adapters/lambda/index.d.ts +0 -12
  137. package/dist/adapters/lambda/index.d.ts.map +0 -1
  138. package/dist/deprecated/internals/middlewares.d.ts +0 -52
  139. package/dist/deprecated/internals/middlewares.d.ts.map +0 -1
  140. package/dist/deprecated/internals/procedure.d.ts +0 -96
  141. package/dist/deprecated/internals/procedure.d.ts.map +0 -1
  142. package/dist/deprecated/interop.d.ts +0 -44
  143. package/dist/deprecated/interop.d.ts.map +0 -1
  144. package/dist/deprecated/router.d.ts +0 -204
  145. package/dist/deprecated/router.d.ts.map +0 -1
  146. package/dist/internals.d.ts +0 -10
  147. package/dist/internals.d.ts.map +0 -1
  148. package/dist/parseTRPCMessage-1377f305.js +0 -56
  149. package/dist/parseTRPCMessage-95955211.js +0 -63
  150. package/dist/parseTRPCMessage-a0f17853.mjs +0 -54
  151. package/dist/rpc/parseTRPCMessage.d.ts +0 -5
  152. package/dist/rpc/parseTRPCMessage.d.ts.map +0 -1
  153. package/dist/subscription.d.ts +0 -13
  154. package/dist/subscription.d.ts.map +0 -1
  155. package/dist/subscription.js +0 -38
  156. package/dist/subscription.mjs +0 -34
  157. package/src/adapters/lambda/index.ts +0 -18
  158. package/src/deprecated/internals/middlewares.ts +0 -61
  159. package/src/deprecated/internals/procedure.ts +0 -411
  160. package/src/deprecated/interop.ts +0 -232
  161. package/src/deprecated/router.ts +0 -945
  162. package/src/internals.ts +0 -12
  163. package/src/rpc/parseTRPCMessage.ts +0 -84
  164. package/src/subscription.ts +0 -43
@@ -1,945 +0,0 @@
1
- /* eslint-disable @typescript-eslint/ban-types */
2
- import { defaultFormatter } from '../error/formatter';
3
- import { TRPCError } from '../error/TRPCError';
4
- import { getHTTPStatusCodeFromError } from '../http/getHTTPStatusCode';
5
- import { inferObservableValue, Observable } from '../observable';
6
- import {
7
- TRPC_ERROR_CODE_KEY,
8
- TRPC_ERROR_CODE_NUMBER,
9
- TRPC_ERROR_CODES_BY_KEY,
10
- TRPCErrorShape,
11
- } from '../rpc';
12
- import {
13
- CombinedDataTransformer,
14
- DataTransformerOptions,
15
- DefaultDataTransformer,
16
- defaultTransformer,
17
- } from '../transformer';
18
- import { FlatOverwrite, ThenArg } from '../types';
19
- import { MiddlewareFunction } from './internals/middlewares';
20
- import {
21
- createProcedure,
22
- CreateProcedureOptions,
23
- CreateProcedureWithInput,
24
- CreateProcedureWithInputOutputParser,
25
- CreateProcedureWithoutInput,
26
- inferProcedureFromOptions,
27
- Procedure,
28
- ProcedureCallOptions,
29
- } from './internals/procedure';
30
- import { MigrateRouter, migrateRouter } from './interop';
31
-
32
- export type { Procedure } from './internals/procedure';
33
-
34
- /**
35
- * @internal
36
- */
37
- type Prefix<
38
- TPrefix extends string,
39
- TSuffix extends string,
40
- > = `${TPrefix}${TSuffix}`;
41
-
42
- /**
43
- * @internal
44
- */
45
- type Prefixer<TObj extends Record<string, any>, TPrefix extends string> = {
46
- [P in keyof TObj as Prefix<TPrefix, P & string>]: TObj[P];
47
- };
48
-
49
- /**
50
- * @public
51
- * @deprecated
52
- */
53
- export type ProcedureType = 'mutation' | 'query' | 'subscription';
54
-
55
- /**
56
- * @internal
57
- * @deprecated
58
- */
59
- export type ProcedureRecord<
60
- TInputContext = any,
61
- TContext = any,
62
- TMeta = any,
63
- TInput = any,
64
- TParsedInput = any,
65
- TOutput = any,
66
- TParsedOutput = any,
67
- > = Record<
68
- string,
69
- Procedure<
70
- TInputContext,
71
- TContext,
72
- TMeta,
73
- TInput,
74
- TParsedInput,
75
- TOutput,
76
- TParsedOutput
77
- >
78
- >;
79
-
80
- /**
81
- * @public
82
- * @deprecated
83
- */
84
- // ts-prune-ignore-next
85
- export type inferProcedureInput<
86
- TProcedure extends Procedure<any, any, any, any, any, any, any>,
87
- > = TProcedure extends Procedure<any, any, any, infer Input, any, any, any>
88
- ? undefined extends Input
89
- ? Input | null | void // void is necessary to allow procedures with nullish input to be called without an input
90
- : Input
91
- : undefined;
92
-
93
- /**
94
- * @public
95
- * @deprecated
96
- */
97
- export type inferAsyncReturnType<TFunction extends (...args: any) => any> =
98
- ThenArg<ReturnType<TFunction>>;
99
-
100
- /**
101
- * @public
102
- * @deprecated
103
- */
104
- export type inferProcedureOutput<
105
- TProcedure extends Procedure<any, any, any, any, any, any, any>,
106
- > = inferAsyncReturnType<TProcedure['call']>;
107
-
108
- /**
109
- * @public
110
- * @beta
111
- * @deprecated
112
- */
113
- // ts-prune-ignore-next
114
- export type inferSubscriptionOutput<
115
- TRouter extends AnyRouter,
116
- TPath extends keyof TRouter['_def']['subscriptions'],
117
- > = inferObservableValue<
118
- inferAsyncReturnType<TRouter['_def']['subscriptions'][TPath]['call']>
119
- >;
120
-
121
- function getDataTransformer(
122
- transformer: DataTransformerOptions,
123
- ): CombinedDataTransformer {
124
- if ('input' in transformer) {
125
- return transformer;
126
- }
127
- return { input: transformer, output: transformer };
128
- }
129
- /**
130
- * @internal
131
- * @deprecated
132
- */
133
- export type inferHandlerInput<
134
- TProcedure extends Procedure<any, any, any, any, any, any, any>,
135
- > = TProcedure extends Procedure<any, any, any, infer TInput, any, any, any>
136
- ? undefined extends TInput // ? is input optional
137
- ? unknown extends TInput // ? is input unset
138
- ? [(null | undefined)?] // -> there is no input
139
- : [(TInput | null | undefined)?] // -> there is optional input
140
- : [TInput] // -> input is required
141
- : [(null | undefined)?]; // -> there is no input
142
-
143
- type inferHandlerFn<TProcedures extends ProcedureRecord> = <
144
- TProcedure extends TProcedures[TPath],
145
- TPath extends string & keyof TProcedures,
146
- >(
147
- path: TPath,
148
- ...args: inferHandlerInput<TProcedure>
149
- ) => Promise<inferProcedureOutput<TProcedures[TPath]>>;
150
-
151
- /**
152
- * @internal
153
- * @deprecated
154
- */
155
- export type inferRouterContext<TRouter extends AnyRouter> = Parameters<
156
- TRouter['createCaller']
157
- >[0];
158
-
159
- /**
160
- * @internal
161
- */
162
- // ts-prune-ignore-next
163
- export type inferRouterMeta<TRouter extends AnyRouter> = TRouter extends Router<
164
- any,
165
- any,
166
- infer TMeta,
167
- any,
168
- any,
169
- any,
170
- any,
171
- any
172
- >
173
- ? TMeta
174
- : {};
175
-
176
- /**
177
- * @public
178
- * @deprecated
179
- */
180
- export type AnyRouter<TContext extends Record<string, any> = any> = Router<
181
- any,
182
- TContext,
183
- any,
184
- any,
185
- any,
186
- any,
187
- any,
188
- any
189
- >;
190
-
191
- /**
192
- * @internal
193
- * @deprecated
194
- */
195
- // ts-prune-ignore-next
196
- export type inferRouterError<TRouter extends AnyRouter> = ReturnType<
197
- TRouter['getErrorShape']
198
- >;
199
- const PROCEDURE_DEFINITION_MAP: Record<
200
- ProcedureType,
201
- 'mutations' | 'queries' | 'subscriptions'
202
- > = {
203
- query: 'queries',
204
- mutation: 'mutations',
205
- subscription: 'subscriptions',
206
- };
207
-
208
- /**
209
- * @internal
210
- * @deprecated
211
- */
212
- export type ErrorFormatter<TContext, TShape extends TRPCErrorShape<number>> = ({
213
- error,
214
- }: {
215
- error: TRPCError;
216
- type: ProcedureType | 'unknown';
217
- path: string | undefined;
218
- input: unknown;
219
- ctx: TContext | undefined;
220
- shape: DefaultErrorShape;
221
- }) => TShape;
222
-
223
- type DefaultErrorData = {
224
- code: TRPC_ERROR_CODE_KEY;
225
- httpStatus: number;
226
- path?: string;
227
- stack?: string;
228
- };
229
-
230
- /**
231
- * @internal
232
- * @deprecated
233
- */
234
- export interface DefaultErrorShape
235
- extends TRPCErrorShape<TRPC_ERROR_CODE_NUMBER, DefaultErrorData> {
236
- message: string;
237
- code: TRPC_ERROR_CODE_NUMBER;
238
- }
239
-
240
- /**
241
- * Create an empty object with `Object.create(null)`.
242
- * Objects made from `Object.create(null)` are totally empty -- they do not inherit anything from Object.prototype.
243
- */
244
- function safeObject(): {};
245
-
246
- /**
247
- * Create an object without inheriting anything from `Object.prototype`
248
- */
249
- function safeObject<TObj>(obj: TObj): TObj;
250
- /**
251
- * Merge two objects without inheritance from `Object.prototype`
252
- */
253
- function safeObject<TObj1, TObj2>(obj1: TObj1, obj2: TObj2): TObj1 & TObj2;
254
- function safeObject(...args: unknown[]) {
255
- return Object.assign(Object.create(null), ...args);
256
- }
257
- type SwapProcedureContext<
258
- TProcedure extends Procedure<any, any, any, any, any, any, any>,
259
- TNewContext,
260
- > = TProcedure extends Procedure<
261
- infer TInputContext,
262
- infer _TOldContext,
263
- infer TMeta,
264
- infer TInput,
265
- infer TParsedInput,
266
- infer TOutput,
267
- infer TParsedOutput
268
- >
269
- ? Procedure<
270
- TInputContext,
271
- TNewContext,
272
- TMeta,
273
- TInput,
274
- TParsedInput,
275
- TOutput,
276
- TParsedOutput
277
- >
278
- : never;
279
-
280
- type SwapContext<
281
- TObj extends ProcedureRecord<any, any, any, any, any, any>,
282
- TNewContext,
283
- > = {
284
- [P in keyof TObj]: SwapProcedureContext<TObj[P], TNewContext>;
285
- };
286
-
287
- /**
288
- * @internal The type signature of this class may change without warning.
289
- * @deprecated
290
- */
291
- export class Router<
292
- TInputContext extends Record<string, any>,
293
- TContext extends Record<string, any>,
294
- TMeta extends Record<string, any>,
295
- TQueries extends ProcedureRecord<
296
- TInputContext,
297
- TContext,
298
- any,
299
- any,
300
- any,
301
- any,
302
- any
303
- >,
304
- TMutations extends ProcedureRecord<
305
- TInputContext,
306
- TContext,
307
- any,
308
- any,
309
- any,
310
- any,
311
- any
312
- >,
313
- TSubscriptions extends ProcedureRecord<
314
- TInputContext,
315
- TContext,
316
- unknown,
317
- unknown,
318
- Observable<unknown, unknown>,
319
- unknown,
320
- unknown
321
- >,
322
- TErrorShape extends TRPCErrorShape<number>,
323
- TTransformer extends CombinedDataTransformer = DefaultDataTransformer,
324
- > {
325
- readonly _def: {
326
- queries: TQueries;
327
- mutations: TMutations;
328
- subscriptions: TSubscriptions;
329
- middlewares: MiddlewareFunction<TInputContext, TContext, TMeta>[];
330
- errorFormatter: ErrorFormatter<TContext, TErrorShape>;
331
- transformer: CombinedDataTransformer;
332
- };
333
-
334
- constructor(def?: {
335
- queries?: TQueries;
336
- mutations?: TMutations;
337
- subscriptions?: TSubscriptions;
338
- middlewares?: MiddlewareFunction<TInputContext, TContext, TMeta>[];
339
- errorFormatter?: ErrorFormatter<TContext, TErrorShape>;
340
- transformer?: CombinedDataTransformer;
341
- }) {
342
- this._def = {
343
- queries: (def?.queries ?? safeObject()) as TQueries,
344
- mutations: (def?.mutations ?? safeObject()) as TMutations,
345
- subscriptions: (def?.subscriptions ?? safeObject()) as TSubscriptions,
346
- middlewares: def?.middlewares ?? [],
347
- errorFormatter: def?.errorFormatter ?? defaultFormatter,
348
- transformer: def?.transformer ?? defaultTransformer,
349
- };
350
- }
351
-
352
- private static prefixProcedures<
353
- TProcedures extends ProcedureRecord,
354
- TPrefix extends string,
355
- >(procedures: TProcedures, prefix: TPrefix): Prefixer<TProcedures, TPrefix> {
356
- const eps: ProcedureRecord = safeObject();
357
- for (const [key, procedure] of Object.entries(procedures)) {
358
- eps[prefix + key] = procedure;
359
- }
360
- return eps as any;
361
- }
362
-
363
- public query<
364
- TPath extends string,
365
- TInput,
366
- TParsedInput,
367
- TOutput,
368
- TParsedOutput,
369
- >(
370
- path: TPath,
371
- procedure: CreateProcedureWithInputOutputParser<
372
- TContext,
373
- TMeta,
374
- TInput,
375
- TParsedInput,
376
- TOutput,
377
- TParsedOutput
378
- >,
379
- ): Router<
380
- TInputContext,
381
- TContext,
382
- TMeta,
383
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
384
- TQueries,
385
- TMutations,
386
- TSubscriptions,
387
- TErrorShape,
388
- TTransformer
389
- >;
390
-
391
- public query<TPath extends string, TInput, TOutput>(
392
- path: TPath,
393
- procedure: CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
394
- ): Router<
395
- TInputContext,
396
- TContext,
397
- TMeta,
398
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
399
- TQueries,
400
- TMutations,
401
- TSubscriptions,
402
- TErrorShape,
403
- TTransformer
404
- >;
405
-
406
- public query<TPath extends string, TOutput, TParsedOutput>(
407
- path: TPath,
408
- procedure: CreateProcedureWithoutInput<
409
- TContext,
410
- TMeta,
411
- TOutput,
412
- TParsedOutput
413
- >,
414
- ): Router<
415
- TInputContext,
416
- TContext,
417
- TMeta,
418
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
419
- TQueries,
420
- TMutations,
421
- TSubscriptions,
422
- TErrorShape,
423
- TTransformer
424
- >;
425
-
426
- query(
427
- path: string,
428
- procedure: CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
429
- ) {
430
- const router = new Router<TContext, TContext, TMeta, any, {}, {}, any, any>(
431
- {
432
- queries: safeObject({
433
- [path]: createProcedure(procedure),
434
- }),
435
- },
436
- );
437
-
438
- return this.merge(router);
439
- }
440
-
441
- public mutation<
442
- TPath extends string,
443
- TInput,
444
- TParsedInput,
445
- TOutput,
446
- TParsedOutput,
447
- >(
448
- path: TPath,
449
- procedure: CreateProcedureWithInputOutputParser<
450
- TContext,
451
- TMeta,
452
- TInput,
453
- TParsedInput,
454
- TOutput,
455
- TParsedOutput
456
- >,
457
- ): Router<
458
- TInputContext,
459
- TContext,
460
- TMeta,
461
- TQueries,
462
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
463
- TMutations,
464
- TSubscriptions,
465
- TErrorShape,
466
- TTransformer
467
- >;
468
-
469
- public mutation<TPath extends string, TInput, TOutput>(
470
- path: TPath,
471
- procedure: CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
472
- ): Router<
473
- TInputContext,
474
- TContext,
475
- TMeta,
476
- TQueries,
477
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
478
- TMutations,
479
- TSubscriptions,
480
- TErrorShape,
481
- TTransformer
482
- >;
483
-
484
- public mutation<TPath extends string, TOutput, TParsedOutput>(
485
- path: TPath,
486
- procedure: CreateProcedureWithoutInput<
487
- TContext,
488
- TMeta,
489
- TOutput,
490
- TParsedOutput
491
- >,
492
- ): Router<
493
- TInputContext,
494
- TContext,
495
- TMeta,
496
- TQueries,
497
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
498
- TMutations,
499
- TSubscriptions,
500
- TErrorShape,
501
- TTransformer
502
- >;
503
-
504
- public mutation(
505
- path: string,
506
- procedure: CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
507
- ) {
508
- const router = new Router<TContext, TContext, TMeta, {}, any, {}, any, any>(
509
- {
510
- mutations: safeObject({
511
- [path]: createProcedure(procedure),
512
- }),
513
- },
514
- );
515
-
516
- return this.merge(router);
517
- }
518
-
519
- /**
520
- * @beta Might change without a major version bump
521
- */
522
- public subscription<
523
- TPath extends string,
524
- TInput,
525
- TParsedInput,
526
- TOutput extends Observable<unknown, unknown>,
527
- >(
528
- path: TPath,
529
- procedure: Omit<
530
- CreateProcedureWithInputOutputParser<
531
- TContext,
532
- TMeta,
533
- TInput,
534
- TParsedInput,
535
- TOutput,
536
- unknown
537
- >,
538
- 'output'
539
- >,
540
- ): Router<
541
- TInputContext,
542
- TContext,
543
- TMeta,
544
- TQueries,
545
- TMutations,
546
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
547
- TSubscriptions,
548
- TErrorShape,
549
- TTransformer
550
- >;
551
-
552
- /**
553
- * @beta Might change without a major version bump
554
- */
555
- public subscription<
556
- TPath extends string,
557
- TInput,
558
- TOutput extends Observable<unknown, unknown>,
559
- >(
560
- path: TPath,
561
- procedure: Omit<
562
- CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
563
- 'output'
564
- >,
565
- ): Router<
566
- TInputContext,
567
- TContext,
568
- TMeta,
569
- TQueries,
570
- TMutations,
571
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
572
- TSubscriptions,
573
- TErrorShape,
574
- TTransformer
575
- >;
576
-
577
- /**
578
- * @beta Might change without a major version bump
579
- */
580
- public subscription<
581
- TPath extends string,
582
- TOutput extends Observable<unknown, unknown>,
583
- >(
584
- path: TPath,
585
- procedure: Omit<
586
- CreateProcedureWithoutInput<TContext, TMeta, TOutput, unknown>,
587
- 'output'
588
- >,
589
- ): Router<
590
- TInputContext,
591
- TContext,
592
- TMeta,
593
- TQueries,
594
- TMutations,
595
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
596
- TSubscriptions,
597
- TErrorShape,
598
- TTransformer
599
- >;
600
-
601
- public subscription(
602
- path: string,
603
- procedure: Omit<
604
- CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
605
- 'output'
606
- >,
607
- ) {
608
- const router = new Router<TContext, TContext, TMeta, {}, {}, any, any, any>(
609
- {
610
- subscriptions: safeObject({
611
- [path]: createProcedure(procedure),
612
- }),
613
- },
614
- );
615
-
616
- return this.merge(router) as any;
617
- }
618
-
619
- /**
620
- * Merge router with other router
621
- * @param router
622
- */
623
- public merge<
624
- TChildRouter extends Router<TContext, any, TMeta, any, any, any, any, any>,
625
- >(
626
- router: TChildRouter,
627
- ): Router<
628
- TInputContext,
629
- inferRouterContext<TChildRouter>,
630
- TMeta,
631
- TChildRouter['_def']['queries'] & TQueries,
632
- TChildRouter['_def']['mutations'] & TMutations,
633
- TChildRouter['_def']['subscriptions'] & TSubscriptions,
634
- TErrorShape,
635
- TTransformer
636
- >;
637
-
638
- /**
639
- * Merge router with other router
640
- * @param prefix Prefix that this router should live under
641
- * @param router
642
- */
643
- public merge<
644
- TPath extends string,
645
- TChildRouter extends Router<TContext, any, TMeta, any, any, any, any, any>,
646
- >(
647
- prefix: TPath,
648
- router: TChildRouter,
649
- ): Router<
650
- TInputContext,
651
- inferRouterContext<TChildRouter>,
652
- TMeta,
653
- Prefixer<TChildRouter['_def']['queries'], `${TPath}`> & TQueries,
654
- Prefixer<TChildRouter['_def']['mutations'], `${TPath}`> & TMutations,
655
- Prefixer<TChildRouter['_def']['subscriptions'], `${TPath}`> &
656
- TSubscriptions,
657
- TErrorShape,
658
- TTransformer
659
- >;
660
-
661
- public merge(prefixOrRouter: unknown, maybeRouter?: unknown) {
662
- let prefix = '';
663
- let childRouter: AnyRouter;
664
-
665
- if (typeof prefixOrRouter === 'string' && maybeRouter instanceof Router) {
666
- prefix = prefixOrRouter;
667
- childRouter = maybeRouter;
668
- } else if (prefixOrRouter instanceof Router) {
669
- childRouter = prefixOrRouter;
670
- } /* istanbul ignore next */ else {
671
- throw new Error('Invalid args');
672
- }
673
-
674
- const duplicateQueries = Object.keys(childRouter._def.queries).filter(
675
- (key) => !!this._def.queries[prefix + key],
676
- );
677
- const duplicateMutations = Object.keys(childRouter._def.mutations).filter(
678
- (key) => !!this._def.mutations[prefix + key],
679
- );
680
- const duplicateSubscriptions = Object.keys(
681
- childRouter._def.subscriptions,
682
- ).filter((key) => !!this._def.subscriptions[prefix + key]);
683
-
684
- const duplicates = [
685
- ...duplicateQueries,
686
- ...duplicateMutations,
687
- ...duplicateSubscriptions,
688
- ];
689
- if (duplicates.length) {
690
- throw new Error(`Duplicate endpoint(s): ${duplicates.join(', ')}`);
691
- }
692
-
693
- const mergeProcedures = (defs: ProcedureRecord) => {
694
- const newDefs = safeObject() as typeof defs;
695
- for (const [key, procedure] of Object.entries(defs)) {
696
- const newProcedure = procedure.inheritMiddlewares(
697
- this._def.middlewares,
698
- );
699
- newDefs[key] = newProcedure;
700
- }
701
-
702
- return Router.prefixProcedures(newDefs, prefix);
703
- };
704
-
705
- return new Router<
706
- TInputContext,
707
- any,
708
- TMeta,
709
- any,
710
- any,
711
- any,
712
- TErrorShape,
713
- any
714
- >({
715
- ...this._def,
716
- queries: safeObject(
717
- this._def.queries,
718
- mergeProcedures(childRouter._def.queries),
719
- ),
720
- mutations: safeObject(
721
- this._def.mutations,
722
- mergeProcedures(childRouter._def.mutations),
723
- ),
724
- subscriptions: safeObject(
725
- this._def.subscriptions,
726
- mergeProcedures(childRouter._def.subscriptions),
727
- ),
728
- });
729
- }
730
-
731
- /**
732
- * Invoke procedure. Only for internal use within library.
733
- */
734
- private async call(
735
- opts: ProcedureCallOptions<TInputContext>,
736
- ): Promise<unknown> {
737
- const { type, path } = opts;
738
- const defTarget = PROCEDURE_DEFINITION_MAP[type];
739
- const defs = this._def[defTarget];
740
- const procedure = defs[path] as
741
- | Procedure<TInputContext, TContext, TMeta, any, any, any, any>
742
- | undefined;
743
-
744
- if (!procedure) {
745
- throw new TRPCError({
746
- code: 'NOT_FOUND',
747
- message: `No "${type}"-procedure on path "${path}"`,
748
- });
749
- }
750
-
751
- return procedure.call(opts);
752
- }
753
-
754
- public createCaller(ctx: TInputContext): {
755
- query: inferHandlerFn<TQueries>;
756
- mutation: inferHandlerFn<TMutations>;
757
- subscription: inferHandlerFn<TSubscriptions>;
758
- } {
759
- return {
760
- query: (path, ...args) => {
761
- return this.call({
762
- type: 'query',
763
- ctx,
764
- path,
765
- rawInput: args[0],
766
- }) as any;
767
- },
768
- mutation: (path, ...args) => {
769
- return this.call({
770
- type: 'mutation',
771
- ctx,
772
- path,
773
- rawInput: args[0],
774
- }) as any;
775
- },
776
- subscription: (path, ...args) => {
777
- return this.call({
778
- type: 'subscription',
779
- ctx,
780
- path,
781
- rawInput: args[0],
782
- }) as any;
783
- },
784
- };
785
- }
786
-
787
- /**
788
- * Function to be called before any procedure is invoked
789
- * @link https://trpc.io/docs/middlewares
790
- */
791
- public middleware<TNewContext extends Record<string, any>>(
792
- middleware: MiddlewareFunction<TContext, TNewContext, TMeta>,
793
- ): Router<
794
- TInputContext,
795
- TNewContext,
796
- TMeta,
797
- SwapContext<TQueries, TNewContext>,
798
- SwapContext<TMutations, TNewContext>,
799
- SwapContext<TSubscriptions, TNewContext>,
800
- TErrorShape,
801
- TTransformer
802
- > {
803
- return new Router({
804
- ...this._def,
805
- middlewares: [...this._def.middlewares, middleware as any],
806
- } as any);
807
- }
808
-
809
- /**
810
- * Format errors
811
- * @link https://trpc.io/docs/error-formatting
812
- */
813
- public formatError<
814
- TErrorFormatter extends ErrorFormatter<TContext, TRPCErrorShape<number>>,
815
- >(errorFormatter: TErrorFormatter) {
816
- if (this._def.errorFormatter !== (defaultFormatter as any)) {
817
- throw new Error(
818
- 'You seem to have double `formatError()`-calls in your router tree',
819
- );
820
- }
821
- return new Router<
822
- TInputContext,
823
- TContext,
824
- TMeta,
825
- TQueries,
826
- TMutations,
827
- TSubscriptions,
828
- ReturnType<TErrorFormatter>,
829
- TTransformer
830
- >({
831
- ...this._def,
832
- errorFormatter: errorFormatter as any,
833
- });
834
- }
835
-
836
- public getErrorShape(opts: {
837
- error: TRPCError;
838
- type: ProcedureType | 'unknown';
839
- path: string | undefined;
840
- input: unknown;
841
- ctx: TContext | undefined;
842
- }): TErrorShape {
843
- const { path, error } = opts;
844
- const { code } = opts.error;
845
- const shape: DefaultErrorShape = {
846
- message: error.message,
847
- code: TRPC_ERROR_CODES_BY_KEY[code],
848
- data: {
849
- code,
850
- httpStatus: getHTTPStatusCodeFromError(error),
851
- },
852
- };
853
- if (
854
- globalThis.process?.env?.NODE_ENV !== 'production' &&
855
- typeof opts.error.stack === 'string'
856
- ) {
857
- shape.data.stack = opts.error.stack;
858
- }
859
- if (typeof path === 'string') {
860
- shape.data.path = path;
861
- }
862
- return this._def.errorFormatter({ ...opts, shape });
863
- }
864
-
865
- /**
866
- * Add data transformer to serialize/deserialize input args + output
867
- * @link https://trpc.io/docs/data-transformers
868
- */
869
- transformer(_transformer: DataTransformerOptions) {
870
- const transformer = getDataTransformer(_transformer);
871
-
872
- if (this._def.transformer !== defaultTransformer) {
873
- throw new Error(
874
- 'You seem to have double `transformer()`-calls in your router tree',
875
- );
876
- }
877
- return new Router<
878
- TInputContext,
879
- TContext,
880
- TMeta,
881
- TQueries,
882
- TMutations,
883
- TSubscriptions,
884
- TErrorShape,
885
- CombinedDataTransformer
886
- >({
887
- ...this._def,
888
- transformer,
889
- });
890
- }
891
-
892
- /**
893
- * Flattens the generics of TQueries/TMutations/TSubscriptions.
894
- * ⚠️ Experimental - might disappear. ⚠️
895
- *
896
- * @alpha
897
- */
898
- public flat(): Router<
899
- TInputContext,
900
- TContext,
901
- TMeta,
902
- FlatOverwrite<{}, TQueries>,
903
- FlatOverwrite<{}, TMutations>,
904
- FlatOverwrite<{}, TSubscriptions>,
905
- TErrorShape,
906
- TTransformer
907
- > {
908
- return this as any;
909
- }
910
-
911
- /**
912
- * Interop mode for v9.x -> v10.x
913
- */
914
- public interop(): MigrateRouter<
915
- TInputContext,
916
- TContext,
917
- TMeta,
918
- TQueries,
919
- TMutations,
920
- TSubscriptions,
921
- TErrorShape,
922
- TTransformer
923
- > {
924
- return migrateRouter(this);
925
- }
926
- }
927
-
928
- /**
929
- * @deprecated
930
- */
931
- export function router<
932
- TContext extends Record<string, any> = {},
933
- TMeta extends Record<string, any> = {},
934
- >() {
935
- return new Router<
936
- TContext,
937
- TContext,
938
- TMeta,
939
- {},
940
- {},
941
- {},
942
- DefaultErrorShape,
943
- DefaultDataTransformer
944
- >();
945
- }