assemblyai 3.0.0 → 3.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.esm.js CHANGED
@@ -1,24 +1,7 @@
1
- import axios, { isAxiosError } from 'axios';
2
1
  import WebSocket from 'ws';
3
- import { Writable } from 'stream';
2
+ import Stream from 'stream';
4
3
  import fs from 'fs';
5
4
 
6
- function createAxiosClient(params) {
7
- const client = axios.create({
8
- baseURL: params.baseUrl,
9
- headers: { Authorization: params.apiKey },
10
- });
11
- client.interceptors.response.use(undefined, throwApiError);
12
- return client;
13
- }
14
- function throwApiError(error) {
15
- var _a, _b;
16
- if (isAxiosError(error) && ((_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.error)) {
17
- return Promise.reject(new Error(error.response.data.error));
18
- }
19
- return Promise.reject(error);
20
- }
21
-
22
5
  /******************************************************************************
23
6
  Copyright (c) Microsoft Corporation.
24
7
 
@@ -57,36 +40,70 @@ typeof SuppressedError === "function" ? SuppressedError : function (error, suppr
57
40
  class BaseService {
58
41
  /**
59
42
  * Create a new service.
60
- * @param params The AxiosInstance to send HTTP requests to the API.
43
+ * @param params The parameters to use for the service.
61
44
  */
62
- constructor(client) {
63
- this.client = client;
45
+ constructor(params) {
46
+ this.params = params;
47
+ }
48
+ fetch(input, init) {
49
+ var _a;
50
+ return __awaiter(this, void 0, void 0, function* () {
51
+ init = init !== null && init !== void 0 ? init : {};
52
+ init.headers = (_a = init.headers) !== null && _a !== void 0 ? _a : {};
53
+ init.headers = Object.assign({ Authorization: this.params.apiKey, "Content-Type": "application/json" }, init.headers);
54
+ if (!input.startsWith("http"))
55
+ input = this.params.baseUrl + input;
56
+ const response = yield fetch(input, init);
57
+ if (response.status >= 400) {
58
+ let json;
59
+ const text = yield response.text();
60
+ if (text) {
61
+ try {
62
+ json = JSON.parse(text);
63
+ }
64
+ catch (_b) {
65
+ /* empty */
66
+ }
67
+ if (json === null || json === void 0 ? void 0 : json.error)
68
+ throw new Error(json.error);
69
+ throw new Error(text);
70
+ }
71
+ throw new Error(`HTTP Error: ${response.status} ${response.statusText}`);
72
+ }
73
+ return response;
74
+ });
75
+ }
76
+ fetchJson(input, init) {
77
+ return __awaiter(this, void 0, void 0, function* () {
78
+ const response = yield this.fetch(input, init);
79
+ return response.json();
80
+ });
64
81
  }
65
82
  }
66
83
 
67
84
  class LemurService extends BaseService {
68
85
  summary(params) {
69
- return __awaiter(this, void 0, void 0, function* () {
70
- const { data } = yield this.client.post("/lemur/v3/generate/summary", params);
71
- return data;
86
+ return this.fetchJson("/lemur/v3/generate/summary", {
87
+ method: "POST",
88
+ body: JSON.stringify(params),
72
89
  });
73
90
  }
74
91
  questionAnswer(params) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const { data } = yield this.client.post("/lemur/v3/generate/question-answer", params);
77
- return data;
92
+ return this.fetchJson("/lemur/v3/generate/question-answer", {
93
+ method: "POST",
94
+ body: JSON.stringify(params),
78
95
  });
79
96
  }
80
97
  actionItems(params) {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- const { data } = yield this.client.post("/lemur/v3/generate/action-items", params);
83
- return data;
98
+ return this.fetchJson("/lemur/v3/generate/action-items", {
99
+ method: "POST",
100
+ body: JSON.stringify(params),
84
101
  });
85
102
  }
86
103
  task(params) {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- const { data } = yield this.client.post("/lemur/v3/generate/task", params);
89
- return data;
104
+ return this.fetchJson("/lemur/v3/generate/task", {
105
+ method: "POST",
106
+ body: JSON.stringify(params),
90
107
  });
91
108
  }
92
109
  /**
@@ -94,9 +111,8 @@ class LemurService extends BaseService {
94
111
  * @param id ID of the LeMUR request
95
112
  */
96
113
  purgeRequestData(id) {
97
- return __awaiter(this, void 0, void 0, function* () {
98
- const { data } = yield this.client.delete(`/lemur/v3/${id}`);
99
- return data;
114
+ return this.fetchJson(`/lemur/v3/${id}`, {
115
+ method: "DELETE",
100
116
  });
101
117
  }
102
118
  }
@@ -145,12 +161,11 @@ class RealtimeError extends Error {
145
161
  const defaultRealtimeUrl = "wss://api.assemblyai.com/v2/realtime/ws";
146
162
  class RealtimeService {
147
163
  constructor(params) {
148
- var _a, _b, _c;
164
+ var _a, _b;
149
165
  this.listeners = {};
150
166
  this.realtimeUrl = (_a = params.realtimeUrl) !== null && _a !== void 0 ? _a : defaultRealtimeUrl;
151
167
  this.sampleRate = (_b = params.sampleRate) !== null && _b !== void 0 ? _b : 16000;
152
168
  this.wordBoost = params.wordBoost;
153
- this.realtimeUrl = (_c = params.realtimeUrl) !== null && _c !== void 0 ? _c : defaultRealtimeUrl;
154
169
  if ("apiKey" in params)
155
170
  this.apiKey = params.apiKey;
156
171
  if ("token" in params)
@@ -258,7 +273,7 @@ class RealtimeService {
258
273
  this.socket.send(JSON.stringify(payload));
259
274
  }
260
275
  stream() {
261
- const stream = new Writable({
276
+ const stream = new Stream.Writable({
262
277
  write: (chunk, encoding, next) => {
263
278
  this.sendAudio(chunk);
264
279
  next();
@@ -291,30 +306,33 @@ class RealtimeService {
291
306
  }
292
307
  }
293
308
 
294
- class RealtimeServiceFactory {
295
- constructor(client, params) {
296
- this.client = client;
297
- this.params = params;
309
+ class RealtimeServiceFactory extends BaseService {
310
+ constructor(params) {
311
+ super(params);
312
+ this.rtFactoryParams = params;
298
313
  }
299
314
  createService(params) {
300
315
  if (!params)
301
- params = { apiKey: this.params.apiKey };
316
+ params = { apiKey: this.rtFactoryParams.apiKey };
302
317
  else if (!("token" in params) && !params.apiKey) {
303
- params.apiKey = this.params.apiKey;
318
+ params.apiKey = this.rtFactoryParams.apiKey;
304
319
  }
305
320
  return new RealtimeService(params);
306
321
  }
307
322
  createTemporaryToken(params) {
308
323
  return __awaiter(this, void 0, void 0, function* () {
309
- const response = yield this.client.post("/v2/realtime/token", params);
310
- return response.data.token;
324
+ const data = yield this.fetchJson("/v2/realtime/token", {
325
+ method: "POST",
326
+ body: JSON.stringify(params),
327
+ });
328
+ return data.token;
311
329
  });
312
330
  }
313
331
  }
314
332
 
315
333
  class TranscriptService extends BaseService {
316
- constructor(client, files) {
317
- super(client);
334
+ constructor(params, files) {
335
+ super(params);
318
336
  this.files = files;
319
337
  }
320
338
  /**
@@ -331,11 +349,14 @@ class TranscriptService extends BaseService {
331
349
  const uploadUrl = yield this.files.upload(path);
332
350
  params.audio_url = uploadUrl;
333
351
  }
334
- const res = yield this.client.post("/v2/transcript", params);
352
+ const data = yield this.fetchJson("/v2/transcript", {
353
+ method: "POST",
354
+ body: JSON.stringify(params),
355
+ });
335
356
  if ((_a = options === null || options === void 0 ? void 0 : options.poll) !== null && _a !== void 0 ? _a : true) {
336
- return yield this.poll(res.data.id, options);
357
+ return yield this.poll(data.id, options);
337
358
  }
338
- return res.data;
359
+ return data;
339
360
  });
340
361
  }
341
362
  poll(transcriptId, options) {
@@ -364,10 +385,7 @@ class TranscriptService extends BaseService {
364
385
  * @returns A promise that resolves to the transcript.
365
386
  */
366
387
  get(id) {
367
- return __awaiter(this, void 0, void 0, function* () {
368
- const res = yield this.client.get(`/v2/transcript/${id}`);
369
- return res.data;
370
- });
388
+ return this.fetchJson(`/v2/transcript/${id}`);
371
389
  }
372
390
  /**
373
391
  * Retrieves a page of transcript listings.
@@ -376,16 +394,19 @@ class TranscriptService extends BaseService {
376
394
  list(parameters) {
377
395
  return __awaiter(this, void 0, void 0, function* () {
378
396
  let url = "/v2/transcript";
379
- let query;
380
397
  if (typeof parameters === "string") {
381
398
  url = parameters;
382
399
  }
383
400
  else if (parameters) {
384
- query = parameters;
401
+ url = `${url}?${new URLSearchParams(Object.keys(parameters).map((key) => {
402
+ var _a;
403
+ return [
404
+ key,
405
+ ((_a = parameters[key]) === null || _a === void 0 ? void 0 : _a.toString()) || "",
406
+ ];
407
+ }))}`;
385
408
  }
386
- const { data } = yield this.client.get(url, {
387
- params: query,
388
- });
409
+ const data = yield this.fetchJson(url);
389
410
  for (const transcriptListItem of data.transcripts) {
390
411
  transcriptListItem.created = new Date(transcriptListItem.created);
391
412
  if (transcriptListItem.completed) {
@@ -401,27 +422,17 @@ class TranscriptService extends BaseService {
401
422
  * @returns A promise that resolves to the transcript.
402
423
  */
403
424
  delete(id) {
404
- return __awaiter(this, void 0, void 0, function* () {
405
- const res = yield this.client.delete(`/v2/transcript/${id}`);
406
- return res.data;
407
- });
425
+ return this.fetchJson(`/v2/transcript/${id}`, { method: "DELETE" });
408
426
  }
409
427
  /**
410
428
  * Search through the transcript for a specific set of keywords.
411
429
  * You can search for individual words, numbers, or phrases containing up to five words or numbers.
412
430
  * @param id The identifier of the transcript.
413
- * @param id Keywords to search for.
431
+ * @param words Keywords to search for.
414
432
  * @return A promise that resolves to the sentences.
415
433
  */
416
434
  wordSearch(id, words) {
417
- return __awaiter(this, void 0, void 0, function* () {
418
- const { data } = yield this.client.get(`/v2/transcript/${id}/word-search`, {
419
- params: {
420
- words: JSON.stringify(words),
421
- },
422
- });
423
- return data;
424
- });
435
+ return this.fetchJson(`/v2/transcript/${id}/word-search?words=${JSON.stringify(words)}`);
425
436
  }
426
437
  /**
427
438
  * Retrieve all sentences of a transcript.
@@ -429,10 +440,7 @@ class TranscriptService extends BaseService {
429
440
  * @return A promise that resolves to the sentences.
430
441
  */
431
442
  sentences(id) {
432
- return __awaiter(this, void 0, void 0, function* () {
433
- const { data } = yield this.client.get(`/v2/transcript/${id}/sentences`);
434
- return data;
435
- });
443
+ return this.fetchJson(`/v2/transcript/${id}/sentences`);
436
444
  }
437
445
  /**
438
446
  * Retrieve all paragraphs of a transcript.
@@ -440,10 +448,7 @@ class TranscriptService extends BaseService {
440
448
  * @return A promise that resolves to the paragraphs.
441
449
  */
442
450
  paragraphs(id) {
443
- return __awaiter(this, void 0, void 0, function* () {
444
- const { data } = yield this.client.get(`/v2/transcript/${id}/paragraphs`);
445
- return data;
446
- });
451
+ return this.fetchJson(`/v2/transcript/${id}/paragraphs`);
447
452
  }
448
453
  /**
449
454
  * Retrieve subtitles of a transcript.
@@ -453,8 +458,8 @@ class TranscriptService extends BaseService {
453
458
  */
454
459
  subtitles(id, format = "srt") {
455
460
  return __awaiter(this, void 0, void 0, function* () {
456
- const { data } = yield this.client.get(`/v2/transcript/${id}/${format}`);
457
- return data;
461
+ const response = yield this.fetch(`/v2/transcript/${id}/${format}`);
462
+ return yield response.text();
458
463
  });
459
464
  }
460
465
  /**
@@ -463,10 +468,7 @@ class TranscriptService extends BaseService {
463
468
  * @return A promise that resolves to the subtitles text.
464
469
  */
465
470
  redactions(id) {
466
- return __awaiter(this, void 0, void 0, function* () {
467
- const { data } = yield this.client.get(`/v2/transcript/${id}/redacted-audio`);
468
- return data;
469
- });
471
+ return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
470
472
  }
471
473
  }
472
474
  function getPath(path) {
@@ -496,28 +498,33 @@ class FileService extends BaseService {
496
498
  fileData = fs.createReadStream(input);
497
499
  else
498
500
  fileData = input;
499
- const { data } = yield this.client.post("/v2/upload", fileData, {
501
+ const data = yield this.fetchJson("/v2/upload", {
502
+ method: "POST",
503
+ body: fileData,
500
504
  headers: {
501
505
  "Content-Type": "application/octet-stream",
502
506
  },
507
+ duplex: "half",
503
508
  });
504
509
  return data.upload_url;
505
510
  });
506
511
  }
507
512
  }
508
513
 
514
+ const defaultBaseUrl = "https://api.assemblyai.com";
509
515
  class AssemblyAI {
510
516
  /**
511
517
  * Create a new AssemblyAI client.
512
518
  * @param params The parameters for the service, including the API key and base URL, if any.
513
519
  */
514
520
  constructor(params) {
515
- params.baseUrl = params.baseUrl || "https://api.assemblyai.com";
516
- const client = createAxiosClient(params);
517
- this.files = new FileService(client);
518
- this.transcripts = new TranscriptService(client, this.files);
519
- this.lemur = new LemurService(client);
520
- this.realtime = new RealtimeServiceFactory(client, params);
521
+ params.baseUrl = params.baseUrl || defaultBaseUrl;
522
+ if (params.baseUrl && params.baseUrl.endsWith("/"))
523
+ params.baseUrl = params.baseUrl.slice(0, -1);
524
+ this.files = new FileService(params);
525
+ this.transcripts = new TranscriptService(params, this.files);
526
+ this.lemur = new LemurService(params);
527
+ this.realtime = new RealtimeServiceFactory(params);
521
528
  }
522
529
  }
523
530