@trpc/server 10.32.0 → 10.33.1

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 (145) hide show
  1. package/dist/{TRPCError-2b10c8d2.mjs → TRPCError-6a1653a4.mjs} +28 -20
  2. package/dist/{TRPCError-7c3909ea.js → TRPCError-c3de85e9.js} +28 -21
  3. package/dist/{TRPCError-16b2e52d.js → TRPCError-d07099d1.js} +27 -20
  4. package/dist/adapters/aws-lambda/index.js +7 -7
  5. package/dist/adapters/aws-lambda/index.mjs +7 -7
  6. package/dist/adapters/express.js +5 -5
  7. package/dist/adapters/express.mjs +5 -5
  8. package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
  9. package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  10. package/dist/adapters/fastify/index.js +4 -4
  11. package/dist/adapters/fastify/index.mjs +4 -4
  12. package/dist/adapters/fetch/fetchRequestHandler.d.ts +2 -2
  13. package/dist/adapters/fetch/fetchRequestHandler.d.ts.map +1 -1
  14. package/dist/adapters/fetch/index.js +4 -4
  15. package/dist/adapters/fetch/index.mjs +4 -4
  16. package/dist/adapters/fetch/types.d.ts +1 -1
  17. package/dist/adapters/fetch/types.d.ts.map +1 -1
  18. package/dist/adapters/next.js +5 -5
  19. package/dist/adapters/next.mjs +5 -5
  20. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts +3 -3
  21. package/dist/adapters/node-http/content-type/form-data/fileUploadHandler.d.ts.map +1 -1
  22. package/dist/adapters/node-http/content-type/form-data/index.js +18 -13
  23. package/dist/adapters/node-http/content-type/form-data/index.mjs +18 -13
  24. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts +1 -1
  25. package/dist/adapters/node-http/content-type/form-data/memoryUploadHandler.d.ts.map +1 -1
  26. package/dist/adapters/node-http/content-type/form-data/streamSlice.d.ts.map +1 -1
  27. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts +2 -2
  28. package/dist/adapters/node-http/content-type/form-data/uploadHandler.d.ts.map +1 -1
  29. package/dist/adapters/node-http/content-type/json/index.js +2 -2
  30. package/dist/adapters/node-http/content-type/json/index.mjs +2 -2
  31. package/dist/adapters/node-http/index.js +5 -5
  32. package/dist/adapters/node-http/index.mjs +5 -5
  33. package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  34. package/dist/adapters/node-http/types.d.ts +4 -6
  35. package/dist/adapters/node-http/types.d.ts.map +1 -1
  36. package/dist/adapters/standalone.js +5 -5
  37. package/dist/adapters/standalone.mjs +5 -5
  38. package/dist/adapters/ws.d.ts +2 -2
  39. package/dist/adapters/ws.d.ts.map +1 -1
  40. package/dist/adapters/ws.js +4 -3
  41. package/dist/adapters/ws.mjs +4 -3
  42. package/dist/{config-65c5b6d1.mjs → config-7eddc5bb.mjs} +1 -1
  43. package/dist/{config-f3ce0144.js → config-943bca43.js} +1 -1
  44. package/dist/{config-54786004.js → config-94ba862d.js} +1 -1
  45. package/dist/{contentType-acc3be52.mjs → contentType-53e30af8.mjs} +3 -3
  46. package/dist/{contentType-8356d528.js → contentType-5b601838.js} +3 -3
  47. package/dist/{contentType-7354963c.js → contentType-b87cced2.js} +3 -3
  48. package/dist/core/internals/getParseFn.d.ts +1 -1
  49. package/dist/core/internals/getParseFn.d.ts.map +1 -1
  50. package/dist/core/internals/mergeRouters.d.ts +5 -5
  51. package/dist/core/internals/mergeRouters.d.ts.map +1 -1
  52. package/dist/core/middleware.d.ts +1 -1
  53. package/dist/core/middleware.d.ts.map +1 -1
  54. package/dist/core/parser.d.ts +3 -3
  55. package/dist/core/parser.d.ts.map +1 -1
  56. package/dist/core/procedure.d.ts +1 -1
  57. package/dist/core/procedure.d.ts.map +1 -1
  58. package/dist/core/router.d.ts +4 -4
  59. package/dist/core/router.d.ts.map +1 -1
  60. package/dist/core/types.d.ts +1 -1
  61. package/dist/core/types.d.ts.map +1 -1
  62. package/dist/deprecated/internals/middlewares.d.ts +1 -1
  63. package/dist/deprecated/internals/middlewares.d.ts.map +1 -1
  64. package/dist/deprecated/internals/procedure.d.ts +5 -5
  65. package/dist/deprecated/internals/procedure.d.ts.map +1 -1
  66. package/dist/deprecated/router.d.ts +17 -17
  67. package/dist/deprecated/router.d.ts.map +1 -1
  68. package/dist/error/TRPCError.d.ts.map +1 -1
  69. package/dist/error/formatter.d.ts +1 -1
  70. package/dist/error/formatter.d.ts.map +1 -1
  71. package/dist/error/utils.d.ts +4 -1
  72. package/dist/error/utils.d.ts.map +1 -1
  73. package/dist/http/contentType.d.ts.map +1 -1
  74. package/dist/http/index.js +4 -4
  75. package/dist/http/index.mjs +4 -4
  76. package/dist/http/internals/types.d.ts +1 -1
  77. package/dist/http/internals/types.d.ts.map +1 -1
  78. package/dist/http/resolveHTTPResponse.d.ts +1 -1
  79. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  80. package/dist/index.js +9 -9
  81. package/dist/index.mjs +11 -11
  82. package/dist/internals/types.d.ts +1 -1
  83. package/dist/internals/types.d.ts.map +1 -1
  84. package/dist/{nodeHTTPRequestHandler-51d58a23.js → nodeHTTPRequestHandler-46ec104c.js} +2 -1
  85. package/dist/{nodeHTTPRequestHandler-071d36b5.js → nodeHTTPRequestHandler-61ea01f2.js} +2 -1
  86. package/dist/{nodeHTTPRequestHandler-bb12529f.mjs → nodeHTTPRequestHandler-dbfdf9a5.mjs} +2 -1
  87. package/dist/observable/internals/pipe.d.ts +1 -1
  88. package/dist/observable/internals/pipe.d.ts.map +1 -1
  89. package/dist/{resolveHTTPResponse-1f03fdfd.js → resolveHTTPResponse-199d522a.js} +4 -4
  90. package/dist/{resolveHTTPResponse-dd3677b3.mjs → resolveHTTPResponse-25592372.mjs} +4 -4
  91. package/dist/{resolveHTTPResponse-e0047ea2.js → resolveHTTPResponse-33a8a5fc.js} +4 -4
  92. package/dist/rpc/envelopes.d.ts +11 -11
  93. package/dist/rpc/envelopes.d.ts.map +1 -1
  94. package/dist/shared/createProxy/index.d.ts +1 -1
  95. package/dist/shared/getErrorShape.d.ts +1 -1
  96. package/dist/shared/getErrorShape.d.ts.map +1 -1
  97. package/dist/shared/internal/serialize.d.ts +4 -4
  98. package/dist/shared/internal/serialize.d.ts.map +1 -1
  99. package/dist/shared/transformTRPCResponse.d.ts +1 -1
  100. package/dist/subscription.d.ts +1 -1
  101. package/dist/subscription.d.ts.map +1 -1
  102. package/dist/subscription.js +4 -2
  103. package/dist/subscription.mjs +4 -2
  104. package/dist/transformer.d.ts +2 -2
  105. package/dist/transformer.d.ts.map +1 -1
  106. package/dist/types.d.ts +5 -5
  107. package/dist/types.d.ts.map +1 -1
  108. package/package.json +4 -4
  109. package/src/adapters/aws-lambda/utils.ts +3 -3
  110. package/src/adapters/fastify/fastifyRequestHandler.ts +4 -4
  111. package/src/adapters/fetch/fetchRequestHandler.ts +7 -6
  112. package/src/adapters/fetch/types.ts +1 -1
  113. package/src/adapters/node-http/content-type/form-data/fileUploadHandler.ts +13 -11
  114. package/src/adapters/node-http/content-type/form-data/index.ts +2 -2
  115. package/src/adapters/node-http/content-type/form-data/memoryUploadHandler.ts +1 -1
  116. package/src/adapters/node-http/content-type/form-data/streamSlice.ts +5 -3
  117. package/src/adapters/node-http/content-type/form-data/uploadHandler.ts +2 -2
  118. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +1 -0
  119. package/src/adapters/node-http/types.ts +29 -29
  120. package/src/adapters/ws.ts +8 -7
  121. package/src/core/internals/getParseFn.ts +1 -1
  122. package/src/core/internals/mergeRouters.ts +6 -6
  123. package/src/core/middleware.ts +4 -5
  124. package/src/core/parser.ts +5 -5
  125. package/src/core/procedure.ts +1 -1
  126. package/src/core/router.ts +4 -4
  127. package/src/core/types.ts +1 -1
  128. package/src/deprecated/internals/middlewares.ts +2 -2
  129. package/src/deprecated/internals/procedure.ts +25 -28
  130. package/src/deprecated/router.ts +35 -35
  131. package/src/error/TRPCError.ts +35 -4
  132. package/src/error/formatter.ts +1 -1
  133. package/src/error/utils.ts +1 -9
  134. package/src/http/contentType.ts +2 -3
  135. package/src/http/internals/types.ts +1 -1
  136. package/src/http/resolveHTTPResponse.ts +6 -6
  137. package/src/internals/types.ts +1 -1
  138. package/src/observable/internals/pipe.ts +1 -1
  139. package/src/rpc/envelopes.ts +7 -7
  140. package/src/shared/createProxy/index.ts +1 -1
  141. package/src/shared/getErrorShape.ts +1 -1
  142. package/src/shared/internal/serialize.ts +4 -14
  143. package/src/subscription.ts +4 -2
  144. package/src/transformer.ts +3 -3
  145. package/src/types.ts +8 -7
@@ -12,7 +12,7 @@ export type UploadHandlerPart = {
12
12
 
13
13
  export type UploadHandler = (
14
14
  part: UploadHandlerPart,
15
- ) => Promise<NodeOnDiskFile | File | string | null | undefined>;
15
+ ) => Promise<File | NodeOnDiskFile | string | null | undefined>;
16
16
 
17
17
  export function composeUploadHandlers(
18
18
  ...handlers: UploadHandler[]
@@ -53,7 +53,7 @@ export type MemoryUploadHandlerOptions = {
53
53
  * @param mimetype
54
54
  * @param encoding
55
55
  */
56
- filter?(args: MemoryUploadHandlerFilterArgs): boolean | Promise<boolean>;
56
+ filter?(args: MemoryUploadHandlerFilterArgs): Promise<boolean> | boolean;
57
57
  };
58
58
 
59
59
  /**
@@ -21,6 +21,7 @@ export async function nodeHTTPRequestHandler<
21
21
  >(opts: NodeHTTPRequestHandlerOptions<TRouter, TRequest, TResponse>) {
22
22
  const handleViaMiddleware = opts.middleware ?? ((_req, _res, next) => next());
23
23
 
24
+ // eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
24
25
  return handleViaMiddleware(opts.req, opts.res, async (err) => {
25
26
  if (err) throw err;
26
27
 
@@ -5,7 +5,7 @@ import { MaybePromise } from '../../types';
5
5
  import { NodeHTTPContentTypeHandler } from './internals/contentType';
6
6
 
7
7
  interface ParsedQs {
8
- [key: string]: undefined | string | string[] | ParsedQs | ParsedQs[];
8
+ [key: string]: ParsedQs | ParsedQs[] | string[] | string | undefined;
9
9
  }
10
10
 
11
11
  export type NodeHTTPRequest = IncomingMessage & {
@@ -45,46 +45,46 @@ export type NodeHTTPCreateContextOption<
45
45
  /**
46
46
  * @internal
47
47
  */
48
- interface ConnectMiddleware<
48
+ type ConnectMiddleware<
49
49
  TRequest extends NodeHTTPRequest = NodeHTTPRequest,
50
50
  TResponse extends NodeHTTPResponse = NodeHTTPResponse,
51
- > {
52
- (req: TRequest, res: TResponse, next: (err?: any) => any): void;
53
- }
51
+ > = (req: TRequest, res: TResponse, next: (err?: any) => any) => void;
54
52
 
55
53
  export type NodeHTTPHandlerOptions<
56
54
  TRouter extends AnyRouter,
57
55
  TRequest extends NodeHTTPRequest,
58
56
  TResponse extends NodeHTTPResponse,
59
- > = HTTPBaseHandlerOptions<TRouter, TRequest> & {
60
- /**
61
- * By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
62
- *
63
- * This can be used to handle them manually or via the `cors` npm package: https://www.npmjs.com/package/cors
64
- *
65
- * ```ts
66
- * import cors from 'cors'
67
- *
68
- * nodeHTTPRequestHandler({
69
- * cors: cors()
70
- * })
71
- * ```
72
- *
73
- * You can also use it for other needs which a connect/node.js compatible middleware can solve,
74
- * though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
75
- */
76
- middleware?: ConnectMiddleware;
77
- maxBodySize?: number;
78
- experimental_contentTypeHandlers?: NodeHTTPContentTypeHandler<
79
- TRequest,
80
- TResponse
81
- >[];
82
- } & NodeHTTPCreateContextOption<TRouter, TRequest, TResponse>;
57
+ > = HTTPBaseHandlerOptions<TRouter, TRequest> &
58
+ NodeHTTPCreateContextOption<TRouter, TRequest, TResponse> & {
59
+ /**
60
+ * By default, http `OPTIONS` requests are not handled, and CORS headers are not returned.
61
+ *
62
+ * This can be used to handle them manually or via the `cors` npm package: https://www.npmjs.com/package/cors
63
+ *
64
+ * ```ts
65
+ * import cors from 'cors'
66
+ *
67
+ * nodeHTTPRequestHandler({
68
+ * cors: cors()
69
+ * })
70
+ * ```
71
+ *
72
+ * You can also use it for other needs which a connect/node.js compatible middleware can solve,
73
+ * though you might wish to consider an alternative solution like the Express adapter if your needs are complex.
74
+ */
75
+ middleware?: ConnectMiddleware;
76
+ maxBodySize?: number;
77
+ experimental_contentTypeHandlers?: NodeHTTPContentTypeHandler<
78
+ TRequest,
79
+ TResponse
80
+ >[];
81
+ };
83
82
 
84
83
  export type NodeHTTPRequestHandlerOptions<
85
84
  TRouter extends AnyRouter,
86
85
  TRequest extends NodeHTTPRequest,
87
86
  TResponse extends NodeHTTPResponse,
87
+ // eslint-disable-next-line @typescript-eslint/sort-type-constituents
88
88
  > = {
89
89
  req: TRequest;
90
90
  res: TResponse;
@@ -7,7 +7,6 @@ import {
7
7
  ProcedureType,
8
8
  } from '../core';
9
9
  import { getTRPCErrorFromUnknown, TRPCError } from '../error/TRPCError';
10
- import { getCauseFromUnknown } from '../error/utils';
11
10
  import { BaseHandlerOptions } from '../internals/types';
12
11
  import { isObservable, Unsubscribable } from '../observable';
13
12
  import {
@@ -101,10 +100,11 @@ export function parseMessage(
101
100
  export type WSSHandlerOptions<TRouter extends AnyRouter> = BaseHandlerOptions<
102
101
  TRouter,
103
102
  IncomingMessage
104
- > & {
105
- wss: ws.Server;
106
- process?: NodeJS.Process;
107
- } & NodeHTTPCreateContextOption<TRouter, IncomingMessage, ws>;
103
+ > &
104
+ NodeHTTPCreateContextOption<TRouter, IncomingMessage, ws> & {
105
+ wss: ws.Server;
106
+ process?: NodeJS.Process;
107
+ };
108
108
 
109
109
  export type CreateWSSContextFnOptions = NodeHTTPCreateContextFnOptions<
110
110
  IncomingMessage,
@@ -130,7 +130,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
130
130
 
131
131
  function stopSubscription(
132
132
  subscription: Unsubscribable,
133
- { id, jsonrpc }: { id: JSONRPC2.RequestId } & JSONRPC2.BaseEnvelope,
133
+ { id, jsonrpc }: JSONRPC2.BaseEnvelope & { id: JSONRPC2.RequestId },
134
134
  ) {
135
135
  subscription.unsubscribe();
136
136
 
@@ -280,6 +280,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
280
280
  }
281
281
  client.on('message', async (message) => {
282
282
  try {
283
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
283
284
  const msgJSON: unknown = JSON.parse(message.toString());
284
285
  const msgs: unknown[] = Array.isArray(msgJSON) ? msgJSON : [msgJSON];
285
286
  const promises = msgs
@@ -289,7 +290,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
289
290
  } catch (cause) {
290
291
  const error = new TRPCError({
291
292
  code: 'PARSE_ERROR',
292
- cause: getCauseFromUnknown(cause),
293
+ cause,
293
294
  });
294
295
 
295
296
  respond({
@@ -1,6 +1,6 @@
1
1
  import { Parser } from '../parser';
2
2
 
3
- export type ParseFn<TType> = (value: unknown) => TType | Promise<TType>;
3
+ export type ParseFn<TType> = (value: unknown) => Promise<TType> | TType;
4
4
 
5
5
  export function getParseFn<TType>(procedureParser: Parser): ParseFn<TType> {
6
6
  const parser = procedureParser as any;
@@ -28,12 +28,12 @@ export type MergeRouters<
28
28
  {
29
29
  _config: TRouterDef['_config'];
30
30
  router: true;
31
- procedures: TRouterDef['procedures'] & Head['_def']['procedures'];
32
- record: TRouterDef['record'] & Head['_def']['record'];
33
- queries: TRouterDef['queries'] & Head['_def']['queries'];
34
- mutations: TRouterDef['mutations'] & Head['_def']['mutations'];
35
- subscriptions: TRouterDef['subscriptions'] &
36
- Head['_def']['subscriptions'];
31
+ procedures: Head['_def']['procedures'] & TRouterDef['procedures'];
32
+ record: Head['_def']['record'] & TRouterDef['record'];
33
+ queries: Head['_def']['queries'] & TRouterDef['queries'];
34
+ mutations: Head['_def']['mutations'] & TRouterDef['mutations'];
35
+ subscriptions: Head['_def']['subscriptions'] &
36
+ TRouterDef['subscriptions'];
37
37
  }
38
38
  >
39
39
  : Router<TRouterDef> & TRouterDef['record'];
@@ -1,5 +1,4 @@
1
1
  import { TRPCError } from '../error/TRPCError';
2
- import { getCauseFromUnknown } from '../error/utils';
3
2
  import { Simplify } from '../types';
4
3
  import { AnyRootConfig } from './internals/config';
5
4
  import { ParseFn } from './internals/getParseFn';
@@ -46,8 +45,8 @@ interface MiddlewareErrorResult<_TParams extends ProcedureParams>
46
45
  * @internal
47
46
  */
48
47
  export type MiddlewareResult<TParams extends ProcedureParams> =
49
- | MiddlewareOKResult<TParams>
50
- | MiddlewareErrorResult<TParams>;
48
+ | MiddlewareErrorResult<TParams>
49
+ | MiddlewareOKResult<TParams>;
51
50
 
52
51
  /**
53
52
  * @internal
@@ -209,7 +208,7 @@ export function createInputMiddleware<TInput>(parse: ParseFn<TInput>) {
209
208
  } catch (cause) {
210
209
  throw new TRPCError({
211
210
  code: 'BAD_REQUEST',
212
- cause: getCauseFromUnknown(cause),
211
+ cause,
213
212
  });
214
213
  }
215
214
 
@@ -249,7 +248,7 @@ export function createOutputMiddleware<TOutput>(parse: ParseFn<TOutput>) {
249
248
  throw new TRPCError({
250
249
  message: 'Output validation failed',
251
250
  code: 'INTERNAL_SERVER_ERROR',
252
- cause: getCauseFromUnknown(cause),
251
+ cause,
253
252
  });
254
253
  }
255
254
  };
@@ -13,7 +13,7 @@ export type ParserSuperstructEsque<TInput> = {
13
13
 
14
14
  export type ParserCustomValidatorEsque<TInput> = (
15
15
  input: unknown,
16
- ) => TInput | Promise<TInput>;
16
+ ) => Promise<TInput> | TInput;
17
17
 
18
18
  export type ParserYupEsque<TInput> = {
19
19
  validateSync: (input: unknown) => TInput;
@@ -24,18 +24,18 @@ export type ParserScaleEsque<TInput> = {
24
24
  };
25
25
 
26
26
  export type ParserWithoutInput<TInput> =
27
- | ParserYupEsque<TInput>
28
- | ParserSuperstructEsque<TInput>
29
27
  | ParserCustomValidatorEsque<TInput>
30
28
  | ParserMyZodEsque<TInput>
31
- | ParserScaleEsque<TInput>;
29
+ | ParserScaleEsque<TInput>
30
+ | ParserSuperstructEsque<TInput>
31
+ | ParserYupEsque<TInput>;
32
32
 
33
33
  export type ParserWithInputOutput<TInput, TParsedInput> = ParserZodEsque<
34
34
  TInput,
35
35
  TParsedInput
36
36
  >;
37
37
 
38
- export type Parser = ParserWithoutInput<any> | ParserWithInputOutput<any, any>;
38
+ export type Parser = ParserWithInputOutput<any, any> | ParserWithoutInput<any>;
39
39
 
40
40
  export type inferParser<TParser extends Parser> =
41
41
  TParser extends ParserWithInputOutput<infer $TIn, infer $TOut>
@@ -78,7 +78,7 @@ export interface Procedure<
78
78
  TParams extends ProcedureParams,
79
79
  > {
80
80
  _type: TType;
81
- _def: TParams & ProcedureBuilderDef<TParams>;
81
+ _def: ProcedureBuilderDef<TParams> & TParams;
82
82
  /**
83
83
  * @deprecated use `._def.meta` instead
84
84
  */
@@ -83,7 +83,7 @@ export type AnyRouterDef<
83
83
  */
84
84
  type inferHandlerFn<TProcedures extends ProcedureRecord> = <
85
85
  TProcedure extends TProcedures[TPath],
86
- TPath extends keyof TProcedures & string,
86
+ TPath extends string & keyof TProcedures,
87
87
  >(
88
88
  path: TPath,
89
89
  ...args: inferHandlerInput<TProcedure>
@@ -109,7 +109,7 @@ type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord> = {
109
109
  */
110
110
  type RouterCaller<TDef extends AnyRouterDef> = (
111
111
  ctx: TDef['_config']['$types']['ctx'],
112
- ) => {
112
+ ) => DecoratedProcedureRecord<TDef['record']> & {
113
113
  /**
114
114
  * @deprecated
115
115
  */
@@ -122,7 +122,7 @@ type RouterCaller<TDef extends AnyRouterDef> = (
122
122
  * @deprecated
123
123
  */
124
124
  subscription: inferHandlerFn<TDef['subscriptions']>;
125
- } & DecoratedProcedureRecord<TDef['record']>;
125
+ };
126
126
 
127
127
  export interface Router<TDef extends AnyRouterDef> {
128
128
  _def: TDef;
@@ -137,7 +137,7 @@ export interface Router<TDef extends AnyRouterDef> {
137
137
  type: ProcedureType | 'unknown';
138
138
  path: string | undefined;
139
139
  input: unknown;
140
- ctx: undefined | TDef['_config']['$types']['ctx'];
140
+ ctx: TDef['_config']['$types']['ctx'] | undefined;
141
141
  }): TDef['_config']['$types']['errorShape'];
142
142
  }
143
143
 
package/src/core/types.ts CHANGED
@@ -42,7 +42,7 @@ export type inferProcedureOutput<TProcedure> =
42
42
  */
43
43
  export type inferSubscriptionOutput<
44
44
  TRouter extends AnyRouter,
45
- TPath extends keyof TRouter['_def']['subscriptions'] & string,
45
+ TPath extends string & keyof TRouter['_def']['subscriptions'],
46
46
  > = inferObservableValue<
47
47
  inferProcedureOutput<TRouter['_def']['subscriptions'][TPath]>
48
48
  >;
@@ -40,8 +40,8 @@ interface MiddlewareErrorResult<TContext>
40
40
  * @deprecated
41
41
  */
42
42
  export type MiddlewareResult<TContext> =
43
- | MiddlewareOKResult<TContext>
44
- | MiddlewareErrorResult<TContext>;
43
+ | MiddlewareErrorResult<TContext>
44
+ | MiddlewareOKResult<TContext>;
45
45
 
46
46
  /**
47
47
  * @deprecated
@@ -1,5 +1,4 @@
1
1
  import { getTRPCErrorFromUnknown, TRPCError } from '../../error/TRPCError';
2
- import { getCauseFromUnknown } from '../../error/utils';
3
2
  import { InferLast } from '../../types';
4
3
  import { ProcedureType } from '../router';
5
4
  import {
@@ -23,7 +22,7 @@ export type ProcedureParserSuperstructEsque<TType> = {
23
22
 
24
23
  export type ProcedureParserCustomValidatorEsque<TType> = (
25
24
  input: unknown,
26
- ) => TType | Promise<TType>;
25
+ ) => Promise<TType> | TType;
27
26
 
28
27
  export type ProcedureParserYupEsque<TType> = {
29
28
  validateSync: (input: unknown) => TType;
@@ -33,10 +32,10 @@ export type ProcedureParserWithInputOutput<TInput, TOutput> =
33
32
  ProcedureParserZodEsque<TInput, TOutput>;
34
33
 
35
34
  export type ProcedureParser<TType> =
36
- | ProcedureParserYupEsque<TType>
37
- | ProcedureParserSuperstructEsque<TType>
38
35
  | ProcedureParserCustomValidatorEsque<TType>
39
- | ProcedureParserMyZodEsque<TType>;
36
+ | ProcedureParserMyZodEsque<TType>
37
+ | ProcedureParserSuperstructEsque<TType>
38
+ | ProcedureParserYupEsque<TType>;
40
39
 
41
40
  export type ProcedureResolver<TContext, TInput, TOutput> = (opts: {
42
41
  ctx: TContext;
@@ -45,7 +44,7 @@ export type ProcedureResolver<TContext, TInput, TOutput> = (opts: {
45
44
  }) => Promise<TOutput> | TOutput;
46
45
 
47
46
  interface ProcedureOptions<TContext, TMeta, TInput, TOutput, TParsedOutput> {
48
- middlewares: Array<MiddlewareFunction<any, any, any>>;
47
+ middlewares: MiddlewareFunction<any, any, any>[];
49
48
  resolver: ProcedureResolver<TContext, TInput, TOutput>;
50
49
  inputParser: ProcedureParser<TInput>;
51
50
  outputParser: ProcedureParser<TParsedOutput>;
@@ -63,7 +62,7 @@ export interface ProcedureCallOptions<TContext> {
63
62
  type: ProcedureType;
64
63
  }
65
64
 
66
- type ParseFn<TType> = (value: unknown) => TType | Promise<TType>;
65
+ type ParseFn<TType> = (value: unknown) => Promise<TType> | TType;
67
66
 
68
67
  function getParseFn<TType>(
69
68
  procedureParser: ProcedureParser<TType>,
@@ -112,7 +111,7 @@ export class Procedure<
112
111
  TParsedOutput,
113
112
  TFinalOutput = unknown extends TParsedOutput ? TOutput : TParsedOutput,
114
113
  > {
115
- private middlewares: Readonly<Array<MiddlewareFunction<any, any, any>>>;
114
+ private middlewares: Readonly<MiddlewareFunction<any, any, any>[]>;
116
115
  private resolver: ProcedureResolver<TContext, TParsedInput, TOutput>;
117
116
  private readonly inputParser: ProcedureParser<TParsedInput>;
118
117
  private parseInputFn: ParseFn<TParsedInput>;
@@ -154,7 +153,7 @@ export class Procedure<
154
153
  } catch (cause) {
155
154
  throw new TRPCError({
156
155
  code: 'BAD_REQUEST',
157
- cause: getCauseFromUnknown(cause),
156
+ cause,
158
157
  });
159
158
  }
160
159
  }
@@ -165,7 +164,7 @@ export class Procedure<
165
164
  } catch (cause) {
166
165
  throw new TRPCError({
167
166
  code: 'INTERNAL_SERVER_ERROR',
168
- cause: getCauseFromUnknown(cause),
167
+ cause,
169
168
  message: 'Output validation failed',
170
169
  });
171
170
  }
@@ -254,25 +253,23 @@ export class Procedure<
254
253
  public inheritMiddlewares(
255
254
  middlewares: MiddlewareFunction<TInputContext, TContext, TMeta>[],
256
255
  ): this {
257
- const Constructor: {
258
- new (
259
- opts: ProcedureOptions<
260
- TContext,
261
- TMeta,
262
- TParsedInput,
263
- TOutput,
264
- TFinalOutput
265
- >,
266
- ): Procedure<
267
- TInputContext,
256
+ const Constructor: new (
257
+ opts: ProcedureOptions<
268
258
  TContext,
269
259
  TMeta,
270
- TInput,
271
260
  TParsedInput,
272
261
  TOutput,
273
- TParsedOutput
274
- >;
275
- } = (this as any).constructor;
262
+ TFinalOutput
263
+ >,
264
+ ) => Procedure<
265
+ TInputContext,
266
+ TContext,
267
+ TMeta,
268
+ TInput,
269
+ TParsedInput,
270
+ TOutput,
271
+ TParsedOutput
272
+ > = (this as any).constructor;
276
273
 
277
274
  const instance = new Constructor({
278
275
  middlewares: [...middlewares, ...this.middlewares],
@@ -314,8 +311,8 @@ export type CreateProcedureWithoutInput<
314
311
  TParsedOutput,
315
312
  > = {
316
313
  output?:
317
- | ProcedureParserWithInputOutput<TOutput, TParsedOutput>
318
- | ProcedureParser<TOutput>;
314
+ | ProcedureParser<TOutput>
315
+ | ProcedureParserWithInputOutput<TOutput, TParsedOutput>;
319
316
  meta?: TMeta;
320
317
  resolve: ProcedureResolver<TContext, undefined, InferLast<TOutput>>;
321
318
  };
@@ -328,6 +325,7 @@ export type CreateProcedureOptions<
328
325
  TOutput = undefined,
329
326
  TParsedOutput = undefined,
330
327
  > =
328
+ | CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>
331
329
  | CreateProcedureWithInputOutputParser<
332
330
  TContext,
333
331
  TMeta,
@@ -336,7 +334,6 @@ export type CreateProcedureOptions<
336
334
  TOutput,
337
335
  TParsedOutput
338
336
  >
339
- | CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>
340
337
  | CreateProcedureWithoutInput<TContext, TMeta, TOutput, TParsedOutput>;
341
338
 
342
339
  export function createProcedure<
@@ -43,14 +43,14 @@ type Prefix<
43
43
  * @internal
44
44
  */
45
45
  type Prefixer<TObj extends Record<string, any>, TPrefix extends string> = {
46
- [P in keyof TObj as Prefix<TPrefix, string & P>]: TObj[P];
46
+ [P in keyof TObj as Prefix<TPrefix, P & string>]: TObj[P];
47
47
  };
48
48
 
49
49
  /**
50
50
  * @public
51
51
  * @deprecated
52
52
  */
53
- export type ProcedureType = 'query' | 'mutation' | 'subscription';
53
+ export type ProcedureType = 'mutation' | 'query' | 'subscription';
54
54
 
55
55
  /**
56
56
  * @internal
@@ -138,11 +138,11 @@ export type inferHandlerInput<
138
138
  ? [(null | undefined)?] // -> there is no input
139
139
  : [(TInput | null | undefined)?] // -> there is optional input
140
140
  : [TInput] // -> input is required
141
- : [(undefined | null)?]; // -> there is no input
141
+ : [(null | undefined)?]; // -> there is no input
142
142
 
143
143
  type inferHandlerFn<TProcedures extends ProcedureRecord> = <
144
144
  TProcedure extends TProcedures[TPath],
145
- TPath extends keyof TProcedures & string,
145
+ TPath extends string & keyof TProcedures,
146
146
  >(
147
147
  path: TPath,
148
148
  ...args: inferHandlerInput<TProcedure>
@@ -198,7 +198,7 @@ export type inferRouterError<TRouter extends AnyRouter> = ReturnType<
198
198
  >;
199
199
  const PROCEDURE_DEFINITION_MAP: Record<
200
200
  ProcedureType,
201
- 'queries' | 'mutations' | 'subscriptions'
201
+ 'mutations' | 'queries' | 'subscriptions'
202
202
  > = {
203
203
  query: 'queries',
204
204
  mutation: 'mutations',
@@ -216,7 +216,7 @@ export type ErrorFormatter<TContext, TShape extends TRPCErrorShape<number>> = ({
216
216
  type: ProcedureType | 'unknown';
217
217
  path: string | undefined;
218
218
  input: unknown;
219
- ctx: undefined | TContext;
219
+ ctx: TContext | undefined;
220
220
  shape: DefaultErrorShape;
221
221
  }) => TShape;
222
222
 
@@ -380,8 +380,8 @@ export class Router<
380
380
  TInputContext,
381
381
  TContext,
382
382
  TMeta,
383
- TQueries &
384
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
383
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
384
+ TQueries,
385
385
  TMutations,
386
386
  TSubscriptions,
387
387
  TErrorShape,
@@ -395,8 +395,8 @@ export class Router<
395
395
  TInputContext,
396
396
  TContext,
397
397
  TMeta,
398
- TQueries &
399
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
398
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
399
+ TQueries,
400
400
  TMutations,
401
401
  TSubscriptions,
402
402
  TErrorShape,
@@ -415,8 +415,8 @@ export class Router<
415
415
  TInputContext,
416
416
  TContext,
417
417
  TMeta,
418
- TQueries &
419
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
418
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
419
+ TQueries,
420
420
  TMutations,
421
421
  TSubscriptions,
422
422
  TErrorShape,
@@ -459,8 +459,8 @@ export class Router<
459
459
  TContext,
460
460
  TMeta,
461
461
  TQueries,
462
- TMutations &
463
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
462
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
463
+ TMutations,
464
464
  TSubscriptions,
465
465
  TErrorShape,
466
466
  TTransformer
@@ -474,8 +474,8 @@ export class Router<
474
474
  TContext,
475
475
  TMeta,
476
476
  TQueries,
477
- TMutations &
478
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
477
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
478
+ TMutations,
479
479
  TSubscriptions,
480
480
  TErrorShape,
481
481
  TTransformer
@@ -494,8 +494,8 @@ export class Router<
494
494
  TContext,
495
495
  TMeta,
496
496
  TQueries,
497
- TMutations &
498
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
497
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
498
+ TMutations,
499
499
  TSubscriptions,
500
500
  TErrorShape,
501
501
  TTransformer
@@ -543,8 +543,8 @@ export class Router<
543
543
  TMeta,
544
544
  TQueries,
545
545
  TMutations,
546
- TSubscriptions &
547
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
546
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
547
+ TSubscriptions,
548
548
  TErrorShape,
549
549
  TTransformer
550
550
  >;
@@ -568,8 +568,8 @@ export class Router<
568
568
  TMeta,
569
569
  TQueries,
570
570
  TMutations,
571
- TSubscriptions &
572
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
571
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
572
+ TSubscriptions,
573
573
  TErrorShape,
574
574
  TTransformer
575
575
  >;
@@ -592,8 +592,8 @@ export class Router<
592
592
  TMeta,
593
593
  TQueries,
594
594
  TMutations,
595
- TSubscriptions &
596
- Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>>,
595
+ Record<TPath, inferProcedureFromOptions<TInputContext, typeof procedure>> &
596
+ TSubscriptions,
597
597
  TErrorShape,
598
598
  TTransformer
599
599
  >;
@@ -628,9 +628,9 @@ export class Router<
628
628
  TInputContext,
629
629
  inferRouterContext<TChildRouter>,
630
630
  TMeta,
631
- TQueries & TChildRouter['_def']['queries'],
632
- TMutations & TChildRouter['_def']['mutations'],
633
- TSubscriptions & TChildRouter['_def']['subscriptions'],
631
+ TChildRouter['_def']['queries'] & TQueries,
632
+ TChildRouter['_def']['mutations'] & TMutations,
633
+ TChildRouter['_def']['subscriptions'] & TSubscriptions,
634
634
  TErrorShape,
635
635
  TTransformer
636
636
  >;
@@ -650,10 +650,10 @@ export class Router<
650
650
  TInputContext,
651
651
  inferRouterContext<TChildRouter>,
652
652
  TMeta,
653
- TQueries & Prefixer<TChildRouter['_def']['queries'], `${TPath}`>,
654
- TMutations & Prefixer<TChildRouter['_def']['mutations'], `${TPath}`>,
655
- TSubscriptions &
656
- Prefixer<TChildRouter['_def']['subscriptions'], `${TPath}`>,
653
+ Prefixer<TChildRouter['_def']['queries'], `${TPath}`> & TQueries,
654
+ Prefixer<TChildRouter['_def']['mutations'], `${TPath}`> & TMutations,
655
+ Prefixer<TChildRouter['_def']['subscriptions'], `${TPath}`> &
656
+ TSubscriptions,
657
657
  TErrorShape,
658
658
  TTransformer
659
659
  >;
@@ -672,14 +672,14 @@ export class Router<
672
672
  }
673
673
 
674
674
  const duplicateQueries = Object.keys(childRouter._def.queries).filter(
675
- (key) => !!this._def['queries'][prefix + key],
675
+ (key) => !!this._def.queries[prefix + key],
676
676
  );
677
677
  const duplicateMutations = Object.keys(childRouter._def.mutations).filter(
678
- (key) => !!this._def['mutations'][prefix + key],
678
+ (key) => !!this._def.mutations[prefix + key],
679
679
  );
680
680
  const duplicateSubscriptions = Object.keys(
681
681
  childRouter._def.subscriptions,
682
- ).filter((key) => !!this._def['subscriptions'][prefix + key]);
682
+ ).filter((key) => !!this._def.subscriptions[prefix + key]);
683
683
 
684
684
  const duplicates = [
685
685
  ...duplicateQueries,
@@ -838,7 +838,7 @@ export class Router<
838
838
  type: ProcedureType | 'unknown';
839
839
  path: string | undefined;
840
840
  input: unknown;
841
- ctx: undefined | TContext;
841
+ ctx: TContext | undefined;
842
842
  }): TErrorShape {
843
843
  const { path, error } = opts;
844
844
  const { code } = opts.error;