Skip to content

Commit 0b3021c

Browse files
feat: apply luke's suggestions
Co-Authored-By: Luke Oliff <luke@lukeoliff.com>
1 parent 0370aa3 commit 0b3021c

5 files changed

Lines changed: 28 additions & 15 deletions

File tree

src/lib/fetch.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,32 @@ export const resolveFetch = (customFetch?: Fetch): Fetch => {
2222
return (...args) => _fetch(...args);
2323
};
2424

25+
interface FetchWithAuthOptions {
26+
apiKey?: string;
27+
customFetch?: Fetch;
28+
accessToken?: string;
29+
}
30+
2531
/**
2632
* Resolves a fetch function that includes an "Authorization" header with the provided API key.
2733
*
2834
* @param apiKey - The API key to include in the "Authorization" header.
2935
* @param customFetch - An optional custom fetch function to use instead of the global fetch function.
3036
* @returns A fetch function that can be used to make HTTP requests with the provided API key in the "Authorization" header.
3137
*/
32-
export const fetchWithAuth = (apiKey: string, customFetch?: Fetch, token?: string): Fetch => {
38+
export const fetchWithAuth = ({
39+
apiKey,
40+
customFetch,
41+
accessToken,
42+
}: Readonly<FetchWithAuthOptions>): Fetch => {
3343
const fetch = resolveFetch(customFetch);
3444
const HeadersConstructor = resolveHeadersConstructor();
3545

3646
return async (input, init) => {
3747
const headers = new HeadersConstructor(init?.headers);
3848

3949
if (!headers.has("Authorization")) {
40-
headers.set("Authorization", token ? `Bearer ${token}` : `Token ${apiKey}`);
50+
headers.set("Authorization", accessToken ? `Bearer ${accessToken}` : `Token ${apiKey}`);
4151
}
4252

4353
return fetch(input, { ...init, headers });

src/lib/types/DeepgramClientOptions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ interface ITransport<C, O> {
2424
}
2525

2626
export type DefaultNamespaceOptions = {
27-
key: string;
27+
key?: string;
2828
fetch: {
2929
options: { url: TransportUrl };
3030
};

src/packages/AbstractClient.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const noop = () => {};
2323
*/
2424
export abstract class AbstractClient extends EventEmitter {
2525
protected factory: Function | undefined = undefined;
26-
protected key: string;
26+
protected key: string | undefined = undefined;
2727
protected accessToken: string | undefined = undefined;
2828
protected options: DefaultClientOptions;
2929
public namespace: string = "global";
@@ -48,25 +48,21 @@ export abstract class AbstractClient extends EventEmitter {
4848
this.accessToken = options.accessToken;
4949
}
5050

51-
let key;
52-
5351
if (typeof options.key === "function") {
5452
this.factory = options.key;
55-
key = this.factory();
53+
this.key = this.factory();
5654
} else {
57-
key = options.key;
55+
this.key = options.key;
5856
}
5957

60-
if (!key) {
61-
key = process.env.DEEPGRAM_API_KEY as string;
58+
if (!this.key) {
59+
this.key = process.env.DEEPGRAM_API_KEY as string;
6260
}
6361

64-
if (!key && !this.accessToken) {
62+
if (!this.key && !this.accessToken) {
6563
throw new DeepgramError("A deepgram API key or temporary auth token is required.");
6664
}
6765

68-
this.key = key;
69-
7066
options = convertLegacyOptions(options);
7167

7268
/**

src/packages/AbstractLiveClient.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,14 @@ export abstract class AbstractLiveClient extends AbstractClient {
145145
* Native websocket transport (browser)
146146
*/
147147
if (NATIVE_WEBSOCKET_AVAILABLE) {
148+
const accessToken = this.accessToken;
149+
const apiKey = this.namespaceOptions.key;
150+
if (!accessToken && !apiKey) {
151+
throw new Error("Don't know how to set authentication headers for WebSocket connection.");
152+
}
148153
this.conn = new WebSocket(
149154
requestUrl,
150-
this.accessToken ? ["bearer", this.accessToken] : ["token", this.namespaceOptions.key]
155+
accessToken ? ["bearer", accessToken] : ["token", apiKey!]
151156
);
152157
this.setupConnection();
153158
return;

src/packages/AbstractRestClient.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export abstract class AbstractRestClient extends AbstractClient {
2929
);
3030
}
3131

32-
this.fetch = fetchWithAuth(this.key, this.namespaceOptions.fetch.client, this.accessToken);
32+
const { accessToken, key: apiKey, fetch: customFetch } = this;
33+
34+
this.fetch = fetchWithAuth({ accessToken, apiKey, customFetch });
3335

3436
if (this.proxy) {
3537
this.baseUrl = this.namespaceOptions.fetch.options.proxy!.url;

0 commit comments

Comments
 (0)