@trpc/server 10.0.0-alpha.20 → 10.0.0-alpha.21

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 (89) hide show
  1. package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +3 -3
  2. package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +3 -3
  3. package/adapters/express/dist/trpc-server-adapters-express.esm.js +3 -3
  4. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +3 -3
  5. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +3 -3
  6. package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +3 -3
  7. package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +3 -3
  8. package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +3 -3
  9. package/adapters/next/dist/trpc-server-adapters-next.esm.js +3 -3
  10. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +3 -3
  11. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +3 -3
  12. package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +3 -3
  13. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +3 -3
  14. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +3 -3
  15. package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +3 -3
  16. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js +2 -2
  17. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js +2 -2
  18. package/adapters/ws/dist/trpc-server-adapters-ws.esm.js +2 -2
  19. package/dist/TRPCError.d.ts +3 -3
  20. package/dist/TRPCError.d.ts.map +1 -1
  21. package/dist/adapters/express.js +4 -5
  22. package/dist/adapters/express.mjs +4 -5
  23. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  24. package/dist/adapters/fastify/index.js +5 -6
  25. package/dist/adapters/fastify/index.mjs +4 -5
  26. package/dist/adapters/next.js +4 -5
  27. package/dist/adapters/next.mjs +4 -5
  28. package/dist/adapters/node-http/index.js +4 -5
  29. package/dist/adapters/node-http/index.mjs +4 -5
  30. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  31. package/dist/adapters/node-http/types.d.ts +3 -3
  32. package/dist/adapters/node-http/types.d.ts.map +1 -1
  33. package/dist/adapters/standalone.d.ts.map +1 -1
  34. package/dist/adapters/standalone.js +10 -8
  35. package/dist/adapters/standalone.mjs +10 -7
  36. package/dist/adapters/ws.d.ts +3 -3
  37. package/dist/adapters/ws.d.ts.map +1 -1
  38. package/dist/adapters/ws.js +3 -3
  39. package/dist/adapters/ws.mjs +3 -3
  40. package/dist/assertNotBrowser.d.ts.map +1 -1
  41. package/dist/{codes-7f46544e.js → codes-4e4d55a9.js} +2 -0
  42. package/dist/{codes-7a3d4f65.mjs → codes-6c13cf43.mjs} +2 -0
  43. package/dist/http/internals/getHTTPStatusCode.d.ts +1 -0
  44. package/dist/http/internals/getHTTPStatusCode.d.ts.map +1 -1
  45. package/dist/http/internals/types.d.ts +4 -3
  46. package/dist/http/internals/types.d.ts.map +1 -1
  47. package/dist/http/resolveHTTPResponse.d.ts +1 -1
  48. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  49. package/dist/index.js +13 -8
  50. package/dist/index.mjs +15 -10
  51. package/dist/internals/errors.d.ts +1 -0
  52. package/dist/internals/errors.d.ts.map +1 -1
  53. package/dist/internals/middlewares.d.ts +2 -1
  54. package/dist/internals/middlewares.d.ts.map +1 -1
  55. package/dist/internals/procedure.d.ts +16 -11
  56. package/dist/internals/procedure.d.ts.map +1 -1
  57. package/dist/{nodeHTTPRequestHandler-5fccb3bb.mjs → nodeHTTPRequestHandler-13c4bded.mjs} +2 -3
  58. package/dist/{nodeHTTPRequestHandler-7d71ac09.js → nodeHTTPRequestHandler-745e43e5.js} +3 -4
  59. package/dist/{resolveHTTPResponse-c3777a47.js → resolveHTTPResponse-b1167b6a.js} +6 -5
  60. package/dist/{resolveHTTPResponse-3292a84a.mjs → resolveHTTPResponse-de40fda7.mjs} +6 -5
  61. package/dist/router.d.ts +29 -25
  62. package/dist/router.d.ts.map +1 -1
  63. package/dist/rpc/codes.d.ts +2 -0
  64. package/dist/rpc/codes.d.ts.map +1 -1
  65. package/dist/rpc/index.js +1 -1
  66. package/dist/rpc/index.mjs +1 -1
  67. package/dist/{subscription-41611bc2.js → subscription-4b936ba3.js} +1 -1
  68. package/dist/{subscription-0d8a48ec.mjs → subscription-8a85f253.mjs} +1 -1
  69. package/dist/{transformTRPCResponse-ff424ff4.mjs → transformTRPCResponse-9fdeecca.mjs} +18 -6
  70. package/dist/{transformTRPCResponse-43a869de.js → transformTRPCResponse-a57d3226.js} +18 -5
  71. package/package.json +7 -3
  72. package/src/TRPCError.ts +2 -2
  73. package/src/adapters/fastify/fastifyRequestHandler.ts +0 -1
  74. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +0 -1
  75. package/src/adapters/node-http/types.ts +3 -3
  76. package/src/adapters/standalone.ts +10 -3
  77. package/src/adapters/ws.ts +5 -5
  78. package/src/assertNotBrowser.ts +1 -0
  79. package/src/http/internals/getHTTPStatusCode.ts +1 -0
  80. package/src/http/internals/types.ts +5 -6
  81. package/src/http/resolveHTTPResponse.ts +4 -4
  82. package/src/internals/errors.ts +20 -4
  83. package/src/internals/middlewares.ts +2 -1
  84. package/src/internals/procedure.ts +63 -16
  85. package/src/router.ts +89 -28
  86. package/src/rpc/codes.ts +1 -0
  87. package/ws/dist/trpc-server-ws.cjs.dev.js +2 -2
  88. package/ws/dist/trpc-server-ws.cjs.prod.js +2 -2
  89. package/ws/dist/trpc-server-ws.esm.js +2 -2
@@ -2,7 +2,7 @@
2
2
  import { TRPCError } from '../TRPCError';
3
3
  import { assertNotBrowser } from '../assertNotBrowser';
4
4
  import { ProcedureType } from '../router';
5
- import { getErrorFromUnknown } from './errors';
5
+ import { getCauseFromUnknown, getErrorFromUnknown } from './errors';
6
6
  import { MiddlewareFunction, middlewareMarker } from './middlewares';
7
7
  import { wrapCallSafe } from './wrapCallSafe';
8
8
 
@@ -44,11 +44,12 @@ export type ProcedureResolver<TContext, TInput, TOutput> = (opts: {
44
44
  type: ProcedureType;
45
45
  }) => Promise<TOutput> | TOutput;
46
46
 
47
- interface ProcedureOptions<TContext, TInput, TOutput, TParsedOutput> {
48
- middlewares: Array<MiddlewareFunction<any, any>>;
47
+ interface ProcedureOptions<TContext, TMeta, TInput, TOutput, TParsedOutput> {
48
+ middlewares: Array<MiddlewareFunction<any, any, any>>;
49
49
  resolver: ProcedureResolver<TContext, TInput, TOutput>;
50
50
  inputParser: ProcedureParser<TInput>;
51
51
  outputParser: ProcedureParser<TParsedOutput>;
52
+ meta: TMeta;
52
53
  }
53
54
 
54
55
  /**
@@ -100,21 +101,29 @@ function getParseFn<T>(procedureParser: ProcedureParser<T>): ParseFn<T> {
100
101
  export class Procedure<
101
102
  TInputContext,
102
103
  TContext,
104
+ TMeta,
103
105
  TInput,
104
106
  TParsedInput,
105
107
  TOutput,
106
108
  TParsedOutput,
107
109
  TFinalOutput = unknown extends TParsedOutput ? TOutput : TParsedOutput,
108
110
  > {
109
- private middlewares: Readonly<Array<MiddlewareFunction<any, any>>>;
111
+ private middlewares: Readonly<Array<MiddlewareFunction<any, any, any>>>;
110
112
  private resolver: ProcedureResolver<TContext, TParsedInput, TOutput>;
111
113
  private readonly inputParser: ProcedureParser<TParsedInput>;
112
114
  private parseInputFn: ParseFn<TParsedInput>;
113
115
  private readonly outputParser: ProcedureParser<TFinalOutput>;
114
116
  private parseOutputFn: ParseFn<TFinalOutput>;
117
+ public readonly meta: TMeta;
115
118
 
116
119
  constructor(
117
- opts: ProcedureOptions<TContext, TParsedInput, TOutput, TFinalOutput>,
120
+ opts: ProcedureOptions<
121
+ TContext,
122
+ TMeta,
123
+ TParsedInput,
124
+ TOutput,
125
+ TFinalOutput
126
+ >,
118
127
  ) {
119
128
  this.middlewares = opts.middlewares;
120
129
  this.resolver = opts.resolver;
@@ -122,6 +131,7 @@ export class Procedure<
122
131
  this.parseInputFn = getParseFn(this.inputParser);
123
132
  this.outputParser = opts.outputParser;
124
133
  this.parseOutputFn = getParseFn(this.outputParser);
134
+ this.meta = opts.meta;
125
135
  }
126
136
 
127
137
  private async parseInput(rawInput: unknown): Promise<TParsedInput> {
@@ -130,7 +140,7 @@ export class Procedure<
130
140
  } catch (cause) {
131
141
  throw new TRPCError({
132
142
  code: 'BAD_REQUEST',
133
- cause,
143
+ cause: getCauseFromUnknown(cause),
134
144
  });
135
145
  }
136
146
  }
@@ -141,7 +151,7 @@ export class Procedure<
141
151
  } catch (cause) {
142
152
  throw new TRPCError({
143
153
  code: 'INTERNAL_SERVER_ERROR',
144
- cause,
154
+ cause: getCauseFromUnknown(cause),
145
155
  message: 'Output validation failed',
146
156
  });
147
157
  }
@@ -158,7 +168,11 @@ export class Procedure<
158
168
  const middlewaresWithResolver = this.middlewares.concat([
159
169
  async ({ ctx }: { ctx: TContext }) => {
160
170
  const input = await this.parseInput(opts.rawInput);
161
- const rawOutput = await this.resolver({ ...opts, ctx, input });
171
+ const rawOutput = await this.resolver({
172
+ ...opts,
173
+ ctx,
174
+ input,
175
+ });
162
176
  const data = await this.parseOutput(rawOutput);
163
177
  return {
164
178
  marker: middlewareMarker,
@@ -178,6 +192,7 @@ export class Procedure<
178
192
  type: opts.type,
179
193
  path: opts.path,
180
194
  rawInput: opts.rawInput,
195
+ meta: this.meta,
181
196
  next: nextFns[index + 1],
182
197
  }),
183
198
  );
@@ -213,14 +228,21 @@ export class Procedure<
213
228
  * @param middlewares
214
229
  */
215
230
  public inheritMiddlewares(
216
- middlewares: MiddlewareFunction<TInputContext, TContext>[],
231
+ middlewares: MiddlewareFunction<TInputContext, TContext, TMeta>[],
217
232
  ): this {
218
233
  const Constructor: {
219
234
  new (
220
- opts: ProcedureOptions<TContext, TParsedInput, TOutput, TFinalOutput>,
235
+ opts: ProcedureOptions<
236
+ TContext,
237
+ TMeta,
238
+ TParsedInput,
239
+ TOutput,
240
+ TFinalOutput
241
+ >,
221
242
  ): Procedure<
222
243
  TInputContext,
223
244
  TContext,
245
+ TMeta,
224
246
  TInput,
225
247
  TParsedInput,
226
248
  TOutput,
@@ -233,20 +255,23 @@ export class Procedure<
233
255
  resolver: this.resolver,
234
256
  inputParser: this.inputParser,
235
257
  outputParser: this.outputParser,
258
+ meta: this.meta,
236
259
  });
237
260
 
238
261
  return instance as any;
239
262
  }
240
263
  }
241
264
 
242
- export type CreateProcedureWithInput<TContext, TInput, TOutput> = {
265
+ export type CreateProcedureWithInput<TContext, TMeta, TInput, TOutput> = {
243
266
  input: ProcedureParser<TInput>;
244
267
  output?: ProcedureParser<TOutput>;
268
+ meta?: TMeta;
245
269
  resolve: ProcedureResolver<TContext, TInput, TOutput>;
246
270
  };
247
271
 
248
272
  export type CreateProcedureWithInputOutputParser<
249
273
  TContext,
274
+ TMeta,
250
275
  TInput,
251
276
  TParsedInput,
252
277
  TOutput,
@@ -254,18 +279,26 @@ export type CreateProcedureWithInputOutputParser<
254
279
  > = {
255
280
  input: ProcedureParserWithInputOutput<TInput, TParsedInput>;
256
281
  output?: ProcedureParserWithInputOutput<TOutput, TParsedOutput>;
282
+ meta?: TMeta;
257
283
  resolve: ProcedureResolver<TContext, TParsedInput, TOutput>;
258
284
  };
259
285
 
260
- export type CreateProcedureWithoutInput<TContext, TOutput, TParsedOutput> = {
286
+ export type CreateProcedureWithoutInput<
287
+ TContext,
288
+ TMeta,
289
+ TOutput,
290
+ TParsedOutput,
291
+ > = {
261
292
  output?:
262
293
  | ProcedureParserWithInputOutput<TOutput, TParsedOutput>
263
294
  | ProcedureParser<TOutput>;
295
+ meta?: TMeta;
264
296
  resolve: ProcedureResolver<TContext, undefined, TOutput>;
265
297
  };
266
298
 
267
299
  export type CreateProcedureOptions<
268
300
  TContext,
301
+ TMeta = undefined,
269
302
  TInput = undefined,
270
303
  TParsedInput = undefined,
271
304
  TOutput = undefined,
@@ -273,16 +306,18 @@ export type CreateProcedureOptions<
273
306
  > =
274
307
  | CreateProcedureWithInputOutputParser<
275
308
  TContext,
309
+ TMeta,
276
310
  TInput,
277
311
  TParsedInput,
278
312
  TOutput,
279
313
  TParsedOutput
280
314
  >
281
- | CreateProcedureWithInput<TContext, TInput, TOutput>
282
- | CreateProcedureWithoutInput<TContext, TOutput, TParsedOutput>;
315
+ | CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>
316
+ | CreateProcedureWithoutInput<TContext, TMeta, TOutput, TParsedOutput>;
283
317
 
284
318
  export function createProcedure<
285
319
  TContext,
320
+ TMeta,
286
321
  TInput,
287
322
  TParsedInput,
288
323
  TOutput,
@@ -290,12 +325,21 @@ export function createProcedure<
290
325
  >(
291
326
  opts: CreateProcedureOptions<
292
327
  TContext,
328
+ TMeta,
293
329
  TInput,
294
330
  TParsedInput,
295
331
  TOutput,
296
332
  TParsedOutput
297
333
  >,
298
- ): Procedure<unknown, TContext, TInput, TParsedInput, TOutput, TParsedOutput> {
334
+ ): Procedure<
335
+ unknown,
336
+ TContext,
337
+ TMeta,
338
+ TInput,
339
+ TParsedInput,
340
+ TOutput,
341
+ TParsedOutput
342
+ > {
299
343
  const inputParser =
300
344
  'input' in opts
301
345
  ? opts.input
@@ -319,14 +363,16 @@ export function createProcedure<
319
363
  resolver: opts.resolve as any,
320
364
  middlewares: [],
321
365
  outputParser: outputParser as any,
366
+ meta: opts.meta as any,
322
367
  });
323
368
  }
324
369
 
325
370
  export type inferProcedureFromOptions<
326
371
  TInputContext,
327
- TOptions extends CreateProcedureOptions<any, any, any, any, any>,
372
+ TOptions extends CreateProcedureOptions<any, any, any, any, any, any>,
328
373
  > = TOptions extends CreateProcedureOptions<
329
374
  infer TContext,
375
+ infer TMeta,
330
376
  infer TInput,
331
377
  infer TParsedInput,
332
378
  infer TOutput,
@@ -335,6 +381,7 @@ export type inferProcedureFromOptions<
335
381
  ? Procedure<
336
382
  TInputContext,
337
383
  TContext,
384
+ TMeta,
338
385
  unknown extends TInput ? undefined : TInput,
339
386
  unknown extends TParsedInput ? undefined : TParsedInput,
340
387
  TOutput,
package/src/router.ts CHANGED
@@ -38,6 +38,7 @@ export type ProcedureType = 'query' | 'mutation' | 'subscription';
38
38
  export type ProcedureRecord<
39
39
  TInputContext = any,
40
40
  TContext = any,
41
+ TMeta = any,
41
42
  TInput = any,
42
43
  TParsedInput = any,
43
44
  TOutput = any,
@@ -47,6 +48,7 @@ export type ProcedureRecord<
47
48
  Procedure<
48
49
  TInputContext,
49
50
  TContext,
51
+ TMeta,
50
52
  TInput,
51
53
  TParsedInput,
52
54
  TOutput,
@@ -58,8 +60,8 @@ export type ProcedureRecord<
58
60
  * @public
59
61
  */
60
62
  export type inferProcedureInput<
61
- TProcedure extends Procedure<any, any, any, any, any, any>,
62
- > = TProcedure extends Procedure<any, any, infer Input, any, any, any>
63
+ TProcedure extends Procedure<any, any, any, any, any, any, any>,
64
+ > = TProcedure extends Procedure<any, any, any, infer Input, any, any, any>
63
65
  ? undefined extends Input
64
66
  ? Input | null | void // void is necessary to allow procedures with nullish input to be called without an input
65
67
  : Input
@@ -75,7 +77,7 @@ export type inferAsyncReturnType<TFunction extends (...args: any) => any> =
75
77
  * @public
76
78
  */
77
79
  export type inferProcedureOutput<
78
- TProcedure extends Procedure<any, any, any, any, any, any>,
80
+ TProcedure extends Procedure<any, any, any, any, any, any, any>,
79
81
  > = inferAsyncReturnType<TProcedure['call']>;
80
82
 
81
83
  /**
@@ -103,8 +105,8 @@ function getDataTransformer(
103
105
  * @internal
104
106
  */
105
107
  export type inferHandlerInput<
106
- TProcedure extends Procedure<any, any, any, any, any, any>,
107
- > = TProcedure extends Procedure<any, any, infer TInput, any, any, any>
108
+ TProcedure extends Procedure<any, any, any, any, any, any, any>,
109
+ > = TProcedure extends Procedure<any, any, any, infer TInput, any, any, any>
108
110
  ? undefined extends TInput // ? is input optional
109
111
  ? unknown extends TInput // ? is input unset
110
112
  ? [(null | undefined)?] // -> there is no input
@@ -127,6 +129,21 @@ export type inferRouterContext<TRouter extends AnyRouter> = Parameters<
127
129
  TRouter['createCaller']
128
130
  >[0];
129
131
 
132
+ /**
133
+ * @internal
134
+ */
135
+ export type inferRouterMeta<TRouter extends AnyRouter> = TRouter extends Router<
136
+ any,
137
+ any,
138
+ infer TMeta,
139
+ any,
140
+ any,
141
+ any,
142
+ any
143
+ >
144
+ ? TMeta
145
+ : {};
146
+
130
147
  /**
131
148
  * @public
132
149
  */
@@ -136,6 +153,7 @@ export type AnyRouter<TContext = any> = Router<
136
153
  any,
137
154
  any,
138
155
  any,
156
+ any,
139
157
  any
140
158
  >;
141
159
 
@@ -214,12 +232,13 @@ const defaultTransformer: CombinedDataTransformer = {
214
232
  };
215
233
 
216
234
  type SwapProcedureContext<
217
- TProcedure extends Procedure<any, any, any, any, any, any>,
235
+ TProcedure extends Procedure<any, any, any, any, any, any, any>,
218
236
  TNewContext,
219
237
  > = TProcedure extends Procedure<
220
238
  infer TInputContext,
221
239
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
222
240
  infer _TOldContext,
241
+ infer TMeta,
223
242
  infer TInput,
224
243
  infer TParsedInput,
225
244
  infer TOutput,
@@ -228,6 +247,7 @@ type SwapProcedureContext<
228
247
  ? Procedure<
229
248
  TInputContext,
230
249
  TNewContext,
250
+ TMeta,
231
251
  TInput,
232
252
  TParsedInput,
233
253
  TOutput,
@@ -248,13 +268,23 @@ type SwapContext<
248
268
  export class Router<
249
269
  TInputContext,
250
270
  TContext,
251
- TQueries extends ProcedureRecord<TInputContext, TContext, any, any, any, any>,
271
+ TMeta extends Record<string, any>,
272
+ TQueries extends ProcedureRecord<
273
+ TInputContext,
274
+ TContext,
275
+ any,
276
+ any,
277
+ any,
278
+ any,
279
+ any
280
+ >,
252
281
  TMutations extends ProcedureRecord<
253
282
  TInputContext,
254
283
  TContext,
255
284
  any,
256
285
  any,
257
286
  any,
287
+ any,
258
288
  any
259
289
  >,
260
290
  TSubscriptions extends ProcedureRecord<
@@ -263,6 +293,7 @@ export class Router<
263
293
  unknown,
264
294
  unknown,
265
295
  Subscription<unknown>,
296
+ unknown,
266
297
  unknown
267
298
  >,
268
299
  TErrorShape extends TRPCErrorShape<number>,
@@ -271,7 +302,7 @@ export class Router<
271
302
  queries: TQueries;
272
303
  mutations: TMutations;
273
304
  subscriptions: TSubscriptions;
274
- middlewares: MiddlewareFunction<TInputContext, TContext>[];
305
+ middlewares: MiddlewareFunction<TInputContext, TContext, TMeta>[];
275
306
  errorFormatter: ErrorFormatter<TContext, TErrorShape>;
276
307
  transformer: CombinedDataTransformer;
277
308
  };
@@ -280,7 +311,7 @@ export class Router<
280
311
  queries?: TQueries;
281
312
  mutations?: TMutations;
