@deepgram/sdk 3.3.5 → 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.
Files changed (295) hide show
  1. package/README.md +129 -9
  2. package/dist/main/DeepgramClient.d.ts +65 -12
  3. package/dist/main/DeepgramClient.d.ts.map +1 -1
  4. package/dist/main/DeepgramClient.js +73 -18
  5. package/dist/main/DeepgramClient.js.map +1 -1
  6. package/dist/main/index.d.ts +13 -4
  7. package/dist/main/index.d.ts.map +1 -1
  8. package/dist/main/index.js +18 -8
  9. package/dist/main/index.js.map +1 -1
  10. package/dist/main/lib/constants.d.ts +16 -7
  11. package/dist/main/lib/constants.d.ts.map +1 -1
  12. package/dist/main/lib/constants.js +26 -9
  13. package/dist/main/lib/constants.js.map +1 -1
  14. package/dist/main/lib/enums/LiveConnectionState.d.ts +5 -0
  15. package/dist/main/lib/enums/LiveConnectionState.d.ts.map +1 -1
  16. package/dist/main/lib/enums/LiveConnectionState.js +5 -0
  17. package/dist/main/lib/enums/LiveConnectionState.js.map +1 -1
  18. package/dist/main/lib/enums/LiveTranscriptionEvents.d.ts +24 -3
  19. package/dist/main/lib/enums/LiveTranscriptionEvents.d.ts.map +1 -1
  20. package/dist/main/lib/enums/LiveTranscriptionEvents.js +23 -2
  21. package/dist/main/lib/enums/LiveTranscriptionEvents.js.map +1 -1
  22. package/dist/main/lib/enums/index.d.ts +2 -2
  23. package/dist/main/lib/enums/index.d.ts.map +1 -1
  24. package/dist/main/lib/enums/index.js +16 -5
  25. package/dist/main/lib/enums/index.js.map +1 -1
  26. package/dist/main/lib/fetch.d.ts +19 -1
  27. package/dist/main/lib/fetch.d.ts.map +1 -1
  28. package/dist/main/lib/fetch.js +19 -1
  29. package/dist/main/lib/fetch.js.map +1 -1
  30. package/dist/main/lib/helpers.d.ts +9 -5
  31. package/dist/main/lib/helpers.d.ts.map +1 -1
  32. package/dist/main/lib/helpers.js +82 -7
  33. package/dist/main/lib/helpers.js.map +1 -1
  34. package/dist/main/lib/types/CreateProjectKeySchema.d.ts +0 -1
  35. package/dist/main/lib/types/CreateProjectKeySchema.d.ts.map +1 -1
  36. package/dist/main/lib/types/DeepgramClientOptions.d.ts +75 -8
  37. package/dist/main/lib/types/DeepgramClientOptions.d.ts.map +1 -1
  38. package/dist/main/lib/types/Fetch.d.ts +3 -13
  39. package/dist/main/lib/types/Fetch.d.ts.map +1 -1
  40. package/dist/main/lib/types/LiveConfigOptions.d.ts +6 -3
  41. package/dist/main/lib/types/LiveConfigOptions.d.ts.map +1 -1
  42. package/dist/main/lib/types/TranscriptionSchema.d.ts +0 -1
  43. package/dist/main/lib/types/TranscriptionSchema.d.ts.map +1 -1
  44. package/dist/main/lib/types/UpdateProjectMemberScopeSchema.d.ts +1 -1
  45. package/dist/main/lib/types/UpdateProjectMemberScopeSchema.d.ts.map +1 -1
  46. package/dist/main/lib/types/UpdateProjectSchema.d.ts +0 -1
  47. package/dist/main/lib/types/UpdateProjectSchema.d.ts.map +1 -1
  48. package/dist/main/lib/types/index.d.ts +39 -39
  49. package/dist/main/lib/types/index.d.ts.map +1 -1
  50. package/dist/main/lib/types/index.js +53 -0
  51. package/dist/main/lib/types/index.js.map +1 -1
  52. package/dist/main/lib/version.d.ts +1 -1
  53. package/dist/main/lib/version.js +1 -1
  54. package/dist/main/packages/AbstractClient.d.ts +63 -10
  55. package/dist/main/packages/AbstractClient.d.ts.map +1 -1
  56. package/dist/main/packages/AbstractClient.js +103 -50
  57. package/dist/main/packages/AbstractClient.js.map +1 -1
  58. package/dist/main/packages/AbstractLiveClient.d.ts +114 -0
  59. package/dist/main/packages/AbstractLiveClient.d.ts.map +1 -0
  60. package/dist/main/packages/AbstractLiveClient.js +238 -0
  61. package/dist/main/packages/AbstractLiveClient.js.map +1 -0
  62. package/dist/main/packages/AbstractRestClient.d.ts +105 -0
  63. package/dist/main/packages/AbstractRestClient.d.ts.map +1 -0
  64. package/dist/main/packages/AbstractRestClient.js +185 -0
  65. package/dist/main/packages/AbstractRestClient.js.map +1 -0
  66. package/dist/main/packages/ListenClient.d.ts +21 -4
  67. package/dist/main/packages/ListenClient.d.ts.map +1 -1
  68. package/dist/main/packages/ListenClient.js +25 -5
  69. package/dist/main/packages/ListenClient.js.map +1 -1
  70. package/dist/main/packages/ListenLiveClient.d.ts +58 -0
  71. package/dist/main/packages/ListenLiveClient.d.ts.map +1 -0
  72. package/dist/main/packages/ListenLiveClient.js +119 -0
  73. package/dist/main/packages/ListenLiveClient.js.map +1 -0
  74. package/dist/main/packages/ListenRestClient.d.ts +57 -0
  75. package/dist/main/packages/ListenRestClient.d.ts.map +1 -0
  76. package/dist/main/packages/ListenRestClient.js +167 -0
  77. package/dist/main/packages/ListenRestClient.js.map +1 -0
  78. package/dist/main/packages/ManageRestClient.d.ts +286 -0
  79. package/dist/main/packages/ManageRestClient.d.ts.map +1 -0
  80. package/dist/main/packages/ManageRestClient.js +628 -0
  81. package/dist/main/packages/ManageRestClient.js.map +1 -0
  82. package/dist/main/packages/ReadRestClient.d.ts +57 -0
  83. package/dist/main/packages/ReadRestClient.d.ts.map +1 -0
  84. package/dist/main/packages/ReadRestClient.js +165 -0
  85. package/dist/main/packages/ReadRestClient.js.map +1 -0
  86. package/dist/main/packages/SelfHostedRestClient.d.ts +47 -0
  87. package/dist/main/packages/SelfHostedRestClient.d.ts.map +1 -0
  88. package/dist/main/packages/SelfHostedRestClient.js +120 -0
  89. package/dist/main/packages/SelfHostedRestClient.js.map +1 -0
  90. package/dist/main/packages/SpeakRestClient.d.ts +36 -0
  91. package/dist/main/packages/SpeakRestClient.d.ts.map +1 -0
  92. package/dist/main/packages/SpeakRestClient.js +84 -0
  93. package/dist/main/packages/SpeakRestClient.js.map +1 -0
  94. package/dist/main/packages/index.d.ts +10 -8
  95. package/dist/main/packages/index.d.ts.map +1 -1
  96. package/dist/main/packages/index.js +24 -17
  97. package/dist/main/packages/index.js.map +1 -1
  98. package/dist/module/DeepgramClient.d.ts +65 -12
  99. package/dist/module/DeepgramClient.d.ts.map +1 -1
  100. package/dist/module/DeepgramClient.js +72 -17
  101. package/dist/module/DeepgramClient.js.map +1 -1
  102. package/dist/module/index.d.ts +13 -4
  103. package/dist/module/index.d.ts.map +1 -1
  104. package/dist/module/index.js +18 -8
  105. package/dist/module/index.js.map +1 -1
  106. package/dist/module/lib/constants.d.ts +16 -7
  107. package/dist/module/lib/constants.d.ts.map +1 -1
  108. package/dist/module/lib/constants.js +26 -9
  109. package/dist/module/lib/constants.js.map +1 -1
  110. package/dist/module/lib/enums/LiveConnectionState.d.ts +5 -0
  111. package/dist/module/lib/enums/LiveConnectionState.d.ts.map +1 -1
  112. package/dist/module/lib/enums/LiveConnectionState.js +5 -0
  113. package/dist/module/lib/enums/LiveConnectionState.js.map +1 -1
  114. package/dist/module/lib/enums/LiveTranscriptionEvents.d.ts +24 -3
  115. package/dist/module/lib/enums/LiveTranscriptionEvents.d.ts.map +1 -1
  116. package/dist/module/lib/enums/LiveTranscriptionEvents.js +23 -2
  117. package/dist/module/lib/enums/LiveTranscriptionEvents.js.map +1 -1
  118. package/dist/module/lib/enums/index.d.ts +2 -2
  119. package/dist/module/lib/enums/index.d.ts.map +1 -1
  120. package/dist/module/lib/enums/index.js +2 -2
  121. package/dist/module/lib/enums/index.js.map +1 -1
  122. package/dist/module/lib/fetch.d.ts +19 -1
  123. package/dist/module/lib/fetch.d.ts.map +1 -1
  124. package/dist/module/lib/fetch.js +19 -1
  125. package/dist/module/lib/fetch.js.map +1 -1
  126. package/dist/module/lib/helpers.d.ts +9 -5
  127. package/dist/module/lib/helpers.d.ts.map +1 -1
  128. package/dist/module/lib/helpers.js +75 -4
  129. package/dist/module/lib/helpers.js.map +1 -1
  130. package/dist/module/lib/types/CreateProjectKeySchema.d.ts +0 -1
  131. package/dist/module/lib/types/CreateProjectKeySchema.d.ts.map +1 -1
  132. package/dist/module/lib/types/DeepgramClientOptions.d.ts +75 -8
  133. package/dist/module/lib/types/DeepgramClientOptions.d.ts.map +1 -1
  134. package/dist/module/lib/types/Fetch.d.ts +3 -13
  135. package/dist/module/lib/types/Fetch.d.ts.map +1 -1
  136. package/dist/module/lib/types/LiveConfigOptions.d.ts +6 -3
  137. package/dist/module/lib/types/LiveConfigOptions.d.ts.map +1 -1
  138. package/dist/module/lib/types/TranscriptionSchema.d.ts +0 -1
  139. package/dist/module/lib/types/TranscriptionSchema.d.ts.map +1 -1
  140. package/dist/module/lib/types/UpdateProjectMemberScopeSchema.d.ts +1 -1
  141. package/dist/module/lib/types/UpdateProjectMemberScopeSchema.d.ts.map +1 -1
  142. package/dist/module/lib/types/UpdateProjectSchema.d.ts +0 -1
  143. package/dist/module/lib/types/UpdateProjectSchema.d.ts.map +1 -1
  144. package/dist/module/lib/types/index.d.ts +39 -39
  145. package/dist/module/lib/types/index.d.ts.map +1 -1
  146. package/dist/module/lib/types/index.js +39 -1
  147. package/dist/module/lib/types/index.js.map +1 -1
  148. package/dist/module/lib/version.d.ts +1 -1
  149. package/dist/module/lib/version.js +1 -1
  150. package/dist/module/packages/AbstractClient.d.ts +63 -10
  151. package/dist/module/packages/AbstractClient.d.ts.map +1 -1
  152. package/dist/module/packages/AbstractClient.js +99 -50
  153. package/dist/module/packages/AbstractClient.js.map +1 -1
  154. package/dist/module/packages/AbstractLiveClient.d.ts +114 -0
  155. package/dist/module/packages/AbstractLiveClient.d.ts.map +1 -0
  156. package/dist/module/packages/AbstractLiveClient.js +211 -0
  157. package/dist/module/packages/AbstractLiveClient.js.map +1 -0
  158. package/dist/module/packages/AbstractRestClient.d.ts +105 -0
  159. package/dist/module/packages/AbstractRestClient.d.ts.map +1 -0
  160. package/dist/module/packages/AbstractRestClient.js +178 -0
  161. package/dist/module/packages/AbstractRestClient.js.map +1 -0
  162. package/dist/module/packages/ListenClient.d.ts +21 -4
  163. package/dist/module/packages/ListenClient.d.ts.map +1 -1
  164. package/dist/module/packages/ListenClient.js +25 -5
  165. package/dist/module/packages/ListenClient.js.map +1 -1
  166. package/dist/module/packages/ListenLiveClient.d.ts +58 -0
  167. package/dist/module/packages/ListenLiveClient.d.ts.map +1 -0
  168. package/dist/module/packages/ListenLiveClient.js +115 -0
  169. package/dist/module/packages/ListenLiveClient.js.map +1 -0
  170. package/dist/module/packages/ListenRestClient.d.ts +57 -0
  171. package/dist/module/packages/ListenRestClient.d.ts.map +1 -0
  172. package/dist/module/packages/ListenRestClient.js +163 -0
  173. package/dist/module/packages/ListenRestClient.js.map +1 -0
  174. package/dist/module/packages/ManageRestClient.d.ts +286 -0
  175. package/dist/module/packages/ManageRestClient.d.ts.map +1 -0
  176. package/dist/module/packages/ManageRestClient.js +624 -0
  177. package/dist/module/packages/ManageRestClient.js.map +1 -0
  178. package/dist/module/packages/ReadRestClient.d.ts +57 -0
  179. package/dist/module/packages/ReadRestClient.d.ts.map +1 -0
  180. package/dist/module/packages/ReadRestClient.js +161 -0
  181. package/dist/module/packages/ReadRestClient.js.map +1 -0
  182. package/dist/module/packages/SelfHostedRestClient.d.ts +47 -0
  183. package/dist/module/packages/SelfHostedRestClient.d.ts.map +1 -0
  184. package/dist/module/packages/SelfHostedRestClient.js +116 -0
  185. package/dist/module/packages/SelfHostedRestClient.js.map +1 -0
  186. package/dist/module/packages/SpeakRestClient.d.ts +36 -0
  187. package/dist/module/packages/SpeakRestClient.d.ts.map +1 -0
  188. package/dist/module/packages/SpeakRestClient.js +80 -0
  189. package/dist/module/packages/SpeakRestClient.js.map +1 -0
  190. package/dist/module/packages/index.d.ts +10 -8
  191. package/dist/module/packages/index.d.ts.map +1 -1
  192. package/dist/module/packages/index.js +10 -8
  193. package/dist/module/packages/index.js.map +1 -1
  194. package/dist/umd/deepgram.js +1 -1
  195. package/package.json +8 -6
  196. package/src/DeepgramClient.ts +81 -17
  197. package/src/index.ts +32 -7
  198. package/src/lib/constants.ts +33 -13
  199. package/src/lib/enums/LiveConnectionState.ts +11 -4
  200. package/src/lib/enums/LiveTranscriptionEvents.ts +27 -4
  201. package/src/lib/enums/index.ts +2 -2
  202. package/src/lib/fetch.ts +22 -2
  203. package/src/lib/helpers.ts +98 -9
  204. package/src/lib/types/CreateProjectKeySchema.ts +0 -1
  205. package/src/lib/types/DeepgramClientOptions.ts +75 -11
  206. package/src/lib/types/Fetch.ts +2 -24
  207. package/src/lib/types/LiveConfigOptions.ts +7 -3
  208. package/src/lib/types/TranscriptionSchema.ts +0 -2
  209. package/src/lib/types/UpdateProjectMemberScopeSchema.ts +1 -1
  210. package/src/lib/types/UpdateProjectSchema.ts +0 -1
  211. package/src/lib/types/index.ts +39 -54
  212. package/src/lib/version.ts +1 -1
  213. package/src/packages/AbstractClient.ts +129 -61
  214. package/src/packages/AbstractLiveClient.ts +280 -0
  215. package/src/packages/AbstractRestClient.ts +221 -0
  216. package/src/packages/ListenClient.ts +26 -5
  217. package/src/packages/ListenLiveClient.ts +133 -0
  218. package/src/packages/ListenRestClient.ts +201 -0
  219. package/src/packages/ManageRestClient.ts +760 -0
  220. package/src/packages/ReadRestClient.ts +200 -0
  221. package/src/packages/SelfHostedRestClient.ts +134 -0
  222. package/src/packages/SpeakRestClient.ts +79 -0
  223. package/src/packages/index.ts +10 -8
  224. package/dist/main/packages/AbstractRestfulClient.d.ts +0 -26
  225. package/dist/main/packages/AbstractRestfulClient.d.ts.map +0 -1
  226. package/dist/main/packages/AbstractRestfulClient.js +0 -118
  227. package/dist/main/packages/AbstractRestfulClient.js.map +0 -1
  228. package/dist/main/packages/AbstractWsClient.d.ts +0 -10
  229. package/dist/main/packages/AbstractWsClient.d.ts.map +0 -1
  230. package/dist/main/packages/AbstractWsClient.js +0 -33
  231. package/dist/main/packages/AbstractWsClient.js.map +0 -1
  232. package/dist/main/packages/LiveClient.d.ts +0 -28
  233. package/dist/main/packages/LiveClient.d.ts.map +0 -1
  234. package/dist/main/packages/LiveClient.js +0 -110
  235. package/dist/main/packages/LiveClient.js.map +0 -1
  236. package/dist/main/packages/ManageClient.d.ts +0 -97
  237. package/dist/main/packages/ManageClient.d.ts.map +0 -1
  238. package/dist/main/packages/ManageClient.js +0 -463
  239. package/dist/main/packages/ManageClient.js.map +0 -1
  240. package/dist/main/packages/OnPremClient.d.ts +0 -21
  241. package/dist/main/packages/OnPremClient.d.ts.map +0 -1
  242. package/dist/main/packages/OnPremClient.js +0 -99
  243. package/dist/main/packages/OnPremClient.js.map +0 -1
  244. package/dist/main/packages/PrerecordedClient.d.ts +0 -10
  245. package/dist/main/packages/PrerecordedClient.d.ts.map +0 -1
  246. package/dist/main/packages/PrerecordedClient.js +0 -125
  247. package/dist/main/packages/PrerecordedClient.js.map +0 -1
  248. package/dist/main/packages/ReadClient.d.ts +0 -10
  249. package/dist/main/packages/ReadClient.d.ts.map +0 -1
  250. package/dist/main/packages/ReadClient.js +0 -123
  251. package/dist/main/packages/ReadClient.js.map +0 -1
  252. package/dist/main/packages/SpeakClient.d.ts +0 -12
  253. package/dist/main/packages/SpeakClient.d.ts.map +0 -1
  254. package/dist/main/packages/SpeakClient.js +0 -57
  255. package/dist/main/packages/SpeakClient.js.map +0 -1
  256. package/dist/module/packages/AbstractRestfulClient.d.ts +0 -26
  257. package/dist/module/packages/AbstractRestfulClient.d.ts.map +0 -1
  258. package/dist/module/packages/AbstractRestfulClient.js +0 -114
  259. package/dist/module/packages/AbstractRestfulClient.js.map +0 -1
  260. package/dist/module/packages/AbstractWsClient.d.ts +0 -10
  261. package/dist/module/packages/AbstractWsClient.d.ts.map +0 -1
  262. package/dist/module/packages/AbstractWsClient.js +0 -29
  263. package/dist/module/packages/AbstractWsClient.js.map +0 -1
  264. package/dist/module/packages/LiveClient.d.ts +0 -28
  265. package/dist/module/packages/LiveClient.d.ts.map +0 -1
  266. package/dist/module/packages/LiveClient.js +0 -106
  267. package/dist/module/packages/LiveClient.js.map +0 -1
  268. package/dist/module/packages/ManageClient.d.ts +0 -97
  269. package/dist/module/packages/ManageClient.d.ts.map +0 -1
  270. package/dist/module/packages/ManageClient.js +0 -459
  271. package/dist/module/packages/ManageClient.js.map +0 -1
  272. package/dist/module/packages/OnPremClient.d.ts +0 -21
  273. package/dist/module/packages/OnPremClient.d.ts.map +0 -1
  274. package/dist/module/packages/OnPremClient.js +0 -95
  275. package/dist/module/packages/OnPremClient.js.map +0 -1
  276. package/dist/module/packages/PrerecordedClient.d.ts +0 -10
  277. package/dist/module/packages/PrerecordedClient.d.ts.map +0 -1
  278. package/dist/module/packages/PrerecordedClient.js +0 -121
  279. package/dist/module/packages/PrerecordedClient.js.map +0 -1
  280. package/dist/module/packages/ReadClient.d.ts +0 -10
  281. package/dist/module/packages/ReadClient.d.ts.map +0 -1
  282. package/dist/module/packages/ReadClient.js +0 -119
  283. package/dist/module/packages/ReadClient.js.map +0 -1
  284. package/dist/module/packages/SpeakClient.d.ts +0 -12
  285. package/dist/module/packages/SpeakClient.d.ts.map +0 -1
  286. package/dist/module/packages/SpeakClient.js +0 -53
  287. package/dist/module/packages/SpeakClient.js.map +0 -1
  288. package/src/packages/AbstractRestfulClient.ts +0 -160
  289. package/src/packages/AbstractWsClient.ts +0 -42
  290. package/src/packages/LiveClient.ts +0 -140
  291. package/src/packages/ManageClient.ts +0 -584
  292. package/src/packages/OnPremClient.ts +0 -113
  293. package/src/packages/PrerecordedClient.ts +0 -162
  294. package/src/packages/ReadClient.ts +0 -161
  295. 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 { applySettingDefaults, stripTrailingSlash } from "../lib/helpers";
