@trpc/server 10.0.0-alpha.29 → 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 (60) hide show
  1. package/dist/adapters/node-http/types.d.ts +2 -2
  2. package/dist/adapters/node-http/types.d.ts.map +1 -1
  3. package/dist/core/index.d.ts +1 -1
  4. package/dist/core/index.d.ts.map +1 -1
  5. package/dist/core/initTRPC.d.ts +30 -25
  6. package/dist/core/initTRPC.d.ts.map +1 -1
  7. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +121 -41
  8. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
  9. package/dist/core/internals/config.d.ts +3 -5
  10. package/dist/core/internals/config.d.ts.map +1 -1
  11. package/dist/core/internals/internalProcedure.d.ts +18 -1
  12. package/dist/core/internals/internalProcedure.d.ts.map +1 -1
  13. package/dist/core/internals/mergeRouters.d.ts.map +1 -1
  14. package/dist/core/internals/prefixObjectKeys.d.ts +2 -0
  15. package/dist/core/internals/prefixObjectKeys.d.ts.map +1 -0
  16. package/dist/core/internals/procedureBuilder.d.ts +39 -8
  17. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  18. package/dist/core/middleware.d.ts +16 -7
  19. package/dist/core/middleware.d.ts.map +1 -1
  20. package/dist/core/procedure.d.ts +26 -3
  21. package/dist/core/procedure.d.ts.map +1 -1
  22. package/dist/core/router.d.ts +52 -60
  23. package/dist/core/router.d.ts.map +1 -1
  24. package/dist/core/types.d.ts +9 -9
  25. package/dist/core/types.d.ts.map +1 -1
  26. package/dist/deprecated/interop.d.ts +23 -23
  27. package/dist/deprecated/interop.d.ts.map +1 -1
  28. package/dist/http/internals/types.d.ts +3 -3
  29. package/dist/http/internals/types.d.ts.map +1 -1
  30. package/dist/index.js +100 -56
  31. package/dist/index.mjs +100 -56
  32. package/dist/internals/types.d.ts +2 -2
  33. package/dist/internals/types.d.ts.map +1 -1
  34. package/dist/observable/observable.d.ts +1 -1
  35. package/dist/observable/observable.d.ts.map +1 -1
  36. package/dist/types.d.ts +8 -0
  37. package/dist/types.d.ts.map +1 -1
  38. package/package.json +2 -2
  39. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +2 -2
  40. package/src/adapters/node-http/types.ts +2 -4
  41. package/src/core/index.ts +1 -1
  42. package/src/core/initTRPC.ts +14 -9
  43. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +184 -104
  44. package/src/core/internals/config.ts +4 -5
  45. package/src/core/internals/internalProcedure.ts +52 -21
  46. package/src/core/internals/mergeRouters.ts +3 -15
  47. package/src/core/internals/prefixObjectKeys.ts +9 -0
  48. package/src/core/internals/procedureBuilder.ts +92 -9
  49. package/src/core/middleware.ts +7 -4
  50. package/src/core/procedure.ts +38 -2
  51. package/src/core/router.ts +143 -136
  52. package/src/core/types.ts +33 -16
  53. package/src/deprecated/interop.ts +110 -50
  54. package/src/http/internals/types.ts +3 -3
  55. package/src/internals/types.ts +2 -2
  56. package/src/observable/observable.ts +6 -3
  57. package/src/types.ts +9 -0
  58. package/dist/core/internals/mergeRoutersGeneric.d.ts +0 -21
  59. package/dist/core/internals/mergeRoutersGeneric.d.ts.map +0 -1
  60. package/src/core/internals/mergeRoutersGeneric.ts +0 -35
@@ -1,4 +1,6 @@
1
1
  /* eslint-disable @typescript-eslint/ban-types */
2
+ import { inferProcedureOutput } from '.';
3
+ import { Filter, Prefixer } from '..';
2
4
  import { TRPCError } from '../error/TRPCError';
