diff --git a/src/api/auth/index.ts b/src/api/auth/index.ts index 0c1526c6..2408f44a 100644 --- a/src/api/auth/index.ts +++ b/src/api/auth/index.ts @@ -1,5 +1,8 @@ import { TelegramUser } from '@v9v/ts-react-telegram-login'; +import { API } from '~/constants/api'; +import { api, unwrap } from '~/utils/api'; + import { ApiAttachSocialRequest, ApiAttachSocialResult, @@ -22,24 +25,20 @@ import { ApiUpdateUserResult, ApiUserLoginRequest, ApiUserLoginResult, -} from '~/api/auth/types'; -import { API } from '~/constants/api'; -import { api, cleanResult } from '~/utils/api'; +} from './types'; export const apiUserLogin = ({ username, password }: ApiUserLoginRequest) => api .post(API.USER.LOGIN, { username, password }) - .then(cleanResult); + .then(unwrap); export const apiAuthGetUser = () => - api.get(API.USER.ME).then(cleanResult); + api.get(API.USER.ME).then(unwrap); export const apiAuthGetUserProfile = ({ username, }: ApiAuthGetUserProfileRequest) => - api - .get(API.USER.PROFILE(username)) - .then(cleanResult); + api.get(API.USER.PROFILE(username)).then(unwrap); export const apiAuthGetUpdates = ({ exclude_dialogs, @@ -49,44 +48,40 @@ export const apiAuthGetUpdates = ({ .get(API.USER.GET_UPDATES, { params: { exclude_dialogs, last }, }) - .then(cleanResult); + .then(unwrap); export const apiUpdateUser = ({ user }: ApiUpdateUserRequest) => - api.patch(API.USER.ME, user).then(cleanResult); + api.patch(API.USER.ME, user).then(unwrap); export const apiUpdatePhoto = ({ file }: ApiUpdatePhotoRequest) => - api.post(API.USER.UPDATE_PHOTO, file).then(cleanResult); + api.post(API.USER.UPDATE_PHOTO, file).then(unwrap); export const apiUpdateCover = ({ file }: ApiUpdatePhotoRequest) => - api.post(API.USER.UPDATE_COVER, file).then(cleanResult); + api.post(API.USER.UPDATE_COVER, file).then(unwrap); export const apiRequestRestoreCode = (field: string) => - api - .post<{ field: string }>(API.USER.REQUEST_CODE(), { field }) - .then(cleanResult); + api.post<{ field: string }>(API.USER.REQUEST_CODE(), { field }).then(unwrap); export const apiCheckRestoreCode = ({ code }: ApiCheckRestoreCodeRequest) => - api - .get(API.USER.REQUEST_CODE(code)) - .then(cleanResult); + api.get(API.USER.REQUEST_CODE(code)).then(unwrap); export const apiRestoreCode = ({ code, password }: ApiRestoreCodeRequest) => api .put(API.USER.REQUEST_CODE(code), { password }) - .then(cleanResult); + .then(unwrap); export const apiGetSocials = () => - api.get(API.USER.GET_SOCIALS).then(cleanResult); + api.get(API.USER.GET_SOCIALS).then(unwrap); export const apiDropSocial = ({ id, provider }: ApiDropSocialRequest) => api .delete(API.USER.DROP_SOCIAL(provider, id)) - .then(cleanResult); + .then(unwrap); export const apiAttachSocial = ({ token }: ApiAttachSocialRequest) => api .post(API.USER.ATTACH_SOCIAL, { token }) - .then(cleanResult); + .then(unwrap); export const apiLoginWithSocial = ({ token, @@ -99,7 +94,7 @@ export const apiLoginWithSocial = ({ username, password, }) - .then(cleanResult); + .then(unwrap); export const apiAttachTelegram = (data: TelegramUser) => api.post(API.USER.ATTACH_TELEGRAM, data); diff --git a/src/api/boris/index.ts b/src/api/boris/index.ts index 190e024e..ab884689 100644 --- a/src/api/boris/index.ts +++ b/src/api/boris/index.ts @@ -2,16 +2,16 @@ import axios from 'axios'; import { API } from '~/constants/api'; import { IGetGithubIssuesResult, StatBackend } from '~/types/boris'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; export const getBorisBackendStats = () => - api.get(API.BORIS.GET_BACKEND_STATS).then(cleanResult); + api.get(API.BORIS.GET_BACKEND_STATS).then(unwrap); export const getGithubIssues = () => { return axios .get(API.BORIS.GITHUB_ISSUES, { params: { state: 'all', sort: 'created' }, }) - .then(result => result.data) + .then(unwrap) .catch(() => []); }; diff --git a/src/api/flow/index.ts b/src/api/flow/index.ts index db2fe935..06f4859e 100644 --- a/src/api/flow/index.ts +++ b/src/api/flow/index.ts @@ -1,12 +1,12 @@ import { API } from '~/constants/api'; import { GetSearchResultsRequest, GetSearchResultsResult } from '~/types/flow'; import { PostCellViewRequest, PostCellViewResult } from '~/types/node'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; export const postCellView = ({ id, flow }: PostCellViewRequest) => api .post(API.NODES.SET_CELL_VIEW(id), { flow }) - .then(cleanResult); + .then(unwrap); export const getSearchResults = ({ text, @@ -17,4 +17,4 @@ export const getSearchResults = ({ .get(API.SEARCH.NODES, { params: { text, skip, take }, }) - .then(cleanResult); + .then(unwrap); diff --git a/src/api/lab/index.ts b/src/api/lab/index.ts index 770cf37e..42cc98cf 100644 --- a/src/api/lab/index.ts +++ b/src/api/lab/index.ts @@ -5,12 +5,21 @@ import { GetLabStatsResult, GetLabUpdatesResult, } from '~/types/lab'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; -export const getLabNodes = ({ offset, limit, sort, search }: GetLabNodesRequest) => +export const getLabNodes = ({ + offset, + limit, + sort, + search, +}: GetLabNodesRequest) => api - .get(API.LAB.NODES, { params: { offset, limit, sort, search } }) - .then(cleanResult); + .get(API.LAB.NODES, { + params: { offset, limit, sort, search }, + }) + .then(unwrap); -export const getLabStats = () => api.get(API.LAB.STATS).then(cleanResult); -export const getLabUpdates = () => api.get(API.LAB.UPDATES).then(cleanResult); +export const getLabStats = () => + api.get(API.LAB.STATS).then(unwrap); +export const getLabUpdates = () => + api.get(API.LAB.UPDATES).then(unwrap); diff --git a/src/api/messages/index.ts b/src/api/messages/index.ts index cbca3035..0b659a58 100644 --- a/src/api/messages/index.ts +++ b/src/api/messages/index.ts @@ -7,23 +7,31 @@ import { ApiSendMessageResult, } from '~/api/messages/types'; import { API } from '~/constants/api'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; -export const apiGetUserMessages = ({ username, after, before }: ApiGetUserMessagesRequest) => +export const apiGetUserMessages = ({ + username, + after, + before, +}: ApiGetUserMessagesRequest) => api .get(API.USER.MESSAGES(username), { params: { after, before }, }) - .then(cleanResult); + .then(unwrap); export const apiSendMessage = ({ username, message }: ApiSendMessageRequest) => api .post(API.USER.MESSAGE_SEND(username), { message }) - .then(cleanResult); + .then(unwrap); -export const apiDeleteMessage = ({ username, id, is_locked }: ApiDeleteMessageRequest) => +export const apiDeleteMessage = ({ + username, + id, + is_locked, +}: ApiDeleteMessageRequest) => api .delete(API.USER.MESSAGE_DELETE(username, id), { params: { is_locked }, }) - .then(cleanResult); + .then(unwrap); diff --git a/src/api/metadata/index.ts b/src/api/metadata/index.ts index e1e1de86..bc770f50 100644 --- a/src/api/metadata/index.ts +++ b/src/api/metadata/index.ts @@ -1,8 +1,10 @@ import { ApiGetEmbedYoutubeResult } from '~/api/metadata/types'; import { API } from '~/constants/api'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; export const apiGetEmbedYoutube = (ids: string[]) => api - .get(API.EMBED.YOUTUBE, { params: { ids: ids.join(',') } }) - .then(cleanResult); + .get(API.EMBED.YOUTUBE, { + params: { ids: ids.join(',') }, + }) + .then(unwrap); diff --git a/src/api/node/index.ts b/src/api/node/index.ts index 89bfa4a8..aebb2ebf 100644 --- a/src/api/node/index.ts +++ b/src/api/node/index.ts @@ -26,7 +26,7 @@ import { GetNodeDiffRequest, GetNodeDiffResult, } from '~/types/node'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; export type ApiPostNodeRequest = { node: INode }; export type ApiPostNodeResult = { @@ -45,7 +45,7 @@ export type ApiGetNodeCommentsResponse = { }; export const apiPostNode = ({ node }: ApiPostNodeRequest) => - api.post(API.NODES.SAVE, node).then(cleanResult); + api.post(API.NODES.SAVE, node).then(unwrap); export const getNodeDiff = ({ start, @@ -68,7 +68,7 @@ export const getNodeDiff = ({ with_valid, }, }) - .then(cleanResult); + .then(unwrap); export const apiGetNode = ( { id }: ApiGetNodeRequest, @@ -76,7 +76,7 @@ export const apiGetNode = ( ) => api .get(API.NODES.GET(id), config) - .then(cleanResult) + .then(unwrap) .then((data) => ({ node: data.node, last_seen: data.last_seen, @@ -90,13 +90,13 @@ export const apiGetNodeWithCancel = ({ id }: ApiGetNodeRequest) => { .get(API.NODES.GET(id), { cancelToken: cancelToken.token, }) - .then(cleanResult), + .then(unwrap), cancel: cancelToken.cancel, }; }; export const apiPostComment = ({ id, data }: ApiPostCommentRequest) => - api.post(API.NODES.COMMENT(id), data).then(cleanResult); + api.post(API.NODES.COMMENT(id), data).then(unwrap); export const apiLikeComment = ( nodeId: number, @@ -110,7 +110,7 @@ export const apiLikeComment = ( data, { cancelToken: options?.cancelToken }, ) - .then(cleanResult); + .then(unwrap); export const apiGetNodeComments = ({ id, @@ -121,31 +121,31 @@ export const apiGetNodeComments = ({ .get(API.NODES.COMMENT(id), { params: { take, skip }, }) - .then(cleanResult); + .then(unwrap); export const apiGetNodeRelated = ({ id }: ApiGetNodeRelatedRequest) => - api.get(API.NODES.RELATED(id)).then(cleanResult); + api.get(API.NODES.RELATED(id)).then(unwrap); export const apiPostNodeTags = ({ id, tags }: ApiPostNodeTagsRequest) => api .post(API.NODES.UPDATE_TAGS(id), { tags }) - .then(cleanResult); + .then(unwrap); export const apiDeleteNodeTag = ({ id, tagId }: ApiDeleteNodeTagsRequest) => api .delete(API.NODES.DELETE_TAG(id, tagId)) - .then(cleanResult); + .then(unwrap); export const apiPostNodeLike = ({ id }: ApiPostNodeLikeRequest) => - api.post(API.NODES.LIKE(id)).then(cleanResult); + api.post(API.NODES.LIKE(id)).then(unwrap); export const apiPostNodeHeroic = ({ id }: ApiPostNodeHeroicRequest) => - api.post(API.NODES.HEROIC(id)).then(cleanResult); + api.post(API.NODES.HEROIC(id)).then(unwrap); export const apiLockNode = ({ id, is_locked }: ApiLockNodeRequest) => api .delete(API.NODES.DELETE(id), { params: { is_locked } }) - .then(cleanResult); + .then(unwrap); export const apiLockComment = ({ id, @@ -158,4 +158,4 @@ export const apiLockComment = ({ is_locked: isLocked, }, }) - .then(cleanResult); + .then(unwrap); diff --git a/src/api/notes/index.ts b/src/api/notes/index.ts index 0e114dd5..605d87ef 100644 --- a/src/api/notes/index.ts +++ b/src/api/notes/index.ts @@ -6,26 +6,26 @@ import { ApiUpdateNoteRequest, } from '~/api/notes/types'; import { URLS } from '~/constants/urls'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; export const apiListNotes = ({ limit, offset, search }: ApiListNotesRequest) => api .get(URLS.NOTES, { params: { limit, offset, search } }) - .then(cleanResult); + .then(unwrap); export const apiCreateNote = ({ text }: ApiCreateNoteRequest) => api .post(URLS.NOTES, { text, }) - .then(cleanResult); + .then(unwrap); export const apiDeleteNote = (id: number) => - api.delete(URLS.NOTE(id)).then(cleanResult); + api.delete(URLS.NOTE(id)).then(unwrap); export const apiUpdateNote = ({ id, text }: ApiUpdateNoteRequest) => api .put(URLS.NOTE(id), { content: text, }) - .then(cleanResult); + .then(unwrap); diff --git a/src/api/notifications/settings.ts b/src/api/notifications/settings.ts index 232bbb94..93ef0d1e 100644 --- a/src/api/notifications/settings.ts +++ b/src/api/notifications/settings.ts @@ -1,6 +1,6 @@ import { API } from '~/constants/api'; import { NotificationSettings } from '~/types/notifications'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; import { notificationSettingsFromRequest, notificationSettingsToRequest, @@ -15,13 +15,11 @@ import { export const apiGetNotificationSettings = (): Promise => api .get(API.NOTIFICATIONS.SETTINGS) - .then(cleanResult) + .then(unwrap) .then(notificationSettingsFromRequest); export const apiGetNotifications = () => - api - .get(API.NOTIFICATIONS.LIST) - .then(cleanResult); + api.get(API.NOTIFICATIONS.LIST).then(unwrap); export const apiUpdateNotificationSettings = ( settings: Partial, @@ -31,5 +29,5 @@ export const apiUpdateNotificationSettings = ( API.NOTIFICATIONS.SETTINGS, notificationSettingsToRequest(settings), ) - .then(cleanResult) + .then(unwrap) .then(notificationSettingsFromRequest); diff --git a/src/api/tags/index.ts b/src/api/tags/index.ts index fc137593..782ea94a 100644 --- a/src/api/tags/index.ts +++ b/src/api/tags/index.ts @@ -5,14 +5,25 @@ import { ApiGetTagSuggestionsRequest, ApiGetTagSuggestionsResult, } from '~/types/tags'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; -export const apiGetNodesOfTag = ({ tag, offset, limit }: ApiGetNodesOfTagRequest) => +export const apiGetNodesOfTag = ({ + tag, + offset, + limit, +}: ApiGetNodesOfTagRequest) => api - .get(API.TAG.NODES, { params: { name: tag, offset, limit } }) - .then(cleanResult); + .get(API.TAG.NODES, { + params: { name: tag, offset, limit }, + }) + .then(unwrap); -export const apiGetTagSuggestions = ({ search, exclude }: ApiGetTagSuggestionsRequest) => +export const apiGetTagSuggestions = ({ + search, + exclude, +}: ApiGetTagSuggestionsRequest) => api - .get(API.TAG.AUTOCOMPLETE, { params: { search, exclude } }) - .then(cleanResult); + .get(API.TAG.AUTOCOMPLETE, { + params: { search, exclude }, + }) + .then(unwrap); diff --git a/src/api/uploads/index.ts b/src/api/uploads/index.ts index 7a4334ec..d75d57cd 100644 --- a/src/api/uploads/index.ts +++ b/src/api/uploads/index.ts @@ -1,7 +1,7 @@ import { ApiUploadFileRequest, ApiUploadFIleResult } from '~/api/uploads/types'; import { API } from '~/constants/api'; import { UploadTarget, UploadType } from '~/constants/uploads'; -import { api, cleanResult } from '~/utils/api'; +import { api, unwrap } from '~/utils/api'; export const apiUploadFile = ({ file, @@ -16,5 +16,5 @@ export const apiUploadFile = ({ .post(API.USER.UPLOAD(target, type), data, { onUploadProgress: onProgress, }) - .then(cleanResult); + .then(unwrap); }; diff --git a/src/utils/api/index.ts b/src/utils/api/index.ts index 33cc97f4..7068dd10 100644 --- a/src/utils/api/index.ts +++ b/src/utils/api/index.ts @@ -9,7 +9,7 @@ export const api = axios.create({ }); // Pass token to axios -api.interceptors.request.use(options => { +api.interceptors.request.use((options) => { const token = getMOBXStore().auth.token; if (!token) { @@ -20,15 +20,21 @@ api.interceptors.request.use(options => { }); // Logout on 401 -api.interceptors.response.use(undefined, (error: AxiosError<{ error: string }>) => { - if (error.response?.status === HTTP_RESPONSES.UNAUTHORIZED) { - getMOBXStore().auth.logout(); - } +api.interceptors.response.use( + undefined, + (error: AxiosError<{ error: string }>) => { + if (error.response?.status === HTTP_RESPONSES.UNAUTHORIZED) { + getMOBXStore().auth.logout(); + } - error.message = error?.response?.data?.error || error?.response?.statusText || error.message; + error.message = + error?.response?.data?.error || + error?.response?.statusText || + error.message; - throw error; -}); + throw error; + }, +); export const HTTP_RESPONSES = { SUCCESS: 200, @@ -38,6 +44,7 @@ export const HTTP_RESPONSES = { UNAUTHORIZED: 401, NOT_FOUND: 404, TOO_MANY_REQUESTS: 429, -}; +} as const; -export const cleanResult = (response: AxiosResponse): T => response?.data; +export const unwrap = (response: AxiosResponse): T => + response?.data;