4
- import { DeepgramClientOptions } from "../lib/types";
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 Client.
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
- * An isomorphic Javascript client for interacting with the Deepgram API.
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 baseUrl: URL;
14
-
15
- constructor(protected key: string, protected options: DeepgramClientOptions) {
16
- this.key = key;
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
- this.key = process.env.DEEPGRAM_API_KEY as string;
56
+ key = process.env.DEEPGRAM_API_KEY as string;
20
57
  }
21
58
 
22
- if (!this.key) {
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.options = applySettingDefaults(options, DEFAULT_OPTIONS);
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
- let baseUrlString: string = this.options.global.url;
35
- let proxyUrlString: string;
65
+ options = convertLegacyOptions(options);
36
66
 
37
67
  /**
38
- * Check if the base URL provided is missing a protocol and warn in the console.
68
+ * Apply default options.
39
69
  */
40
- if (!baseUrlString.startsWith("http") && !baseUrlString.startsWith("ws")) {
41
- console.warn(
42
- `The base URL provided does not begin with http, https, ws, or wss and will default to https as standard.`
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
- * Applying proxy to base URL.
128
+ * Version and template the endpoint for input argument..
48
129
  */
49
- if (this.options.restProxy?.url) {
50
- /**
51
- * Prevent client using a real API key when using a proxy configuration.
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
- this.baseUrl = this.resolveBaseUrl(baseUrlString);
74
- }
134
+ /**
135
+ * Create a URL object.
136
+ */
137
+ const url = new URL(endpoint as string, this.baseUrl);
75
138
 
76
- protected resolveBaseUrl(url: string) {
77
- if (!/^https?:\/\//i.test(url)) {
78
- url = "https://" + url;
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 new URL(stripTrailingSlash(url));
146
+ return url;
82
147
  }
83
148
 
84
- protected willProxy() {
85
- const proxyUrl = this.options.restProxy?.url;
86
-
87
- return !!proxyUrl;
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 };