@deepgram/sdk 3.3.4 → 3.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -9
- package/dist/main/DeepgramClient.d.ts +65 -12
- package/dist/main/DeepgramClient.d.ts.map +1 -1
- package/dist/main/DeepgramClient.js +73 -18
- package/dist/main/DeepgramClient.js.map +1 -1
- package/dist/main/index.d.ts +13 -4
- package/dist/main/index.d.ts.map +1 -1
- package/dist/main/index.js +18 -8
- package/dist/main/index.js.map +1 -1
- package/dist/main/lib/constants.d.ts +16 -7
- package/dist/main/lib/constants.d.ts.map +1 -1
- package/dist/main/lib/constants.js +26 -9
- package/dist/main/lib/constants.js.map +1 -1
- package/dist/main/lib/enums/LiveConnectionState.d.ts +5 -0
- package/dist/main/lib/enums/LiveConnectionState.d.ts.map +1 -1
- package/dist/main/lib/enums/LiveConnectionState.js +5 -0
- package/dist/main/lib/enums/LiveConnectionState.js.map +1 -1
- package/dist/main/lib/enums/LiveTranscriptionEvents.d.ts +24 -3
- package/dist/main/lib/enums/LiveTranscriptionEvents.d.ts.map +1 -1
- package/dist/main/lib/enums/LiveTranscriptionEvents.js +23 -2
- package/dist/main/lib/enums/LiveTranscriptionEvents.js.map +1 -1
- package/dist/main/lib/enums/index.d.ts +2 -2
- package/dist/main/lib/enums/index.d.ts.map +1 -1
- package/dist/main/lib/enums/index.js +16 -5
- package/dist/main/lib/enums/index.js.map +1 -1
- package/dist/main/lib/fetch.d.ts +19 -1
- package/dist/main/lib/fetch.d.ts.map +1 -1
- package/dist/main/lib/fetch.js +19 -1
- package/dist/main/lib/fetch.js.map +1 -1
- package/dist/main/lib/helpers.d.ts +9 -5
- package/dist/main/lib/helpers.d.ts.map +1 -1
- package/dist/main/lib/helpers.js +82 -7
- package/dist/main/lib/helpers.js.map +1 -1
- package/dist/main/lib/types/CreateProjectKeySchema.d.ts +0 -1
- package/dist/main/lib/types/CreateProjectKeySchema.d.ts.map +1 -1
- package/dist/main/lib/types/DeepgramClientOptions.d.ts +75 -8
- package/dist/main/lib/types/DeepgramClientOptions.d.ts.map +1 -1
- package/dist/main/lib/types/Fetch.d.ts +3 -13
- package/dist/main/lib/types/Fetch.d.ts.map +1 -1
- package/dist/main/lib/types/LiveConfigOptions.d.ts +6 -3
- package/dist/main/lib/types/LiveConfigOptions.d.ts.map +1 -1
- package/dist/main/lib/types/SyncPrerecordedResponse.d.ts +1 -0
- package/dist/main/lib/types/SyncPrerecordedResponse.d.ts.map +1 -1
- package/dist/main/lib/types/TranscriptionSchema.d.ts +0 -1
- package/dist/main/lib/types/TranscriptionSchema.d.ts.map +1 -1
- package/dist/main/lib/types/UpdateProjectMemberScopeSchema.d.ts +1 -1
- package/dist/main/lib/types/UpdateProjectMemberScopeSchema.d.ts.map +1 -1
- package/dist/main/lib/types/UpdateProjectSchema.d.ts +0 -1
- package/dist/main/lib/types/UpdateProjectSchema.d.ts.map +1 -1
- package/dist/main/lib/types/index.d.ts +39 -39
- package/dist/main/lib/types/index.d.ts.map +1 -1
- package/dist/main/lib/types/index.js +53 -0
- package/dist/main/lib/types/index.js.map +1 -1
- package/dist/main/lib/version.d.ts +1 -1
- package/dist/main/lib/version.js +1 -1
- package/dist/main/packages/AbstractClient.d.ts +63 -10
- package/dist/main/packages/AbstractClient.d.ts.map +1 -1
- package/dist/main/packages/AbstractClient.js +103 -50
- package/dist/main/packages/AbstractClient.js.map +1 -1
- package/dist/main/packages/AbstractLiveClient.d.ts +114 -0
- package/dist/main/packages/AbstractLiveClient.d.ts.map +1 -0
- package/dist/main/packages/AbstractLiveClient.js +238 -0
- package/dist/main/packages/AbstractLiveClient.js.map +1 -0
- package/dist/main/packages/AbstractRestClient.d.ts +105 -0
- package/dist/main/packages/AbstractRestClient.d.ts.map +1 -0
- package/dist/main/packages/AbstractRestClient.js +185 -0
- package/dist/main/packages/AbstractRestClient.js.map +1 -0
- package/dist/main/packages/ListenClient.d.ts +21 -4
- package/dist/main/packages/ListenClient.d.ts.map +1 -1
- package/dist/main/packages/ListenClient.js +25 -5
- package/dist/main/packages/ListenClient.js.map +1 -1
- package/dist/main/packages/ListenLiveClient.d.ts +58 -0
- package/dist/main/packages/ListenLiveClient.d.ts.map +1 -0
- package/dist/main/packages/ListenLiveClient.js +119 -0
- package/dist/main/packages/ListenLiveClient.js.map +1 -0
- package/dist/main/packages/ListenRestClient.d.ts +57 -0
- package/dist/main/packages/ListenRestClient.d.ts.map +1 -0
- package/dist/main/packages/ListenRestClient.js +167 -0
- package/dist/main/packages/ListenRestClient.js.map +1 -0
- package/dist/main/packages/ManageRestClient.d.ts +286 -0
- package/dist/main/packages/ManageRestClient.d.ts.map +1 -0
- package/dist/main/packages/ManageRestClient.js +628 -0
- package/dist/main/packages/ManageRestClient.js.map +1 -0
- package/dist/main/packages/ReadRestClient.d.ts +57 -0
- package/dist/main/packages/ReadRestClient.d.ts.map +1 -0
- package/dist/main/packages/ReadRestClient.js +165 -0
- package/dist/main/packages/ReadRestClient.js.map +1 -0
- package/dist/main/packages/SelfHostedRestClient.d.ts +47 -0
- package/dist/main/packages/SelfHostedRestClient.d.ts.map +1 -0
- package/dist/main/packages/SelfHostedRestClient.js +120 -0
- package/dist/main/packages/SelfHostedRestClient.js.map +1 -0
- package/dist/main/packages/SpeakRestClient.d.ts +36 -0
- package/dist/main/packages/SpeakRestClient.d.ts.map +1 -0
- package/dist/main/packages/SpeakRestClient.js +84 -0
- package/dist/main/packages/SpeakRestClient.js.map +1 -0
- package/dist/main/packages/index.d.ts +10 -8
- package/dist/main/packages/index.d.ts.map +1 -1
- package/dist/main/packages/index.js +24 -17
- package/dist/main/packages/index.js.map +1 -1
- package/dist/module/DeepgramClient.d.ts +65 -12
- package/dist/module/DeepgramClient.d.ts.map +1 -1
- package/dist/module/DeepgramClient.js +72 -17
- package/dist/module/DeepgramClient.js.map +1 -1
- package/dist/module/index.d.ts +13 -4
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +18 -8
- package/dist/module/index.js.map +1 -1
- package/dist/module/lib/constants.d.ts +16 -7
- package/dist/module/lib/constants.d.ts.map +1 -1
- package/dist/module/lib/constants.js +26 -9
- package/dist/module/lib/constants.js.map +1 -1
- package/dist/module/lib/enums/LiveConnectionState.d.ts +5 -0
- package/dist/module/lib/enums/LiveConnectionState.d.ts.map +1 -1
- package/dist/module/lib/enums/LiveConnectionState.js +5 -0
- package/dist/module/lib/enums/LiveConnectionState.js.map +1 -1
- package/dist/module/lib/enums/LiveTranscriptionEvents.d.ts +24 -3
- package/dist/module/lib/enums/LiveTranscriptionEvents.d.ts.map +1 -1
- package/dist/module/lib/enums/LiveTranscriptionEvents.js +23 -2
- package/dist/module/lib/enums/LiveTranscriptionEvents.js.map +1 -1
- package/dist/module/lib/enums/index.d.ts +2 -2
- package/dist/module/lib/enums/index.d.ts.map +1 -1
- package/dist/module/lib/enums/index.js +2 -2
- package/dist/module/lib/enums/index.js.map +1 -1
- package/dist/module/lib/fetch.d.ts +19 -1
- package/dist/module/lib/fetch.d.ts.map +1 -1
- package/dist/module/lib/fetch.js +19 -1
- package/dist/module/lib/fetch.js.map +1 -1
- package/dist/module/lib/helpers.d.ts +9 -5
- package/dist/module/lib/helpers.d.ts.map +1 -1
- package/dist/module/lib/helpers.js +75 -4
- package/dist/module/lib/helpers.js.map +1 -1
- package/dist/module/lib/types/CreateProjectKeySchema.d.ts +0 -1
- package/dist/module/lib/types/CreateProjectKeySchema.d.ts.map +1 -1
- package/dist/module/lib/types/DeepgramClientOptions.d.ts +75 -8
- package/dist/module/lib/types/DeepgramClientOptions.d.ts.map +1 -1
- package/dist/module/lib/types/Fetch.d.ts +3 -13
- package/dist/module/lib/types/Fetch.d.ts.map +1 -1
- package/dist/module/lib/types/LiveConfigOptions.d.ts +6 -3
- package/dist/module/lib/types/LiveConfigOptions.d.ts.map +1 -1
- package/dist/module/lib/types/SyncPrerecordedResponse.d.ts +1 -0
- package/dist/module/lib/types/SyncPrerecordedResponse.d.ts.map +1 -1
- package/dist/module/lib/types/TranscriptionSchema.d.ts +0 -1
- package/dist/module/lib/types/TranscriptionSchema.d.ts.map +1 -1
- package/dist/module/lib/types/UpdateProjectMemberScopeSchema.d.ts +1 -1
- package/dist/module/lib/types/UpdateProjectMemberScopeSchema.d.ts.map +1 -1
- package/dist/module/lib/types/UpdateProjectSchema.d.ts +0 -1
- package/dist/module/lib/types/UpdateProjectSchema.d.ts.map +1 -1
- package/dist/module/lib/types/index.d.ts +39 -39
- package/dist/module/lib/types/index.d.ts.map +1 -1
- package/dist/module/lib/types/index.js +39 -1
- package/dist/module/lib/types/index.js.map +1 -1
- package/dist/module/lib/version.d.ts +1 -1
- package/dist/module/lib/version.js +1 -1
- package/dist/module/packages/AbstractClient.d.ts +63 -10
- package/dist/module/packages/AbstractClient.d.ts.map +1 -1
- package/dist/module/packages/AbstractClient.js +99 -50
- package/dist/module/packages/AbstractClient.js.map +1 -1
- package/dist/module/packages/AbstractLiveClient.d.ts +114 -0
- package/dist/module/packages/AbstractLiveClient.d.ts.map +1 -0
- package/dist/module/packages/AbstractLiveClient.js +211 -0
- package/dist/module/packages/AbstractLiveClient.js.map +1 -0
- package/dist/module/packages/AbstractRestClient.d.ts +105 -0
- package/dist/module/packages/AbstractRestClient.d.ts.map +1 -0
- package/dist/module/packages/AbstractRestClient.js +178 -0
- package/dist/module/packages/AbstractRestClient.js.map +1 -0
- package/dist/module/packages/ListenClient.d.ts +21 -4
- package/dist/module/packages/ListenClient.d.ts.map +1 -1
- package/dist/module/packages/ListenClient.js +25 -5
- package/dist/module/packages/ListenClient.js.map +1 -1
- package/dist/module/packages/ListenLiveClient.d.ts +58 -0
- package/dist/module/packages/ListenLiveClient.d.ts.map +1 -0
- package/dist/module/packages/ListenLiveClient.js +115 -0
- package/dist/module/packages/ListenLiveClient.js.map +1 -0
- package/dist/module/packages/ListenRestClient.d.ts +57 -0
- package/dist/module/packages/ListenRestClient.d.ts.map +1 -0
- package/dist/module/packages/ListenRestClient.js +163 -0
- package/dist/module/packages/ListenRestClient.js.map +1 -0
- package/dist/module/packages/ManageRestClient.d.ts +286 -0
- package/dist/module/packages/ManageRestClient.d.ts.map +1 -0
- package/dist/module/packages/ManageRestClient.js +624 -0
- package/dist/module/packages/ManageRestClient.js.map +1 -0
- package/dist/module/packages/ReadRestClient.d.ts +57 -0
- package/dist/module/packages/ReadRestClient.d.ts.map +1 -0
- package/dist/module/packages/ReadRestClient.js +161 -0
- package/dist/module/packages/ReadRestClient.js.map +1 -0
- package/dist/module/packages/SelfHostedRestClient.d.ts +47 -0
- package/dist/module/packages/SelfHostedRestClient.d.ts.map +1 -0
- package/dist/module/packages/SelfHostedRestClient.js +116 -0
- package/dist/module/packages/SelfHostedRestClient.js.map +1 -0
- package/dist/module/packages/SpeakRestClient.d.ts +36 -0
- package/dist/module/packages/SpeakRestClient.d.ts.map +1 -0
- package/dist/module/packages/SpeakRestClient.js +80 -0
- package/dist/module/packages/SpeakRestClient.js.map +1 -0
- package/dist/module/packages/index.d.ts +10 -8
- package/dist/module/packages/index.d.ts.map +1 -1
- package/dist/module/packages/index.js +10 -8
- package/dist/module/packages/index.js.map +1 -1
- package/dist/umd/deepgram.js +1 -1
- package/package.json +8 -6
- package/src/DeepgramClient.ts +81 -17
- package/src/index.ts +32 -7
- package/src/lib/constants.ts +33 -13
- package/src/lib/enums/LiveConnectionState.ts +11 -4
- package/src/lib/enums/LiveTranscriptionEvents.ts +27 -4
- package/src/lib/enums/index.ts +2 -2
- package/src/lib/fetch.ts +22 -2
- package/src/lib/helpers.ts +98 -9
- package/src/lib/types/CreateProjectKeySchema.ts +0 -1
- package/src/lib/types/DeepgramClientOptions.ts +75 -11
- package/src/lib/types/Fetch.ts +2 -24
- package/src/lib/types/LiveConfigOptions.ts +7 -3
- package/src/lib/types/SyncPrerecordedResponse.ts +1 -0
- package/src/lib/types/TranscriptionSchema.ts +0 -2
- package/src/lib/types/UpdateProjectMemberScopeSchema.ts +1 -1
- package/src/lib/types/UpdateProjectSchema.ts +0 -1
- package/src/lib/types/index.ts +39 -54
- package/src/lib/version.ts +1 -1
- package/src/packages/AbstractClient.ts +129 -61
- package/src/packages/AbstractLiveClient.ts +280 -0
- package/src/packages/AbstractRestClient.ts +221 -0
- package/src/packages/ListenClient.ts +26 -5
- package/src/packages/ListenLiveClient.ts +133 -0
- package/src/packages/ListenRestClient.ts +201 -0
- package/src/packages/ManageRestClient.ts +760 -0
- package/src/packages/ReadRestClient.ts +200 -0
- package/src/packages/SelfHostedRestClient.ts +134 -0
- package/src/packages/SpeakRestClient.ts +79 -0
- package/src/packages/index.ts +10 -8
- package/dist/main/packages/AbstractRestfulClient.d.ts +0 -26
- package/dist/main/packages/AbstractRestfulClient.d.ts.map +0 -1
- package/dist/main/packages/AbstractRestfulClient.js +0 -118
- package/dist/main/packages/AbstractRestfulClient.js.map +0 -1
- package/dist/main/packages/AbstractWsClient.d.ts +0 -10
- package/dist/main/packages/AbstractWsClient.d.ts.map +0 -1
- package/dist/main/packages/AbstractWsClient.js +0 -33
- package/dist/main/packages/AbstractWsClient.js.map +0 -1
- package/dist/main/packages/LiveClient.d.ts +0 -28
- package/dist/main/packages/LiveClient.d.ts.map +0 -1
- package/dist/main/packages/LiveClient.js +0 -110
- package/dist/main/packages/LiveClient.js.map +0 -1
- package/dist/main/packages/ManageClient.d.ts +0 -97
- package/dist/main/packages/ManageClient.d.ts.map +0 -1
- package/dist/main/packages/ManageClient.js +0 -463
- package/dist/main/packages/ManageClient.js.map +0 -1
- package/dist/main/packages/OnPremClient.d.ts +0 -21
- package/dist/main/packages/OnPremClient.d.ts.map +0 -1
- package/dist/main/packages/OnPremClient.js +0 -99
- package/dist/main/packages/OnPremClient.js.map +0 -1
- package/dist/main/packages/PrerecordedClient.d.ts +0 -10
- package/dist/main/packages/PrerecordedClient.d.ts.map +0 -1
- package/dist/main/packages/PrerecordedClient.js +0 -125
- package/dist/main/packages/PrerecordedClient.js.map +0 -1
- package/dist/main/packages/ReadClient.d.ts +0 -10
- package/dist/main/packages/ReadClient.d.ts.map +0 -1
- package/dist/main/packages/ReadClient.js +0 -123
- package/dist/main/packages/ReadClient.js.map +0 -1
- package/dist/main/packages/SpeakClient.d.ts +0 -12
- package/dist/main/packages/SpeakClient.d.ts.map +0 -1
- package/dist/main/packages/SpeakClient.js +0 -57
- package/dist/main/packages/SpeakClient.js.map +0 -1
- package/dist/module/packages/AbstractRestfulClient.d.ts +0 -26
- package/dist/module/packages/AbstractRestfulClient.d.ts.map +0 -1
- package/dist/module/packages/AbstractRestfulClient.js +0 -114
- package/dist/module/packages/AbstractRestfulClient.js.map +0 -1
- package/dist/module/packages/AbstractWsClient.d.ts +0 -10
- package/dist/module/packages/AbstractWsClient.d.ts.map +0 -1
- package/dist/module/packages/AbstractWsClient.js +0 -29
- package/dist/module/packages/AbstractWsClient.js.map +0 -1
- package/dist/module/packages/LiveClient.d.ts +0 -28
- package/dist/module/packages/LiveClient.d.ts.map +0 -1
- package/dist/module/packages/LiveClient.js +0 -106
- package/dist/module/packages/LiveClient.js.map +0 -1
- package/dist/module/packages/ManageClient.d.ts +0 -97
- package/dist/module/packages/ManageClient.d.ts.map +0 -1
- package/dist/module/packages/ManageClient.js +0 -459
- package/dist/module/packages/ManageClient.js.map +0 -1
- package/dist/module/packages/OnPremClient.d.ts +0 -21
- package/dist/module/packages/OnPremClient.d.ts.map +0 -1
- package/dist/module/packages/OnPremClient.js +0 -95
- package/dist/module/packages/OnPremClient.js.map +0 -1
- package/dist/module/packages/PrerecordedClient.d.ts +0 -10
- package/dist/module/packages/PrerecordedClient.d.ts.map +0 -1
- package/dist/module/packages/PrerecordedClient.js +0 -121
- package/dist/module/packages/PrerecordedClient.js.map +0 -1
- package/dist/module/packages/ReadClient.d.ts +0 -10
- package/dist/module/packages/ReadClient.d.ts.map +0 -1
- package/dist/module/packages/ReadClient.js +0 -119
- package/dist/module/packages/ReadClient.js.map +0 -1
- package/dist/module/packages/SpeakClient.d.ts +0 -12
- package/dist/module/packages/SpeakClient.d.ts.map +0 -1
- package/dist/module/packages/SpeakClient.js +0 -53
- package/dist/module/packages/SpeakClient.js.map +0 -1
- package/src/packages/AbstractRestfulClient.ts +0 -160
- package/src/packages/AbstractWsClient.ts +0 -42
- package/src/packages/LiveClient.ts +0 -140
- package/src/packages/ManageClient.ts +0 -584
- package/src/packages/OnPremClient.ts +0 -113
- package/src/packages/PrerecordedClient.ts +0 -162
- package/src/packages/ReadClient.ts +0 -161
- package/src/packages/SpeakClient.ts +0 -50
|
@@ -1,89 +1,157 @@
|
|
|
1
|
+
import EventEmitter from "events";
|
|
1
2
|
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
|
|
2
3
|
import { DeepgramError } from "../lib/errors";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
4
|
+
import { appendSearchParams, applyDefaults, convertLegacyOptions } from "../lib/helpers";
|
|
5
|
+
import type {
|
|
6
|
+
DeepgramClientOptions,
|
|
7
|
+
DefaultClientOptions,
|
|
8
|
+
DefaultNamespaceOptions,
|
|
9
|
+
NamespaceOptions,
|
|
10
|
+
} from "../lib/types";
|
|
11
|
+
|
|
12
|
+
export const noop = () => {};
|
|
5
13
|
|
|
6
14
|
/**
|
|
7
|
-
* Deepgram
|
|
15
|
+
* Represents an abstract Deepgram client that provides a base implementation for interacting with the Deepgram API.
|
|
16
|
+
*
|
|
17
|
+
* The `AbstractClient` class is responsible for:
|
|
18
|
+
* - Initializing the Deepgram API key
|
|
19
|
+
* - Applying default options for the client and namespace
|
|
20
|
+
* - Providing a namespace for organizing API requests
|
|
8
21
|
*
|
|
9
|
-
*
|
|
10
|
-
* @see https://developers.deepgram.com
|
|
22
|
+
* Subclasses of `AbstractClient` should implement the specific functionality for interacting with the Deepgram API.
|
|
11
23
|
*/
|
|
12
|
-
export abstract class AbstractClient {
|
|
13
|
-
protected
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
24
|
+
export abstract class AbstractClient extends EventEmitter {
|
|
25
|
+
protected factory: Function | undefined = undefined;
|
|
26
|
+
protected key: string;
|
|
27
|
+
protected options: DefaultClientOptions;
|
|
28
|
+
public namespace: string = "global";
|
|
29
|
+
public version: string = "v1";
|
|
30
|
+
public baseUrl: string = DEFAULT_URL;
|
|
31
|
+
public logger: Function = noop;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Constructs a new instance of the DeepgramClient class with the provided options.
|
|
35
|
+
*
|
|
36
|
+
* @param options - The options to configure the DeepgramClient instance.
|
|
37
|
+
* @param options.key - The Deepgram API key to use for authentication. If not provided, the `DEEPGRAM_API_KEY` environment variable will be used.
|
|
38
|
+
* @param options.global - Global options that apply to all requests made by the DeepgramClient instance.
|
|
39
|
+
* @param options.global.fetch - Options to configure the fetch requests made by the DeepgramClient instance.
|
|
40
|
+
* @param options.global.fetch.options - Additional options to pass to the fetch function, such as `url` and `headers`.
|
|
41
|
+
* @param options.namespace - Options specific to a particular namespace within the DeepgramClient instance.
|
|
42
|
+
*/
|
|
43
|
+
constructor(options: DeepgramClientOptions) {
|
|
44
|
+
super();
|
|
45
|
+
|
|
46
|
+
let key;
|
|
47
|
+
|
|
48
|
+
if (typeof options.key === "function") {
|
|
49
|
+
this.factory = options.key;
|
|
50
|
+
key = this.factory();
|
|
51
|
+
} else {
|
|
52
|
+
key = options.key;
|
|
53
|
+
}
|
|
17
54
|
|
|
18
55
|
if (!key) {
|
|
19
|
-
|
|
56
|
+
key = process.env.DEEPGRAM_API_KEY as string;
|
|
20
57
|
}
|
|
21
58
|
|
|
22
|
-
if (!
|
|
23
|
-
throw new DeepgramError("A deepgram API key is required");
|
|
59
|
+
if (!key) {
|
|
60
|
+
throw new DeepgramError("A deepgram API key is required.");
|
|
24
61
|
}
|
|
25
62
|
|
|
26
|
-
this.
|
|
27
|
-
|
|
28
|
-
if (!this.options.global?.url) {
|
|
29
|
-
throw new DeepgramError(
|
|
30
|
-
`An API URL is required. It should be set to ${DEFAULT_URL} by default. No idea what happened!`
|
|
31
|
-
);
|
|
32
|
-
}
|
|
63
|
+
this.key = key;
|
|
33
64
|
|
|
34
|
-
|
|
35
|
-
let proxyUrlString: string;
|
|
65
|
+
options = convertLegacyOptions(options);
|
|
36
66
|
|
|
37
67
|
/**
|
|
38
|
-
*
|
|
68
|
+
* Apply default options.
|
|
39
69
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
70
|
+
this.options = applyDefaults<DeepgramClientOptions, DefaultClientOptions>(
|
|
71
|
+
options,
|
|
72
|
+
DEFAULT_OPTIONS
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
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
|
+
*/
|
|
82
|
+
public v(version: string = "v1"): this {
|
|
83
|
+
this.version = version;
|
|
84
|
+
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
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.
|
|
94
|
+
*/
|
|
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
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Generates a URL for an API endpoint with optional query parameters and transcription options.
|
|
109
|
+
*
|
|
110
|
+
* @param endpoint - The API endpoint URL, which may contain placeholders for fields.
|
|
111
|
+
* @param fields - An optional object containing key-value pairs to replace placeholders in the endpoint URL.
|
|
112
|
+
* @param transcriptionOptions - Optional transcription options to include as query parameters in the URL.
|
|
113
|
+
* @returns A URL object representing the constructed API request URL.
|
|
114
|
+
*/
|
|
115
|
+
public getRequestUrl(
|
|
116
|
+
endpoint: string,
|
|
117
|
+
fields: { [key: string]: string } = { version: this.version },
|
|
118
|
+
transcriptionOptions?: {
|
|
119
|
+
[key: string]: unknown;
|
|
44
120
|
}
|
|
121
|
+
): URL {
|
|
122
|
+
/**
|
|
123
|
+
* If we pass in fields without a version, set a version.
|
|
124
|
+
*/
|
|
125
|
+
fields.version = this.version;
|
|
45
126
|
|
|
46
127
|
/**
|
|
47
|
-
*
|
|
128
|
+
* Version and template the endpoint for input argument..
|
|
48
129
|
*/
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
*/
|
|
53
|
-
if (this.key !== "proxy") {
|
|
54
|
-
throw new DeepgramError(
|
|
55
|
-
`Do not attempt to pass any other API key than the string "proxy" when making proxied REST requests. Please ensure your proxy application is responsible for writing our API key to the Authorization header.`
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
proxyUrlString = this.options.restProxy.url;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Check if the proxy URL provided is missing a protocol and warn in the console.
|
|
63
|
-
*/
|
|
64
|
-
if (!proxyUrlString.startsWith("http") && !proxyUrlString.startsWith("ws")) {
|
|
65
|
-
console.warn(
|
|
66
|
-
`The proxy URL provided does not begin with http, https, ws, or wss and will default to https as standard.`
|
|
67
|
-
);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
baseUrlString = proxyUrlString;
|
|
71
|
-
}
|
|
130
|
+
endpoint = endpoint.replace(/:(\w+)/g, function (_, key) {
|
|
131
|
+
return fields![key];
|
|
132
|
+
});
|
|
72
133
|
|
|
73
|
-
|
|
74
|
-
|
|
134
|
+
/**
|
|
135
|
+
* Create a URL object.
|
|
136
|
+
*/
|
|
137
|
+
const url = new URL(endpoint as string, this.baseUrl);
|
|
75
138
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
139
|
+
/**
|
|
140
|
+
* If there are transcription options, append them to the request as URL querystring parameters
|
|
141
|
+
*/
|
|
142
|
+
if (transcriptionOptions) {
|
|
143
|
+
appendSearchParams(url.searchParams, transcriptionOptions);
|
|
79
144
|
}
|
|
80
145
|
|
|
81
|
-
return
|
|
146
|
+
return url;
|
|
82
147
|
}
|
|
83
148
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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);
|
|
88
156
|
}
|
|
89
157
|
}
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import { AbstractClient, noop } from "./AbstractClient";
|
|
2
|
+
import { CONNECTION_STATE, SOCKET_STATES } from "../lib/constants";
|
|
3
|
+
import type { DeepgramClientOptions, LiveSchema } from "../lib/types";
|
|
4
|
+
import type { WebSocket as WSWebSocket } from "ws";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Represents a constructor for a WebSocket-like object that can be used in the application.
|
|
8
|
+
* The constructor takes the following parameters:
|
|
9
|
+
* @param address - The URL or address of the WebSocket server.
|
|
10
|
+
* @param _ignored - An optional parameter that is ignored.
|
|
11
|
+
* @param options - An optional object containing headers to be included in the WebSocket connection.
|
|
12
|
+
* @returns A WebSocket-like object that implements the WebSocketLike interface.
|
|
13
|
+
*/
|
|
14
|
+
interface WebSocketLikeConstructor {
|
|
15
|
+
new (
|
|
16
|
+
address: string | URL,
|
|
17
|
+
_ignored?: any,
|
|
18
|
+
options?: { headers: Object | undefined }
|
|
19
|
+
): WebSocketLike;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Represents the types of WebSocket-like connections that can be used in the application.
|
|
24
|
+
* This type is used to provide a common interface for different WebSocket implementations,
|
|
25
|
+
* such as the native WebSocket API, a WebSocket wrapper library, or a dummy implementation
|
|
26
|
+
* for testing purposes.
|
|
27
|
+
*/
|
|
28
|
+
type WebSocketLike = WebSocket | WSWebSocket | WSWebSocketDummy;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Represents the types of data that can be sent or received over a WebSocket-like connection.
|
|
32
|
+
*/
|
|
33
|
+
type SocketDataLike = string | ArrayBufferLike | Blob;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Represents an error that occurred in a WebSocket-like connection.
|
|
37
|
+
* @property {any} error - The underlying error object.
|
|
38
|
+
* @property {string} message - A human-readable error message.
|
|
39
|
+
* @property {string} type - The type of the error.
|
|
40
|
+
*/
|
|
41
|
+
interface WebSocketLikeError {
|
|
42
|
+
error: any;
|
|
43
|
+
message: string;
|
|
44
|
+
type: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Indicates whether a native WebSocket implementation is available in the current environment.
|
|
49
|
+
*/
|
|
50
|
+
const NATIVE_WEBSOCKET_AVAILABLE = typeof WebSocket !== "undefined";
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Represents an abstract live client that extends the AbstractClient class.
|
|
54
|
+
* The AbstractLiveClient class provides functionality for connecting, reconnecting, and disconnecting a WebSocket connection, as well as sending data over the connection.
|
|
55
|
+
* Subclasses of this class are responsible for setting up the connection event handlers.
|
|
56
|
+
*
|
|
57
|
+
* @abstract
|
|
58
|
+
*/
|
|
59
|
+
export abstract class AbstractLiveClient extends AbstractClient {
|
|
60
|
+
public headers: { [key: string]: string };
|
|
61
|
+
public transport: WebSocketLikeConstructor | null;
|
|
62
|
+
public conn: WebSocketLike | null = null;
|
|
63
|
+
public sendBuffer: Function[] = [];
|
|
64
|
+
|
|
65
|
+
constructor(options: DeepgramClientOptions) {
|
|
66
|
+
super(options);
|
|
67
|
+
|
|
68
|
+
const {
|
|
69
|
+
key,
|
|
70
|
+
websocket: { options: websocketOptions, client },
|
|
71
|
+
} = this.namespaceOptions;
|
|
72
|
+
|
|
73
|
+
if (this.proxy) {
|
|
74
|
+
this.baseUrl = websocketOptions.proxy!.url;
|
|
75
|
+
} else {
|
|
76
|
+
this.baseUrl = websocketOptions.url;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (client) {
|
|
80
|
+
this.transport = client;
|
|
81
|
+
} else {
|
|
82
|
+
this.transport = null;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (websocketOptions._nodeOnlyHeaders) {
|
|
86
|
+
this.headers = websocketOptions._nodeOnlyHeaders;
|
|
87
|
+
} else {
|
|
88
|
+
this.headers = {};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (!("Authorization" in this.headers)) {
|
|
92
|
+
this.headers["Authorization"] = `Token ${key}`; // Add default token
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Connects the socket, unless already connected.
|
|
98
|
+
*
|
|
99
|
+
* @protected Can only be called from within the class.
|
|
100
|
+
*/
|
|
101
|
+
protected connect(transcriptionOptions: LiveSchema, endpoint: string): void {
|
|
102
|
+
if (this.conn) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
this.reconnect = (options = transcriptionOptions) => {
|
|
107
|
+
this.connect(options, endpoint);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const requestUrl = this.getRequestUrl(endpoint, {}, transcriptionOptions);
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Custom websocket transport
|
|
114
|
+
*/
|
|
115
|
+
if (this.transport) {
|
|
116
|
+
this.conn = new this.transport(requestUrl, undefined, {
|
|
117
|
+
headers: this.headers,
|
|
118
|
+
});
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Native websocket transport (browser)
|
|
124
|
+
*/
|
|
125
|
+
if (NATIVE_WEBSOCKET_AVAILABLE) {
|
|
126
|
+
this.conn = new WebSocket(requestUrl, ["token", this.namespaceOptions.key]);
|
|
127
|
+
this.setupConnection();
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Dummy websocket
|
|
133
|
+
*/
|
|
134
|
+
this.conn = new WSWebSocketDummy(requestUrl, undefined, {
|
|
135
|
+
close: () => {
|
|
136
|
+
this.conn = null;
|
|
137
|
+
},
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* WS package for node environment
|
|
142
|
+
*/
|
|
143
|
+
import("ws").then(({ default: WS }) => {
|
|
144
|
+
this.conn = new WS(requestUrl, undefined, {
|
|
145
|
+
headers: this.headers,
|
|
146
|
+
});
|
|
147
|
+
this.setupConnection();
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Reconnects the socket using new or existing transcription options.
|
|
153
|
+
*
|
|
154
|
+
* @param options - The transcription options to use when reconnecting the socket.
|
|
155
|
+
*/
|
|
156
|
+
public reconnect: (options: LiveSchema) => void = noop;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Disconnects the socket from the client.
|
|
160
|
+
*
|
|
161
|
+
* @param code A numeric status code to send on disconnect.
|
|
162
|
+
* @param reason A custom reason for the disconnect.
|
|
163
|
+
*/
|
|
164
|
+
public disconnect(code?: number, reason?: string): void {
|
|
165
|
+
if (this.conn) {
|
|
166
|
+
this.conn.onclose = function () {}; // noop
|
|
167
|
+
if (code) {
|
|
168
|
+
this.conn.close(code, reason ?? "");
|
|
169
|
+
} else {
|
|
170
|
+
this.conn.close();
|
|
171
|
+
}
|
|
172
|
+
this.conn = null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Returns the current connection state of the WebSocket connection.
|
|
178
|
+
*
|
|
179
|
+
* @returns The current connection state of the WebSocket connection.
|
|
180
|
+
*/
|
|
181
|
+
public connectionState(): CONNECTION_STATE {
|
|
182
|
+
switch (this.conn && this.conn.readyState) {
|
|
183
|
+
case SOCKET_STATES.connecting:
|
|
184
|
+
return CONNECTION_STATE.Connecting;
|
|
185
|
+
case SOCKET_STATES.open:
|
|
186
|
+
return CONNECTION_STATE.Open;
|
|
187
|
+
case SOCKET_STATES.closing:
|
|
188
|
+
return CONNECTION_STATE.Closing;
|
|
189
|
+
default:
|
|
190
|
+
return CONNECTION_STATE.Closed;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Returns the current ready state of the WebSocket connection.
|
|
196
|
+
*
|
|
197
|
+
* @returns The current ready state of the WebSocket connection.
|
|
198
|
+
*/
|
|
199
|
+
public getReadyState(): SOCKET_STATES {
|
|
200
|
+
return this.conn?.readyState ?? SOCKET_STATES.closed;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Returns `true` is the connection is open.
|
|
205
|
+
*/
|
|
206
|
+
public isConnected(): boolean {
|
|
207
|
+
return this.connectionState() === CONNECTION_STATE.Open;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Sends data to the Deepgram API via websocket connection
|
|
212
|
+
* @param data Audio data to send to Deepgram
|
|
213
|
+
*
|
|
214
|
+
* Conforms to RFC #146 for Node.js - does not send an empty byte.
|
|
215
|
+
* @see https://github.com/deepgram/deepgram-python-sdk/issues/146
|
|
216
|
+
*/
|
|
217
|
+
send(data: SocketDataLike): void {
|
|
218
|
+
const callback = async () => {
|
|
219
|
+
if (data instanceof Blob) {
|
|
220
|
+
if (data.size === 0) {
|
|
221
|
+
this.log("warn", "skipping `send` for zero-byte blob", data);
|
|
222
|
+
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
data = await data.arrayBuffer();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (typeof data !== "string") {
|
|
230
|
+
if (data.byteLength === 0) {
|
|
231
|
+
this.log("warn", "skipping `send` for zero-byte blob", data);
|
|
232
|
+
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this.conn?.send(data);
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
if (this.isConnected()) {
|
|
241
|
+
callback();
|
|
242
|
+
} else {
|
|
243
|
+
this.sendBuffer.push(callback);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Determines whether the current instance should proxy requests.
|
|
249
|
+
* @returns {boolean} true if the current instance should proxy requests; otherwise, false
|
|
250
|
+
*/
|
|
251
|
+
get proxy(): boolean {
|
|
252
|
+
return this.key === "proxy" && !!this.namespaceOptions.websocket.options.proxy?.url;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Sets up the connection event handlers.
|
|
257
|
+
*
|
|
258
|
+
* @abstract Requires subclasses to set up context aware event handlers.
|
|
259
|
+
*/
|
|
260
|
+
abstract setupConnection(): void;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
class WSWebSocketDummy {
|
|
264
|
+
binaryType: string = "arraybuffer";
|
|
265
|
+
close: Function;
|
|
266
|
+
onclose: Function = () => {};
|
|
267
|
+
onerror: Function = () => {};
|
|
268
|
+
onmessage: Function = () => {};
|
|
269
|
+
onopen: Function = () => {};
|
|
270
|
+
readyState: number = SOCKET_STATES.connecting;
|
|
271
|
+
send: Function = () => {};
|
|
272
|
+
url: string | URL | null = null;
|
|
273
|
+
|
|
274
|
+
constructor(address: URL, _protocols: undefined, options: { close: Function }) {
|
|
275
|
+
this.url = address.toString();
|
|
276
|
+
this.close = options.close;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
export { AbstractLiveClient as AbstractWsClient };
|