Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created February 7, 2020 06:16
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 parzibyte/4a43faf562c81ff96c377caa52b46c55 to your computer and use it in GitHub Desktop.
Save parzibyte/4a43faf562c81ff96c377caa52b46c55 to your computer and use it in GitHub Desktop.
const RUTA_SERVIDOR = "ruta";
const manejarRespuesta = (respuesta) => {
// Manejar respuesta como se necesite. Yo lo hago así:
if (respuesta.error) {
throw new Error(JSON.stringify(respuesta.error));
}
return respuesta.data;
};
export const HttpClient = {
post: (ruta, datos) => fetch(RUTA_SERVIDOR + ruta, {
credentials: 'include',
method: 'POST',
body: JSON.stringify(datos),
})
.then(r => r.json())
.then(manejarRespuesta),
put: (ruta, datos) => fetch(RUTA_SERVIDOR + ruta, {
credentials: 'include',
method: 'PUT',
body: JSON.stringify(datos),
})
.then(r => r.json())
.then(manejarRespuesta),
get: ruta => fetch(RUTA_SERVIDOR + ruta, {
credentials: 'include',
})
.then(r => r.json())
.then(manejarRespuesta),
delete: ruta => fetch(RUTA_SERVIDOR + ruta, {
credentials: 'include',
method: 'DELETE',
})
.then(r => r.json())
.then(manejarRespuesta),
};
export default { HttpClient };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment