-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
middleware.ts
285 lines (267 loc) · 7.37 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import { TRPCError } from '../error/TRPCError';
import type { Simplify } from '../types';
import type { AnyRootConfig, RootConfig } from './internals/config';
import type { ParseFn } from './internals/getParseFn';
import type { ProcedureBuilderMiddleware } from './internals/procedureBuilder';
import type {
DefaultValue,
MiddlewareMarker,
Overwrite,
UnsetMarker,
} from './internals/utils';
import type { ProcedureParams } from './procedure';
import type { ProcedureType } from './types';
/**
* @internal
*/
interface MiddlewareResultBase {
/**
* All middlewares should pass through their `next()`'s output.
* Requiring this marker makes sure that can't be forgotten at compile-time.
*/
readonly marker: MiddlewareMarker;
}
/**
* @internal
*/
interface MiddlewareOKResult<_TParams extends ProcedureParams>
extends MiddlewareResultBase {
ok: true;
data: unknown;
// this could be extended with `input`/`rawInput` later
}
/**
* @internal
*/
interface MiddlewareErrorResult<_TParams extends ProcedureParams>
extends MiddlewareResultBase {
ok: false;
error: TRPCError;
}
/**
* @internal
*/
export type MiddlewareResult<TParams extends ProcedureParams> =
| MiddlewareErrorResult<TParams>
| MiddlewareOKResult<TParams>;
/**
* @internal
*/
export interface MiddlewareBuilder<
TRoot extends ProcedureParams,
TNewParams extends ProcedureParams,
> {
/**
* Create a new builder based on the current middleware builder
*/
unstable_pipe<$Params extends ProcedureParams>(
fn: {
_config: TRoot['_config'];
_meta: TRoot['_meta'];
_ctx_out: Overwrite<TRoot['_ctx_out'], TNewParams['_ctx_out']>;
_input_in: DefaultValue<TRoot['_input_in'], TNewParams['_input_in']>;
_input_out: DefaultValue<TRoot['_input_out'], TNewParams['_input_out']>;
_output_in: DefaultValue<TRoot['_output_in'], TNewParams['_output_in']>;
_output_out: DefaultValue<
TRoot['_output_out'],
TNewParams['_output_out']
>;
} extends infer OParams extends ProcedureParams
?
| MiddlewareBuilder<OParams, $Params>
| MiddlewareFunction<OParams, $Params>
: never,
): CreateMiddlewareReturnInput<
TRoot,
TNewParams,
Overwrite<TNewParams, $Params>
>;
/**
* List of middlewares within this middleware builder
*/
_middlewares: MiddlewareFunction<TRoot, TNewParams>[];
}
/**
* @internal
* FIXME: there must be a nicer way of doing this, it's hard to maintain when we have several structures like this
*/
type CreateMiddlewareReturnInput<
TRoot extends ProcedureParams,
TPrev extends ProcedureParams,
TNext extends ProcedureParams,
> = MiddlewareBuilder<
TRoot,
{
_config: TPrev['_config'];
_meta: TPrev['_meta'];
_ctx_out: Overwrite<TPrev['_ctx_out'], TNext['_ctx_out']>;
_input_in: DefaultValue<TNext['_input_in'], TPrev['_input_in']>;
_input_out: DefaultValue<TNext['_input_out'], TPrev['_input_out']>;
_output_in: DefaultValue<TNext['_output_in'], TPrev['_output_in']>;
_output_out: DefaultValue<TNext['_output_out'], TPrev['_output_out']>;
}
>;
/**
* @internal
*/
type deriveParamsFromConfig<
TConfig extends AnyRootConfig,
TInputIn = unknown,
> = {
_config: TConfig;
// eslint-disable-next-line @typescript-eslint/ban-types
_ctx_out: {};
_input_out: UnsetMarker;
_input_in: TInputIn;
_output_in: unknown;
_output_out: unknown;
_meta: TConfig['$types']['meta'];
};
/**
* @internal
*/
export type MiddlewareFunction<
TParams extends ProcedureParams,
TParamsAfter extends ProcedureParams,
> = {
(opts: {
ctx: Simplify<
Overwrite<TParams['_config']['$types']['ctx'], TParams['_ctx_out']>
>;
type: ProcedureType;
path: string;
input: TParams['_input_in'];
rawInput: unknown;
meta: TParams['_meta'] | undefined;
next: {
(): Promise<MiddlewareResult<TParams>>;
<$Context>(opts: { ctx: $Context }): Promise<
MiddlewareResult<{
_config: TParams['_config'];
_ctx_out: $Context;
_input_in: TParams['_input_in'];
_input_out: TParams['_input_out'];
_output_in: TParams['_output_in'];
_output_out: TParams['_output_out'];
_meta: TParams['_meta'];
}>
>;
(opts: { rawInput: unknown }): Promise<MiddlewareResult<TParams>>;
};
}): Promise<MiddlewareResult<TParamsAfter>>;
_type?: string | undefined;
};
/**
* @internal
*/
export function createMiddlewareFactory<
TConfig extends AnyRootConfig,
TInputIn = unknown,
>() {
function createMiddlewareInner<TNewParams extends ProcedureParams>(
middlewares: MiddlewareFunction<any, any>[],
): MiddlewareBuilder<deriveParamsFromConfig<TConfig, TInputIn>, TNewParams> {
return {
_middlewares: middlewares,
unstable_pipe(middlewareBuilderOrFn) {
const pipedMiddleware =
'_middlewares' in middlewareBuilderOrFn
? middlewareBuilderOrFn._middlewares
: [middlewareBuilderOrFn];
return createMiddlewareInner([
...(middlewares as any),
...pipedMiddleware,
]);
},
};
}
function createMiddleware<TNewParams extends ProcedureParams>(
fn: MiddlewareFunction<
deriveParamsFromConfig<TConfig, TInputIn>,
TNewParams
>,
): MiddlewareBuilder<deriveParamsFromConfig<TConfig, TInputIn>, TNewParams> {
return createMiddlewareInner([fn]);
}
return createMiddleware;
}
export const experimental_standaloneMiddleware = <
TCtx extends {
ctx?: object;
meta?: object;
input?: unknown;
},
>() => ({
create: createMiddlewareFactory<
RootConfig<{
ctx: TCtx extends { ctx: infer T extends object } ? T : object;
meta: TCtx extends { meta: infer T extends object } ? T : object;
errorShape: object;
transformer: object;
}>,
TCtx extends { input: infer T } ? T : unknown
>(),
});
function isPlainObject(obj: unknown) {
return obj && typeof obj === 'object' && !Array.isArray(obj);
}
/**
* @internal
* Please note, `trpc-openapi` uses this function.
*/
export function createInputMiddleware<TInput>(parse: ParseFn<TInput>) {
const inputMiddleware: ProcedureBuilderMiddleware = async ({
next,
rawInput,
input,
}) => {
let parsedInput: ReturnType<typeof parse>;
try {
parsedInput = await parse(rawInput);
} catch (cause) {
throw new TRPCError({
code: 'BAD_REQUEST',
cause,
});
}
// Multiple input parsers
const combinedInput =
isPlainObject(input) && isPlainObject(parsedInput)
? {
...input,
...parsedInput,
}
: parsedInput;
// TODO fix this typing?
return next({ input: combinedInput } as any);
};
inputMiddleware._type = 'input';
return inputMiddleware;
}
/**
* @internal
*/
export function createOutputMiddleware<TOutput>(parse: ParseFn<TOutput>) {
const outputMiddleware: ProcedureBuilderMiddleware = async ({ next }) => {
const result = await next();
if (!result.ok) {
// pass through failures without validating
return result;
}
try {
const data = await parse(result.data);
return {
...result,
data,
};
} catch (cause) {
throw new TRPCError({
message: 'Output validation failed',
code: 'INTERNAL_SERVER_ERROR',
cause,
});
}
};
outputMiddleware._type = 'output';
return outputMiddleware;
}