-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
162 changed files
with
5,748 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { bigint } from "./bigint"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { date } from "./date"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { enum_ } from "./enum"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
Oops, something went wrong.