@cartesia/cartesia-js 0.0.3 → 1.0.0-alpha.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.
Files changed (103) hide show
  1. package/.turbo/turbo-build.log +68 -38
  2. package/CHANGELOG.md +12 -0
  3. package/README.md +123 -16
  4. package/dist/chunk-3FL2SNIR.js +17 -0
  5. package/dist/chunk-3GBZUGUD.js +17 -0
  6. package/dist/chunk-4RMSIQLG.js +25 -0
  7. package/dist/chunk-BCQ63627.js +32 -0
  8. package/dist/chunk-JOHSCOLW.js +106 -0
  9. package/dist/chunk-LYPTISWL.js +75 -0
  10. package/dist/chunk-NDNN326Q.js +207 -0
  11. package/dist/chunk-WBK6LLXX.js +58 -0
  12. package/dist/chunk-WE63M7PJ.js +119 -0
  13. package/dist/{chunk-HNLIBHEN.mjs → chunk-WIFMLPT5.js} +31 -16
  14. package/dist/chunk-X7SJMF2R.js +22 -0
  15. package/dist/index.cjs +652 -0
  16. package/dist/index.d.cts +10 -0
  17. package/dist/index.d.ts +10 -0
  18. package/dist/index.js +20 -0
  19. package/dist/lib/client.cjs +89 -0
  20. package/dist/lib/client.d.cts +11 -0
  21. package/dist/lib/client.d.ts +2 -0
  22. package/dist/lib/client.js +7 -42
  23. package/dist/lib/constants.cjs +42 -0
  24. package/dist/lib/constants.d.cts +4 -0
  25. package/dist/lib/constants.d.ts +2 -3
  26. package/dist/lib/constants.js +8 -37
  27. package/dist/lib/index.cjs +531 -0
  28. package/dist/lib/index.d.cts +16 -0
  29. package/dist/lib/index.d.ts +6 -2
  30. package/dist/lib/index.js +13 -409
  31. package/dist/react/index.cjs +846 -0
  32. package/dist/react/index.d.cts +33 -0
  33. package/dist/react/index.d.ts +20 -13
  34. package/dist/react/index.js +161 -501
  35. package/dist/react/utils.cjs +57 -0
  36. package/dist/react/utils.d.cts +7 -0
  37. package/dist/react/utils.d.ts +7 -0
  38. package/dist/react/utils.js +7 -0
  39. package/dist/tts/index.cjs +470 -0
  40. package/dist/tts/index.d.cts +17 -0
  41. package/dist/tts/index.d.ts +17 -0
  42. package/dist/tts/index.js +12 -0
  43. package/dist/tts/player.cjs +198 -0
  44. package/dist/tts/player.d.cts +43 -0
  45. package/dist/tts/player.d.ts +43 -0
  46. package/dist/tts/player.js +8 -0
  47. package/dist/tts/source.cjs +167 -0
  48. package/dist/tts/source.d.cts +53 -0
  49. package/dist/tts/source.d.ts +53 -0
  50. package/dist/tts/source.js +7 -0
  51. package/dist/{audio/utils.js → tts/utils.cjs} +13 -54
  52. package/dist/tts/utils.d.cts +67 -0
  53. package/dist/tts/utils.d.ts +67 -0
  54. package/dist/{audio/utils.mjs → tts/utils.js} +2 -6
  55. package/dist/tts/websocket.cjs +453 -0
  56. package/dist/tts/websocket.d.cts +53 -0
  57. package/dist/tts/websocket.d.ts +53 -0
  58. package/dist/tts/websocket.js +11 -0
  59. package/dist/types/index.cjs +18 -0
  60. package/dist/types/index.d.cts +55 -0
  61. package/dist/types/index.d.ts +50 -1
  62. package/dist/types/index.js +1 -18
  63. package/dist/voices/index.cjs +155 -0
  64. package/dist/voices/index.d.cts +12 -0
  65. package/dist/voices/index.d.ts +12 -0
  66. package/dist/voices/index.js +9 -0
  67. package/package.json +11 -7
  68. package/src/index.ts +4 -0
  69. package/src/lib/client.ts +14 -1
  70. package/src/lib/constants.ts +13 -3
  71. package/src/lib/index.ts +6 -3
  72. package/src/react/index.ts +167 -75
  73. package/src/react/utils.ts +11 -0
  74. package/src/tts/index.ts +17 -0
  75. package/src/tts/player.ts +109 -0
  76. package/src/tts/source.ts +98 -0
  77. package/src/{audio → tts}/utils.ts +19 -97
  78. package/src/tts/websocket.ts +210 -0
  79. package/src/types/index.ts +63 -0
  80. package/src/voices/index.ts +47 -0
  81. package/dist/audio/index.d.mts +0 -5
  82. package/dist/audio/index.d.ts +0 -5
  83. package/dist/audio/index.js +0 -396
  84. package/dist/audio/index.mjs +0 -9
  85. package/dist/audio/utils.d.mts +0 -5
  86. package/dist/audio/utils.d.ts +0 -5
  87. package/dist/chunk-3CYTAFLF.mjs +0 -262
  88. package/dist/chunk-FRIBQZPN.mjs +0 -113
  89. package/dist/chunk-XSFPHPPG.mjs +0 -18
  90. package/dist/index-DSBmfK9-.d.mts +0 -158
  91. package/dist/index-qwAyxV5I.d.ts +0 -158
  92. package/dist/lib/client.d.mts +0 -9
  93. package/dist/lib/client.mjs +0 -7
  94. package/dist/lib/constants.d.mts +0 -5
  95. package/dist/lib/constants.mjs +0 -10
  96. package/dist/lib/index.d.mts +0 -12
  97. package/dist/lib/index.mjs +0 -19
  98. package/dist/react/index.d.mts +0 -26
  99. package/dist/react/index.mjs +0 -130
  100. package/dist/types/index.d.mts +0 -6
  101. package/index.ts +0 -3
  102. package/src/audio/index.ts +0 -282
  103. /package/dist/{types/index.mjs → chunk-FXPGR372.js} +0 -0
