Skip to content

Instantly share code, notes, and snippets.

@ljaviertovar
Last active November 14, 2022 01:57
Show Gist options
  • Save ljaviertovar/42ee275275a972b6ba6e399c08dccb5f to your computer and use it in GitHub Desktop.
Save ljaviertovar/42ee275275a972b6ba6e399c08dccb5f to your computer and use it in GitHub Desktop.
Helper to make HTTP requests to a REST API
export const httpHelper = () => {
const customFetch = async (url, options = {}) => {
const defaultMethod = "GET"
const defaultHeaders = {
"Content-Type": "application/json",
Accept: "application/json",
}
const controller = new AbortController()
options.signal = controller.signal
options.method = options.method || defaultMethod
options.headers = options.headers ?
{
...defaultHeaders,
...options.headers
} :
defaultHeaders
options.body = JSON.stringify(options.body) || false
if (!options.body) delete options.body
setTimeout(() => {
controller.abort()
}, 3000)
try {
const response = await fetch(url, options)
return await response.json()
} catch (err) {
return err
}
}
const get = (url, options = {}) => customFetch(url, options)
const post = (url, options) => {
options.method = "POST"
return customFetch(url, options)
}
const put = (url, options) => {
options.method = "PUT"
return customFetch(url, options)
}
const del = (url, options) => {
options.method = "DELETE"
return customFetch(url, options)
}
return {
get,
post,
put,
del,
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment