Skip to content

Commit

Permalink
feat: plug live client into new options
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeocodes committed Apr 30, 2024
1 parent 7bcc14c commit 21046a7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 82 deletions.
64 changes: 64 additions & 0 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,67 @@ export function isLiveSchema(arg: any): arg is LiveSchema {
export function isDeepgramClientOptions(arg: any): arg is DeepgramClientOptions {
return arg && typeof arg.global !== "undefined";
}

export const convertLegacyOptions = (optionsArg: DeepgramClientOptions): DeepgramClientOptions => {
const newOptions: DeepgramClientOptions = {};

if (optionsArg._experimentalCustomFetch) {
newOptions.global = {
fetch: {
client: optionsArg._experimentalCustomFetch,
},
};
}

optionsArg = merge(optionsArg, newOptions);

if (optionsArg.restProxy?.url) {
newOptions.global = {
fetch: {
options: {
proxy: {
url: optionsArg.restProxy?.url,
},
},
},
};
}

optionsArg = merge(optionsArg, newOptions);

if (optionsArg.global?.url) {
newOptions.global = {
fetch: {
options: {
url: optionsArg.global.url,
},
},
websocket: {
options: {
url: optionsArg.global.url,
},
},
};
}

optionsArg = merge(optionsArg, newOptions);

if (optionsArg.global?.headers) {
newOptions.global = {
fetch: {
options: {
headers: optionsArg.global?.headers,
},
},
websocket: {
options: {
_nodeOnlyHeaders: optionsArg.global?.headers,
},
},
};
}

optionsArg = merge(optionsArg, newOptions);

return optionsArg;
};
98 changes: 28 additions & 70 deletions src/packages/AbstractClient.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import EventEmitter from "events";
import { DEFAULT_OPTIONS } from "../lib/constants";
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
import { DeepgramError } from "../lib/errors";
import { applyDefaults } from "../lib/helpers";
import { DeepgramClientOptions } from "../lib/types";
import merge from "deepmerge";
import { appendSearchParams, applyDefaults, convertLegacyOptions } from "../lib/helpers";
import { DeepgramClientOptions, LiveSchema, TranscriptionSchema } from "../lib/types";
import {
DefaultClientOptions,
DefaultNamespaceOptions,
Expand All @@ -26,7 +25,8 @@ export abstract class AbstractClient extends EventEmitter {
protected namespaceOptions: DefaultNamespaceOptions;
protected options: DefaultClientOptions;
public namespace: string = "global";
public version: number = 1;
public version: string = "v1";
public baseUrl: string = DEFAULT_URL;

/**
* Constructs a new instance of the DeepgramClient class with the provided options.
Expand Down Expand Up @@ -60,70 +60,6 @@ export abstract class AbstractClient extends EventEmitter {

this.key = key;

const convertLegacyOptions = (optionsArg: DeepgramClientOptions): DeepgramClientOptions => {
const newOptions: DeepgramClientOptions = {};

if (optionsArg._experimentalCustomFetch) {
newOptions.global = {
fetch: {
client: optionsArg._experimentalCustomFetch,
},
};
}

optionsArg = merge(optionsArg, newOptions);

if (optionsArg.restProxy?.url) {
newOptions.global = {
fetch: {
options: {
proxy: {
url: optionsArg.restProxy?.url,
},
},
},
};
}

optionsArg = merge(optionsArg, newOptions);

if (optionsArg.global?.url) {
newOptions.global = {
fetch: {
options: {
url: optionsArg.global.url,
},
},
websocket: {
options: {
url: optionsArg.global.url,
},
},
};
}

optionsArg = merge(optionsArg, newOptions);

if (optionsArg.global?.headers) {
newOptions.global = {
fetch: {
options: {
headers: optionsArg.global?.headers,
},
},
websocket: {
options: {
_nodeOnlyHeaders: optionsArg.global?.headers,
},
},
};
}

optionsArg = merge(optionsArg, newOptions);

return optionsArg;
};

options = convertLegacyOptions(options);

/**
Expand All @@ -143,7 +79,7 @@ export abstract class AbstractClient extends EventEmitter {
);
}

public v(version: number = 1) {
public v(version: string = "v1"): this {
this.version = version;

return this;
Expand All @@ -156,4 +92,26 @@ export abstract class AbstractClient extends EventEmitter {
get proxy(): boolean {
return this.key === "proxy" && !!this.namespaceOptions.fetch.options.proxy?.url;
}

/**
* Generates a URL for the specified endpoint and transcription options.
*
* @param endpoint - The endpoint URL, which may contain a "{version}" placeholder that will be replaced with the client's version.
* @param transcriptionOptions - The transcription options to include as query parameters in the URL.
* @returns A URL object representing the generated URL.
*/
public getRequestUrl(
endpoint: string,
transcriptionOptions: LiveSchema | TranscriptionSchema
): URL {
/**
* Version the URL endpoints if they can be versioned.
*/
endpoint = endpoint.replace("{version}", this.version);

const url = new URL(endpoint as string, this.baseUrl);
appendSearchParams(url.searchParams, transcriptionOptions);

return url;
}
}
2 changes: 0 additions & 2 deletions src/packages/AbstractLiveClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { DeepgramClientOptions } from "../lib/types";
import { AbstractClient } from "./AbstractClient";

export abstract class AbstractLiveClient extends AbstractClient {
protected baseUrl: string;

// Constructor implementation
constructor(options: DeepgramClientOptions) {
super(options);
Expand Down
1 change: 0 additions & 1 deletion src/packages/AbstractRestClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { isBrowser } from "../lib/helpers";

export abstract class AbstractRestClient extends AbstractClient {
protected fetch: Fetch;
protected baseUrl: string;

// Constructor implementation
constructor(options: DeepgramClientOptions) {
Expand Down
10 changes: 2 additions & 8 deletions src/packages/LiveClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { AbstractLiveClient } from "./AbstractLiveClient";
import { appendSearchParams, isDeepgramClientOptions, isLiveSchema } from "../lib/helpers";
import { DeepgramError } from "../lib/errors";
import { LiveConnectionState, LiveTranscriptionEvents } from "../lib/enums";
import { w3cwebsocket } from "websocket";
Expand All @@ -26,13 +25,8 @@ export class LiveClient extends AbstractLiveClient {
) {
super(options);

// endpoint = endpoint.replace("{version}", this.version.toString());

const url = new URL(endpoint as string, this.baseUrl);
url.protocol = url.protocol.toLowerCase().replace(/(http)(s)?/gi, "ws$2");
appendSearchParams(url.searchParams, transcriptionOptions);

this._socket = new w3cwebsocket(url.toString(), ["token", this.key]);
const requestUrl = this.getRequestUrl(endpoint, transcriptionOptions);
this._socket = new w3cwebsocket(requestUrl.toString(), ["token", this.key]);

this._socket.onopen = () => {
this.emit(LiveTranscriptionEvents.Open, this);
Expand Down
2 changes: 1 addition & 1 deletion test/live.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("connecting to our transcription websocket", () => {

beforeEach(() => {
deepgram = createClient(faker.string.alphanumeric(40), {
global: { url: "https://api.mock.deepgram.com" },
global: { url: "wss://api.mock.deepgram.com" },
});
});

Expand Down

0 comments on commit 21046a7

Please sign in to comment.