Skip to content

Instantly share code, notes, and snippets.

@7pulse
Created June 10, 2023 18:40
Show Gist options
  • Save 7pulse/249e579cdb719ca0e771586356193c83 to your computer and use it in GitHub Desktop.
Save 7pulse/249e579cdb719ca0e771586356193c83 to your computer and use it in GitHub Desktop.
JavaScript Fetcher Function (with documentation from chatGPT ^^)
/**
* Performs an HTTP request with the specified method, URL, and optional data.
*
* @param {string} method - The HTTP method to be used for the request.
* @param {string} url - The URL to which the request will be sent.
* @param {object} [data] - The data to be sent with the request body. It should be in JSON format.
* @throws {Error} If an error occurs during the request or if the response status is not in the 200-299 range.
*/
const fetcher = async (method, url, data) => {
const options = {
method,
headers: {},
};
if (data) {
options.headers['Content-Type'] = 'application/json';
options.body = JSON.stringify(data);
}
try {
const response = await fetch(`${BASE_URL}${url}`, options);
if (!response.ok) {
throw new Error('Error en la petición');
}
const json = await response.json();
} catch (error) {
console.log(error);
throw error;
}
};
/**
* Returns a bound version of the fetcher function with the HTTP method set to 'GET'.
*
* @param {string} url - The URL to which the GET request will be sent.
* @returns {Function} A bound version of the fetcher function with the HTTP method set to 'GET'.
*/
export const get = (url) => fetcher.bind(null, 'GET');
/**
* Returns a bound version of the fetcher function with the HTTP method set to 'POST'.
*
* @param {string} url - The URL to which the POST request will be sent.
* @param {object} data - The data to be sent with the request body. It should be in JSON format.
* @returns {Function} A bound version of the fetcher function with the HTTP method set to 'POST'.
*/
export const post = (url, data) => fetcher.bind(null, 'POST');
/**
* Returns a bound version of the fetcher function with the HTTP method set to 'PUT'.
*
* @param {string} url - The URL to which the PUT request will be sent.
* @param {object} data - The data to be sent with the request body. It should be in JSON format.
* @returns {Function} A bound version of the fetcher function with the HTTP method set to 'PUT'.
*/
export const put = (url, data) => fetcher.bind(null, 'PUT');
/**
* Returns a bound version of the fetcher function with the HTTP method set to 'DELETE'.
*
* @param {string} url - The URL to which the DELETE request will be sent.
* @returns {Function} A bound version of the fetcher function with the HTTP method set to 'DELETE'.
*/
export const del = (url) => fetcher.bind(null, 'DELETE');
// How to use it
// import { get, post, put, del } from './api';
/*
const endpoints = {
users: '/users',
user: (id) => `/users/${id}`,
}
const getUsers = get(endpoints.users);
const getUser = get(endpoints.user(1));
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment