mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-25 12:56:41 +07:00
added transforms and request wrappers
This commit is contained in:
parent
e119ae5fc4
commit
cb8ca5248d
3 changed files with 68 additions and 22 deletions
|
@ -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<IResultWithStatus<{ token: string, status?: number }>> => (
|
||||
api.post(API.USER.LOGIN, { username, password })
|
||||
export const apiUserLogin = ({
|
||||
username,
|
||||
password,
|
||||
}: {
|
||||
username: string;
|
||||
password: string;
|
||||
}): Promise<IResultWithStatus<{ token: string; status?: number }>> =>
|
||||
api
|
||||
.post(API.USER.LOGIN, { username, password })
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware)
|
||||
.then(userLoginTransform)
|
||||
);
|
||||
.then(userLoginTransform);
|
||||
|
||||
export const getAuthSelf = (): Promise<IResultWithStatus<{ user: IUser }>> => (
|
||||
api.get(API.USER.ME)
|
||||
export const getAuthSelf = ({ access }): Promise<IResultWithStatus<{ user: IUser }>> =>
|
||||
api
|
||||
.get(API.USER.ME, configWithToken(access))
|
||||
.then(resultMiddleware)
|
||||
.catch(errorMiddleware)
|
||||
.then(authMeTransform)
|
||||
);
|
||||
.then(authMeTransform);
|
||||
|
|
|
@ -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 = <T extends {}>({
|
||||
status,
|
||||
data,
|
||||
}: {
|
||||
status: number;
|
||||
data: T;
|
||||
}): { status: number; data: T } => {
|
||||
return data && { status, data };
|
||||
};
|
||||
|
||||
export const errorMiddleware = <T extends any>(debug): IResultWithStatus<T> =>
|
||||
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}` },
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue