From 3db690c39a21e985e90ff5feaf4c17351c461ae4 Mon Sep 17 00:00:00 2001 From: Naman Anand Date: Mon, 25 Sep 2023 23:53:37 +0530 Subject: [PATCH] chore: add custom header for createClient (#16) Because - add custom header for createClient This commit - add custom header for createClient --- src/helper/createClient.ts | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) 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, }); }