Skip to content

Instantly share code, notes, and snippets.

@jonmircha
Last active May 9, 2024 16:03
Show Gist options
  • Save jonmircha/19a43c5714a398830d6834084f253a0a to your computer and use it in GitHub Desktop.
Save jonmircha/19a43c5714a398830d6834084f253a0a to your computer and use it in GitHub Desktop.
Script que te permite simplificar peticiones HTTP con Fetch, esta escrita en VanillaJS por lo que puedes usarla con cualquier framework o librería
export const helpHttp = () => {
const customFetch = (endpoint, options) => {
const defaultHeader = {
accept: "application/json",
};
const controller = new AbortController();
options.signal = controller.signal;
options.method = options.method || "GET";
options.headers = options.headers
? { ...defaultHeader, ...options.headers }
: defaultHeader;
options.body = JSON.stringify(options.body) || false;
if (!options.body) delete options.body;
//console.log(options);
setTimeout(() => controller.abort(), 3000);
return fetch(endpoint, options)
.then((res) =>
res.ok
? res.json()
: Promise.reject({
err: true,
status: res.status || "00",
statusText: res.statusText || "Ocurrió un error",
})
)
.catch((err) => 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,
};
};
@Bmez1
Copy link

Bmez1 commented Nov 18, 2021

Muchas gracias profe

@mokadev90
Copy link

Después de varios meses de haber visto la clase donde lo desarrollábamos ahora estoy por usar tu librería para un proyecto real, Gracias totales Jon!!

@GPCubo
Copy link

GPCubo commented Dec 8, 2021

Gracias Profe!<3

@D-landJS
Copy link

Excelente maestro, espero mejorar pronto su funcionalidad :D

@brayanmunoz11
Copy link

Grande JonMircha <3

@projecto2021
Copy link

Eres el numero uno profe

@Luiscadt
Copy link

El mejor profesor

@Gerard-strock
Copy link

Fenomenal!

@pedrosorrentino
Copy link

Muchas gracias! Tus clases de React son TOP

@lucascincunegui
Copy link

que Dios te recontra bendiga, muchas gracias!

@DiegoGutierrezP
Copy link

THANKS

@chelosilvero78
Copy link

gracias prof... excelente.. minimalismo en su maxima expresión.. :)

@Josephdiaz99
Copy link

Gracias Profesor!

@Neira21
Copy link

Neira21 commented Jul 16, 2022

Muchas Gracias

@DavidxAponte
Copy link

Muchas gracias por tu dedicacion

@JosueDuran08z
Copy link

¡Muchas gracias!

@kralion
Copy link

kralion commented Sep 16, 2022

Gracias PROFE,

@jesusds603
Copy link

Eres grande, Drake

@avielLV
Copy link

avielLV commented Nov 10, 2022

muchas gracias

@danielInteriano
Copy link

Muy buen aporte...ya lo había escrito seguiendo el curso, pero gracias.

@jesuschirii
Copy link

Graciasss!

@Joel-x-x
Copy link

Gracias amigo!!

@000EZE000
Copy link

Gracias Profe!!!

@hd-esteban
Copy link

gracias

@Denilpv08
Copy link

Muchas Gracias!!!

@szarap
Copy link

szarap commented Feb 20, 2023

thank you!

@Gonzza14
Copy link

Gonzza14 commented Apr 1, 2023

Muchas gracias Jon, sos un grande!

@Angel-Raa
Copy link

Mucha gracias!

@ChrisGlz14
Copy link

El mejor enseñando, no hay profe como el!

@Christopher-37
Copy link

Gracias por siempre compartir tus grandes conocimientos con el mundo :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment