diff --git a/.drone.yml b/.drone.yml index 381f7d0..5fde9cb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -64,21 +64,4 @@ steps: - cp -a $${ENV_PATH}/${DRONE_BRANCH}/. $${BUILD_PATH}/${DRONE_BRANCH} - docker-compose build - docker-compose up -d -# - name: telgram_notify -# image: appleboy/drone-telegram -# when: -# status: -# - success -# - failure -# settings: -# token: -# from_secret: telegram_token -# to: -# from_secret: telegram_chat_id -# format: markdown -# message: > -# {{#success build.status}}🤓{{else}}😨{{/success}} -# [{{repo.name}} / {{commit.branch}}]({{ build.link }}) -# ``` -# {{ commit.message }} -# ``` + diff --git a/src/redux/store.ts b/src/redux/store.ts index e8212ab..48328a5 100644 --- a/src/redux/store.ts +++ b/src/redux/store.ts @@ -69,13 +69,13 @@ export function configureStore(): { store: Store; persistor: Persistor } { // Pass token to axios api.interceptors.request.use(options => { - const token = store.getState().user.token; + const token = store.getState().user.user.token; if (!token) { return options; } - return assocPath(['headers', 'authorization'], `Bearer ${token}`, options); + return assocPath(['headers', 'authorization'], token, options); }); // Logout on 401 diff --git a/src/utils/api/index.ts b/src/utils/api/index.ts index fae0f68..38d0a38 100644 --- a/src/utils/api/index.ts +++ b/src/utils/api/index.ts @@ -6,6 +6,8 @@ import { LatLngLiteral } from 'leaflet'; import { IRoute } from '~/redux/map/types'; import { INominatimResult } from '~/redux/types'; import { api } from './instance'; +import { postMapInterceptor } from '~/utils/api/interceptors'; +import { PostMapRequest, PostMapResponse } from '~/utils/api/types'; interface IGetRouteList { min: number; @@ -62,15 +64,9 @@ export const postMap = ({ provider, is_public, description, -}: Partial & { - force: boolean; -}) => +}: PostMapRequest) => api - .post<{ - route: IRoute; - error?: string; - code?: string; - }>( + .post( API.POST_MAP, { route: { @@ -86,7 +82,7 @@ export const postMap = ({ }, force, }, - ); + ).catch(postMapInterceptor); export const checkIframeToken = ({ viewer_id, diff --git a/src/utils/api/interceptors.ts b/src/utils/api/interceptors.ts new file mode 100644 index 0000000..a4b52d8 --- /dev/null +++ b/src/utils/api/interceptors.ts @@ -0,0 +1,14 @@ +import { AxiosError } from 'axios'; +import { PostMapResponse } from '~/utils/api/types'; + +export const postMapInterceptor = (res: AxiosError) => { + if (res.response?.data.code) { + return res.response; + } + + if (res.response?.data.error) { + throw new Error(res.response?.data.error); + } + + throw res; +}; diff --git a/src/utils/api/types.ts b/src/utils/api/types.ts new file mode 100644 index 0000000..2fdf3a1 --- /dev/null +++ b/src/utils/api/types.ts @@ -0,0 +1,11 @@ +import { IRoute } from '~/redux/map/types'; + +export interface PostMapResponse { + route: IRoute; + error?: string; + code?: string; +} + +export type PostMapRequest = Partial & { + force: boolean; +}