import type { StandardSchemaV1 } from "@standard-schema/spec"; import { unsupportedFeature } from "./errors"; export type SchemaHookResult = boolean | Promise; type SchemaHook = (ctx: TContext) => SchemaHookResult; export interface EventTypeToken { readonly _eventType?: T; readonly canSubscribe?: SchemaHook; readonly schema?: PrimitiveSchema; } export interface QueueTypeToken { readonly _queueMessage?: TMessage; readonly _queueComplete?: TComplete; /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ readonly canPublish?: SchemaHook; readonly message?: PrimitiveSchema; readonly complete?: PrimitiveSchema; } /** @deprecated Use `event()`. */ export type Type = EventTypeToken; interface EventOptions { canSubscribe?: SchemaHook; schema?: PrimitiveSchema; } interface QueueOptions { /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ canPublish?: SchemaHook; message?: PrimitiveSchema; complete?: PrimitiveSchema; } export function event( options?: EventOptions, ): EventTypeToken { return (options ?? {}) as EventTypeToken; } /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ export function queue( options?: QueueOptions, ): QueueTypeToken { return (options ?? {}) as QueueTypeToken; } export type PrimitiveSchema = StandardSchemaV1 | EventTypeToken; export interface EventSchemaDefinition { schema: PrimitiveSchema; canSubscribe?: SchemaHook; } export interface QueueSchemaDefinition { message: PrimitiveSchema; complete?: PrimitiveSchema; /** @deprecated Queues were deprecated in 3.0 and will be removed in a future version. Use actions and store pending work in SQLite (`c.db`). See https://rivet.dev/actors/docs/deprecated/queues */ canPublish?: SchemaHook; } export type EventSchema = | PrimitiveSchema | EventSchemaDefinition; export type QueueSchema = | PrimitiveSchema | QueueSchemaDefinition | QueueTypeToken; export type EventSchemaConfig = Record< string, EventSchema >; export type QueueSchemaConfig<_TContext = any> = Record; export type AnySchemaConfig = EventSchemaConfig | QueueSchemaConfig; /** @deprecated Use `EventSchema` or `QueueSchema`. */ export type Schema = QueueSchema; /** @deprecated Use `EventSchemaConfig` or `QueueSchemaConfig`. */ export type SchemaConfig = QueueSchemaConfig; export type InferSchema = T extends QueueSchemaDefinition ? InferSchema : T extends QueueTypeToken ? M : T extends EventSchemaDefinition ? InferSchema : T extends StandardSchemaV1 ? O : T extends EventTypeToken ? R : never; export type InferSchemaMap> = { [K in keyof T]: InferSchema; }; export type InferQueueComplete = T extends QueueTypeToken ? [C] extends [never] ? never : C : T extends QueueSchemaDefinition ? T["complete"] extends PrimitiveSchema ? InferSchema : never : never; export type InferQueueCompleteMap = { [K in keyof T]: InferQueueComplete; }; export type InferEventArgs = T extends readonly unknown[] ? number extends T["length"] ? [T] : T : [T]; export type ValidationResult = | { success: true; data: T } | { success: false; issues: unknown[] }; export function isStandardSchema(value: unknown): value is StandardSchemaV1 { return typeof value === "object" && value !== null && "~standard" in value; } export function isQueueSchemaDefinition( value: unknown, ): value is QueueSchemaDefinition { if (isEventSchemaDefinition(value)) { return false; } return ( typeof value === "object" && value !== null && "message" in value && (value as { message?: unknown }).message !== undefined ); } export function isEventSchemaDefinition( value: unknown, ): value is EventSchemaDefinition { return ( typeof value === "object" && value !== null && "schema" in value && (value as { schema?: unknown }).schema !== undefined ); } export function hasSchemaConfigKey( schemas: T | undefined, key: string, ): boolean { if (!schemas) { return false; } return Object.hasOwn(schemas, key); } export function getEventCanSubscribe( schemas: EventSchemaConfig | undefined, key: string, ): SchemaHook | undefined { const schema = schemas?.[key]; if (!schema || isStandardSchema(schema)) { return undefined; } const maybeCanSubscribe = (schema as { canSubscribe?: unknown }) .canSubscribe; return typeof maybeCanSubscribe === "function" ? (maybeCanSubscribe as SchemaHook) : undefined; } export function getQueueCanPublish( schemas: QueueSchemaConfig | undefined, key: string, ): SchemaHook | undefined { const schema = schemas?.[key]; if (!schema || isStandardSchema(schema)) { return undefined; } const maybeCanPublish = (schema as { canPublish?: unknown }).canPublish; return typeof maybeCanPublish === "function" ? (maybeCanPublish as SchemaHook) : undefined; } function getValidationSchema( schema: QueueSchema | EventSchema | undefined, ): QueueSchema | EventSchema | undefined { if (!schema) { return undefined; } if (isEventSchemaDefinition(schema)) { return schema.schema; } if (isQueueSchemaDefinition(schema)) { return schema.message; } if ( typeof schema === "object" && schema !== null && "schema" in schema && (schema as { schema?: unknown }).schema !== undefined ) { return (schema as { schema: QueueSchema | EventSchema }).schema; } if ( typeof schema === "object" && schema !== null && "message" in schema && (schema as { message?: unknown }).message !== undefined ) { return (schema as { message: QueueSchema | EventSchema }).message; } return schema; } function isPromiseLike(value: unknown): value is PromiseLike { return ( typeof value === "object" && value !== null && "then" in value && typeof (value as { then?: unknown }).then === "function" ); } export async function validateSchema( schemas: T | undefined, key: keyof T & string, data: unknown, ): Promise[typeof key]>> { const schema = getValidationSchema(schemas?.[key]); if (!schema) { return { success: true, data: data as InferSchemaMap[typeof key] }; } if (isStandardSchema(schema)) { const result = await schema["~standard"].validate(data); if (result.issues) { return { success: false, issues: [...result.issues] }; } return { success: true, data: result.value as InferSchemaMap[typeof key], }; } return { success: true, data: data as InferSchemaMap[typeof key] }; } export function validateSchemaSync( schemas: T | undefined, key: keyof T & string, data: unknown, ): ValidationResult[typeof key]> { const schema = getValidationSchema(schemas?.[key]); if (!schema) { return { success: true, data: data as InferSchemaMap[typeof key] }; } if (isStandardSchema(schema)) { const result = schema["~standard"].validate(data); if (isPromiseLike(result)) { throw unsupportedFeature("async schema validation"); } if (result.issues) { return { success: false, issues: [...result.issues] }; } return { success: true, data: result.value as InferSchemaMap[typeof key], }; } return { success: true, data: data as InferSchemaMap[typeof key] }; }