@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,160 +0,0 @@
|
|
|
1
|
-
import { DeepgramApiError, DeepgramError, DeepgramUnknownError } from "../lib/errors";
|
|
2
|
-
import { Readable } from "stream";
|
|
3
|
-
import { fetchWithAuth, resolveResponse } from "../lib/fetch";
|
|
4
|
-
import type { Fetch, FetchParameters, RequestMethodType } from "../lib/types/Fetch";
|
|
5
|
-
import { AbstractClient } from "./AbstractClient";
|
|
6
|
-
import { DeepgramClientOptions } from "../lib/types";
|
|
7
|
-
import { isBrowser } from "../lib/helpers";
|
|
8
|
-
|
|
9
|
-
export abstract class AbstractRestfulClient extends AbstractClient {
|
|
10
|
-
protected fetch: Fetch;
|
|
11
|
-
|
|
12
|
-
constructor(protected key: string, protected options: DeepgramClientOptions) {
|
|
13
|
-
super(key, options);
|
|
14
|
-
|
|
15
|
-
if (isBrowser() && !this._willProxy()) {
|
|
16
|
-
throw new DeepgramError(
|
|
17
|
-
"Due to CORS we are unable to support REST-based API calls to our API from the browser. Please consider using a proxy, and including a `restProxy: { url: ''}` in your Deepgram client options."
|
|
18
|
-
);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
this.fetch = fetchWithAuth(this.key, options._experimentalCustomFetch);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
protected _getErrorMessage(err: any): string {
|
|
25
|
-
return err.msg || err.message || err.error_description || err.error || JSON.stringify(err);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
protected async handleError(error: unknown, reject: (reason?: any) => void) {
|
|
29
|
-
const Res = await resolveResponse();
|
|
30
|
-
|
|
31
|
-
if (error instanceof Res) {
|
|
32
|
-
error
|
|
33
|
-
.json()
|
|
34
|
-
.then((err) => {
|
|
35
|
-
reject(new DeepgramApiError(this._getErrorMessage(err), error.status || 500));
|
|
36
|
-
})
|
|
37
|
-
.catch((err) => {
|
|
38
|
-
reject(new DeepgramUnknownError(this._getErrorMessage(err), err));
|
|
39
|
-
});
|
|
40
|
-
} else {
|
|
41
|
-
reject(new DeepgramUnknownError(this._getErrorMessage(error), error));
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
protected _getRequestParams(
|
|
46
|
-
method: RequestMethodType,
|
|
47
|
-
headers?: Record<string, string>,
|
|
48
|
-
parameters?: FetchParameters,
|
|
49
|
-
body?: string | Buffer | Readable
|
|
50
|
-
) {
|
|
51
|
-
const params: { [k: string]: any } = {
|
|
52
|
-
...this.options?.fetch,
|
|
53
|
-
method,
|
|
54
|
-
headers: { ...this.options?.fetch?.headers, ...headers } || {},
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
if (method === "GET") {
|
|
58
|
-
return params;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
params.body = body;
|
|
62
|
-
params.duplex = "half";
|
|
63
|
-
|
|
64
|
-
return { ...params, ...parameters };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
protected async _handleRequest(
|
|
68
|
-
fetcher: Fetch,
|
|
69
|
-
method: RequestMethodType,
|
|
70
|
-
url: string | URL,
|
|
71
|
-
headers?: Record<string, string>,
|
|
72
|
-
parameters?: FetchParameters,
|
|
73
|
-
body?: string | Buffer | Readable
|
|
74
|
-
): Promise<any> {
|
|
75
|
-
return new Promise((resolve, reject) => {
|
|
76
|
-
fetcher(url, this._getRequestParams(method, headers, parameters, body))
|
|
77
|
-
.then((result) => {
|
|
78
|
-
if (!result.ok) throw result;
|
|
79
|
-
|
|
80
|
-
return result.json();
|
|
81
|
-
})
|
|
82
|
-
.then((data) => resolve(data))
|
|
83
|
-
.catch((error) => this.handleError(error, reject));
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
protected async _handleRawRequest(
|
|
88
|
-
fetcher: Fetch,
|
|
89
|
-
method: RequestMethodType,
|
|
90
|
-
url: string | URL,
|
|
91
|
-
headers?: Record<string, string>,
|
|
92
|
-
parameters?: FetchParameters,
|
|
93
|
-
body?: string | Buffer | Readable
|
|
94
|
-
): Promise<any> {
|
|
95
|
-
return new Promise((resolve, reject) => {
|
|
96
|
-
fetcher(url, this._getRequestParams(method, headers, parameters, body))
|
|
97
|
-
.then((result) => {
|
|
98
|
-
if (!result.ok) throw result;
|
|
99
|
-
|
|
100
|
-
return result;
|
|
101
|
-
})
|
|
102
|
-
.then((data) => resolve(data))
|
|
103
|
-
.catch((error) => this.handleError(error, reject));
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
protected async get(
|
|
108
|
-
fetcher: Fetch,
|
|
109
|
-
url: string | URL,
|
|
110
|
-
headers?: Record<string, string>,
|
|
111
|
-
parameters?: FetchParameters
|
|
112
|
-
): Promise<any> {
|
|
113
|
-
return this._handleRequest(fetcher, "GET", url, headers, parameters);
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
protected async post(
|
|
117
|
-
fetcher: Fetch,
|
|
118
|
-
url: string | URL,
|
|
119
|
-
body: string | Buffer | Readable,
|
|
120
|
-
headers?: Record<string, string>,
|
|
121
|
-
parameters?: FetchParameters
|
|
122
|
-
): Promise<any> {
|
|
123
|
-
return this._handleRequest(fetcher, "POST", url, headers, parameters, body);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
protected async put(
|
|
127
|
-
fetcher: Fetch,
|
|
128
|
-
url: string | URL,
|
|
129
|
-
body: string | Buffer | Readable,
|
|
130
|
-
headers?: Record<string, string>,
|
|
131
|
-
parameters?: FetchParameters
|
|
132
|
-
): Promise<any> {
|
|
133
|
-
return this._handleRequest(fetcher, "PUT", url, headers, parameters, body);
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
protected async patch(
|
|
137
|
-
fetcher: Fetch,
|
|
138
|
-
url: string | URL,
|
|
139
|
-
body: string | Buffer | Readable,
|
|
140
|
-
headers?: Record<string, string>,
|
|
141
|
-
parameters?: FetchParameters
|
|
142
|
-
): Promise<any> {
|
|
143
|
-
return this._handleRequest(fetcher, "PATCH", url, headers, parameters, body);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
protected async delete(
|
|
147
|
-
fetcher: Fetch,
|
|
148
|
-
url: string | URL,
|
|
149
|
-
headers?: Record<string, string>,
|
|
150
|
-
parameters?: FetchParameters
|
|
151
|
-
): Promise<any> {
|
|
152
|
-
return this._handleRequest(fetcher, "DELETE", url, headers, parameters);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
private _willProxy() {
|
|
156
|
-
const proxyUrl = this.options.restProxy?.url;
|
|
157
|
-
|
|
158
|
-
return !!proxyUrl;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { EventEmitter } from "events";
|
|
2
|
-
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
|
|
3
|
-
import { applySettingDefaults, stripTrailingSlash } from "../lib/helpers";
|
|
4
|
-
import { DeepgramClientOptions } from "../lib/types";
|
|
5
|
-
|
|
6
|
-
export abstract class AbstractWsClient extends EventEmitter {
|
|
7
|
-
protected baseUrl: URL;
|
|
8
|
-
|
|
9
|
-
constructor(
|
|
10
|
-
protected key: string,
|
|
11
|
-
protected options: DeepgramClientOptions | undefined = DEFAULT_OPTIONS
|
|
12
|
-
) {
|
|
13
|
-
super();
|
|
14
|
-
|
|
15
|
-
this.key = key;
|
|
16
|
-
|
|
17
|
-
if (!key) {
|
|
18
|
-
this.key = process.env.DEEPGRAM_API_KEY as string;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
if (!this.key) {
|
|
22
|
-
throw new Error("A deepgram API key is required");
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
this.options = applySettingDefaults(options, DEFAULT_OPTIONS);
|
|
26
|
-
|
|
27
|
-
if (!this.options.global?.url) {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`An API URL is required. It should be set to ${DEFAULT_URL} by default. No idea what happened!`
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let url = this.options.global.url;
|
|
34
|
-
|
|
35
|
-
if (!/^https?:\/\//i.test(url)) {
|
|
36
|
-
url = "https://" + url;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
this.baseUrl = new URL(stripTrailingSlash(url));
|
|
40
|
-
this.baseUrl.protocol = this.baseUrl.protocol.toLowerCase().replace(/(http)(s)?/gi, "ws$2");
|
|
41
|
-
}
|
|
42
|
-
}
|
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
import { AbstractWsClient } from "./AbstractWsClient";
|
|
2
|
-
import { appendSearchParams } from "../lib/helpers";
|
|
3
|
-
import { DeepgramError } from "../lib/errors";
|
|
4
|
-
import { DEFAULT_OPTIONS } from "../lib/constants";
|
|
5
|
-
import { LiveConnectionState, LiveTranscriptionEvents } from "../lib/enums";
|
|
6
|
-
import { w3cwebsocket } from "websocket";
|
|
7
|
-
|
|
8
|
-
import type {
|
|
9
|
-
LiveSchema,
|
|
10
|
-
LiveConfigOptions,
|
|
11
|
-
LiveMetadataEvent,
|
|
12
|
-
LiveTranscriptionEvent,
|
|
13
|
-
DeepgramClientOptions,
|
|
14
|
-
UtteranceEndEvent,
|
|
15
|
-
SpeechStartedEvent,
|
|
16
|
-
} from "../lib/types";
|
|
17
|
-
|
|
18
|
-
export class LiveClient extends AbstractWsClient {
|
|
19
|
-
private _socket: w3cwebsocket;
|
|
20
|
-
|
|
21
|
-
constructor(
|
|
22
|
-
protected key: string,
|
|
23
|
-
protected options: DeepgramClientOptions | undefined = DEFAULT_OPTIONS,
|
|
24
|
-
private transcriptionOptions: LiveSchema = {},
|
|
25
|
-
endpoint = "v1/listen"
|
|
26
|
-
) {
|
|
27
|
-
super(key, options);
|
|
28
|
-
|
|
29
|
-
const url = new URL(endpoint, this.baseUrl);
|
|
30
|
-
url.protocol = url.protocol.toLowerCase().replace(/(http)(s)?/gi, "ws$2");
|
|
31
|
-
appendSearchParams(url.searchParams, this.transcriptionOptions);
|
|
32
|
-
|
|
33
|
-
this._socket = new w3cwebsocket(url.toString(), ["token", this.key]);
|
|
34
|
-
|
|
35
|
-
this._socket.onopen = () => {
|
|
36
|
-
this.emit(LiveTranscriptionEvents.Open, this);
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
this._socket.onclose = (event: any) => {
|
|
40
|
-
this.emit(LiveTranscriptionEvents.Close, event);
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
this._socket.onerror = (event) => {
|
|
44
|
-
this.emit(LiveTranscriptionEvents.Error, event);
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
this._socket.onmessage = (event) => {
|
|
48
|
-
try {
|
|
49
|
-
const data: any = JSON.parse(event.data.toString());
|
|
50
|
-
|
|
51
|
-
if (data.type === LiveTranscriptionEvents.Metadata) {
|
|
52
|
-
this.emit(LiveTranscriptionEvents.Metadata, data as LiveMetadataEvent);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
if (data.type === LiveTranscriptionEvents.Transcript) {
|
|
56
|
-
this.emit(LiveTranscriptionEvents.Transcript, data as LiveTranscriptionEvent);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (data.type === LiveTranscriptionEvents.UtteranceEnd) {
|
|
60
|
-
this.emit(LiveTranscriptionEvents.UtteranceEnd, data as UtteranceEndEvent);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
if (data.type === LiveTranscriptionEvents.SpeechStarted) {
|
|
64
|
-
this.emit(LiveTranscriptionEvents.SpeechStarted, data as SpeechStartedEvent);
|
|
65
|
-
}
|
|
66
|
-
} catch (error) {
|
|
67
|
-
this.emit(LiveTranscriptionEvents.Error, {
|
|
68
|
-
event,
|
|
69
|
-
message: "Unable to parse `data` as JSON.",
|
|
70
|
-
error,
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
public configure(config: LiveConfigOptions): void {
|
|
77
|
-
this._socket.send(
|
|
78
|
-
JSON.stringify({
|
|
79
|
-
type: "Configure",
|
|
80
|
-
processors: config,
|
|
81
|
-
})
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public keepAlive(): void {
|
|
86
|
-
this._socket.send(
|
|
87
|
-
JSON.stringify({
|
|
88
|
-
type: "KeepAlive",
|
|
89
|
-
})
|
|
90
|
-
);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
public getReadyState(): LiveConnectionState {
|
|
94
|
-
return this._socket.readyState;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Sends data to the Deepgram API via websocket connection
|
|
99
|
-
* @param data Audio data to send to Deepgram
|
|
100
|
-
*
|
|
101
|
-
* Conforms to RFC #146 for Node.js - does not send an empty byte.
|
|
102
|
-
* In the browser, a Blob will contain length with no audio.
|
|
103
|
-
* @see https://github.com/deepgram/deepgram-python-sdk/issues/146
|
|
104
|
-
*/
|
|
105
|
-
public send(data: string | ArrayBufferLike | Blob): void {
|
|
106
|
-
if (this._socket.readyState === LiveConnectionState.OPEN) {
|
|
107
|
-
if (typeof data === "string") {
|
|
108
|
-
this._socket.send(data); // send text data
|
|
109
|
-
} else if ((data as any) instanceof Blob) {
|
|
110
|
-
this._socket.send(data as unknown as ArrayBufferLike); // send blob data
|
|
111
|
-
} else {
|
|
112
|
-
const buffer = data as ArrayBufferLike;
|
|
113
|
-
|
|
114
|
-
if (buffer.byteLength > 0) {
|
|
115
|
-
this._socket.send(buffer); // send buffer when not zero-byte (or browser)
|
|
116
|
-
} else {
|
|
117
|
-
this.emit(
|
|
118
|
-
LiveTranscriptionEvents.Warning,
|
|
119
|
-
"Zero-byte detected, skipping. Send `CloseStream` if trying to close the connection."
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
} else {
|
|
124
|
-
throw new DeepgramError("Could not send. Connection not open.");
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Denote that you are finished sending audio and close
|
|
130
|
-
* the websocket connection when transcription is finished
|
|
131
|
-
*/
|
|
132
|
-
public finish(): void {
|
|
133
|
-
// tell the server to close the socket
|
|
134
|
-
this._socket.send(
|
|
135
|
-
JSON.stringify({
|
|
136
|
-
type: "CloseStream",
|
|
137
|
-
})
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
}
|