282
313
  subscriptions?: TSubscriptions;
283
- middlewares?: MiddlewareFunction<TInputContext, TContext>[];
314
+ middlewares?: MiddlewareFunction<TInputContext, TContext, TMeta>[];
284
315
  errorFormatter?: ErrorFormatter<TContext, TErrorShape>;
285
316
  transformer?: CombinedDataTransformer;
286
317
  }) {
@@ -315,6 +346,7 @@ export class Router<
315
346
  path: TPath,
316
347
  procedure: CreateProcedureWithInputOutputParser<
317
348
  TContext,
349
+ TMeta,
318
350
  TInput,
319
351
  TParsedInput,
320
352
  TOutput,
@@ -323,6 +355,7 @@ export class Router<
323
355
  ): Router<
324
356
  TInputContext,
325
357
  TContext,
358
+ TMeta,
326
359
  TQueries &
327
360
  Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
328
361
  TMutations,
@@ -332,10 +365,11 @@ export class Router<
332
365
 
333
366
  public query<TPath extends string, TInput, TOutput>(
334
367
  path: TPath,
335
- procedure: CreateProcedureWithInput<TContext, TInput, TOutput>,
368
+ procedure: CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
336
369
  ): Router<
337
370
  TInputContext,
338
371
  TContext,
372
+ TMeta,
339
373
  TQueries &
340
374
  Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
341
375
  TMutations,
@@ -345,10 +379,16 @@ export class Router<
345
379
 
346
380
  public query<TPath extends string, TOutput, TParsedOutput>(
347
381
  path: TPath,
348
- procedure: CreateProcedureWithoutInput<TContext, TOutput, TParsedOutput>,
382
+ procedure: CreateProcedureWithoutInput<
383
+ TContext,
384
+ TMeta,
385
+ TOutput,
386
+ TParsedOutput
387
+ >,
349
388
  ): Router<
350
389
  TInputContext,
351
390
  TContext,
391
+ TMeta,
352
392
  TQueries &
353
393
  Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
354
394
  TMutations,
@@ -358,9 +398,9 @@ export class Router<
358
398
 
