@trpc/server 11.0.0-alpha-tmp-issues-5557-types-node-wut.324 → 11.0.0-alpha-tmp-04-02-no-typesProxy.331

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 (103) hide show
  1. package/adapters/next-app-dir/index.d.ts +1 -0
  2. package/adapters/next-app-dir/index.js +1 -0
  3. package/dist/adapters/aws-lambda/index.js +1 -0
  4. package/dist/adapters/aws-lambda/index.mjs +1 -0
  5. package/dist/adapters/aws-lambda/utils.js +1 -0
  6. package/dist/adapters/aws-lambda/utils.mjs +1 -0
  7. package/dist/adapters/fastify/fastifyRequestHandler.js +1 -0
  8. package/dist/adapters/fastify/fastifyRequestHandler.mjs +1 -0
  9. package/dist/adapters/fastify/fastifyTRPCPlugin.js +1 -1
  10. package/dist/adapters/fastify/fastifyTRPCPlugin.mjs +1 -1
  11. package/dist/adapters/fetch/fetchRequestHandler.js +1 -0
  12. package/dist/adapters/fetch/fetchRequestHandler.mjs +1 -0
  13. package/dist/adapters/next-app-dir/formDataToObject.d.ts +2 -0
  14. package/dist/adapters/next-app-dir/formDataToObject.d.ts.map +1 -0
  15. package/dist/adapters/next-app-dir/formDataToObject.js +34 -0
  16. package/dist/adapters/next-app-dir/formDataToObject.mjs +32 -0
  17. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts +24 -0
  18. package/dist/adapters/next-app-dir/nextAppDirCaller.d.ts.map +1 -0
  19. package/dist/adapters/next-app-dir/nextAppDirCaller.js +86 -0
  20. package/dist/adapters/next-app-dir/nextAppDirCaller.mjs +84 -0
  21. package/dist/adapters/next-app-dir/notFound.d.ts +7 -0
  22. package/dist/adapters/next-app-dir/notFound.d.ts.map +1 -0
  23. package/dist/adapters/next-app-dir/notFound.js +16 -0
  24. package/dist/adapters/next-app-dir/notFound.mjs +14 -0
  25. package/dist/adapters/next-app-dir/redirect.d.ts +18 -0
  26. package/dist/adapters/next-app-dir/redirect.d.ts.map +1 -0
  27. package/dist/adapters/next-app-dir/redirect.js +34 -0
  28. package/dist/adapters/next-app-dir/redirect.mjs +31 -0
  29. package/dist/adapters/next-app-dir/rethrowNextErrors.d.ts +6 -0
  30. package/dist/adapters/next-app-dir/rethrowNextErrors.d.ts.map +1 -0
  31. package/dist/adapters/next-app-dir/rethrowNextErrors.js +23 -0
  32. package/dist/adapters/next-app-dir/rethrowNextErrors.mjs +21 -0
  33. package/dist/adapters/next-app-dir.d.ts +4 -0
  34. package/dist/adapters/next-app-dir.d.ts.map +1 -0
  35. package/dist/adapters/next-app-dir.js +11 -0
  36. package/dist/adapters/next-app-dir.mjs +3 -0
  37. package/dist/adapters/next.js +1 -0
  38. package/dist/adapters/next.mjs +1 -0
  39. package/dist/adapters/node-http/content-type/json/getPostBody.js +1 -0
  40. package/dist/adapters/node-http/content-type/json/getPostBody.mjs +1 -0
  41. package/dist/adapters/node-http/content-type/json/index.js +1 -0
  42. package/dist/adapters/node-http/content-type/json/index.mjs +1 -0
  43. package/dist/adapters/node-http/nodeHTTPRequestHandler.js +1 -0
  44. package/dist/adapters/node-http/nodeHTTPRequestHandler.mjs +1 -0
  45. package/dist/adapters/standalone.js +1 -0
  46. package/dist/adapters/standalone.mjs +1 -0
  47. package/dist/adapters/ws.js +1 -0
  48. package/dist/adapters/ws.mjs +1 -0
  49. package/dist/bundle-analysis.json +178 -92
  50. package/dist/http.js +1 -0
  51. package/dist/http.mjs +1 -0
  52. package/dist/index.js +1 -0
  53. package/dist/index.mjs +1 -0
  54. package/dist/rpc.js +1 -0
  55. package/dist/rpc.mjs +1 -0
  56. package/dist/shared.js +1 -0
  57. package/dist/shared.mjs +1 -0
  58. package/dist/unstable-core-do-not-import/clientish/inference.d.ts +3 -3
  59. package/dist/unstable-core-do-not-import/clientish/inference.d.ts.map +1 -1
  60. package/dist/unstable-core-do-not-import/error/getErrorShape.js +1 -0
  61. package/dist/unstable-core-do-not-import/error/getErrorShape.mjs +1 -0
  62. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.js +1 -0
  63. package/dist/unstable-core-do-not-import/http/getHTTPStatusCode.mjs +1 -0
  64. package/dist/unstable-core-do-not-import/http/index.d.ts +5 -1
  65. package/dist/unstable-core-do-not-import/http/index.d.ts.map +1 -1
  66. package/dist/unstable-core-do-not-import/http/types.d.ts +7 -10
  67. package/dist/unstable-core-do-not-import/http/types.d.ts.map +1 -1
  68. package/dist/unstable-core-do-not-import/initTRPC.d.ts +3 -3
  69. package/dist/unstable-core-do-not-import/initTRPC.d.ts.map +1 -1
  70. package/dist/unstable-core-do-not-import/initTRPC.js +2 -4
  71. package/dist/unstable-core-do-not-import/initTRPC.mjs +2 -4
  72. package/dist/unstable-core-do-not-import/procedure.d.ts +23 -5
  73. package/dist/unstable-core-do-not-import/procedure.d.ts.map +1 -1
  74. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts +36 -15
  75. package/dist/unstable-core-do-not-import/procedureBuilder.d.ts.map +1 -1
  76. package/dist/unstable-core-do-not-import/procedureBuilder.js +28 -3
  77. package/dist/unstable-core-do-not-import/procedureBuilder.mjs +29 -4
  78. package/dist/unstable-core-do-not-import/router.d.ts +12 -6
  79. package/dist/unstable-core-do-not-import/router.d.ts.map +1 -1
  80. package/dist/unstable-core-do-not-import/router.js +18 -23
  81. package/dist/unstable-core-do-not-import/router.mjs +20 -25
  82. package/dist/unstable-core-do-not-import/utils.d.ts +8 -0
  83. package/dist/unstable-core-do-not-import/utils.d.ts.map +1 -1
  84. package/dist/unstable-core-do-not-import/utils.js +16 -0
  85. package/dist/unstable-core-do-not-import/utils.mjs +15 -1
  86. package/dist/unstable-core-do-not-import.js +2 -0
  87. package/dist/unstable-core-do-not-import.mjs +1 -1
  88. package/package.json +10 -5
  89. package/src/adapters/fastify/fastifyTRPCPlugin.ts +1 -1
  90. package/src/adapters/next-app-dir/formDataToObject.ts +36 -0
  91. package/src/adapters/next-app-dir/nextAppDirCaller.ts +129 -0
  92. package/src/adapters/next-app-dir/notFound.ts +12 -0
  93. package/src/adapters/next-app-dir/redirect.ts +30 -0
  94. package/src/adapters/next-app-dir/rethrowNextErrors.ts +25 -0
  95. package/src/adapters/next-app-dir.ts +3 -0
  96. package/src/unstable-core-do-not-import/clientish/inference.ts +9 -5
  97. package/src/unstable-core-do-not-import/http/index.ts +5 -1
  98. package/src/unstable-core-do-not-import/http/types.ts +9 -10
  99. package/src/unstable-core-do-not-import/initTRPC.ts +2 -6
  100. package/src/unstable-core-do-not-import/procedure.ts +27 -7
  101. package/src/unstable-core-do-not-import/procedureBuilder.ts +106 -27
  102. package/src/unstable-core-do-not-import/router.ts +49 -35
  103. package/src/unstable-core-do-not-import/utils.ts +20 -0
@@ -9,7 +9,11 @@ export type {
9
9
  HTTPHeaders,
10
10
  HTTPRequest,
11
11
  HTTPResponse,
12
- OnErrorFunction,
12
+ /**
13
+ * @deprecated Use `HTTPErrorHandler` instead
14
+ */
15
+ HTTPErrorHandler as OnErrorFunction,
16
+ HTTPErrorHandler,
13
17
  ProcedureCall,
14
18
  ResolveHTTPRequestOptionsContextFn,
15
19
  ResponseChunk,
@@ -1,5 +1,5 @@
1
1
  import type { TRPCError } from '../error/TRPCError';
2
- import type { ProcedureType } from '../procedure';
2
+ import type { ErrorHandlerOptions, ProcedureType } from '../procedure';
3
3
  import type {
4
4
  AnyRouter,
5
5
  inferRouterContext,
@@ -86,24 +86,23 @@ export type TRPCRequestInfo = {
86
86
  export type ResolveHTTPRequestOptionsContextFn<TRouter extends AnyRouter> =
87
87
  (opts: { info: TRPCRequestInfo }) => Promise<inferRouterContext<TRouter>>;
88
88
 
89
+ interface HTTPErrorHandlerOptions<TRouter extends AnyRouter, TRequest>
90
+ extends ErrorHandlerOptions<inferRouterContext<TRouter>> {
91
+ req: TRequest;
92
+ }
89
93
  /**
90
94
  * @internal
91
95
  */
92
- export type OnErrorFunction<TRouter extends AnyRouter, TRequest> = (opts: {
93
- error: TRPCError;
94
- type: ProcedureType | 'unknown';
95
- path: string | undefined;
96
- req: TRequest;
97
- input: unknown;
98
- ctx: inferRouterContext<TRouter> | undefined;
99
- }) => void;
96
+ export type HTTPErrorHandler<TRouter extends AnyRouter, TRequest> = (
97
+ opts: HTTPErrorHandlerOptions<TRouter, TRequest>,
98
+ ) => void;
100
99
 
101
100
  /**
102
101
  * Base interface for any response handler
103
102
  * @internal
104
103
  */
105
104
  export interface BaseHandlerOptions<TRouter extends AnyRouter, TRequest> {
106
- onError?: OnErrorFunction<TRouter, TRequest>;
105
+ onError?: HTTPErrorHandler<TRouter, TRequest>;
107
106
  /**
108
107
  * @deprecated use `allowBatching` instead, this will be removed in v12
109
108
  */
@@ -1,4 +1,3 @@
1
- import { createFlatProxy } from './createProxy';
2
1
  import {
3
2
  defaultFormatter,
4
3
  type DefaultErrorShape,
@@ -16,6 +15,7 @@ import {
16
15
  import type { DataTransformerOptions } from './transformer';
17
16
  import { defaultTransformer, getDataTransformer } from './transformer';
18
17
  import type { Unwrap, ValidateShape } from './types';
18
+ import { $typesProxy } from './utils';
19
19
 
20
20
  type inferErrorFormatterShape<TType> = TType extends ErrorFormatter<
21
21
  any,
@@ -95,11 +95,7 @@ class TRPCBuilder<TContext extends object, TMeta extends object> {
95
95
  * These are just types, they can't be used at runtime
96
96
  * @internal
97
97
  */
98
- $types: createFlatProxy((key) => {
99
- throw new Error(
100
- `Tried to access "$types.${key}" which is not available at runtime`,
101
- );
102
- }),
98
+ $types: $typesProxy,
103
99
  };
104
100
 
105
101
  {
@@ -1,3 +1,4 @@
1
+ import type { TRPCError } from './error/TRPCError';
1
2
  import type { ProcedureCallOptions } from './procedureBuilder';
2
3
 
3
4
  export const procedureTypes = ['query', 'mutation', 'subscription'] as const;
@@ -23,6 +24,7 @@ interface BuiltProcedureDef {
23
24
  input: unknown;
24
25
  output: unknown;
25
26
  }
27
+
26
28
  /**
27
29
  *
28
30
  * @internal
@@ -32,8 +34,14 @@ export interface Procedure<
32
34
  TDef extends BuiltProcedureDef,
33
35
  > {
34
36
  _def: {
35
- _input_in: TDef['input'];
36
- _output_out: TDef['output'];
37
+ /**
38
+ * These are just types, they can't be used at runtime
39
+ * @internal
40
+ */
41
+ $types: {
42
+ input: TDef['input'];
43
+ output: TDef['output'];
44
+ };
37
45
  procedure: true;
38
46
  type: TType;
39
47
  /**
@@ -41,11 +49,12 @@ export interface Procedure<
41
49
  * Meta is not inferrable on individual procedures, only on the router
42
50
  */
43
51
  meta: unknown;
52
+ experimental_caller: boolean;
44
53
  };
45
54
  /**
46
55
  * @internal
47
56
  */
48
- (opts: ProcedureCallOptions): Promise<unknown>;
57
+ (opts: ProcedureCallOptions<unknown>): Promise<TDef['output']>;
49
58
  }
50
59
 
51
60
  export interface QueryProcedure<TDef extends BuiltProcedureDef>
@@ -63,12 +72,23 @@ export type AnySubscriptionProcedure = SubscriptionProcedure<any>;
63
72
  export type AnyProcedure = Procedure<ProcedureType, any>;
64
73
 
65
74
  export type inferProcedureInput<TProcedure extends AnyProcedure> =
66
- undefined extends inferProcedureParams<TProcedure>['_input_in']
67
- ? void | inferProcedureParams<TProcedure>['_input_in']
68
- : inferProcedureParams<TProcedure>['_input_in'];
75
+ undefined extends inferProcedureParams<TProcedure>['$types']['input']
76
+ ? void | inferProcedureParams<TProcedure>['$types']['input']
77
+ : inferProcedureParams<TProcedure>['$types']['input'];
69
78
 
70
79
  export type inferProcedureParams<TProcedure> = TProcedure extends AnyProcedure
71
80
  ? TProcedure['_def']
72
81
  : never;
73
82
  export type inferProcedureOutput<TProcedure> =
74
- inferProcedureParams<TProcedure>['_output_out'];
83
+ inferProcedureParams<TProcedure>['$types']['output'];
84
+
85
+ /**
86
+ * @internal
87
+ */
88
+ export interface ErrorHandlerOptions<TContext> {
89
+ error: TRPCError;
90
+ type: ProcedureType | 'unknown';
91
+ path: string | undefined;
92
+ input: unknown;
93
+ ctx: TContext | undefined;
94
+ }
@@ -30,7 +30,7 @@ import type {
30
30
  Simplify,
31
31
  TypeError,
32
32
  } from './types';
33
- import { mergeWithoutOverrides } from './utils';
33
+ import { $typesProxy, mergeWithoutOverrides } from './utils';
34
34
 
35
35
  type IntersectIfDefined<TType, TWith> = TType extends UnsetMarker
36
36
  ? TWith
@@ -45,6 +45,11 @@ type DefaultValue<TValue, TFallback> = TValue extends UnsetMarker
45
45
  ? TFallback
46
46
  : TValue;
47
47
 
48
+ export type CallerOverride<TContext> = (opts: {
49
+ args: unknown[];
50
+ invoke: (opts: ProcedureCallOptions<TContext>) => Promise<unknown>;
51
+ _def: AnyProcedure['_def'];
52
+ }) => Promise<unknown>;
48
53
  type ProcedureBuilderDef<TMeta> = {
49
54
  procedure: true;
50
55
  inputs: Parser[];
@@ -52,9 +57,20 @@ type ProcedureBuilderDef<TMeta> = {
52
57
  meta?: TMeta;
53
58
  resolver?: ProcedureBuilderResolver;
54
59
  middlewares: AnyMiddlewareFunction[];
60
+ /**
61
+ * @deprecated use `type` instead
62
+ */
55
63
  mutation?: boolean;
64
+ /**
65
+ * @deprecated use `type` instead
66
+ */
56
67
  query?: boolean;
68
+ /**
69
+ * @deprecated use `type` instead
70
+ */
57
71
  subscription?: boolean;
72
+ type?: ProcedureType;
73
+ caller?: CallerOverride<unknown>;
58
74
  };
59
75
 
60
76
  type AnyProcedureBuilderDef = ProcedureBuilderDef<any>;
@@ -98,6 +114,7 @@ export type AnyProcedureBuilder = ProcedureBuilder<
98
114
  any,
99
115
  any,
100
116
  any,
117
+ any,
101
118
  any
102
119
  >;
103
120
 
@@ -114,7 +131,8 @@ export type inferProcedureBuilderResolverOptions<
114
131
  infer _TInputIn,
115
132
  infer TInputOut,
116
133
  infer _TOutputIn,
117
- infer _TOutputOut
134
+ infer _TOutputOut,
135
+ infer _TCaller
118
136
  >
119
137
  ? ProcedureResolverOptions<
120
138
  TContext,
@@ -144,6 +162,7 @@ export interface ProcedureBuilder<
144
162
  TInputOut,
145
163
  TOutputIn,
146
164
  TOutputOut,
165
+ TCaller extends boolean,
147
166
  > {
148
167
  /**
149
168
  * Add an input parser to the procedure.
@@ -168,7 +187,8 @@ export interface ProcedureBuilder<
168
187
  IntersectIfDefined<TInputIn, inferParser<$Parser>['in']>,
169
188
  IntersectIfDefined<TInputOut, inferParser<$Parser>['out']>,
170
189
  TOutputIn,
171
- TOutputOut
190
+ TOutputOut,
191
+ TCaller
172
192
  >;
173
193
  /**
174
194
  * Add an output parser to the procedure.
@@ -183,7 +203,8 @@ export interface ProcedureBuilder<
183
203
  TInputIn,
184
204
  TInputOut,
185
205
  IntersectIfDefined<TOutputIn, inferParser<$Parser>['in']>,
186
- IntersectIfDefined<TOutputOut, inferParser<$Parser>['out']>
206
+ IntersectIfDefined<TOutputOut, inferParser<$Parser>['out']>,
207
+ TCaller
187
208
  >;
188
209
  /**
189
210
  * Add a meta data to the procedure.
@@ -198,7 +219,8 @@ export interface ProcedureBuilder<
198
219
  TInputIn,
199
220
  TInputOut,
200
221
  TOutputIn,
201
- TOutputOut
222
+ TOutputOut,
223
+ TCaller
202
224
  >;
203
225
  /**
204
226
  * Add a middleware to the procedure.
@@ -226,7 +248,8 @@ export interface ProcedureBuilder<
226
248
  TInputIn,
227
249
  TInputOut,
228
250
  TOutputIn,
229
- TOutputOut
251
+ TOutputOut,
252
+ TCaller
230
253
  >;
231
254
 
232
255
  /**
@@ -250,7 +273,8 @@ export interface ProcedureBuilder<
250
273
  $InputIn,
251
274
  $InputOut,
252
275
  $OutputIn,
253
- $OutputOut
276
+ $OutputOut,
277
+ TCaller
254
278
  >
255
279
  : TypeError<'Meta mismatch'>
256
280
  : TypeError<'Context mismatch'>,
@@ -261,7 +285,8 @@ export interface ProcedureBuilder<
261
285
  IntersectIfDefined<TInputIn, $InputIn>,
262
286
  IntersectIfDefined<TInputIn, $InputOut>,
263
287
  IntersectIfDefined<TOutputIn, $OutputIn>,
264
- IntersectIfDefined<TOutputOut, $OutputOut>
288
+ IntersectIfDefined<TOutputOut, $OutputOut>,
289
+ TCaller
265
290
  >;
266
291
  /**
267
292
  * Query procedure
@@ -276,10 +301,14 @@ export interface ProcedureBuilder<
276
301
  TOutputIn,
277
302
  $Output
278
303
  >,
279
- ): QueryProcedure<{
280
- input: DefaultValue<TInputIn, void>;
281
- output: DefaultValue<TOutputOut, $Output>;
282
- }>;
304
+ ): TCaller extends true
305
+ ? (
306
+ input: DefaultValue<TInputIn, void>,
307
+ ) => Promise<DefaultValue<TOutputOut, $Output>>
308
+ : QueryProcedure<{
309
+ input: DefaultValue<TInputIn, void>;
310
+ output: DefaultValue<TOutputOut, $Output>;
311
+ }>;
283
312
 
284
313
  /**
285
314
  * Mutation procedure
@@ -294,10 +323,14 @@ export interface ProcedureBuilder<
294
323
  TOutputIn,
295
324
  $Output
296
325
  >,
297
- ): MutationProcedure<{
298
- input: DefaultValue<TInputIn, void>;
299
- output: DefaultValue<TOutputOut, $Output>;
300
- }>;
326
+ ): TCaller extends true
327
+ ? (
328
+ input: DefaultValue<TInputIn, void>,
329
+ ) => Promise<DefaultValue<TOutputOut, $Output>>
330
+ : MutationProcedure<{
331
+ input: DefaultValue<TInputIn, void>;
332
+ output: DefaultValue<TOutputOut, $Output>;
333
+ }>;
301
334
 
302
335
  /**
303
336
  * Subscription procedure
@@ -312,10 +345,29 @@ export interface ProcedureBuilder<
312
345
  TOutputIn,
313
346
  $Output
314
347
  >,
315
- ): SubscriptionProcedure<{
316
- input: DefaultValue<TInputIn, void>;
317
- output: DefaultValue<TOutputOut, inferObservableValue<$Output>>;
318
- }>;
348
+ ): TCaller extends true
349
+ ? TypeError<'Not implemented'>
350
+ : SubscriptionProcedure<{
351
+ input: DefaultValue<TInputIn, void>;
352
+ output: DefaultValue<TOutputOut, inferObservableValue<$Output>>;
353
+ }>;
354
+
355
+ /**
356
+ * Overrides the way a procedure is invoked
357
+ * Do not use this unless you know what you're doing - this is an experimental API
358
+ */
359
+ experimental_caller(
360
+ caller: CallerOverride<TContext>,
361
+ ): ProcedureBuilder<
362
+ TContext,
363
+ TMeta,
364
+ TContextOverrides,
365
+ TInputIn,
366
+ TInputOut,
367
+ TOutputIn,
368
+ TOutputOut,
369
+ true
370
+ >;
319
371
  /**
320
372
  * @internal
321
373
  */
@@ -350,7 +402,8 @@ export function createBuilder<TContext, TMeta>(
350
402
  UnsetMarker,
351
403
  UnsetMarker,
352
404
  UnsetMarker,
353
- UnsetMarker
405
+ UnsetMarker,
406
+ false
354
407
  > {
355
408
  const _def: AnyProcedureBuilderDef = {
356
409
  procedure: true,
@@ -412,16 +465,21 @@ export function createBuilder<TContext, TMeta>(
412
465
  resolver,
413
466
  ) as AnySubscriptionProcedure;
414
467
  },
468
+ experimental_caller(caller) {
469
+ return createNewBuilder(_def, {
470
+ caller,
471
+ }) as any;
472
+ },
415
473
  };
416
474
 
417
475
  return builder;
418
476
  }
419
477
 
420
478
  function createResolver(
421
- _def: AnyProcedureBuilderDef & { type: ProcedureType },
479
+ _defIn: AnyProcedureBuilderDef & { type: ProcedureType },
422
480
  resolver: AnyResolver,
423
481
  ) {
424
- const finalBuilder = createNewBuilder(_def, {
482
+ const finalBuilder = createNewBuilder(_defIn, {
425
483
  resolver,
426
484
  middlewares: [
427
485
  async function resolveMiddleware(opts) {
@@ -435,15 +493,36 @@ function createResolver(
435
493
  },
436
494
  ],
437
495
  });
496
+ const _def: AnyProcedure['_def'] = {
497
+ ...finalBuilder._def,
498
+ type: _defIn.type,
499
+ experimental_caller: Boolean(finalBuilder._def.caller),
500
+ meta: finalBuilder._def.meta,
501
+ $types: $typesProxy,
502
+ };
503
+
504
+ const invoke = createProcedureCaller(finalBuilder._def);
505
+ const callerOverride = finalBuilder._def.caller;
506
+ if (!callerOverride) {
507
+ return invoke;
508
+ }
509
+ const callerWrapper = async (...args: unknown[]) => {
510
+ return await callerOverride({
511
+ args,
512
+ invoke,
513
+ _def: _def,
514
+ });
515
+ };
438
516
 
439
- return createProcedureCaller(finalBuilder._def);
517
+ callerWrapper._def = _def;
518
+ return callerWrapper;
440
519
  }
441
520
 
442
521
  /**
443
522
  * @internal
444
523
  */
445
- export interface ProcedureCallOptions {
446
- ctx: unknown;
524
+ export interface ProcedureCallOptions<TContext> {
525
+ ctx: TContext;
447
526
  getRawInput: GetRawInputFn;
448
527
  input?: unknown;
449
528
  path: string;
@@ -456,7 +535,7 @@ If you want to call this function on the server, see https://trpc.io/docs/v11/se
456
535
  `.trim();
457
536
 
458
537
  function createProcedureCaller(_def: AnyProcedureBuilderDef): AnyProcedure {
459
- async function procedure(opts: ProcedureCallOptions) {
538
+ async function procedure(opts: ProcedureCallOptions<unknown>) {
460
539
  // is direct server-side call
461
540
  if (!opts || !('getRawInput' in opts)) {
462
541
  throw new Error(codeblock);
@@ -1,13 +1,18 @@
1
1
  import type { Observable } from '../observable';
2
2
  import { createRecursiveProxy } from './createProxy';
3
3
  import { defaultFormatter } from './error/formatter';
4
- import { TRPCError } from './error/TRPCError';
5
- import type { AnyProcedure, inferProcedureInput } from './procedure';
4
+ import { getTRPCErrorFromUnknown, TRPCError } from './error/TRPCError';
5
+ import type {
6
+ AnyProcedure,
7
+ ErrorHandlerOptions,
8
+ inferProcedureInput,
9
+ inferProcedureOutput,
10
+ } from './procedure';
6
11
  import type { ProcedureCallOptions } from './procedureBuilder';
7
12
  import type { AnyRootTypes, RootConfig } from './rootConfig';
8
13
  import { defaultTransformer } from './transformer';
9
14
  import type { MaybePromise, ValueOf } from './types';
10
- import { mergeWithoutOverrides, omitPrototype } from './utils';
15
+ import { isFunction, mergeWithoutOverrides, omitPrototype } from './utils';
11
16
 
12
17
  export interface RouterRecord {
13
18
  [key: string]: AnyProcedure | RouterRecord;
@@ -17,8 +22,8 @@ type DecorateProcedure<TProcedure extends AnyProcedure> = (
17
22
  input: inferProcedureInput<TProcedure>,
18
23
  ) => Promise<
19
24
  TProcedure['_def']['type'] extends 'subscription'
20
- ? Observable<TProcedure['_def']['_output_out'], TRPCError>
21
- : TProcedure['_def']['_output_out']
25
+ ? Observable<inferProcedureOutput<TProcedure>, TRPCError>
26
+ : inferProcedureOutput<TProcedure>
22
27
  >;
23
28
 
24
29
  /**
@@ -32,6 +37,14 @@ export type DecorateRouterRecord<TRecord extends RouterRecord> = {
32
37
  : never;
33
38
  };
34
39
 
40
+ /**
41
+ * @internal
42
+ */
43
+
44
+ export type RouterCallerErrorHandler<TContext> = (
45
+ opts: ErrorHandlerOptions<TContext>,
46
+ ) => void;
47
+
35
48
  /**
36
49
  * @internal
37
50
  */
@@ -45,6 +58,9 @@ export type RouterCaller<
45
58
  * e.g. wrapped in `React.cache` to avoid unnecessary computations
46
59
  */
47
60
  ctx: TRoot['ctx'] | (() => MaybePromise<TRoot['ctx']>),
61
+ options?: {
62
+ onError?: RouterCallerErrorHandler<TRoot['ctx']>;
63
+ },
48
64
  ) => DecorateRouterRecord<TRecord>;
49
65
 
50
66
  export interface Router<
@@ -191,21 +207,9 @@ export function createRouterFactory<TRoot extends AnyRootTypes>(
191
207
  return {
192
208
  ...record,
193
209
  _def,
194
- createCaller(ctx: TRoot['ctx']) {
195
- const proxy = createRecursiveProxy(({ path, args }) => {
196
- const fullPath = path.join('.');
197
- const procedure = _def.procedures[fullPath] as AnyProcedure;
198
-
199
- return procedure({
200
- path: fullPath,
201
- getRawInput: async () => args[0],
202
- ctx,
203
- type: procedure._def.type,
204
- });
205
- });
206
-
207
- return proxy as ReturnType<RouterCaller<any, any>>;
208
- },
210
+ createCaller: createCallerFactory<TRoot>()({
211
+ _def,
212
+ }),
209
213
  };
210
214
  }
211
215
 
@@ -221,7 +225,7 @@ function isProcedure(
221
225
  * @internal
222
226
  */
223
227
  export function callProcedure(
224
- opts: ProcedureCallOptions & {
228
+ opts: ProcedureCallOptions<unknown> & {
225
229
  procedures: RouterRecord;
226
230
  allowMethodOverride?: boolean;
227
231
  },
@@ -244,34 +248,44 @@ export function callProcedure(
244
248
 
245
249
  export function createCallerFactory<TRoot extends AnyRootTypes>() {
246
250
  return function createCallerInner<TRecord extends RouterRecord>(
247
- router: Router<TRoot, TRecord>,
251
+ router: Pick<Router<TRoot, TRecord>, '_def'>,
248
252
  ): RouterCaller<TRoot, TRecord> {
249
253
  const _def = router._def;
250
254
  type Context = TRoot['ctx'];
251
255
 
252
- return function createCaller(maybeContext) {
253
- const proxy = createRecursiveProxy(({ path, args }) => {
256
+ return function createCaller(
257
+ ctxOrCallback,
258
+ options?: {
259
+ onError?: RouterCallerErrorHandler<Context>;
260
+ },
261
+ ) {
262
+ const proxy = createRecursiveProxy(async ({ path, args }) => {
254
263
  const fullPath = path.join('.');
255
264
 
256
265
  const procedure = _def.procedures[fullPath] as AnyProcedure;
257
266
 
258
- const callProc = (ctx: Context) =>
259
- procedure({
267
+ let ctx: Context | undefined = undefined;
268
+ try {
269
+ ctx = isFunction(ctxOrCallback)
270
+ ? await Promise.resolve(ctxOrCallback())
271
+ : ctxOrCallback;
272
+
273
+ return await procedure({
260
274
  path: fullPath,
261
275
  getRawInput: async () => args[0],
262
276
  ctx,
263
277
  type: procedure._def.type,
264
278
  });
265
-
266
- if (typeof maybeContext === 'function') {
267
- const context = (maybeContext as () => MaybePromise<Context>)();
268
- if (context instanceof Promise) {
269
- return context.then(callProc);
270
- }
271
- return callProc(context);
279
+ } catch (cause) {
280
+ options?.onError?.({
281
+ ctx,
282
+ error: getTRPCErrorFromUnknown(cause),
283
+ input: args[0],
284
+ path: fullPath,
285
+ type: procedure._def.type,
286
+ });
287
+ throw cause;
272
288
  }
273
-
274
- return callProc(maybeContext);
275
289
  });
276
290
 
277
291
  return proxy as ReturnType<RouterCaller<any, any>>;
@@ -1,3 +1,5 @@
1
+ import { createFlatProxy } from './createProxy';
2
+
1
3
  /**
2
4
  * Ensures there are no duplicate keys when building a procedure.
3
5
  * @internal
@@ -27,6 +29,11 @@ export function isObject(value: unknown): value is Record<string, unknown> {
27
29
  return !!value && !Array.isArray(value) && typeof value === 'object';
28
30
  }
29
31
 
32
+ type AnyFn = (...args: any[]) => unknown;
33
+ export function isFunction(fn: unknown): fn is AnyFn {
34
+ return typeof fn === 'function';
35
+ }
36
+
30
37
  /**
31
38
  * Create an object without inheriting anything from `Object.prototype`
32
39
  * @internal
@@ -36,3 +43,16 @@ export function omitPrototype<TObj extends Record<string, unknown>>(
36
43
  ): TObj {
37
44
  return Object.assign(Object.create(null), obj);
38
45
  }
46
+
47
+ /**
48
+ * Prevents access to `$types` at runtime
49
+ * @internal
50
+ */
51
+ export const $typesProxy = createFlatProxy<any>((key) => {
52
+ if (key === 'prototype') {
53
+ return undefined;
54
+ }
55
+ throw new Error(
56
+ `Tried to access "$types.${key}" which is not available at runtime`,
57
+ );
58
+ });