Skip to content

Commit af4eaae

Browse files
committed
refactor(cli): read every env credential name from the resolver's map
1 parent 55b48e5 commit af4eaae

6 files changed

Lines changed: 26 additions & 21 deletions

File tree

‎packages/cli/src/auth/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export {
2525

2626
export { configDir, credentialPath } from "./paths.js";
2727

28-
export { tryResolveCredential } from "./resolver.js";
28+
export { ENV_CREDENTIAL_VAR, tryResolveCredential } from "./resolver.js";
2929
export type { ResolvedCredential } from "./resolver.js";
3030

3131
export { AuthClient } from "./client.js";

‎packages/cli/src/auth/resolver.ts‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
import { isHeaderSafe, readStore } from "./store.js";
2020
import { ErrInvalidStore, ErrLoginExpired, ErrNotConfigured, isAuthError } from "./errors.js";
2121

22-
type CredentialSource = "env" | "env_alias" | "env_oauth" | "file_json" | "file_legacy";
22+
type EnvSource = "env" | "env_alias" | "env_oauth";
23+
type CredentialSource = EnvSource | "file_json" | "file_legacy";
24+
25+
export const ENV_CREDENTIAL_VAR: Record<EnvSource, string> = {
26+
env: "HEYGEN_API_KEY",
27+
env_alias: "HYPERFRAMES_API_KEY",
28+
env_oauth: "HEYGEN_ACCESS_TOKEN",
29+
};
30+
31+
export const envCredentialVar = (source: CredentialSource): string | undefined =>
32+
source in ENV_CREDENTIAL_VAR ? ENV_CREDENTIAL_VAR[source as EnvSource] : undefined;
2333

2434
interface ApiKeyCredential {
2535
type: "api_key";
@@ -40,12 +50,6 @@ interface OAuthCredential {
4050

4151
export type ResolvedCredential = ApiKeyCredential | OAuthCredential;
4252

43-
export const ENV_CREDENTIAL_VAR: Partial<Record<CredentialSource, string>> = {
44-
env: "HEYGEN_API_KEY",
45-
env_alias: "HYPERFRAMES_API_KEY",
46-
env_oauth: "HEYGEN_ACCESS_TOKEN",
47-
};
48-
4953
const EXPIRY_SKEW_MS = 60 * 1000;
5054

5155
export interface ResolveOptions {
@@ -55,17 +59,17 @@ export interface ResolveOptions {
5559
export async function resolveCredential(opts: ResolveOptions = {}): Promise<ResolvedCredential> {
5660
const now = (opts.now ?? (() => new Date()))();
5761

58-
const heygenEnv = headerSafeEnv("HEYGEN_API_KEY");
62+
const heygenEnv = headerSafeEnv(ENV_CREDENTIAL_VAR.env);
5963
if (heygenEnv) {
6064
return { type: "api_key", key: heygenEnv, source: "env" };
6165
}
6266

63-
const hfEnv = headerSafeEnv("HYPERFRAMES_API_KEY");
67+
const hfEnv = headerSafeEnv(ENV_CREDENTIAL_VAR.env_alias);
6468
if (hfEnv) {
6569
return { type: "api_key", key: hfEnv, source: "env_alias" };
6670
}
6771

68-
const accessToken = headerSafeEnv("HEYGEN_ACCESS_TOKEN");
72+
const accessToken = headerSafeEnv(ENV_CREDENTIAL_VAR.env_oauth);
6973
if (accessToken) {
7074
return { type: "oauth", access_token: accessToken, source: "env_oauth", refreshable: false };
7175
}

‎packages/cli/src/commands/auth/logout.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ import { failCommand } from "../../utils/commandResult.js";
44
* `--keep-api-key`, only the OAuth block is cleared (no-op for
55
* API-key-only stores).
66
*
7-
* Env-only credentials (`HEYGEN_API_KEY`, `HYPERFRAMES_API_KEY`) can't
8-
* be cleared by this command — we tell the user to unset them.
7+
* Env-only credentials (`HEYGEN_API_KEY`, `HYPERFRAMES_API_KEY`,
8+
* `HEYGEN_ACCESS_TOKEN`) can't be cleared by this command — we tell the user to unset them.
99
*/
1010

1111
import { defineCommand } from "citty";
1212
import {
13+
ENV_CREDENTIAL_VAR,
1314
clearOAuth,
1415
configDir,
1516
credentialPath,
1617
deleteStore,
1718
readStore,
1819
revokeTokens,
1920
} from "../../auth/index.js";
20-
import { ENV_CREDENTIAL_VAR } from "../../auth/resolver.js";
2121
import { c } from "../../ui/colors.js";
2222

2323
export default defineCommand({

‎packages/cli/src/commands/auth/status.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { failCommand, setCommandExitCode } from "../../utils/commandResult.js";
1717

1818
import { defineCommand } from "citty";
1919
import {
20+
ENV_CREDENTIAL_VAR,
2021
AuthClient,
2122
isAuthError,
2223
loadUserInfo,
@@ -258,9 +259,9 @@ function identityRows(user: UserInfo): [string, string][] {
258259
}
259260

260261
const SOURCE_LABELS: Record<ResolvedCredential["source"], string> = {
261-
env: "env (HEYGEN_API_KEY)",
262-
env_alias: "env (HYPERFRAMES_API_KEY)",
263-
env_oauth: "env (HEYGEN_ACCESS_TOKEN)",
262+
env: `env (${ENV_CREDENTIAL_VAR.env})`,
263+
env_alias: `env (${ENV_CREDENTIAL_VAR.env_alias})`,
264+
env_oauth: `env (${ENV_CREDENTIAL_VAR.env_oauth})`,
264265
file_legacy: "file (~/.heygen/credentials — legacy plaintext)",
265266
file_json: "file (~/.heygen/credentials)",
266267
};

‎packages/cli/src/commands/publish.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
lintProject,
1313
} from "../utils/lintProject.js";
1414
import { formatLintStartupMessage } from "../utils/lintFormat.js";
15-
import { ENV_CREDENTIAL_VAR } from "../auth/resolver.js";
15+
import { envCredentialVar } from "../auth/resolver.js";
1616
import {
1717
buildPublishFileMap,
1818
publishProjectArchive,
@@ -183,7 +183,7 @@ export default defineCommand({
183183
if (updateTarget || spaceOverride) {
184184
if (credential?.type !== "oauth") {
185185
const flag = updateTarget ? "--update" : "--space";
186-
const envKey = credential && ENV_CREDENTIAL_VAR[credential.source];
186+
const envKey = credential && envCredentialVar(credential.source);
187187
console.log();
188188
console.log(
189189
` ${c.error(

‎packages/cli/src/utils/publishProject.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { CSS_URL_RE, isNonRelativeUrl, isPathInside } from "@hyperframes/core";
77
import { buildAuthHeaders } from "../auth/client.js";
88
import { tryResolveCredential } from "../auth/index.js";
99
import { isAuthError } from "../auth/errors.js";
10-
import { ENV_CREDENTIAL_VAR, isTokenExpired, type ResolvedCredential } from "../auth/resolver.js";
10+
import { envCredentialVar, isTokenExpired, type ResolvedCredential } from "../auth/resolver.js";
1111
import { refreshIfNeeded } from "../cloud/auth.js";
1212
import { writeProjectLink } from "./projectLink.js";
1313

@@ -168,7 +168,7 @@ async function metadataRequestError(response: Response, fallback: string): Promi
168168
const LOGIN_EXPIRED = "Your login expired. Run hyperframes auth login, then publish again.";
169169
const LOGIN_CHANGED = "Your login changed during publish. Run publish again.";
170170
function rejectedCredentialMessage(credential: ResolvedCredential): string {
171-
const envVar = ENV_CREDENTIAL_VAR[credential.source];
171+
const envVar = envCredentialVar(credential.source);
172172
return envVar ? `${envVar} was rejected. Fix or unset it, then publish again.` : LOGIN_EXPIRED;
173173
}
174174

0 commit comments

Comments
 (0)