Skip to content

Commit dbe02d8

Browse files
committed
feat(auth): add support for DEEPGRAM_ACCESS_TOKEN as per the other SDKs
1 parent bf1cf96 commit dbe02d8

3 files changed

Lines changed: 37 additions & 7 deletions

File tree

src/packages/AbstractClient.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ export abstract class AbstractClient extends EventEmitter {
3636
*
3737
* @param options - The options to configure the DeepgramClient instance.
3838
* @param options.key - The Deepgram API key to use for authentication. If not provided, the `DEEPGRAM_API_KEY` environment variable will be used.
39+
* @param options.accessToken - The Deepgram access token to use for authentication. If not provided, the `DEEPGRAM_ACCESS_TOKEN` environment variable will be used.
3940
* @param options.global - Global options that apply to all requests made by the DeepgramClient instance.
4041
* @param options.global.fetch - Options to configure the fetch requests made by the DeepgramClient instance.
4142
* @param options.global.fetch.options - Additional options to pass to the fetch function, such as `url` and `headers`.
@@ -60,14 +61,20 @@ export abstract class AbstractClient extends EventEmitter {
6061
this.key = options.key;
6162
}
6263

63-
// if we still have neither, try to use the DEEPGRAM_API_KEY environment variable
64+
// implement priority-based credential resolution for environment variables
6465
if (!this.key && !this.accessToken) {
65-
this.key = process.env.DEEPGRAM_API_KEY as string;
66+
// check for DEEPGRAM_ACCESS_TOKEN first (higher priority)
67+
this.accessToken = process.env.DEEPGRAM_ACCESS_TOKEN as string;
68+
69+
// if still no access token, fall back to DEEPGRAM_API_KEY (lower priority)
70+
if (!this.accessToken) {
71+
this.key = process.env.DEEPGRAM_API_KEY as string;
72+
}
6673
}
6774

6875
// if we STILL have neither, throw an error
6976
if (!this.key && !this.accessToken) {
70-
throw new DeepgramError("A deepgram API key or temporary auth token is required.");
77+
throw new DeepgramError("A deepgram API key or access token is required.");
7178
}
7279

7380
options = convertLegacyOptions(options);

test/client.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ describe("testing creation of a deepgram client object", () => {
2727
});
2828

2929
it("it should throw an error if no valid apiKey is provided", () => {
30-
expect(() => createClient("")).to.throw("A deepgram API key is required");
30+
expect(() => createClient("")).to.throw("A deepgram API key or access token is required");
3131
});
3232

3333
it("it should throw an error if invalid options are provided", () => {
3434
// const client = createClient({ global: { fetch: { options: { url: "" } } } });
3535
// console.log(client);
3636
// process.exit(1);
3737
expect(() => createClient({ global: { fetch: { options: { url: "" } } } })).to.throw(
38-
`A deepgram API key is required.`
38+
`A deepgram API key or access token is required.`
3939
);
4040
});
4141

test/fetch.test.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,30 @@ describe("fetchWithAuth", () => {
99
expect(headers?.get("Authorization")).to.equal(`Token ${apiKey}`);
1010
return new Response();
1111
};
12-
const fetchWithAuthInstance = fetchWithAuth(apiKey, customFetch);
12+
const fetchWithAuthInstance = fetchWithAuth({ apiKey, customFetch });
13+
await fetchWithAuthInstance("https://example.com");
14+
});
15+
16+
it("should include the Authorization header with Bearer for access token", async () => {
17+
const accessToken = "test-access-token";
18+
const customFetch = async (input: RequestInfo | URL, init?: RequestInit | undefined) => {
19+
const headers = new Headers(init?.headers);
20+
expect(headers?.get("Authorization")).to.equal(`Bearer ${accessToken}`);
21+
return new Response();
22+
};
23+
const fetchWithAuthInstance = fetchWithAuth({ accessToken, customFetch });
24+
await fetchWithAuthInstance("https://example.com");
25+
});
26+
27+
it("should prioritize access token over API key when both are provided", async () => {
28+
const apiKey = "test-api-key";
29+
const accessToken = "test-access-token";
30+
const customFetch = async (input: RequestInfo | URL, init?: RequestInit | undefined) => {
31+
const headers = new Headers(init?.headers);
32+
expect(headers?.get("Authorization")).to.equal(`Bearer ${accessToken}`);
33+
return new Response();
34+
};
35+
const fetchWithAuthInstance = fetchWithAuth({ apiKey, accessToken, customFetch });
1336
await fetchWithAuthInstance("https://example.com");
1437
});
1538

@@ -20,7 +43,7 @@ describe("fetchWithAuth", () => {
2043
expect(headers?.get("Authorization")).to.equal("existing-token");
2144
return new Response();
2245
};
23-
const fetchWithAuthInstance = fetchWithAuth(apiKey, customFetch);
46+
const fetchWithAuthInstance = fetchWithAuth({ apiKey, customFetch });
2447
await fetchWithAuthInstance("https://example.com", {
2548
headers: { Authorization: "existing-token" },
2649
});

0 commit comments

Comments
 (0)