Skip to content

Commit

Permalink
v2 TS SDK with Websocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
eyw520 committed Nov 27, 2024
1 parent 1584450 commit 7fc96a0
Show file tree
Hide file tree
Showing 162 changed files with 5,748 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ import { CartesiaClient } from "@cartesia/cartesia-js";

const client = new CartesiaClient({ apiKeyHeader: "YOUR_API_KEY_HEADER" });
await client.tts.bytes({
model_id: "sonic-english",
modelId: "sonic-english",
transcript: "Hello, world!",
voice: {
mode: "id",
id: "694f9389-aac1-45b6-b726-9d9369183238",
},
language: "en",
output_format: {
outputFormat: {
container: "raw",
sample_rate: 44100,
sampleRate: 44100,
encoding: "pcm_f32le",
},
});
Expand Down
24 changes: 14 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,33 @@
"test": "jest"
},
"dependencies": {
"url-join": "4.0.1",
"emittery": "^1.0.3",
"form-data": "^4.0.0",
"form-data-encoder": "^4.0.2",
"formdata-node": "^6.0.3",
"human-id": "^4.1.1",
"node-fetch": "2.7.0",
"qs": "6.11.2",
"readable-stream": "^4.5.2",
"form-data-encoder": "^4.0.2"
"url-join": "4.0.1",
"ws": "^8.14.2"
},
"devDependencies": {
"@types/url-join": "4.0.1",
"@types/qs": "6.9.8",
"@types/jest": "29.5.5",
"@types/node": "17.0.33",
"@types/node-fetch": "2.6.9",
"@types/qs": "6.9.8",
"@types/readable-stream": "^4.0.15",
"@types/url-join": "4.0.1",
"@types/ws": "^8.5.13",
"fetch-mock-jest": "^1.5.1",
"webpack": "^5.94.0",
"ts-loader": "^9.3.1",
"jest": "29.7.0",
"@types/jest": "29.5.5",
"ts-jest": "29.1.1",
"jest-environment-jsdom": "29.7.0",
"@types/node": "17.0.33",
"prettier": "2.7.1",
"typescript": "4.6.4"
"ts-jest": "29.1.1",
"ts-loader": "^9.3.1",
"typescript": "4.6.4",
"webpack": "^5.94.0"
},
"browser": {
"fs": false,
Expand Down
7 changes: 7 additions & 0 deletions src/api/resources/tts/types/WebSocketStreamOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

export interface WebSocketStreamOptions {
timeout?: number;
}
9 changes: 9 additions & 0 deletions src/api/resources/tts/types/WebSocketTimestampsResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/

import * as Cartesia from "../../../index";

export interface WebSocketTimestampsResponse extends Cartesia.WebSocketBaseResponse {
wordTimestamps?: Cartesia.WordTimestamps;
}
99 changes: 99 additions & 0 deletions src/core/schemas/Schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { SchemaUtils } from "./builders";

export type Schema<Raw = unknown, Parsed = unknown> = BaseSchema<Raw, Parsed> & SchemaUtils<Raw, Parsed>;

export type inferRaw<S extends Schema> = S extends Schema<infer Raw, any> ? Raw : never;
export type inferParsed<S extends Schema> = S extends Schema<any, infer Parsed> ? Parsed : never;

export interface BaseSchema<Raw, Parsed> {
parse: (raw: unknown, opts?: SchemaOptions) => MaybeValid<Parsed>;
json: (parsed: unknown, opts?: SchemaOptions) => MaybeValid<Raw>;
getType: () => SchemaType | SchemaType;
}

export const SchemaType = {
BIGINT: "bigint",
DATE: "date",
ENUM: "enum",
LIST: "list",
STRING_LITERAL: "stringLiteral",
BOOLEAN_LITERAL: "booleanLiteral",
OBJECT: "object",
ANY: "any",
BOOLEAN: "boolean",
NUMBER: "number",
STRING: "string",
UNKNOWN: "unknown",
RECORD: "record",
SET: "set",
UNION: "union",
UNDISCRIMINATED_UNION: "undiscriminatedUnion",
OPTIONAL: "optional",
} as const;
export type SchemaType = typeof SchemaType[keyof typeof SchemaType];

export type MaybeValid<T> = Valid<T> | Invalid;

export interface Valid<T> {
ok: true;
value: T;
}

export interface Invalid {
ok: false;
errors: ValidationError[];
}

export interface ValidationError {
path: string[];
message: string;
}

export interface SchemaOptions {
/**
* how to handle unrecognized keys in objects
*
* @default "fail"
*/
unrecognizedObjectKeys?: "fail" | "passthrough" | "strip";

/**
* whether to fail when an unrecognized discriminant value is
* encountered in a union
*
* @default false
*/
allowUnrecognizedUnionMembers?: boolean;

/**
* whether to fail when an unrecognized enum value is encountered
*
* @default false
*/
allowUnrecognizedEnumValues?: boolean;

/**
* whether to allow data that doesn't conform to the schema.
* invalid data is passed through without transformation.
*
* when this is enabled, .parse() and .json() will always
* return `ok: true`. `.parseOrThrow()` and `.jsonOrThrow()`
* will never fail.
*
* @default false
*/
skipValidation?: boolean;

/**
* each validation failure contains a "path" property, which is
* the breadcrumbs to the offending node in the JSON. you can supply
* a prefix that is prepended to all the errors' paths. this can be
* helpful for zurg's internal debug logging.
*/
breadcrumbsPrefix?: string[];

/**
* whether to send 'null' for optional properties explicitly set to 'undefined'.
*/
omitUndefined?: boolean;
}
50 changes: 50 additions & 0 deletions src/core/schemas/builders/bigint/bigint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { BaseSchema, Schema, SchemaType } from "../../Schema";
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType";
import { maybeSkipValidation } from "../../utils/maybeSkipValidation";
import { getSchemaUtils } from "../schema-utils";

export function bigint(): Schema<string, bigint> {
const baseSchema: BaseSchema<string, bigint> = {
parse: (raw, { breadcrumbsPrefix = [] } = {}) => {
if (typeof raw !== "string") {
return {
ok: false,
errors: [
{
path: breadcrumbsPrefix,
message: getErrorMessageForIncorrectType(raw, "string"),
},
],
};
}
return {
ok: true,
value: BigInt(raw),
};
},
json: (bigint, { breadcrumbsPrefix = [] } = {}) => {
if (typeof bigint === "bigint") {
return {
ok: true,
value: bigint.toString(),
};
} else {
return {
ok: false,
errors: [
{
path: breadcrumbsPrefix,
message: getErrorMessageForIncorrectType(bigint, "bigint"),
},
],
};
}
},
getType: () => SchemaType.BIGINT,
};

return {
...maybeSkipValidation(baseSchema),
...getSchemaUtils(baseSchema),
};
}
1 change: 1 addition & 0 deletions src/core/schemas/builders/bigint/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { bigint } from "./bigint";
65 changes: 65 additions & 0 deletions src/core/schemas/builders/date/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { BaseSchema, Schema, SchemaType } from "../../Schema";
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType";
import { maybeSkipValidation } from "../../utils/maybeSkipValidation";
import { getSchemaUtils } from "../schema-utils";

// https://stackoverflow.com/questions/12756159/regex-and-iso8601-formatted-datetime
const ISO_8601_REGEX =
/^([+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24:?00)([.,]\d+(?!:))?)?(\17[0-5]\d([.,]\d+)?)?([zZ]|([+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/;

export function date(): Schema<string, Date> {
const baseSchema: BaseSchema<string, Date> = {
parse: (raw, { breadcrumbsPrefix = [] } = {}) => {
if (typeof raw !== "string") {
return {
ok: false,
errors: [
{
path: breadcrumbsPrefix,
message: getErrorMessageForIncorrectType(raw, "string"),
},
],
};
}
if (!ISO_8601_REGEX.test(raw)) {
return {
ok: false,
errors: [
{
path: breadcrumbsPrefix,
message: getErrorMessageForIncorrectType(raw, "ISO 8601 date string"),
},
],
};
}
return {
ok: true,
value: new Date(raw),
};
},
json: (date, { breadcrumbsPrefix = [] } = {}) => {
if (date instanceof Date) {
return {
ok: true,
value: date.toISOString(),
};
} else {
return {
ok: false,
errors: [
{
path: breadcrumbsPrefix,
message: getErrorMessageForIncorrectType(date, "Date object"),
},
],
};
}
},
getType: () => SchemaType.DATE,
};

return {
...maybeSkipValidation(baseSchema),
...getSchemaUtils(baseSchema),
};
}
1 change: 1 addition & 0 deletions src/core/schemas/builders/date/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { date } from "./date";
43 changes: 43 additions & 0 deletions src/core/schemas/builders/enum/enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Schema, SchemaType } from "../../Schema";
import { createIdentitySchemaCreator } from "../../utils/createIdentitySchemaCreator";
import { getErrorMessageForIncorrectType } from "../../utils/getErrorMessageForIncorrectType";

export function enum_<U extends string, E extends U[]>(values: E): Schema<E[number], E[number]> {
const validValues = new Set<string>(values);

const schemaCreator = createIdentitySchemaCreator(
SchemaType.ENUM,
(value, { allowUnrecognizedEnumValues, breadcrumbsPrefix = [] } = {}) => {
if (typeof value !== "string") {
return {
ok: false,
errors: [
{
path: breadcrumbsPrefix,
message: getErrorMessageForIncorrectType(value, "string"),
},
],
};
}

if (!validValues.has(value) && !allowUnrecognizedEnumValues) {
return {
ok: false,
errors: [
{
path: breadcrumbsPrefix,
message: getErrorMessageForIncorrectType(value, "enum"),
},
],
};
}

return {
ok: true,
value: value as U,
};
}
);

return schemaCreator();
}
1 change: 1 addition & 0 deletions src/core/schemas/builders/enum/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { enum_ } from "./enum";
14 changes: 14 additions & 0 deletions src/core/schemas/builders/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export * from "./bigint";
export * from "./date";
export * from "./enum";
export * from "./lazy";
export * from "./list";
export * from "./literals";
export * from "./object";
export * from "./object-like";
export * from "./primitives";
export * from "./record";
export * from "./schema-utils";
export * from "./set";
export * from "./undiscriminated-union";
export * from "./union";
3 changes: 3 additions & 0 deletions src/core/schemas/builders/lazy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { lazy } from "./lazy";
export type { SchemaGetter } from "./lazy";
export { lazyObject } from "./lazyObject";
Loading

0 comments on commit 7fc96a0

Please sign in to comment.