@transcribe-api/sdk 0.1.2 → 0.1.3

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 (3) hide show
  1. package/index.js +47 -28
  2. package/package.json +15 -15
  3. package/worker.js +27 -8
package/index.js CHANGED
@@ -15,24 +15,34 @@ const TERMINAL_JOB_STATUSES = new Set(["completed", "failed", "insufficient_fund
15
15
  const BATCH_MP4_UNSUPPORTED_MESSAGE = "Batch uploads do not support .mp4 for MVP. Supported batch audio formats: mp3, mpeg, mpga, m4a, wav, webm.";
16
16
  const BATCH_UNSUPPORTED_MESSAGE = "Unsupported batch audio format. Supported batch audio formats: mp3, mpeg, mpga, m4a, wav, webm.";
17
17
 
18
- export class TranscribeAPIError extends Error {
19
- constructor(error, { status = null, code = null, extra = null, response = null } = {}) {
20
- super(error);
21
- this.name = "TranscribeAPIError";
22
- this.status = status;
23
- this.code = code;
24
- this.error = error;
25
- this.response = response;
26
- if (extra && typeof extra === "object") {
27
- for (const [key, value] of Object.entries(extra)) {
28
- if (key === "status" || key === "stack" || key === "name") {
29
- continue;
30
- }
31
- this[key] = value;
32
- }
33
- }
34
- }
35
- }
18
+ export class TranscribeAPIError extends Error {
19
+ constructor(message, { status = null, code = null, extra = null, response = null } = {}) {
20
+ super(message);
21
+ this.name = "TranscribeAPIError";
22
+ this.status = status;
23
+ this.code = code;
24
+ this.response = response || { message, ...(code ? { code } : {}) };
25
+ if (extra && typeof extra === "object") {
26
+ for (const [key, value] of Object.entries(extra)) {
27
+ if (key === "status" || key === "stack" || key === "name" || key === "message" || key === "code") {
28
+ continue;
29
+ }
30
+ this[key] = value;
31
+ }
32
+ }
33
+ }
34
+
35
+ toJSON() {
36
+ return {
37
+ message: this.message,
38
+ ...(this.code ? { code: this.code } : {}),
39
+ ...Object.fromEntries(
40
+ Object.entries(this)
41
+ .filter(([key]) => !["name", "status", "code", "response"].includes(key)),
42
+ ),
43
+ };
44
+ }
45
+ }
36
46
 
37
47
  function sleep(ms) {
38
48
  return new Promise((resolve) => setTimeout(resolve, ms));
@@ -803,16 +813,25 @@ async function parseApiResponse(response) {
803
813
  } catch {
804
814
  data = { raw: text };
805
815
  }
806
- if (!response.ok) {
807
- throw new TranscribeAPIError(data.error || text || `HTTP ${response.status}`, {
808
- status: response.status,
809
- code: data.code || null,
810
- extra: data,
811
- response: data,
812
- });
813
- }
814
- return data;
815
- }
816
+ if (!response.ok) {
817
+ const message = data.message || data.error || text || `HTTP ${response.status}`;
818
+ const extra = { ...data };
819
+ delete extra.message;
820
+ delete extra.error;
821
+ delete extra.code;
822
+ throw new TranscribeAPIError(message, {
823
+ status: response.status,
824
+ code: data.code || null,
825
+ extra,
826
+ response: {
827
+ message,
828
+ ...(data.code ? { code: data.code } : {}),
829
+ ...extra,
830
+ },
831
+ });
832
+ }
833
+ return data;
834
+ }
816
835
 
817
836
  function normalizeHeaders(headers = {}) {
818
837
  return Object.fromEntries(
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
- {
2
- "name": "@transcribe-api/sdk",
3
- "version": "0.1.2",
4
- "description": "Official JavaScript SDK for Transcribe API.",
5
- "type": "module",
1
+ {
2
+ "name": "@transcribe-api/sdk",
3
+ "version": "0.1.3",
4
+ "description": "Official JavaScript SDK for Transcribe API.",
5
+ "type": "module",
6
6
  "main": "index.js",
7
7
  "exports": {
8
8
  ".": "./index.js",
@@ -16,13 +16,13 @@
16
16
  "worker.js",
17
17
  "README.md"
18
18
  ],
19
- "keywords": [
20
- "transcription",
21
- "speech-to-text",
22
- "whisper",
23
- "audio",
24
- "api"
25
- ],
26
- "author": "Transcribe API",
27
- "license": "MIT"
28
- }
19
+ "keywords": [
20
+ "transcription",
21
+ "speech-to-text",
22
+ "whisper",
23
+ "audio",
24
+ "api"
25
+ ],
26
+ "author": "Transcribe API",
27
+ "license": "MIT"
28
+ }
package/worker.js CHANGED
@@ -14,22 +14,32 @@ const BATCH_MP4_UNSUPPORTED_MESSAGE = "Batch uploads do not support .mp4 for MVP
14
14
  const BATCH_UNSUPPORTED_MESSAGE = "Unsupported batch audio format. Supported batch audio formats: mp3, mpeg, mpga, m4a, wav, webm.";
15
15
 
16
16
  export class TranscribeAPIError extends Error {
17
- constructor(error, { status = null, code = null, extra = null, response = null } = {}) {
18
- super(error);
17
+ constructor(message, { status = null, code = null, extra = null, response = null } = {}) {
18
+ super(message);
19
19
  this.name = "TranscribeAPIError";
20
20
  this.status = status;
21
21
  this.code = code;
22
- this.error = error;
23
- this.response = response;
22
+ this.response = response || { message, ...(code ? { code } : {}) };
24
23
  if (extra && typeof extra === "object") {
25
24
  for (const [key, value] of Object.entries(extra)) {
26
- if (key === "status" || key === "stack" || key === "name") {
25
+ if (key === "status" || key === "stack" || key === "name" || key === "message" || key === "code") {
27
26
  continue;
28
27
  }
29
28
  this[key] = value;
30
29
  }
31
30
  }
32
31
  }
32
+
33
+ toJSON() {
34
+ return {
35
+ message: this.message,
36
+ ...(this.code ? { code: this.code } : {}),
37
+ ...Object.fromEntries(
38
+ Object.entries(this)
39
+ .filter(([key]) => !["name", "status", "code", "response"].includes(key)),
40
+ ),
41
+ };
42
+ }
33
43
  }
34
44
 
35
45
  function sleep(ms) {
@@ -574,11 +584,20 @@ async function parseApiResponse(response) {
574
584
  data = { raw: text };
575
585
  }
576
586
  if (!response.ok) {
577
- throw new TranscribeAPIError(data.error || text || `HTTP ${response.status}`, {
587
+ const message = data.message || data.error || text || `HTTP ${response.status}`;
588
+ const extra = { ...data };
589
+ delete extra.message;
590
+ delete extra.error;
591
+ delete extra.code;
592
+ throw new TranscribeAPIError(message, {
578
593
  status: response.status,
579
594
  code: data.code || null,
580
- extra: data,
581
- response: data,
595
+ extra,
596
+ response: {
597
+ message,
598
+ ...(data.code ? { code: data.code } : {}),
599
+ ...extra,
600
+ },
582
601
  });
583
602
  }
584
603
  return data;