From 0611020027e0e8173de80da672ac30d62617e668 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Wed, 6 May 2020 19:32:08 +0300 Subject: [PATCH] axiosApi --- package-lock.json | 31 ++++++++++++++++++ package.json | 1 + src/api/axiosApi.js | 65 ++++++++++++++++++++++++++++++++++++++ src/features/system/api.js | 2 +- 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/api/axiosApi.js diff --git a/package-lock.json b/package-lock.json index 2a3acc7..0727054 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1548,6 +1548,37 @@ "integrity": "sha512-wMHVg2EOHaMRxbzgFJ9gtjOOCrI80OHLG14rxi28XwOW8ux6IiEbRCGGGqCtdAIg4FQCbW20k9RsT4y3gJlFug==", "dev": true }, + "axios": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz", + "integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==", + "requires": { + "follow-redirects": "1.5.10" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + }, + "follow-redirects": { + "version": "1.5.10", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.10.tgz", + "integrity": "sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==", + "requires": { + "debug": "=3.1.0" + } + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + } + } + }, "babel-eslint": { "version": "10.0.1", "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.0.1.tgz", diff --git a/package.json b/package.json index 842892f..e9e97c0 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "serve:build": "http-server ./build" }, "dependencies": { + "axios": "^0.19.2", "bootstrap": "4.3.1", "immer": "2.1.3", "prop-types": "15.7.2", diff --git a/src/api/axiosApi.js b/src/api/axiosApi.js new file mode 100644 index 0000000..0856b75 --- /dev/null +++ b/src/api/axiosApi.js @@ -0,0 +1,65 @@ +import axios from "axios"; + +function getHeaders() { + return { + "Content-Type": "application/json" + }; +} + +function internalRequest(url, options) { + return axios + .request(url, options) + .then((res) => res.data) + .catch(function (error) { + if (error.response && error.response.data) { + throw ( + { + ...error.response.data, + message: error.response.data.detail || error.response.data.title + } || error + ); + } + // The request was made but no response was received + // `error.request` is an instance of XMLHttpRequest in the browser and an instance of + // http.ClientRequest in node.js + throw error; + }); +} + +export function post(url, data) { + const options = { + method: "post", + data: JSON.stringify(data), + headers: getHeaders() + }; + + return internalRequest(url, options); +} + +export function put(url, data) { + const options = { + method: "put", + data: JSON.stringify(data), + headers: getHeaders() + }; + + return internalRequest(url, options); +} + +export function del(url, data) { + const options = { + method: "delete", + data: JSON.stringify(data), + headers: getHeaders() + }; + + return internalRequest(url, options); +} + +export function get(url) { + const options = { + method: "GET", + headers: getHeaders() + }; + return internalRequest(url, options); +} diff --git a/src/features/system/api.js b/src/features/system/api.js index 9f2058b..7e8dad2 100644 --- a/src/features/system/api.js +++ b/src/features/system/api.js @@ -1,4 +1,4 @@ -import { get } from "../../api/api"; +import { get } from "../../api/axiosApi"; const baseUrl = process.env.REVERSE_PROXY_API_URL + "/system"; const getSystemDateTime = () => get(`${baseUrl}/datetime`);