Sync / foldkit-durable

Codec

namespace · packages/durable/src/codec.ts:49

const Codec = { fromSchema }

Its type

interface Codec<Value, Encoded = unknown> {
  readonly encode: (value: Value) => Encoded
  decode(value: Encoded): Value
  readonly compared?: ((value: Encoded) => unknown) | undefined
}

Encodes a value to JSON-compatible data and decodes it back strictly.

A callback that throws means the value is invalid. Encoded is the wire/storage side, so Journal.append can require it instead of accepting unknown. An application that already has a Schema.Codec passes it directly; Codec.fromSchema is the explicit conversion.

In the guides

Members

Codec.fromSchema

function · packages/durable/src/codec.ts:42

fromSchema<Value, Encoded>(
  schema: Schema.Codec<Value, Encoded>,
): Codec<Value, Encoded>

The function pair a Schema.Codec denotes. Decoding validates unknown input and throws a SchemaError when it does not match, which the journal reports as InvalidOperationError.