Created
February 7, 2020 06:16
-
-
Save parzibyte/4a43faf562c81ff96c377caa52b46c55 to your computer and use it in GitHub Desktop.
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
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