diff --git a/src/lib/helpers.ts b/src/lib/helpers.ts index 88738ef..ed3300c 100644 --- a/src/lib/helpers.ts +++ b/src/lib/helpers.ts @@ -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; +}; diff --git a/src/packages/AbstractClient.ts b/src/packages/AbstractClient.ts index 1a254e4..5006918 100644 --- a/src/packages/AbstractClient.ts +++ b/src/packages/AbstractClient.ts @@ -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, @@ -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. @@ -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); /** @@ -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; @@ -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; + } } diff --git a/src/packages/AbstractLiveClient.ts b/src/packages/AbstractLiveClient.ts index 1e6294a..da25fe1 100644 --- a/src/packages/AbstractLiveClient.ts +++ b/src/packages/AbstractLiveClient.ts @@ -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); diff --git a/src/packages/AbstractRestClient.ts b/src/packages/AbstractRestClient.ts index 65fbb9e..c499c06 100644 --- a/src/packages/AbstractRestClient.ts +++ b/src/packages/AbstractRestClient.ts @@ -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) { diff --git a/src/packages/LiveClient.ts b/src/packages/LiveClient.ts index 5899f3e..28e0294 100644 --- a/src/packages/LiveClient.ts +++ b/src/packages/LiveClient.ts @@ -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"; @@ -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); diff --git a/test/live.test.ts b/test/live.test.ts index b777af2..dbaef96 100644 --- a/test/live.test.ts +++ b/test/live.test.ts @@ -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" }, }); });