Skip to content

Commit

Permalink
chore: add custom header for createClient (#16)
Browse files Browse the repository at this point in the history
Because

- add custom header for createClient

This commit

- add custom header for createClient
  • Loading branch information
iamnamananand996 authored Sep 25, 2023
1 parent dd13735 commit 3db690c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/helper/createClient.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,40 @@
import axios from "axios";
import axios, { AxiosHeaders } from "axios";
import { env } from "./config";
import { Nullable } from "../types";

export function createClient(
accessToken: Nullable<string>,
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")
: undefined,
"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<string> = `${
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,
});
}

0 comments on commit 3db690c

Please sign in to comment.