Skip to content

Commit

Permalink
Add axiosInstance.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Feb 24, 2024
1 parent f83f9b6 commit 491fd59
Show file tree
Hide file tree
Showing 23 changed files with 333 additions and 319 deletions.
27 changes: 13 additions & 14 deletions backend/src/services/AgencyService.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -9,9 +8,9 @@ import Env from '../config/env.config'
* @returns {Promise<number>}
*/
export const validate = (data: movininTypes.ValidateAgencyPayload): Promise<number> =>
axios
axiosInstance
.post(
`${Env.API_HOST}/api/validate-agency`,
'/api/validate-agency',
data,
{ withCredentials: true }
)
Expand All @@ -24,9 +23,9 @@ export const validate = (data: movininTypes.ValidateAgencyPayload): Promise<numb
* @returns {Promise<number>}
*/
export const update = (data: movininTypes.UpdateAgencyPayload): Promise<number> =>
axios
axiosInstance
.put(
`${Env.API_HOST}/api/update-agency`,
'/api/update-agency',
data,
{ withCredentials: true }
)
Expand All @@ -39,9 +38,9 @@ export const update = (data: movininTypes.UpdateAgencyPayload): Promise<number>
* @returns {Promise<number>}
*/
export const deleteAgency = (id: string): Promise<number> =>
axios
axiosInstance
.delete(
`${Env.API_HOST}/api/delete-agency/${encodeURIComponent(id)}`,
`/api/delete-agency/${encodeURIComponent(id)}`,
{ withCredentials: true }
)
.then((res) => res.status)
Expand All @@ -53,9 +52,9 @@ export const deleteAgency = (id: string): Promise<number> =>
* @returns {Promise<movininTypes.User>}
*/
export const getAgency = (id: string): Promise<movininTypes.User> =>
axios
axiosInstance
.get(
`${Env.API_HOST}/api/agency/${encodeURIComponent(id)}`,
`/api/agency/${encodeURIComponent(id)}`,
{ withCredentials: true }
)
.then((res) => res.data)
Expand All @@ -70,9 +69,9 @@ export const getAgency = (id: string): Promise<movininTypes.User> =>
*/
export const getAgencies = (keyword: string, page: number, size: number)
: Promise<movininTypes.Result<movininTypes.User>> =>
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)
Expand All @@ -83,9 +82,9 @@ export const getAgencies = (keyword: string, page: number, size: number)
* @returns {Promise<movininTypes.User[]>}
*/
export const getAllAgencies = (): Promise<movininTypes.User[]> =>
axios
axiosInstance
.get(
`${Env.API_HOST}/api/all-agencies`,
'/api/all-agencies',
{ withCredentials: true }
)
.then((res) => res.data)
27 changes: 13 additions & 14 deletions backend/src/services/BookingService.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand All @@ -10,9 +9,9 @@ import * as UserService from './UserService'
* @returns {Promise<movininTypes.Booking>}
*/
export const create = (data: movininTypes.Booking): Promise<movininTypes.Booking> =>
axios
axiosInstance
.post(
`${Env.API_HOST}/api/create-booking`,
'/api/create-booking',
data,
{ withCredentials: true }
)
Expand All @@ -25,9 +24,9 @@ export const create = (data: movininTypes.Booking): Promise<movininTypes.Booking
* @returns {Promise<number>}
*/
export const update = (data: movininTypes.Booking): Promise<number> =>
axios
axiosInstance
.put(
`${Env.API_HOST}/api/update-booking`,
'/api/update-booking',
data,
{ withCredentials: true }
)
Expand All @@ -40,9 +39,9 @@ export const update = (data: movininTypes.Booking): Promise<number> =>
* @returns {Promise<number>}
*/
export const updateStatus = (data: movininTypes.UpdateStatusPayload): Promise<number> =>
axios
axiosInstance
.post(
`${Env.API_HOST}/api/update-booking-status`,
'/api/update-booking-status',
data,
{ withCredentials: true }
)
Expand All @@ -55,9 +54,9 @@ export const updateStatus = (data: movininTypes.UpdateStatusPayload): Promise<nu
* @returns {Promise<number>}
*/
export const deleteBookings = (ids: string[]): Promise<number> =>
axios
axiosInstance
.post(
`${Env.API_HOST}/api/delete-bookings`,
'/api/delete-bookings',
ids,
{ withCredentials: true }
)
Expand All @@ -70,9 +69,9 @@ export const deleteBookings = (ids: string[]): Promise<number> =>
* @returns {Promise<movininTypes.Booking>}
*/
export const getBooking = (id: string): Promise<movininTypes.Booking> =>
axios
axiosInstance
.get(
`${Env.API_HOST}/api/booking/${encodeURIComponent(id)}/${UserService.getLanguage()}`,
`/api/booking/${encodeURIComponent(id)}/${UserService.getLanguage()}`,
{ withCredentials: true }
)
.then((res) => res.data)
Expand All @@ -86,9 +85,9 @@ export const getBooking = (id: string): Promise<movininTypes.Booking> =>
* @returns {Promise<movininTypes.Result<movininTypes.Booking>>}
*/
export const getBookings = (payload: movininTypes.GetBookingsPayload, page: number, size: number): Promise<movininTypes.Result<movininTypes.Booking>> =>
axios
axiosInstance
.post(
`${Env.API_HOST}/api/bookings/${page}/${size}/${UserService.getLanguage()}`,
`/api/bookings/${page}/${size}/${UserService.getLanguage()}`,
payload,
{ withCredentials: true }
)
Expand Down
31 changes: 15 additions & 16 deletions backend/src/services/LocationService.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand All @@ -10,9 +9,9 @@ import * as UserService from './UserService'
* @returns {Promise<number>}
*/
export const validate = (data: movininTypes.ValidateLocationPayload): Promise<number> =>
axios
axiosInstance
.post(
`${Env.API_HOST}/api/validate-location`,
'/api/validate-location',
data,
{ withCredentials: true }
)
Expand All @@ -25,9 +24,9 @@ export const validate = (data: movininTypes.ValidateLocationPayload): Promise<nu
* @returns {Promise<number>}
*/
export const create = (data: movininTypes.LocationName[]): Promise<number> =>
axios
axiosInstance
.post(
`${Env.API_HOST}/api/create-location`,
'/api/create-location',
data,
{ withCredentials: true }
)
Expand All @@ -41,9 +40,9 @@ export const create = (data: movininTypes.LocationName[]): Promise<number> =>
* @returns {Promise<number>}
*/
export const update = (id: string, data: movininTypes.LocationName[]): Promise<number> =>
axios
axiosInstance
.put(
`${Env.API_HOST}/api/update-location/${id}`,
`/api/update-location/${id}`,
data,
{ withCredentials: true }
)
Expand All @@ -56,9 +55,9 @@ export const update = (id: string, data: movininTypes.LocationName[]): Promise<n
* @returns {Promise<number>}
*/
export const deleteLocation = (id: string): Promise<number> =>
axios
axiosInstance
.delete(
`${Env.API_HOST}/api/delete-location/${encodeURIComponent(id)}`,
`/api/delete-location/${encodeURIComponent(id)}`,
{ withCredentials: true }
)
.then((res) => res.status)
Expand All @@ -70,9 +69,9 @@ export const deleteLocation = (id: string): Promise<number> =>
* @returns {Promise<movininTypes.Location>}
*/
export const getLocation = (id: string): Promise<movininTypes.Location> =>
axios
axiosInstance
.get(
`${Env.API_HOST}/api/location/${encodeURIComponent(id)}/${UserService.getLanguage()}`,
`/api/location/${encodeURIComponent(id)}/${UserService.getLanguage()}`,
{ withCredentials: true }
)
.then((res) => res.data)
Expand All @@ -86,9 +85,9 @@ export const getLocation = (id: string): Promise<movininTypes.Location> =>
* @returns {Promise<movininTypes.Result<movininTypes.Location>>}
*/
export const getLocations = (keyword: string, page: number, size: number): Promise<movininTypes.Result<movininTypes.Location>> =>
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)
Expand All @@ -100,9 +99,9 @@ export const getLocations = (keyword: string, page: number, size: number): Promi
* @returns {Promise<number>}
*/
export const check = (id: string): Promise<number> =>
axios
axiosInstance
.get(
`${Env.API_HOST}/api/check-location/${encodeURIComponent(id)}`,
`/api/check-location/${encodeURIComponent(id)}`,
{ withCredentials: true }
)
.then((res) => res.status)
22 changes: 11 additions & 11 deletions backend/src/services/NotificationService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import * as movininTypes from 'movinin-types'
import axiosInstance from './axiosInstance'
import Env from '../config/env.config'

/**
Expand All @@ -9,9 +9,9 @@ import Env from '../config/env.config'
* @returns {Promise<movininTypes.NotificationCounter>}
*/
export const getNotificationCounter = (userId: string): Promise<movininTypes.NotificationCounter> => (
axios
axiosInstance
.get(
`${Env.API_HOST}/api/notification-counter/${encodeURIComponent(userId)}`,
`/api/notification-counter/${encodeURIComponent(userId)}`,
{ withCredentials: true }
)
.then((res) => res.data)
Expand All @@ -25,9 +25,9 @@ export const getNotificationCounter = (userId: string): Promise<movininTypes.Not
* @returns {Promise<number>}
*/
export const markAsRead = (userId: string, ids: string[]): Promise<number> => (
axios
axiosInstance
.post(
`${Env.API_HOST}/api/mark-notifications-as-read/${encodeURIComponent(userId)}`,
`/api/mark-notifications-as-read/${encodeURIComponent(userId)}`,
{ ids },
{ withCredentials: true }
)
Expand All @@ -42,9 +42,9 @@ export const markAsRead = (userId: string, ids: string[]): Promise<number> => (
* @returns {Promise<number>}
*/
export const markAsUnread = (userId: string, ids: string[]): Promise<number> => (
axios
axiosInstance
.post(
`${Env.API_HOST}/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`,
`/api/mark-notifications-as-unread/${encodeURIComponent(userId)}`,
{ ids },
{ withCredentials: true }
)
Expand All @@ -59,9 +59,9 @@ export const markAsUnread = (userId: string, ids: string[]): Promise<number> =>
* @returns {Promise<number>}
*/
export const deleteNotifications = (userId: string, ids: string[]): Promise<number> => (
axios
axiosInstance
.post(
`${Env.API_HOST}/api/delete-notifications/${encodeURIComponent(userId)}`,
`/api/delete-notifications/${encodeURIComponent(userId)}`,
{ ids },
{ withCredentials: true }
)
Expand All @@ -76,9 +76,9 @@ export const deleteNotifications = (userId: string, ids: string[]): Promise<numb
* @returns {Promise<movininTypes.Result<movininTypes.Notification>>}
*/
export const getNotifications = (userId: string, page: number): Promise<movininTypes.Result<movininTypes.Notification>> => (
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)
Expand Down
Loading

0 comments on commit 491fd59

Please sign in to comment.