From 491fd59a5e0029801c8b455d158687952930c640 Mon Sep 17 00:00:00 2001 From: aelassas Date: Sat, 24 Feb 2024 11:57:46 +0100 Subject: [PATCH] Add axiosInstance.ts --- backend/src/services/AgencyService.ts | 27 +++-- backend/src/services/BookingService.ts | 27 +++-- backend/src/services/LocationService.ts | 31 +++-- backend/src/services/NotificationService.ts | 22 ++-- backend/src/services/PropertyService.ts | 43 ++++--- backend/src/services/UserService.ts | 109 +++++++++--------- backend/src/services/axiosInstance.ts | 6 + frontend/src/services/AgencyService.ts | 11 +- frontend/src/services/BookingService.ts | 19 ++-- frontend/src/services/LocationService.ts | 11 +- frontend/src/services/NotificationService.ts | 22 ++-- frontend/src/services/PropertyService.ts | 15 ++- frontend/src/services/UserService.ts | 86 +++++++------- frontend/src/services/axiosInstance.ts | 6 + mobile/common/AxiosHelper.ts | 8 +- mobile/config/env.config.ts | 2 +- mobile/services/AgencyService.ts | 9 +- mobile/services/BookingService.ts | 25 ++-- mobile/services/LocationService.ts | 13 +-- mobile/services/NotificationService.ts | 24 ++-- mobile/services/PropertyService.ts | 13 +-- mobile/services/UserService.ts | 114 ++++++++++--------- mobile/services/axiosInstance.ts | 9 ++ 23 files changed, 333 insertions(+), 319 deletions(-) create mode 100644 backend/src/services/axiosInstance.ts create mode 100644 frontend/src/services/axiosInstance.ts create mode 100644 mobile/services/axiosInstance.ts diff --git a/backend/src/services/AgencyService.ts b/backend/src/services/AgencyService.ts index 4736c8f1..91fcae3b 100644 --- a/backend/src/services/AgencyService.ts +++ b/backend/src/services/AgencyService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' /** * Validate an Agency name. @@ -9,9 +8,9 @@ import Env from '../config/env.config' * @returns {Promise} */ export const validate = (data: movininTypes.ValidateAgencyPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/validate-agency`, + '/api/validate-agency', data, { withCredentials: true } ) @@ -24,9 +23,9 @@ export const validate = (data: movininTypes.ValidateAgencyPayload): Promise} */ export const update = (data: movininTypes.UpdateAgencyPayload): Promise => - axios + axiosInstance .put( - `${Env.API_HOST}/api/update-agency`, + '/api/update-agency', data, { withCredentials: true } ) @@ -39,9 +38,9 @@ export const update = (data: movininTypes.UpdateAgencyPayload): Promise * @returns {Promise} */ export const deleteAgency = (id: string): Promise => - axios + axiosInstance .delete( - `${Env.API_HOST}/api/delete-agency/${encodeURIComponent(id)}`, + `/api/delete-agency/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.status) @@ -53,9 +52,9 @@ export const deleteAgency = (id: string): Promise => * @returns {Promise} */ export const getAgency = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/agency/${encodeURIComponent(id)}`, + `/api/agency/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.data) @@ -70,9 +69,9 @@ export const getAgency = (id: string): Promise => */ export const getAgencies = (keyword: string, page: number, size: number) : Promise> => - axios + axiosInstance .get( - `${Env.API_HOST}/api/agencies/${page}/${size}/?s=${encodeURIComponent(keyword)}`, + `/api/agencies/${page}/${size}/?s=${encodeURIComponent(keyword)}`, { withCredentials: true } ) .then((res) => res.data) @@ -83,9 +82,9 @@ export const getAgencies = (keyword: string, page: number, size: number) * @returns {Promise} */ export const getAllAgencies = (): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/all-agencies`, + '/api/all-agencies', { withCredentials: true } ) .then((res) => res.data) diff --git a/backend/src/services/BookingService.ts b/backend/src/services/BookingService.ts index 3a5929ed..ddbdc537 100644 --- a/backend/src/services/BookingService.ts +++ b/backend/src/services/BookingService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' /** @@ -10,9 +9,9 @@ import * as UserService from './UserService' * @returns {Promise} */ export const create = (data: movininTypes.Booking): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/create-booking`, + '/api/create-booking', data, { withCredentials: true } ) @@ -25,9 +24,9 @@ export const create = (data: movininTypes.Booking): Promise} */ export const update = (data: movininTypes.Booking): Promise => - axios + axiosInstance .put( - `${Env.API_HOST}/api/update-booking`, + '/api/update-booking', data, { withCredentials: true } ) @@ -40,9 +39,9 @@ export const update = (data: movininTypes.Booking): Promise => * @returns {Promise} */ export const updateStatus = (data: movininTypes.UpdateStatusPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/update-booking-status`, + '/api/update-booking-status', data, { withCredentials: true } ) @@ -55,9 +54,9 @@ export const updateStatus = (data: movininTypes.UpdateStatusPayload): Promise} */ export const deleteBookings = (ids: string[]): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-bookings`, + '/api/delete-bookings', ids, { withCredentials: true } ) @@ -70,9 +69,9 @@ export const deleteBookings = (ids: string[]): Promise => * @returns {Promise} */ export const getBooking = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/booking/${encodeURIComponent(id)}/${UserService.getLanguage()}`, + `/api/booking/${encodeURIComponent(id)}/${UserService.getLanguage()}`, { withCredentials: true } ) .then((res) => res.data) @@ -86,9 +85,9 @@ export const getBooking = (id: string): Promise => * @returns {Promise>} */ export const getBookings = (payload: movininTypes.GetBookingsPayload, page: number, size: number): Promise> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/bookings/${page}/${size}/${UserService.getLanguage()}`, + `/api/bookings/${page}/${size}/${UserService.getLanguage()}`, payload, { withCredentials: true } ) diff --git a/backend/src/services/LocationService.ts b/backend/src/services/LocationService.ts index 709b1111..11349bb4 100644 --- a/backend/src/services/LocationService.ts +++ b/backend/src/services/LocationService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' /** @@ -10,9 +9,9 @@ import * as UserService from './UserService' * @returns {Promise} */ export const validate = (data: movininTypes.ValidateLocationPayload): Promise => - axios + axiosInstance .post( -`${Env.API_HOST}/api/validate-location`, +'/api/validate-location', data, { withCredentials: true } ) @@ -25,9 +24,9 @@ export const validate = (data: movininTypes.ValidateLocationPayload): Promise} */ export const create = (data: movininTypes.LocationName[]): Promise => - axios + axiosInstance .post( -`${Env.API_HOST}/api/create-location`, +'/api/create-location', data, { withCredentials: true } ) @@ -41,9 +40,9 @@ export const create = (data: movininTypes.LocationName[]): Promise => * @returns {Promise} */ export const update = (id: string, data: movininTypes.LocationName[]): Promise => - axios + axiosInstance .put( -`${Env.API_HOST}/api/update-location/${id}`, +`/api/update-location/${id}`, data, { withCredentials: true } ) @@ -56,9 +55,9 @@ export const update = (id: string, data: movininTypes.LocationName[]): Promise} */ export const deleteLocation = (id: string): Promise => - axios + axiosInstance .delete( -`${Env.API_HOST}/api/delete-location/${encodeURIComponent(id)}`, +`/api/delete-location/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.status) @@ -70,9 +69,9 @@ export const deleteLocation = (id: string): Promise => * @returns {Promise} */ export const getLocation = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/location/${encodeURIComponent(id)}/${UserService.getLanguage()}`, + `/api/location/${encodeURIComponent(id)}/${UserService.getLanguage()}`, { withCredentials: true } ) .then((res) => res.data) @@ -86,9 +85,9 @@ export const getLocation = (id: string): Promise => * @returns {Promise>} */ export const getLocations = (keyword: string, page: number, size: number): Promise> => - axios + axiosInstance .get( - `${Env.API_HOST}/api/locations/${page}/${size}/${UserService.getLanguage()}/?s=${encodeURIComponent(keyword)}`, + `/api/locations/${page}/${size}/${UserService.getLanguage()}/?s=${encodeURIComponent(keyword)}`, { withCredentials: true } ) .then((res) => res.data) @@ -100,9 +99,9 @@ export const getLocations = (keyword: string, page: number, size: number): Promi * @returns {Promise} */ export const check = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/check-location/${encodeURIComponent(id)}`, + `/api/check-location/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.status) diff --git a/backend/src/services/NotificationService.ts b/backend/src/services/NotificationService.ts index 702335c0..f86b3ef6 100644 --- a/backend/src/services/NotificationService.ts +++ b/backend/src/services/NotificationService.ts @@ -1,5 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' +import axiosInstance from './axiosInstance' import Env from '../config/env.config' /** @@ -9,9 +9,9 @@ import Env from '../config/env.config' * @returns {Promise} */ export const getNotificationCounter = (userId: string): Promise => ( - axios + axiosInstance .get( - `${Env.API_HOST}/api/notification-counter/${encodeURIComponent(userId)}`, + `/api/notification-counter/${encodeURIComponent(userId)}`, { withCredentials: true } ) .then((res) => res.data) @@ -25,9 +25,9 @@ export const getNotificationCounter = (userId: string): Promise} */ export const markAsRead = (userId: string, ids: string[]): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/mark-notifications-as-read/${encodeURIComponent(userId)}`, + `/api/mark-notifications-as-read/${encodeURIComponent(userId)}`, { ids }, { withCredentials: true } ) @@ -42,9 +42,9 @@ export const markAsRead = (userId: string, ids: string[]): Promise => ( * @returns {Promise} */ export const markAsUnread = (userId: string, ids: string[]): Promise => ( - axios + axiosInstance .post( -`${Env.API_HOST}/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`, +`/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`, { ids }, { withCredentials: true } ) @@ -59,9 +59,9 @@ export const markAsUnread = (userId: string, ids: string[]): Promise => * @returns {Promise} */ export const deleteNotifications = (userId: string, ids: string[]): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-notifications/${encodeURIComponent(userId)}`, + `/api/delete-notifications/${encodeURIComponent(userId)}`, { ids }, { withCredentials: true } ) @@ -76,9 +76,9 @@ export const deleteNotifications = (userId: string, ids: string[]): Promise>} */ export const getNotifications = (userId: string, page: number): Promise> => ( - axios + axiosInstance .get( - `${Env.API_HOST}/api/notifications/${encodeURIComponent(userId)}/${page}/${Env.PAGE_SIZE}`, + `/api/notifications/${encodeURIComponent(userId)}/${page}/${Env.PAGE_SIZE}`, { withCredentials: true } ) .then((res) => res.data) diff --git a/backend/src/services/PropertyService.ts b/backend/src/services/PropertyService.ts index 86a501ec..e794e4ea 100644 --- a/backend/src/services/PropertyService.ts +++ b/backend/src/services/PropertyService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' /** @@ -10,9 +9,9 @@ import * as UserService from './UserService' * @returns {Promise} */ export const create = (data: movininTypes.CreatePropertyPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/create-property`, + '/api/create-property', data, { withCredentials: true } ) @@ -25,9 +24,9 @@ export const create = (data: movininTypes.CreatePropertyPayload): Promise} */ export const update = (data: movininTypes.UpdatePropertyPayload): Promise => - axios + axiosInstance .put( - `${Env.API_HOST}/api/update-property`, + '/api/update-property', data, { withCredentials: true } ) @@ -40,9 +39,9 @@ export const update = (data: movininTypes.UpdatePropertyPayload): Promise} */ export const check = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/check-property/${encodeURIComponent(id)}`, + `/api/check-property/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.status) @@ -54,9 +53,9 @@ export const check = (id: string): Promise => * @returns {Promise} */ export const deleteProperty = (id: string): Promise => - axios + axiosInstance .delete( - `${Env.API_HOST}/api/delete-property/${encodeURIComponent(id)}`, + `/api/delete-property/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.status) @@ -71,9 +70,9 @@ export const uploadImage = (file: Blob): Promise => { const formData = new FormData() formData.append('image', file) - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/upload-property-image`, + '/api/upload-property-image', formData, { withCredentials: true, @@ -90,9 +89,9 @@ export const uploadImage = (file: Blob): Promise => { * @returns {Promise} */ export const deleteImage = (id: string): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-property-image/${encodeURIComponent(id)}`, + `/api/delete-property-image/${encodeURIComponent(id)}`, null, { withCredentials: true } ) @@ -105,9 +104,9 @@ export const deleteImage = (id: string): Promise => * @returns {Promise} */ export const deleteTempImage = (image: string): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-temp-property-image/${encodeURIComponent(image)}`, + `/api/delete-temp-property-image/${encodeURIComponent(image)}`, null, { withCredentials: true } ) @@ -120,9 +119,9 @@ export const deleteTempImage = (image: string): Promise => * @returns {Promise} */ export const getProperty = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/property/${encodeURIComponent(id)}/${UserService.getLanguage()}`, + `/api/property/${encodeURIComponent(id)}/${UserService.getLanguage()}`, { withCredentials: true } ) .then((res) => res.data) @@ -137,9 +136,9 @@ export const getProperty = (id: string): Promise => * @returns {Promise>} */ export const getProperties = (keyword: string, data: movininTypes.GetPropertiesPayload, page: number, size: number): Promise> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/properties/${page}/${size}/?s=${encodeURIComponent(keyword)}`, + `/api/properties/${page}/${size}/?s=${encodeURIComponent(keyword)}`, data, { withCredentials: true } ) @@ -155,9 +154,9 @@ export const getProperties = (keyword: string, data: movininTypes.GetPropertiesP * @returns {Promise} */ export const getBookingProperties = (keyword: string, data: movininTypes.GetBookingPropertiesPayload, page: number, size: number): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/booking-properties/${page}/${size}/?s=${encodeURIComponent(keyword)}`, + `/api/booking-properties/${page}/${size}/?s=${encodeURIComponent(keyword)}`, data, { withCredentials: true } ) diff --git a/backend/src/services/UserService.ts b/backend/src/services/UserService.ts index cfd3239a..a8ff2b4c 100644 --- a/backend/src/services/UserService.ts +++ b/backend/src/services/UserService.ts @@ -1,5 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' +import axiosInstance from './axiosInstance' import Env from '../config/env.config' /** @@ -9,9 +9,9 @@ import Env from '../config/env.config' * @returns {Promise} */ export const create = (data: movininTypes.CreateUserPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/create-user`, + '/api/create-user', data, { withCredentials: true } ) @@ -24,9 +24,9 @@ export const create = (data: movininTypes.CreateUserPayload): Promise => * @returns {Promise} */ export const signup = (data: movininTypes.SignUpPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/admin-sign-up/ `, + '/api/admin-sign-up/ ', data ) .then((res) => res.status) @@ -40,9 +40,9 @@ export const signup = (data: movininTypes.SignUpPayload): Promise => * @returns {Promise} */ export const checkToken = (userId: string, email: string, token: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/check-token/${Env.APP_TYPE}/${encodeURIComponent(userId)}/${encodeURIComponent(email)}/${encodeURIComponent(token)}` + `/api/check-token/${Env.APP_TYPE}/${encodeURIComponent(userId)}/${encodeURIComponent(email)}/${encodeURIComponent(token)}` ) .then((res) => res.status) @@ -53,9 +53,9 @@ export const checkToken = (userId: string, email: string, token: string): Promis * @returns {Promise} */ export const deleteTokens = (userId: string): Promise => - axios + axiosInstance .delete( - `${Env.API_HOST}/api/delete-tokens/${encodeURIComponent(userId)}` + `/api/delete-tokens/${encodeURIComponent(userId)}` ) .then((res) => res.status) @@ -68,9 +68,9 @@ export const deleteTokens = (userId: string): Promise => * @returns {Promise} */ export const resend = (email?: string, reset = false, appType: string = movininTypes.AppType.Backend): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/resend/${appType}/${encodeURIComponent(email || '')}/${reset}` + `/api/resend/${appType}/${encodeURIComponent(email || '')}/${reset}` ) .then((res) => res.status) @@ -81,9 +81,9 @@ export const resend = (email?: string, reset = false, appType: string = movininT * @returns {Promise} */ export const activate = (data: movininTypes.ActivatePayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/activate/ `, + '/api/activate/ ', data, { withCredentials: true } ) @@ -96,9 +96,9 @@ export const activate = (data: movininTypes.ActivatePayload): Promise => * @returns {Promise} */ export const validateEmail = (data: movininTypes.ValidateEmailPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/validate-email`, + '/api/validate-email', data ) .then((exist) => exist.status) @@ -110,9 +110,9 @@ export const validateEmail = (data: movininTypes.ValidateEmailPayload): Promise< * @returns {Promise<{ status: number, data: movininTypes.User }>} */ export const signin = (data: movininTypes.SignInPayload): Promise<{ status: number, data: movininTypes.User }> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/sign-in/${Env.APP_TYPE}`, + `/api/sign-in/${Env.APP_TYPE}`, data, { withCredentials: true } ) @@ -141,11 +141,12 @@ export const signout = async (redirect = true) => { localStorage.removeItem('mi-user') deleteAllCookies() - await axios.post( - `${Env.API_HOST}/api/sign-out`, - null, - { withCredentials: true } - ) + await axiosInstance + .post( + '/api/sign-out', + null, + { withCredentials: true } + ) if (redirect) { window.location.href = '/sign-in' @@ -158,9 +159,9 @@ export const signout = async (redirect = true) => { * @returns {Promise} */ export const validateAccessToken = (): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/validate-access-token`, + '/api/validate-access-token', null, { withCredentials: true } ) @@ -174,9 +175,9 @@ export const validateAccessToken = (): Promise => * @returns {Promise} */ export const confirmEmail = (email: string, token: string): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/confirm-email/${encodeURIComponent(email)}/${encodeURIComponent(token)}` + `/api/confirm-email/${encodeURIComponent(email)}/${encodeURIComponent(token)}` ) .then((res) => res.status) ) @@ -188,9 +189,9 @@ export const confirmEmail = (email: string, token: string): Promise => ( * @returns {Promise} */ export const resendLink = (data: movininTypes.ResendLinkPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/resend-link`, + '/api/resend-link', data, { withCredentials: true } ) @@ -234,9 +235,9 @@ export const getQueryLanguage = (): string | null => { * @returns {Promise} */ export const updateLanguage = (data: movininTypes.UpdateLanguagePayload) => - axios + axiosInstance .post( - `${Env.API_HOST}/api/update-language`, + '/api/update-language', data, { withCredentials: true } ) @@ -276,9 +277,9 @@ export const getCurrentUser = (): movininTypes.User | null => { */ export const getUser = (id?: string): Promise => { if (id) { - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/user/${encodeURIComponent(id)}`, + `/api/user/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.data) @@ -297,9 +298,9 @@ export const getUser = (id?: string): Promise => { * @returns {Promise>} */ export const getRenters = (keyword: string, page: number, size: number): Promise> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/users/${page}/${size}/?s=${encodeURIComponent(keyword)}`, + `/api/users/${page}/${size}/?s=${encodeURIComponent(keyword)}`, { types: [movininTypes.RecordType.User] }, { withCredentials: true } ) @@ -320,9 +321,9 @@ export const getUsers = ( page: number, size: number ): Promise> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/users/${page}/${size}/?s=${encodeURIComponent(keyword)}`, + `/api/users/${page}/${size}/?s=${encodeURIComponent(keyword)}`, payload, { withCredentials: true } ) @@ -335,9 +336,9 @@ export const getUsers = ( * @returns {Promise} */ export const updateUser = (data: movininTypes.UpdateUserPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/update-user`, + '/api/update-user', data, { withCredentials: true } ) @@ -350,9 +351,9 @@ export const updateUser = (data: movininTypes.UpdateUserPayload): Promise} */ export const updateEmailNotifications = (data: movininTypes.UpdateEmailNotificationsPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/update-email-notifications`, + '/api/update-email-notifications', data, { withCredentials: true } ) @@ -377,9 +378,9 @@ export const createAvatar = (file: Blob): Promise => { const formData = new FormData() formData.append('image', file) - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/create-avatar`, + '/api/create-avatar', formData, { withCredentials: true, @@ -400,9 +401,9 @@ export const updateAvatar = (userId: string, file: Blob): Promise => { const formData = new FormData() formData.append('image', file) - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/update-avatar/${encodeURIComponent(userId)}`, + `/api/update-avatar/${encodeURIComponent(userId)}`, formData, { withCredentials: true, @@ -419,9 +420,9 @@ export const updateAvatar = (userId: string, file: Blob): Promise => { * @returns {Promise} */ export const deleteAvatar = (userId: string): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-avatar/${encodeURIComponent(userId)}`, + `/api/delete-avatar/${encodeURIComponent(userId)}`, null, { withCredentials: true } ) @@ -434,9 +435,9 @@ export const deleteAvatar = (userId: string): Promise => * @returns {Promise} */ export const deleteTempAvatar = (avatar: string): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-temp-avatar/${encodeURIComponent(avatar)}`, + `/api/delete-temp-avatar/${encodeURIComponent(avatar)}`, null, { withCredentials: true } ) @@ -451,9 +452,9 @@ export const deleteTempAvatar = (avatar: string): Promise => ( * @returns {Promise} */ export const checkPassword = (id: string, pass: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/check-password/${encodeURIComponent(id)}/${encodeURIComponent(pass)}`, + `/api/check-password/${encodeURIComponent(id)}/${encodeURIComponent(pass)}`, { withCredentials: true } ) .then((res) => res.status) @@ -465,9 +466,9 @@ export const checkPassword = (id: string, pass: string): Promise => * @returns {Promise} */ export const changePassword = (data: movininTypes.ChangePasswordPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/change-password/ `, + '/api/change-password/ ', data, { withCredentials: true } ) @@ -480,9 +481,9 @@ export const changePassword = (data: movininTypes.ChangePasswordPayload): Promis * @returns {Promise} */ export const deleteUsers = (ids: string[]): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-users`, + '/api/delete-users', ids, { withCredentials: true } ) diff --git a/backend/src/services/axiosInstance.ts b/backend/src/services/axiosInstance.ts new file mode 100644 index 00000000..50bb6a28 --- /dev/null +++ b/backend/src/services/axiosInstance.ts @@ -0,0 +1,6 @@ +import axios from 'axios' +import Env from '../config/env.config' + +const axiosInstance = axios.create({ baseURL: Env.API_HOST }) + +export default axiosInstance diff --git a/frontend/src/services/AgencyService.ts b/frontend/src/services/AgencyService.ts index 47838abe..09c3149d 100644 --- a/frontend/src/services/AgencyService.ts +++ b/frontend/src/services/AgencyService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' /** * Get all agencies. @@ -8,9 +7,9 @@ import Env from '../config/env.config' * @returns {Promise} */ export const getAllAgencies = (): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/all-agencies`, + '/api/all-agencies', { withCredentials: true } ) .then((res) => res.data) @@ -24,9 +23,9 @@ export const getAllAgencies = (): Promise => * @returns {Promise>} */ export const getAgencies = (keyword: string, page: number, size: number): Promise> => - axios + axiosInstance .get( - `${Env.API_HOST}/api/agencies/${page}/${size}/?s=${encodeURIComponent(keyword)}`, + `/api/agencies/${page}/${size}/?s=${encodeURIComponent(keyword)}`, { withCredentials: true } ) .then((res) => res.data) diff --git a/frontend/src/services/BookingService.ts b/frontend/src/services/BookingService.ts index 70ecbfe7..a3c00ff7 100644 --- a/frontend/src/services/BookingService.ts +++ b/frontend/src/services/BookingService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' /** @@ -10,9 +9,9 @@ import * as UserService from './UserService' * @returns {Promise} */ export const checkout = (data: movininTypes.CheckoutPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/checkout`, + '/api/checkout', data ) .then((res) => res.status) @@ -26,9 +25,9 @@ export const checkout = (data: movininTypes.CheckoutPayload): Promise => * @returns {Promise>} */ export const getBookings = (payload: movininTypes.GetBookingsPayload, page: number, size: number): Promise> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/bookings/${page}/${size}/${UserService.getLanguage()}`, + `/api/bookings/${page}/${size}/${UserService.getLanguage()}`, payload, { withCredentials: true } ) @@ -41,9 +40,9 @@ export const getBookings = (payload: movininTypes.GetBookingsPayload, page: numb * @returns {Promise} */ export const getBooking = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/booking/${encodeURIComponent(id)}/${UserService.getLanguage()}`, + `/api/booking/${encodeURIComponent(id)}/${UserService.getLanguage()}`, { withCredentials: true } ) .then((res) => res.data) @@ -55,9 +54,9 @@ export const getBooking = (id: string): Promise => * @returns {Promise} */ export const cancel = (id: string): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/cancel-booking/${encodeURIComponent(id)}`, + `/api/cancel-booking/${encodeURIComponent(id)}`, null, { withCredentials: true } ).then((res) => res.status) diff --git a/frontend/src/services/LocationService.ts b/frontend/src/services/LocationService.ts index a474c7de..a6cf8361 100644 --- a/frontend/src/services/LocationService.ts +++ b/frontend/src/services/LocationService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' /** @@ -12,9 +11,9 @@ import * as UserService from './UserService' * @returns {Promise>} */ export const getLocations = (keyword: string, page: number, size: number): Promise> => - axios + axiosInstance .get( - `${Env.API_HOST}/api/locations/${page}/${size}/${UserService.getLanguage()}/?s=${encodeURIComponent(keyword)}` + `/api/locations/${page}/${size}/${UserService.getLanguage()}/?s=${encodeURIComponent(keyword)}` ) .then((res) => res.data) @@ -25,8 +24,8 @@ export const getLocations = (keyword: string, page: number, size: number): Promi * @returns {Promise} */ export const getLocation = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/location/${encodeURIComponent(id)}/${UserService.getLanguage()}` + `/api/location/${encodeURIComponent(id)}/${UserService.getLanguage()}` ) .then((res) => res.data) diff --git a/frontend/src/services/NotificationService.ts b/frontend/src/services/NotificationService.ts index 1043a469..233c97b4 100644 --- a/frontend/src/services/NotificationService.ts +++ b/frontend/src/services/NotificationService.ts @@ -1,5 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' +import axiosInstance from './axiosInstance' import Env from '../config/env.config' /** @@ -9,9 +9,9 @@ import Env from '../config/env.config' * @returns {Promise} */ export const getNotificationCounter = (userId: string): Promise => ( - axios + axiosInstance .get( - `${Env.API_HOST}/api/notification-counter/${encodeURIComponent(userId)}`, + `/api/notification-counter/${encodeURIComponent(userId)}`, { withCredentials: true } ) .then((res) => res.data) @@ -25,9 +25,9 @@ export const getNotificationCounter = (userId: string): Promise} */ export const markAsRead = (userId: string, ids: string[]): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/mark-notifications-as-read/${encodeURIComponent(userId)}`, + `/api/mark-notifications-as-read/${encodeURIComponent(userId)}`, { ids }, { withCredentials: true } ) @@ -42,9 +42,9 @@ export const markAsRead = (userId: string, ids: string[]): Promise => ( * @returns {Promise} */ export const markAsUnread = (userId: string, ids: string[]): Promise => ( - axios + axiosInstance .post( -`${Env.API_HOST}/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`, +`/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`, { ids }, { withCredentials: true } ) @@ -59,9 +59,9 @@ export const markAsUnread = (userId: string, ids: string[]): Promise => * @returns {Promise} */ export const deleteNotifications = (userId: string, ids: string[]): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-notifications/${encodeURIComponent(userId)}`, + `/api/delete-notifications/${encodeURIComponent(userId)}`, { ids }, { withCredentials: true } ) @@ -76,9 +76,9 @@ export const deleteNotifications = (userId: string, ids: string[]): Promise>} */ export const getNotifications = (userId: string, page: number): Promise> => ( - axios + axiosInstance .get( - `${Env.API_HOST}/api/notifications/${encodeURIComponent(userId)}/${page}/${Env.PAGE_SIZE}`, + `/api/notifications/${encodeURIComponent(userId)}/${page}/${Env.PAGE_SIZE}`, { withCredentials: true } ) .then((res) => res.data) diff --git a/frontend/src/services/PropertyService.ts b/frontend/src/services/PropertyService.ts index 136870dc..81d8d393 100644 --- a/frontend/src/services/PropertyService.ts +++ b/frontend/src/services/PropertyService.ts @@ -1,6 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' -import Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' /** @@ -12,9 +11,9 @@ import * as UserService from './UserService' * @returns {Promise>} */ export const getProperties = (data: movininTypes.GetPropertiesPayload, page: number, size: number): Promise> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/frontend-properties/${page}/${size}}`, + `/api/frontend-properties/${page}/${size}}`, data ).then((res) => res.data) @@ -25,9 +24,9 @@ export const getProperties = (data: movininTypes.GetPropertiesPayload, page: num * @returns {Promise} */ export const getProperty = (id: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/property/${encodeURIComponent(id)}/${UserService.getLanguage()}` + `/api/property/${encodeURIComponent(id)}/${UserService.getLanguage()}` ) .then((res) => res.data) @@ -41,9 +40,9 @@ export const getProperty = (id: string): Promise => * @returns {Promise} */ export const getBookingProperties = (keyword: string, data: movininTypes.GetBookingPropertiesPayload, page: number, size: number): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/booking-properties/${page}/${size}/?s=${encodeURIComponent(keyword)}`, + `/api/booking-properties/${page}/${size}/?s=${encodeURIComponent(keyword)}`, data, { withCredentials: true } ) diff --git a/frontend/src/services/UserService.ts b/frontend/src/services/UserService.ts index 1759db76..7ae5dd48 100644 --- a/frontend/src/services/UserService.ts +++ b/frontend/src/services/UserService.ts @@ -1,5 +1,5 @@ -import axios from 'axios' import * as movininTypes from 'movinin-types' +import axiosInstance from './axiosInstance' import Env from '../config/env.config' /** @@ -9,9 +9,9 @@ import Env from '../config/env.config' * @returns {Promise} */ export const signup = (data: movininTypes.SignUpPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/sign-up/ `, + '/api/sign-up/ ', data ) .then((res) => res.status) @@ -25,9 +25,9 @@ export const signup = (data: movininTypes.SignUpPayload): Promise => * @returns {Promise} */ export const checkToken = (userId: string, email: string, token: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/check-token/${Env.APP_TYPE}/${encodeURIComponent(userId)}/${encodeURIComponent(email)}/${encodeURIComponent(token)}` + `/api/check-token/${Env.APP_TYPE}/${encodeURIComponent(userId)}/${encodeURIComponent(email)}/${encodeURIComponent(token)}` ) .then((res) => res.status) @@ -38,9 +38,9 @@ export const checkToken = (userId: string, email: string, token: string): Promis * @returns {Promise} */ export const deleteTokens = (userId: string): Promise => - axios + axiosInstance .delete( - `${Env.API_HOST}/api/delete-tokens/${encodeURIComponent(userId)}` + `/api/delete-tokens/${encodeURIComponent(userId)}` ) .then((res) => res.status) @@ -52,9 +52,9 @@ export const deleteTokens = (userId: string): Promise => * @returns {Promise} */ export const resend = (email?: string, reset = false): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/resend/${Env.APP_TYPE}/${encodeURIComponent(email || '')}/${reset}` + `/api/resend/${Env.APP_TYPE}/${encodeURIComponent(email || '')}/${reset}` ) .then((res) => res.status) @@ -65,9 +65,9 @@ export const resend = (email?: string, reset = false): Promise => * @returns {Promise} */ export const activate = (data: movininTypes.ActivatePayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/activate/ `, + '/api/activate/ ', data, { withCredentials: true } ) @@ -80,9 +80,9 @@ export const activate = (data: movininTypes.ActivatePayload): Promise => * @returns {Promise} */ export const validateEmail = (data: movininTypes.ValidateEmailPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/validate-email`, + '/api/validate-email', data ) .then((exist) => exist.status) @@ -94,9 +94,9 @@ export const validateEmail = (data: movininTypes.ValidateEmailPayload): Promise< * @returns {Promise<{ status: number, data: movininTypes.User }>} */ export const signin = (data: movininTypes.SignInPayload): Promise<{ status: number, data: movininTypes.User }> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/sign-in/${Env.APP_TYPE}`, + `/api/sign-in/${Env.APP_TYPE}`, data, { withCredentials: true } ) @@ -126,11 +126,13 @@ export const signout = async (redirect = true, redirectSignin = false) => { localStorage.removeItem('mi-user') deleteAllCookies() - await axios.post( - `${Env.API_HOST}/api/sign-out`, - null, - { withCredentials: true } - ) + await + axiosInstance + .post( + '/api/sign-out', + null, + { withCredentials: true } + ) if (redirect) { window.location.href = '/' @@ -146,9 +148,9 @@ export const signout = async (redirect = true, redirectSignin = false) => { * @returns {Promise} */ export const validateAccessToken = (): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/validate-access-token`, + '/api/validate-access-token', null, { withCredentials: true } ) @@ -162,9 +164,9 @@ export const validateAccessToken = (): Promise => * @returns {Promise} */ export const confirmEmail = (email: string, token: string): Promise => ( - axios + axiosInstance .post( - `${Env.API_HOST}/api/confirm-email/${encodeURIComponent(email)}/${encodeURIComponent(token)}` + `/api/confirm-email/${encodeURIComponent(email)}/${encodeURIComponent(token)}` ) .then((res) => res.status) ) @@ -176,9 +178,9 @@ export const confirmEmail = (email: string, token: string): Promise => ( * @returns {Promise} */ export const resendLink = (data: movininTypes.ResendLinkPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/resend-link`, + '/api/resend-link', data, { withCredentials: true } ) @@ -222,9 +224,9 @@ export const getQueryLanguage = () => { * @returns {Promise} */ export const updateLanguage = (data: movininTypes.UpdateLanguagePayload) => - axios + axiosInstance .post( - `${Env.API_HOST}/api/update-language`, + '/api/update-language', data, { withCredentials: true } ) @@ -264,9 +266,9 @@ export const getCurrentUser = (): movininTypes.User | null => { */ export const getUser = (id?: string): Promise => { if (id) { - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/user/${encodeURIComponent(id)}`, + `/api/user/${encodeURIComponent(id)}`, { withCredentials: true } ) .then((res) => res.data) @@ -283,9 +285,9 @@ export const getUser = (id?: string): Promise => { * @returns {Promise} */ export const updateUser = (data: movininTypes.UpdateUserPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/update-user`, + '/api/update-user', data, { withCredentials: true } ) @@ -298,9 +300,9 @@ export const updateUser = (data: movininTypes.UpdateUserPayload): Promise} */ export const updateEmailNotifications = (data: movininTypes.UpdateEmailNotificationsPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/update-email-notifications`, + '/api/update-email-notifications', data, { withCredentials: true } ) @@ -326,9 +328,9 @@ export const updateAvatar = (userId: string, file: Blob): Promise => { const formData = new FormData() formData.append('image', file) - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/update-avatar/${encodeURIComponent(userId)}`, + `/api/update-avatar/${encodeURIComponent(userId)}`, formData, { withCredentials: true, @@ -345,9 +347,9 @@ export const updateAvatar = (userId: string, file: Blob): Promise => { * @returns {Promise} */ export const deleteAvatar = (userId: string): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/delete-avatar/${encodeURIComponent(userId)}`, + `/api/delete-avatar/${encodeURIComponent(userId)}`, null, { withCredentials: true } ) @@ -361,9 +363,9 @@ export const deleteAvatar = (userId: string): Promise => * @returns {Promise} */ export const checkPassword = (id: string, pass: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/check-password/${encodeURIComponent(id)}/${encodeURIComponent(pass)}`, + `/api/check-password/${encodeURIComponent(id)}/${encodeURIComponent(pass)}`, { withCredentials: true } ) .then((res) => res.status) @@ -375,9 +377,9 @@ export const checkPassword = (id: string, pass: string): Promise => * @returns {Promise} */ export const changePassword = (data: movininTypes.ChangePasswordPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/change-password/ `, + '/api/change-password/ ', data, { withCredentials: true } ) diff --git a/frontend/src/services/axiosInstance.ts b/frontend/src/services/axiosInstance.ts new file mode 100644 index 00000000..50bb6a28 --- /dev/null +++ b/frontend/src/services/axiosInstance.ts @@ -0,0 +1,6 @@ +import axios from 'axios' +import Env from '../config/env.config' + +const axiosInstance = axios.create({ baseURL: Env.API_HOST }) + +export default axiosInstance diff --git a/mobile/common/AxiosHelper.ts b/mobile/common/AxiosHelper.ts index f86fd743..b4fb2fde 100644 --- a/mobile/common/AxiosHelper.ts +++ b/mobile/common/AxiosHelper.ts @@ -1,14 +1,14 @@ -import { AxiosStatic } from 'axios' +import { AxiosInstance } from 'axios' import axiosRetry from 'axios-retry' import * as Env from '../config/env.config' /** * Initialize axios-retry. * - * @param {AxiosStatic} axios + * @param {AxiosInstance} axiosInstance */ -export const init = (axios: AxiosStatic) => { - axiosRetry(axios, { +export const init = (axiosInstance: AxiosInstance) => { + axiosRetry(axiosInstance, { retries: Env.AXIOS_RETRIES, // number of retries retryDelay: (retryCount) => { console.log(`retry attempt: ${retryCount}`) diff --git a/mobile/config/env.config.ts b/mobile/config/env.config.ts index 846c3df3..d5cdbb16 100644 --- a/mobile/config/env.config.ts +++ b/mobile/config/env.config.ts @@ -51,7 +51,7 @@ export const API_HOST: string = MI_API_HOST export const AXIOS_TIMEOUT: number = 5000 /** - * Number of maximum axios retries. + * Number of maximum axiosInstance retries. * * @type {number} */ diff --git a/mobile/services/AgencyService.ts b/mobile/services/AgencyService.ts index 06b833c2..e6afba73 100644 --- a/mobile/services/AgencyService.ts +++ b/mobile/services/AgencyService.ts @@ -1,9 +1,8 @@ -import axios from 'axios' -import * as Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as AxiosHelper from '../common/AxiosHelper' import * as movininTypes from '../miscellaneous/movininTypes' -AxiosHelper.init(axios) +AxiosHelper.init(axiosInstance) /** * Get all agencies. @@ -11,8 +10,8 @@ AxiosHelper.init(axios) * @returns {Promise} */ export const getAllAgencies = (): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/all-agencies` + '/api/all-agencies' ) .then((res) => res.data) diff --git a/mobile/services/BookingService.ts b/mobile/services/BookingService.ts index 8f555374..3ec8b87e 100644 --- a/mobile/services/BookingService.ts +++ b/mobile/services/BookingService.ts @@ -1,10 +1,9 @@ -import axios from 'axios' -import * as Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' import * as AxiosHelper from '../common/AxiosHelper' import * as movininTypes from '../miscellaneous/movininTypes' -AxiosHelper.init(axios) +AxiosHelper.init(axiosInstance) /** * Complete the checkout process and create the booking. @@ -13,9 +12,9 @@ AxiosHelper.init(axios) * @returns {Promise} */ export const checkout = (data: movininTypes.CheckoutPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/checkout`, + '/api/checkout', data ) .then((res) => res.status) @@ -32,9 +31,9 @@ export const checkout = (data: movininTypes.CheckoutPayload): Promise => export const getBookings = async (payload: movininTypes.GetBookingsPayload, page: number, size: number): Promise> => { const headers = await UserService.authHeader() const language = await UserService.getLanguage() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/bookings/${page}/${size}/${language}`, + `/api/bookings/${page}/${size}/${language}`, payload, { headers } ) @@ -51,9 +50,9 @@ export const getBookings = async (payload: movininTypes.GetBookingsPayload, page export const getBooking = async (id: string): Promise => { const headers = await UserService.authHeader() const language = await UserService.getLanguage() - return axios + return axiosInstance .get( -`${Env.API_HOST}/api/booking/${encodeURIComponent(id)}/${language}`, +`/api/booking/${encodeURIComponent(id)}/${language}`, { headers } ) .then((res) => res.data) @@ -68,9 +67,9 @@ export const getBooking = async (id: string): Promise => { */ export const hasBookings = async (renter: string): Promise => { const headers = await UserService.authHeader() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/has-bookings/${encodeURIComponent(renter)}`, + `/api/has-bookings/${encodeURIComponent(renter)}`, { headers } ) .then((res) => res.status) @@ -85,9 +84,9 @@ export const hasBookings = async (renter: string): Promise => { */ export const cancel = async (id: string): Promise => { const headers = await UserService.authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/cancel-booking/${encodeURIComponent(id)}`, + `/api/cancel-booking/${encodeURIComponent(id)}`, null, { headers } ).then((res) => res.status) diff --git a/mobile/services/LocationService.ts b/mobile/services/LocationService.ts index 4c531bc1..32565dc2 100644 --- a/mobile/services/LocationService.ts +++ b/mobile/services/LocationService.ts @@ -1,10 +1,9 @@ -import axios from 'axios' -import * as Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' import * as AxiosHelper from '../common/AxiosHelper' import * as movininTypes from '../miscellaneous/movininTypes' -AxiosHelper.init(axios) +AxiosHelper.init(axiosInstance) /** * Get locations. @@ -17,9 +16,9 @@ AxiosHelper.init(axios) */ export const getLocations = async (keyword: string, page: number, size: number): Promise> => { const language = await UserService.getLanguage() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/locations/${page}/${size}/${language}/?s=${encodeURIComponent(keyword)}` + `/api/locations/${page}/${size}/${language}/?s=${encodeURIComponent(keyword)}` ) .then((res) => res.data) } @@ -33,9 +32,9 @@ export const getLocations = async (keyword: string, page: number, size: number): */ export const getLocation = async (id: string): Promise => { const language = await UserService.getLanguage() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/location/${encodeURIComponent(id)}/${language}` + `/api/location/${encodeURIComponent(id)}/${language}` ) .then((res) => res.data) } diff --git a/mobile/services/NotificationService.ts b/mobile/services/NotificationService.ts index d6fa886e..56d0b8b3 100644 --- a/mobile/services/NotificationService.ts +++ b/mobile/services/NotificationService.ts @@ -1,10 +1,10 @@ -import axios from 'axios' +import axiosInstance from './axiosInstance' import * as Env from '../config/env.config' import * as UserService from './UserService' import * as AxiosHelper from '../common/AxiosHelper' import * as movininTypes from '../miscellaneous/movininTypes' -AxiosHelper.init(axios) +AxiosHelper.init(axiosInstance) /** * Get a NotificationCounter by UserID. @@ -15,9 +15,9 @@ AxiosHelper.init(axios) */ export const getNotificationCounter = async (userId: string): Promise => { const headers = await UserService.authHeader() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/notification-counter/${encodeURIComponent(userId)}`, + `/api/notification-counter/${encodeURIComponent(userId)}`, { headers } ) .then((res) => res.data) @@ -33,9 +33,9 @@ export const getNotificationCounter = async (userId: string): Promise => { const headers = await UserService.authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/mark-notifications-as-read/${encodeURIComponent(userId)}`, + `/api/mark-notifications-as-read/${encodeURIComponent(userId)}`, { ids }, { headers } ) @@ -52,9 +52,9 @@ export const markAsRead = async (userId: string, ids: string[]): Promise */ export const markAsUnread = async (userId: string, ids: string[]): Promise => { const headers = await UserService.authHeader() - return axios + return axiosInstance .post( -`${Env.API_HOST}/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`, +`/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`, { ids }, { headers } ) @@ -71,9 +71,9 @@ export const markAsUnread = async (userId: string, ids: string[]): Promise => { const headers = await UserService.authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/delete-notifications/${encodeURIComponent(userId)}`, + `/api/delete-notifications/${encodeURIComponent(userId)}`, { ids }, { headers } ) @@ -90,9 +90,9 @@ export const deleteNotifications = async (userId: string, ids: string[]): Promis */ export const getNotifications = async (userId: string, page: number): Promise> => { const headers = await UserService.authHeader() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/notifications/${encodeURIComponent(userId)}/${page}/${Env.PAGE_SIZE}`, + `/api/notifications/${encodeURIComponent(userId)}/${page}/${Env.PAGE_SIZE}`, { headers } ) .then((res) => res.data) diff --git a/mobile/services/PropertyService.ts b/mobile/services/PropertyService.ts index ff672896..4eec75b8 100644 --- a/mobile/services/PropertyService.ts +++ b/mobile/services/PropertyService.ts @@ -1,10 +1,9 @@ -import axios from 'axios' -import * as Env from '../config/env.config' +import axiosInstance from './axiosInstance' import * as UserService from './UserService' import * as AxiosHelper from '../common/AxiosHelper' import * as movininTypes from '../miscellaneous/movininTypes' -AxiosHelper.init(axios) +AxiosHelper.init(axiosInstance) /** * Get Properties. @@ -16,9 +15,9 @@ AxiosHelper.init(axios) * @returns {Promise>} */ export const getProperties = async (data: movininTypes.GetPropertiesPayload, page: number, size: number): Promise> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/frontend-properties/${page}/${size}}`, + `/api/frontend-properties/${page}/${size}}`, data ) .then((res) => res.data) @@ -32,9 +31,9 @@ export const getProperties = async (data: movininTypes.GetPropertiesPayload, pag */ export const getProperty = async (id: string): Promise => { const language = await UserService.getLanguage() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/property/${encodeURIComponent(id)}/${language}` + `/api/property/${encodeURIComponent(id)}/${language}` ) .then((res) => res.data) } diff --git a/mobile/services/UserService.ts b/mobile/services/UserService.ts index fe550a9a..a3eb20dd 100644 --- a/mobile/services/UserService.ts +++ b/mobile/services/UserService.ts @@ -1,14 +1,14 @@ -import axios from 'axios' import { Platform } from 'react-native' import type { NativeStackNavigationProp } from '@react-navigation/native-stack' import * as Localization from 'expo-localization' +import axiosInstance from './axiosInstance' import * as Env from '../config/env.config' import * as AsyncStorage from '../common/AsyncStorage' import * as AxiosHelper from '../common/AxiosHelper' import * as ToastHelper from '../common/ToastHelper' import * as movininTypes from '../miscellaneous/movininTypes' -AxiosHelper.init(axios) +AxiosHelper.init(axiosInstance) /** * Get authentication header. @@ -32,9 +32,9 @@ export const authHeader = async () => { * @returns {Promise} */ export const signup = (data: movininTypes.FrontendSignUpPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/sign-up`, + '/api/sign-up', data ) .then((res) => res.status) @@ -48,9 +48,9 @@ export const signup = (data: movininTypes.FrontendSignUpPayload): Promise} */ export const checkToken = (userId: string, email: string, token: string): Promise => - axios + axiosInstance .get( - `${Env.API_HOST}/api/check-token/${Env.APP_TYPE}/${encodeURIComponent(userId)}/${encodeURIComponent(email)}/${encodeURIComponent(token)}` + `/api/check-token/${Env.APP_TYPE}/${encodeURIComponent(userId)}/${encodeURIComponent(email)}/${encodeURIComponent(token)}` ) .then((res) => res.status) @@ -61,8 +61,8 @@ export const checkToken = (userId: string, email: string, token: string): Promis * @returns {Promise} */ export const deleteTokens = (userId: string): Promise => - axios - .delete(`${Env.API_HOST}/api/delete-tokens/${encodeURIComponent(userId)}`) + axiosInstance + .delete(`/api/delete-tokens/${encodeURIComponent(userId)}`) .then((res) => res.status) /** @@ -73,9 +73,9 @@ export const deleteTokens = (userId: string): Promise => * @returns {Promise} */ export const resend = (email: string, reset = false): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/resend/${Env.APP_TYPE}/${encodeURIComponent(email)}/${reset}` + `/api/resend/${Env.APP_TYPE}/${encodeURIComponent(email)}/${reset}` ) .then((res) => res.status) @@ -88,9 +88,9 @@ export const resend = (email: string, reset = false): Promise => */ export const activate = async (data: movininTypes.ActivatePayload): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/activate/ `, + '/api/activate/ ', data, { headers } ) @@ -104,9 +104,9 @@ export const activate = async (data: movininTypes.ActivatePayload): Promise} */ export const validateEmail = (data: movininTypes.ValidateEmailPayload): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/validate-email`, + '/api/validate-email', data ) .then((res) => res.status) @@ -119,9 +119,9 @@ export const validateEmail = (data: movininTypes.ValidateEmailPayload): Promise< * @returns {Promise<{ status: number, data: movininTypes.User }>} */ export const signin = async (data: movininTypes.SignInPayload): Promise<{ status: number, data: movininTypes.User }> => - axios + axiosInstance .post( - `${Env.API_HOST}/api/sign-in/frontend`, + '/api/sign-in/frontend', data ) .then(async (res) => { @@ -140,9 +140,9 @@ export const signin = async (data: movininTypes.SignInPayload): Promise<{ status */ export const getPushToken = async (userId: string): Promise<{ status: number, data: string }> => { const headers = await authHeader() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/push-token/${encodeURIComponent(userId)}`, + `/api/push-token/${encodeURIComponent(userId)}`, { headers } ) .then((res) => ({ status: res.status, data: res.data })) @@ -158,9 +158,9 @@ export const getPushToken = async (userId: string): Promise<{ status: number, da */ export const createPushToken = async (userId: string, token: string): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/create-push-token/${encodeURIComponent(userId)}/${encodeURIComponent(token)}`, + `/api/create-push-token/${encodeURIComponent(userId)}/${encodeURIComponent(token)}`, null, { headers } ) @@ -176,11 +176,12 @@ export const createPushToken = async (userId: string, token: string): Promise => { const headers = await authHeader() - return axios.post( - `${Env.API_HOST}/api/delete-push-token/${encodeURIComponent(userId)}`, - null, - { headers } - ) + return axiosInstance + .post( + `/api/delete-push-token/${encodeURIComponent(userId)}`, + null, + { headers } + ) .then((res) => res.status) } @@ -216,9 +217,9 @@ export const signout = async ( */ export const validateAccessToken = async (): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/validate-access-token`, + '/api/validate-access-token', null, { headers, @@ -236,9 +237,9 @@ export const validateAccessToken = async (): Promise => { * @returns {Promise} */ export const confirmEmail = (email: string, token: string): Promise => - axios + axiosInstance .post( - `${Env.API_HOST}/api/confirm-email/${encodeURIComponent(email)}/${encodeURIComponent(token)}` + `/api/confirm-email/${encodeURIComponent(email)}/${encodeURIComponent(token)}` ) .then((res) => res.status) @@ -251,9 +252,9 @@ export const confirmEmail = (email: string, token: string): Promise => */ export const resendLink = async (data: movininTypes.ResendLinkPayload): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/resend-link`, + '/api/resend-link', data, { headers } ) @@ -291,18 +292,19 @@ export const getLanguage = async () => { */ export const updateLanguage = async (data: movininTypes.UpdateLanguagePayload) => { const headers = await authHeader() - return axios.post(`${Env.API_HOST}/api/update-language`, data, { headers }).then(async (res) => { - if (res.status === 200) { - const user = await AsyncStorage.getObject('mi-user') - if (user) { - user.language = data.language - await AsyncStorage.storeObject('mi-user', user) - } else { - ToastHelper.error() + return axiosInstance + .post('/api/update-language', data, { headers }).then(async (res) => { + if (res.status === 200) { + const user = await AsyncStorage.getObject('mi-user') + if (user) { + user.language = data.language + await AsyncStorage.storeObject('mi-user', user) + } else { + ToastHelper.error() + } } - } - return res.status - }) + return res.status + }) } /** @@ -339,8 +341,8 @@ export const getCurrentUser = async () => { */ export const getUser = async (id: string): Promise => { const headers = await authHeader() - return axios - .get(`${Env.API_HOST}/api/user/${encodeURIComponent(id)}`, { + return axiosInstance + .get(`/api/user/${encodeURIComponent(id)}`, { headers, }) .then((res) => res.data) @@ -355,9 +357,9 @@ export const getUser = async (id: string): Promise => { */ export const updateUser = async (data: movininTypes.UpdateUserPayload): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/update-user`, + '/api/update-user', data, { headers } ) @@ -373,9 +375,9 @@ export const updateUser = async (data: movininTypes.UpdateUserPayload): Promise< */ export const updateEmailNotifications = async (data: movininTypes.UpdateEmailNotificationsPayload): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/update-email-notifications`, + '/api/update-email-notifications', data, { headers } ) @@ -401,9 +403,9 @@ export const updateEmailNotifications = async (data: movininTypes.UpdateEmailNot */ export const checkPassword = async (id: string, pass: string): Promise => { const headers = await authHeader() - return axios + return axiosInstance .get( - `${Env.API_HOST}/api/check-password/${encodeURIComponent(id)}/${encodeURIComponent(pass)}`, + `/api/check-password/${encodeURIComponent(id)}/${encodeURIComponent(pass)}`, { headers } ) .then((res) => res.status) @@ -418,9 +420,9 @@ export const checkPassword = async (id: string, pass: string): Promise = */ export const changePassword = async (data: movininTypes.ChangePasswordPayload): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/change-password/ `, + '/api/change-password/ ', data, { headers } ) @@ -446,9 +448,9 @@ export const updateAvatar = async (userId: string, file: BlobInfo): Promise => { const headers = await authHeader() - return axios + return axiosInstance .post( - `${Env.API_HOST}/api/delete-avatar/${encodeURIComponent(userId)}`, + `/api/delete-avatar/${encodeURIComponent(userId)}`, null, { headers } ) diff --git a/mobile/services/axiosInstance.ts b/mobile/services/axiosInstance.ts new file mode 100644 index 00000000..8c012009 --- /dev/null +++ b/mobile/services/axiosInstance.ts @@ -0,0 +1,9 @@ +import axios from 'axios' +import * as AxiosHelper from '../common/AxiosHelper' +import * as Env from '../config/env.config' + +const axiosInstance = axios.create({ baseURL: Env.API_HOST }) + +AxiosHelper.init(axiosInstance) + +export default axiosInstance