-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrote how variables are extracted from the template string
- Loading branch information
1 parent
f6012f9
commit 14775d0
Showing
1 changed file
with
40 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,52 @@ | ||
/* | ||
Take in raw query string and | ||
/* | ||
Take in raw query string and | ||
return a fetch api compatible object | ||
*/ | ||
|
||
import findLine from './find-line' | ||
|
||
|
||
const getVars = (lines, vars, variableMap) => { | ||
let body = lines.findIndex(line => line.match(/body/)) | ||
let headers = lines.findIndex(line => line.match(/headers/)) | ||
if(headers === -1) variableMap.body -= 1 | ||
|
||
if(body !== -1 && headers !== -1 && body < headers) { | ||
variableMap.headers += 1 | ||
variableMap.body -= 1 | ||
} | ||
|
||
return { | ||
body: body || body === 0 ? JSON.stringify(vars[variableMap.body]) : null, | ||
headers: headers || headers === 0 ? vars[variableMap.headers] : null | ||
} | ||
} | ||
|
||
export default (strings, vars) => { | ||
let variableMap = { | ||
headers: 0, | ||
body: 1, | ||
} | ||
|
||
let lines = strings | ||
const buildObjectFromTag = (strings, vars) => { | ||
const namespace = 'legible-request-var-' | ||
return strings | ||
// First, add namespaced placeholders to elements in `strings` | ||
// for each element in `vars` | ||
.map((str, index) => str + (vars[index] ? `${namespace}${index}` : '')) | ||
// Join the elements into a single string | ||
.join('') | ||
// Split them back out by linebreak | ||
.split('\n') | ||
// Remove empty elements | ||
.filter(i => i) | ||
// Trim each element | ||
.map(s => s.trim()) | ||
// Split each element at `:` | ||
.map(s => s.split(':').map(s => s.trim())) | ||
// Strings from above step with multiple `:` in them will get split | ||
// more than once, meaning `values` is an array of strings instead of | ||
// a single string. Let's fix that. | ||
.map(([key, ...values]) => [key, values.join(':')]) | ||
// If `value` is a reference, replace it with respective element in `vars` | ||
.map(([key, value]) => { | ||
// Ignore non-namespaced value | ||
if (!value.startsWith(namespace)) return [key, value] | ||
// Get the index at the end of the namespaced string | ||
const index = parseInt(value.replace(namespace, ''), 10) | ||
// Return an array of the object key and replaced value from `vars` | ||
return [key, vars[index]] | ||
}) | ||
// Convert to object | ||
.reduce((obj, [key, value]) => { | ||
return {...obj, [key]: value} | ||
}, {}) | ||
} | ||
|
||
let url = findLine('url', lines) | ||
|
||
if(!url) { | ||
url = vars[0] | ||
variableMap = { headers: 1, body: 2 } | ||
} | ||
|
||
let { headers, body } = getVars(lines, vars, variableMap) | ||
export default (strings, vars) => { | ||
const { url, method, body, ...options } = buildObjectFromTag(strings, vars) | ||
|
||
return { | ||
url: findLine('url', lines) || vars[0], | ||
url, | ||
options: { | ||
method: findLine('method', lines) || 'GET', | ||
headers, | ||
body, | ||
method: method || 'GET', | ||
body: body ? JSON.stringify(body) : null, | ||
...options | ||
} | ||
} | ||
} | ||
} |