Skip to content

Instantly share code, notes, and snippets.

@Adrian-Samuel
Last active August 13, 2019 15:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adrian-Samuel/6b97bd5969553da80fabd45ac484665f to your computer and use it in GitHub Desktop.
Save Adrian-Samuel/6b97bd5969553da80fabd45ac484665f to your computer and use it in GitHub Desktop.
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