@cartesia/cartesia-js 1.0.1 → 1.0.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.
@@ -43,6 +43,26 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
43
43
  mod
44
44
  ));
45
45
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
46
+ var __async = (__this, __arguments, generator) => {
47
+ return new Promise((resolve, reject) => {
48
+ var fulfilled = (value) => {
49
+ try {
50
+ step(generator.next(value));
51
+ } catch (e) {
52
+ reject(e);
53
+ }
54
+ };
55
+ var rejected = (value) => {
56
+ try {
57
+ step(generator.throw(value));
58
+ } catch (e) {
59
+ reject(e);
60
+ }
61
+ };
62
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
63
+ step((generator = generator.apply(__this, __arguments)).next());
64
+ });
65
+ };
46
66
 
47
67
  // src/lib/client.ts
48
68
  var client_exports = {};
@@ -66,20 +86,25 @@ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
66
86
  // src/lib/client.ts
67
87
  var Client = class {
68
88
  constructor(options = {}) {
69
- if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
89
+ const apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
90
+ if (!apiKey) {
70
91
  throw new Error("Missing Cartesia API key.");
71
92
  }
72
- this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
93
+ this.apiKey = typeof apiKey === "function" ? apiKey : () => __async(this, null, function* () {
94
+ return apiKey;
95
+ });
73
96
  this.baseUrl = options.baseUrl || BASE_URL;
74
97
  }
75
- fetch(path, options = {}) {
76
- const url = constructApiUrl(this.baseUrl, path);
77
- return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
78
- headers: __spreadValues({
79
- "X-API-Key": this.apiKey,
80
- "Cartesia-Version": CARTESIA_VERSION
81
- }, options.headers)
82
- }));
98
+ _fetch(_0) {
99
+ return __async(this, arguments, function* (path, options = {}) {
100
+ const url = constructApiUrl(this.baseUrl, path);
101
+ const headers = new Headers(options.headers);
102
+ headers.set("X-API-Key", yield this.apiKey());
103
+ headers.set("Cartesia-Version", CARTESIA_VERSION);
104
+ return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
105
+ headers
106
+ }));
107
+ });
83
108
  }
84
109
  };
85
110
  // Annotate the CommonJS export names for ESM import in node:
@@ -2,10 +2,10 @@ import { ClientOptions } from '../types/index.cjs';
2
2
  import 'emittery';
3
3
 
4
4
  declare class Client {
5
- apiKey: string;
5
+ apiKey: () => Promise<string>;
6
6
  baseUrl: string;
7
7
  constructor(options?: ClientOptions);
8
- fetch(path: string, options?: RequestInit): Promise<Response>;
8
+ protected _fetch(path: string, options?: RequestInit): Promise<Response>;
9
9
  }
10
10
 
11
11
  export { Client };
@@ -2,10 +2,10 @@ import { ClientOptions } from '../types/index.js';
2
2
  import 'emittery';
3
3
 
4
4
  declare class Client {
5
- apiKey: string;
5
+ apiKey: () => Promise<string>;
6
6
  baseUrl: string;
7
7
  constructor(options?: ClientOptions);
8
- fetch(path: string, options?: RequestInit): Promise<Response>;
8
+ protected _fetch(path: string, options?: RequestInit): Promise<Response>;
9
9
  }
10
10
 
11
11
  export { Client };
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  Client
3
- } from "../chunk-5M33ZF3Y.js";
3
+ } from "../chunk-PISCPZK4.js";
4
4
  import "../chunk-2BFEKY3F.js";
5
5
  import "../chunk-GHY2WEOK.js";
6
6
  export {
@@ -122,20 +122,25 @@ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
122
122
  // src/lib/client.ts
123
123
  var Client = class {
124
124
  constructor(options = {}) {
125
- if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
125
+ const apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
126
+ if (!apiKey) {
126
127
  throw new Error("Missing Cartesia API key.");
127
128
  }
128
- this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
129
+ this.apiKey = typeof apiKey === "function" ? apiKey : () => __async(this, null, function* () {
130
+ return apiKey;
131
+ });
129
132
  this.baseUrl = options.baseUrl || BASE_URL;
130
133
  }
131
- fetch(path, options = {}) {
132
- const url = constructApiUrl(this.baseUrl, path);
133
- return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
134
- headers: __spreadValues({
135
- "X-API-Key": this.apiKey,
136
- "Cartesia-Version": CARTESIA_VERSION
137
- }, options.headers)
138
- }));
134
+ _fetch(_0) {
135
+ return __async(this, arguments, function* (path, options = {}) {
136
+ const url = constructApiUrl(this.baseUrl, path);
137
+ const headers = new Headers(options.headers);
138
+ headers.set("X-API-Key", yield this.apiKey());
139
+ headers.set("Cartesia-Version", CARTESIA_VERSION);
140
+ return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
141
+ headers
142
+ }));
143
+ });
139
144
  }
140
145
  };
141
146
 
@@ -478,56 +483,61 @@ var WebSocket = class extends Client {
478
483
  * @throws {Error} If the WebSocket fails to connect.
479
484
  */
480
485
  connect() {
481
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
482
- websocket: true
486
+ return __async(this, null, function* () {
487
+ const emitter = new import_emittery2.default();
488
+ this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
489
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
490
+ websocket: true
491
+ });
492
+ url.searchParams.set("api_key", yield this.apiKey());
493
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
494
+ return url.toString();
495
+ }));
496
+ this.socket.binaryType = "arraybuffer";
497
+ this.socket.onopen = () => {
498
+ __privateSet(this, _isConnected, true);
499
+ emitter.emit("open");
500
+ };
501
+ this.socket.onclose = () => {
502
+ __privateSet(this, _isConnected, false);
503
+ emitter.emit("close");
504
+ };
505
+ return new Promise(
506
+ (resolve, reject) => {
507
+ var _a, _b, _c;
508
+ (_a = this.socket) == null ? void 0 : _a.addEventListener(
509
+ "open",
510
+ () => {
511
+ resolve(getEmitteryCallbacks(emitter));
512
+ },
513
+ {
514
+ once: true
515
+ }
516
+ );
517
+ const aborter = new AbortController();
518
+ (_b = this.socket) == null ? void 0 : _b.addEventListener(
519
+ "error",
520
+ () => {
521
+ aborter.abort();
522
+ reject(new Error("WebSocket failed to connect."));
523
+ },
524
+ {
525
+ signal: aborter.signal
526
+ }
527
+ );
528
+ (_c = this.socket) == null ? void 0 : _c.addEventListener(
529
+ "close",
530
+ () => {
531
+ aborter.abort();
532
+ reject(new Error("WebSocket closed before it could connect."));
533
+ },
534
+ {
535
+ signal: aborter.signal
536
+ }
537
+ );
538
+ }
539
+ );
483
540
  });
484
- url.searchParams.set("api_key", this.apiKey);
485
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
486
- const emitter = new import_emittery2.default();
487
- this.socket = new import_partysocket.WebSocket(url.toString());
488
- this.socket.onopen = () => {
489
- __privateSet(this, _isConnected, true);
490
- emitter.emit("open");
491
- };
492
- this.socket.onclose = () => {
493
- __privateSet(this, _isConnected, false);
494
- emitter.emit("close");
495
- };
496
- return new Promise(
497
- (resolve, reject) => {
498
- var _a, _b, _c;
499
- (_a = this.socket) == null ? void 0 : _a.addEventListener(
500
- "open",
501
- () => {
502
- resolve(getEmitteryCallbacks(emitter));
503
- },
504
- {
505
- once: true
506
- }
507
- );
508
- const aborter = new AbortController();
509
- (_b = this.socket) == null ? void 0 : _b.addEventListener(
510
- "error",
511
- () => {
512
- aborter.abort();
513
- reject(new Error("WebSocket failed to connect."));
514
- },
515
- {
516
- signal: aborter.signal
517
- }
518
- );
519
- (_c = this.socket) == null ? void 0 : _c.addEventListener(
520
- "close",
521
- () => {
522
- aborter.abort();
523
- reject(new Error("WebSocket closed before it could connect."));
524
- },
525
- {
526
- signal: aborter.signal
527
- }
528
- );
529
- }
530
- );
531
541
  }
532
542
  /**
533
543
  * Disconnect from the Cartesia streaming WebSocket.
@@ -568,40 +578,43 @@ var TTS = class extends Client {
568
578
  var Voices = class extends Client {
569
579
  list() {
570
580
  return __async(this, null, function* () {
571
- const response = yield this.fetch("/voices");
581
+ const response = yield this._fetch("/voices");
572
582
  return response.json();
573
583
  });
574
584
  }
575
585
  get(voiceId) {
576
586
  return __async(this, null, function* () {
577
- const response = yield this.fetch(`/voices/${voiceId}`);
587
+ const response = yield this._fetch(`/voices/${voiceId}`);
578
588
  return response.json();
579
589
  });
580
590
  }
581
591
  create(voice) {
582
592
  return __async(this, null, function* () {
583
- const response = yield this.fetch("/voices", {
593
+ const response = yield this._fetch("/voices", {
584
594
  method: "POST",
585
595
  body: JSON.stringify(voice)
586
596
  });
587
597
  return response.json();
588
598
  });
589
599
  }
600
+ update(id, voice) {
601
+ return __async(this, null, function* () {
602
+ const response = yield this._fetch(`/voices/${id}`, {
603
+ method: "PATCH",
604
+ body: JSON.stringify(voice)
605
+ });
606
+ return response.json();
607
+ });
608
+ }
590
609
  clone(options) {
591
610
  return __async(this, null, function* () {
592
- if (options.mode === "url") {
593
- const response = yield this.fetch(
594
- `/voices/clone/url?link=${options.link}`,
595
- {
596
- method: "POST"
597
- }
598
- );
599
- return response.json();
600
- }
601
611
  if (options.mode === "clip") {
602
612
  const formData = new FormData();
603
613
  formData.append("clip", options.clip);
604
- const response = yield this.fetch("/voices/clone/clip", {
614
+ if (options.enhance !== void 0) {
615
+ formData.append("enhance", options.enhance.toString());
616
+ }
617
+ const response = yield this._fetch("/voices/clone/clip", {
605
618
  method: "POST",
606
619
  body: formData
607
620
  });
@@ -610,6 +623,16 @@ var Voices = class extends Client {
610
623
  throw new Error("Invalid mode for clone()");
611
624
  });
612
625
  }
626
+ mix(options) {
627
+ return __async(this, null, function* () {
628
+ const request = options;
629
+ const response = yield this._fetch("/voices/mix", {
630
+ method: "POST",
631
+ body: JSON.stringify(request)
632
+ });
633
+ return response.json();
634
+ });
635
+ }
613
636
  };
614
637
 
615
638
  // src/lib/index.ts
package/dist/lib/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import {
2
2
  Cartesia
3
- } from "../chunk-OFH3ML4L.js";
4
- import "../chunk-KUSVZXDT.js";
5
- import "../chunk-2NA5SEML.js";
6
- import "../chunk-ASZKHN7Q.js";
7
- import "../chunk-5M33ZF3Y.js";
3
+ } from "../chunk-ZF6HASZT.js";
4
+ import "../chunk-YFN6TFR4.js";
5
+ import "../chunk-NWCW6C7H.js";
6
+ import "../chunk-EUW2435M.js";
7
+ import "../chunk-PISCPZK4.js";
8
8
  import "../chunk-2BFEKY3F.js";
9
9
  import "../chunk-BHY7MNGT.js";
10
10
  import "../chunk-6YQ6KDIQ.js";
@@ -123,20 +123,25 @@ var constructApiUrl = (baseUrl, path, { websocket = false } = {}) => {
123
123
  // src/lib/client.ts
124
124
  var Client = class {
125
125
  constructor(options = {}) {
126
- if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
126
+ const apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
127
+ if (!apiKey) {
127
128
  throw new Error("Missing Cartesia API key.");
128
129
  }
129
- this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
130
+ this.apiKey = typeof apiKey === "function" ? apiKey : () => __async(this, null, function* () {
131
+ return apiKey;
132
+ });
130
133
  this.baseUrl = options.baseUrl || BASE_URL;
131
134
  }
132
- fetch(path, options = {}) {
133
- const url = constructApiUrl(this.baseUrl, path);
134
- return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
135
- headers: __spreadValues({
136
- "X-API-Key": this.apiKey,
137
- "Cartesia-Version": CARTESIA_VERSION
138
- }, options.headers)
139
- }));
135
+ _fetch(_0) {
136
+ return __async(this, arguments, function* (path, options = {}) {
137
+ const url = constructApiUrl(this.baseUrl, path);
138
+ const headers = new Headers(options.headers);
139
+ headers.set("X-API-Key", yield this.apiKey());
140
+ headers.set("Cartesia-Version", CARTESIA_VERSION);
141
+ return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
142
+ headers
143
+ }));
144
+ });
140
145
  }
141
146
  };
142
147
 
@@ -492,56 +497,61 @@ var WebSocket = class extends Client {
492
497
  * @throws {Error} If the WebSocket fails to connect.
493
498
  */
