ui: pick api_middleware.ts from saas
This commit is contained in:
parent
25e6862ce6
commit
3dd31dc0a7
2 changed files with 60 additions and 68 deletions
|
|
@ -1,68 +0,0 @@
|
|||
import logger from 'App/logger';
|
||||
import APIClient from './api_client';
|
||||
import { FETCH_ACCOUNT, UPDATE_JWT } from './duck/user';
|
||||
import { handleSpotJWT } from "App/utils";
|
||||
|
||||
export default () => (next) => (action) => {
|
||||
const { types, call, ...rest } = action;
|
||||
if (!call) {
|
||||
return next(action);
|
||||
}
|
||||
const [REQUEST, SUCCESS, FAILURE] = types;
|
||||
next({ ...rest, type: REQUEST });
|
||||
const client = new APIClient();
|
||||
|
||||
return call(client)
|
||||
.then(async (response) => {
|
||||
// if (response.status === 403) {
|
||||
// next({ type: FETCH_ACCOUNT.FAILURE });
|
||||
// }
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
return Promise.reject(text);
|
||||
}
|
||||
return response.json();
|
||||
})
|
||||
.then((json) => json || {}) // TEMP TODO on server: no empty responses
|
||||
.then(({ jwt, spotJwt, errors, data }) => {
|
||||
if (errors) {
|
||||
next({ type: FAILURE, errors, data });
|
||||
} else {
|
||||
next({ type: SUCCESS, data, ...rest });
|
||||
}
|
||||
if (jwt) {
|
||||
next({ type: UPDATE_JWT, data: { jwt } });
|
||||
}
|
||||
if (spotJwt) {
|
||||
handleSpotJWT(spotJwt);
|
||||
}
|
||||
})
|
||||
.catch(async (e) => {
|
||||
if (e.response?.status === 403) {
|
||||
next({ type: FETCH_ACCOUNT.FAILURE });
|
||||
}
|
||||
|
||||
const data = await e.response?.json();
|
||||
logger.error('Error during API request. ', e);
|
||||
return next({ type: FAILURE, errors: data ? parseError(data.errors) : [] });
|
||||
});
|
||||
};
|
||||
|
||||
export function parseError(e) {
|
||||
try {
|
||||
return [...JSON.parse(e).errors] || [];
|
||||
} catch {
|
||||
return Array.isArray(e) ? e : [e];
|
||||
}
|
||||
}
|
||||
|
||||
function jwtExpired(token) {
|
||||
try {
|
||||
const base64Url = token.split('.')[1];
|
||||
const base64 = base64Url.replace('-', '+').replace('_', '/');
|
||||
const tokenObj = JSON.parse(window.atob(base64));
|
||||
return tokenObj.exp * 1000 < Date.now(); // exp in Unix time (sec)
|
||||
} catch (e) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
60
frontend/app/api_middleware.ts
Normal file
60
frontend/app/api_middleware.ts
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
import logger from 'App/logger';
|
||||
import APIClient from './api_client';
|
||||
import { FETCH_ACCOUNT, UPDATE_JWT } from 'Duck/user';
|
||||
import { handleSpotJWT } from "App/utils";
|
||||
|
||||
export default () => {
|
||||
return (next: any) => async (action: any) => {
|
||||
const { types, call, ...rest } = action;
|
||||
|
||||
if (!call) {
|
||||
return next(action);
|
||||
}
|
||||
|
||||
const [REQUEST, SUCCESS, FAILURE] = types;
|
||||
next({ ...rest, type: REQUEST });
|
||||
|
||||
try {
|
||||
const client = new APIClient();
|
||||
const response = await call(client);
|
||||
|
||||
if (!response.ok) {
|
||||
const text = await response.text();
|
||||
throw new Error(text);
|
||||
}
|
||||
|
||||
const json = await response.json() || {}; // TEMP TODO on server: no empty responses
|
||||
const { jwt, spotJwt, errors, data } = json;
|
||||
|
||||
if (errors) {
|
||||
next({ type: FAILURE, errors, data });
|
||||
} else {
|
||||
next({ type: SUCCESS, data, ...rest });
|
||||
}
|
||||
|
||||
if (jwt) {
|
||||
next({ type: UPDATE_JWT, data: jwt });
|
||||
}
|
||||
if (spotJwt) {
|
||||
handleSpotJWT(spotJwt);
|
||||
}
|
||||
|
||||
} catch (e) {
|
||||
if (e.response?.status === 403) {
|
||||
next({ type: FETCH_ACCOUNT.FAILURE });
|
||||
}
|
||||
|
||||
const data = await e.response?.json();
|
||||
logger.error('Error during API request. ', e);
|
||||
return next({ type: FAILURE, errors: data ? parseError(data.errors) : [] });
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export function parseError(e: any) {
|
||||
try {
|
||||
return [...JSON.parse(e).errors] || [];
|
||||
} catch {
|
||||
return Array.isArray(e) ? e : [e];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue