assemblyai 2.0.2 → 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/README.md CHANGED
@@ -40,7 +40,7 @@ bun add assemblyai
40
40
  Import the AssemblyAI package and create an AssemblyAI object with your API key:
41
41
 
42
42
  ```javascript
43
- import AssemblyAI from "assemblyai";
43
+ import { AssemblyAI } from "assemblyai";
44
44
 
45
45
  const client = new AssemblyAI({
46
46
  apiKey: process.env.ASSEMBLYAI_API_KEY,
@@ -94,7 +94,7 @@ const transcript = await client.transcripts.get(transcript.id)
94
94
 
95
95
  ## List transcripts
96
96
 
97
- This will return a paged list of transcripts that you have transcript.
97
+ This will return a page of transcripts that you have transcript.
98
98
 
99
99
  ```javascript
100
100
  const page = await client.transcripts.list()
package/dist/index.d.ts CHANGED
@@ -1,4 +1,2 @@
1
- import { AssemblyAI } from "./services";
2
- export * from "./services";
3
1
  export type * from "./types";
4
- export default AssemblyAI;
2
+ export * from "./services";
package/dist/index.esm.js CHANGED
@@ -1,23 +1,6 @@
1
- import axios, { isAxiosError } from 'axios';
2
1
  import WebSocket from 'ws';
3
- import { Writable } from 'stream';
4
- import { readFile } from 'fs/promises';
5
-
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
- }
2
+ import Stream from 'stream';
3
+ import fs from 'fs';
21
4
 
22
5
  /******************************************************************************
23
6
  Copyright (c) Microsoft Corporation.
@@ -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,20 +385,28 @@ 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
- // TODO: add options overload to support list querystring parameters
373
390
  /**
374
- * Retrieves a paged list of transcript listings.
375
- * @param nextUrl The URL to retrieve the transcript list from. If not provided, the first page will be retrieved.
376
- * @returns
391
+ * Retrieves a page of transcript listings.
392
+ * @param parameters The parameters to filter the transcript list by, or the URL to retrieve the transcript list from.
377
393
  */