494
499
  connect() {
495
- const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
496
- websocket: true
500
+ return __async(this, null, function* () {
501
+ const emitter = new import_emittery2.default();
502
+ this.socket = new import_partysocket.WebSocket(() => __async(this, null, function* () {
503
+ const url = constructApiUrl(this.baseUrl, "/tts/websocket", {
504
+ websocket: true
505
+ });
506
+ url.searchParams.set("api_key", yield this.apiKey());
507
+ url.searchParams.set("cartesia_version", CARTESIA_VERSION);
508
+ return url.toString();
509
+ }));
510
+ this.socket.binaryType = "arraybuffer";
511
+ this.socket.onopen = () => {
512
+ __privateSet(this, _isConnected, true);
513
+ emitter.emit("open");
514
+ };
515
+ this.socket.onclose = () => {
516
+ __privateSet(this, _isConnected, false);
517
+ emitter.emit("close");
518
+ };
519
+ return new Promise(
520
+ (resolve, reject) => {
521
+ var _a, _b, _c;
522
+ (_a = this.socket) == null ? void 0 : _a.addEventListener(
523
+ "open",
524
+ () => {
525
+ resolve(getEmitteryCallbacks(emitter));
526
+ },
527
+ {
528
+ once: true
529
+ }
530
+ );
531
+ const aborter = new AbortController();
532
+ (_b = this.socket) == null ? void 0 : _b.addEventListener(
533
+ "error",
534
+ () => {
535
+ aborter.abort();
536
+ reject(new Error("WebSocket failed to connect."));
537
+ },
538
+ {
539
+ signal: aborter.signal
540
+ }
541
+ );
542
+ (_c = this.socket) == null ? void 0 : _c.addEventListener(
543
+ "close",
544
+ () => {
545
+ aborter.abort();
546
+ reject(new Error("WebSocket closed before it could connect."));
547
+ },
548
+ {
549
+ signal: aborter.signal
550
+ }
551
+ );
552
+ }
553
+ );
497
554
  });
498
- url.searchParams.set("api_key", this.apiKey);
499
- url.searchParams.set("cartesia_version", CARTESIA_VERSION);
500
- const emitter = new import_emittery2.default();
501
- this.socket = new import_partysocket.WebSocket(url.toString());
502
- this.socket.onopen = () => {
503
- __privateSet(this, _isConnected, true);
504
- emitter.emit("open");
505
- };
506
- this.socket.onclose = () => {
507
- __privateSet(this, _isConnected, false);
508
- emitter.emit("close");
509
- };
510
- return new Promise(
511
- (resolve, reject) => {
512
- var _a, _b, _c;
513
- (_a = this.socket) == null ? void 0 : _a.addEventListener(
514
- "open",
515
- () => {
516
- resolve(getEmitteryCallbacks(emitter));
517
- },
518
- {
519
- once: true
520
- }
521
- );
522
- const aborter = new AbortController();
523
- (_b = this.socket) == null ? void 0 : _b.addEventListener(
524
- "error",
525
- () => {
526
- aborter.abort();
527
- reject(new Error("WebSocket failed to connect."));
528
- },
529
- {
530
- signal: aborter.signal
531
- }
532
- );
533
- (_c = this.socket) == null ? void 0 : _c.addEventListener(
534
- "close",
535
- () => {
536
- aborter.abort();
537
- reject(new Error("WebSocket closed before it could connect."));
538
- },
539
- {
540
- signal: aborter.signal
541
- }
542
- );
543
- }
544
- );
545
555
  }
