diff --git a/cli/context.ts b/cli/context.ts index 3424cab720..787e6b71fb 100644 --- a/cli/context.ts +++ b/cli/context.ts @@ -2,6 +2,7 @@ import { colors, GlobalUserInfo, log, setClient, UserService } from "./deps.ts"; import { loginInteractive, tryGetLoginInfo } from "./login.ts"; import { GlobalOptions } from "./types.ts"; +import { getHeaders } from "./utils.ts"; import { addWorkspace, getActiveWorkspace, @@ -104,8 +105,18 @@ export async function tryResolveVersion( const workspaceRes = await tryResolveWorkspace(opts); if (workspaceRes.isError) return undefined; + const requestHeaders: HeadersInit = new Headers(); + + const extraHeaders = getHeaders(); + if (extraHeaders) { + for (const [key, value] of Object.entries(extraHeaders)) { + requestHeaders.set(key, value); + } + } + const response = await fetch( - new URL(new URL(workspaceRes.value.remote).origin + "/api/version") + new URL(new URL(workspaceRes.value.remote).origin + "/api/version"), + { headers: requestHeaders, method: "GET" } ); const version = await response.text(); try { diff --git a/cli/main.ts b/cli/main.ts index d362fcc481..74e24b215b 100644 --- a/cli/main.ts +++ b/cli/main.ts @@ -22,6 +22,7 @@ import dev from "./dev.ts"; import { tryResolveVersion } from "./context.ts"; import { GlobalOptions } from "./types.ts"; import { OpenAPI } from "./deps.ts"; +import { getHeaders } from "./utils.ts"; addEventListener("error", (event) => { if (event.error) { @@ -122,15 +123,9 @@ try { }); log.debug("Debug logging enabled. CLI build against " + VERSION); - const headers = Deno.env.get("HEADERS"); - if (headers) { - const parsedHeaders = Object.fromEntries( - headers.split(",").map((h) => h.split(":").map((s) => s.trim())) - ); - log.debug( - "Headers from env keys: " + JSON.stringify(Object.keys(parsedHeaders)) - ); - OpenAPI.HEADERS = parsedHeaders; + const extraHeaders = getHeaders(); + if (extraHeaders) { + OpenAPI.HEADERS = extraHeaders; } await command.parse(Deno.args); } catch (e) { diff --git a/cli/pull.ts b/cli/pull.ts index ed40b487b4..eb94de40e3 100644 --- a/cli/pull.ts +++ b/cli/pull.ts @@ -2,6 +2,7 @@ import { GlobalOptions } from "./types.ts"; import { colors, Command, JSZip } from "./deps.ts"; import { Workspace } from "./workspace.ts"; +import { getHeaders } from "./utils.ts"; export async function downloadZip( workspace: Workspace, @@ -15,6 +16,13 @@ export async function downloadZip( requestHeaders.set("Authorization", "Bearer " + workspace.token); requestHeaders.set("Content-Type", "application/octet-stream"); + const extraHeaders = getHeaders(); + if (extraHeaders) { + for (const [key, value] of Object.entries(extraHeaders)) { + requestHeaders.set(key, value); + } + } + const zipResponse = await fetch( workspace.remote + "api/w/" + diff --git a/cli/utils.ts b/cli/utils.ts index fe6ca02215..12f74b7b9b 100644 --- a/cli/utils.ts +++ b/cli/utils.ts @@ -74,3 +74,18 @@ export function deepEqual(a: T, b: T): boolean { // true if both NaN, false otherwise return a !== a && b !== b; } + +export function getHeaders(): Record | undefined { + const headers = Deno.env.get("HEADERS"); + if (headers) { + const parsedHeaders = Object.fromEntries( + headers.split(",").map((h) => h.split(":").map((s) => s.trim())) + ); + log.debug( + "Headers from env keys: " + JSON.stringify(Object.keys(parsedHeaders)) + ); + return parsedHeaders; + } else { + return undefined; + } +}