@trpc/server 10.0.0-alpha.30 → 10.0.0-alpha.31

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 (34) hide show
  1. package/dist/core/initTRPC.d.ts +29 -6
  2. package/dist/core/initTRPC.d.ts.map +1 -1
  3. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +80 -20
  4. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
  5. package/dist/core/internals/config.d.ts +2 -2
  6. package/dist/core/internals/config.d.ts.map +1 -1
  7. package/dist/core/internals/procedureBuilder.d.ts +4 -0
  8. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  9. package/dist/core/middleware.d.ts +9 -1
  10. package/dist/core/middleware.d.ts.map +1 -1
  11. package/dist/core/procedure.d.ts +12 -1
  12. package/dist/core/procedure.d.ts.map +1 -1
  13. package/dist/core/router.d.ts +27 -17
  14. package/dist/core/router.d.ts.map +1 -1
  15. package/dist/core/types.d.ts +2 -2
  16. package/dist/core/types.d.ts.map +1 -1
  17. package/dist/deprecated/interop.d.ts +22 -6
  18. package/dist/deprecated/interop.d.ts.map +1 -1
  19. package/dist/index.js +13 -10
  20. package/dist/index.mjs +13 -10
  21. package/dist/types.d.ts +5 -3
  22. package/dist/types.d.ts.map +1 -1
  23. package/package.json +2 -2
  24. package/src/core/initTRPC.ts +1 -1
  25. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +260 -200
  26. package/src/core/internals/config.ts +3 -2
  27. package/src/core/internals/mergeRouters.ts +3 -3
  28. package/src/core/internals/procedureBuilder.ts +4 -0
  29. package/src/core/middleware.ts +2 -0
  30. package/src/core/procedure.ts +13 -0
  31. package/src/core/router.ts +58 -29
  32. package/src/core/types.ts +4 -3
  33. package/src/deprecated/interop.ts +65 -32
  34. package/src/types.ts +5 -3
