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