@deepgram/sdk 3.0.0-alpha.9 → 3.0.0-beta.1
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 +240 -99
- package/dist/main/DeepgramClient.d.ts +2 -7
- package/dist/main/DeepgramClient.d.ts.map +1 -1
- package/dist/main/DeepgramClient.js +5 -28
- package/dist/main/DeepgramClient.js.map +1 -1
- package/dist/main/index.d.ts +5 -0
- package/dist/main/index.d.ts.map +1 -1
- package/dist/main/index.js +9 -2
- package/dist/main/index.js.map +1 -1
- package/dist/main/lib/constants.d.ts +4 -8
- package/dist/main/lib/constants.d.ts.map +1 -1
- package/dist/main/lib/constants.js +5 -1
- package/dist/main/lib/constants.js.map +1 -1
- package/dist/main/lib/helpers.d.ts.map +1 -1
- package/dist/main/lib/helpers.js +5 -5
- package/dist/main/lib/helpers.js.map +1 -1
- package/dist/main/lib/types/DeepgramClientOptions.d.ts +6 -1
- package/dist/main/lib/types/DeepgramClientOptions.d.ts.map +1 -1
- package/dist/main/lib/types/Fetch.d.ts +6 -4
- package/dist/main/lib/types/Fetch.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectBalancesResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectBalancesResponse.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectInvitesResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectInvitesResponse.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectKeysResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectKeysResponse.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectMembersResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectMembersResponse.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectUsageFieldsResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectUsageFieldsResponse.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectUsageRequestsResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectUsageRequestsResponse.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectUsageSummaryResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectUsageSummaryResponse.d.ts.map +1 -1
- package/dist/main/lib/types/GetProjectsResponse.d.ts +1 -1
- package/dist/main/lib/types/GetProjectsResponse.d.ts.map +1 -1
- package/dist/main/lib/types/ListOnPremCredentialsResponse.d.ts +1 -1
- package/dist/main/lib/types/ListOnPremCredentialsResponse.d.ts.map +1 -1
- package/dist/main/lib/types/LiveTranscriptionEvent.d.ts +11 -15
- package/dist/main/lib/types/LiveTranscriptionEvent.d.ts.map +1 -1
- package/dist/main/lib/types/SyncPrerecordedResponse.d.ts +16 -16
- package/dist/main/lib/types/SyncPrerecordedResponse.d.ts.map +1 -1
- package/dist/main/lib/types/TranscriptionSchema.d.ts +2 -2
- package/dist/main/lib/types/TranscriptionSchema.d.ts.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 +16 -0
- package/dist/main/packages/AbstractClient.d.ts.map +1 -0
- package/dist/main/packages/AbstractClient.js +51 -0
- package/dist/main/packages/AbstractClient.js.map +1 -0
- package/dist/main/packages/AbstractRestfulClient.d.ts +14 -12
- package/dist/main/packages/AbstractRestfulClient.d.ts.map +1 -1
- package/dist/main/packages/AbstractRestfulClient.js +28 -23
- package/dist/main/packages/AbstractRestfulClient.js.map +1 -1
- package/dist/main/packages/AbstractWsClient.d.ts +10 -0
- package/dist/main/packages/AbstractWsClient.d.ts.map +1 -0
- package/dist/main/packages/AbstractWsClient.js +33 -0
- package/dist/main/packages/AbstractWsClient.js.map +1 -0
- package/dist/main/packages/ListenClient.d.ts +4 -7
- package/dist/main/packages/ListenClient.d.ts.map +1 -1
- package/dist/main/packages/ListenClient.js +6 -10
- package/dist/main/packages/ListenClient.js.map +1 -1
- package/dist/main/packages/LiveClient.d.ts +10 -7
- package/dist/main/packages/LiveClient.d.ts.map +1 -1
- package/dist/main/packages/LiveClient.js +24 -15
- package/dist/main/packages/LiveClient.js.map +1 -1
- package/dist/main/packages/ManageClient.d.ts.map +1 -1
- package/dist/main/packages/ManageClient.js +22 -66
- package/dist/main/packages/ManageClient.js.map +1 -1
- package/dist/main/packages/OnPremClient.d.ts.map +1 -1
- package/dist/main/packages/OnPremClient.js +4 -12
- package/dist/main/packages/OnPremClient.js.map +1 -1
- package/dist/main/packages/PrerecordedClient.d.ts.map +1 -1
- package/dist/main/packages/PrerecordedClient.js +4 -12
- package/dist/main/packages/PrerecordedClient.js.map +1 -1
- package/dist/main/packages/index.d.ts +9 -0
- package/dist/main/packages/index.d.ts.map +1 -0
- package/dist/main/packages/index.js +20 -0
- package/dist/main/packages/index.js.map +1 -0
- package/dist/module/DeepgramClient.d.ts +2 -7
- package/dist/module/DeepgramClient.d.ts.map +1 -1
- package/dist/module/DeepgramClient.js +5 -28
- package/dist/module/DeepgramClient.js.map +1 -1
- package/dist/module/index.d.ts +5 -0
- package/dist/module/index.d.ts.map +1 -1
- package/dist/module/index.js +6 -1
- package/dist/module/index.js.map +1 -1
- package/dist/module/lib/constants.d.ts +4 -8
- package/dist/module/lib/constants.d.ts.map +1 -1
- package/dist/module/lib/constants.js +4 -0
- package/dist/module/lib/constants.js.map +1 -1
- package/dist/module/lib/helpers.d.ts.map +1 -1
- package/dist/module/lib/helpers.js +2 -5
- package/dist/module/lib/helpers.js.map +1 -1
- package/dist/module/lib/types/DeepgramClientOptions.d.ts +6 -1
- package/dist/module/lib/types/DeepgramClientOptions.d.ts.map +1 -1
- package/dist/module/lib/types/Fetch.d.ts +6 -4
- package/dist/module/lib/types/Fetch.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectBalancesResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectBalancesResponse.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectInvitesResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectInvitesResponse.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectKeysResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectKeysResponse.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectMembersResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectMembersResponse.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectUsageFieldsResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectUsageFieldsResponse.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectUsageRequestsResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectUsageRequestsResponse.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectUsageSummaryResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectUsageSummaryResponse.d.ts.map +1 -1
- package/dist/module/lib/types/GetProjectsResponse.d.ts +1 -1
- package/dist/module/lib/types/GetProjectsResponse.d.ts.map +1 -1
- package/dist/module/lib/types/ListOnPremCredentialsResponse.d.ts +1 -1
- package/dist/module/lib/types/ListOnPremCredentialsResponse.d.ts.map +1 -1
- package/dist/module/lib/types/LiveTranscriptionEvent.d.ts +11 -15
- package/dist/module/lib/types/LiveTranscriptionEvent.d.ts.map +1 -1
- package/dist/module/lib/types/SyncPrerecordedResponse.d.ts +16 -16
- package/dist/module/lib/types/SyncPrerecordedResponse.d.ts.map +1 -1
- package/dist/module/lib/types/TranscriptionSchema.d.ts +2 -2
- package/dist/module/lib/types/TranscriptionSchema.d.ts.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 +16 -0
- package/dist/module/packages/AbstractClient.d.ts.map +1 -0
- package/dist/module/packages/AbstractClient.js +47 -0
- package/dist/module/packages/AbstractClient.js.map +1 -0
- package/dist/module/packages/AbstractRestfulClient.d.ts +14 -12
- package/dist/module/packages/AbstractRestfulClient.d.ts.map +1 -1
- package/dist/module/packages/AbstractRestfulClient.js +29 -24
- package/dist/module/packages/AbstractRestfulClient.js.map +1 -1
- package/dist/module/packages/AbstractWsClient.d.ts +10 -0
- package/dist/module/packages/AbstractWsClient.d.ts.map +1 -0
- package/dist/module/packages/AbstractWsClient.js +29 -0
- package/dist/module/packages/AbstractWsClient.js.map +1 -0
- package/dist/module/packages/ListenClient.d.ts +4 -7
- package/dist/module/packages/ListenClient.d.ts.map +1 -1
- package/dist/module/packages/ListenClient.js +6 -10
- package/dist/module/packages/ListenClient.js.map +1 -1
- package/dist/module/packages/LiveClient.d.ts +10 -7
- package/dist/module/packages/LiveClient.d.ts.map +1 -1
- package/dist/module/packages/LiveClient.js +24 -15
- package/dist/module/packages/LiveClient.js.map +1 -1
- package/dist/module/packages/ManageClient.d.ts.map +1 -1
- package/dist/module/packages/ManageClient.js +22 -66
- package/dist/module/packages/ManageClient.js.map +1 -1
- package/dist/module/packages/OnPremClient.d.ts.map +1 -1
- package/dist/module/packages/OnPremClient.js +4 -12
- package/dist/module/packages/OnPremClient.js.map +1 -1
- package/dist/module/packages/PrerecordedClient.d.ts.map +1 -1
- package/dist/module/packages/PrerecordedClient.js +4 -12
- package/dist/module/packages/PrerecordedClient.js.map +1 -1
- package/dist/module/packages/index.d.ts +9 -0
- package/dist/module/packages/index.d.ts.map +1 -0
- package/dist/module/packages/index.js +9 -0
- package/dist/module/packages/index.js.map +1 -0
- package/dist/umd/deepgram.js +1 -1
- package/package.json +7 -1
- package/src/DeepgramClient.ts +5 -43
- package/src/index.ts +7 -1
- package/src/lib/constants.ts +6 -0
- package/src/lib/helpers.ts +2 -10
- package/src/lib/types/DeepgramClientOptions.ts +7 -1
- package/src/lib/types/Fetch.ts +15 -5
- package/src/lib/types/GetProjectBalancesResponse.ts +1 -1
- package/src/lib/types/GetProjectInvitesResponse.ts +1 -1
- package/src/lib/types/GetProjectKeysResponse.ts +1 -1
- package/src/lib/types/GetProjectMembersResponse.ts +1 -1
- package/src/lib/types/GetProjectUsageFieldsResponse.ts +1 -1
- package/src/lib/types/GetProjectUsageRequestsResponse.ts +1 -1
- package/src/lib/types/GetProjectUsageSummaryResponse.ts +1 -1
- package/src/lib/types/GetProjectsResponse.ts +1 -1
- package/src/lib/types/ListOnPremCredentialsResponse.ts +1 -1
- package/src/lib/types/LiveTranscriptionEvent.ts +11 -15
- package/src/lib/types/SyncPrerecordedResponse.ts +16 -16
- package/src/lib/types/TranscriptionSchema.ts +2 -2
- package/src/lib/version.ts +1 -1
- package/src/packages/AbstractClient.ts +57 -0
- package/src/packages/AbstractRestfulClient.ts +35 -26
- package/src/packages/AbstractWsClient.ts +42 -0
- package/src/packages/ListenClient.ts +6 -15
- package/src/packages/LiveClient.ts +29 -17
- package/src/packages/ManageClient.ts +22 -66
- package/src/packages/OnPremClient.ts +4 -12
- package/src/packages/PrerecordedClient.ts +4 -14
- package/src/packages/index.ts +8 -0
package/src/lib/constants.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { isBrowser } from "./helpers";
|
|
2
|
+
import { FetchOptions } from "./types/Fetch";
|
|
2
3
|
import { version } from "./version";
|
|
3
4
|
|
|
4
5
|
export const DEFAULT_HEADERS = {
|
|
6
|
+
"Content-Type": `application/json`,
|
|
5
7
|
"X-Client-Info": `@deepgram/sdk; ${isBrowser() ? "browser" : "server"}; v${version}`,
|
|
6
8
|
"User-Agent": `@deepgram/sdk/${version}`,
|
|
7
9
|
};
|
|
@@ -10,9 +12,13 @@ export const DEFAULT_URL = "api.deepgram.com";
|
|
|
10
12
|
|
|
11
13
|
export const DEFAULT_GLOBAL_OPTIONS = {
|
|
12
14
|
url: DEFAULT_URL,
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const DEFAULT_FETCH_OPTIONS: FetchOptions = {
|
|
13
18
|
headers: DEFAULT_HEADERS,
|
|
14
19
|
};
|
|
15
20
|
|
|
16
21
|
export const DEFAULT_OPTIONS = {
|
|
17
22
|
global: DEFAULT_GLOBAL_OPTIONS,
|
|
23
|
+
fetch: DEFAULT_FETCH_OPTIONS,
|
|
18
24
|
};
|
package/src/lib/helpers.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Headers as CrossFetchHeaders } from "cross-fetch";
|
|
2
2
|
import { DeepgramClientOptions, FileSource, PrerecordedSource, UrlSource } from "./types";
|
|
3
3
|
import { Readable } from "stream";
|
|
4
|
+
import merge from "deepmerge";
|
|
4
5
|
|
|
5
6
|
export function stripTrailingSlash(url: string): string {
|
|
6
7
|
return url.replace(/\/$/, "");
|
|
@@ -12,16 +13,7 @@ export function applySettingDefaults(
|
|
|
12
13
|
options: DeepgramClientOptions,
|
|
13
14
|
defaults: DeepgramClientOptions
|
|
14
15
|
): DeepgramClientOptions {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const { global: DEFAULT_GLOBAL_OPTIONS } = defaults;
|
|
18
|
-
|
|
19
|
-
return {
|
|
20
|
-
global: {
|
|
21
|
-
...DEFAULT_GLOBAL_OPTIONS,
|
|
22
|
-
...globalOptions,
|
|
23
|
-
},
|
|
24
|
-
};
|
|
16
|
+
return merge(defaults, options);
|
|
25
17
|
}
|
|
26
18
|
|
|
27
19
|
export function appendSearchParams(
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { FetchOptions } from "./Fetch";
|
|
2
|
+
|
|
1
3
|
export interface DeepgramClientOptions {
|
|
2
|
-
global
|
|
4
|
+
global?: {
|
|
3
5
|
/**
|
|
4
6
|
* Optional headers for initializing the client.
|
|
5
7
|
*/
|
|
@@ -10,4 +12,8 @@ export interface DeepgramClientOptions {
|
|
|
10
12
|
*/
|
|
11
13
|
url?: string;
|
|
12
14
|
};
|
|
15
|
+
fetch?: FetchOptions;
|
|
16
|
+
restProxy?: {
|
|
17
|
+
url: null | string;
|
|
18
|
+
};
|
|
13
19
|
}
|
package/src/lib/types/Fetch.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
1
|
export type Fetch = typeof fetch;
|
|
2
2
|
|
|
3
3
|
export interface FetchOptions {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
method?: RequestMethodType;
|
|
5
|
+
headers?: Record<string, string>;
|
|
6
|
+
cache?: "default" | "no-cache" | "reload" | "force-cache" | "only-if-cached"; // default, no-cache, reload, force-cache, only-if-cached
|
|
7
|
+
credentials?: "include" | "same-origin" | "omit"; // include, same-origin, omit
|
|
8
|
+
redirect?: "manual" | "follow" | "error"; // manual, follow, error
|
|
9
|
+
referrerPolicy?: // no-referrer, no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
|
|
10
|
+
| "no-referrer"
|
|
11
|
+
| "no-referrer-when-downgrade"
|
|
12
|
+
| "origin"
|
|
13
|
+
| "origin-when-cross-origin"
|
|
14
|
+
| "same-origin"
|
|
15
|
+
| "strict-origin"
|
|
16
|
+
| "strict-origin-when-cross-origin"
|
|
17
|
+
| "unsafe-url";
|
|
8
18
|
}
|
|
9
19
|
|
|
10
|
-
export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
20
|
+
export type RequestMethodType = "GET" | "POST" | "PUT" | "DELETE" | "PATCH"; // GET, POST, PUT, DELETE, etc.
|
|
11
21
|
|
|
12
22
|
export interface FetchParameters {
|
|
13
23
|
/**
|
|
@@ -6,21 +6,17 @@ export interface LiveTranscriptionEvent {
|
|
|
6
6
|
is_final?: boolean;
|
|
7
7
|
speech_final?: boolean;
|
|
8
8
|
channel: {
|
|
9
|
-
alternatives:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
];
|
|
22
|
-
}
|
|
23
|
-
];
|
|
9
|
+
alternatives: {
|
|
10
|
+
transcript: string;
|
|
11
|
+
confidence: boolean;
|
|
12
|
+
words: {
|
|
13
|
+
word: string;
|
|
14
|
+
start: number;
|
|
15
|
+
end: number;
|
|
16
|
+
confidence: number;
|
|
17
|
+
punctuated_word: string;
|
|
18
|
+
}[];
|
|
19
|
+
}[];
|
|
24
20
|
};
|
|
25
21
|
metadata: {
|
|
26
22
|
request_id: string;
|
|
@@ -6,17 +6,17 @@ export interface SyncPrerecordedResponse {
|
|
|
6
6
|
interface Alternative {
|
|
7
7
|
transcript: string;
|
|
8
8
|
confidence: number;
|
|
9
|
-
words:
|
|
10
|
-
summaries?:
|
|
9
|
+
words: WordBase[];
|
|
10
|
+
summaries?: Summary[];
|
|
11
11
|
paragraphs?: ParagraphGroup;
|
|
12
|
-
entities?:
|
|
13
|
-
translations?:
|
|
14
|
-
topics?:
|
|
12
|
+
entities?: Entity[];
|
|
13
|
+
translations?: Translation[];
|
|
14
|
+
topics?: TopicGroup[];
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
interface Channel {
|
|
18
|
-
search?:
|
|
19
|
-
alternatives:
|
|
18
|
+
search?: Search[];
|
|
19
|
+
alternatives: Alternative[];
|
|
20
20
|
detected_language?: string;
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -42,9 +42,9 @@ interface Metadata {
|
|
|
42
42
|
created: string;
|
|
43
43
|
duration: number;
|
|
44
44
|
channels: number;
|
|
45
|
-
models:
|
|
45
|
+
models: string[];
|
|
46
46
|
model_info: Record<string, ModelInfo>;
|
|
47
|
-
warnings?:
|
|
47
|
+
warnings?: Warning[];
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
interface ModelInfo {
|
|
@@ -54,7 +54,7 @@ interface ModelInfo {
|
|
|
54
54
|
}
|
|
55
55
|
|
|
56
56
|
interface Paragraph {
|
|
57
|
-
sentences:
|
|
57
|
+
sentences: Sentence[];
|
|
58
58
|
start: number;
|
|
59
59
|
end: number;
|
|
60
60
|
num_words: number;
|
|
@@ -62,18 +62,18 @@ interface Paragraph {
|
|
|
62
62
|
|
|
63
63
|
interface ParagraphGroup {
|
|
64
64
|
transcript: string;
|
|
65
|
-
paragraphs:
|
|
65
|
+
paragraphs: Paragraph[];
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
interface Result {
|
|
69
|
-
channels:
|
|
70
|
-
utterances?:
|
|
69
|
+
channels: Channel[];
|
|
70
|
+
utterances?: Utterance[];
|
|
71
71
|
summary?: TranscriptionSummary;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
74
|
interface Search {
|
|
75
75
|
query: string;
|
|
76
|
-
hits:
|
|
76
|
+
hits: Hit[];
|
|
77
77
|
}
|
|
78
78
|
|
|
79
79
|
interface Sentence {
|
|
@@ -98,7 +98,7 @@ interface Topic {
|
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
interface TopicGroup {
|
|
101
|
-
topics:
|
|
101
|
+
topics: Topic[];
|
|
102
102
|
text: string;
|
|
103
103
|
start_word: number;
|
|
104
104
|
end_word: number;
|
|
@@ -115,7 +115,7 @@ interface Utterance {
|
|
|
115
115
|
confidence: number;
|
|
116
116
|
channel: number;
|
|
117
117
|
transcript: string;
|
|
118
|
-
words:
|
|
118
|
+
words: WordBase[];
|
|
119
119
|
speaker?: number;
|
|
120
120
|
id: string;
|
|
121
121
|
}
|
|
@@ -35,7 +35,7 @@ interface TranscriptionSchema extends Record<string, unknown> {
|
|
|
35
35
|
/**
|
|
36
36
|
* @see https://developers.deepgram.com/docs/redaction
|
|
37
37
|
*/
|
|
38
|
-
redact?:
|
|
38
|
+
redact?: string[] | string | boolean;
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* @see https://developers.deepgram.com/docs/diarization
|
|
@@ -81,7 +81,7 @@ interface TranscriptionSchema extends Record<string, unknown> {
|
|
|
81
81
|
/**
|
|
82
82
|
* @see https://developers.deepgram.com/docs/tagging
|
|
83
83
|
*/
|
|
84
|
-
tag?:
|
|
84
|
+
tag?: string[];
|
|
85
85
|
|
|
86
86
|
[key: string]: unknown;
|
|
87
87
|
}
|
package/src/lib/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const version = "3.0.0-
|
|
1
|
+
export const version = "3.0.0-beta.1";
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { DEFAULT_OPTIONS, DEFAULT_URL } from "../lib/constants";
|
|
2
|
+
import { applySettingDefaults, stripTrailingSlash } from "../lib/helpers";
|
|
3
|
+
import { DeepgramClientOptions } from "../lib/types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Deepgram Client.
|
|
7
|
+
*
|
|
8
|
+
* An isomorphic Javascript client for interacting with the Deepgram API.
|
|
9
|
+
* @see https://developers.deepgram.com
|
|
10
|
+
*/
|
|
11
|
+
export abstract class AbstractClient {
|
|
12
|
+
protected baseUrl: URL;
|
|
13
|
+
|
|
14
|
+
constructor(protected key: string, protected options: DeepgramClientOptions) {
|
|
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
|
+
if (this.willProxy()) {
|
|
34
|
+
this.baseUrl = this.resolveBaseUrl(this.options.restProxy?.url as string);
|
|
35
|
+
|
|
36
|
+
if (this.options.global.headers) {
|
|
37
|
+
this.options.global.headers["X-Deepgram-Proxy"] = this.options.global.url;
|
|
38
|
+
}
|
|
39
|
+
} else {
|
|
40
|
+
this.baseUrl = this.resolveBaseUrl(this.options.global.url);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
protected resolveBaseUrl(url: string) {
|
|
45
|
+
if (!/^https?:\/\//i.test(url)) {
|
|
46
|
+
url = "https://" + url;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return new URL(stripTrailingSlash(url));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
protected willProxy() {
|
|
53
|
+
const proxyUrl = this.options.restProxy?.url;
|
|
54
|
+
|
|
55
|
+
return !!proxyUrl;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -1,18 +1,24 @@
|
|
|
1
|
-
import { DeepgramApiError, DeepgramUnknownError } from "../lib/errors";
|
|
1
|
+
import { DeepgramApiError, DeepgramError, DeepgramUnknownError } from "../lib/errors";
|
|
2
2
|
import { Readable } from "stream";
|
|
3
3
|
import { fetchWithAuth, resolveResponse } from "../lib/fetch";
|
|
4
|
-
import type { 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";
|
|
5
8
|
|
|
6
|
-
export class AbstractRestfulClient {
|
|
7
|
-
protected baseUrl: URL;
|
|
8
|
-
protected headers: Record<string, string>;
|
|
9
|
+
export abstract class AbstractRestfulClient extends AbstractClient {
|
|
9
10
|
protected fetch: Fetch;
|
|
10
11
|
|
|
11
|
-
constructor(
|
|
12
|
-
|
|
13
|
-
this.headers = headers;
|
|
12
|
+
constructor(protected key: string, protected options: DeepgramClientOptions) {
|
|
13
|
+
super(key, options);
|
|
14
14
|
|
|
15
|
-
this.
|
|
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);
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
protected _getErrorMessage(err: any): string {
|
|
@@ -38,35 +44,38 @@ export class AbstractRestfulClient {
|
|
|
38
44
|
|
|
39
45
|
protected _getRequestParams(
|
|
40
46
|
method: RequestMethodType,
|
|
41
|
-
|
|
47
|
+
headers?: Record<string, string>,
|
|
42
48
|
parameters?: FetchParameters,
|
|
43
49
|
body?: string | Buffer | Readable
|
|
44
50
|
) {
|
|
45
|
-
const params: { [k: string]: any } = {
|
|
51
|
+
const params: { [k: string]: any } = {
|
|
52
|
+
...this.options?.fetch,
|
|
53
|
+
method,
|
|
54
|
+
headers: { ...this.options?.fetch?.headers, ...headers } || {},
|
|
55
|
+
};
|
|
46
56
|
|
|
47
57
|
if (method === "GET") {
|
|
48
58
|
return params;
|
|
49
59
|
}
|
|
50
60
|
|
|
51
|
-
params.headers = { "Content-Type": "application/json", ...options?.headers };
|
|
52
61
|
params.body = body;
|
|
62
|
+
params.duplex = "half";
|
|
53
63
|
|
|
54
|
-
return { ...params, ...parameters
|
|
64
|
+
return { ...params, ...parameters };
|
|
55
65
|
}
|
|
56
66
|
|
|
57
67
|
protected async _handleRequest(
|
|
58
68
|
fetcher: Fetch,
|
|
59
69
|
method: RequestMethodType,
|
|
60
70
|
url: string | URL,
|
|
61
|
-
|
|
71
|
+
headers?: Record<string, string>,
|
|
62
72
|
parameters?: FetchParameters,
|
|
63
73
|
body?: string | Buffer | Readable
|
|
64
74
|
): Promise<any> {
|
|
65
75
|
return new Promise((resolve, reject) => {
|
|
66
|
-
fetcher(url, this._getRequestParams(method,
|
|
76
|
+
fetcher(url, this._getRequestParams(method, headers, parameters, body))
|
|
67
77
|
.then((result) => {
|
|
68
78
|
if (!result.ok) throw result;
|
|
69
|
-
if (options?.noResolveJson) return result;
|
|
70
79
|
return result.json();
|
|
71
80
|
})
|
|
72
81
|
.then((data) => resolve(data))
|
|
@@ -77,48 +86,48 @@ export class AbstractRestfulClient {
|
|
|
77
86
|
protected async get(
|
|
78
87
|
fetcher: Fetch,
|
|
79
88
|
url: string | URL,
|
|
80
|
-
|
|
89
|
+
headers?: Record<string, string>,
|
|
81
90
|
parameters?: FetchParameters
|
|
82
91
|
): Promise<any> {
|
|
83
|
-
return this._handleRequest(fetcher, "GET", url,
|
|
92
|
+
return this._handleRequest(fetcher, "GET", url, headers, parameters);
|
|
84
93
|
}
|
|
85
94
|
|
|
86
95
|
protected async post(
|
|
87
96
|
fetcher: Fetch,
|
|
88
97
|
url: string | URL,
|
|
89
98
|
body: string | Buffer | Readable,
|
|
90
|
-
|
|
99
|
+
headers?: Record<string, string>,
|
|
91
100
|
parameters?: FetchParameters
|
|
92
101
|
): Promise<any> {
|
|
93
|
-
return this._handleRequest(fetcher, "POST", url,
|
|
102
|
+
return this._handleRequest(fetcher, "POST", url, headers, parameters, body);
|
|
94
103
|
}
|
|
95
104
|
|
|
96
105
|
protected async put(
|
|
97
106
|
fetcher: Fetch,
|
|
98
107
|
url: string | URL,
|
|
99
108
|
body: string | Buffer | Readable,
|
|
100
|
-
|
|
109
|
+
headers?: Record<string, string>,
|
|
101
110
|
parameters?: FetchParameters
|
|
102
111
|
): Promise<any> {
|
|
103
|
-
return this._handleRequest(fetcher, "PUT", url,
|
|
112
|
+
return this._handleRequest(fetcher, "PUT", url, headers, parameters, body);
|
|
104
113
|
}
|
|
105
114
|
|
|
106
115
|
protected async patch(
|
|
107
116
|
fetcher: Fetch,
|
|
108
117
|
url: string | URL,
|
|
109
118
|
body: string | Buffer | Readable,
|
|
110
|
-
|
|
119
|
+
headers?: Record<string, string>,
|
|
111
120
|
parameters?: FetchParameters
|
|
112
121
|
): Promise<any> {
|
|
113
|
-
return this._handleRequest(fetcher, "PATCH", url,
|
|
122
|
+
return this._handleRequest(fetcher, "PATCH", url, headers, parameters, body);
|
|
114
123
|
}
|
|
115
124
|
|
|
116
125
|
protected async delete(
|
|
117
126
|
fetcher: Fetch,
|
|
118
127
|
url: string | URL,
|
|
119
|
-
|
|
128
|
+
headers?: Record<string, string>,
|
|
120
129
|
parameters?: FetchParameters
|
|
121
130
|
): Promise<any> {
|
|
122
|
-
return this._handleRequest(fetcher, "DELETE", url,
|
|
131
|
+
return this._handleRequest(fetcher, "DELETE", url, headers, parameters);
|
|
123
132
|
}
|
|
124
133
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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,23 +1,14 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { AbstractClient } from "./AbstractClient";
|
|
2
2
|
import { LiveClient } from "./LiveClient";
|
|
3
3
|
import { LiveSchema } from "../lib/types";
|
|
4
|
+
import { PrerecordedClient } from "./PrerecordedClient";
|
|
4
5
|
|
|
5
|
-
export class ListenClient {
|
|
6
|
-
protected baseUrl: URL;
|
|
7
|
-
protected headers: Record<string, string>;
|
|
8
|
-
protected key: string;
|
|
9
|
-
|
|
10
|
-
constructor(baseUrl: URL, headers: Record<string, string>, apiKey: string) {
|
|
11
|
-
this.baseUrl = baseUrl;
|
|
12
|
-
this.headers = headers;
|
|
13
|
-
this.key = apiKey;
|
|
14
|
-
}
|
|
15
|
-
|
|
6
|
+
export class ListenClient extends AbstractClient {
|
|
16
7
|
get prerecorded() {
|
|
17
|
-
return new PrerecordedClient(this.
|
|
8
|
+
return new PrerecordedClient(this.key, this.options);
|
|
18
9
|
}
|
|
19
10
|
|
|
20
|
-
public live(
|
|
21
|
-
return new LiveClient(this.
|
|
11
|
+
public live(transcriptionOptions: LiveSchema, endpoint = "v1/listen") {
|
|
12
|
+
return new LiveClient(this.key, this.options, transcriptionOptions, endpoint);
|
|
22
13
|
}
|
|
23
14
|
}
|