@@ -1,4 +1,4 @@
1
- import { DefaultErrorShape, ErrorFormatter } from '../../error/formatter';
1
+ import { ErrorFormatter } from '../../error/formatter';
2
2
  import {
3
3
  CombinedDataTransformer,
4
4
  DataTransformerOptions,
@@ -33,7 +33,8 @@ export type CreateInitGenerics<T extends InitGenerics> = T;
33
33
  */
34
34
  export interface RootConfig extends InitGenerics {
35
35
  transformer: CombinedDataTransformer;
36
- errorShape: DefaultErrorShape;
36
+ // FIXME this should probably be restricted
37
+ errorShape: any;
37
38
  }
38
39
 
39
40
  /**
@@ -4,9 +4,9 @@ import { AnyRouter, createRouterFactory } from '../router';
4
4
  import { mergeWithoutOverrides } from './mergeWithoutOverrides';
5
5
 
6
6
  export function mergeRouters(...routerList: AnyRouter[]): AnyRouter {
7
- const procedures = mergeWithoutOverrides(
7
+ const record = mergeWithoutOverrides(
8
8
  {},
9
- ...routerList.map((r) => r._def.procedures),
9
+ ...routerList.map((r) => r._def.record),
10
10
  );
11
11
  const errorFormatter = routerList.reduce(
12
12
  (currentErrorFormatter, nextRouter) => {
@@ -40,6 +40,6 @@ export function mergeRouters(...routerList: AnyRouter[]): AnyRouter {
40
40
  const router = createRouterFactory({
41
41
  errorFormatter,
42
42
  transformer,
43
- })(procedures);
43
+ })(record);
44
44
  return router;
45
45
  }
@@ -17,6 +17,7 @@ type CreateProcedureReturnInput<
17
17
  TPrev extends ProcedureParams,
18
18
  TNext extends ProcedureParams,
19
19
  > = ProcedureBuilder<{
20
+ _config: TPrev['_config'];
20
21
  _meta: TPrev['_meta'];
21
22
  _ctx_in: TPrev['_ctx_in'];
22
23
  _ctx_out: Overwrite<TPrev['_ctx_out'], TNext['_ctx_out']>;
@@ -33,6 +34,7 @@ export interface ProcedureBuilder<TParams extends ProcedureParams> {
33
34
  input<$TParser extends Parser>(
34
35
  schema: $TParser,
35
36
  ): ProcedureBuilder<{
37
+ _config: TParams['_config'];
36
38
  _meta: TParams['_meta'];
37
39
  _ctx_in: TParams['_ctx_in'];
38
40
  _ctx_out: TParams['_ctx_out'];
@@ -47,6 +49,7 @@ export interface ProcedureBuilder<TParams extends ProcedureParams> {
47
49
  output<$TParser extends Parser>(
48
50
  schema: $TParser,
49
51
  ): ProcedureBuilder<{
52
+ _config: TParams['_config'];
50
53
  _meta: TParams['_meta'];
51
54
  _ctx_in: TParams['_ctx_in'];
52
55
  _ctx_out: TParams['_ctx_out'];
@@ -168,6 +171,7 @@ export interface ProcedureBuilder<TParams extends ProcedureParams> {
168
171
  }
169
172
 
170
173
  export function createBuilder<TConfig extends RootConfig>(): ProcedureBuilder<{
174
+ _config: TConfig;
171
175
  _ctx_in: TConfig['ctx'];
172
176
  _ctx_out: TConfig['ctx'];
173
177
  _input_out: UnsetMarker;
@@ -58,6 +58,7 @@ export type MiddlewareFunction<
58
58
  (): Promise<MiddlewareResult<TParams>>;
59
59
  <$TContext>(opts: { ctx: $TContext }): Promise<
60
60
  MiddlewareResult<{
61
+ _config: any;
61
62
  _ctx_in: TParams['_ctx_in'];
62
63
  _ctx_out: $TContext;
63
64
  _input_in: TParams['_input_in'];
@@ -78,6 +79,7 @@ export function createMiddlewareFactory<TConfig extends RootConfig>() {
78
79
  return function createMiddleware<$TNewParams extends ProcedureParams>(
79
80
  fn: MiddlewareFunction<
80
81
  {
82
+ _config: TConfig;
81
83
  _ctx_in: TConfig['ctx'];
82
84
  _ctx_out: TConfig['ctx'];
83
85
  _input_out: unknown;
@@ -1,3 +1,6 @@
1
+ import { DefaultErrorShape } from '../error/formatter';
2
+ import { CombinedDataTransformer } from '../transformer';
3
+ import { RootConfig } from './internals/config';
1
4
  import { UnsetMarker } from './internals/utils';
2
5
 
3
6
  type ClientContext = Record<string, unknown>;
@@ -12,6 +15,12 @@ export interface ProcedureOptions {
12
15
  * @internal
13
16
  */
14
17
  export interface ProcedureParams<
18
+ TConfig extends RootConfig = {
19
+ transformer: CombinedDataTransformer;
20
+ errorShape: DefaultErrorShape;
21
+ ctx: Record<string, unknown>;
22
+ meta: Record<string, unknown>;
23
+ },
15
24
  TContextIn = unknown,
16
25
  TContextOut = unknown,
17
26
  TInputIn = unknown,
@@ -20,6 +29,8 @@ export interface ProcedureParams<
20
29
  TOutputOut = unknown,
21
30
  TMeta = unknown,
22
31
  > {
32
+ // FIXME make non-optional
33
+ _config: TConfig;
23
34
  /**
24
35
  * @internal
25
36
  */
@@ -64,10 +75,12 @@ export type ProcedureArgs<TParams extends ProcedureParams> =
64
75
  * @internal
65
76
  */
66
77
  export interface ProcedureBase<TParams extends ProcedureParams> {
78
+ _def: TParams;
67
79
  /**
68
80
  * @deprecated use `._def.meta` instead
69
81
  */
70
82
  meta?: TParams['_meta'];
83
+ _procedure: true;
71
84
  }
72
85
 
73
86
  export interface QueryProcedure<TParams extends ProcedureParams>
@@ -1,6 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/ban-types */
2
2
  import { inferProcedureOutput } from '.';
3
- import { Filter } from '..';
3
+ import { Filter, Prefixer } from '..';
4
4
  import { TRPCError } from '../error/TRPCError';
5
5
  import {
6
6
  DefaultErrorShape,
@@ -36,13 +36,36 @@ export interface ProcedureStructure {
36
36
  procedures: ProcedureRecord;
37
37
  }
38
38
 
39
+ type ObjKeyof<T> = T extends object ? keyof T : never;
40
+ type KeyofKeyof<T> = ObjKeyof<T> | { [K in keyof T]: ObjKeyof<T[K]> }[keyof T];
41
+ type StripNever<T> = Pick<
42
+ T,
43
+ { [K in keyof T]: [T[K]] extends [never] ? never : K }[keyof T]
44
+ >;
45
+ type Lookup<T, K> = T extends any ? (K extends keyof T ? T[K] : never) : never;
46
+ type SimpleFlatten<T> = T extends object
47
+ ? StripNever<{
48
+ [K in KeyofKeyof<T>]:
49
+ | Exclude<K extends keyof T ? T[K] : never, object>
50
+ | { [P in keyof T]: Lookup<T[P], K> }[keyof T];
51
+ }>
52
+ : T;
53
+
54
+ type PrefixedProcedures<T extends ProcedureRouterRecord> = {
55
+ [K in keyof T]: T[K] extends AnyRouter
56
+ ? T[K] extends Procedure<any>
57
+ ? never
58
+ : Prefixer<T[K]['_def']['procedures'], `${K & string}.`>
59
+ : never;
60
+ };
39
61
  export interface RouterDef<
40
62
  // FIXME this should use RootConfig
41
63
  TContext,
42
64
  TErrorShape extends TRPCErrorShape<number>,
43
65
  TMeta extends Record<string, unknown>,
44
- TProcedures extends ProcedureRouterRecord,
66
+ TRecord extends ProcedureRouterRecord,
45
67
  > {
68
+ router: true;
46
69
  /**
47
70
  * @internal
48
71
  */
@@ -57,21 +80,21 @@ export interface RouterDef<
57
80
  _meta: TMeta;
58
81
  errorFormatter: ErrorFormatter<TContext, TErrorShape>;
59
82
  transformer: CombinedDataTransformer;
60
- // FIXME: separate `procedures` and `children`-routers
61
- procedures: TProcedures;
62
- // FIXME decide if these are deprecated
63
- /**
64
- * @deprecated
65
- */
66
- subscriptions: Filter<TProcedures, SubscriptionProcedure<any>>;
67
- /**
68
- * @deprecated
69
- */
70
- queries: Filter<TProcedures, QueryProcedure<any>>;
71
- /**
72
- * @deprecated
73
- */
74
- mutations: Filter<TProcedures, MutationProcedure<any>>;
83
+ // FIXME this is slow
84
+ procedures: Filter<TRecord, Procedure<any>> &
85
+ SimpleFlatten<PrefixedProcedures<TRecord>>;
86
+ routers: Filter<TRecord, Router<any>>;
87
+ record: TRecord;
88
+ // FIXME this is slow
89
+ subscriptions: Filter<TRecord, SubscriptionProcedure<any>> &
90
+ Filter<
91
+ SimpleFlatten<PrefixedProcedures<TRecord>>,
92
+ SubscriptionProcedure<any>
93
+ >;
94
+ queries: Filter<TRecord, QueryProcedure<any>> &
95
+ Filter<SimpleFlatten<PrefixedProcedures<TRecord>>, QueryProcedure<any>>;
96
+ mutations: Filter<TRecord, MutationProcedure<any>> &
97
+ Filter<SimpleFlatten<PrefixedProcedures<TRecord>>, MutationProcedure<any>>;
75
98
  }
76
99
 
77
100
  export type AnyRouterDef<TContext = any> = RouterDef<TContext, any, any, any>;
@@ -124,7 +147,7 @@ export interface Router<TDef extends AnyRouterDef> {
124
147
  TDef['_ctx'],
125
148
  TDef['_errorShape'],
126
149
  TDef['_meta'],
127
- TDef['procedures']
150
+ TDef['record']
128
151
  >;
129
152
  /** @deprecated **/
130
153
  errorFormatter: TDef['errorFormatter'];
@@ -185,16 +208,19 @@ const emptyRouter = {
185
208
  export function createRouterFactory<TConfig extends RootConfig>(
186
209
  defaults?: RouterDefaultOptions<TConfig['ctx']>,
187
210
  ) {
188
- return function createRouterInner<TProcedures extends ProcedureRouterRecord>(
189
- procedures: TProcedures,
211
+ return function createRouterInner<
212
+ TProcRouterRecord extends ProcedureRouterRecord,
213
+ >(
214
+ opts: TProcRouterRecord,
190
215
  ): Router<
191
216
  RouterDef<
192
217
  TConfig['ctx'],
193
218
  TConfig['errorShape'],
194
219
  TConfig['meta'],
195
- TProcedures
220
+ TProcRouterRecord
196
221
  >
197
- > {
222
+ > &
223
+ TProcRouterRecord {
198
224
  const routerProcedures: Record<string, Procedure<any>> = omitPrototype({});
199
225
  function recursiveGetPaths(procedures: ProcedureRouterRecord, path = '') {
200
226
  for (const [key, procedureOrRouter] of Object.entries(procedures ?? {})) {
@@ -210,7 +236,7 @@ export function createRouterFactory<TConfig extends RootConfig>(
210
236
  }
211
237
  }
212
238
 
213
- recursiveGetPaths(procedures);
239
+ recursiveGetPaths(opts);
214
240
 
215
241
  const result = mergeWithoutOverrides<
216
242
  RouterDefaultOptions<TConfig['ctx']> & RouterBuildOptions<TConfig['ctx']>
@@ -223,9 +249,11 @@ export function createRouterFactory<TConfig extends RootConfig>(
223
249
  );
224
250
 
225
251
  const _def: AnyRouterDef<TConfig['ctx']> = {
252
+ router: true,
226
253
  procedures: {},
227
254
  ...emptyRouter,
228
255
  ...result,
256
+ record: opts,
229
257
  queries: Object.entries(result.procedures || {})
230
258
  .filter((pair) => (pair[1] as any)._def.query)
231
259
  .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
@@ -235,11 +263,9 @@ export function createRouterFactory<TConfig extends RootConfig>(
235
263
  subscriptions: Object.entries(result.procedures || {})
236
264
  .filter((pair) => (pair[1] as any)._def.subscription)
237
265
  .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
238
- };
239
- const def = {
240
- _def,
241
- transformer: _def.transformer,
242
- errorFormatter: _def.errorFormatter,
266
+ routers: Object.entries(result.procedures || {})
267
+ .filter((pair) => (pair[1] as any)._def._router)
268
+ .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
243
269
  };
244
270
 
245
271
  function callProcedure(opts: InternalProcedureCallOptions) {
@@ -257,7 +283,10 @@ export function createRouterFactory<TConfig extends RootConfig>(
257
283
  return procedure(opts);
258
284
  }
259
285
  const router: AnyRouter = {
260
- ...def,
286
+ ...opts,
287
+ _def,
288
+ transformer: _def.transformer,
289
+ errorFormatter: _def.errorFormatter,
261
290
  createCaller(ctx) {
262
291
  return {
263
292
  query: (path, ...args) =>
package/src/core/types.ts CHANGED
@@ -40,9 +40,10 @@ export type inferProcedureInput<TProcedure extends Procedure<any>> =
40
40
  // ? TProcedure['mutate']
41
41
  // : never;
42
42
 
43
- export type inferProcedureParams<TProcedure extends Procedure<any>> =
44
- TProcedure extends Procedure<infer $Params> ? $Params : never;
45
- export type inferProcedureOutput<TProcedure extends Procedure<any>> =
43
+ export type inferProcedureParams<TProcedure> = TProcedure extends Procedure<any>
44
+ ? TProcedure['_def']
45
+ : never;
46
+ export type inferProcedureOutput<TProcedure> =
46
47
  inferProcedureParams<TProcedure>['_output_out'];
47
48
 
48
49
  export type inferSubscriptionOutput<
@@ -1,4 +1,5 @@
1
- import { ProcedureParams, ProcedureType } from '..';
1
+ import { CombinedDataTransformer, ProcedureParams, ProcedureType } from '..';
2
+ import { CreateRootConfig, RootConfig } from '../core/internals/config';
2
3
  import { getParseFnOrPassThrough } from '../core/internals/getParseFn';
3
4
  import {
4
5
  createInputMiddleware,
@@ -27,48 +28,53 @@ import { ProcedureRecord } from './router';
27
28
 
28
29
  type AnyOldProcedure = OldProcedure<any, any, any, any, any, any, any, any>;
29
30
 
30
- type convertProcedureParams<TProcedure extends AnyOldProcedure> =
31
- TProcedure extends OldProcedure<
32
- infer TInputContext,
33
- infer TContext,
34
- infer TMeta,
35
- infer TInput,
36
- infer TParsedInput,
37
- infer TOutput,
38
- infer _TParsedOutput,
39
- infer TFinalInput
40
- >
41
- ? ProcedureParams<
42
- TInputContext,
43
- TContext,
44
- TInput,
45
- TParsedInput,
46
- TOutput,
47
- TFinalInput,
48
- TMeta
49
- >
50
- : never;
31
+ type convertProcedureParams<
32
+ TConfig extends RootConfig,
33
+ TProcedure extends AnyOldProcedure,
34
+ > = TProcedure extends OldProcedure<
35
+ infer TInputContext,
36
+ infer TContext,
37
+ infer TMeta,
38
+ infer TInput,
39
+ infer TParsedInput,
40
+ infer TOutput,
41
+ infer _TParsedOutput,
42
+ infer TFinalInput
43
+ >
44
+ ? ProcedureParams<
45
+ TConfig,
46
+ TInputContext,
47
+ TContext,
48
+ TInput,
49
+ TParsedInput,
50
+ TOutput,
51
+ TFinalInput,
52
+ TMeta
53
+ >
54
+ : never;
51
55
 
52
56
  type MigrateProcedure<
57
+ TConfig extends RootConfig,
53
58
  TProcedure extends AnyOldProcedure,
54
59
  TType extends ProcedureType,
55
60
  > = TType extends 'query'
56
- ? QueryProcedure<convertProcedureParams<TProcedure>>
61
+ ? QueryProcedure<convertProcedureParams<TConfig, TProcedure>>
57
62
  : TType extends 'mutation'
58
- ? MutationProcedure<convertProcedureParams<TProcedure>>
63
+ ? MutationProcedure<convertProcedureParams<TConfig, TProcedure>>
59
64
  : TType extends 'subscription'
60
- ? SubscriptionProcedure<convertProcedureParams<TProcedure>>
65
+ ? SubscriptionProcedure<convertProcedureParams<TConfig, TProcedure>>
61
66
  : never;
62
67
 
63
68
  export type MigrateProcedureRecord<
69
+ TConfig extends RootConfig,
64
70
  T extends ProcedureRecord<any>,
65
71
  TType extends ProcedureType,
66
72
  > = {
67
- [K in keyof T]: MigrateProcedure<T[K], TType>;
73
+ [K in keyof T]: MigrateProcedure<TConfig, T[K], TType>;
68
74
  };
69
75
 
70
76
  export type MigrateRouter<
71
- TInputContext,
77
+ TInputContext extends Record<string, any>,
72
78
  TContext,
73
79
  TMeta extends Record<string, any>,
74
80
  TQueries extends ProcedureRecord<
@@ -98,15 +104,42 @@ export type MigrateRouter<
98
104
  unknown,
99
105
  unknown
100
106
  >,
101
- TErrorShape extends TRPCErrorShape<number>,
107
+ TErrorShape extends TRPCErrorShape<any>,
102
108
  > = NewRouter<
103
109
  RouterDef<
104
110
  TInputContext,
105
111
  TErrorShape,
106
112
  TMeta,
107
- MigrateProcedureRecord<TQueries, 'query'> &
108
- MigrateProcedureRecord<TMutations, 'mutation'> &
109
- MigrateProcedureRecord<TSubscriptions, 'subscription'>
113
+ MigrateProcedureRecord<
114
+ CreateRootConfig<{
115
+ ctx: TInputContext;
116
+ errorShape: TErrorShape;
117
+ meta: TMeta;
118
+ transformer: CombinedDataTransformer;
119
+ }>,
120
+ TQueries,
121
+ 'query'
122
+ > &
123
+ MigrateProcedureRecord<
124
+ CreateRootConfig<{
125
+ ctx: TInputContext;
126
+ errorShape: TErrorShape;
127
+ meta: TMeta;
128
+ transformer: CombinedDataTransformer;
129
+ }>,
130
+ TMutations,
131
+ 'mutation'
132
+ > &
133
+ MigrateProcedureRecord<
134
+ CreateRootConfig<{
135
+ ctx: TInputContext;
136
+ errorShape: TErrorShape;
137
+ meta: TMeta;
138
+ transformer: CombinedDataTransformer;
139
+ }>,
140
+ TSubscriptions,
141
+ 'subscription'
142
+ >
110
143
  >
111
144
  >;
112
145
 
@@ -134,7 +167,7 @@ export type MigrateOldRouter<TRouter extends AnyOldRouter> =
134
167
  function migrateProcedure<
135
168
  TProcedure extends AnyOldProcedure,
136
169
  TType extends ProcedureType,
137
- >(oldProc: TProcedure, type: TType): MigrateProcedure<TProcedure, TType> {
170
+ >(oldProc: TProcedure, type: TType): MigrateProcedure<any, TProcedure, TType> {
138
171
  const def = oldProc._def();
139
172
 
140
173
  const inputParser = getParseFnOrPassThrough(def.inputParser);
package/src/types.ts CHANGED
@@ -66,9 +66,11 @@ export type InferLast<T> = T & { [K in keyof T]: T[K] };
66
66
  export type inferAsyncReturnType<TFunction extends (...args: any) => any> =
67
67
  ThenArg<ReturnType<TFunction>>;
68
68
 
69
+ type FilterKeys<T extends object, K> = {
70
+ [TKey in keyof T]: T[TKey] extends K ? TKey : never;
71
+ }[keyof T];
72
+
69
73
  /**
70
74
  * @internal
71
75
  */
72
- export type Filter<T extends Record<any, any> | any[], K> = {
73
- [TKey in keyof T]: T[TKey] extends K ? T[TKey] : never;
74
- };
76
+ export type Filter<T extends object, K> = Pick<T, FilterKeys<T, K>>;