2024-04-15 11:54:12 -07:00
|
|
|
import { apiBaseUrl, artifactApiBaseUrl, envCredential } from "@/util/env";
|
2024-04-01 21:34:52 +03:00
|
|
|
import axios from "axios";
|
|
|
|
|
|
|
|
|
|
const client = axios.create({
|
|
|
|
|
baseURL: apiBaseUrl,
|
|
|
|
|
headers: {
|
|
|
|
|
"Content-Type": "application/json",
|
2024-04-15 11:54:12 -07:00
|
|
|
"x-api-key": envCredential,
|
2024-04-01 21:34:52 +03:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-07 21:52:59 +03:00
|
|
|
const artifactApiClient = axios.create({
|
|
|
|
|
baseURL: artifactApiBaseUrl,
|
|
|
|
|
});
|
|
|
|
|
|
2024-04-15 11:54:12 -07:00
|
|
|
export function setAuthorizationHeader(token: string) {
|
|
|
|
|
client.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeAuthorizationHeader() {
|
2024-05-07 11:31:05 -07:00
|
|
|
if (client.defaults.headers.common["Authorization"]) {
|
|
|
|
|
delete client.defaults.headers.common["Authorization"];
|
|
|
|
|
}
|
2024-04-15 11:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function setApiKeyHeader(apiKey: string) {
|
|
|
|
|
client.defaults.headers.common["X-API-Key"] = apiKey;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function removeApiKeyHeader() {
|
2024-05-07 11:31:05 -07:00
|
|
|
if (client.defaults.headers.common["X-API-Key"]) {
|
|
|
|
|
delete client.defaults.headers.common["X-API-Key"];
|
|
|
|
|
}
|
2024-04-15 11:54:12 -07:00
|
|
|
}
|
|
|
|
|
|
2024-05-07 11:31:05 -07:00
|
|
|
async function getClient(credentialGetter: CredentialGetter | null) {
|
|
|
|
|
if (credentialGetter) {
|
|
|
|
|
removeApiKeyHeader();
|
|
|
|
|
const credential = await credentialGetter();
|
|
|
|
|
if (!credential) {
|
|
|
|
|
console.warn("No credential found");
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
setAuthorizationHeader(credential);
|
|
|
|
|
}
|
|
|
|
|
return client;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export type CredentialGetter = () => Promise<string | null>;
|
|
|
|
|
|
|
|
|
|
export { getClient, artifactApiClient };
|