diff --git a/src/redux/auth/api.ts b/src/redux/auth/api.ts index 664bbc1a..6253169f 100644 --- a/src/redux/auth/api.ts +++ b/src/redux/auth/api.ts @@ -1,22 +1,31 @@ -import {api, authMiddleware, errorMiddleware, resultMiddleware} from "~/utils/api"; -import { API } from "~/constants/api"; -import {IResultWithStatus} from "~/redux/types"; -import {authMeTransform, userLoginTransform} from "~/redux/auth/transforms"; -import {IUser} from "~/redux/auth/types"; +import { + api, + authMiddleware, + errorMiddleware, + resultMiddleware, + configWithToken, +} from '~/utils/api'; +import { API } from '~/constants/api'; +import { IResultWithStatus } from '~/redux/types'; +import { authMeTransform, userLoginTransform } from '~/redux/auth/transforms'; +import { IUser } from '~/redux/auth/types'; -export const apiUserLogin = ( - { username, password }: - { username: string, password: string } -): Promise> => ( - api.post(API.USER.LOGIN, { username, password }) +export const apiUserLogin = ({ + username, + password, +}: { + username: string; + password: string; +}): Promise> => + api + .post(API.USER.LOGIN, { username, password }) .then(resultMiddleware) .catch(errorMiddleware) - .then(userLoginTransform) -); + .then(userLoginTransform); -export const getAuthSelf = (): Promise> => ( - api.get(API.USER.ME) +export const getAuthSelf = ({ access }): Promise> => + api + .get(API.USER.ME, configWithToken(access)) .then(resultMiddleware) .catch(errorMiddleware) - .then(authMeTransform) -); + .then(authMeTransform); diff --git a/src/utils/api/index.ts b/src/utils/api/index.ts index 84937899..eaad04e4 100644 --- a/src/utils/api/index.ts +++ b/src/utils/api/index.ts @@ -1,7 +1,8 @@ -import axios from 'axios'; -import { API } from "~/constants/api"; +import axios, { AxiosRequestConfig } from 'axios'; +import { API } from '~/constants/api'; import { store } from '~/redux/store'; -import { push } from "connected-react-router"; +import { push } from 'connected-react-router'; +import { IResultWithStatus } from '~/redux/types'; export const authMiddleware = r => { store.dispatch(push('/login')); @@ -12,5 +13,40 @@ export const api = axios.create({ baseURL: API.BASE, }); -export const resultMiddleware = ({ status, data }) => ({ status, data }); -export const errorMiddleware = ({ status, data, response }) => ({ status, data: data || { response } }); +export const HTTP_RESPONSES = { + SUCCESS: 200, + CREATED: 201, + CONNECTION_REFUSED: 408, + BAD_REQUEST: 400, + UNAUTHORIZED: 401, + NOT_FOUND: 404, + TOO_MANY_REQUESTS: 429, +}; + +export const resultMiddleware = ({ + status, + data, +}: { + status: number; + data: T; +}): { status: number; data: T } => { + return data && { status, data }; +}; + +export const errorMiddleware = (debug): IResultWithStatus => + debug && debug.response + ? debug.response + : { + status: HTTP_RESPONSES.CONNECTION_REFUSED, + data: null, + debug, + error: 'Network_Disconnected', + }; + +export const configWithToken = ( + access: string, + config: AxiosRequestConfig = {}, +): AxiosRequestConfig => ({ + ...config, + headers: { ...(config.headers || {}), Authorization: `Bearer ${access}` }, +}); diff --git a/tslint.json b/tslint.json index 7718f5f7..848eed60 100644 --- a/tslint.json +++ b/tslint.json @@ -22,7 +22,8 @@ "jsx-curly-spacing": [true, "never"], "jsx-equals-spacing": [true, "never"], "jsx-no-multiline-js": false, - "jsx-boolean-value": false + "jsx-boolean-value": false, + "prefer-array-literal": false }, "rulesDirectory": [] }