@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.
Files changed (187) hide show
  1. package/README.md +240 -99
  2. package/dist/main/DeepgramClient.d.ts +2 -7
  3. package/dist/main/DeepgramClient.d.ts.map +1 -1
  4. package/dist/main/DeepgramClient.js +5 -28
  5. package/dist/main/DeepgramClient.js.map +1 -1
  6. package/dist/main/index.d.ts +5 -0
  7. package/dist/main/index.d.ts.map +1 -1
  8. package/dist/main/index.js +9 -2
  9. package/dist/main/index.js.map +1 -1
  10. package/dist/main/lib/constants.d.ts +4 -8
  11. package/dist/main/lib/constants.d.ts.map +1 -1
  12. package/dist/main/lib/constants.js +5 -1
  13. package/dist/main/lib/constants.js.map +1 -1
  14. package/dist/main/lib/helpers.d.ts.map +1 -1
  15. package/dist/main/lib/helpers.js +5 -5
  16. package/dist/main/lib/helpers.js.map +1 -1
  17. package/dist/main/lib/types/DeepgramClientOptions.d.ts +6 -1
  18. package/dist/main/lib/types/DeepgramClientOptions.d.ts.map +1 -1
  19. package/dist/main/lib/types/Fetch.d.ts +6 -4
  20. package/dist/main/lib/types/Fetch.d.ts.map +1 -1
  21. package/dist/main/lib/types/GetProjectBalancesResponse.d.ts +1 -1
  22. package/dist/main/lib/types/GetProjectBalancesResponse.d.ts.map +1 -1
  23. package/dist/main/lib/types/GetProjectInvitesResponse.d.ts +1 -1
  24. package/dist/main/lib/types/GetProjectInvitesResponse.d.ts.map +1 -1
  25. package/dist/main/lib/types/GetProjectKeysResponse.d.ts +1 -1
  26. package/dist/main/lib/types/GetProjectKeysResponse.d.ts.map +1 -1
  27. package/dist/main/lib/types/GetProjectMembersResponse.d.ts +1 -1
  28. package/dist/main/lib/types/GetProjectMembersResponse.d.ts.map +1 -1
  29. package/dist/main/lib/types/GetProjectUsageFieldsResponse.d.ts +1 -1
  30. package/dist/main/lib/types/GetProjectUsageFieldsResponse.d.ts.map +1 -1
  31. package/dist/main/lib/types/GetProjectUsageRequestsResponse.d.ts +1 -1
  32. package/dist/main/lib/types/GetProjectUsageRequestsResponse.d.ts.map +1 -1
  33. package/dist/main/lib/types/GetProjectUsageSummaryResponse.d.ts +1 -1
  34. package/dist/main/lib/types/GetProjectUsageSummaryResponse.d.ts.map +1 -1
  35. package/dist/main/lib/types/GetProjectsResponse.d.ts +1 -1
  36. package/dist/main/lib/types/GetProjectsResponse.d.ts.map +1 -1
  37. package/dist/main/lib/types/ListOnPremCredentialsResponse.d.ts +1 -1
  38. package/dist/main/lib/types/ListOnPremCredentialsResponse.d.ts.map +1 -1
  39. package/dist/main/lib/types/LiveTranscriptionEvent.d.ts +11 -15
  40. package/dist/main/lib/types/LiveTranscriptionEvent.d.ts.map +1 -1
  41. package/dist/main/lib/types/SyncPrerecordedResponse.d.ts +16 -16
  42. package/dist/main/lib/types/SyncPrerecordedResponse.d.ts.map +1 -1
  43. package/dist/main/lib/types/TranscriptionSchema.d.ts +2 -2
  44. package/dist/main/lib/types/TranscriptionSchema.d.ts.map +1 -1
  45. package/dist/main/lib/version.d.ts +1 -1
  46. package/dist/main/lib/version.js +1 -1
  47. package/dist/main/packages/AbstractClient.d.ts +16 -0
  48. package/dist/main/packages/AbstractClient.d.ts.map +1 -0
  49. package/dist/main/packages/AbstractClient.js +51 -0
  50. package/dist/main/packages/AbstractClient.js.map +1 -0
  51. package/dist/main/packages/AbstractRestfulClient.d.ts +14 -12
  52. package/dist/main/packages/AbstractRestfulClient.d.ts.map +1 -1
  53. package/dist/main/packages/AbstractRestfulClient.js +28 -23
  54. package/dist/main/packages/AbstractRestfulClient.js.map +1 -1
  55. package/dist/main/packages/AbstractWsClient.d.ts +10 -0
  56. package/dist/main/packages/AbstractWsClient.d.ts.map +1 -0
  57. package/dist/main/packages/AbstractWsClient.js +33 -0
  58. package/dist/main/packages/AbstractWsClient.js.map +1 -0
  59. package/dist/main/packages/ListenClient.d.ts +4 -7
  60. package/dist/main/packages/ListenClient.d.ts.map +1 -1
  61. package/dist/main/packages/ListenClient.js +6 -10
  62. package/dist/main/packages/ListenClient.js.map +1 -1
  63. package/dist/main/packages/LiveClient.d.ts +10 -7
  64. package/dist/main/packages/LiveClient.d.ts.map +1 -1
  65. package/dist/main/packages/LiveClient.js +24 -15
  66. package/dist/main/packages/LiveClient.js.map +1 -1
  67. package/dist/main/packages/ManageClient.d.ts.map +1 -1
  68. package/dist/main/packages/ManageClient.js +22 -66
  69. package/dist/main/packages/ManageClient.js.map +1 -1
  70. package/dist/main/packages/OnPremClient.d.ts.map +1 -1
  71. package/dist/main/packages/OnPremClient.js +4 -12
  72. package/dist/main/packages/OnPremClient.js.map +1 -1
  73. package/dist/main/packages/PrerecordedClient.d.ts.map +1 -1
  74. package/dist/main/packages/PrerecordedClient.js +4 -12
  75. package/dist/main/packages/PrerecordedClient.js.map +1 -1
  76. package/dist/main/packages/index.d.ts +9 -0
  77. package/dist/main/packages/index.d.ts.map +1 -0
  78. package/dist/main/packages/index.js +20 -0
  79. package/dist/main/packages/index.js.map +1 -0
  80. package/dist/module/DeepgramClient.d.ts +2 -7
  81. package/dist/module/DeepgramClient.d.ts.map +1 -1
  82. package/dist/module/DeepgramClient.js +5 -28
  83. package/dist/module/DeepgramClient.js.map +1 -1
  84. package/dist/module/index.d.ts +5 -0
  85. package/dist/module/index.d.ts.map +1 -1
  86. package/dist/module/index.js +6 -1
  87. package/dist/module/index.js.map +1 -1
  88. package/dist/module/lib/constants.d.ts +4 -8
  89. package/dist/module/lib/constants.d.ts.map +1 -1
  90. package/dist/module/lib/constants.js +4 -0
  91. package/dist/module/lib/constants.js.map +1 -1
  92. package/dist/module/lib/helpers.d.ts.map +1 -1
  93. package/dist/module/lib/helpers.js +2 -5
  94. package/dist/module/lib/helpers.js.map +1 -1
  95. package/dist/module/lib/types/DeepgramClientOptions.d.ts +6 -1
  96. package/dist/module/lib/types/DeepgramClientOptions.d.ts.map +1 -1
  97. package/dist/module/lib/types/Fetch.d.ts +6 -4
  98. package/dist/module/lib/types/Fetch.d.ts.map +1 -1
  99. package/dist/module/lib/types/GetProjectBalancesResponse.d.ts +1 -1
  100. package/dist/module/lib/types/GetProjectBalancesResponse.d.ts.map +1 -1
  101. package/dist/module/lib/types/GetProjectInvitesResponse.d.ts +1 -1
  102. package/dist/module/lib/types/GetProjectInvitesResponse.d.ts.map +1 -1
  103. package/dist/module/lib/types/GetProjectKeysResponse.d.ts +1 -1
  104. package/dist/module/lib/types/GetProjectKeysResponse.d.ts.map +1 -1
  105. package/dist/module/lib/types/GetProjectMembersResponse.d.ts +1 -1
  106. package/dist/module/lib/types/GetProjectMembersResponse.d.ts.map +1 -1
  107. package/dist/module/lib/types/GetProjectUsageFieldsResponse.d.ts +1 -1
  108. package/dist/module/lib/types/GetProjectUsageFieldsResponse.d.ts.map +1 -1
  109. package/dist/module/lib/types/GetProjectUsageRequestsResponse.d.ts +1 -1
  110. package/dist/module/lib/types/GetProjectUsageRequestsResponse.d.ts.map +1 -1
  111. package/dist/module/lib/types/GetProjectUsageSummaryResponse.d.ts +1 -1
  112. package/dist/module/lib/types/GetProjectUsageSummaryResponse.d.ts.map +1 -1
  113. package/dist/module/lib/types/GetProjectsResponse.d.ts +1 -1
  114. package/dist/module/lib/types/GetProjectsResponse.d.ts.map +1 -1
  115. package/dist/module/lib/types/ListOnPremCredentialsResponse.d.ts +1 -1
  116. package/dist/module/lib/types/ListOnPremCredentialsResponse.d.ts.map +1 -1
  117. package/dist/module/lib/types/LiveTranscriptionEvent.d.ts +11 -15
  118. package/dist/module/lib/types/LiveTranscriptionEvent.d.ts.map +1 -1
  119. package/dist/module/lib/types/SyncPrerecordedResponse.d.ts +16 -16
  120. package/dist/module/lib/types/SyncPrerecordedResponse.d.ts.map +1 -1
  121. package/dist/module/lib/types/TranscriptionSchema.d.ts +2 -2
  122. package/dist/module/lib/types/TranscriptionSchema.d.ts.map +1 -1
  123. package/dist/module/lib/version.d.ts +1 -1
  124. package/dist/module/lib/version.js +1 -1
  125. package/dist/module/packages/AbstractClient.d.ts +16 -0
  126. package/dist/module/packages/AbstractClient.d.ts.map +1 -0
  127. package/dist/module/packages/AbstractClient.js +47 -0
  128. package/dist/module/packages/AbstractClient.js.map +1 -0
  129. package/dist/module/packages/AbstractRestfulClient.d.ts +14 -12
  130. package/dist/module/packages/AbstractRestfulClient.d.ts.map +1 -1
  131. package/dist/module/packages/AbstractRestfulClient.js +29 -24
  132. package/dist/module/packages/AbstractRestfulClient.js.map +1 -1
  133. package/dist/module/packages/AbstractWsClient.d.ts +10 -0
  134. package/dist/module/packages/AbstractWsClient.d.ts.map +1 -0
  135. package/dist/module/packages/AbstractWsClient.js +29 -0
  136. package/dist/module/packages/AbstractWsClient.js.map +1 -0
  137. package/dist/module/packages/ListenClient.d.ts +4 -7
  138. package/dist/module/packages/ListenClient.d.ts.map +1 -1
  139. package/dist/module/packages/ListenClient.js +6 -10
  140. package/dist/module/packages/ListenClient.js.map +1 -1
  141. package/dist/module/packages/LiveClient.d.ts +10 -7
  142. package/dist/module/packages/LiveClient.d.ts.map +1 -1
  143. package/dist/module/packages/LiveClient.js +24 -15
  144. package/dist/module/packages/LiveClient.js.map +1 -1
  145. package/dist/module/packages/ManageClient.d.ts.map +1 -1
  146. package/dist/module/packages/ManageClient.js +22 -66
  147. package/dist/module/packages/ManageClient.js.map +1 -1
  148. package/dist/module/packages/OnPremClient.d.ts.map +1 -1
  149. package/dist/module/packages/OnPremClient.js +4 -12
  150. package/dist/module/packages/OnPremClient.js.map +1 -1
  151. package/dist/module/packages/PrerecordedClient.d.ts.map +1 -1
  152. package/dist/module/packages/PrerecordedClient.js +4 -12
  153. package/dist/module/packages/PrerecordedClient.js.map +1 -1
  154. package/dist/module/packages/index.d.ts +9 -0
  155. package/dist/module/packages/index.d.ts.map +1 -0
  156. package/dist/module/packages/index.js +9 -0
  157. package/dist/module/packages/index.js.map +1 -0
  158. package/dist/umd/deepgram.js +1 -1
  159. package/package.json +7 -1
  160. package/src/DeepgramClient.ts +5 -43
  161. package/src/index.ts +7 -1
  162. package/src/lib/constants.ts +6 -0
  163. package/src/lib/helpers.ts +2 -10
  164. package/src/lib/types/DeepgramClientOptions.ts +7 -1
  165. package/src/lib/types/Fetch.ts +15 -5
  166. package/src/lib/types/GetProjectBalancesResponse.ts +1 -1
  167. package/src/lib/types/GetProjectInvitesResponse.ts +1 -1
  168. package/src/lib/types/GetProjectKeysResponse.ts +1 -1
  169. package/src/lib/types/GetProjectMembersResponse.ts +1 -1
  170. package/src/lib/types/GetProjectUsageFieldsResponse.ts +1 -1
  171. package/src/lib/types/GetProjectUsageRequestsResponse.ts +1 -1
  172. package/src/lib/types/GetProjectUsageSummaryResponse.ts +1 -1
  173. package/src/lib/types/GetProjectsResponse.ts +1 -1
  174. package/src/lib/types/ListOnPremCredentialsResponse.ts +1 -1
  175. package/src/lib/types/LiveTranscriptionEvent.ts +11 -15
  176. package/src/lib/types/SyncPrerecordedResponse.ts +16 -16
  177. package/src/lib/types/TranscriptionSchema.ts +2 -2
  178. package/src/lib/version.ts +1 -1
  179. package/src/packages/AbstractClient.ts +57 -0
  180. package/src/packages/AbstractRestfulClient.ts +35 -26
  181. package/src/packages/AbstractWsClient.ts +42 -0
  182. package/src/packages/ListenClient.ts +6 -15
  183. package/src/packages/LiveClient.ts +29 -17
  184. package/src/packages/ManageClient.ts +22 -66
  185. package/src/packages/OnPremClient.ts +4 -12
  186. package/src/packages/PrerecordedClient.ts +4 -14
  187. package/src/packages/index.ts +8 -0
@@ -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
  };
@@ -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
- const { global: globalOptions } = options;
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
  }
@@ -1,13 +1,23 @@
1
1
  export type Fetch = typeof fetch;
2
2
 
3
3
  export interface FetchOptions {
4
- headers?: {
5
- [key: string]: string;
6
- };
7
- noResolveJson?: boolean;
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
  /**
@@ -1,5 +1,5 @@
1
1
  export interface GetProjectBalancesResponse {
2
- balances: [GetProjectBalanceResponse];
2
+ balances: GetProjectBalanceResponse[];
3
3
  }
4
4
 
5
5
  export interface GetProjectBalanceResponse {
@@ -1,5 +1,5 @@
1
1
  export interface GetProjectInvitesResponse {
2
- invites: [Invite];
2
+ invites: Invite[];
3
3
  }
4
4
 
5
5
  interface Invite {
@@ -1,5 +1,5 @@
1
1
  export interface GetProjectKeysResponse {
2
- api_keys: [GetProjectKeyResponse];
2
+ api_keys: GetProjectKeyResponse[];
3
3
  }
4
4
 
5
5
  export interface GetProjectKeyResponse {
@@ -1,5 +1,5 @@
1
1
  export interface GetProjectMembersResponse {
2
- members: [Member];
2
+ members: Member[];
3
3
  }
4
4
 
5
5
  interface Member {
@@ -1,6 +1,6 @@
1
1
  export interface GetProjectUsageFieldsResponse {
2
2
  tags: string[];
3
- models: [UsageModel];
3
+ models: UsageModel[];
4
4
  processing_methods: string[];
5
5
  languages: string[];
6
6
  features: string[];
@@ -1,7 +1,7 @@
1
1
  export interface GetProjectUsageRequestsResponse {
2
2
  page: number;
3
3
  limit: number;
4
- requests: [GetProjectUsageRequestResponse];
4
+ requests: GetProjectUsageRequestResponse[];
5
5
  }
6
6
 
7
7
  export interface GetProjectUsageRequestResponse {
@@ -5,7 +5,7 @@ export interface GetProjectUsageSummaryResponse {
5
5
  units: string;
6
6
  amount: number;
7
7
  };
8
- results: [UsageSummary];
8
+ results: UsageSummary[];
9
9
  }
10
10
 
11
11
  interface UsageSummary {
@@ -1,5 +1,5 @@
1
1
  export interface GetProjectsResponse {
2
- projects: [Project];
2
+ projects: Project[];
3
3
  }
4
4
 
5
5
  interface Project {
@@ -1,5 +1,5 @@
1
1
  export interface ListOnPremCredentialsResponse {
2
- distribution_credentials: [OnPremCredentialResponse];
2
+ distribution_credentials: OnPremCredentialResponse[];
3
3
  }
4
4
 
5
5
  export interface OnPremCredentialResponse {
@@ -6,21 +6,17 @@ export interface LiveTranscriptionEvent {
6
6
  is_final?: boolean;
7
7
  speech_final?: boolean;
8
8
  channel: {
9
- alternatives: [
10
- {
11
- transcript: string;
12
- confidence: boolean;
13
- words: [
14
- {
15
- word: string;
16
- start: number;
17
- end: number;
18
- confidence: number;
19
- punctuated_word: string;
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: Array<WordBase>;
10
- summaries?: Array<Summary>;
9
+ words: WordBase[];
10
+ summaries?: Summary[];
11
11
  paragraphs?: ParagraphGroup;
12
- entities?: Array<Entity>;
13
- translations?: Array<Translation>;
14
- topics?: Array<TopicGroup>;
12
+ entities?: Entity[];
13
+ translations?: Translation[];
14
+ topics?: TopicGroup[];
15
15
  }
16
16
 
17
17
  interface Channel {
18
- search?: Array<Search>;
19
- alternatives: Array<Alternative>;
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: Array<string>;
45
+ models: string[];
46
46
  model_info: Record<string, ModelInfo>;
47
- warnings?: Array<Warning>;
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: Array<Sentence>;
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: Array<Paragraph>;
65
+ paragraphs: Paragraph[];
66
66
  }
67
67
 
68
68
  interface Result {
69
- channels: Array<Channel>;
70
- utterances?: Array<Utterance>;
69
+ channels: Channel[];
70
+ utterances?: Utterance[];
71
71
  summary?: TranscriptionSummary;
72
72
  }
73
73
 
74
74
  interface Search {
75
75
  query: string;
76
- hits: Array<Hit>;
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: Array<Topic>;
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: Array<WordBase>;
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?: Array<string> | string | boolean;
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?: Array<string>;
84
+ tag?: string[];
85
85
 
86
86
  [key: string]: unknown;
87
87
  }
@@ -1 +1 @@
1
- export const version = "3.0.0-alpha.9";
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, FetchOptions, FetchParameters, RequestMethodType } from "../lib/types/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(baseUrl: URL, headers: Record<string, string>, apiKey: string) {
12
- this.baseUrl = baseUrl;
13
- this.headers = headers;
12
+ constructor(protected key: string, protected options: DeepgramClientOptions) {
13
+ super(key, options);
14
14
 
15
- this.fetch = fetchWithAuth(apiKey);
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
- options?: FetchOptions,
47
+ headers?: Record<string, string>,
42
48
  parameters?: FetchParameters,
43
49
  body?: string | Buffer | Readable
44
50
  ) {
45
- const params: { [k: string]: any } = { method, headers: options?.headers || {} };
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, duplex: "half" };
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
- options?: FetchOptions,
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, options, parameters, body))
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
- options?: FetchOptions,
89
+ headers?: Record<string, string>,
81
90
  parameters?: FetchParameters
82
91
  ): Promise<any> {
83
- return this._handleRequest(fetcher, "GET", url, options, parameters);
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
- options?: FetchOptions,
99
+ headers?: Record<string, string>,
91
100
  parameters?: FetchParameters
92
101
  ): Promise<any> {
93
- return this._handleRequest(fetcher, "POST", url, options, parameters, body);
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
- options?: FetchOptions,
109
+ headers?: Record<string, string>,
101
110
  parameters?: FetchParameters
102
111
  ): Promise<any> {
103
- return this._handleRequest(fetcher, "PUT", url, options, parameters, body);
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
- options?: FetchOptions,
119
+ headers?: Record<string, string>,
111
120
  parameters?: FetchParameters
112
121
  ): Promise<any> {
113
- return this._handleRequest(fetcher, "PATCH", url, options, parameters, body);
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
- options?: FetchOptions,
128
+ headers?: Record<string, string>,
120
129
  parameters?: FetchParameters
121
130
  ): Promise<any> {
122
- return this._handleRequest(fetcher, "DELETE", url, options, parameters);
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 { PrerecordedClient } from "./PrerecordedClient";
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.baseUrl, this.headers, this.key);
8
+ return new PrerecordedClient(this.key, this.options);
18
9
  }
19
10
 
20
- public live(options: LiveSchema, endpoint = "v1/listen") {
21
- return new LiveClient(this.baseUrl, this.key, options, endpoint);
11
+ public live(transcriptionOptions: LiveSchema, endpoint = "v1/listen") {
12
+ return new LiveClient(this.key, this.options, transcriptionOptions, endpoint);
22
13
  }
23
14
  }