Last active
August 13, 2019 15:07
-
-
Save Adrian-Samuel/6b97bd5969553da80fabd45ac484665f to your computer and use it in GitHub Desktop.
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
const requestBuilder = async (domain, RAD, endpoint, ID, parameters, isDisplay) => { | |
let url = `https://${domain}/API/Account/${RAD}`; | |
if (isDisplay) { | |
url = url + `DisplayTemplate` | |
} | |
if (endpoint == "Label") { | |
url = `${url}/ItemAs${endpoint}` | |
} else { | |
url = `${url}/${endpoint}` | |
} | |
if (ID == "") { | |
url = `${url}.json` | |
} else { | |
url = `${url}/${ID}.json` | |
} | |
if (parameters != "") { | |
url = `${url}?${parameters}` | |
} | |
if (ID == "") { | |
const queryParamCheck = url.includes('?') ? `${url}offset` : `${url}?offset` | |
let offset = 0; | |
let results = []; | |
let nextPage = true | |
while (nextPage) { | |
const makeRequest = await fetch(`${queryParamCheck}=${offset}`, { | |
credentials: 'same-origin' | |
}) | |
const requestJSONData = await makeRequest.json(); | |
if (requestJSONData.hasOwnProperty(endpoint)) { | |
console.log(requestJSONData[endpoint]) | |
results.push(...requestJSONData[endpoint]) | |
offset += 100 | |
} else { | |
nextPage = false; | |
} | |
} | |
console.log(`The results from your request ${url} is `, results) | |
return results; | |
} else { | |
const makeRequest = await fetch(`${url}`, { | |
credentials: 'same-origin' | |
}) | |
const requestJSONData = await makeRequest.json(); | |
return requestJSONData[endpoint] | |
} | |
} | |
//Example | |
(async () => { | |
console.log(await requestBuilder(window.location.host, 127704, 'Item', '220', '', false)) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment