-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add custom header for createClient (#16)
Because - add custom header for createClient This commit - add custom header for createClient
- Loading branch information
1 parent
dd13735
commit 3db690c
Showing
1 changed file
with
17 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |