diff --git a/src/helper/createClient.ts b/src/helper/createClient.ts index ecbf8b9..15b46e1 100644 --- a/src/helper/createClient.ts +++ b/src/helper/createClient.ts @@ -1,13 +1,19 @@ -import axios from "axios"; +import axios, { AxiosHeaders } from "axios"; import { env } from "./config"; import { Nullable } from "../types"; export function createClient( accessToken: Nullable, - product: "base" | "model" | "vdp" + product: "base" | "model" | "vdp", + headers: AxiosHeaders ) { - const headers = accessToken - ? { + let clientHeaders = {}; + + if (headers) { + clientHeaders = headers; + } else { + if (accessToken) { + clientHeaders = { Authorization: `Bearer ${accessToken}`, "CF-Access-Client-Id": env("CF_ACCESS_CLIENT_ID") ? env("CF_ACCESS_CLIENT_ID") @@ -15,25 +21,20 @@ export function createClient( "CF-Access-Client-Secret": env("CF_ACCESS_CLIENT_SECRET") ? env("CF_ACCESS_CLIENT_SECRET") : undefined, - } - : {}; + }; + } + } - if ( - !process.env.API_GATEWAY_URL && - !env("API_GATEWAY_URL") - ) { - throw new Error( - "API_GATEWAY_URL or API_GATEWAY_URL is not defined" - ); + if (!process.env.API_GATEWAY_URL && !env("API_GATEWAY_URL")) { + throw new Error("API_GATEWAY_URL or API_GATEWAY_URL is not defined"); } let baseURL: Nullable = `${ - process.env.API_GATEWAY_URL ?? - env("API_GATEWAY_URL") + process.env.API_GATEWAY_URL ?? env("API_GATEWAY_URL") }/${product}/${env("API_VERSION")}`; return axios.create({ baseURL, - headers, + headers: clientHeaders, }); }