@cartesia/cartesia-js 1.0.1 → 1.0.2

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,60 @@ 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.onopen = () => {
497
+ __privateSet(this, _isConnected, true);
498
+ emitter.emit("open");
499
+ };
500
+ this.socket.onclose = () => {
501
+ __privateSet(this, _isConnected, false);
502
+ emitter.emit("close");
503
+ };
504
+ return new Promise(
505
+ (resolve, reject) => {
506
+ var _a, _b, _c;
507
+ (_a = this.socket) == null ? void 0 : _a.addEventListener(
508
+ "open",
509
+ () => {
510
+ resolve(getEmitteryCallbacks(emitter));
511
+ },
512
+ {
513
+ once: true
514
+ }
515
+ );
516
+ const aborter = new AbortController();
517
+ (_b = this.socket) == null ? void 0 : _b.addEventListener(
518
+ "error",
519
+ () => {
520
+ aborter.abort();
521
+ reject(new Error("WebSocket failed to connect."));
522
+ },
523
+ {
524
+ signal: aborter.signal
525
+ }
526
+ );
527
+ (_c = this.socket) == null ? void 0 : _c.addEventListener(
528
+ "close",
529
+ () => {
530
+ aborter.abort();
531
+ reject(new Error("WebSocket closed before it could connect."));
532
+ },
533
+ {
534
+ signal: aborter.signal
535
+ }
536
+ );
537
+ }
538
+ );
483
539
  });
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
540
  }
532
541
  /**
533
542
  * Disconnect from the Cartesia streaming WebSocket.
@@ -568,40 +577,43 @@ var TTS = class extends Client {
568
577
  var Voices = class extends Client {
569
578
  list() {
570
579
  return __async(this, null, function* () {
571
- const response = yield this.fetch("/voices");
580
+ const response = yield this._fetch("/voices");
572
581
  return response.json();
573
582
  });
574
583
  }
575
584
  get(voiceId) {
576
585
  return __async(this, null, function* () {
577
- const response = yield this.fetch(`/voices/${voiceId}`);
586
+ const response = yield this._fetch(`/voices/${voiceId}`);
578
587
  return response.json();
579
588
  });
580
589
  }
581
590
  create(voice) {
582
591
  return __async(this, null, function* () {
583
- const response = yield this.fetch("/voices", {
592
+ const response = yield this._fetch("/voices", {
584
593
  method: "POST",
585
594
  body: JSON.stringify(voice)
586
595
  });
587
596
  return response.json();
588
597
  });
589
598
  }
599
+ update(id, voice) {
600
+ return __async(this, null, function* () {
601
+ const response = yield this._fetch(`/voices/${id}`, {
602
+ method: "PATCH",
603
+ body: JSON.stringify(voice)
604
+ });
605
+ return response.json();
606
+ });
607
+ }
590
608
  clone(options) {
591
609
  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
610
  if (options.mode === "clip") {
602
611
  const formData = new FormData();
603
612
  formData.append("clip", options.clip);
604
- const response = yield this.fetch("/voices/clone/clip", {
613
+ if (options.enhance !== void 0) {
614
+ formData.append("enhance", options.enhance.toString());
615
+ }
616
+ const response = yield this._fetch("/voices/clone/clip", {
605
617
  method: "POST",
606
618
  body: formData
607
619
  });
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-NVOCUUOF.js";
4
+ import "../chunk-UCYL2SOX.js";
5
+ import "../chunk-EDAAHENY.js";
6
+ import "../chunk-IZBPLCGW.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,60 @@ 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.onopen = () => {
511
+ __privateSet(this, _isConnected, true);
512
+ emitter.emit("open");
513
+ };
514
+ this.socket.onclose = () => {
515
+ __privateSet(this, _isConnected, false);
516
+ emitter.emit("close");
517
+ };
518
+ return new Promise(
519
+ (resolve, reject) => {
520
+ var _a, _b, _c;
521
+ (_a = this.socket) == null ? void 0 : _a.addEventListener(
522
+ "open",
523
+ () => {
524
+ resolve(getEmitteryCallbacks(emitter));
525
+ },
526
+ {
527
+ once: true
528
+ }
529
+ );
530
+ const aborter = new AbortController();
531
+ (_b = this.socket) == null ? void 0 : _b.addEventListener(
532
+ "error",
533
+ () => {
534
+ aborter.abort();
535
+ reject(new Error("WebSocket failed to connect."));
536
+ },
537
+ {
538
+ signal: aborter.signal
539
+ }
540
+ );
541
+ (_c = this.socket) == null ? void 0 : _c.addEventListener(
542
+ "close",
543
+ () => {
544
+ aborter.abort();
545
+ reject(new Error("WebSocket closed before it could connect."));
546
+ },
547
+ {
548
+ signal: aborter.signal
549
+ }
550
+ );
551
+ }
552
+ );
497
553
  });
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
554
  }
546
555
  /**
547
556
  * Disconnect from the Cartesia streaming WebSocket.
@@ -582,40 +591,43 @@ var TTS = class extends Client {
582
591
  var Voices = class extends Client {
583
592
  list() {
584
593
  return __async(this, null, function* () {
585
- const response = yield this.fetch("/voices");
594
+ const response = yield this._fetch("/voices");
586
595
  return response.json();
587
596
  });
588
597
  }
589
598
  get(voiceId) {
590
599
  return __async(this, null, function* () {
591
- const response = yield this.fetch(`/voices/${voiceId}`);
600
+ const response = yield this._fetch(`/voices/${voiceId}`);
592
601
  return response.json();
593
602
  });
594
603
  }
595
604
  create(voice) {
596
605
  return __async(this, null, function* () {
597
- const response = yield this.fetch("/voices", {
606
+ const response = yield this._fetch("/voices", {
598
607
  method: "POST",
599
608
  body: JSON.stringify(voice)
600
609
  });
601
610
  return response.json();
602
611
  });
603
612
  }
613
+ update(id, voice) {
614
+ return __async(this, null, function* () {
615
+ const response = yield this._fetch(`/voices/${id}`, {
616
+ method: "PATCH",
617
+ body: JSON.stringify(voice)
618
+ });
619
+ return response.json();
620
+ });
621
+ }
604
622
  clone(options) {
605
623
  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
624
  if (options.mode === "clip") {
616
625
  const formData = new FormData();
617
626
  formData.append("clip", options.clip);
618
- const response = yield this.fetch("/voices/clone/clip", {
627
+ if (options.enhance !== void 0) {
628
+ formData.append("enhance", options.enhance.toString());
629
+ }
630
+ const response = yield this._fetch("/voices/clone/clip", {
619
631
  method: "POST",
620
632
  body: formData
621
633
  });
@@ -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-NVOCUUOF.js";
4
+ import "../chunk-UCYL2SOX.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-EDAAHENY.js";
9
+ import "../chunk-IZBPLCGW.js";
10
+ import "../chunk-PISCPZK4.js";
11
11
  import "../chunk-2BFEKY3F.js";
12
12
  import {
13
13
  Player