Skip to content

chore: add custom header for createClient #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
});
}