@@ -0,0 +1,53 @@
1
+ import * as emittery from 'emittery';
2
+ import { WebSocket as WebSocket$1 } from 'partysocket';
3
+ import { Client } from '../lib/client.js';
4
+ import { WebSocketOptions, StreamRequest, EmitteryCallbacks, ConnectionEventData } from '../types/index.js';
5
+ import Source from './source.js';
6
+
7
+ declare class WebSocket extends Client {
8
+ #private;
9
+ socket?: WebSocket$1;
10
+ /**
11
+ * Create a new WebSocket client.
12
+ *
13
+ * @param args - Arguments to pass to the Client constructor.
14
+ */
15
+ constructor({ sampleRate }: WebSocketOptions, ...args: ConstructorParameters<typeof Client>);
16
+ /**
17
+ * Send a message over the WebSocket in order to start a stream.
18
+ *
19
+ * @param inputs - Stream options.
20
+ * @param options - Options for the stream.
21
+ * @param options.timeout - The maximum time to wait for a chunk before cancelling the stream.
22
+ * If `0`, the stream will not time out.
23
+ * @returns A Source object that can be passed to a Player to play the audio.
24
+ */
25
+ send(inputs: StreamRequest["inputs"], { timeout }?: StreamRequest["options"]): {
26
+ on: <Name extends keyof emittery.OmnipresentEventData | "message">(eventName: Name | readonly Name[], listener: (eventData: ({
27
+ message: string;
28
+ } & emittery.OmnipresentEventData)[Name]) => void | Promise<void>) => emittery.UnsubscribeFunction;
29
+ off: <Name_1 extends keyof emittery.OmnipresentEventData | "message">(eventName: Name_1 | readonly Name_1[], listener: (eventData: ({
30
+ message: string;
31
+ } & emittery.OmnipresentEventData)[Name_1]) => void | Promise<void>) => void;
32
+ once: <Name_2 extends keyof emittery.OmnipresentEventData | "message">(eventName: Name_2 | readonly Name_2[]) => emittery.EmitteryOncePromise<({
33
+ message: string;
34
+ } & emittery.OmnipresentEventData)[Name_2]>;
35
+ events: <Name_3 extends "message">(eventName: Name_3 | readonly Name_3[]) => AsyncIterableIterator<{
36
+ message: string;
37
+ }[Name_3]>;
38
+ source: Source;
39
+ };
40
+ /**
41
+ * Authenticate and connect to a Cartesia streaming WebSocket.
42
+ *
43
+ * @returns A promise that resolves when the WebSocket is connected.
44
+ * @throws {Error} If the WebSocket fails to connect.
45
+ */
46
+ connect(): Promise<EmitteryCallbacks<ConnectionEventData>>;
47
+ /**
48
+ * Disconnect from the Cartesia streaming WebSocket.
49
+ */
50
+ disconnect(): void;
51
+ }
52
+
53
+ export { WebSocket as default };
@@ -0,0 +1,11 @@
1
+ import {
2
+ WebSocket
3
+ } from "../chunk-NDNN326Q.js";
4
+ import "../chunk-JOHSCOLW.js";
5
+ import "../chunk-BCQ63627.js";
6
+ import "../chunk-3GBZUGUD.js";
7
+ import "../chunk-LYPTISWL.js";
8
+ import "../chunk-WIFMLPT5.js";
9
+ export {
10
+ WebSocket as default
11
+ };
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/types/index.ts
17
+ var types_exports = {};
18
+ module.exports = __toCommonJS(types_exports);
@@ -0,0 +1,55 @@
1
+ import emittery__default from 'emittery';
2
+
3
+ interface ClientOptions {
4
+ apiKey?: string;
5
+ baseUrl?: string;
6
+ }
7
+ type Sentinel = null;
8
+ type Chunk = string | Sentinel;
9
+ type ConnectionEventData = {
10
+ open: never;
11
+ close: never;
12
+ };
13
+ type StreamRequest = {
14
+ inputs: object;
15
+ options: {
16
+ timeout?: number;
17
+ };
18
+ };
19
+ type EmitteryCallbacks<T> = {
20
+ on: emittery__default<T>["on"];
21
+ off: emittery__default<T>["off"];
22
+ once: emittery__default<T>["once"];
23
+ events: emittery__default<T>["events"];
24
+ };
25
+ type CloneOptions = {
26
+ mode: "url";
27
+ link: string;
28
+ } | {
29
+ mode: "clip";
30
+ clip: Blob;
31
+ };
32
+ type Voice = {
33
+ id: string;
34
+ name: string;
35
+ description: string;
36
+ embedding: number[];
37
+ is_public: boolean;
38
+ user_id: string;
39
+ created_at: string;
40
+ };
41
+ type CreateVoice = Pick<Voice, "name" | "description" | "embedding"> & Partial<Omit<Voice, "name" | "description" | "embedding">>;
42
+ type CloneResponse = {
43
+ embedding: number[];
44
+ };
45
+ type WebSocketOptions = {
46
+ sampleRate: number;
47
+ };
48
+ type SourceEventData = {
49
+ enqueue: never;
50
+ close: never;
51
+ wait: never;
52
+ read: never;
53
+ };
54
+
55
+ export type { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Sentinel, SourceEventData, StreamRequest, Voice, WebSocketOptions };
@@ -1,6 +1,55 @@
1
+ import emittery__default from 'emittery';
2
+
1
3
  interface ClientOptions {
2
4
  apiKey?: string;
3
5
  baseUrl?: string;
4
6
  }
