1
1
const _ = require ( 'lodash' ) ;
2
- const fetch = require ( 'node-fetch' ) ;
3
2
const Resolver = require ( './Resolver' ) ;
4
3
const SelectiveResolver = require ( './SelectiveResolver' ) ;
5
4
const PrefixSelector = require ( './PrefixSelector' ) ;
6
5
7
6
module . exports = class URLResolver extends SelectiveResolver {
8
- constructor ( selector ) {
7
+ constructor ( selector , fetchArg ) {
9
8
super ( selector || ( new PrefixSelector ( 'url.' ) ) ) ;
9
+ this . $fetch = fetchArg ;
10
10
}
11
11
12
12
resolve ( config ) {
@@ -30,12 +30,14 @@ module.exports = class URLResolver extends SelectiveResolver {
30
30
if ( self . selector . matches ( v ) ) {
31
31
try {
32
32
const selectedValue = self . selector . resolveValue ( v ) ;
33
- let urlPath = path . substring ( 0 , path . lastIndexOf ( '.' ) ) ;
34
- let method = parentConfig . has ( `${ urlPath } .method` ) ? parentConfig . get ( `${ urlPath } .method` ) : null ;
35
- let authorization = parentConfig . has ( `${ urlPath } .authorization` ) ? parentConfig . get ( `${ urlPath } .authorization` ) : null ;
36
- let body = parentConfig . has ( `${ urlPath } .body` ) ? parentConfig . get ( `${ urlPath } .body` ) : null ;
37
- let headers = parentConfig . has ( `${ urlPath } .headers` ) ? parentConfig . get ( `${ urlPath } .headers` ) : null ;
38
- const fetchedValue = await this . fetch ( selectedValue , authorization , method , body , headers ) ;
33
+ const urlPath = path . substring ( 0 , path . lastIndexOf ( '.' ) ) ;
34
+ const method = parentConfig . has ( `${ urlPath } .method` ) ? parentConfig . get ( `${ urlPath } .method` ) : null ;
35
+ const authorization = parentConfig . has ( `${ urlPath } .authorization` ) ? parentConfig . get ( `${ urlPath } .authorization` ) : null ;
36
+ const body = parentConfig . has ( `${ urlPath } .body` ) ? parentConfig . get ( `${ urlPath } .body` ) : null ;
37
+ const headers = parentConfig . has ( `${ urlPath } .headers` ) ? parentConfig . get ( `${ urlPath } .headers` ) : null ;
38
+ const fetchedValue = await this . fetch (
39
+ selectedValue , authorization , method , body , headers ,
40
+ ) ;
39
41
return fetchedValue ;
40
42
} catch ( e ) {
41
43
return v ;
@@ -46,14 +48,16 @@ module.exports = class URLResolver extends SelectiveResolver {
46
48
return resolvedConfig ;
47
49
}
48
50
49
- async fetch ( url , authorization , method , body , headers ) {
50
- let _headers = authorization ? { 'authorization' :authorization } : { } ;
51
- _ . assignIn ( _headers , headers )
52
- let opts = { method : method || 'get' , headers : _headers }
53
- if ( method && method ?. toLowerCase ( ) != 'get' && method ?. toLowerCase ( ) != 'head' ) {
54
- _ . assignIn ( opts , JSON . stringify ( body || { } ) )
51
+ async fetch ( url , authorization , method , body , headers ) {
52
+ if ( ! this . $fetch ) {
53
+ throw new Error ( 'fetch is required' ) ;
55
54
}
56
- return await fetch ( url , opts ) . then ( res => res . json ( ) ) ;
55
+ const $headers = authorization ? { authorization } : { } ;
56
+ _ . assignIn ( $headers , headers ) ;
57
+ const opts = { method : method || 'get' , headers : $headers } ;
58
+ if ( method && method ?. toLowerCase ( ) !== 'get' && method ?. toLowerCase ( ) !== 'head' ) {
59
+ _ . assignIn ( opts , JSON . stringify ( body || { } ) ) ;
60
+ }
61
+ return this . $fetch ( url , opts ) . then ( ( res ) => res . json ( ) ) ;
57
62
}
58
-
59
63
} ;
0 commit comments