fixed loading route and maps

This commit is contained in:
Fedor Katurov 2019-12-12 13:52:30 +07:00
parent 1f774a8299
commit 62cb8d8e18
18 changed files with 715 additions and 369 deletions

50
src/utils/middleware.ts Normal file
View file

@ -0,0 +1,50 @@
import { AxiosRequestConfig } from "axios";
export type Unwrap<T> = T extends (...args: any[]) => Promise<infer U> ? U : T;
export interface IApiErrorResult {
detail?: string;
code?: string;
}
export interface IResultWithStatus<T> {
status: any;
data?: Partial<T> & IApiErrorResult;
error?: string;
debug?: string;
}
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 } => ({ status, data }));
export const errorMiddleware = <T extends any>(debug): IResultWithStatus<T> => (debug && debug.response
? debug.response
: {
status: HTTP_RESPONSES.CONNECTION_REFUSED,
data: {},
debug,
error: 'Ошибка сети',
});
export const configWithToken = (
token: string,
config: AxiosRequestConfig = {},
): AxiosRequestConfig => ({
...config,
headers: { ...(config.headers || {}), Authorization: `${token}` },
});