@trpc/server 10.0.0-proxy-beta.14 → 10.0.0-proxy-beta.16

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 (77) hide show
  1. package/dist/adapters/aws-lambda/index.d.ts +1 -1
  2. package/dist/adapters/aws-lambda/index.d.ts.map +1 -1
  3. package/dist/adapters/aws-lambda/index.js +24 -18
  4. package/dist/adapters/aws-lambda/index.mjs +20 -20
  5. package/dist/adapters/aws-lambda/utils.d.ts +4 -1
  6. package/dist/adapters/aws-lambda/utils.d.ts.map +1 -1
  7. package/dist/adapters/express.js +3 -3
  8. package/dist/adapters/express.mjs +4 -4
  9. package/dist/adapters/fastify/index.js +2 -2
  10. package/dist/adapters/fastify/index.mjs +3 -3
  11. package/dist/adapters/fetch/index.js +2 -2
  12. package/dist/adapters/fetch/index.mjs +3 -3
  13. package/dist/adapters/next.js +3 -3
  14. package/dist/adapters/next.mjs +4 -4
  15. package/dist/adapters/node-http/index.js +3 -3
  16. package/dist/adapters/node-http/index.mjs +4 -4
  17. package/dist/adapters/node-http/types.d.ts +3 -2
  18. package/dist/adapters/node-http/types.d.ts.map +1 -1
  19. package/dist/adapters/standalone.js +3 -3
  20. package/dist/adapters/standalone.mjs +4 -4
  21. package/dist/adapters/ws.js +2 -2
  22. package/dist/adapters/ws.mjs +3 -3
  23. package/dist/core/initTRPC.d.ts +22 -28
  24. package/dist/core/initTRPC.d.ts.map +1 -1
  25. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +40 -100
  26. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
  27. package/dist/core/internals/config.d.ts +33 -11
  28. package/dist/core/internals/config.d.ts.map +1 -1
  29. package/dist/core/internals/mergeRouters.d.ts.map +1 -1
  30. package/dist/core/internals/procedureBuilder.d.ts +16 -21
  31. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  32. package/dist/core/middleware.d.ts +10 -18
  33. package/dist/core/middleware.d.ts.map +1 -1
  34. package/dist/core/procedure.d.ts +2 -13
  35. package/dist/core/procedure.d.ts.map +1 -1
  36. package/dist/core/router.d.ts +26 -40
  37. package/dist/core/router.d.ts.map +1 -1
  38. package/dist/core/types.d.ts +3 -3
  39. package/dist/core/types.d.ts.map +1 -1
  40. package/dist/deprecated/interop.d.ts +18 -13
  41. package/dist/deprecated/interop.d.ts.map +1 -1
  42. package/dist/error/TRPCError.d.ts +1 -1
  43. package/dist/http/internals/types.d.ts +3 -3
  44. package/dist/http/internals/types.d.ts.map +1 -1
  45. package/dist/{index-5a6a2e91.mjs → index-0332d528.mjs} +1 -1
  46. package/dist/index.js +53 -30
  47. package/dist/index.mjs +51 -28
  48. package/dist/internals/transformTRPCResponse.d.ts.map +1 -1
  49. package/dist/internals/types.d.ts +2 -3
  50. package/dist/internals/types.d.ts.map +1 -1
  51. package/dist/{nodeHTTPRequestHandler-efc38711.js → nodeHTTPRequestHandler-0e40a231.js} +1 -1
  52. package/dist/{nodeHTTPRequestHandler-205f7545.mjs → nodeHTTPRequestHandler-bb607c2b.mjs} +1 -1
  53. package/dist/{resolveHTTPResponse-bf4c6ceb.js → resolveHTTPResponse-92daf326.js} +2 -2
  54. package/dist/{resolveHTTPResponse-4ceb1785.mjs → resolveHTTPResponse-9dff3dcc.mjs} +2 -2
  55. package/dist/shared/index.mjs +1 -1
  56. package/dist/{transformTRPCResponse-959de787.mjs → transformTRPCResponse-23e99f82.mjs} +20 -40
  57. package/dist/{transformTRPCResponse-05aa84c1.js → transformTRPCResponse-92c79c47.js} +18 -39
  58. package/package.json +7 -7
  59. package/src/adapters/aws-lambda/index.ts +5 -54
  60. package/src/adapters/aws-lambda/utils.ts +59 -1
  61. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +2 -2
  62. package/src/adapters/node-http/types.ts +3 -2
  63. package/src/adapters/ws.ts +1 -1
  64. package/src/core/initTRPC.ts +48 -38
  65. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +40 -100
  66. package/src/core/internals/config.ts +35 -15
  67. package/src/core/internals/mergeRouters.ts +19 -7
  68. package/src/core/internals/procedureBuilder.ts +31 -34
  69. package/src/core/middleware.ts +7 -9
  70. package/src/core/procedure.ts +2 -15
  71. package/src/core/router.ts +56 -92
  72. package/src/core/types.ts +3 -3
  73. package/src/deprecated/interop.ts +49 -39
  74. package/src/http/internals/types.ts +8 -3
  75. package/src/http/resolveHTTPResponse.ts +1 -1
  76. package/src/internals/transformTRPCResponse.ts +4 -2
  77. package/src/internals/types.ts +2 -3
