1
1
import { Result } from "../utils" ;
2
- import { AnyApiEndpoint , AnyApiEndpoints , Method } from "./spec" ;
2
+ import { AnyApiEndpoint , AnyApiEndpoints , isMethod , Method } from "./spec" ;
3
3
import { ParsedQs } from "qs" ;
4
4
5
5
export type Validators <
@@ -25,18 +25,61 @@ export type ValidatorsMap = {
25
25
[ Path in string ] : Partial < Record < Method , AnyValidators > > ;
26
26
} ;
27
27
28
+ export const runValidators = ( validators : AnyValidators , error : unknown ) => {
29
+ const newD = ( ) => Result . data ( undefined ) ;
30
+ return {
31
+ preCheck : error ,
32
+ params : validators . params ?.( ) ?? newD ( ) ,
33
+ query : validators . query ?.( ) ?? newD ( ) ,
34
+ body : validators . body ?.( ) ?? newD ( ) ,
35
+ headers : validators . headers ?.( ) ?? newD ( ) ,
36
+ } ;
37
+ } ;
38
+
39
+ export type ResponseValidators <
40
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
41
+ BodyValidator extends AnyValidator | undefined ,
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ HeadersValidator extends AnyValidator | undefined ,
44
+ > = {
45
+ body : BodyValidator ;
46
+ headers : HeadersValidator ;
47
+ } ;
48
+ export type AnyResponseValidators = Partial <
49
+ ResponseValidators < AnyValidator , AnyValidator >
50
+ > ;
51
+ export const runResponseValidators = ( validators : {
52
+ validator : AnyResponseValidators ;
53
+ error : unknown ;
54
+ } ) => {
55
+ const newD = ( ) => Result . data ( undefined ) ;
56
+ return {
57
+ // TODO: スキーマが間違っていても、bodyのvalidatorがなぜか定義されていない
58
+ preCheck : validators . error ,
59
+ body : validators . validator . body ?.( ) ?? newD ( ) ,
60
+ headers : validators . validator . headers ?.( ) ?? newD ( ) ,
61
+ } ;
62
+ } ;
63
+
28
64
export type Validator < Data , Error > = ( ) => Result < Data , Error > ;
29
65
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30
66
export type AnyValidator = Validator < any , any > ;
31
67
32
68
export type ValidatorsInput = {
33
69
path : string ;
34
70
method : string ;
35
- params ? : Record < string , string > ;
71
+ params : Record < string , string | string [ ] > ;
36
72
query ?: ParsedQs ;
37
73
body ?: Record < string , string > ;
38
74
headers : Record < string , string | string [ ] | undefined > ;
39
75
} ;
76
+ export type ResponseValidatorsInput = {
77
+ path : string ;
78
+ method : string ;
79
+ statusCode : number ;
80
+ body ?: unknown ;
81
+ headers : Headers ;
82
+ } ;
40
83
41
84
type ValidationError = {
42
85
actual : string ;
@@ -100,3 +143,36 @@ export const getApiSpec = <
100
143
const r = validatePathAndMethod ( endpoints , maybePath , maybeMethod ) ;
101
144
return Result . map ( r , ( d ) => endpoints [ d . path ] [ d . method ] ) ;
102
145
} ;
146
+
147
+ export const preCheck = < E extends AnyApiEndpoints > (
148
+ endpoints : E ,
149
+ path : string ,
150
+ maybeMethod : string ,
151
+ ) => {
152
+ const method = maybeMethod ?. toLowerCase ( ) ;
153
+ if ( ! isMethod ( method ) ) {
154
+ return Result . error ( newValidatorMethodNotFoundError ( method ) ) ;
155
+ }
156
+ return getApiSpec ( endpoints , path , method ) ;
157
+ } ;
158
+
159
+ export type ValidatorError =
160
+ | ValidatorMethodNotFoundError
161
+ | ValidatorPathNotFoundError ;
162
+
163
+ export const newValidatorMethodNotFoundError = ( method : string ) => ( {
164
+ target : "method" ,
165
+ actual : method ,
166
+ message : `method does not exist in endpoint` ,
167
+ } ) ;
168
+ type ValidatorMethodNotFoundError = ReturnType <
169
+ typeof newValidatorMethodNotFoundError
170
+ > ;
171
+ export const newValidatorPathNotFoundError = ( path : string ) => ( {
172
+ target : "path" ,
173
+ actual : path ,
174
+ message : `path does not exist in endpoints` ,
175
+ } ) ;
176
+ type ValidatorPathNotFoundError = ReturnType <
177
+ typeof newValidatorPathNotFoundError
178
+ > ;
0 commit comments