Skip to content

Commit 565ae2c

Browse files
committed
feat: merge in new websocket transmitter changes
1 parent 331454e commit 565ae2c

30 files changed

Lines changed: 1309 additions & 257 deletions

examples/node-prerecorded/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const transcribeUrl = async () => {
2121
const transcribeFile = async () => {
2222
const deepgram = createClient(process.env.DEEPGRAM_API_KEY);
2323

24-
const file = fs.readFileSync("./examples/nasa.mp4");
24+
const file = fs.readFileSync("./examples/spacewalk.wav");
2525

2626
console.log("Transcribing file", file);
2727
const { result, error } = await deepgram.listen.prerecorded.transcribeFile(file, {

examples/spacewalk.wav

2.18 MB
Binary file not shown.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
"watch": "nodemon -e ts --watch src --exec \"npm run build\"",
4848
"test": "mocha -r ts-node/register test/*test.ts test/**/*test.ts --inspect --exit",
4949
"test:coverage": "nyc --reporter=lcovonly --reporter=text --reporter=text-summary npm run test",
50-
"docs": "typedoc --entryPoints src/index.ts --out docs/v2 --includes src/**/*.ts",
51-
"docs:json": "typedoc --entryPoints src/index.ts --includes src/**/*.ts --json docs/v2/spec.json --excludeExternals"
50+
"docs": "typedoc --entryPoints src/index.ts --out docs/ --includes src/packages/**/*.ts --emit none",
51+
"docs:json": "typedoc --entryPoints src/index.ts --includes src/packages/**/*.ts --json docs/spec.json --emit none"
5252
},
5353
"dependencies": {
5454
"@deepgram/captions": "^1.1.1",

src/DeepgramClient.ts

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,68 @@ import {
44
ListenClient,
55
ManageClient,
66
ReadClient,
7-
SelfHostedClient as OnPremClient,
8-
SelfHostedClient,
7+
OnPremClient,
8+
SelfHostedRestClient,
99
SpeakClient,
1010
} from "./packages";
1111

1212
/**
13-
* The DeepgramClient class provides access to various Deepgram API clients, including ListenClient, ManageClient, SelfHostedClient, ReadClient, and SpeakClient.
13+
* The DeepgramClient class provides access to various Deepgram API clients, including ListenClient, ManageClient, SelfHostedRestClient, ReadClient, and SpeakClient.
1414
*
1515
* @see https://github.com/deepgram/deepgram-js-sdk
1616
*/
1717
export default class DeepgramClient extends AbstractClient {
18+
/**
19+
* Returns a new instance of the ListenClient, which provides access to the Deepgram API's listening functionality.
20+
*
21+
* @returns {ListenClient} A new instance of the ListenClient.
22+
*/
1823
get listen(): ListenClient {
1924
return new ListenClient(this.options);
2025
}
2126

27+
/**
28+
* Returns a new instance of the ManageClient, which provides access to the Deepgram API's management functionality.
29+
*
30+
* @returns {ManageClient} A new instance of the ManageClient.
31+
*/
2232
get manage(): ManageClient {
2333
return new ManageClient(this.options);
2434
}
2535

2636
/**
27-
* backwards compatibility for renaming onprem to selfhosted
37+
* Returns a new instance of the SelfHostedRestClient, which provides access to the Deepgram API's self-hosted functionality.
38+
*
39+
* @returns {OnPremClient} A new instance of the SelfHostedRestClient named as OnPremClient.
2840
* @deprecated use selfhosted() instead
2941
*/
3042
get onprem(): OnPremClient {
3143
return this.selfhosted;
3244
}
3345

34-
get selfhosted(): SelfHostedClient {
35-
return new SelfHostedClient(this.options);
46+
/**
47+
* Returns a new instance of the SelfHostedRestClient, which provides access to the Deepgram API's self-hosted functionality.
48+
*
49+
* @returns {SelfHostedRestClient} A new instance of the SelfHostedRestClient.
50+
*/
51+
get selfhosted(): SelfHostedRestClient {
52+
return new SelfHostedRestClient(this.options);
3653
}
3754

55+
/**
56+
* Returns a new instance of the ReadClient, which provides access to the Deepgram API's reading functionality.
57+
*
58+
* @returns {ReadClient} A new instance of the ReadClient.
59+
*/
3860
get read(): ReadClient {
3961
return new ReadClient(this.options);
4062
}
4163

64+
/**
65+
* Returns a new instance of the SpeakClient, which provides access to the Deepgram API's speaking functionality.
66+
*
67+
* @returns {SpeakClient} A new instance of the SpeakClient.
68+
*/
4269
get speak(): SpeakClient {
4370
return new SpeakClient(this.options);
4471
}

src/lib/fetch.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import type { Fetch } from "./types";
1010
*/
1111
export const resolveFetch = (customFetch?: Fetch): Fetch => {
1212
let _fetch: Fetch;
13+
1314
if (customFetch) {
1415
_fetch = customFetch;
1516
} else if (typeof fetch === "undefined") {
1617
_fetch = crossFetch as unknown as Fetch;
1718
} else {
1819
_fetch = fetch;
1920
}
21+
2022
return (...args) => _fetch(...args);
2123
};
2224

src/lib/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export function stripTrailingSlash(url: string): string {
1919
export const isBrowser = () => typeof window !== "undefined";
2020
export const isServer = () => typeof process !== "undefined";
2121

22-
export function applyDefaults<O, S>(options: Partial<O>, subordinate: Partial<S>): S {
22+
export function applyDefaults<O, S>(options: Partial<O> = {}, subordinate: Partial<S> = {}): S {
2323
return merge(subordinate, options);
2424
}
2525

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,9 @@
1-
import { FetchOptions } from "./Fetch";
1+
import { Fetch, FetchOptions } from "./Fetch";
22

33
export type IKeyFactory = () => string;
44
export type IFetch = typeof fetch;
55
export type IWebSocket = typeof WebSocket;
66

7-
/**
8-
* Defines the arguments for creating a Deepgram client.
9-
*
10-
* The `DeepgramClientArgs` type represents the possible arguments that can be passed when creating a Deepgram client. It can be either:
11-
*
12-
* 1. An array with two elements:
13-
* - The first element is a string or an `IKeyFactory` object, representing the API key.
14-
* - The second element is a `DeepgramClientOptions` object, representing the configuration options for the Deepgram client.
15-
* 2. An array with a single `DeepgramClientOptions` object, representing the configuration options for the Deepgram client.
16-
*/
17-
18-
/**
19-
* Configures the options for a Deepgram client.
20-
*
21-
* The `DeepgramClientOptions` interface defines the configuration options for a Deepgram client. It includes options for various namespaces, such as `global`, `listen`, `manage`, `onprem`, `read`, and `speak`. Each namespace has its own options for configuring the transport, including the URL, proxy, and options for the fetch and WebSocket clients.
22-
*
23-
* The `global` namespace is used to configure options that apply globally to the Deepgram client. The other namespaces are used to configure options specific to different Deepgram API endpoints.
24-
*/
25-
export interface DeepgramClientOptions {
26-
key?: string | IKeyFactory;
27-
global?: NamespaceOptions & { url?: string; headers?: { [index: string]: any } };
28-
listen?: NamespaceOptions;
29-
manage?: NamespaceOptions;
30-
onprem?: NamespaceOptions;
31-
read?: NamespaceOptions;
32-
speak?: NamespaceOptions;
33-
34-
/**
35-
* Support introductory format
36-
*/
37-
[index: string]: any;
38-
// _experimentalCustomFetch?: Fetch;
39-
// restProxy?: {
40-
// url: null | string;
41-
// };
42-
}
43-
447
interface TransportFetchOptions extends TransportOptions, FetchOptions {}
458
interface TransportWebSocketOptions extends TransportOptions {
469
_nodeOnlyHeaders?: { [index: string]: any };
@@ -59,12 +22,9 @@ interface ITransport<C, O> {
5922
client?: C;
6023
options?: O;
6124
}
62-
export interface NamespaceOptions {
63-
fetch?: ITransport<IFetch, TransportFetchOptions>;
64-
websocket?: ITransport<IWebSocket, TransportWebSocketOptions>;
65-
}
6625

6726
export type DefaultNamespaceOptions = {
27+
key: string;
6828
fetch: {
6929
options: { url: TransportUrl };
7030
};
@@ -73,6 +33,52 @@ export type DefaultNamespaceOptions = {
7333
};
7434
} & NamespaceOptions;
7535

36+
export interface NamespaceOptions {
37+
key?: string;
38+
fetch?: ITransport<IFetch, TransportFetchOptions>;
39+
websocket?: ITransport<IWebSocket, TransportWebSocketOptions>;
40+
}
41+
7642
export type DefaultClientOptions = {
77-
global: DefaultNamespaceOptions;
43+
global: Partial<DefaultNamespaceOptions>;
7844
} & DeepgramClientOptions;
45+
46+
/**
47+
* Configures the options for a Deepgram client.
48+
*
49+
* The `DeepgramClientOptions` interface defines the configuration options for a Deepgram client. It includes options for various namespaces, such as `global`, `listen`, `manage`, `onprem`, `read`, and `speak`. Each namespace has its own options for configuring the transport, including the URL, proxy, and options for the fetch and WebSocket clients.
50+
*
51+
* The `global` namespace is used to configure options that apply globally to the Deepgram client. The other namespaces are used to configure options specific to different Deepgram API endpoints.
52+
* Support introductory formats:
53+
* - fetch: FetchOptions;
54+
* - _experimentalCustomFetch?: Fetch;
55+
* - restProxy?: {
56+
* url: null | string;
57+
* };
58+
*/
59+
export interface DeepgramClientOptions {
60+
key?: string | IKeyFactory;
61+
global?: NamespaceOptions & { url?: string; headers?: { [index: string]: any } };
62+
listen?: NamespaceOptions;
63+
manage?: NamespaceOptions;
64+
onprem?: NamespaceOptions;
65+
read?: NamespaceOptions;
66+
speak?: NamespaceOptions;
67+
68+
/**
69+
* @deprecated as of 3.4, use a namespace like `global` instead
70+
*/
71+
fetch?: FetchOptions;
72+
73+
/**
74+
* @deprecated as of 3.4, use a namespace like `global` instead
75+
*/
76+
_experimentalCustomFetch?: Fetch;
77+
78+
/**
79+
* @deprecated as of 3.4, use a namespace like `global` instead
80+
*/
81+
restProxy?: {
82+
url: null | string;
83+
};
84+
}

src/lib/types/LiveConfigOptions.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
export interface LiveConfigOptions {
2-
numerals?: boolean;
3-
}
1+
import { LiveSchema } from "./TranscriptionSchema";
2+
3+
/**
4+
* Partial configuration options for the LiveSchema, including:
5+
* - `numerals`: Configures how numerals are handled in the live transcription.
6+
*/
7+
export type LiveConfigOptions = Partial<Pick<LiveSchema, "numerals">>;

src/packages/AbstractClient.ts

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import EventEmitter from "events";
22
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
33
import { DeepgramError } from "../lib/errors";
44
import { appendSearchParams, applyDefaults, convertLegacyOptions } from "../lib/helpers";
5-
import { DeepgramClientOptions, LiveSchema, TranscriptionSchema } from "../lib/types";
6-
import {
5+
import type {
6+
DeepgramClientOptions,
77
DefaultClientOptions,
88
DefaultNamespaceOptions,
99
NamespaceOptions,
10-
} from "../lib/types/DeepgramClientOptions";
10+
} from "../lib/types";
11+
12+
export const noop = () => {};
1113

1214
/**
1315
* Represents an abstract Deepgram client that provides a base implementation for interacting with the Deepgram API.
@@ -22,11 +24,11 @@ import {
2224
export abstract class AbstractClient extends EventEmitter {
2325
protected factory: Function | undefined = undefined;
2426
protected key: string;
25-
protected namespaceOptions: DefaultNamespaceOptions;
2627
protected options: DefaultClientOptions;
2728
public namespace: string = "global";
2829
public version: string = "v1";
2930
public baseUrl: string = DEFAULT_URL;
31+
public logger: Function = noop;
3032

3133
/**
3234
* Constructs a new instance of the DeepgramClient class with the provided options.
@@ -69,28 +71,37 @@ export abstract class AbstractClient extends EventEmitter {
6971
options,
7072
DEFAULT_OPTIONS
7173
);
72-
73-
/**
74-
* Roll up options for this namespace.
75-
*/
76-
this.namespaceOptions = applyDefaults<NamespaceOptions, DefaultNamespaceOptions>(
77-
this.options[this.namespace],
78-
this.options.global!
79-
);
8074
}
8175

76+
/**
77+
* Sets the version for the current instance of the Deepgram API and returns the instance.
78+
*
79+
* @param version - The version to set for the Deepgram API instance. Defaults to "v1" if not provided.
80+
* @returns The current instance of the AbstractClient with the updated version.
81+
*/
8282
public v(version: string = "v1"): this {
8383
this.version = version;
8484

8585
return this;
8686
}
8787

8888
/**
89-
* Determines whether the current instance should proxy requests.
90-
* @returns {boolean} true if the current instance should proxy requests; otherwise, false
89+
* Gets the namespace options for the current instance of the AbstractClient.
90+
* The namespace options include the default options merged with the global options,
91+
* and the API key for the current instance.
92+
*
93+
* @returns The namespace options for the current instance.
9194
*/
92-
get proxy(): boolean {
93-
return this.key === "proxy" && !!this.namespaceOptions.fetch.options.proxy?.url;
95+
get namespaceOptions(): DefaultNamespaceOptions {
96+
const defaults = applyDefaults<NamespaceOptions, DefaultNamespaceOptions>(
97+
(this.options as any)[this.namespace],
98+
this.options.global
99+
);
100+
101+
return {
102+
...defaults,
103+
key: this.key,
104+
};
94105
}
95106

96107
/**
@@ -134,4 +145,13 @@ export abstract class AbstractClient extends EventEmitter {
134145

135146
return url;
136147
}
148+
149+
/**
150+
* Logs the message.
151+
*
152+
* For customized logging, `this.logger` can be overridden.
153+
*/
154+
public log(kind: string, msg: string, data?: any) {
155+
this.logger(kind, msg, data);
156+
}
137157
}

0 commit comments

Comments
 (0)