* feat: enable platform emails * fixup! feat: enable platform emails * fixup! fixup! feat: enable platform emails * fixup! Merge branch 'main' into chore-platform-emails * fixup! fixup! Merge branch 'main' into chore-platform-emails * fixup! fixup! fixup! Merge branch 'main' into chore-platform-emails * chore: add oauth client repo * chore: enable platform emails using oauth client * fix: call getConnectedCalendar with dbWrite since it creates destinationCalendar for new credentials * chore: update doc * fixup! chore: update doc * fixup! fixup! chore: update doc
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import axios from "axios";
|
|
|
|
// Immediately Invoked Function Expression to create simple singleton class like
|
|
|
|
const http = (function () {
|
|
const instance = axios.create({
|
|
timeout: 10000,
|
|
headers: {},
|
|
});
|
|
let refreshUrl = "";
|
|
return {
|
|
instance: instance,
|
|
get: instance.get,
|
|
post: instance.post,
|
|
put: instance.put,
|
|
patch: instance.patch,
|
|
delete: instance.delete,
|
|
responseInterceptor: instance.interceptors.response,
|
|
setRefreshUrl: (url: string) => {
|
|
refreshUrl = url;
|
|
},
|
|
getRefreshUrl: () => {
|
|
return refreshUrl;
|
|
},
|
|
setUrl: (url: string) => {
|
|
instance.defaults.baseURL = url;
|
|
},
|
|
getUrl: () => {
|
|
return instance.defaults.baseURL;
|
|
},
|
|
setAuthorizationHeader: (accessToken: string) => {
|
|
instance.defaults.headers.common["Authorization"] = `Bearer ${accessToken}`;
|
|
},
|
|
getAuthorizationHeader: () => {
|
|
return instance.defaults.headers.common?.["Authorization"]?.toString() ?? "";
|
|
},
|
|
setClientIdHeader: (clientId: string) => {
|
|
instance.defaults.headers.common["x-cal-client-id"] = clientId;
|
|
},
|
|
getClientIdHeader: () => {
|
|
return instance.defaults.headers.common?.["x-cal-client-id"]?.toString() ?? "";
|
|
},
|
|
refreshTokens: async (refreshUrl: string): Promise<string> => {
|
|
const response = await fetch(`${refreshUrl}`, {
|
|
method: "GET",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
Authorization: http.getAuthorizationHeader(),
|
|
},
|
|
});
|
|
const res = await response.json();
|
|
if (res.accessToken) {
|
|
http.setAuthorizationHeader(res.accessToken);
|
|
return res.accessToken;
|
|
}
|
|
return "";
|
|
},
|
|
};
|
|
})();
|
|
|
|
export default http;
|