7
+ type Sentinel = null;
8
+ type Chunk = string | Sentinel;
9
+ type ConnectionEventData = {
10
+ open: never;
11
+ close: never;
12
+ };
13
+ type StreamRequest = {
14
+ inputs: object;
15
+ options: {
16
+ timeout?: number;
17
+ };
18
+ };
19
+ type EmitteryCallbacks<T> = {
20
+ on: emittery__default<T>["on"];
21
+ off: emittery__default<T>["off"];
22
+ once: emittery__default<T>["once"];
23
+ events: emittery__default<T>["events"];
24
+ };
25
+ type CloneOptions = {
26
+ mode: "url";
27
+ link: string;
28
+ } | {
29
+ mode: "clip";
30
+ clip: Blob;
31
+ };
32
+ type Voice = {
33
+ id: string;
34
+ name: string;
35
+ description: string;
36
+ embedding: number[];
37
+ is_public: boolean;
38
+ user_id: string;
39
+ created_at: string;
40
+ };
41
+ type CreateVoice = Pick<Voice, "name" | "description" | "embedding"> & Partial<Omit<Voice, "name" | "description" | "embedding">>;
42
+ type CloneResponse = {
43
+ embedding: number[];
44
+ };
45
+ type WebSocketOptions = {
46
+ sampleRate: number;
47
+ };
48
+ type SourceEventData = {
49
+ enqueue: never;
50
+ close: never;
51
+ wait: never;
52
+ read: never;
53
+ };
5
54
 
6
- export type { ClientOptions };
55
+ export type { Chunk, ClientOptions, CloneOptions, CloneResponse, ConnectionEventData, CreateVoice, EmitteryCallbacks, Sentinel, SourceEventData, StreamRequest, Voice, WebSocketOptions };
@@ -1,18 +1 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __copyProps = (to, from, except, desc) => {
7
- if (from && typeof from === "object" || typeof from === "function") {
8
- for (let key of __getOwnPropNames(from))
9
- if (!__hasOwnProp.call(to, key) && key !== except)
10
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
- }
12
- return to;
13
- };
14
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
-
16
- // src/types/index.ts
17
- var types_exports = {};
18
- module.exports = __toCommonJS(types_exports);
1
+ import "../chunk-FXPGR372.js";
@@ -0,0 +1,155 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __defProps = Object.defineProperties;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
9
+ var __getProtoOf = Object.getPrototypeOf;
10
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
11
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
12
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
13
+ var __spreadValues = (a, b) => {
14
+ for (var prop in b || (b = {}))
15
+ if (__hasOwnProp.call(b, prop))
16
+ __defNormalProp(a, prop, b[prop]);
17
+ if (__getOwnPropSymbols)
18
+ for (var prop of __getOwnPropSymbols(b)) {
19
+ if (__propIsEnum.call(b, prop))
20
+ __defNormalProp(a, prop, b[prop]);
21
+ }
22
+ return a;
23
+ };
24
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
25
+ var __export = (target, all) => {
26
+ for (var name in all)
27
+ __defProp(target, name, { get: all[name], enumerable: true });
28
+ };
29
+ var __copyProps = (to, from, except, desc) => {
30
+ if (from && typeof from === "object" || typeof from === "function") {
31
+ for (let key of __getOwnPropNames(from))
32
+ if (!__hasOwnProp.call(to, key) && key !== except)
33
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
34
+ }
35
+ return to;
36
+ };
37
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
38
+ // If the importer is in node compatibility mode or this is not an ESM
39
+ // file that has been converted to a CommonJS file using a Babel-
40
+ // compatible transform (i.e. "__esModule" has not been set), then set
41
+ // "default" to the CommonJS "module.exports" for node compatibility.
42
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
43
+ mod
44
+ ));
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
+ };
66
+
67
+ // src/voices/index.ts
68
+ var voices_exports = {};
69
+ __export(voices_exports, {
70
+ default: () => Voices
71
+ });
72
+ module.exports = __toCommonJS(voices_exports);
73
+
74
+ // src/lib/client.ts
75
+ var import_cross_fetch = __toESM(require("cross-fetch"), 1);
76
+
77
+ // src/lib/constants.ts
78
+ var BASE_URL = "https://api.cartesia.ai/v0";
79
+ var constructApiUrl = (baseUrl, path, protocol) => {
80
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
81
+ if (!protocol) {
82
+ return new URL(`${baseUrl}${normalizedPath}`);
83
+ }
84
+ if (!["http", "ws"].includes(protocol)) {
85
+ throw new Error(`Invalid protocol: ${protocol}`);
86
+ }
87
+ return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
88
+ };
89
+
90
+ // src/lib/client.ts
91
+ var Client = class {
92
+ constructor(options = {}) {
93
+ if (!(options.apiKey || process.env.CARTESIA_API_KEY)) {
94
+ throw new Error("Missing Cartesia API key.");
95
+ }
96
+ this.apiKey = options.apiKey || process.env.CARTESIA_API_KEY;
97
+ this.baseUrl = options.baseUrl || BASE_URL;
98
+ }
99
+ fetch(path, options = {}) {
100
+ const url = constructApiUrl(this.baseUrl, path);
101
+ return (0, import_cross_fetch.default)(url.toString(), __spreadProps(__spreadValues({}, options), {
102
+ headers: __spreadValues({
103
+ "X-API-KEY": this.apiKey
104
+ }, options.headers)
105
+ }));
106
+ }
107
+ };
108
+
109
+ // src/voices/index.ts
110
+ var Voices = class extends Client {
111
+ list() {
112
+ return __async(this, null, function* () {
113
+ const response = yield this.fetch("/voices");
114
+ return response.json();
115
+ });
116
+ }
117
+ get(voiceId) {
118
+ return __async(this, null, function* () {
119
+ const response = yield this.fetch(`/voices/${voiceId}`);
120
+ return response.json();
121
+ });
122
+ }
123
+ create(voice) {
124
+ return __async(this, null, function* () {
125
+ const response = yield this.fetch("/voices", {
126
+ method: "POST",
127
+ body: JSON.stringify(voice)
128
+ });
129
+ return response.json();
130
+ });
131
+ }
132
+ clone(options) {
133
+ return __async(this, null, function* () {
134
+ if (options.mode === "url") {
135
+ const response = yield this.fetch(
136
+ `/voices/clone/url?link=${options.link}`,
137
+ {
138
+ method: "POST"
139
+ }
140
+ );
141
+ return response.json();
142
+ }
143
+ if (options.mode === "clip") {
144
+ const formData = new FormData();
145
+ formData.append("clip", options.clip);
146
+ const response = yield this.fetch("/voices/clone/clip", {
147
+ method: "POST",
148
+ body: formData
149
+ });
150
+ return response.json();
151
+ }
152
+ throw new Error("Invalid mode for clone()");
153
+ });
154
+ }
155
+ };
@@ -0,0 +1,12 @@
1
+ import { Client } from '../lib/client.cjs';
2
+ import { Voice, CreateVoice, CloneOptions, CloneResponse } from '../types/index.cjs';
3
+ import 'emittery';
4
+
5
+ declare class Voices extends Client {
6
+ list(): Promise<Voice[]>;
7
+ get(voiceId: string): Promise<Voice>;
8
+ create(voice: CreateVoice): Promise<Voice>;
9
+ clone(options: CloneOptions): Promise<CloneResponse>;
10
+ }
11
+
12
+ export { Voices as default };
@@ -0,0 +1,12 @@
1
+ import { Client } from '../lib/client.js';
2
+ import { Voice, CreateVoice, CloneOptions, CloneResponse } from '../types/index.js';
3
+ import 'emittery';
4
+
5
+ declare class Voices extends Client {
6
+ list(): Promise<Voice[]>;
7
+ get(voiceId: string): Promise<Voice>;
8
+ create(voice: CreateVoice): Promise<Voice>;
9
+ clone(options: CloneOptions): Promise<CloneResponse>;
10
+ }
11
+
12
+ export { Voices as default };
@@ -0,0 +1,9 @@
1
+ import {
2
+ Voices
3
+ } from "../chunk-WBK6LLXX.js";
4
+ import "../chunk-BCQ63627.js";
5
+ import "../chunk-3GBZUGUD.js";
6
+ import "../chunk-WIFMLPT5.js";
7
+ export {
8
+ Voices as default
9
+ };
package/package.json CHANGED
@@ -4,23 +4,27 @@
4
4
  "name": "Cartesia",
5
5
  "url": "https://cartesia.ai"
6
6
  },
7
- "version": "0.0.3",
7
+ "version": "1.0.0-alpha.1",
8
8
  "description": "Client for the Cartesia API.",
9
- "main": "./dist/index.js",
10
- "module": "./dist/index.mjs",
9
+ "type": "module",
10
+ "module": "./dist/index.js",
11
11
  "types": "./dist/index.d.ts",
12
12
  "exports": {
13
13
  ".": {
14
- "import": "./dist/index.mjs",
15
- "require": "./dist/index.js"
14
+ "import": "./dist/index.js",
15
+ "require": "./dist/index.cjs"
16
16
  },
17
17
  "./react": {
18
- "import": "./dist/react/index.mjs",
19
- "require": "./dist/react/index.js"
18
+ "import": "./dist/react/index.js",
19
+ "require": "./dist/react/index.cjs"
20
20
  }
21
21
  },
22
+ "engines": {
23
+ "node": ">=18"
24
+ },
22
25
  "dependencies": {
23
26
  "base64-js": "^1.5.1",
27
+ "cross-fetch": "^4.0.0",
24
28
  "emittery": "^1.0.3",
25
29
  "human-id": "^4.1.1",
26
30
  "partysocket": "^1.0.1"
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export { Cartesia as default } from "./lib";
2
+ export * from "./lib";
3
+ export * from "./types";
4
+ export { default as WebPlayer } from "./tts/player";
package/src/lib/client.ts CHANGED
@@ -1,5 +1,6 @@
1
+ import fetch from "cross-fetch";
1
2
  import type { ClientOptions } from "../types";
2
- import { BASE_URL } from "./constants";
3
+ import { BASE_URL, constructApiUrl } from "./constants";
3
4
 
4
5
  export class Client {
5
6
  apiKey: string;
@@ -14,4 +15,16 @@ export class Client {
14
15
  this.apiKey = (options.apiKey || process.env.CARTESIA_API_KEY)!;
15
16
  this.baseUrl = options.baseUrl || BASE_URL;
16
17
  }
18
+
19
+ fetch(path: string, options: RequestInit = {}) {
20
+ const url = constructApiUrl(this.baseUrl, path);
21
+
22
+ return fetch(url.toString(), {
23
+ ...options,
24
+ headers: {
25
+ "X-API-KEY": this.apiKey,
26
+ ...options.headers,
27
+ },
28
+ });
29
+ }
17
30
  }
@@ -1,6 +1,16 @@
1
1
  export const BASE_URL = "https://api.cartesia.ai/v0";
2
- export const SAMPLE_RATE = 44100;
3
2
 
4
- export const constructWebsocketUrl = (baseUrl: string) => {
5
- return new URL(`${baseUrl.replace(/^http/, "ws")}/audio/websocket`);
3
+ export const constructApiUrl = (
4
+ baseUrl: string,
5
+ path: string,
6
+ protocol?: string,
7
+ ) => {
8
+ const normalizedPath = path.startsWith("/") ? path : `/${path}`;
9
+ if (!protocol) {
10
+ return new URL(`${baseUrl}${normalizedPath}`);
11
+ }
12
+ if (!["http", "ws"].includes(protocol)) {
13
+ throw new Error(`Invalid protocol: ${protocol}`);
14
+ }
15
+ return new URL(`${baseUrl.replace(/^http/, protocol)}${normalizedPath}`);
6
16
  };
package/src/lib/index.ts CHANGED
@@ -1,13 +1,16 @@
1
- import Audio from "../audio";
1
+ import TTS from "../tts";
2
2
  import type { ClientOptions } from "../types";
3
+ import Voices from "../voices";
3
4
  import { Client } from "./client";
4
5
 
5
6
  export class Cartesia extends Client {
6
- audio: Audio;
7
+ tts: TTS;
8
+ voices: Voices;
7
9
 
8
10
  constructor(options: ClientOptions = {}) {
9
11
  super(options);
10
12
 
11
- this.audio = new Audio(options);
13
+ this.tts = new TTS(options);
14
+ this.voices = new Voices(options);
12
15
  }
13
16
  }