From 3dd31dc0a7440c3652aaab5b4556746608114968 Mon Sep 17 00:00:00 2001 From: nick-delirium Date: Fri, 6 Sep 2024 16:13:09 +0200 Subject: [PATCH] ui: pick api_middleware.ts from saas --- frontend/app/api_middleware.js | 68 ---------------------------------- frontend/app/api_middleware.ts | 60 ++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 68 deletions(-) delete mode 100644 frontend/app/api_middleware.js create mode 100644 frontend/app/api_middleware.ts diff --git a/frontend/app/api_middleware.js b/frontend/app/api_middleware.js deleted file mode 100644 index e856617b5..000000000 --- a/frontend/app/api_middleware.js +++ /dev/null @@ -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; - } -} diff --git a/frontend/app/api_middleware.ts b/frontend/app/api_middleware.ts new file mode 100644 index 000000000..bd440a871 --- /dev/null +++ b/frontend/app/api_middleware.ts @@ -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]; + } +} \ No newline at end of file