@trpc/server 11.0.0-next-alpha.133 → 11.0.0-next-alpha.139
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/bundle-analysis.json +21 -21
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/initTRPC.d.ts +2 -41
- package/dist/core/initTRPC.d.ts.map +1 -1
- package/dist/core/internals/procedureBuilder.d.ts +46 -60
- package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
- package/dist/core/internals/utils.d.ts +1 -9
- package/dist/core/internals/utils.d.ts.map +1 -1
- package/dist/core/middleware.d.ts +33 -85
- package/dist/core/middleware.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.mjs +2 -2
- package/dist/{procedureBuilder-0ee77d9c.js → procedureBuilder-416387b5.js} +10 -9
- package/dist/{procedureBuilder-88b211d2.mjs → procedureBuilder-5431b157.mjs} +10 -9
- package/dist/{procedureBuilder-c990500b.js → procedureBuilder-e77bab71.js} +11 -13
- package/dist/unstableInternalsExport.js +1 -1
- package/dist/unstableInternalsExport.mjs +1 -1
- package/package.json +2 -2
- package/src/core/index.ts +0 -1
- package/src/core/initTRPC.ts +8 -2
- package/src/core/internals/procedureBuilder.ts +173 -104
- package/src/core/internals/utils.ts +1 -14
- package/src/core/middleware.ts +114 -163
- package/dist/core/internals/builderTypes.d.ts +0 -14
- package/dist/core/internals/builderTypes.d.ts.map +0 -1
- package/src/core/internals/builderTypes.ts +0 -14
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { getTRPCErrorFromUnknown, TRPCError } from '../../error/TRPCError';
|
|
2
2
|
import { MaybePromise, Simplify } from '../../types';
|
|
3
3
|
import {
|
|
4
|
+
AnyMiddlewareFunction,
|
|
4
5
|
createInputMiddleware,
|
|
5
6
|
createOutputMiddleware,
|
|
6
7
|
MiddlewareBuilder,
|
|
@@ -18,8 +19,6 @@ import {
|
|
|
18
19
|
SubscriptionProcedure,
|
|
19
20
|
} from '../procedure';
|
|
20
21
|
import { ProcedureType } from '../types';
|
|
21
|
-
import { AnyProcedureBuilderParams } from './builderTypes';
|
|
22
|
-
import { AnyRootConfig } from './config';
|
|
23
22
|
import { getParseFn } from './getParseFn';
|
|
24
23
|
import { mergeWithoutOverrides } from './mergeWithoutOverrides';
|
|
25
24
|
import {
|
|
@@ -27,38 +26,22 @@ import {
|
|
|
27
26
|
GetRawInputFn,
|
|
28
27
|
middlewareMarker,
|
|
29
28
|
Overwrite,
|
|
30
|
-
ResolveOptions,
|
|
31
29
|
UnsetMarker,
|
|
32
30
|
} from './utils';
|
|
33
31
|
|
|
34
|
-
type
|
|
35
|
-
TPrev extends AnyProcedureBuilderParams,
|
|
36
|
-
TNext extends AnyProcedureBuilderParams,
|
|
37
|
-
> = ProcedureBuilder<{
|
|
38
|
-
_config: TPrev['_config'];
|
|
39
|
-
_meta: TPrev['_meta'];
|
|
40
|
-
_ctx_out: Overwrite<TPrev['_ctx_out'], TNext['_ctx_out']>;
|
|
41
|
-
_input_in: TPrev['_input_in'];
|
|
42
|
-
_input_out: UnsetMarker extends TNext['_input_out']
|
|
43
|
-
? TPrev['_input_out']
|
|
44
|
-
: Overwrite<TPrev['_input_out'], TNext['_input_out']>;
|
|
45
|
-
_output_in: DefaultValue<TNext['_output_in'], TPrev['_output_in']>;
|
|
46
|
-
_output_out: DefaultValue<TNext['_output_out'], TPrev['_output_out']>;
|
|
47
|
-
}>;
|
|
48
|
-
|
|
49
|
-
type OverwriteIfDefined<TType, TWith> = UnsetMarker extends TType
|
|
32
|
+
type IntersectIfDefined<TType, TWith> = UnsetMarker extends TType
|
|
50
33
|
? TWith
|
|
51
34
|
: Simplify<TType & TWith>;
|
|
52
35
|
|
|
53
36
|
type ErrorMessage<TMessage extends string> = TMessage;
|
|
54
37
|
|
|
55
|
-
export type ProcedureBuilderDef<
|
|
38
|
+
export type ProcedureBuilderDef<TMeta> = {
|
|
56
39
|
procedure: true;
|
|
57
40
|
inputs: Parser[];
|
|
58
41
|
output?: Parser;
|
|
59
|
-
meta?:
|
|
42
|
+
meta?: TMeta;
|
|
60
43
|
resolver?: ProcedureBuilderResolver;
|
|
61
|
-
middlewares:
|
|
44
|
+
middlewares: AnyMiddlewareFunction[];
|
|
62
45
|
mutation?: boolean;
|
|
63
46
|
query?: boolean;
|
|
64
47
|
subscription?: boolean;
|
|
@@ -66,117 +49,191 @@ export type ProcedureBuilderDef<TParams extends AnyProcedureBuilderParams> = {
|
|
|
66
49
|
|
|
67
50
|
export type AnyProcedureBuilderDef = ProcedureBuilderDef<any>;
|
|
68
51
|
|
|
69
|
-
|
|
52
|
+
/**
|
|
53
|
+
* Procedure resolver options
|
|
54
|
+
* @internal
|
|
55
|
+
*/
|
|
56
|
+
interface ResolverOptions<TContext, _TMeta, TContextOverridesIn, TInputOut> {
|
|
57
|
+
ctx: Simplify<Overwrite<TContext, TContextOverridesIn>>;
|
|
58
|
+
input: TInputOut extends UnsetMarker ? undefined : TInputOut;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* A procedure resolver
|
|
63
|
+
*/
|
|
64
|
+
type ProcedureResolver<
|
|
65
|
+
TContext,
|
|
66
|
+
_TMeta,
|
|
67
|
+
TContextOverrides,
|
|
68
|
+
TInputOut,
|
|
69
|
+
TOutputIn,
|
|
70
|
+
TOutputOut,
|
|
71
|
+
> = (opts: {
|
|
72
|
+
ctx: Simplify<Overwrite<TContext, TContextOverrides>>;
|
|
73
|
+
input: TInputOut extends UnsetMarker ? undefined : TInputOut;
|
|
74
|
+
}) => MaybePromise<DefaultValue<TOutputIn, TOutputOut>>;
|
|
75
|
+
|
|
76
|
+
type AnyResolver = ProcedureResolver<any, any, any, any, any, any>;
|
|
77
|
+
export interface ProcedureBuilder<
|
|
78
|
+
TContext,
|
|
79
|
+
TMeta,
|
|
80
|
+
TContextOverrides,
|
|
81
|
+
TInputIn,
|
|
82
|
+
TInputOut,
|
|
83
|
+
TOutputIn,
|
|
84
|
+
TOutputOut,
|
|
85
|
+
> {
|
|
70
86
|
/**
|
|
71
87
|
* Add an input parser to the procedure.
|
|
88
|
+
* @see https://trpc.io/docs/server/validators
|
|
72
89
|
*/
|
|
73
90
|
input<$Parser extends Parser>(
|
|
74
|
-
schema:
|
|
91
|
+
schema: TInputOut extends UnsetMarker
|
|
75
92
|
? $Parser
|
|
76
93
|
: inferParser<$Parser>['out'] extends Record<string, unknown> | undefined
|
|
77
|
-
?
|
|
94
|
+
? TInputOut extends Record<string, unknown> | undefined
|
|
78
95
|
? undefined extends inferParser<$Parser>['out'] // if current is optional the previous must be too
|
|
79
|
-
? undefined extends
|
|
96
|
+
? undefined extends TInputOut
|
|
80
97
|
? $Parser
|
|
81
98
|
: ErrorMessage<'Cannot chain an optional parser to a required parser'>
|
|
82
99
|
: $Parser
|
|
83
100
|
: ErrorMessage<'All input parsers did not resolve to an object'>
|
|
84
101
|
: ErrorMessage<'All input parsers did not resolve to an object'>,
|
|
85
|
-
): ProcedureBuilder<
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
TParams['_input_out'],
|
|
95
|
-
inferParser<$Parser>['out']
|
|
96
|
-
>;
|
|
97
|
-
|
|
98
|
-
_output_in: TParams['_output_in'];
|
|
99
|
-
_output_out: TParams['_output_out'];
|
|
100
|
-
}>;
|
|
102
|
+
): ProcedureBuilder<
|
|
103
|
+
TContext,
|
|
104
|
+
TMeta,
|
|
105
|
+
TContextOverrides,
|
|
106
|
+
IntersectIfDefined<TInputIn, inferParser<$Parser>['in']>,
|
|
107
|
+
IntersectIfDefined<TInputOut, inferParser<$Parser>['out']>,
|
|
108
|
+
TOutputIn,
|
|
109
|
+
TOutputOut
|
|
110
|
+
>;
|
|
101
111
|
/**
|
|
102
112
|
* Add an output parser to the procedure.
|
|
113
|
+
* @see https://trpc.io/docs/server/validators
|
|
103
114
|
*/
|
|
104
115
|
output<$Parser extends Parser>(
|
|
105
116
|
schema: $Parser,
|
|
106
|
-
): ProcedureBuilder<
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
117
|
+
): ProcedureBuilder<
|
|
118
|
+
TContext,
|
|
119
|
+
TMeta,
|
|
120
|
+
TContextOverrides,
|
|
121
|
+
TInputIn,
|
|
122
|
+
TInputOut,
|
|
123
|
+
IntersectIfDefined<TOutputIn, inferParser<$Parser>['in']>,
|
|
124
|
+
IntersectIfDefined<TOutputOut, inferParser<$Parser>['out']>
|
|
125
|
+
>;
|
|
115
126
|
/**
|
|
116
127
|
* Add a meta data to the procedure.
|
|
128
|
+
* @see https://trpc.io/docs/server/metadata
|
|
117
129
|
*/
|
|
118
|
-
meta(
|
|
130
|
+
meta(
|
|
131
|
+
meta: TMeta,
|
|
132
|
+
): ProcedureBuilder<
|
|
133
|
+
TContext,
|
|
134
|
+
TMeta,
|
|
135
|
+
TContextOverrides,
|
|
136
|
+
TInputIn,
|
|
137
|
+
TInputOut,
|
|
138
|
+
TOutputIn,
|
|
139
|
+
TOutputOut
|
|
140
|
+
>;
|
|
119
141
|
/**
|
|
120
142
|
* Add a middleware to the procedure.
|
|
143
|
+
* @see https://trpc.io/docs/server/middlewares
|
|
121
144
|
*/
|
|
122
|
-
use<$
|
|
145
|
+
use<$ContextOverridesOut>(
|
|
123
146
|
fn:
|
|
124
|
-
| MiddlewareBuilder<
|
|
125
|
-
|
|
126
|
-
|
|
147
|
+
| MiddlewareBuilder<
|
|
148
|
+
Overwrite<TContext, TContextOverrides>,
|
|
149
|
+
TMeta,
|
|
150
|
+
$ContextOverridesOut,
|
|
151
|
+
TInputIn
|
|
152
|
+
>
|
|
153
|
+
| MiddlewareFunction<
|
|
154
|
+
TContext,
|
|
155
|
+
TMeta,
|
|
156
|
+
TContextOverrides,
|
|
157
|
+
$ContextOverridesOut,
|
|
158
|
+
TInputIn
|
|
159
|
+
>,
|
|
160
|
+
): ProcedureBuilder<
|
|
161
|
+
TContext,
|
|
162
|
+
TMeta,
|
|
163
|
+
Overwrite<TContextOverrides, $ContextOverridesOut>,
|
|
164
|
+
TInputIn,
|
|
165
|
+
TInputOut,
|
|
166
|
+
TOutputIn,
|
|
167
|
+
TOutputOut
|
|
168
|
+
>;
|
|
127
169
|
/**
|
|
128
170
|
* Query procedure
|
|
171
|
+
* @see https://trpc.io/docs/concepts#vocabulary
|
|
129
172
|
*/
|
|
130
173
|
query<$Output>(
|
|
131
|
-
resolver:
|
|
132
|
-
|
|
133
|
-
|
|
174
|
+
resolver: ProcedureResolver<
|
|
175
|
+
TContext,
|
|
176
|
+
TMeta,
|
|
177
|
+
TContextOverrides,
|
|
178
|
+
TInputOut,
|
|
179
|
+
TOutputIn,
|
|
180
|
+
$Output
|
|
181
|
+
>,
|
|
134
182
|
): QueryProcedure<{
|
|
135
|
-
input: DefaultValue<
|
|
136
|
-
output: DefaultValue<
|
|
183
|
+
input: DefaultValue<TInputIn, void>;
|
|
184
|
+
output: DefaultValue<TOutputOut, $Output>;
|
|
137
185
|
}>;
|
|
138
186
|
|
|
139
187
|
/**
|
|
140
188
|
* Mutation procedure
|
|
189
|
+
* @see https://trpc.io/docs/concepts#vocabulary
|
|
141
190
|
*/
|
|
142
191
|
mutation<$Output>(
|
|
143
|
-
resolver:
|
|
144
|
-
|
|
145
|
-
|
|
192
|
+
resolver: ProcedureResolver<
|
|
193
|
+
TContext,
|
|
194
|
+
TMeta,
|
|
195
|
+
TContextOverrides,
|
|
196
|
+
TInputOut,
|
|
197
|
+
TOutputIn,
|
|
198
|
+
$Output
|
|
199
|
+
>,
|
|
146
200
|
): MutationProcedure<{
|
|
147
|
-
input: DefaultValue<
|
|
148
|
-
output: DefaultValue<
|
|
201
|
+
input: DefaultValue<TInputIn, void>;
|
|
202
|
+
output: DefaultValue<TOutputOut, $Output>;
|
|
149
203
|
}>;
|
|
150
204
|
|
|
151
205
|
/**
|
|
152
|
-
*
|
|
206
|
+
* Subscription procedure
|
|
207
|
+
* @see https://trpc.io/docs/concepts#vocabulary
|
|
153
208
|
*/
|
|
154
209
|
subscription<$Output>(
|
|
155
|
-
resolver:
|
|
156
|
-
|
|
157
|
-
|
|
210
|
+
resolver: ProcedureResolver<
|
|
211
|
+
TContext,
|
|
212
|
+
TMeta,
|
|
213
|
+
TContextOverrides,
|
|
214
|
+
TInputOut,
|
|
215
|
+
TOutputIn,
|
|
216
|
+
$Output
|
|
217
|
+
>,
|
|
158
218
|
): SubscriptionProcedure<{
|
|
159
|
-
input: DefaultValue<
|
|
160
|
-
output: DefaultValue<
|
|
219
|
+
input: DefaultValue<TInputIn, void>;
|
|
220
|
+
output: DefaultValue<TOutputOut, $Output>;
|
|
161
221
|
}>;
|
|
162
222
|
/**
|
|
163
223
|
* @internal
|
|
164
224
|
*/
|
|
165
|
-
_def: ProcedureBuilderDef<
|
|
225
|
+
_def: ProcedureBuilderDef<TMeta>;
|
|
166
226
|
}
|
|
167
227
|
|
|
168
|
-
type AnyProcedureBuilder = ProcedureBuilder<any>;
|
|
169
|
-
|
|
170
|
-
export type ProcedureBuilderMiddleware = MiddlewareFunction<any, any>;
|
|
171
|
-
|
|
228
|
+
type AnyProcedureBuilder = ProcedureBuilder<any, any, any, any, any, any, any>;
|
|
172
229
|
export type ProcedureBuilderResolver = (
|
|
173
|
-
opts:
|
|
230
|
+
opts: ResolverOptions<any, any, any, any>,
|
|
174
231
|
) => Promise<unknown>;
|
|
175
232
|
|
|
176
233
|
function createNewBuilder(
|
|
177
234
|
def1: AnyProcedureBuilderDef,
|
|
178
235
|
def2: Partial<AnyProcedureBuilderDef>,
|
|
179
|
-
) {
|
|
236
|
+
): AnyProcedureBuilder {
|
|
180
237
|
const { middlewares = [], inputs, meta, ...rest } = def2;
|
|
181
238
|
|
|
182
239
|
// TODO: maybe have a fn here to warn about calls
|
|
@@ -188,17 +245,17 @@ function createNewBuilder(
|
|
|
188
245
|
});
|
|
189
246
|
}
|
|
190
247
|
|
|
191
|
-
export function createBuilder<
|
|
248
|
+
export function createBuilder<TContext, TMeta>(
|
|
192
249
|
initDef: Partial<AnyProcedureBuilderDef> = {},
|
|
193
|
-
): ProcedureBuilder<
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
250
|
+
): ProcedureBuilder<
|
|
251
|
+
TContext,
|
|
252
|
+
TMeta,
|
|
253
|
+
object,
|
|
254
|
+
UnsetMarker,
|
|
255
|
+
UnsetMarker,
|
|
256
|
+
UnsetMarker,
|
|
257
|
+
UnsetMarker
|
|
258
|
+
> {
|
|
202
259
|
const _def: AnyProcedureBuilderDef = {
|
|
203
260
|
procedure: true,
|
|
204
261
|
inputs: [],
|
|
@@ -206,26 +263,36 @@ export function createBuilder<TConfig extends AnyRootConfig>(
|
|
|
206
263
|
...initDef,
|
|
207
264
|
};
|
|
208
265
|
|
|
209
|
-
|
|
266
|
+
type AnyProcedureBuilder = ProcedureBuilder<
|
|
267
|
+
any,
|
|
268
|
+
any,
|
|
269
|
+
any,
|
|
270
|
+
any,
|
|
271
|
+
any,
|
|
272
|
+
any,
|
|
273
|
+
any
|
|
274
|
+
>;
|
|
275
|
+
|
|
276
|
+
const builder: AnyProcedureBuilder = {
|
|
210
277
|
_def,
|
|
211
278
|
input(input) {
|
|
212
|
-
const parser = getParseFn(input);
|
|
279
|
+
const parser = getParseFn(input as Parser);
|
|
213
280
|
return createNewBuilder(_def, {
|
|
214
|
-
inputs: [input],
|
|
281
|
+
inputs: [input as Parser],
|
|
215
282
|
middlewares: [createInputMiddleware(parser)],
|
|
216
|
-
})
|
|
283
|
+
});
|
|
217
284
|
},
|
|
218
285
|
output(output: Parser) {
|
|
219
|
-
const
|
|
286
|
+
const parser = getParseFn(output);
|
|
220
287
|
return createNewBuilder(_def, {
|
|
221
288
|
output,
|
|
222
|
-
middlewares: [createOutputMiddleware(
|
|
223
|
-
})
|
|
289
|
+
middlewares: [createOutputMiddleware(parser)],
|
|
290
|
+
});
|
|
224
291
|
},
|
|
225
292
|
meta(meta) {
|
|
226
293
|
return createNewBuilder(_def, {
|
|
227
|
-
meta
|
|
228
|
-
})
|
|
294
|
+
meta,
|
|
295
|
+
});
|
|
229
296
|
},
|
|
230
297
|
use(middlewareBuilderOrFn) {
|
|
231
298
|
// Distinguish between a middleware builder and a middleware function
|
|
@@ -235,8 +302,8 @@ export function createBuilder<TConfig extends AnyRootConfig>(
|
|
|
235
302
|
: [middlewareBuilderOrFn];
|
|
236
303
|
|
|
237
304
|
return createNewBuilder(_def, {
|
|
238
|
-
middlewares: middlewares
|
|
239
|
-
})
|
|
305
|
+
middlewares: middlewares,
|
|
306
|
+
});
|
|
240
307
|
},
|
|
241
308
|
query(resolver) {
|
|
242
309
|
return createResolver(
|
|
@@ -257,11 +324,13 @@ export function createBuilder<TConfig extends AnyRootConfig>(
|
|
|
257
324
|
) as AnySubscriptionProcedure;
|
|
258
325
|
},
|
|
259
326
|
};
|
|
327
|
+
|
|
328
|
+
return builder;
|
|
260
329
|
}
|
|
261
330
|
|
|
262
331
|
function createResolver(
|
|
263
332
|
_def: AnyProcedureBuilderDef & { type: ProcedureType },
|
|
264
|
-
resolver:
|
|
333
|
+
resolver: AnyResolver,
|
|
265
334
|
) {
|
|
266
335
|
const finalBuilder = createNewBuilder(_def, {
|
|
267
336
|
resolver,
|
|
@@ -311,7 +380,7 @@ function createProcedureCaller(_def: AnyProcedureBuilderDef): AnyProcedure {
|
|
|
311
380
|
}
|
|
312
381
|
|
|
313
382
|
// run the middlewares recursively with the resolver as the last one
|
|
314
|
-
|
|
383
|
+
async function callRecursive(
|
|
315
384
|
callOpts: {
|
|
316
385
|
ctx: any;
|
|
317
386
|
index: number;
|
|
@@ -321,7 +390,7 @@ function createProcedureCaller(_def: AnyProcedureBuilderDef): AnyProcedure {
|
|
|
321
390
|
index: 0,
|
|
322
391
|
ctx: opts.ctx,
|
|
323
392
|
},
|
|
324
|
-
): Promise<MiddlewareResult<any>>
|
|
393
|
+
): Promise<MiddlewareResult<any>> {
|
|
325
394
|
try {
|
|
326
395
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
327
396
|
const middleware = _def.middlewares[callOpts.index]!;
|
|
@@ -366,7 +435,7 @@ function createProcedureCaller(_def: AnyProcedureBuilderDef): AnyProcedure {
|
|
|
366
435
|
marker: middlewareMarker,
|
|
367
436
|
};
|
|
368
437
|
}
|
|
369
|
-
}
|
|
438
|
+
}
|
|
370
439
|
|
|
371
440
|
// there's always at least one "next" since we wrap this.resolver in a middleware
|
|
372
441
|
const result = await callRecursive();
|
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AnyProcedureBuilderParams } from './builderTypes';
|
|
1
|
+
import { WithoutIndexSignature } from '../../types';
|
|
3
2
|
|
|
4
3
|
/**
|
|
5
4
|
* @internal
|
|
@@ -54,18 +53,6 @@ export const unsetMarker = Symbol('unsetMarker');
|
|
|
54
53
|
*/
|
|
55
54
|
export type UnsetMarker = typeof unsetMarker;
|
|
56
55
|
|
|
57
|
-
/**
|
|
58
|
-
* @internal
|
|
59
|
-
*/
|
|
60
|
-
export interface ResolveOptions<TParams extends AnyProcedureBuilderParams> {
|
|
61
|
-
ctx: Simplify<
|
|
62
|
-
Overwrite<TParams['_config']['$types']['ctx'], TParams['_ctx_out']>
|
|
63
|
-
>;
|
|
64
|
-
input: TParams['_input_out'] extends UnsetMarker
|
|
65
|
-
? undefined
|
|
66
|
-
: TParams['_input_out'];
|
|
67
|
-
}
|
|
68
|
-
|
|
69
56
|
/**
|
|
70
57
|
* @internal
|
|
71
58
|
*/
|