-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathgetParseFn.ts
92 lines (75 loc) · 2.34 KB
/
getParseFn.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
// Credit to @trpc/server
// https://github.com/trpc/trpc/blob/main/packages/server/src/core/parser.ts
// https://github.com/trpc/trpc/blob/main/packages/server/src/core/internals/getParseFn.ts
export type ParserZodEsque<TInput, TParsedInput> = {
_input: TInput;
_output: TParsedInput;
};
export type ParserMyZodEsque<TInput> = {
parse: (input: any) => TInput;
};
export type ParserSuperstructEsque<TInput> = {
create: (input: unknown) => TInput;
};
export type ParserCustomValidatorEsque<TInput> = (input: unknown) => TInput | Promise<TInput>;
export type ParserYupEsque<TInput> = {
validateSync: (input: unknown) => TInput;
};
export type ParserWithoutInput<TInput> =
| ParserYupEsque<TInput>
| ParserSuperstructEsque<TInput>
| ParserCustomValidatorEsque<TInput>
| ParserMyZodEsque<TInput>;
export type ParserWithInputOutput<TInput, TParsedInput> = ParserZodEsque<TInput, TParsedInput>;
export type Parser = ParserWithoutInput<any> | ParserWithInputOutput<any, any>;
export type inferParser<TParser extends Parser> = TParser extends ParserWithInputOutput<
infer $TIn,
infer $TOut
>
? {
in: $TIn;
out: $TOut;
}
: TParser extends ParserWithoutInput<infer $InOut>
? {
in: $InOut;
out: $InOut;
}
: never;
export type ParseFn<TType> = (value: unknown) => TType | Promise<TType>;
export function getParseFn<TType>(procedureParser: Parser): ParseFn<TType> {
const parser = procedureParser as any;
if (typeof parser === "function") {
// ProcedureParserCustomValidatorEsque
return parser;
}
if (typeof parser.parseAsync === "function") {
// ProcedureParserZodEsque
return parser.parseAsync.bind(parser);
}
if (typeof parser.parse === "function") {
// ProcedureParserZodEsque
return parser.parse.bind(parser);
}
if (typeof parser.validateSync === "function") {
// ProcedureParserYupEsque
return parser.validateSync.bind(parser);
}
if (typeof parser.create === "function") {
// ProcedureParserSuperstructEsque
return parser.create.bind(parser);
}
throw new Error("Could not find a validator fn");
}
/**
* @deprecated only for backwards compat
* @internal
*/
export function getParseFnOrPassThrough<TType>(
procedureParser: Parser | undefined
): ParseFn<TType> {
if (!procedureParser) {
return v => v as TType;
}
return getParseFn(procedureParser);
}