3
5
  import {
4
6
  DefaultErrorShape,
@@ -15,28 +17,55 @@ import {
15
17
  } from './internals/internalProcedure';
16
18
  import { mergeWithoutOverrides } from './internals/mergeWithoutOverrides';
17
19
  import { omitPrototype } from './internals/omitPrototype';
18
- import { EnsureRecord, ValidateShape } from './internals/utils';
19
- import { Procedure } from './procedure';
20
+ import {
21
+ MutationProcedure,
22
+ Procedure,
23
+ QueryProcedure,
24
+ SubscriptionProcedure,
25
+ } from './procedure';
20
26
  import { ProcedureType } from './types';
21
27
 
22
- // FIXME this should properly use TContext maybe?
23
- export type ProcedureRecord<_TContext> = Record<string, Procedure<any>>;
24
- type AnyProcedureRecord = ProcedureRecord<any>;
28
+ type ProcedureRecord = Record<string, Procedure<any>>;
29
+
30
+ export type ProcedureRouterRecord = Record<string, Procedure<any> | AnyRouter>;
31
+
25
32
  export interface ProcedureStructure {
26
- queries: AnyProcedureRecord;
27
- mutations: AnyProcedureRecord;
28
- subscriptions: AnyProcedureRecord;
33
+ queries: Record<string, QueryProcedure<any>>;
34
+ mutations: Record<string, MutationProcedure<any>>;
35
+ subscriptions: Record<string, SubscriptionProcedure<any>>;
36
+ procedures: ProcedureRecord;
29
37
  }
30
38
 
31
- export interface RouterParams<
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
+ };
61
+ export interface RouterDef<
32
62
  // FIXME this should use RootConfig
33
63
  TContext,
34
64
  TErrorShape extends TRPCErrorShape<number>,
35
65
  TMeta extends Record<string, unknown>,
36
- TQueries extends ProcedureRecord<TContext>,
37
- TMutations extends ProcedureRecord<TContext>,
38
- TSubscriptions extends ProcedureRecord<TContext>,
39
- > extends ProcedureStructure {
66
+ TRecord extends ProcedureRouterRecord,
67
+ > {
68
+ router: true;
40
69
  /**
41
70
  * @internal
42
71
  */
@@ -49,114 +78,107 @@ export interface RouterParams<
49
78
  * @internal
50
79
  */
51
80
  _meta: TMeta;
52
- queries: TQueries;
53
- mutations: TMutations;
54
- subscriptions: TSubscriptions;
55
81
  errorFormatter: ErrorFormatter<TContext, TErrorShape>;
56
82
  transformer: CombinedDataTransformer;
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>>;
57
98
  }
58
99
 
59
- export type AnyRouterParams<TContext = any> = RouterParams<
60
- TContext,
61
- any,
62
- any,
63
- any,
64
- any,
65
- any
66
- >;
100
+ export type AnyRouterDef<TContext = any> = RouterDef<TContext, any, any, any>;
67
101
 
68
102
  /**
69
103
  * @internal
70
104
  */
71
105
  export type inferHandlerInput<TProcedure extends Procedure<any>> =
72
- TProcedure extends Procedure<infer TParams>
73
- ? undefined extends TParams['_input_in'] // ? is input optional
74
- ? unknown extends TParams['_input_in'] // ? is input unset
106
+ TProcedure extends Procedure<infer TDef>
107
+ ? undefined extends TDef['_input_in'] // ? is input optional
108
+ ? unknown extends TDef['_input_in'] // ? is input unset
75
109
  ? [(null | undefined)?] // -> there is no input
76
- : [(TParams['_input_in'] | null | undefined)?] // -> there is optional input
77
- : [TParams['_input_in']] // -> input is required
110
+ : [(TDef['_input_in'] | null | undefined)?] // -> there is optional input
111
+ : [TDef['_input_in']] // -> input is required
78
112
  : [(undefined | null)?]; // -> there is no input
79
113
 
80
114
  /**
81
115
  * @internal
82
116
  */
83
- type inferHandlerFn<TProcedures extends ProcedureRecord<any>> = <
117
+ type inferHandlerFn<TProcedures extends Record<string, Procedure<any>>> = <
84
118
  TProcedure extends TProcedures[TPath],
85
119
  TPath extends keyof TProcedures & string,
86
120
  >(
87
121
  path: TPath,
88
122
  ...args: inferHandlerInput<TProcedure>
89
- ) => ReturnType<TProcedure>;
123
+ ) => inferProcedureOutput<TProcedure>;
90
124
 
91
125
  /**
92
126
  * This only exists b/c of interop mode
93
127
  * @internal
94
128
  */
95
129
 
96
- type RouterCaller<TParams extends AnyRouterParams> = (ctx: TParams['_ctx']) => {
130
+ type RouterCaller<TDef extends AnyRouterDef> = (ctx: TDef['_ctx']) => {
97
131
  /**
98
132
  * @deprecated
99
133
  */
100
- query: inferHandlerFn<TParams['queries']>;
134
+ query: inferHandlerFn<TDef['queries']>;
101
135
  /**
102
136
  * @deprecated
103
137
  */
104
- mutation: inferHandlerFn<TParams['mutations']>;
138
+ mutation: inferHandlerFn<TDef['mutations']>;
105
139
  /**
106
140
  * @deprecated
107
141
  */
108
- subscription: inferHandlerFn<TParams['subscriptions']>;
109
-
110
- queries: TParams['queries'];
111
- mutations: TParams['mutations'];
112
- subscriptions: TParams['subscriptions'];
142
+ subscription: inferHandlerFn<TDef['subscriptions']>;
113
143
  };
114
144
 
115
- export interface Router<TParams extends AnyRouterParams>
116
- extends ProcedureStructure {
117
- _def: RouterParams<
118
- TParams['_ctx'],
119
- TParams['_errorShape'],
120
- TParams['_meta'],
121
- TParams['queries'],
122
- TParams['mutations'],
123
- TParams['subscriptions']
145
+ export interface Router<TDef extends AnyRouterDef> {
146
+ _def: RouterDef<
147
+ TDef['_ctx'],
148
+ TDef['_errorShape'],
149
+ TDef['_meta'],
150
+ TDef['record']
124
151
  >;
125
- queries: TParams['queries'];
126
- mutations: TParams['mutations'];
127
- subscriptions: TParams['subscriptions'];
128
- errorFormatter: TParams['errorFormatter'];
129
- transformer: TParams['transformer'];
152
+ /** @deprecated **/
153
+ errorFormatter: TDef['errorFormatter'];
154
+ /** @deprecated **/
155
+ transformer: TDef['transformer'];
130
156
 
131
- createCaller: RouterCaller<TParams>;
157
+ // FIXME rename me and deprecate
158
+ createCaller: RouterCaller<TDef>;
159
+ // FIXME rename me and deprecate
132
160
  getErrorShape(opts: {
133
161
  error: TRPCError;
134
162
  type: ProcedureType | 'unknown';
135
163
  path: string | undefined;
136
164
  input: unknown;
137
- ctx: undefined | TParams['_ctx'];
138
- }): TParams['_errorShape'];
165
+ ctx: undefined | TDef['_ctx'];
166
+ }): TDef['_errorShape'];
139
167
  }
140
168
 
141
- /**
142
- * @internal
143
- */
144
- export type RouterOptions<TContext> = Partial<AnyRouterParams<TContext>>;
145
-
146
169
  /**
147
170
  * @internal
148
171
  */
149
172
  export type RouterDefaultOptions<TContext> = Pick<
150
- AnyRouterParams<TContext>,
173
+ AnyRouterDef<TContext>,
151
174
  'transformer' | 'errorFormatter'
152
175
  >;
153
176
 
154
177
  /**
155
178
  * @internal
156
179
  */
157
- type RouterBuildOptions<TContext> = Pick<
158
- RouterOptions<TContext>,
159
- 'queries' | 'subscriptions' | 'mutations'
180
+ export type RouterBuildOptions<TContext> = Partial<
181
+ Pick<AnyRouterDef<TContext>, 'procedures'>
160
182
  >;
161
183
 
162
184
  export type AnyRouter = Router<any>;
@@ -178,81 +200,93 @@ const emptyRouter = {
178
200
  errorFormatter: defaultFormatter,
179
201
  transformer: defaultTransformer,
180
202
  };
181
- // type EmptyRouter = typeof emptyRouter;
182
-
183
- const PROCEDURE_DEFINITION_MAP: Record<
184
- ProcedureType,
185
- 'queries' | 'mutations' | 'subscriptions'
186
- > = {
187
- query: 'queries',
188
- mutation: 'mutations',
189
- subscription: 'subscriptions',
190
- };
191
203
 
192
204
  /**
193
205
  *
194
206
  * @internal
195
207
  */
