1- /*
2- Take in raw query string and
1+ /*
2+ Take in raw query string and
33 return a fetch api compatible object
44*/
55
6- import findLine from './find-line'
7-
8-
9- const getVars = ( lines , vars , variableMap ) => {
10- let body = lines . findIndex ( line => line . match ( / b o d y / ) )
11- let headers = lines . findIndex ( line => line . match ( / h e a d e r s / ) )
12- if ( headers === - 1 ) variableMap . body -= 1
13-
14- if ( body !== - 1 && headers !== - 1 && body < headers ) {
15- variableMap . headers += 1
16- variableMap . body -= 1
17- }
18-
19- return {
20- body : body || body === 0 ? JSON . stringify ( vars [ variableMap . body ] ) : null ,
21- headers : headers || headers === 0 ? vars [ variableMap . headers ] : null
22- }
23- }
24-
25- export default ( strings , vars ) => {
26- let variableMap = {
27- headers : 0 ,
28- body : 1 ,
29- }
30-
31- let lines = strings
6+ const buildObjectFromTag = ( strings , vars ) => {
7+ const namespace = 'legible-request-var-'
8+ return strings
9+ // First, add namespaced placeholders to elements in `strings`
10+ // for each element in `vars`
11+ . map ( ( str , index ) => str + ( vars [ index ] ? `${ namespace } ${ index } ` : '' ) )
12+ // Join the elements into a single string
3213 . join ( '' )
14+ // Split them back out by linebreak
3315 . split ( '\n' )
16+ // Remove empty elements
3417 . filter ( i => i )
18+ // Trim each element
19+ . map ( s => s . trim ( ) )
20+ // Split each element at `:`
21+ . map ( s => s . split ( ':' ) . map ( s => s . trim ( ) ) )
22+ // Strings from above step with multiple `:` in them will get split
23+ // more than once, meaning `values` is an array of strings instead of
24+ // a single string. Let's fix that.
25+ . map ( ( [ key , ...values ] ) => [ key , values . join ( ':' ) ] )
26+ // If `value` is a reference, replace it with respective element in `vars`
27+ . map ( ( [ key , value ] ) => {
28+ // Ignore non-namespaced value
29+ if ( ! value . startsWith ( namespace ) ) return [ key , value ]
30+ // Get the index at the end of the namespaced string
31+ const index = parseInt ( value . replace ( namespace , '' ) , 10 )
32+ // Return an array of the object key and replaced value from `vars`
33+ return [ key , vars [ index ] ]
34+ } )
35+ // Convert to object
36+ . reduce ( ( obj , [ key , value ] ) => {
37+ return { ...obj , [ key ] : value }
38+ } , { } )
39+ }
3540
36- let url = findLine ( 'url' , lines )
37-
38- if ( ! url ) {
39- url = vars [ 0 ]
40- variableMap = { headers : 1 , body : 2 }
41- }
42-
43- let { headers, body } = getVars ( lines , vars , variableMap )
41+ export default ( strings , vars ) => {
42+ const { url, method, body, ...options } = buildObjectFromTag ( strings , vars )
4443
4544 return {
46- url : findLine ( 'url' , lines ) || vars [ 0 ] ,
45+ url,
4746 options : {
48- method : findLine ( ' method' , lines ) || 'GET' ,
49- headers ,
50- body ,
47+ method : method || 'GET' ,
48+ body : body ? JSON . stringify ( body ) : null ,
49+ ... options
5150 }
5251 }
53- }
52+ }
0 commit comments