diff --git a/src/service/network-service.js b/src/service/network-service.js index fa0ef72..e1ee1ab 100644 --- a/src/service/network-service.js +++ b/src/service/network-service.js @@ -1,5 +1,5 @@ -import { storeGet } from '../store'; -import * as Sentry from "@sentry/browser"; +import { storeGet, storeSet } from '../store'; +import * as Sentry from '@sentry/browser'; const LOCAL_DEVELOPMENT = process.env.REACT_APP_LOCAL_DEVELOPMENT; @@ -7,13 +7,13 @@ export class NetworkService { static TOKEN_KEY static REFRESH_TOKEN_KEY - static logApiError = (endpoint, status = 0, resp) => { - try { - if ([500].includes(status)) { - if (LOCAL_DEVELOPMENT !== '1') { + static logApiError = (endpoint, status = 0, resp = {}) => { + if (status === 500) { + if (LOCAL_DEVELOPMENT !== '1') { + try { Sentry.init({ - dsn: "https://e7b2134f7d816f663bb83e51b106a694@o4508381921738752.ingest.de.sentry.io/4508381935501392", - environment: process.env.NODE_ENV || "development" + dsn: 'https://e7b2134f7d816f663bb83e51b106a694@o4508381921738752.ingest.de.sentry.io/4508381935501392', + environment: process.env.NODE_ENV || 'development' }); const error = new Error(`Status ${status}`); @@ -24,53 +24,19 @@ export class NetworkService { details: resp } }); + } catch (err) { + console.log(err); } } - } catch (err) { - console.log(err); + } else if (status === 403) { + storeSet.main.token(''); + const { pathname } = window.location; + if (!['/login', '/loginadmin', '/reset-password'].includes(pathname)) { + window.location.replace('/login'); + } } } - static postEmptyResponse = (url, body, callback, errorCallback) => { - fetch(url, { - method: 'POST', - mode: 'cors', - headers: { - 'Content-Type': 'application/json', - 'Authorization': storeGet.main.getToken(), - 'Access-Control-Allow-Origin': '*' - }, - body: JSON.stringify(body) - }) - .then(data => { - if (data.status >= 400 && data.status <= 599) - errorCallback(data.status) - else - callback() - }) - .catch(err => errorCallback(err)); - }; - - static putEmptyResponse = (url, body, callback, errorCallback) => { - fetch(url, { - method: 'PUT', - mode: 'cors', - headers: { - 'Content-Type': 'application/json', - 'Authorization': storeGet.main.getToken(), - 'Access-Control-Allow-Origin': '*' - }, - body: JSON.stringify(body) - }) - .then(data => { - if (data.status >= 400 && data.status <= 599) - errorCallback(data.status) - else - callback() - }) - .catch(err => errorCallback(err)); - }; - static post = (url, body, callback, errorCallback, queryParams) => { if (queryParams) { @@ -101,6 +67,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; + this.logApiError(url, status); return { response: await response.json(), status: status } }) .then(data => { @@ -144,6 +111,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; + this.logApiError(url, status); return { response: await response.json(), status: status } }) .then(data => { @@ -187,6 +155,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; + this.logApiError(url, status); return { response: await response.blob(), status: status } }) .then(data => { @@ -228,6 +197,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; + this.logApiError(url, status); return { response: await response.json(), status: status } }) .then(data => { @@ -241,28 +211,6 @@ export class NetworkService { .catch(err => errorCallback(err)); }; - static patch = (url, body, callback, errorCallback) => { - fetch(url, { - method: 'PATCH', - mode: 'cors', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify(body) - }) - .then(async response => { - let status = response.status; - return { response: await response.json(), status: status } - }) - .then(data => { - if (data.status >= 400 && data.status <= 599) - errorCallback(data.response) - else - callback(data.response) - }) - .catch(err => errorCallback(err)); - }; - static put = (url, body, callback, errorCallback, queryParams = null) => { if (queryParams) { url += '?' @@ -293,6 +241,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; + this.logApiError(url, status); return { response: await response.json(), status: status } }) .then(data => { @@ -306,79 +255,6 @@ export class NetworkService { .catch(err => errorCallback(err)); }; - static unauthorizedPostEmptyResponse = (url, body, callback, errorCallback) => { - fetch(url, { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*' - }, - body: JSON.stringify(body) - }) - .then(data => callback(data)) - .catch(err => errorCallback(err)); - - }; - - static unauthorizedPutEmptyResponse = (url, body, callback, errorCallback) => { - fetch(url, { - method: 'PUT', - headers: { - 'Content-Type': 'application/json', - 'Access-Control-Allow-Origin': '*' - }, - body: JSON.stringify(body) - }) - .then(data => callback(data)) - .catch(err => errorCallback(err)); - - }; - - static unauthorizedGet = (url, queryParams, callback, errorCallback) => { - fetch(url, { - method: 'GET', - mode: 'cors', - headers: { - 'Content-Type': 'application/json', - } - }) - .then(async response => { - let status = response.status; - return { response: await response.json(), status: status } - }) - .then(data => { - if (data.status >= 400 && data.status <= 599) - errorCallback(data.response) - else - callback(data.response) - }) - .catch(err => errorCallback(err)); - - }; - - static unauthorizedPatch = (url, body, callback, errorCallback) => { - fetch(url, { - method: 'PATCH', - mode: 'cors', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(body) - }) - .then(async response => { - let status = response.status; - return { response: await response.json(), status: status } - }) - .then(data => { - if (data.status >= 400 && data.status <= 599) - errorCallback(data.response) - else - callback(data.response) - }) - .catch(err => errorCallback(err)); - - }; - static isNotBlank(value) { return value !== null && value !== undefined && value !== '' } @@ -413,7 +289,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; - //console.log('status in fetch:', status) + this.logApiError(url, status); return { response: await response.json(), status: status } }) .then(data => { @@ -459,6 +335,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; + this.logApiError(url, status); return { response: await response.blob(), status: status } }) .then(data => { @@ -472,53 +349,6 @@ export class NetworkService { .catch(err => errorCallback(err)); }; - static promiseGet = async (url, queryParams = null) => { - const response = await fetch(url, { - method: 'GET', - mode: 'cors', - headers: { - 'Content-Type': 'application/json', - 'Authorization': 'Bearer ' + storeGet.main.getToken(), - 'Access-Control-Allow-Origin': '*' - } - }); - - const json = await response.json(); - return json; - } - - static deleteEmptyResponse = (url, callback, errorCallback, queryParams = null) => { - if (queryParams) { - let params = '?' - - for (let i = 0; i < queryParams.length; i++) { - params += queryParams[i][0] + '=' + queryParams[i][1] - if (queryParams.length !== i + 1) - params += '&' - url += params - params = '' - } - - } - - fetch(url, { - method: 'DELETE', - mode: 'cors', - headers: { - 'Content-Type': 'application/json', - 'Authorization': storeGet.main.getToken(), - 'Access-Control-Allow-Origin': '*' - } - }) - .then(data => { - if (data.status >= 400 && data.status <= 599) - errorCallback(data.status) - else - callback() - }) - .catch(err => errorCallback(err)); - } - static delete = (url, body, callback, errorCallback, queryParams = null) => { if (queryParams) { let params = '?' @@ -543,6 +373,7 @@ export class NetworkService { }) .then(async response => { let status = response.status; + this.logApiError(url, status); return { response: await response.json(), status: status } }) .then(data => {