196
- export function createRouterFactory<TSettings extends RootConfig>(
197
- defaults?: RouterDefaultOptions<TSettings['ctx']>,
208
+ export function createRouterFactory<TConfig extends RootConfig>(
209
+ defaults?: RouterDefaultOptions<TConfig['ctx']>,
198
210
  ) {
199
211
  return function createRouterInner<
200
- TProcedures extends RouterBuildOptions<TSettings['ctx']>,
212
+ TProcRouterRecord extends ProcedureRouterRecord,
201
213
  >(
202
- procedures: ValidateShape<
203
- TProcedures,
204
- RouterBuildOptions<TSettings['ctx']>
205
- >,
206
- ): Router<{
207
- _ctx: TSettings['ctx'];
208
- _errorShape: TSettings['errorShape'];
209
- _meta: TSettings['meta'];
210
- queries: EnsureRecord<TProcedures['queries']>;
211
- mutations: EnsureRecord<TProcedures['mutations']>;
212
- subscriptions: EnsureRecord<TProcedures['subscriptions']>;
213
- errorFormatter: ErrorFormatter<TSettings['ctx'], TSettings['errorShape']>;
214
- transformer: TSettings['transformer'];
215
- }> {
214
+ opts: TProcRouterRecord,
215
+ ): Router<
216
+ RouterDef<
217
+ TConfig['ctx'],
218
+ TConfig['errorShape'],
219
+ TConfig['meta'],
220
+ TProcRouterRecord
221
+ >
222
+ > &
223
+ TProcRouterRecord {
224
+ const routerProcedures: Record<string, Procedure<any>> = omitPrototype({});
225
+ function recursiveGetPaths(procedures: ProcedureRouterRecord, path = '') {
226
+ for (const [key, procedureOrRouter] of Object.entries(procedures ?? {})) {
227
+ const newPath = `${path}${key}`;
228
+
229
+ if (typeof procedureOrRouter === 'object') {
230
+ const router = procedureOrRouter as AnyRouter;
231
+ recursiveGetPaths(router._def.procedures, `${newPath}.`);
232
+ continue;
233
+ }
234
+
235
+ routerProcedures[newPath] = procedureOrRouter;
236
+ }
237
+ }
238
+
239
+ recursiveGetPaths(opts);
240
+
216
241
  const result = mergeWithoutOverrides<
217
- RouterDefaultOptions<TSettings['ctx']> &
218
- RouterBuildOptions<TSettings['ctx']>
242
+ RouterDefaultOptions<TConfig['ctx']> & RouterBuildOptions<TConfig['ctx']>
219
243
  >(
220
244
  {
221
245
  transformer: defaults?.transformer ?? defaultTransformer,
222
246
  errorFormatter: defaults?.errorFormatter ?? defaultFormatter,
223
247
  },
224
- {
225
- queries: omitPrototype(procedures.queries),
226
- mutations: omitPrototype(procedures.mutations),
227
- subscriptions: omitPrototype(procedures.subscriptions),
228
- },
248
+ { procedures: routerProcedures },
229
249
  );
230
250
 
231
- const _def: AnyRouterParams<TSettings['ctx']> = {
251
+ const _def: AnyRouterDef<TConfig['ctx']> = {
252
+ router: true,
253
+ procedures: {},
232
254
  ...emptyRouter,
233
255
  ...result,
234
- };
235
- const def = {
236
- _def,
237
- ..._def,
256
+ record: opts,
257
+ queries: Object.entries(result.procedures || {})
258
+ .filter((pair) => (pair[1] as any)._def.query)
259
+ .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
260
+ mutations: Object.entries(result.procedures || {})
261
+ .filter((pair) => (pair[1] as any)._def.mutation)
262
+ .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
263
+ subscriptions: Object.entries(result.procedures || {})
264
+ .filter((pair) => (pair[1] as any)._def.subscription)
265
+ .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
266
+ routers: Object.entries(result.procedures || {})
267
+ .filter((pair) => (pair[1] as any)._def._router)
268
+ .reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
238
269
  };
239
270
 
240
271
  function callProcedure(opts: InternalProcedureCallOptions) {
241
272
  const { type, path } = opts;
242
- const defTarget = PROCEDURE_DEFINITION_MAP[type];
243
- const defs = def[defTarget];
244
273
 
245
- if (!(path in defs)) {
274
+ if (!(path in _def.procedures) || !_def.procedures[path]['_def'][type]) {
246
275
  throw new TRPCError({
247
276
  code: 'NOT_FOUND',
248
277
  message: `No "${type}"-procedure on path "${path}"`,
249
278
  });
250
279
  }
251
- const procedure = defs[path] as InternalProcedure;
280
+
281
+ const procedure = _def.procedures[path] as InternalProcedure;
282
+
252
283
  return procedure(opts);
253
284
  }
254
285
  const router: AnyRouter = {
255
- ...def,
286
+ ...opts,
287
+ _def,
288
+ transformer: _def.transformer,
289
+ errorFormatter: _def.errorFormatter,
256
290
  createCaller(ctx) {
257
291
  return {
258
292
  query: (path, ...args) =>
@@ -329,30 +363,3 @@ export function createRouterFactory<TSettings extends RootConfig>(
329
363
  return router as any;
330
364
  };
331
365
  }
332
-
333
- type combineProcedureRecords<
334
- A extends Partial<AnyRouter>,
335
- B extends Partial<AnyRouter>,
336
- > = {
337
- queries: A['queries'] & B['queries'];
338
- mutations: A['mutations'] & B['mutations'];
339
- subscriptions: A['subscriptions'] & B['subscriptions'];
340
- };
341
- /**
342
- * @internal
343
- */
344
- export type mergeProcedureRecordsVariadic<
345
- Routers extends Partial<AnyRouter>[],
346
- > = Routers extends []
347
- ? {
348
- queries: {};
349
- mutations: {};
350
- subscriptions: {};
351
- }
352
- : Routers extends [infer First, ...infer Rest]
353
- ? First extends Partial<AnyRouter>
354
- ? Rest extends Partial<AnyRouter>[]
355
- ? combineProcedureRecords<First, mergeProcedureRecordsVariadic<Rest>>
356
- : never
357
- : never
358
- : never;
package/src/core/types.ts CHANGED
@@ -1,21 +1,21 @@
1
1
  import { inferObservableValue } from '../observable';
2
- import { inferAsyncReturnType } from '../types';
3
2
  import { Procedure, ProcedureArgs } from './procedure';
4
- import { AnyRouter, AnyRouterParams, Router } from './router';
3
+ import { AnyRouter, AnyRouterDef, Router } from './router';
5
4
 
6
- export type inferRouterParams<TRouter extends AnyRouter> =
7
- TRouter extends Router<infer TParams>
8
- ? TParams extends AnyRouterParams<any>
9
- ? TParams
10
- : never
11
- : never;
5
+ export type inferRouterDef<TRouter extends AnyRouter> = TRouter extends Router<
6
+ infer TParams
7
+ >
8
+ ? TParams extends AnyRouterDef<any>
9
+ ? TParams
10
+ : never
11
+ : never;
12
12
 
13
13
  export type inferRouterContext<TRouter extends AnyRouter> =
14
- inferRouterParams<TRouter>['_ctx'];
14
+ inferRouterDef<TRouter>['_ctx'];
15
15
  export type inferRouterError<TRouter extends AnyRouter> =
16
- inferRouterParams<TRouter>['_errorShape'];
16
+ inferRouterDef<TRouter>['_errorShape'];
17
17
  export type inferRouterMeta<TRouter extends AnyRouter> =
18
- inferRouterParams<TRouter>['_meta'];
18
+ inferRouterDef<TRouter>['_meta'];
19
19
 
20
20
  /**
21
21
  * @public
@@ -23,15 +23,32 @@ export type inferRouterMeta<TRouter extends AnyRouter> =
23
23
  export type ProcedureType = 'query' | 'mutation' | 'subscription';
24
24
 
25
25
  export type inferHandlerInput<TProcedure extends Procedure<any>> =
26
- TProcedure extends Procedure<infer TParams> ? ProcedureArgs<TParams> : never;
26
+ ProcedureArgs<inferProcedureParams<TProcedure>>;
27
27
 
28
28
  export type inferProcedureInput<TProcedure extends Procedure<any>> =
29
29
  inferHandlerInput<TProcedure>[0];
30
30
 
31
- export type inferProcedureOutput<TProcedure extends Procedure<any>> =
32
- inferAsyncReturnType<TProcedure>;
31
+ // /**
32
+ // * @internal
33
+ // */
34
+ // type inferProcedureFn<TProcedure extends Procedure<any>> =
35
+ // TProcedure extends QueryProcedure<any>
36
+ // ? TProcedure['query']
37
+ // : TProcedure extends SubscriptionProcedure<any>
38
+ // ? TProcedure['subscription']
39
+ // : TProcedure extends MutationProcedure<any>
40
+ // ? TProcedure['mutate']
41
+ // : never;
42
+
43
+ export type inferProcedureParams<TProcedure> = TProcedure extends Procedure<any>
44
+ ? TProcedure['_def']
45
+ : never;
46
+ export type inferProcedureOutput<TProcedure> =
47
+ inferProcedureParams<TProcedure>['_output_out'];
33
48
 
34
49
  export type inferSubscriptionOutput<
35
50
  TRouter extends AnyRouter,
36
- TPath extends keyof TRouter['subscriptions'] & string,
37
- > = inferObservableValue<inferProcedureOutput<TRouter['subscriptions'][TPath]>>;
51
+ TPath extends keyof TRouter['_def']['subscriptions'] & string,
52
+ > = inferObservableValue<
53
+ inferProcedureOutput<TRouter['_def']['subscriptions'][TPath]>
54
+ >;