Last active
July 19, 2021 20:44
-
-
Save ArjixWasTaken/f975fc8a5c52470dfb2bc651cb4b3186 to your computer and use it in GitHub Desktop.
An attempt to make the fetch api easier in JS by imitating python's requests module.
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
// Copyright 2021 ArjixWasTaken | |
const isArray = (obj) => Array.isArray(obj); | |
const isDict = (obj) => { | |
if (typeof obj === "object" && !isArray(obj)) return true; | |
return false; | |
}; | |
const isDictEmpty = (obj) => { | |
if (!isDict(obj)) return; | |
return Object.keys(obj).length === 0; | |
}; | |
const stringify = (obj) => { | |
const params = []; | |
for (const [key, value] of Object.entries(obj)) { | |
params.push(`${encodeURIComponent(key)}=${encodeURIComponent(value)}`); | |
} | |
return params.join("&"); | |
}; | |
/** | |
* Apply querystring parameters to a URI. | |
* @param {string} link - A URI. | |
* @param {string} params - Query String Parameters. | |
*/ | |
const applyParams = (link, params) => { | |
if (typeof link !== "string") return; | |
if (isDictEmpty(params)) return link; | |
link = link.trim(); | |
if (link.split("?")[1] && link.split("?")[1].trim() !== "") { | |
link += "&"; | |
} else { | |
link = link.split("?")[0]; | |
link += "?"; | |
} | |
return link + stringify(params); | |
}; | |
const headersToJson = (obj) => { | |
const newObj = {}; | |
for (const key of obj.keys()) { | |
newObj[key] = obj.get(key); | |
} | |
return newObj; | |
}; | |
/** | |
* Make a GET request. | |
* @param {string} link - The link of the site. | |
* @param {Object} options - The options to be used when making the request. | |
* @param {string} options.headers - The headers to be used. | |
* @param {string} options.referer - Overrides the referer in the headers with this one. | |
* @param {string} options.data - Data to be sent in the body of the request. | |
*/ | |
const get = async (link, options = {}) => { | |
const init = {}; | |
if (options.params && !isDictEmpty(options.params)) { | |
link = applyParams(link, options.params); | |
} | |
if ( | |
options.headers && | |
(Object(options.headers).hasOwnProperty("Referer") || | |
Object(options.headers).hasOwnProperty("referer")) | |
) { | |
if (options.referer) { | |
delete options.headers.referer; | |
options.headers.Referer = options.referer; | |
} | |
} | |
init.method = "GET"; | |
if (options.headers && !isDictEmpty(options.headers)) { | |
init.headers = options.headers; | |
} | |
if (options.data && !isDictEmpty(options.data)) { | |
init.body = stringify(options.data); | |
} | |
const req = await fetch(link, init); | |
req.headers = headersToJson(req.headers) | |
return req | |
}; | |
/** | |
* Make a POST request. | |
* @param {string} link - The link of the site. | |
* @param {Object} options - The options to be used when making the request. | |
* @param {string} options.headers - The headers to be used. | |
* @param {string} options.referer - Overrides the referer in the headers with this one. | |
* @param {string} options.data - Data to be sent in the body of the request. | |
* @param {string} options.json - JSON to be sent in the body of the request. | |
*/ | |
const post = async (link, options = {}) => { | |
const init = {}; | |
if (options.params && !isDictEmpty(options.params)) { | |
link = applyParams(link, options.params); | |
} | |
if ( | |
options.headers && | |
(Object(options.headers).hasOwnProperty("Referer") || | |
Object(options.headers).hasOwnProperty("referer")) | |
) { | |
if (options.referer) { | |
delete options.headers.referer; | |
options.headers.Referer = options.referer; | |
} | |
} | |
init.method = "POST"; | |
if (options.headers && !isDictEmpty(options.headers)) { | |
init.headers = options.headers; | |
} | |
if ( | |
options.data && | |
options.json && | |
!isDictEmpty(data) && | |
!isDictEmpty(json) | |
) { | |
throw new Error("You can't have both data and json."); | |
} | |
if (options.data && !isDictEmpty(options.data)) { | |
init.body = stringify(options.data); | |
} | |
if (options.json && !isDictEmpty(options.json)) { | |
init.body = JSON.stringify(options.json); | |
} | |
const req = await fetch(link, init); | |
req.headers = headersToJson(req.headers) | |
return req | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment