-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
121 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,52 @@ | ||
import IAppointment, { IPatient } from "@/types"; | ||
import { withTempBaseUrl } from "@/lib/utils/withTempBaseUrl"; | ||
import IAppointment, { AppointmentStatus } from "@/types"; | ||
import axios from "axios"; | ||
import patientAxiosInstance from "../patient/authorizedRoutes"; | ||
import doctorAxiosInstance from "../doctor/authorizedRoutes"; | ||
|
||
const getAuthTokens = () => { | ||
try { | ||
return JSON.parse(localStorage.getItem("auth") || "{}"); | ||
} catch (e) { | ||
console.error("Failed to parse auth tokens", e); | ||
return {}; | ||
} | ||
}; | ||
|
||
const setAuthTokens = (tokens: Record<string, string>) => { | ||
localStorage.setItem("auth", JSON.stringify(tokens)); | ||
}; | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: `${process.env.NEXT_PUBLIC_API_URL}/appointments`, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
withCredentials: true, | ||
}); | ||
|
||
axiosInstance.interceptors.request.use( | ||
(config) => { | ||
const tokens = getAuthTokens(); | ||
if (tokens.patientToken) { | ||
config.headers.Authorization = `Bearer ${tokens.patientToken}`; | ||
} | ||
return config; | ||
}, | ||
(error) => Promise.reject(error) | ||
); | ||
|
||
axiosInstance.interceptors.response.use( | ||
(response) => response, | ||
async (error: any) => { | ||
const originalRequest = error.config; | ||
const tokens = getAuthTokens(); | ||
|
||
if (error.response?.status === 403) { | ||
setAuthTokens({ ...tokens, patientToken: "" }); | ||
return Promise.reject(error); | ||
} | ||
if (error.response?.status === 401 && !originalRequest._retry) { | ||
originalRequest._retry = true; | ||
try { | ||
const refreshResponse = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/patient/auth/refresh`, { | ||
withCredentials: true, | ||
}); | ||
|
||
const newAccessToken = refreshResponse.data.accessToken; | ||
setAuthTokens({ ...tokens, patientToken: newAccessToken }); | ||
export type verifyPaymentProps = { | ||
paymentData: { razorpay_order_id: string; razorpay_payment_id: string; razorpay_signature: string }; | ||
appointmentId: string | ||
} | ||
|
||
originalRequest.headers.Authorization = `Bearer ${newAccessToken}`; | ||
return axiosInstance(originalRequest); | ||
} catch (refreshError: any) { | ||
if (refreshError.response?.status === 401 || 403) { | ||
setAuthTokens({ ...tokens, patientToken: "" }); | ||
} | ||
return Promise.reject(refreshError); | ||
} | ||
} | ||
|
||
return Promise.reject(error); | ||
} | ||
); | ||
const { doctorInstance, patientInstance, baseUrl } = { | ||
doctorInstance: doctorAxiosInstance, | ||
patientInstance: patientAxiosInstance, | ||
baseUrl: `${process.env.NEXT_PUBLIC_API_URL}/appointments` | ||
} | ||
|
||
export const getDoctorsList = async () => { | ||
const response = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/doctors`); | ||
return response.data; | ||
}; | ||
|
||
export const createAppointment = async (appointment: IAppointment) => { | ||
const response = await axiosInstance.post('/', { appointment }); | ||
const response = await withTempBaseUrl(patientInstance, baseUrl, { | ||
method: 'POST', | ||
url: '/', | ||
data: { | ||
appointment | ||
} | ||
}); | ||
return response.data; | ||
} | ||
}; | ||
|
||
export const verifyPayment = async ({ appointmentId, paymentData }: verifyPaymentProps) => { | ||
const response = await axiosInstance.post('/verify-payment', { paymentData, appointmentId }); | ||
const response = await withTempBaseUrl(patientInstance, baseUrl, { | ||
method: 'POST', | ||
url: '/verify-payment', | ||
data: { | ||
paymentData, appointmentId | ||
} | ||
}); | ||
return response.data; | ||
} | ||
}; | ||
|
||
export type verifyPaymentProps = { | ||
paymentData: { razorpay_order_id: string; razorpay_payment_id: string; razorpay_signature: string }; | ||
appointmentId: string | ||
|
||
export const getAppointmentsDoctor = async (status: AppointmentStatus) => { | ||
const response = await withTempBaseUrl(doctorInstance, baseUrl, { | ||
method: 'GET', | ||
url: `/doctor?status=${status}`, | ||
}) | ||
return response.data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,69 @@ | ||
import { ISlot, Days } from "@/types"; | ||
import axios from "axios"; | ||
import { withTempBaseUrl } from "@/lib/utils/withTempBaseUrl"; | ||
import doctorAxiosInstance from "../doctor/authorizedRoutes"; | ||
|
||
export const axiosInstance = axios.create({ | ||
baseURL: `${process.env.NEXT_PUBLIC_API_URL}/slots`, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
withCredentials: true, | ||
}); | ||
|
||
axiosInstance.interceptors.request.use( | ||
(config) => { | ||
const token = JSON.parse(localStorage.getItem("auth") || "{}"); | ||
if (token.doctorToken) { | ||
config.headers.Authorization = `Bearer ${token.doctorToken}`; | ||
} | ||
return config; | ||
}, | ||
(error) => { | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
axiosInstance.interceptors.response.use( | ||
(response) => { | ||
return response; | ||
}, | ||
async (error: any) => { | ||
const originalRequest = error.config; | ||
|
||
if (error.response?.status === 401 && !originalRequest._retry) { | ||
originalRequest._retry = true; | ||
|
||
try { | ||
const tokens = JSON.parse(localStorage.getItem("auth") || "{}"); | ||
const refreshResponse = await axios.get(`${process.env.NEXT_PUBLIC_API_URL}/doctor/auth/refresh`, { | ||
withCredentials: true, | ||
}); | ||
|
||
const newAccessToken = refreshResponse.data.accessToken; | ||
|
||
localStorage.setItem( | ||
"auth", | ||
JSON.stringify({ | ||
...tokens, | ||
doctorToken: newAccessToken, | ||
}) | ||
); | ||
|
||
originalRequest.headers.Authorization = `Bearer ${newAccessToken}`; | ||
|
||
return axiosInstance(originalRequest); | ||
} catch (refreshError: any) { | ||
if (refreshError.response.status === 401) { | ||
const tokens = JSON.parse(localStorage.getItem("auth") || "{}"); | ||
localStorage.setItem("auth", { | ||
...tokens, | ||
doctorToken: "", | ||
}); | ||
} | ||
return Promise.reject(refreshError); | ||
} | ||
} | ||
|
||
return Promise.reject(error); | ||
} | ||
); | ||
const baseUrl = `${process.env.NEXT_PUBLIC_API_URL}/slots`; | ||
|
||
// =================================================================== // | ||
|
||
export const addSlotsDoctor = async (slots: ISlot[], day: Days) => { | ||
const response = await axiosInstance.post('/day', { slots, day }); | ||
const response = await withTempBaseUrl(doctorAxiosInstance, baseUrl, { | ||
method: 'POST', | ||
url: '/day', | ||
data: { slots, day }, | ||
}); | ||
return response.data; | ||
}; | ||
|
||
export const deleteManyByDayDoctor = async (slots: ISlot[], day: Days) => { | ||
const response = await axiosInstance.delete('/day', { | ||
data: { | ||
slots, | ||
day | ||
} | ||
const response = await withTempBaseUrl(doctorAxiosInstance, baseUrl, { | ||
method: 'DELETE', | ||
url: '/day', | ||
data: { slots, day }, | ||
}); | ||
return response.data | ||
} | ||
return response.data; | ||
}; | ||
|
||
export const addSlotsAllDayDoctor = async (startTimes: string[]) => { | ||
const response = await axiosInstance.post('/all-days', { startTimes }) | ||
const response = await withTempBaseUrl(doctorAxiosInstance, baseUrl, { | ||
method: 'POST', | ||
url: '/all-days', | ||
data: { startTimes }, | ||
}); | ||
return response.data; | ||
} | ||
}; | ||
|
||
export const deleteSlotsAllDayDoctor = async (startTimes: string[]) => { | ||
const response = await axiosInstance.delete('/all-days', { | ||
data: { | ||
startTimes | ||
} | ||
}) | ||
const response = await withTempBaseUrl(doctorAxiosInstance, baseUrl, { | ||
method: 'DELETE', | ||
url: '/all-days', | ||
data: { startTimes }, | ||
}); | ||
return response.data; | ||
} | ||
}; | ||
|
||
export const getSlotsByDayDoctor = async (day: Days) => { | ||
const response = await axiosInstance.get(`/doctor?day=${day}`); | ||
const response = await withTempBaseUrl(doctorAxiosInstance, baseUrl, { | ||
method: 'GET', | ||
url: `/doctor?day=${day}`, | ||
}); | ||
return response.data; | ||
} | ||
}; | ||
|
||
export const getAllSlotsDoctor = async () => { | ||
const response = await axiosInstance.get('/doctor'); | ||
const response = await withTempBaseUrl(doctorAxiosInstance, baseUrl, { | ||
method: 'GET', | ||
url: '/doctor', | ||
}); | ||
return response.data; | ||
} | ||
}; | ||
|
||
// =================================================================== // | ||
|
||
export const getSlotsOfDoctor = async(doctorId:string,date:string)=>{ | ||
|
||
const response = await axiosInstance.get(`/${doctorId}?date=${date}`); | ||
export const getSlotsOfDoctor = async (doctorId: string, date: string) => { | ||
const response = await withTempBaseUrl(doctorAxiosInstance, baseUrl, { | ||
method: 'GET', | ||
url: `/${doctorId}?date=${date}`, | ||
}); | ||
return response.data; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { AxiosInstance, AxiosRequestConfig, AxiosResponse } from "axios"; | ||
|
||
export const withTempBaseUrl = async ( | ||
instance: AxiosInstance, | ||
newBaseUrl: string, | ||
config: AxiosRequestConfig | ||
): Promise<AxiosResponse> => { | ||
const originalBaseUrl = instance.defaults.baseURL; | ||
|
||
try { | ||
instance.defaults.baseURL = newBaseUrl; | ||
const response = await instance(config); | ||
return response; | ||
} finally { | ||
instance.defaults.baseURL = originalBaseUrl; | ||
} | ||
}; |
Oops, something went wrong.
9f88774
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
avm-care – ./
avm-care-sinans-projects-8d312afe.vercel.app
avm-care.vercel.app
avm-care-git-main-sinans-projects-8d312afe.vercel.app