378
- list(nextUrl) {
394
+ list(parameters) {
379
395
  return __awaiter(this, void 0, void 0, function* () {
380
- const { data } = yield this.client.get(nextUrl !== null && nextUrl !== void 0 ? nextUrl : "/v2/transcript");
396
+ let url = "/v2/transcript";
397
+ if (typeof parameters === "string") {
398
+ url = parameters;
399
+ }
400
+ else if (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
+ }))}`;
408
+ }
409
+ const data = yield this.fetchJson(url);
381
410
  for (const transcriptListItem of data.transcripts) {
382
411
  transcriptListItem.created = new Date(transcriptListItem.created);
383
412
  if (transcriptListItem.completed) {
@@ -393,27 +422,17 @@ class TranscriptService extends BaseService {
393
422
  * @returns A promise that resolves to the transcript.
394
423
  */
395
424
  delete(id) {
396
- return __awaiter(this, void 0, void 0, function* () {
397
- const res = yield this.client.delete(`/v2/transcript/${id}`);
398
- return res.data;
399
- });
425
+ return this.fetchJson(`/v2/transcript/${id}`, { method: "DELETE" });
400
426
  }
401
427
  /**
402
428
  * Search through the transcript for a specific set of keywords.
403
429
  * You can search for individual words, numbers, or phrases containing up to five words or numbers.
404
430
  * @param id The identifier of the transcript.
405
- * @param id Keywords to search for.
431
+ * @param words Keywords to search for.
406
432
  * @return A promise that resolves to the sentences.
407
433
  */
408
434
  wordSearch(id, words) {
409
- return __awaiter(this, void 0, void 0, function* () {
410
- const { data } = yield this.client.get(`/v2/transcript/${id}/word-search`, {
411
- params: {
412
- words: JSON.stringify(words),
413
- },
414
- });
415
- return data;
416
- });
435
+ return this.fetchJson(`/v2/transcript/${id}/word-search?words=${JSON.stringify(words)}`);
417
436
  }
418
437
  /**
419
438
  * Retrieve all sentences of a transcript.
@@ -421,10 +440,7 @@ class TranscriptService extends BaseService {
421
440
  * @return A promise that resolves to the sentences.
422
441
  */
423
442
  sentences(id) {
424
- return __awaiter(this, void 0, void 0, function* () {
425
- const { data } = yield this.client.get(`/v2/transcript/${id}/sentences`);
426
- return data;
427
- });
443
+ return this.fetchJson(`/v2/transcript/${id}/sentences`);
428
444
  }
429
445
  /**
430
446
  * Retrieve all paragraphs of a transcript.
@@ -432,10 +448,7 @@ class TranscriptService extends BaseService {
432
448
  * @return A promise that resolves to the paragraphs.
433
449
  */
434
450
  paragraphs(id) {
435
- return __awaiter(this, void 0, void 0, function* () {
436
- const { data } = yield this.client.get(`/v2/transcript/${id}/paragraphs`);
437
- return data;
438
- });
451
+ return this.fetchJson(`/v2/transcript/${id}/paragraphs`);
439
452
  }
440
453
  /**
441
454
  * Retrieve subtitles of a transcript.
@@ -445,8 +458,8 @@ class TranscriptService extends BaseService {
445
458
  */
446
459
  subtitles(id, format = "srt") {
447
460
  return __awaiter(this, void 0, void 0, function* () {
448
- const { data } = yield this.client.get(`/v2/transcript/${id}/${format}`);
449
- return data;
461
+ const response = yield this.fetch(`/v2/transcript/${id}/${format}`);
462
+ return yield response.text();
450
463
  });
451
464
  }
452
465
  /**
@@ -455,10 +468,7 @@ class TranscriptService extends BaseService {
455
468
  * @return A promise that resolves to the subtitles text.
456
469
  */
457
470
  redactions(id) {
458
- return __awaiter(this, void 0, void 0, function* () {
459
- const { data } = yield this.client.get(`/v2/transcript/${id}/redacted-audio`);
460
- return data;
461
- });
471
+ return this.fetchJson(`/v2/transcript/${id}/redacted-audio`);
462
472
  }
463
473
  }
464
474
  function getPath(path) {
@@ -478,49 +488,44 @@ function getPath(path) {
478
488
  class FileService extends BaseService {
479
489
  /**
480
490
  * Upload a local file to AssemblyAI.
481
- * @param path The local file to upload.
491
+ * @param input The local file path to upload, or a stream or buffer of the file to upload.
482
492
  * @return A promise that resolves to the uploaded file URL.
483
493
  */
484
- upload(path) {
494
+ upload(input) {
485
495
  return __awaiter(this, void 0, void 0, function* () {
486
- const file = yield readFile(path);
487
- const { data } = yield this.client.post("/v2/upload", file, {
496
+ let fileData;
497
+ if (typeof input === "string")
498
+ fileData = fs.createReadStream(input);
499
+ else
500
+ fileData = input;
501
+ const data = yield this.fetchJson("/v2/upload", {
502
+ method: "POST",
503
+ body: fileData,
488
504
  headers: {
489
505
  "Content-Type": "application/octet-stream",
490
506
  },
507
+ duplex: "half",
491
508
  });
492
509
  return data.upload_url;
493
510
  });
494
511
  }
495
512
  }
496
513
 
514
+ const defaultBaseUrl = "https://api.assemblyai.com";
497
515
  class AssemblyAI {
498
516
  /**
499
517
  * Create a new AssemblyAI client.
500
518
  * @param params The parameters for the service, including the API key and base URL, if any.
501
519
  */
502
520
  constructor(params) {
503
- params.baseUrl = params.baseUrl || "https://api.assemblyai.com";
504
- const client = createAxiosClient(params);
505
- this.files = new FileService(client);
506
- this.transcripts = new TranscriptService(client, this.files);
507
- this.lemur = new LemurService(client);
508
- 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);
509
528
  }
510
529
  }
511
530
 
512
- var services = /*#__PURE__*/Object.freeze({
513
- __proto__: null,
514
- AssemblyAI: AssemblyAI,
515
- FileService: FileService,
516
- LemurService: LemurService,
517
- RealtimeService: RealtimeService,
518
- RealtimeServiceFactory: RealtimeServiceFactory,
519
- TranscriptService: TranscriptService
520
- });
521
-
522
- class AssemblyAIExports extends AssemblyAI {
523
- }
524
- module.exports = Object.assign(AssemblyAIExports, services);
525
-
526
- export { AssemblyAI, FileService, LemurService, RealtimeService, RealtimeServiceFactory, TranscriptService, AssemblyAI as default };
531
+ export { AssemblyAI, FileService, LemurService, RealtimeService, RealtimeServiceFactory, TranscriptService };