Files
calendar/packages/platform/atoms/lib/http.ts
T
3a22fdb428 feat: API v2 subversioning (#15135)
* feat: API v2 subversioning

* Update app.ts

* Added multiple versions to existing controllers

* Updated the slots controller

* chore: version platform library package with npm package alias

* chore: use consts to version

* chore: use consts to version

* chore: version api for cal provider

* chore: remove timezone controller subversion poc

* fixup! Merge branch 'main' into feat/api-v2-subversioning

---------

Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: Morgan Vernay <morgan@cal.com>
2024-06-04 15:05:39 +00:00

70 lines
2.0 KiB
TypeScript

import axios from "axios";
import { CAL_API_VERSION_HEADER, X_CAL_CLIENT_ID } from "@calcom/platform-constants";
// 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() ?? "";
},
setVersionHeader: (clientId: string) => {
instance.defaults.headers.common[CAL_API_VERSION_HEADER] = clientId;
},
getVersionHeader: () => {
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;