1
- import {
2
- ConstArgumentNode ,
3
- ConstDirectiveNode ,
4
- ConstValueNode ,
5
- Kind ,
6
- valueFromASTUntyped ,
7
- } from "graphql" ;
8
- import { DirectiveConfig , DirectiveObjectArguments } from "./config" ;
9
- import { isConvertableRegexp } from "./regexp" ;
1
+ import { ConstArgumentNode , ConstDirectiveNode , ConstValueNode , Kind , valueFromASTUntyped } from 'graphql' ;
2
+ import { DirectiveConfig , DirectiveObjectArguments } from './config' ;
3
+ import { isConvertableRegexp } from './regexp' ;
10
4
11
5
export interface FormattedDirectiveConfig {
12
6
[ directive : string ] : FormattedDirectiveArguments ;
@@ -22,8 +16,7 @@ export interface FormattedDirectiveObjectArguments {
22
16
23
17
const isFormattedDirectiveObjectArguments = (
24
18
arg : FormattedDirectiveArguments [ keyof FormattedDirectiveArguments ]
25
- ) : arg is FormattedDirectiveObjectArguments =>
26
- arg !== undefined && ! Array . isArray ( arg ) ;
19
+ ) : arg is FormattedDirectiveObjectArguments => arg !== undefined && ! Array . isArray ( arg ) ;
27
20
28
21
// ```yml
29
22
// directives:
@@ -49,18 +42,16 @@ const isFormattedDirectiveObjectArguments = (
49
42
// }
50
43
// }
51
44
// }
52
- export const formatDirectiveConfig = (
53
- config : DirectiveConfig
54
- ) : FormattedDirectiveConfig => {
45
+ export const formatDirectiveConfig = ( config : DirectiveConfig ) : FormattedDirectiveConfig => {
55
46
return Object . fromEntries (
56
47
Object . entries ( config ) . map ( ( [ directive , arg ] ) => {
57
48
const formatted = Object . fromEntries (
58
49
Object . entries ( arg ) . map ( ( [ arg , val ] ) => {
59
50
if ( Array . isArray ( val ) ) {
60
51
return [ arg , val ] ;
61
52
}
62
- if ( typeof val === " string" ) {
63
- return [ arg , [ val , "$1" ] ] ;
53
+ if ( typeof val === ' string' ) {
54
+ return [ arg , [ val , '$1' ] ] ;
64
55
}
65
56
return [ arg , formatDirectiveObjectArguments ( val ) ] ;
66
57
} )
@@ -84,14 +75,12 @@ export const formatDirectiveConfig = (
84
75
// 'uri': ['url', '$2'],
85
76
// 'email': ['email'],
86
77
// }
87
- export const formatDirectiveObjectArguments = (
88
- args : DirectiveObjectArguments
89
- ) : FormattedDirectiveObjectArguments => {
78
+ export const formatDirectiveObjectArguments = ( args : DirectiveObjectArguments ) : FormattedDirectiveObjectArguments => {
90
79
const formatted = Object . entries ( args ) . map ( ( [ arg , val ] ) => {
91
80
if ( Array . isArray ( val ) ) {
92
81
return [ arg , val ] ;
93
82
}
94
- return [ arg , [ val , "$2" ] ] ;
83
+ return [ arg , [ val , '$2' ] ] ;
95
84
} ) ;
96
85
return Object . fromEntries ( formatted ) ;
97
86
} ;
@@ -118,107 +107,88 @@ export const formatDirectiveObjectArguments = (
118
107
// email: String! @required(msg: "message") @constraint(minLength: 100, format: "email")
119
108
// }
120
109
// ```
121
- export const buildApi = (
122
- config : FormattedDirectiveConfig ,
123
- directives : ReadonlyArray < ConstDirectiveNode >
124
- ) : string =>
110
+ export const buildApi = ( config : FormattedDirectiveConfig , directives : ReadonlyArray < ConstDirectiveNode > ) : string =>
125
111
directives
126
- . map ( ( directive ) => {
112
+ . map ( directive => {
127
113
const directiveName = directive . name . value ;
128
114
const argsConfig = config [ directiveName ] ;
129
- return buildApiFromDirectiveArguments (
130
- argsConfig ,
131
- directive . arguments ?? [ ]
132
- ) ;
115
+ return buildApiFromDirectiveArguments ( argsConfig , directive . arguments ?? [ ] ) ;
133
116
} )
134
- . join ( "" ) ;
117
+ . join ( '' ) ;
135
118
136
- const buildApiSchema = (
137
- validationSchema : string [ ] | undefined ,
138
- argValue : ConstValueNode
139
- ) : string => {
119
+ const buildApiSchema = ( validationSchema : string [ ] | undefined , argValue : ConstValueNode ) : string => {
140
120
if ( ! validationSchema ) {
141
- return "" ;
121
+ return '' ;
142
122
}
143
123
const schemaApi = validationSchema [ 0 ] ;
144
- const schemaApiArgs = validationSchema . slice ( 1 ) . map ( ( templateArg ) => {
124
+ const schemaApiArgs = validationSchema . slice ( 1 ) . map ( templateArg => {
145
125
const gqlSchemaArgs = apiArgsFromConstValueNode ( argValue ) ;
146
126
return applyArgToApiSchemaTemplate ( templateArg , gqlSchemaArgs ) ;
147
127
} ) ;
148
- return `.${ schemaApi } (${ schemaApiArgs . join ( ", " ) } )` ;
128
+ return `.${ schemaApi } (${ schemaApiArgs . join ( ', ' ) } )` ;
149
129
} ;
150
130
151
131
const buildApiFromDirectiveArguments = (
152
132
config : FormattedDirectiveArguments ,
153
133
args : ReadonlyArray < ConstArgumentNode >
154
134
) : string => {
155
135
return args
156
- . map ( ( arg ) => {
136
+ . map ( arg => {
157
137
const argName = arg . name . value ;
158
138
const validationSchema = config [ argName ] ;
159
139
if ( isFormattedDirectiveObjectArguments ( validationSchema ) ) {
160
- return buildApiFromDirectiveObjectArguments (
161
- validationSchema ,
162
- arg . value
163
- ) ;
140
+ return buildApiFromDirectiveObjectArguments ( validationSchema , arg . value ) ;
164
141
}
165
142
return buildApiSchema ( validationSchema , arg . value ) ;
166
143
} )
167
- . join ( "" ) ;
144
+ . join ( '' ) ;
168
145
} ;
169
146
170
147
const buildApiFromDirectiveObjectArguments = (
171
148
config : FormattedDirectiveObjectArguments ,
172
149
argValue : ConstValueNode
173
150
) : string => {
174
151
if ( argValue . kind !== Kind . STRING ) {
175
- return "" ;
152
+ return '' ;
176
153
}
177
154
const validationSchema = config [ argValue . value ] ;
178
155
return buildApiSchema ( validationSchema , argValue ) ;
179
156
} ;
180
157
181
- const applyArgToApiSchemaTemplate = (
182
- template : string ,
183
- apiArgs : any [ ]
184
- ) : string => {
158
+ const applyArgToApiSchemaTemplate = ( template : string , apiArgs : any [ ] ) : string => {
185
159
const matches = template . matchAll ( / [ $ ] ( \d + ) / g) ;
186
160
for ( const match of matches ) {
187
161
const placeholder = match [ 0 ] ; // `$1`
188
162
const idx = parseInt ( match [ 1 ] , 10 ) - 1 ; // start with `1 - 1`
189
163
const apiArg = apiArgs [ idx ] ;
190
164
if ( ! apiArg ) {
191
- template = template . replace ( placeholder , "" ) ;
165
+ template = template . replace ( placeholder , '' ) ;
192
166
continue ;
193
167
}
194
168
if ( template === placeholder ) {
195
169
return stringify ( apiArg ) ;
196
170
}
197
171
template = template . replace ( placeholder , apiArg ) ;
198
172
}
199
- if ( template !== "" ) {
173
+ if ( template !== '' ) {
200
174
return stringify ( template , true ) ;
201
175
}
202
176
return template ;
203
177
} ;
204
178
205
179
const stringify = ( arg : any , quoteString ?: boolean ) : string => {
206
180
if ( Array . isArray ( arg ) ) {
207
- return arg . map ( ( v ) => stringify ( v , true ) ) . join ( "," ) ;
181
+ return arg . map ( v => stringify ( v , true ) ) . join ( ',' ) ;
208
182
}
209
- if ( typeof arg === " string" ) {
183
+ if ( typeof arg === ' string' ) {
210
184
if ( isConvertableRegexp ( arg ) ) {
211
185
return arg ;
212
186
}
213
187
if ( quoteString ) {
214
188
return JSON . stringify ( arg ) ;
215
189
}
216
190
}
217
- if (
218
- typeof arg === "boolean" ||
219
- typeof arg === "number" ||
220
- typeof arg === "bigint"
221
- ) {
191
+ if ( typeof arg === 'boolean' || typeof arg === 'number' || typeof arg === 'bigint' ) {
222
192
return `${ arg } ` ;
223
193
}
224
194
return JSON . stringify ( arg ) ;
0 commit comments