359
399
  query(
360
400
  path: string,
361
- procedure: CreateProcedureOptions<TContext, any, any, any, any>,
401
+ procedure: CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
362
402
  ) {
363
- const router = new Router<TContext, TContext, any, {}, {}, any>({
403
+ const router = new Router<TContext, TContext, TMeta, any, {}, {}, any>({
364
404
  queries: safeObject({
365
405
  [path]: createProcedure(procedure),
366
406
  }),
@@ -379,6 +419,7 @@ export class Router<
379
419
  path: TPath,
380
420
  procedure: CreateProcedureWithInputOutputParser<
381
421
  TContext,
422
+ TMeta,
382
423
  TInput,
383
424
  TParsedInput,
384
425
  TOutput,
@@ -387,6 +428,7 @@ export class Router<
387
428
  ): Router<
388
429
  TInputContext,
389
430
  TContext,
431
+ TMeta,
390
432
  TQueries,
391
433
  TMutations &
392
434
  Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
@@ -396,10 +438,11 @@ export class Router<
396
438
 
397
439
  public mutation<TPath extends string, TInput, TOutput>(
398
440
  path: TPath,
399
- procedure: CreateProcedureWithInput<TContext, TInput, TOutput>,
441
+ procedure: CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
400
442
  ): Router<
401
443
  TInputContext,
402
444
  TContext,
445
+ TMeta,
403
446
  TQueries,
404
447
  TMutations &
405
448
  Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
@@ -409,10 +452,16 @@ export class Router<
409
452
 
410
453
  public mutation<TPath extends string, TOutput, TParsedOutput>(
411
454
  path: TPath,
412
- procedure: CreateProcedureWithoutInput<TContext, TOutput, TParsedOutput>,
455
+ procedure: CreateProcedureWithoutInput<
456
+ TContext,
457
+ TMeta,
458
+ TOutput,
459
+ TParsedOutput
460
+ >,
413
461
  ): Router<
414
462
  TInputContext,
415
463
  TContext,
464
+ TMeta,
416
465
  TQueries,
417
466
  TMutations &
418
467
  Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
@@ -422,9 +471,9 @@ export class Router<
422
471
 
423
472
  public mutation(
424
473
  path: string,
425
- procedure: CreateProcedureOptions<TContext, any, any, any, any>,
474
+ procedure: CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
426
475
  ) {
427
- const router = new Router<TContext, TContext, {}, any, {}, any>({
476
+ const router = new Router<TContext, TContext, TMeta, {}, any, {}, any>({
428
477
  mutations: safeObject({
429
478
  [path]: createProcedure(procedure),
430
479
  }),
@@ -446,6 +495,7 @@ export class Router<
446
495
  procedure: Omit<
447
496
  CreateProcedureWithInputOutputParser<
448
497
  TContext,
498
+ TMeta,
449
499
  TInput,
450
500
  TParsedInput,
451
501
  TOutput,
@@ -456,6 +506,7 @@ export class Router<
456
506
  ): Router<
457
507
  TInputContext,
458
508
  TContext,
509
+ TMeta,
459
510
  TQueries,
460
511
  TMutations,
461
512
  TSubscriptions &
@@ -473,12 +524,13 @@ export class Router<
473
524
  >(
474
525
  path: TPath,
475
526
  procedure: Omit<
476
- CreateProcedureWithInput<TContext, TInput, TOutput>,
527
+ CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>,
477
528
  'output'
478
529
  >,
479
530
  ): Router<
480
531
  TInputContext,
481
532
  TContext,
533
+ TMeta,
482
534
  TQueries,
483
535
  TMutations,
484
536
  TSubscriptions &
@@ -495,12 +547,13 @@ export class Router<
495
547
  >(
496
548
  path: TPath,
497
549
  procedure: Omit<
498
- CreateProcedureWithoutInput<TContext, TOutput, unknown>,
550
+ CreateProcedureWithoutInput<TContext, TMeta, TOutput, unknown>,
499
551
  'output'
500
552
  >,
501
553
  ): Router<
502
554
  TInputContext,
503
555
  TContext,
556
+ TMeta,
504
557
  TQueries,
505
558
  TMutations,
506
559
  TSubscriptions &
@@ -511,11 +564,11 @@ export class Router<
511
564
  public subscription(
512
565
  path: string,
513
566
  procedure: Omit<
514
- CreateProcedureOptions<TContext, any, any, any, any>,
567
+ CreateProcedureOptions<TContext, TMeta, any, any, any, any>,
515
568
  'output'
516
569
  >,
517
570
  ) {
518
- const router = new Router<TContext, TContext, {}, {}, any, any>({
571
+ const router = new Router<TContext, TContext, TMeta, {}, {}, any, any>({
519
572
  subscriptions: safeObject({
520
573
  [path]: createProcedure(procedure),
521
574
  }),
@@ -528,11 +581,14 @@ export class Router<
528
581
  * Merge router with other router
529
582
  * @param router
530
583
  */
531
- public merge<TChildRouter extends Router<TContext, any, any, any, any, any>>(
584
+ public merge<
585
+ TChildRouter extends Router<TContext, any, TMeta, any, any, any, any>,
586
+ >(
532
587
  router: TChildRouter,
533
588
  ): Router<
534
589
  TInputContext,
535
590
  inferRouterContext<TChildRouter>,
591
+ TMeta,
536
592
  TQueries & TChildRouter['_def']['queries'],
537
593
  TMutations & TChildRouter['_def']['mutations'],
538
594
  TSubscriptions & TChildRouter['_def']['subscriptions'],
@@ -546,13 +602,14 @@ export class Router<
546
602
  */
547
603
  public merge<
548
604
  TPath extends string,
549
- TChildRouter extends Router<TContext, any, any, any, any, any>,
605
+ TChildRouter extends Router<TContext, any, TMeta, any, any, any, any>,
550
606
  >(
551
607
  prefix: TPath,
552
608
  router: TChildRouter,
553
609
  ): Router<
554
610
  TInputContext,
555
611
  inferRouterContext<TChildRouter>,
612
+ TMeta,
556
613
  TQueries & Prefixer<TChildRouter['_def']['queries'], `${TPath}`>,
557
614
  TMutations & Prefixer<TChildRouter['_def']['mutations'], `${TPath}`>,
558
615
  TSubscriptions &
@@ -605,7 +662,7 @@ export class Router<
605
662
  return Router.prefixProcedures(newDefs, prefix);
606
663
  };
607
664
 
608
- return new Router<TInputContext, any, any, any, any, TErrorShape>({
665
+ return new Router<TInputContext, any, TMeta, any, any, any, TErrorShape>({
609
666
  ...this._def,
610
667
  queries: safeObject(
611
668
  this._def.queries,
@@ -632,7 +689,7 @@ export class Router<
632
689
  const defTarget = PROCEDURE_DEFINITION_MAP[type];
633
690
  const defs = this._def[defTarget];
634
691
  const procedure = defs[path] as
635
- | Procedure<TInputContext, TContext, any, any, any, any>
692
+ | Procedure<TInputContext, TContext, TMeta, any, any, any, any>
636
693
  | undefined;
637
694
 
638
695
  if (!procedure) {
@@ -683,10 +740,11 @@ export class Router<
683
740
  * @link https://trpc.io/docs/middlewares
684
741
  */
685
742
  public middleware<TNewContext>(
686
- middleware: MiddlewareFunction<TContext, TNewContext>,
743
+ middleware: MiddlewareFunction<TContext, TNewContext, TMeta>,
687
744
  ): Router<
688
745
  TInputContext,
689
746
  TNewContext,
747
+ TMeta,
690
748
  SwapContext<TQueries, TNewContext>,
691
749
  SwapContext<TMutations, TNewContext>,
692
750
  SwapContext<TSubscriptions, TNewContext>,
@@ -713,6 +771,7 @@ export class Router<
713
771
  return new Router<
714
772
  TInputContext,
715
773
  TContext,
774
+ TMeta,
716
775
  TQueries,
717
776
  TMutations,
718
777
  TSubscriptions,
@@ -767,6 +826,7 @@ export class Router<
767
826
  return new Router<
768
827
  TInputContext,
769
828
  TContext,
829
+ TMeta,
770
830
  TQueries,
771
831
  TMutations,
772
832
  TSubscriptions,
@@ -786,6 +846,7 @@ export class Router<
786
846
  public flat(): Router<
787
847
  TInputContext,
788
848
  TContext,
849
+ TMeta,
789
850
  flatten<{}, TQueries>,
790
851
  flatten<{}, TMutations>,
791
852
  flatten<{}, TSubscriptions>,
@@ -795,6 +856,6 @@ export class Router<
795
856
  }
796
857
  }
797
858
 
798
- export function router<TContext>() {
799
- return new Router<TContext, TContext, {}, {}, {}, DefaultErrorShape>();
859
+ export function router<TContext, TMeta extends Record<string, any> = {}>() {
860
+ return new Router<TContext, TContext, TMeta, {}, {}, {}, DefaultErrorShape>();
800
861
  }
package/src/rpc/codes.ts CHANGED
@@ -28,6 +28,7 @@ export const TRPC_ERROR_CODES_BY_KEY = {
28
28
  NOT_FOUND: -32004, // 404
29
29
  METHOD_NOT_SUPPORTED: -32005, // 405
30
30
  TIMEOUT: -32008, // 408
31
+ CONFLICT: -32009, // 409
31
32
  PRECONDITION_FAILED: -32012, // 412
32
33
  PAYLOAD_TOO_LARGE: -32013, // 413
33
34
  CLIENT_CLOSED_REQUEST: -32099, // 499
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var adapters_ws_dist_trpcServerAdaptersWs = require('../../adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js');
6
- require('../../dist/transformTRPCResponse-8248515e.cjs.dev.js');
7
- require('../../dist/subscription-9e007280.cjs.dev.js');
6
+ require('../../dist/transformTRPCResponse-c654ea92.cjs.dev.js');
7
+ require('../../dist/subscription-7de48383.cjs.dev.js');
8
8
  require('events');
9
9
 
10
10
  /**
@@ -3,8 +3,8 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var adapters_ws_dist_trpcServerAdaptersWs = require('../../adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js');
6
- require('../../dist/transformTRPCResponse-dcbf66c5.cjs.prod.js');
7
- require('../../dist/subscription-12fb50be.cjs.prod.js');
6
+ require('../../dist/transformTRPCResponse-1e125b28.cjs.prod.js');
7
+ require('../../dist/subscription-f03bec71.cjs.prod.js');
8
8
  require('events');
9
9
 
10
10
  /**
@@ -1,6 +1,6 @@
1
1
  import { applyWSSHandler as applyWSSHandler$1 } from '../../adapters/ws/dist/trpc-server-adapters-ws.esm.js';
2
- import '../../dist/transformTRPCResponse-f73dc4a2.esm.js';
3
- import '../../dist/subscription-a81120d6.esm.js';
2
+ import '../../dist/transformTRPCResponse-45c225c3.esm.js';
3
+ import '../../dist/subscription-2ea469a3.esm.js';
4
4
  import 'events';
5
5
 
6
6
  /**