@@ -1,16 +1,14 @@
1
1
  import { ErrorFormatter } from '../../error/formatter';
2
- import {
3
- CombinedDataTransformer,
4
- DataTransformerOptions,
5
- } from '../../transformer';
6
2
 
7
3
  /**
8
4
  * The initial generics that are used in the init function
9
5
  * @internal
10
6
  */
11
- export interface InitGenerics {
7
+ export interface RootConfigTypes {
12
8
  ctx: Record<string, unknown>;
13
9
  meta: Record<string, unknown>;
10
+ errorShape: unknown;
11
+ transformer: unknown;
14
12
  }
15
13
 
16
14
  /**
@@ -23,40 +21,62 @@ export const isServerDefault: boolean =
23
21
  !!process.env.JEST_WORKER_ID;
24
22
 
25
23
  /**
26
- * The initial params that are used and actually represents real values underneath
24
+ * The runtime config that are used and actually represents real values underneath
27
25
  * @internal
28
26
  */
29
- export interface InitOptions<TType extends InitGenerics> {
30
- transformer: DataTransformerOptions;
31
- errorFormatter: ErrorFormatter<TType['ctx'], any>;
27
+ export interface RuntimeConfig<TTypes extends RootConfigTypes> {
28
+ /**
29
+ * Use a data transformer
30
+ * @link https://trpc.io/docs/data-transformers
31
+ */
32
+ transformer: TTypes['transformer'];
33
+ /**
34
+ * Use custom error formatting
35
+ * @link https://trpc.io/docs/error-formatting
36
+ */
37
+ errorFormatter: ErrorFormatter<TTypes['ctx'], any>;
32
38
  /**
33
39
  * Allow `@trpc/server` to run in non-server environments
40
+ * @warning **Use with caution**, this should likely mainly be used within testing.
34
41
  * @default false
35
42
  */
36
43
  allowOutsideOfServer: boolean;
37
44
  /**
38
45
  * Is this a server environment?
46
+ * @warning **Use with caution**, this should likely mainly be used within testing.
47
+ * @default typeof window === 'undefined' || 'Deno' in window || process.env.NODE_ENV === 'test'
39
48
  */
40
49
  isServer: boolean;
50
+ /**
51
+ * Is this development?
52
+ * Will be used to decide if the API should return stack traces
53
+ * @default process.env.NODE_ENV !== 'production'
54
+ */
55
+ isDev: boolean;
41
56
  }
42
57
 
43
58
  /**
44
59
  * @internal
45
60
  */
46
- export type CreateInitGenerics<TType extends InitGenerics> = TType;
61
+ export type CreateRootConfigTypes<TGenerics extends RootConfigTypes> =
62
+ TGenerics;
47
63
 
48
64
  /**
49
65
  * The config that is resolved after `initTRPC.create()` has been called
50
66
  * Combination of `InitTOptions` + `InitGenerics`
51
67
  * @internal
52
68
  */
53
- export interface RootConfig extends InitGenerics {
54
- transformer: CombinedDataTransformer;
55
- // FIXME this should probably be restricted
56
- errorShape: any;
69
+ export interface RootConfig<TGenerics extends RootConfigTypes>
70
+ extends RuntimeConfig<TGenerics> {
71
+ $types: TGenerics;
57
72
  }
58
73
 
59
74
  /**
60
75
  * @internal
61
76
  */
62
- export type CreateRootConfig<TConfig extends RootConfig> = TConfig;
77
+ export type AnyRootConfig = RootConfig<{
78
+ ctx: any;
79
+ meta: any;
80
+ errorShape: any;
81
+ transformer: any;
82
+ }>;
@@ -11,16 +11,16 @@ export function mergeRouters(...routerList: AnyRouter[]): AnyRouter {
11
11
  const errorFormatter = routerList.reduce(
12
12
  (currentErrorFormatter, nextRouter) => {
13
13
  if (
14
- nextRouter.errorFormatter &&
15
- nextRouter.errorFormatter !== defaultFormatter
14
+ nextRouter._def._config.errorFormatter &&
15
+ nextRouter._def._config.errorFormatter !== defaultFormatter
16
16
  ) {
17
17
  if (
18
18
  currentErrorFormatter !== defaultFormatter &&
19
- currentErrorFormatter !== nextRouter.errorFormatter
19
+ currentErrorFormatter !== nextRouter._def._config.errorFormatter
20
20
  ) {
21
21
  throw new Error('You seem to have several error formatters');
22
22
  }
23
- return nextRouter.errorFormatter;
23
+ return nextRouter._def._config.errorFormatter;
24
24
  }
25
25
  return currentErrorFormatter;
26
26
  },
@@ -28,11 +28,17 @@ export function mergeRouters(...routerList: AnyRouter[]): AnyRouter {
28
28
  );
29
29
 
30
30
  const transformer = routerList.reduce((prev, current) => {
31
- if (current.transformer && current.transformer !== defaultTransformer) {
32
- if (prev !== defaultTransformer && prev !== current.transformer) {
31
+ if (
32
+ current._def._config.transformer &&
33
+ current._def._config.transformer !== defaultTransformer
34
+ ) {
35
+ if (
36
+ prev !== defaultTransformer &&
37
+ prev !== current._def._config.transformer
38
+ ) {
33
39
  throw new Error('You seem to have several transformers');
34
40
  }
35
- return current.transformer;
41
+ return current._def._config.transformer;
36
42
  }
37
43
  return prev;
38
44
  }, defaultTransformer as CombinedDataTransformer);
@@ -40,6 +46,12 @@ export function mergeRouters(...routerList: AnyRouter[]): AnyRouter {
40
46
  const router = createRouterFactory({
41
47
  errorFormatter,
42
48
  transformer,
49
+ isDev: routerList.some((r) => r._def._config.isDev),
50
+ allowOutsideOfServer: routerList.some(
51
+ (r) => r._def._config.allowOutsideOfServer,
52
+ ),
53
+ isServer: routerList.some((r) => r._def._config.isServer),
54
+ $types: routerList[0]?._def._config.$types as any,
43
55
  })(record);
44
56
  return router;
45
57
  }
@@ -17,15 +17,16 @@ import {
17
17
  ProcedureParams,
18
18
  } from '../procedure';
19
19
  import { ProcedureType } from '../types';
20
- import { RootConfig } from './config';
20
+ import { AnyRootConfig } from './config';
21
21
  import { getParseFn } from './getParseFn';
22
22
  import { mergeWithoutOverrides } from './mergeWithoutOverrides';
23
- import { ResolveOptions, middlewareMarker } from './utils';
24
23
  import {
25
24
  DefaultValue as FallbackValue,
26
25
  Overwrite,
27
26
  OverwriteKnown,
27
+ ResolveOptions,
28
28
  UnsetMarker,
29
+ middlewareMarker,
29
30
  } from './utils';
30
31
 
31
32
  type CreateProcedureReturnInput<
@@ -34,7 +35,6 @@ type CreateProcedureReturnInput<
34
35
  > = ProcedureBuilder<{
35
36
  _config: TPrev['_config'];
36
37
  _meta: TPrev['_meta'];
37
- _ctx_in: TPrev['_ctx_in'];
38
38
  _ctx_out: Overwrite<TPrev['_ctx_out'], TNext['_ctx_out']>;
39
39
  _input_in: FallbackValue<TNext['_input_in'], TPrev['_input_in']>;
40
40
  _input_out: FallbackValue<TNext['_input_out'], TPrev['_input_out']>;
@@ -85,26 +85,25 @@ export interface ProcedureBuilder<TParams extends ProcedureParams> {
85
85
  /**
86
86
  * Add an input parser to the procedure.
87
87
  */
88
- input<TParser extends Parser>(
88
+ input<$Parser extends Parser>(
89
89
  schema: TParams['_input_out'] extends UnsetMarker
90
- ? TParser
91
- : inferParser<TParser>['out'] extends Record<string, unknown>
90
+ ? $Parser
91
+ : inferParser<$Parser>['out'] extends Record<string, unknown>
92
92
  ? TParams['_input_out'] extends Record<string, unknown>
93
- ? TParser
93
+ ? $Parser
94
94
  : ErrorMessage<'All input parsers did not resolve to an object'>
95
95
  : ErrorMessage<'All input parsers did not resolve to an object'>,
96
96
  ): ProcedureBuilder<{
97
97
  _config: TParams['_config'];
98
98
  _meta: TParams['_meta'];
99
- _ctx_in: TParams['_ctx_in'];
100
99
  _ctx_out: TParams['_ctx_out'];
101
100
  _input_in: OverwriteIfDefined<
102
101
  TParams['_input_in'],
103
- inferParser<TParser>['in']
102
+ inferParser<$Parser>['in']
104
103
  >;
105
104
  _input_out: OverwriteIfDefined<
106
105
  TParams['_input_out'],
107
- inferParser<TParser>['out']
106
+ inferParser<$Parser>['out']
108
107
  >;
109
108
  _output_in: TParams['_output_in'];
110
109
  _output_out: TParams['_output_out'];
@@ -112,17 +111,16 @@ export interface ProcedureBuilder<TParams extends ProcedureParams> {
112
111
  /**
113
112
  * Add an output parser to the procedure.
114
113
  */
115
- output<TParser extends Parser>(
116
- schema: TParser,
114
+ output<$Parser extends Parser>(
115
+ schema: $Parser,
117
116
  ): ProcedureBuilder<{
118
117
  _config: TParams['_config'];
119
118
  _meta: TParams['_meta'];
120
- _ctx_in: TParams['_ctx_in'];
121
119
  _ctx_out: TParams['_ctx_out'];
122
120
  _input_in: TParams['_input_in'];
123
121
  _input_out: TParams['_input_out'];
124
- _output_in: inferParser<TParser>['in'];
125
- _output_out: inferParser<TParser>['out'];
122
+ _output_in: inferParser<$Parser>['in'];
123
+ _output_out: inferParser<$Parser>['out'];
126
124
  }>;
127
125
  /**
128
126
  * Add a meta data to the procedure.
@@ -131,44 +129,44 @@ export interface ProcedureBuilder<TParams extends ProcedureParams> {
131
129
  /**
132
130
  * Add a middleware to the procedure.
133
131
  */
134
- use<TNewParams extends ProcedureParams>(
135
- fn: MiddlewareFunction<TParams, TNewParams>,
136
- ): CreateProcedureReturnInput<TParams, TNewParams>;
132
+ use<$Params extends ProcedureParams>(
133
+ fn: MiddlewareFunction<TParams, $Params>,
134
+ ): CreateProcedureReturnInput<TParams, $Params>;
137
135
  /**
138
136
  * Extend the procedure with another procedure.
139
137
  * @warning The TypeScript inference fails when chaining concatenated procedures.
140
138
  */
141
- unstable_concat<TProcedureReturnType extends AnyProcedureBuilder>(
142
- proc: TProcedureReturnType,
143
- ): TProcedureReturnType extends ProcedureBuilder<infer $TParams>
139
+ unstable_concat<$ProcedureBuilder extends AnyProcedureBuilder>(
140
+ proc: $ProcedureBuilder,
141
+ ): $ProcedureBuilder extends ProcedureBuilder<infer $TParams>
144
142
  ? CreateProcedureReturnInput<TParams, $TParams>
145
143
  : never;
146
144
  /**
147
145
  * Query procedure
148
146
  */
149
- query<TOutput>(
147
+ query<$Output>(
150
148
  resolver: (
151
149
  opts: ResolveOptions<TParams>,
152
- ) => MaybePromise<FallbackValue<TParams['_output_in'], TOutput>>,
153
- ): BuildProcedure<'query', TParams, TOutput>;
150
+ ) => MaybePromise<FallbackValue<TParams['_output_in'], $Output>>,
151
+ ): BuildProcedure<'query', TParams, $Output>;
154
152
 
155
153
  /**
156
154
  * Mutation procedure
157
155
  */
158
- mutation<TOutput>(
156
+ mutation<$Output>(
159
157
  resolver: (
160
158
  opts: ResolveOptions<TParams>,
161
- ) => MaybePromise<FallbackValue<TParams['_output_in'], TOutput>>,
162
- ): BuildProcedure<'mutation', TParams, TOutput>;
159
+ ) => MaybePromise<FallbackValue<TParams['_output_in'], $Output>>,
160
+ ): BuildProcedure<'mutation', TParams, $Output>;
163
161
 
164
162
  /**
165
163
  * Mutation procedure
166
164
  */
167
- subscription<TOutput>(
165
+ subscription<$Output>(
168
166
  resolver: (
169
167
  opts: ResolveOptions<TParams>,
170
- ) => MaybePromise<FallbackValue<TParams['_output_in'], TOutput>>,
171
- ): BuildProcedure<'subscription', TParams, TOutput>;
168
+ ) => MaybePromise<FallbackValue<TParams['_output_in'], $Output>>,
169
+ ): BuildProcedure<'subscription', TParams, $Output>;
172
170
  /**
173
171
  * @internal
174
172
  */
@@ -197,17 +195,16 @@ function createNewBuilder(
197
195
  } as any);
198
196
  }
199
197
 
200
- export function createBuilder<TConfig extends RootConfig>(
198
+ export function createBuilder<TConfig extends AnyRootConfig>(
201
199
  initDef?: AnyProcedureBuilderDef,
202
200
  ): ProcedureBuilder<{
203
201
  _config: TConfig;
204
- _ctx_in: TConfig['ctx'];
205
- _ctx_out: TConfig['ctx'];
202
+ _ctx_out: TConfig['$types']['ctx'];
206
203
  _input_in: UnsetMarker;
207
204
  _input_out: UnsetMarker;
208
205
  _output_in: UnsetMarker;
209
206
  _output_out: UnsetMarker;
210
- _meta: TConfig['meta'];
207
+ _meta: TConfig['$types']['meta'];
211
208
  }> {
212
209
  const _def: AnyProcedureBuilderDef = initDef || {
213
210
  inputs: [],
@@ -1,6 +1,6 @@
1
1
  import { TRPCError } from '../error/TRPCError';
2
2
  import { getCauseFromUnknown } from '../error/utils';
3
- import { RootConfig } from './internals/config';
3
+ import { AnyRootConfig } from './internals/config';
4
4
  import { ParseFn } from './internals/getParseFn';
5
5
  import { ProcedureBuilderMiddleware } from './internals/procedureBuilder';
6
6
  import { MiddlewareMarker } from './internals/utils';
@@ -60,11 +60,10 @@ export type MiddlewareFunction<
60
60
  meta: TParams['_meta'] | undefined;
61
61
  next: {
62
62
  (): Promise<MiddlewareResult<TParams>>;
63
- <TContext>(opts: { ctx: TContext }): Promise<
63
+ <$Context>(opts: { ctx: $Context }): Promise<
64
64
  MiddlewareResult<{
65
- _config: any;
66
- _ctx_in: TParams['_ctx_in'];
67
- _ctx_out: TContext;
65
+ _config: TParams['_config'];
66
+ _ctx_out: $Context;
68
67
  _input_in: TParams['_input_in'];
69
68
  _input_out: TParams['_input_out'];
70
69
  _output_in: TParams['_output_in'];
@@ -81,18 +80,17 @@ export type MiddlewareFunction<
81
80
  * @internal
82
81
  */
83
82
  // FIXME this should use RootConfig
84
- export function createMiddlewareFactory<TConfig extends RootConfig>() {
83
+ export function createMiddlewareFactory<TConfig extends AnyRootConfig>() {
85
84
  return function createMiddleware<TNewParams extends ProcedureParams>(
86
85
  fn: MiddlewareFunction<
87
86
  {
88
87
  _config: TConfig;
89
- _ctx_in: TConfig['ctx'];
90
- _ctx_out: TConfig['ctx'];
88
+ _ctx_out: TConfig['$types']['ctx'];
91
89
  _input_out: unknown;
92
90
  _input_in: unknown;
93
91
  _output_in: unknown;
94
92
  _output_out: unknown;
95
- _meta: TConfig['meta'];
93
+ _meta: TConfig['$types']['meta'];
96
94
  },
97
95
  TNewParams
98
96
  >,
@@ -1,6 +1,4 @@
1
- import { DefaultErrorShape } from '../error/formatter';
2
- import { CombinedDataTransformer } from '../transformer';
3
- import { RootConfig } from './internals/config';
1
+ import { AnyRootConfig } from './internals/config';
4
2
  import {
5
3
  ProcedureBuilderDef,
6
4
  ProcedureCallOptions,
@@ -26,13 +24,7 @@ export interface ProcedureOptions {
26
24
  * @internal
27
25
  */
28
26
  export interface ProcedureParams<
29
- TConfig extends RootConfig = {
30
- transformer: CombinedDataTransformer;
31
- errorShape: DefaultErrorShape;
32
- ctx: Record<string, unknown>;
33
- meta: Record<string, unknown>;
34
- },
35
- TContextIn = unknown,
27
+ TConfig extends AnyRootConfig = AnyRootConfig,
36
28
  TContextOut = unknown,
37
29
  TInputIn = unknown,
38
30
  TInputOut = unknown,
@@ -40,16 +32,11 @@ export interface ProcedureParams<
40
32
  TOutputOut = unknown,
41
33
  TMeta = unknown,
42
34
  > {
43
- // FIXME make non-optional
44
35
  _config: TConfig;
45
36
  /**
46
37
  * @internal
47
38
  */
48
39
  _meta: TMeta;
49
- /**
50
- * @internal
51
- */
52
- _ctx_in: TContextIn;
53
40
  /**
54
41
  * @internal
55
42
  */
@@ -1,15 +1,10 @@
1
1
  import { TRPCError } from '../error/TRPCError';
2
- import {
3
- DefaultErrorShape,
4
- ErrorFormatter,
5
- defaultFormatter,
6
- } from '../error/formatter';
2
+ import { DefaultErrorShape, defaultFormatter } from '../error/formatter';
7
3
  import { getHTTPStatusCodeFromError } from '../http/internals/getHTTPStatusCode';
8
- import { TRPCErrorShape, TRPC_ERROR_CODES_BY_KEY } from '../rpc';
4
+ import { TRPC_ERROR_CODES_BY_KEY } from '../rpc';
9
5
  import { createRecursiveProxy } from '../shared';
10
- import { CombinedDataTransformer, defaultTransformer } from '../transformer';
11
- import { RootConfig } from './internals/config';
12
- import { mergeWithoutOverrides } from './internals/mergeWithoutOverrides';
6
+ import { defaultTransformer } from '../transformer';
7
+ import { AnyRootConfig } from './internals/config';
13
8
  import { omitPrototype } from './internals/omitPrototype';
14
9
  import { ProcedureCallOptions } from './internals/procedureBuilder';
15
10
  import {
@@ -33,55 +28,52 @@ export interface ProcedureRouterRecord {
33
28
  [key: string]: AnyProcedure | AnyRouter;
34
29
  }
35
30
 
36
- export interface ProcedureStructure {
31
+ /**
32
+ * @deprecated
33
+ */
34
+ interface DeprecatedProcedureRouterRecord {
37
35
  queries: Record<string, AnyQueryProcedure>;
38
36
  mutations: Record<string, AnyMutationProcedure>;
39
37
  subscriptions: Record<string, AnySubscriptionProcedure>;
40
- procedures: ProcedureRecord;
41
38
  }
42
39
 
43
40
  export interface RouterDef<
44
- // FIXME this should use RootConfig
45
- TContext,
46
- TErrorShape extends TRPCErrorShape<number>,
47
- TMeta extends Record<string, unknown>,
41
+ TConfig extends AnyRootConfig,
48
42
  TRecord extends ProcedureRouterRecord,
49
- > {
50
- router: true;
51
- /**
52
- * @internal
53
- */
54
- _ctx: TContext;
55
- /**
56
- * @internal
57
- */
58
- _errorShape: TErrorShape;
59
43
  /**
60
- * @internal
44
+ * @deprecated
61
45
  */
62
- _meta: TMeta;
63
- errorFormatter: ErrorFormatter<TContext, TErrorShape>;
64
- transformer: CombinedDataTransformer;
46
+ TOld extends DeprecatedProcedureRouterRecord = {
47
+ queries: {};
48
+ mutations: {};
49
+ subscriptions: {};
50
+ },
51
+ > {
52
+ _config: TConfig;
53
+ router: true;
65
54
  procedures: TRecord;
66
55
  record: TRecord;
67
56
  /**
68
57
  * V9 queries
69
58
  * @deprecated
70
59
  */
71
- queries: {};
60
+ queries: TOld['queries'];
72
61
  /**
73
62
  * V9 mutations
74
63
  * @deprecated
75
64
  */
76
- mutations: {};
65
+ mutations: TOld['mutations'];
77
66
  /**
78
67
  * V9 subscriptions
79
68
  * @deprecated
80
69
  */
81
- subscriptions: {};
70
+ subscriptions: TOld['subscriptions'];
82
71
  }
83
72
 
84
- export type AnyRouterDef<TContext = any> = RouterDef<TContext, any, any, any>;
73
+ export type AnyRouterDef<
74
+ TConfig extends AnyRootConfig = AnyRootConfig,
75
+ TOld extends DeprecatedProcedureRouterRecord = any,
76
+ > = RouterDef<TConfig, any, TOld>;
85
77
 
86
78
  /**
87
79
  * @internal
@@ -112,7 +104,9 @@ type DecoratedProcedureRecord<TProcedures extends ProcedureRouterRecord> = {
112
104
  /**
113
105
  * @internal
114
106
  */
115
- type RouterCaller<TDef extends AnyRouterDef> = (ctx: TDef['_ctx']) => {
107
+ type RouterCaller<TDef extends AnyRouterDef> = (
108
+ ctx: TDef['_config']['$types']['ctx'],
109
+ ) => {
116
110
  /**
117
111
  * @deprecated
118
112
  */
@@ -129,10 +123,6 @@ type RouterCaller<TDef extends AnyRouterDef> = (ctx: TDef['_ctx']) => {
129
123
 
130
124
  export interface Router<TDef extends AnyRouterDef> {
131
125
  _def: TDef;
132
- /** @deprecated **/
133
- errorFormatter: TDef['errorFormatter'];
134
- /** @deprecated **/
135
- transformer: TDef['transformer'];
136
126
  createCaller: RouterCaller<TDef>;
137
127
  // FIXME rename me and deprecate
138
128
  getErrorShape(opts: {
@@ -140,26 +130,18 @@ export interface Router<TDef extends AnyRouterDef> {
140
130
  type: ProcedureType | 'unknown';
141
131
  path: string | undefined;
142
132
  input: unknown;
143
- ctx: undefined | TDef['_ctx'];
144
- }): TDef['_errorShape'];
133
+ ctx: undefined | TDef['_config']['$types']['ctx'];
134
+ }): TDef['_config']['$types']['errorShape'];
145
135
  }
146
136
 
147
137
  /**
148
138
  * @internal
149
139
  */
150
- export type RouterDefaultOptions<TContext> = Pick<
151
- AnyRouterDef<TContext>,
152
- 'transformer' | 'errorFormatter'
140
+ export type RouterBuildOptions<TConfig extends AnyRootConfig> = Partial<
141
+ Pick<AnyRouterDef<TConfig>, 'procedures'>
153
142
  >;
154
143
 
155
- /**
156
- * @internal
157
- */
158
- export type RouterBuildOptions<TContext> = Partial<
159
- Pick<AnyRouterDef<TContext>, 'procedures'>
160
- >;
161
-
162
- export type AnyRouter = Router<any>;
144
+ export type AnyRouter = Router<AnyRouterDef>;
163
145
 
164
146
  function isRouter(
165
147
  procedureOrRouter: AnyProcedure | AnyRouter,
@@ -193,29 +175,23 @@ const reservedWords = [
193
175
  * @internal
194
176
  */
195
177
  export type CreateRouterInner<
196
- TConfig extends RootConfig,
178
+ TConfig extends AnyRootConfig,
197
179
  TProcRouterRecord extends ProcedureRouterRecord,
198
- > = Router<
199
- RouterDef<
200
- TConfig['ctx'],
201
- TConfig['errorShape'],
202
- TConfig['meta'],
203
- TProcRouterRecord
204
- >
205
- > &
206
- TProcRouterRecord;
180
+ > = Router<RouterDef<TConfig, TProcRouterRecord>> & TProcRouterRecord;
207
181
 
208
182
  /**
209
183
  * @internal
210
184
  */
211
- export function createRouterFactory<TConfig extends RootConfig>(
212
- defaults?: RouterDefaultOptions<TConfig['ctx']>,
185
+ export function createRouterFactory<TConfig extends AnyRootConfig>(
186
+ config: TConfig,
213
187
  ) {
214
188
  return function createRouterInner<
215
189
  TProcRouterRecord extends ProcedureRouterRecord,
216
- >(opts: TProcRouterRecord): CreateRouterInner<TConfig, TProcRouterRecord> {
190
+ >(
191
+ procedures: TProcRouterRecord,
192
+ ): CreateRouterInner<TConfig, TProcRouterRecord> {
217
193
  const reservedWordsUsed = new Set(
218
- Object.keys(opts).filter((v) => reservedWords.includes(v)),
194
+ Object.keys(procedures).filter((v) => reservedWords.includes(v)),
219
195
  );
220
196
  if (reservedWordsUsed.size > 0) {
221
197
  throw new Error(
@@ -234,44 +210,35 @@ export function createRouterFactory<TConfig extends RootConfig>(
234
210
  continue;
235
211
  }
236
212
 
213
+ if (routerProcedures[newPath]) {
214
+ throw new Error(`Duplicate key: ${newPath}`);
215
+ }
216
+
237
217
  routerProcedures[newPath] = procedureOrRouter;
238
218
  }
239
219
  }
220
+ recursiveGetPaths(procedures);
240
221
 
241
- recursiveGetPaths(opts);
242
-
243
- const result = mergeWithoutOverrides<
244
- RouterDefaultOptions<TConfig['ctx']> & RouterBuildOptions<TConfig['ctx']>
245
- >(
246
- {
247
- transformer: defaults?.transformer ?? defaultTransformer,
248
- errorFormatter: defaults?.errorFormatter ?? defaultFormatter,
249
- },
250
- { procedures: routerProcedures },
251
- );
252
-
253
- const _def: AnyRouterDef<TConfig['ctx']> = {
222
+ const _def: AnyRouterDef<TConfig> = {
223
+ _config: config,
254
224
  router: true,
255
- procedures: {},
225
+ procedures: routerProcedures,
256
226
  ...emptyRouter,
257
- ...result,
258
- record: opts,
259
- queries: Object.entries(result.procedures || {})
227
+ record: procedures,
228
+ queries: Object.entries(routerProcedures || {})
260
229
  .filter((pair) => (pair[1] as any)._def.query)
261
230
  .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
262
- mutations: Object.entries(result.procedures || {})
231
+ mutations: Object.entries(routerProcedures || {})
263
232
  .filter((pair) => (pair[1] as any)._def.mutation)
264
233
  .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
265
- subscriptions: Object.entries(result.procedures || {})
234
+ subscriptions: Object.entries(routerProcedures || {})
266
235
  .filter((pair) => (pair[1] as any)._def.subscription)
267
236
  .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
268
237
  };
269
238
 
270
239
  const router: AnyRouter = {
271
- ...opts,
240
+ ...procedures,
272
241
  _def,
273
- transformer: _def.transformer,
274
- errorFormatter: _def.errorFormatter,
275
242
  createCaller(ctx) {
276
243
  const proxy = createRecursiveProxy(({ path, args }) => {
277
244
  // interop mode
@@ -319,16 +286,13 @@ export function createRouterFactory<TConfig extends RootConfig>(
319
286
  httpStatus: getHTTPStatusCodeFromError(error),
320
287
  },
321
288
  };
322
- if (
323
- process.env.NODE_ENV !== 'production' &&
324
- typeof opts.error.stack === 'string'
325
- ) {
289
+ if (config.isDev && typeof opts.error.stack === 'string') {
326
290
  shape.data.stack = opts.error.stack;
327
291
  }
328
292
  if (typeof path === 'string') {
329
293
  shape.data.path = path;
330
294
  }
331
- return this._def.errorFormatter({ ...opts, shape });
295
+ return this._def._config.errorFormatter({ ...opts, shape });
332
296
  },
333
297
  };
334
298
  return router as any;
package/src/core/types.ts CHANGED
@@ -11,11 +11,11 @@ export type inferRouterDef<TRouter extends AnyRouter> = TRouter extends Router<
11
11
  : never;
12
12
 
13
13
  export type inferRouterContext<TRouter extends AnyRouter> =
14
- inferRouterDef<TRouter>['_ctx'];
14
+ inferRouterDef<TRouter>['_config']['$types']['ctx'];
15
15
  export type inferRouterError<TRouter extends AnyRouter> =
16
- inferRouterDef<TRouter>['_errorShape'];
16
+ inferRouterDef<TRouter>['_config']['$types']['errorShape'];
17
17
  export type inferRouterMeta<TRouter extends AnyRouter> =
18
- inferRouterDef<TRouter>['_meta'];
18
+ inferRouterDef<TRouter>['_config']['$types']['meta'];
19
19
 
20
20
  export const procedureTypes = ['query', 'mutation', 'subscription'] as const;
21
21
  /**