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' ;
104
115export interface FormattedDirectiveConfig {
126 [ directive : string ] : FormattedDirectiveArguments ;
@@ -22,8 +16,7 @@ export interface FormattedDirectiveObjectArguments {
2216
2317const isFormattedDirectiveObjectArguments = (
2418 arg : FormattedDirectiveArguments [ keyof FormattedDirectiveArguments ]
25- ) : arg is FormattedDirectiveObjectArguments =>
26- arg !== undefined && ! Array . isArray ( arg ) ;
19+ ) : arg is FormattedDirectiveObjectArguments => arg !== undefined && ! Array . isArray ( arg ) ;
2720
2821// ```yml
2922// directives:
@@ -49,18 +42,16 @@ const isFormattedDirectiveObjectArguments = (
4942// }
5043// }
5144// }
52- export const formatDirectiveConfig = (
53- config : DirectiveConfig
54- ) : FormattedDirectiveConfig => {
45+ export const formatDirectiveConfig = ( config : DirectiveConfig ) : FormattedDirectiveConfig => {
5546 return Object . fromEntries (
5647 Object . entries ( config ) . map ( ( [ directive , arg ] ) => {
5748 const formatted = Object . fromEntries (
5849 Object . entries ( arg ) . map ( ( [ arg , val ] ) => {
5950 if ( Array . isArray ( val ) ) {
6051 return [ arg , val ] ;
6152 }
62- if ( typeof val === " string" ) {
63- return [ arg , [ val , "$1" ] ] ;
53+ if ( typeof val === ' string' ) {
54+ return [ arg , [ val , '$1' ] ] ;
6455 }
6556 return [ arg , formatDirectiveObjectArguments ( val ) ] ;
6657 } )
@@ -84,14 +75,12 @@ export const formatDirectiveConfig = (
8475// 'uri': ['url', '$2'],
8576// 'email': ['email'],
8677// }
87- export const formatDirectiveObjectArguments = (
88- args : DirectiveObjectArguments
89- ) : FormattedDirectiveObjectArguments => {
78+ export const formatDirectiveObjectArguments = ( args : DirectiveObjectArguments ) : FormattedDirectiveObjectArguments => {
9079 const formatted = Object . entries ( args ) . map ( ( [ arg , val ] ) => {
9180 if ( Array . isArray ( val ) ) {
9281 return [ arg , val ] ;
9382 }
94- return [ arg , [ val , "$2" ] ] ;
83+ return [ arg , [ val , '$2' ] ] ;
9584 } ) ;
9685 return Object . fromEntries ( formatted ) ;
9786} ;
@@ -118,107 +107,88 @@ export const formatDirectiveObjectArguments = (
118107// email: String! @required(msg: "message") @constraint(minLength: 100, format: "email")
119108// }
120109// ```
121- export const buildApi = (
122- config : FormattedDirectiveConfig ,
123- directives : ReadonlyArray < ConstDirectiveNode >
124- ) : string =>
110+ export const buildApi = ( config : FormattedDirectiveConfig , directives : ReadonlyArray < ConstDirectiveNode > ) : string =>
125111 directives
126- . map ( ( directive ) => {
112+ . map ( directive => {
127113 const directiveName = directive . name . value ;
128114 const argsConfig = config [ directiveName ] ;
129- return buildApiFromDirectiveArguments (
130- argsConfig ,
131- directive . arguments ?? [ ]
132- ) ;
115+ return buildApiFromDirectiveArguments ( argsConfig , directive . arguments ?? [ ] ) ;
133116 } )
134- . join ( "" ) ;
117+ . join ( '' ) ;
135118
136- const buildApiSchema = (
137- validationSchema : string [ ] | undefined ,
138- argValue : ConstValueNode
139- ) : string => {
119+ const buildApiSchema = ( validationSchema : string [ ] | undefined , argValue : ConstValueNode ) : string => {
140120 if ( ! validationSchema ) {
141- return "" ;
121+ return '' ;
142122 }
143123 const schemaApi = validationSchema [ 0 ] ;
144- const schemaApiArgs = validationSchema . slice ( 1 ) . map ( ( templateArg ) => {
124+ const schemaApiArgs = validationSchema . slice ( 1 ) . map ( templateArg => {
145125 const gqlSchemaArgs = apiArgsFromConstValueNode ( argValue ) ;
146126 return applyArgToApiSchemaTemplate ( templateArg , gqlSchemaArgs ) ;
147127 } ) ;
148- return `.${ schemaApi } (${ schemaApiArgs . join ( ", " ) } )` ;
128+ return `.${ schemaApi } (${ schemaApiArgs . join ( ', ' ) } )` ;
149129} ;
150130
151131const buildApiFromDirectiveArguments = (
152132 config : FormattedDirectiveArguments ,
153133 args : ReadonlyArray < ConstArgumentNode >
154134) : string => {
155135 return args
156- . map ( ( arg ) => {
136+ . map ( arg => {
157137 const argName = arg . name . value ;
158138 const validationSchema = config [ argName ] ;
159139 if ( isFormattedDirectiveObjectArguments ( validationSchema ) ) {
160- return buildApiFromDirectiveObjectArguments (
161- validationSchema ,
162- arg . value
163- ) ;
140+ return buildApiFromDirectiveObjectArguments ( validationSchema , arg . value ) ;
164141 }
165142 return buildApiSchema ( validationSchema , arg . value ) ;
166143 } )
167- . join ( "" ) ;
144+ . join ( '' ) ;
168145} ;
169146
170147const buildApiFromDirectiveObjectArguments = (
171148 config : FormattedDirectiveObjectArguments ,
172149 argValue : ConstValueNode
173150) : string => {
174151 if ( argValue . kind !== Kind . STRING ) {
175- return "" ;
152+ return '' ;
176153 }
177154 const validationSchema = config [ argValue . value ] ;
178155 return buildApiSchema ( validationSchema , argValue ) ;
179156} ;
180157
181- const applyArgToApiSchemaTemplate = (
182- template : string ,
183- apiArgs : any [ ]
184- ) : string => {
158+ const applyArgToApiSchemaTemplate = ( template : string , apiArgs : any [ ] ) : string => {
185159 const matches = template . matchAll ( / [ $ ] ( \d + ) / g) ;
186160 for ( const match of matches ) {
187161 const placeholder = match [ 0 ] ; // `$1`
188162 const idx = parseInt ( match [ 1 ] , 10 ) - 1 ; // start with `1 - 1`
189163 const apiArg = apiArgs [ idx ] ;
190164 if ( ! apiArg ) {
191- template = template . replace ( placeholder , "" ) ;
165+ template = template . replace ( placeholder , '' ) ;
192166 continue ;
193167 }
194168 if ( template === placeholder ) {
195169 return stringify ( apiArg ) ;
196170 }
197171 template = template . replace ( placeholder , apiArg ) ;
198172 }
199- if ( template !== "" ) {
173+ if ( template !== '' ) {
200174 return stringify ( template , true ) ;
201175 }
202176 return template ;
203177} ;
204178
205179const stringify = ( arg : any , quoteString ?: boolean ) : string => {
206180 if ( Array . isArray ( arg ) ) {
207- return arg . map ( ( v ) => stringify ( v , true ) ) . join ( "," ) ;
181+ return arg . map ( v => stringify ( v , true ) ) . join ( ',' ) ;
208182 }
209- if ( typeof arg === " string" ) {
183+ if ( typeof arg === ' string' ) {
210184 if ( isConvertableRegexp ( arg ) ) {
211185 return arg ;
212186 }
213187 if ( quoteString ) {
214188 return JSON . stringify ( arg ) ;
215189 }
216190 }
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' ) {
222192 return `${ arg } ` ;
223193 }
224194 return JSON . stringify ( arg ) ;
0 commit comments