@trpc/server 10.0.0-alpha.28 → 10.0.0-alpha.30
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.
- package/dist/adapters/node-http/types.d.ts +2 -2
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/initTRPC.d.ts +3 -21
- package/dist/core/initTRPC.d.ts.map +1 -1
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +41 -81
- package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
- package/dist/core/internals/config.d.ts +1 -3
- package/dist/core/internals/config.d.ts.map +1 -1
- package/dist/core/internals/internalProcedure.d.ts +18 -1
- package/dist/core/internals/internalProcedure.d.ts.map +1 -1
- package/dist/core/internals/mergeRouters.d.ts.map +1 -1
- package/dist/core/internals/prefixObjectKeys.d.ts +2 -0
- package/dist/core/internals/prefixObjectKeys.d.ts.map +1 -0
- package/dist/core/internals/procedureBuilder.d.ts +35 -8
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
- package/dist/core/middleware.d.ts +8 -7
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/core/procedure.d.ts +14 -2
- package/dist/core/procedure.d.ts.map +1 -1
- package/dist/core/router.d.ts +42 -60
- package/dist/core/router.d.ts.map +1 -1
- package/dist/core/types.d.ts +9 -9
- package/dist/core/types.d.ts.map +1 -1
- package/dist/deprecated/interop.d.ts +8 -24
- package/dist/deprecated/interop.d.ts.map +1 -1
- package/dist/http/internals/types.d.ts +3 -3
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/index.js +94 -58
- package/dist/index.mjs +94 -58
- package/dist/internals/types.d.ts +2 -2
- package/dist/internals/types.d.ts.map +1 -1
- package/dist/observable/observable.d.ts +1 -1
- package/dist/observable/observable.d.ts.map +1 -1
- package/dist/types.d.ts +6 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +2 -2
- package/src/adapters/node-http/types.ts +2 -4
- package/src/core/index.ts +1 -1
- package/src/core/initTRPC.ts +14 -9
- package/src/core/internals/__generated__/mergeRoutersGeneric.ts +284 -324
- package/src/core/internals/config.ts +1 -3
- package/src/core/internals/internalProcedure.ts +52 -21
- package/src/core/internals/mergeRouters.ts +3 -15
- package/src/core/internals/prefixObjectKeys.ts +9 -0
- package/src/core/internals/procedureBuilder.ts +88 -9
- package/src/core/middleware.ts +5 -4
- package/src/core/procedure.ts +25 -2
- package/src/core/router.ts +112 -134
- package/src/core/types.ts +31 -15
- package/src/deprecated/interop.ts +64 -37
- package/src/http/internals/types.ts +3 -3
- package/src/internals/types.ts +2 -2
- package/src/observable/observable.ts +6 -3
- package/src/types.ts +7 -0
- package/dist/core/internals/mergeRoutersGeneric.d.ts +0 -21
- package/dist/core/internals/mergeRoutersGeneric.d.ts.map +0 -1
- package/src/core/internals/mergeRoutersGeneric.ts +0 -35
package/src/core/router.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
+
import { inferProcedureOutput } from '.';
|
|
3
|
+
import { Filter } from '..';
|
|
2
4
|
import { TRPCError } from '../error/TRPCError';
|
|
3
5
|
import {
|
|
4
6
|
DefaultErrorShape,
|
|
@@ -15,28 +17,32 @@ import {
|
|
|
15
17
|
} from './internals/internalProcedure';
|
|
16
18
|
import { mergeWithoutOverrides } from './internals/mergeWithoutOverrides';
|
|
17
19
|
import { omitPrototype } from './internals/omitPrototype';
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
+
import {
|
|
21
|
+
MutationProcedure,
|
|
22
|
+
Procedure,
|
|
23
|
+
QueryProcedure,
|
|
24
|
+
SubscriptionProcedure,
|
|
25
|
+
} from './procedure';
|
|
20
26
|
import { ProcedureType } from './types';
|
|
21
27
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
type
|
|
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:
|
|
27
|
-
mutations:
|
|
28
|
-
subscriptions:
|
|
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
|
|
39
|
+
export interface RouterDef<
|
|
32
40
|
// FIXME this should use RootConfig
|
|
33
41
|
TContext,
|
|
34
42
|
TErrorShape extends TRPCErrorShape<number>,
|
|
35
43
|
TMeta extends Record<string, unknown>,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
TSubscriptions extends ProcedureRecord<TContext>,
|
|
39
|
-
> extends ProcedureStructure {
|
|
44
|
+
TProcedures extends ProcedureRouterRecord,
|
|
45
|
+
> {
|
|
40
46
|
/**
|
|
41
47
|
* @internal
|
|
42
48
|
*/
|
|
@@ -49,114 +55,107 @@ export interface RouterParams<
|
|
|
49
55
|
* @internal
|
|
50
56
|
*/
|
|
51
57
|
_meta: TMeta;
|
|
52
|
-
queries: TQueries;
|
|
53
|
-
mutations: TMutations;
|
|
54
|
-
subscriptions: TSubscriptions;
|
|
55
58
|
errorFormatter: ErrorFormatter<TContext, TErrorShape>;
|
|
56
59
|
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>>;
|
|
57
75
|
}
|
|
58
76
|
|
|
59
|
-
export type
|
|
60
|
-
TContext,
|
|
61
|
-
any,
|
|
62
|
-
any,
|
|
63
|
-
any,
|
|
64
|
-
any,
|
|
65
|
-
any
|
|
66
|
-
>;
|
|
77
|
+
export type AnyRouterDef<TContext = any> = RouterDef<TContext, any, any, any>;
|
|
67
78
|
|
|
68
79
|
/**
|
|
69
80
|
* @internal
|
|
70
81
|
*/
|
|
71
82
|
export type inferHandlerInput<TProcedure extends Procedure<any>> =
|
|
72
|
-
TProcedure extends Procedure<infer
|
|
73
|
-
? undefined extends
|
|
74
|
-
? unknown extends
|
|
83
|
+
TProcedure extends Procedure<infer TDef>
|
|
84
|
+
? undefined extends TDef['_input_in'] // ? is input optional
|
|
85
|
+
? unknown extends TDef['_input_in'] // ? is input unset
|
|
75
86
|
? [(null | undefined)?] // -> there is no input
|
|
76
|
-
: [(
|
|
77
|
-
: [
|
|
87
|
+
: [(TDef['_input_in'] | null | undefined)?] // -> there is optional input
|
|
88
|
+
: [TDef['_input_in']] // -> input is required
|
|
78
89
|
: [(undefined | null)?]; // -> there is no input
|
|
79
90
|
|
|
80
91
|
/**
|
|
81
92
|
* @internal
|
|
82
93
|
*/
|
|
83
|
-
type inferHandlerFn<TProcedures extends
|
|
94
|
+
type inferHandlerFn<TProcedures extends Record<string, Procedure<any>>> = <
|
|
84
95
|
TProcedure extends TProcedures[TPath],
|
|
85
96
|
TPath extends keyof TProcedures & string,
|
|
86
97
|
>(
|
|
87
98
|
path: TPath,
|
|
88
99
|
...args: inferHandlerInput<TProcedure>
|
|
89
|
-
) =>
|
|
100
|
+
) => inferProcedureOutput<TProcedure>;
|
|
90
101
|
|
|
91
102
|
/**
|
|
92
103
|
* This only exists b/c of interop mode
|
|
93
104
|
* @internal
|
|
94
105
|
*/
|
|
95
106
|
|
|
96
|
-
type RouterCaller<
|
|
107
|
+
type RouterCaller<TDef extends AnyRouterDef> = (ctx: TDef['_ctx']) => {
|
|
97
108
|
/**
|
|
98
109
|
* @deprecated
|
|
99
110
|
*/
|
|
100
|
-
query: inferHandlerFn<
|
|
111
|
+
query: inferHandlerFn<TDef['queries']>;
|
|
101
112
|
/**
|
|
102
113
|
* @deprecated
|
|
103
114
|
*/
|
|
104
|
-
mutation: inferHandlerFn<
|
|
115
|
+
mutation: inferHandlerFn<TDef['mutations']>;
|
|
105
116
|
/**
|
|
106
117
|
* @deprecated
|
|
107
118
|
*/
|
|
108
|
-
subscription: inferHandlerFn<
|
|
109
|
-
|
|
110
|
-
queries: TParams['queries'];
|
|
111
|
-
mutations: TParams['mutations'];
|
|
112
|
-
subscriptions: TParams['subscriptions'];
|
|
119
|
+
subscription: inferHandlerFn<TDef['subscriptions']>;
|
|
113
120
|
};
|
|
114
121
|
|
|
115
|
-
export interface Router<
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
TParams['queries'],
|
|
122
|
-
TParams['mutations'],
|
|
123
|
-
TParams['subscriptions']
|
|
122
|
+
export interface Router<TDef extends AnyRouterDef> {
|
|
123
|
+
_def: RouterDef<
|
|
124
|
+
TDef['_ctx'],
|
|
125
|
+
TDef['_errorShape'],
|
|
126
|
+
TDef['_meta'],
|
|
127
|
+
TDef['procedures']
|
|
124
128
|
>;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
transformer: TParams['transformer'];
|
|
129
|
+
/** @deprecated **/
|
|
130
|
+
errorFormatter: TDef['errorFormatter'];
|
|
131
|
+
/** @deprecated **/
|
|
132
|
+
transformer: TDef['transformer'];
|
|
130
133
|
|
|
131
|
-
|
|
134
|
+
// FIXME rename me and deprecate
|
|
135
|
+
createCaller: RouterCaller<TDef>;
|
|
136
|
+
// FIXME rename me and deprecate
|
|
132
137
|
getErrorShape(opts: {
|
|
133
138
|
error: TRPCError;
|
|
134
139
|
type: ProcedureType | 'unknown';
|
|
135
140
|
path: string | undefined;
|
|
136
141
|
input: unknown;
|
|
137
|
-
ctx: undefined |
|
|
138
|
-
}):
|
|
142
|
+
ctx: undefined | TDef['_ctx'];
|
|
143
|
+
}): TDef['_errorShape'];
|
|
139
144
|
}
|
|
140
145
|
|
|
141
|
-
/**
|
|
142
|
-
* @internal
|
|
143
|
-
*/
|
|
144
|
-
export type RouterOptions<TContext> = Partial<AnyRouterParams<TContext>>;
|
|
145
|
-
|
|
146
146
|
/**
|
|
147
147
|
* @internal
|
|
148
148
|
*/
|
|
149
149
|
export type RouterDefaultOptions<TContext> = Pick<
|
|
150
|
-
|
|
150
|
+
AnyRouterDef<TContext>,
|
|
151
151
|
'transformer' | 'errorFormatter'
|
|
152
152
|
>;
|
|
153
153
|
|
|
154
154
|
/**
|
|
155
155
|
* @internal
|
|
156
156
|
*/
|
|
157
|
-
type RouterBuildOptions<TContext> =
|
|
158
|
-
|
|
159
|
-
'queries' | 'subscriptions' | 'mutations'
|
|
157
|
+
export type RouterBuildOptions<TContext> = Partial<
|
|
158
|
+
Pick<AnyRouterDef<TContext>, 'procedures'>
|
|
160
159
|
>;
|
|
161
160
|
|
|
162
161
|
export type AnyRouter = Router<any>;
|
|
@@ -178,77 +177,83 @@ const emptyRouter = {
|
|
|
178
177
|
errorFormatter: defaultFormatter,
|
|
179
178
|
transformer: defaultTransformer,
|
|
180
179
|
};
|
|
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
180
|
|
|
192
181
|
/**
|
|
193
182
|
*
|
|
194
183
|
* @internal
|
|
195
184
|
*/
|
|
196
|
-
export function createRouterFactory<
|
|
197
|
-
defaults?: RouterDefaultOptions<
|
|
185
|
+
export function createRouterFactory<TConfig extends RootConfig>(
|
|
186
|
+
defaults?: RouterDefaultOptions<TConfig['ctx']>,
|
|
198
187
|
) {
|
|
199
|
-
return function createRouterInner<
|
|
200
|
-
TProcedures
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
188
|
+
return function createRouterInner<TProcedures extends ProcedureRouterRecord>(
|
|
189
|
+
procedures: TProcedures,
|
|
190
|
+
): Router<
|
|
191
|
+
RouterDef<
|
|
192
|
+
TConfig['ctx'],
|
|
193
|
+
TConfig['errorShape'],
|
|
194
|
+
TConfig['meta'],
|
|
195
|
+
TProcedures
|
|
196
|
+
>
|
|
197
|
+
> {
|
|
198
|
+
const routerProcedures: Record<string, Procedure<any>> = omitPrototype({});
|
|
199
|
+
function recursiveGetPaths(procedures: ProcedureRouterRecord, path = '') {
|
|
200
|
+
for (const [key, procedureOrRouter] of Object.entries(procedures ?? {})) {
|
|
201
|
+
const newPath = `${path}${key}`;
|
|
202
|
+
|
|
203
|
+
if (typeof procedureOrRouter === 'object') {
|
|
204
|
+
const router = procedureOrRouter as AnyRouter;
|
|
205
|
+
recursiveGetPaths(router._def.procedures, `${newPath}.`);
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
routerProcedures[newPath] = procedureOrRouter;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
recursiveGetPaths(procedures);
|
|
214
|
+
|
|
216
215
|
const result = mergeWithoutOverrides<
|
|
217
|
-
RouterDefaultOptions<
|
|
218
|
-
RouterBuildOptions<TSettings['ctx']>
|
|
216
|
+
RouterDefaultOptions<TConfig['ctx']> & RouterBuildOptions<TConfig['ctx']>
|
|
219
217
|
>(
|
|
220
218
|
{
|
|
221
219
|
transformer: defaults?.transformer ?? defaultTransformer,
|
|
222
220
|
errorFormatter: defaults?.errorFormatter ?? defaultFormatter,
|
|
223
221
|
},
|
|
224
|
-
{
|
|
225
|
-
queries: omitPrototype(procedures.queries),
|
|
226
|
-
mutations: omitPrototype(procedures.mutations),
|
|
227
|
-
subscriptions: omitPrototype(procedures.subscriptions),
|
|
228
|
-
},
|
|
222
|
+
{ procedures: routerProcedures },
|
|
229
223
|
);
|
|
230
224
|
|
|
231
|
-
const _def:
|
|
225
|
+
const _def: AnyRouterDef<TConfig['ctx']> = {
|
|
226
|
+
procedures: {},
|
|
232
227
|
...emptyRouter,
|
|
233
228
|
...result,
|
|
229
|
+
queries: Object.entries(result.procedures || {})
|
|
230
|
+
.filter((pair) => (pair[1] as any)._def.query)
|
|
231
|
+
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
232
|
+
mutations: Object.entries(result.procedures || {})
|
|
233
|
+
.filter((pair) => (pair[1] as any)._def.mutation)
|
|
234
|
+
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
235
|
+
subscriptions: Object.entries(result.procedures || {})
|
|
236
|
+
.filter((pair) => (pair[1] as any)._def.subscription)
|
|
237
|
+
.reduce((acc, [key, val]) => ({ ...acc, [key]: val }), {}),
|
|
234
238
|
};
|
|
235
239
|
const def = {
|
|
236
240
|
_def,
|
|
237
|
-
|
|
241
|
+
transformer: _def.transformer,
|
|
242
|
+
errorFormatter: _def.errorFormatter,
|
|
238
243
|
};
|
|
239
244
|
|
|
240
245
|
function callProcedure(opts: InternalProcedureCallOptions) {
|
|
241
246
|
const { type, path } = opts;
|
|
242
|
-
const defTarget = PROCEDURE_DEFINITION_MAP[type];
|
|
243
|
-
const defs = def[defTarget];
|
|
244
247
|
|
|
245
|
-
if (!(path in
|
|
248
|
+
if (!(path in _def.procedures) || !_def.procedures[path]['_def'][type]) {
|
|
246
249
|
throw new TRPCError({
|
|
247
250
|
code: 'NOT_FOUND',
|
|
248
251
|
message: `No "${type}"-procedure on path "${path}"`,
|
|
249
252
|
});
|
|
250
253
|
}
|
|
251
|
-
|
|
254
|
+
|
|
255
|
+
const procedure = _def.procedures[path] as InternalProcedure;
|
|
256
|
+
|
|
252
257
|
return procedure(opts);
|
|
253
258
|
}
|
|
254
259
|
const router: AnyRouter = {
|
|
@@ -329,30 +334,3 @@ export function createRouterFactory<TSettings extends RootConfig>(
|
|
|
329
334
|
return router as any;
|
|
330
335
|
};
|
|
331
336
|
}
|
|
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,
|
|
3
|
+
import { AnyRouter, AnyRouterDef, Router } from './router';
|
|
5
4
|
|
|
6
|
-
export type
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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
|
-
|
|
14
|
+
inferRouterDef<TRouter>['_ctx'];
|
|
15
15
|
export type inferRouterError<TRouter extends AnyRouter> =
|
|
16
|
-
|
|
16
|
+
inferRouterDef<TRouter>['_errorShape'];
|
|
17
17
|
export type inferRouterMeta<TRouter extends AnyRouter> =
|
|
18
|
-
|
|
18
|
+
inferRouterDef<TRouter>['_meta'];
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* @public
|
|
@@ -23,15 +23,31 @@ 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
|
-
|
|
26
|
+
ProcedureArgs<inferProcedureParams<TProcedure>>;
|
|
27
27
|
|
|
28
28
|
export type inferProcedureInput<TProcedure extends Procedure<any>> =
|
|
29
29
|
inferHandlerInput<TProcedure>[0];
|
|
30
30
|
|
|
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 extends Procedure<any>> =
|
|
44
|
+
TProcedure extends Procedure<infer $Params> ? $Params : never;
|
|
31
45
|
export type inferProcedureOutput<TProcedure extends Procedure<any>> =
|
|
32
|
-
|
|
46
|
+
inferProcedureParams<TProcedure>['_output_out'];
|
|
33
47
|
|
|
34
48
|
export type inferSubscriptionOutput<
|
|
35
49
|
TRouter extends AnyRouter,
|
|
36
|
-
TPath extends keyof TRouter['subscriptions'] & string,
|
|
37
|
-
> = inferObservableValue<
|
|
50
|
+
TPath extends keyof TRouter['_def']['subscriptions'] & string,
|
|
51
|
+
> = inferObservableValue<
|
|
52
|
+
inferProcedureOutput<TRouter['_def']['subscriptions'][TPath]>
|
|
53
|
+
>;
|
|
@@ -1,22 +1,33 @@
|
|
|
1
|
+
import { ProcedureParams, ProcedureType } from '..';
|
|
1
2
|
import { getParseFnOrPassThrough } from '../core/internals/getParseFn';
|
|
2
3
|
import {
|
|
3
4
|
createInputMiddleware,
|
|
4
5
|
createInternalBuilder,
|
|
5
6
|
createOutputMiddleware,
|
|
6
7
|
} from '../core/internals/internalProcedure';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
8
|
+
import { mergeWithoutOverrides } from '../core/internals/mergeWithoutOverrides';
|
|
9
|
+
import {
|
|
10
|
+
MutationProcedure,
|
|
11
|
+
Procedure as NewProcedure,
|
|
12
|
+
QueryProcedure,
|
|
13
|
+
SubscriptionProcedure,
|
|
14
|
+
} from '../core/procedure';
|
|
15
|
+
import {
|
|
16
|
+
Router as NewRouter,
|
|
17
|
+
RouterDef,
|
|
18
|
+
createRouterFactory,
|
|
19
|
+
} from '../core/router';
|
|
9
20
|
import {
|
|
10
21
|
AnyRouter as AnyOldRouter,
|
|
11
22
|
Router as OldRouter,
|
|
12
23
|
} from '../deprecated/router';
|
|
13
24
|
import { TRPCErrorShape } from '../rpc';
|
|
14
|
-
import { CombinedDataTransformer } from '../transformer';
|
|
15
25
|
import { Procedure as OldProcedure } from './internals/procedure';
|
|
16
26
|
import { ProcedureRecord } from './router';
|
|
17
27
|
|
|
18
28
|
type AnyOldProcedure = OldProcedure<any, any, any, any, any, any, any, any>;
|
|
19
|
-
|
|
29
|
+
|
|
30
|
+
type convertProcedureParams<TProcedure extends AnyOldProcedure> =
|
|
20
31
|
TProcedure extends OldProcedure<
|
|
21
32
|
infer TInputContext,
|
|
22
33
|
infer TContext,
|
|
@@ -27,19 +38,33 @@ type MigrateProcedure<TProcedure extends AnyOldProcedure> =
|
|
|
27
38
|
infer _TParsedOutput,
|
|
28
39
|
infer TFinalInput
|
|
29
40
|
>
|
|
30
|
-
?
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
41
|
+
? ProcedureParams<
|
|
42
|
+
TInputContext,
|
|
43
|
+
TContext,
|
|
44
|
+
TInput,
|
|
45
|
+
TParsedInput,
|
|
46
|
+
TOutput,
|
|
47
|
+
TFinalInput,
|
|
48
|
+
TMeta
|
|
49
|
+
>
|
|
39
50
|
: never;
|
|
40
51
|
|
|
41
|
-
|
|
42
|
-
|
|
52
|
+
type MigrateProcedure<
|
|
53
|
+
TProcedure extends AnyOldProcedure,
|
|
54
|
+
TType extends ProcedureType,
|
|
55
|
+
> = TType extends 'query'
|
|
56
|
+
? QueryProcedure<convertProcedureParams<TProcedure>>
|
|
57
|
+
: TType extends 'mutation'
|
|
58
|
+
? MutationProcedure<convertProcedureParams<TProcedure>>
|
|
59
|
+
: TType extends 'subscription'
|
|
60
|
+
? SubscriptionProcedure<convertProcedureParams<TProcedure>>
|
|
61
|
+
: never;
|
|
62
|
+
|
|
63
|
+
export type MigrateProcedureRecord<
|
|
64
|
+
T extends ProcedureRecord<any>,
|
|
65
|
+
TType extends ProcedureType,
|
|
66
|
+
> = {
|
|
67
|
+
[K in keyof T]: MigrateProcedure<T[K], TType>;
|
|
43
68
|
};
|
|
44
69
|
|
|
45
70
|
export type MigrateRouter<
|
|
@@ -74,16 +99,16 @@ export type MigrateRouter<
|
|
|
74
99
|
unknown
|
|
75
100
|
>,
|
|
76
101
|
TErrorShape extends TRPCErrorShape<number>,
|
|
77
|
-
> = NewRouter<
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
102
|
+
> = NewRouter<
|
|
103
|
+
RouterDef<
|
|
104
|
+
TInputContext,
|
|
105
|
+
TErrorShape,
|
|
106
|
+
TMeta,
|
|
107
|
+
MigrateProcedureRecord<TQueries, 'query'> &
|
|
108
|
+
MigrateProcedureRecord<TMutations, 'mutation'> &
|
|
109
|
+
MigrateProcedureRecord<TSubscriptions, 'subscription'>
|
|
110
|
+
>
|
|
111
|
+
>;
|
|
87
112
|
|
|
88
113
|
export type MigrateOldRouter<TRouter extends AnyOldRouter> =
|
|
89
114
|
TRouter extends OldRouter<
|
|
@@ -106,9 +131,10 @@ export type MigrateOldRouter<TRouter extends AnyOldRouter> =
|
|
|
106
131
|
>
|
|
107
132
|
: never;
|
|
108
133
|
|
|
109
|
-
function migrateProcedure<
|
|
110
|
-
|
|
111
|
-
|
|
134
|
+
function migrateProcedure<
|
|
135
|
+
TProcedure extends AnyOldProcedure,
|
|
136
|
+
TType extends ProcedureType,
|
|
137
|
+
>(oldProc: TProcedure, type: TType): MigrateProcedure<TProcedure, TType> {
|
|
112
138
|
const def = oldProc._def();
|
|
113
139
|
|
|
114
140
|
const inputParser = getParseFnOrPassThrough(def.inputParser);
|
|
@@ -125,9 +151,12 @@ function migrateProcedure<TProcedure extends AnyOldProcedure>(
|
|
|
125
151
|
],
|
|
126
152
|
meta: def.meta,
|
|
127
153
|
output: def.outputParser,
|
|
154
|
+
mutation: type === 'mutation',
|
|
155
|
+
query: type === 'query',
|
|
156
|
+
subscription: type === 'subscription',
|
|
128
157
|
});
|
|
129
158
|
|
|
130
|
-
const proc = builder
|
|
159
|
+
const proc = builder[type]((opts) => def.resolver(opts as any));
|
|
131
160
|
|
|
132
161
|
return proc as any;
|
|
133
162
|
}
|
|
@@ -143,27 +172,25 @@ export function migrateRouter<TOldRouter extends AnyOldRouter>(
|
|
|
143
172
|
const mutations: ProcRecord = {};
|
|
144
173
|
const subscriptions: ProcRecord = {};
|
|
145
174
|
for (const [name, procedure] of Object.entries(oldRouter._def.queries)) {
|
|
146
|
-
queries[name] = migrateProcedure(procedure as any);
|
|
175
|
+
queries[name] = migrateProcedure(procedure as any, 'query');
|
|
147
176
|
}
|
|
148
177
|
|
|
149
178
|
for (const [name, procedure] of Object.entries(oldRouter._def.mutations)) {
|
|
150
|
-
mutations[name] = migrateProcedure(procedure as any);
|
|
179
|
+
mutations[name] = migrateProcedure(procedure as any, 'mutation');
|
|
151
180
|
}
|
|
152
181
|
|
|
153
182
|
for (const [name, procedure] of Object.entries(
|
|
154
183
|
oldRouter._def.subscriptions,
|
|
155
184
|
)) {
|
|
156
|
-
subscriptions[name] = migrateProcedure(procedure as any);
|
|
185
|
+
subscriptions[name] = migrateProcedure(procedure as any, 'subscription');
|
|
157
186
|
}
|
|
158
187
|
|
|
188
|
+
const procedures = mergeWithoutOverrides(queries, mutations, subscriptions);
|
|
189
|
+
|
|
159
190
|
const newRouter = createRouterFactory<any>({
|
|
160
191
|
transformer,
|
|
161
192
|
errorFormatter,
|
|
162
|
-
})(
|
|
163
|
-
mutations,
|
|
164
|
-
queries,
|
|
165
|
-
subscriptions,
|
|
166
|
-
});
|
|
193
|
+
})(procedures);
|
|
167
194
|
|
|
168
195
|
return newRouter as any;
|
|
169
196
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AnyRouter, ProcedureType,
|
|
1
|
+
import { AnyRouter, ProcedureType, inferRouterDef } from '../../core';
|
|
2
2
|
import { TRPCError } from '../../error/TRPCError';
|
|
3
3
|
import { BaseHandlerOptions } from '../../internals/types';
|
|
4
4
|
import { TRPCResponse } from '../../rpc';
|
|
@@ -24,8 +24,8 @@ export interface HTTPRequest {
|
|
|
24
24
|
* @internal
|
|
25
25
|
*/
|
|
26
26
|
export type ResponseMetaFn<TRouter extends AnyRouter> = (opts: {
|
|
27
|
-
data: TRPCResponse<unknown,
|
|
28
|
-
ctx?:
|
|
27
|
+
data: TRPCResponse<unknown, inferRouterDef<TRouter>['_error']>[];
|
|
28
|
+
ctx?: inferRouterDef<TRouter>['_ctx'];
|
|
29
29
|
/**
|
|
30
30
|
* The different tRPC paths requested
|
|
31
31
|
**/
|
package/src/internals/types.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { inferRouterDef } from '../core';
|
|
2
2
|
import { ProcedureType } from '../core';
|
|
3
3
|
import { AnyRouter } from '../core/router';
|
|
4
4
|
import { TRPCError } from '../error/TRPCError';
|
|
@@ -20,5 +20,5 @@ export type OnErrorFunction<TRouter extends AnyRouter, TRequest> = (opts: {
|
|
|
20
20
|
path: string | undefined;
|
|
21
21
|
req: TRequest;
|
|
22
22
|
input: unknown;
|
|
23
|
-
ctx: undefined |
|
|
23
|
+
ctx: undefined | inferRouterDef<TRouter>['_ctx'];
|
|
24
24
|
}) => void;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { pipeFromArray } from './internals/pipe';
|
|
2
2
|
import { Observable, Observer, OperatorFunction, TeardownLogic } from './types';
|
|
3
3
|
|
|
4
|
-
export type inferObservableValue<
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
export type inferObservableValue<TObservable> = TObservable extends Observable<
|
|
5
|
+
infer TValue,
|
|
6
|
+
unknown
|
|
7
|
+
>
|
|
8
|
+
? TValue
|
|
9
|
+
: never;
|
|
7
10
|
|
|
8
11
|
export function isObservable(x: unknown): x is Observable<unknown, unknown> {
|
|
9
12
|
return typeof x === 'object' && x !== null && 'subscribe' in x;
|
package/src/types.ts
CHANGED
|
@@ -65,3 +65,10 @@ export type InferLast<T> = T & { [K in keyof T]: T[K] };
|
|
|
65
65
|
*/
|
|
66
66
|
export type inferAsyncReturnType<TFunction extends (...args: any) => any> =
|
|
67
67
|
ThenArg<ReturnType<TFunction>>;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @internal
|
|
71
|
+
*/
|
|
72
|
+
export type Filter<T extends Record<any, any> | any[], K> = {
|
|
73
|
+
[TKey in keyof T]: T[TKey] extends K ? T[TKey] : never;
|
|
74
|
+
};
|