546
556
  /**
547
557
  * Disconnect from the Cartesia streaming WebSocket.
@@ -582,40 +592,43 @@ var TTS = class extends Client {
582
592
  var Voices = class extends Client {
583
593
  list() {
584
594
  return __async(this, null, function* () {
585
- const response = yield this.fetch("/voices");
595
+ const response = yield this._fetch("/voices");
586
596
  return response.json();
587
597
  });
588
598
  }
589
599
  get(voiceId) {
590
600
  return __async(this, null, function* () {
591
- const response = yield this.fetch(`/voices/${voiceId}`);
601
+ const response = yield this._fetch(`/voices/${voiceId}`);
592
602
  return response.json();
593
603
  });
594
604
  }
595
605
  create(voice) {
596
606
  return __async(this, null, function* () {
597
- const response = yield this.fetch("/voices", {
607
+ const response = yield this._fetch("/voices", {
598
608
  method: "POST",
599
609
  body: JSON.stringify(voice)
600
610
  });
601
611
  return response.json();
602
612
  });
603
613
  }
614
+ update(id, voice) {
615
+ return __async(this, null, function* () {
616
+ const response = yield this._fetch(`/voices/${id}`, {
617
+ method: "PATCH",
618
+ body: JSON.stringify(voice)
619
+ });
620
+ return response.json();
621
+ });
622
+ }
604
623
  clone(options) {
605
624
  return __async(this, null, function* () {
606
- if (options.mode === "url") {
607
- const response = yield this.fetch(
608
- `/voices/clone/url?link=${options.link}`,
609
- {
610
- method: "POST"
611
- }
612
- );
613
- return response.json();
614
- }
615
625
  if (options.mode === "clip") {
616
626
  const formData = new FormData();
617
627
  formData.append("clip", options.clip);
618
- const response = yield this.fetch("/voices/clone/clip", {
628
+ if (options.enhance !== void 0) {
629
+ formData.append("enhance", options.enhance.toString());
630
+ }
631
+ const response = yield this._fetch("/voices/clone/clip", {
619
632
  method: "POST",
620
633
  body: formData
621
634
  });
@@ -624,6 +637,16 @@ var Voices = class extends Client {
624
637
  throw new Error("Invalid mode for clone()");
625
638
  });
626
639
  }
640
+ mix(options) {
641
+ return __async(this, null, function* () {
642
+ const request = options;
643
+ const response = yield this._fetch("/voices/mix", {
644
+ method: "POST",
645
+ body: JSON.stringify(request)
646
+ });
647
+ return response.json();
648
+ });
649
+ }
627
650
  };
628
651
 
629
652
  // src/lib/index.ts
@@ -3,7 +3,7 @@ import { StreamRequest } from '../types/index.cjs';
3
3
  import 'emittery';
4
4
 
5
5
  type UseTTSOptions = {
6
- apiKey: string | null;
6
+ apiKey: string | (() => Promise<string>) | null;
7
7
  baseUrl?: string;
8
8
  sampleRate: number;
9
9
  onError?: (error: Error) => void;
@@ -3,7 +3,7 @@ import { StreamRequest } from '../types/index.js';
3
3
  import 'emittery';
4
4
 
5
5
  type UseTTSOptions = {
6
- apiKey: string | null;
6
+ apiKey: string | (() => Promise<string>) | null;
7
7
  baseUrl?: string;
8
8
  sampleRate: number;
9
9
  onError?: (error: Error) => void;
@@ -1,13 +1,13 @@
1
1
  import {
2
2
  Cartesia
3
- } from "../chunk-OFH3ML4L.js";
4
- import "../chunk-KUSVZXDT.js";
3
+ } from "../chunk-ZF6HASZT.js";
4
+ import "../chunk-YFN6TFR4.js";
5
5
  import {
6
6
  pingServer
7
7
  } from "../chunk-NQVZNVOU.js";
8
- import "../chunk-2NA5SEML.js";
9
- import "../chunk-ASZKHN7Q.js";
10
- import "../chunk-5M33ZF3Y.js";
8
+ import "../chunk-NWCW6C7H.js";
9
+ import "../chunk-EUW2435M.js";
10
+ import "../chunk-PISCPZK4.js";
11
11
  import "../chunk-2BFEKY3F.js";
12
12
  import {
13
13
  Player