import { UnionToTuple } from "./type-utils.js"; import { Infer } from "./infer.js"; import { ValidationResult } from "@atproto/lexicon"; //#region core/lib.d.ts /** @see https://atproto.com/specs/lexicon#overview-of-types */ type LexiconType = "null" | "boolean" | "integer" | "string" | "bytes" | "cid-link" | "blob" | "array" | "object" | "params" | "token" | "ref" | "union" | "unknown" | "record" | "query" | "procedure" | "subscription" | "permission-set"; /** * Common options available for lexicon items. * @see https://atproto.com/specs/lexicon#string-formats */ type LexiconItemCommonOptions = { /** Indicates this field must be provided */ required?: boolean; /** Indicates this field can be explicitly set to null */ nullable?: boolean; /** Human-readable description */ description?: string; }; /** * Base interface for all lexicon items. * @see https://atproto.com/specs/lexicon#overview-of-types */ type LexiconItem = LexiconItemCommonOptions & { type: LexiconType; }; /** * Definition in a lexicon namespace. * @see https://atproto.com/specs/lexicon#lexicon-document */ type Def = { type: LexiconType; }; /** * Lexicon namespace document structure. * @see https://atproto.com/specs/lexicon#lexicon-document */ type LexiconNamespace = { /** Namespaced identifier (NSID) for this lexicon */ id: string; /** Named definitions within this namespace */ defs: Record; }; /** * String type options. * @see https://atproto.com/specs/lexicon#string */ type StringOptions = LexiconItemCommonOptions & { /** * Semantic string format constraint. * @see https://atproto.com/specs/lexicon#string-formats */ format?: "at-identifier" | "at-uri" | "cid" | "datetime" | "did" | "handle" | "nsid" | "tid" | "record-key" | "uri" | "language"; /** Maximum string length in bytes */ maxLength?: number; /** Minimum string length in bytes */ minLength?: number; /** Maximum string length in Unicode graphemes */ maxGraphemes?: number; /** Minimum string length in Unicode graphemes */ minGraphemes?: number; /** Hints at expected values, not enforced */ knownValues?: string[]; /** Restricts to an exact set of string values */ enum?: string[]; /** Default value if not provided */ default?: string; /** Fixed, unchangeable value */ const?: string; }; /** * Boolean type options. * @see https://atproto.com/specs/lexicon#boolean */ type BooleanOptions = LexiconItemCommonOptions & { /** Default value if not provided */ default?: boolean; /** Fixed, unchangeable value */ const?: boolean; }; /** * Integer type options. * @see https://atproto.com/specs/lexicon#integer */ type IntegerOptions = LexiconItemCommonOptions & { /** Minimum allowed value (inclusive) */ minimum?: number; /** Maximum allowed value (inclusive) */ maximum?: number; /** Restricts to an exact set of integer values */ enum?: number[]; /** Default value if not provided */ default?: number; /** Fixed, unchangeable value */ const?: number; }; /** * Bytes type options for arbitrary byte arrays. * @see https://atproto.com/specs/lexicon#bytes */ type BytesOptions = LexiconItemCommonOptions & { /** Minimum byte array length */ minLength?: number; /** Maximum byte array length */ maxLength?: number; }; /** * Blob type options for binary data with MIME types. * @see https://atproto.com/specs/lexicon#blob */ type BlobOptions = LexiconItemCommonOptions & { /** Allowed MIME types (e.g., ["image/png", "image/jpeg"]) */ accept?: string[]; /** Maximum blob size in bytes */ maxSize?: number; }; /** * Array type options. * @see https://atproto.com/specs/lexicon#array */ type ArrayOptions = LexiconItemCommonOptions & { /** Minimum array length */ minLength?: number; /** Maximum array length */ maxLength?: number; }; /** * Record type options for repository records. * @see https://atproto.com/specs/lexicon#record */ type RecordOptions = { /** * Record key strategy: "self" for self-describing or "tid" for timestamp IDs * @see https://atproto.com/specs/record-key */ key: string; /** Object schema defining the record structure */ record: { type: "object"; }; /** Human-readable description */ description?: string; }; /** * Union type options for multiple possible types. * @see https://atproto.com/specs/lexicon#union */ type UnionOptions = LexiconItemCommonOptions & { /** If true, only listed refs are allowed; if false, additional types may be added */ closed?: boolean; }; /** * Map of property names to their lexicon item definitions. * @see https://atproto.com/specs/lexicon#object */ type ObjectProperties = Record; /** Resolves to an error string for nested objects, or passes through unchanged. */ type CheckNotObject = T extends { type: "object"; } ? '❌ Nested objects are not supported. Per the Lexicon spec, objects can be "nested inside other definitions by reference" (https://atproto.com/specs/lexicon#object). Use lx.ref() instead.' : T; /** * Object-level options (not property-level). * @see https://atproto.com/specs/lexicon#object */ type ObjectOptions = { /** Human-readable description of the object */ description?: string; /** Indicates this object is a required property when nested in a parent object */ required?: boolean; /** Indicates this object can be explicitly set to null when nested in a parent object */ nullable?: boolean; }; type RequiredKeys = { [K in keyof T]: T[K] extends { required: true; } | { _required: true; } ? K : never }[keyof T]; type NullableKeys = { [K in keyof T]: T[K] extends { nullable: true; } | { _nullable: true; } ? K : never }[keyof T]; /** * Resulting object schema with required and nullable fields extracted. * @see https://atproto.com/specs/lexicon#object */ type ObjectResult = { type: "object"; /** Property definitions */ properties: { [K in keyof T]: T[K] extends { type: "object"; } ? Omit : Omit }; } & ([RequiredKeys] extends [never] ? {} : { required: UnionToTuple>; }) & ([NullableKeys] extends [never] ? {} : { nullable: UnionToTuple>; }) & (O extends { required: true; } ? { _required: true; } : {}) & (O extends { nullable: true; } ? { _nullable: true; } : {}) & Omit; /** * Map of parameter names to their lexicon item definitions. * @see https://atproto.com/specs/lexicon#params */ type ParamsProperties = Record; /** * Resulting params schema with required fields extracted. * @see https://atproto.com/specs/lexicon#params */ type ParamsResult = { type: "params"; /** Parameter definitions */ properties: { [K in keyof T]: Omit }; } & ([RequiredKeys] extends [never] ? {} : { required: UnionToTuple>; }); /** * HTTP request or response body schema. * @see https://atproto.com/specs/lexicon#http-endpoints */ type BodySchema = { /** MIME type encoding (typically "application/json") */ encoding: "application/json" | (string & {}); /** Human-readable description */ description?: string; /** Object schema defining the body structure */ schema?: ObjectResult; }; /** * Error definition for HTTP endpoints. * @see https://atproto.com/specs/lexicon#http-endpoints */ type ErrorDef = { /** Error name/code */ name: string; /** Human-readable error description */ description?: string; }; /** * Query endpoint options (HTTP GET). * @see https://atproto.com/specs/lexicon#query */ type QueryOptions = { /** Human-readable description */ description?: string; /** Query string parameters */ parameters?: ParamsResult; /** Response body schema */ output?: BodySchema; /** Possible error responses */ errors?: ErrorDef[]; }; /** * Procedure endpoint options (HTTP POST). * @see https://atproto.com/specs/lexicon#procedure */ type ProcedureOptions = { /** Human-readable description */ description?: string; /** Query string parameters */ parameters?: ParamsResult; /** Request body schema */ input?: BodySchema; /** Response body schema */ output?: BodySchema; /** Possible error responses */ errors?: ErrorDef[]; }; /** * WebSocket message schema for subscriptions. * @see https://atproto.com/specs/lexicon#subscription */ type MessageSchema = { /** Human-readable description */ description?: string; /** Union of possible message types */ schema: { type: "union"; refs: readonly string[]; }; }; /** * Subscription endpoint options (WebSocket). * @see https://atproto.com/specs/lexicon#subscription */ type SubscriptionOptions = { /** Human-readable description */ description?: string; /** Query string parameters */ parameters?: ParamsResult; /** Message schema for events */ message?: MessageSchema; /** Possible error responses */ errors?: ErrorDef[]; }; /** * A lexicon schema object or a plain NSID string, used to reference * collections or endpoints in permission definitions. */ type NsidResolvable = string | { json: { id: string; }; }; /** * A translatable string with optional language variants. * Used particularly permission-sets. */ type LangString = { default: string; lang: Record; }; /** A plain string or a translatable string with language variants. */ type Translatable = string | LangString; /** * Options for a repo-resource permission. * @see https://atproto.com/specs/permission#repo */ type RepoPermissionOptions = { /** NSID of record types (lexicon schemas or NSID strings). Wildcard (*) grants access to all records. Partial wildcards are not supported. Wildcards are not supported in permissions within a permission set */ collection: NsidResolvable[]; /** defines the set of record operations allowed. If not defined, all operations are allowed */ action?: readonly ("create" | "update" | "delete")[]; }; /** * Options for an RPC-resource permission. * @see https://atproto.com/specs/permission#rpc */ type RpcPermissionOptions = { /** NSID of API endpoints (lexicon schemas or NSID strings). Wildcard (*) gives access to all endpoints. Partial wildcards are not supported. Wildcards are not supported in permissions within a permission set */ lxm: NsidResolvable[]; /** audience of API requests, as a DID service reference: DID followed by required service type fragment (e.g. did:web:api.example.com#srvtype). Supports wildcard (*), though aud and lxm cannot both be wildcard. DID references are not allowed in permission set context. Always required in granular string representation; contingent on `inheritAud` in permission sets */ aud?: string; /** only used inside permission sets. If true, an `aud` value will be inherited from the `include:` invocation, and the `aud` field is not required on the permission */ inheritAud?: boolean; }; /** * Options for a blob-resource permission. * @see https://atproto.com/specs/permission#blob */ type BlobPermissionOptions = { /** MIME types or partial MIME type glob patterns. Same syntax as the `accept` field in the `blob` lexicon type */ accept: string[]; }; /** * Options for an account-resource permission. * @see https://atproto.com/specs/permission#account */ type AccountPermissionOptions = { /** a component of account configuration. Wildcard is not supported. "email": account email address — `read` makes email and verification status visible, `manage` includes `read` and allows changing the email. "repo": ability to update entire public repository using a CAR file — `manage` allows importing CAR files (e.g. during account migration), `read` does nothing */ attr: "email" | "repo"; /** degree of control. If not specified, default is `read` */ action?: "read" | "manage"; }; /** * Options for an identity-resource permission. * @see https://atproto.com/specs/permission#identity */ type IdentityPermissionOptions = { /** an aspect or component of identity. Wildcard (*) indicates full control of DID document and handle. "handle": ability to update handle, including registration in the DID document and any domain names controlled by the PDS */ attr: "handle" | "*"; }; /** Resolves an Options type into a permission entry, converting NsidResolvable fields to strings */ type PermissionEntryOf = { type: "permission"; resource: Resource; } & { [K in keyof Opts]: Opts[K] extends readonly NsidResolvable[] ? string[] : Opts[K] }; /** @see https://atproto.com/specs/permission#repo */ type RepoPermissionEntry = PermissionEntryOf<"repo", RepoPermissionOptions>; /** @see https://atproto.com/specs/permission#rpc */ type RpcPermissionEntry = PermissionEntryOf<"rpc", RpcPermissionOptions>; /** @see https://atproto.com/specs/permission#blob */ type BlobPermissionEntry = PermissionEntryOf<"blob", BlobPermissionOptions>; /** @see https://atproto.com/specs/permission#account */ type AccountPermissionEntry = PermissionEntryOf<"account", AccountPermissionOptions>; /** @see https://atproto.com/specs/permission#identity */ type IdentityPermissionEntry = PermissionEntryOf<"identity", IdentityPermissionOptions>; /** * Union of all permission entry types. * @see https://atproto.com/specs/permission */ type PermissionEntry = RepoPermissionEntry | RpcPermissionEntry | BlobPermissionEntry | AccountPermissionEntry | IdentityPermissionEntry; /** * Options for a permission-set definition. * @see https://atproto.com/specs/permission */ type PermissionSetOptions = { /** Human-readable title, optionally translatable via lx.langString() */ title: Translatable; /** Human-readable detail, optionally translatable via lx.langString() */ detail: Translatable; /** List of permissions in this set */ permissions: PermissionEntry[]; /** Human-readable description */ description?: string; }; /** * Public interface for Lexicon to avoid exposing private implementation details */ type LexiconSchema = { json: T; "~infer": Infer<{ json: T; }>; validate(data: unknown, def?: keyof T["defs"]): ValidationResult>; }; /** * Main API for creating lexicon schemas. * @see https://atproto.com/specs/lexicon */ declare const lx: { /** * Creates a null type. * @see https://atproto.com/specs/lexicon#null */ null(options?: LexiconItemCommonOptions): { type: "null"; } & LexiconItemCommonOptions; /** * Creates a boolean type with optional constraints. * @see https://atproto.com/specs/lexicon#boolean */ boolean(options?: T): T & { type: "boolean"; }; /** * Creates an integer type with optional min/max and enum constraints. * @see https://atproto.com/specs/lexicon#integer */ integer(options?: T): T & { type: "integer"; }; /** * Creates a string type with optional format, length, and value constraints. * @see https://atproto.com/specs/lexicon#string */ string(options?: T): T & { type: "string"; }; /** * Creates an unknown type for flexible, unvalidated objects. * @see https://atproto.com/specs/lexicon#unknown */ unknown(options?: LexiconItemCommonOptions): { type: "unknown"; } & LexiconItemCommonOptions; /** * Creates a bytes type for arbitrary byte arrays. * @see https://atproto.com/specs/lexicon#bytes */ bytes(options?: T): T & { type: "bytes"; }; /** * Creates a CID link reference to content-addressed data. * @see https://atproto.com/specs/lexicon#cid-link */ cidLink(link: Link): { type: "cid-link"; $link: Link; }; /** * Creates a blob type for binary data with MIME type constraints. * @see https://atproto.com/specs/lexicon#blob */ blob(options?: T): T & { type: "blob"; }; /** * Creates an array type with item schema and length constraints. * @see https://atproto.com/specs/lexicon#array */ array(items: Items, options?: Options): Options & { type: "array"; items: Items; }; /** * Creates a token type for symbolic values in unions. * @see https://atproto.com/specs/lexicon#token */ token(description: Description): { type: "token"; description: Description; }; /** * Creates a reference to another schema definition. * @see https://atproto.com/specs/lexicon#ref */ ref(ref: Ref, options?: LexiconItemCommonOptions): LexiconItemCommonOptions & { type: "ref"; ref: Ref; }; /** * Creates a union type for multiple possible type variants. * @see https://atproto.com/specs/lexicon#union */ union(refs: Refs, options?: Options): Options & { type: "union"; refs: Refs; }; /** * Creates a record type for repository records. * @see https://atproto.com/specs/lexicon#record */ record(options: T): T & { type: "record"; }; /** * Creates an object type with defined properties. * @see https://atproto.com/specs/lexicon#object */ object(properties: { [K in keyof T]: CheckNotObject }, options?: O): ObjectResult; /** * Creates a params type for query string parameters. * @see https://atproto.com/specs/lexicon#params */ params(properties: Properties): ParamsResult; /** * Creates a query endpoint definition (HTTP GET). * @see https://atproto.com/specs/lexicon#query */ query(options?: T): T & { type: "query"; }; /** * Creates a procedure endpoint definition (HTTP POST). * @see https://atproto.com/specs/lexicon#procedure */ procedure(options?: T): T & { type: "procedure"; }; /** * Creates a subscription endpoint definition (WebSocket). * @see https://atproto.com/specs/lexicon#subscription */ subscription(options?: T): T & { type: "subscription"; }; /** * Creates a repo-resource permission entry. * @see https://atproto.com/specs/permission#repo */ repoPermission(options: RepoPermissionOptions): RepoPermissionEntry; /** * Creates an RPC-resource permission entry. * @see https://atproto.com/specs/permission#rpc */ rpcPermission(options: RpcPermissionOptions): RpcPermissionEntry; /** * Creates a blob-resource permission entry. * @see https://atproto.com/specs/permission#blob */ blobPermission(options: BlobPermissionOptions): BlobPermissionEntry; /** * Creates an account-resource permission entry. * @see https://atproto.com/specs/permission#account */ accountPermission(options: AccountPermissionOptions): AccountPermissionEntry; /** * Creates an identity-resource permission entry. * @see https://atproto.com/specs/permission#identity */ identityPermission(options: IdentityPermissionOptions): IdentityPermissionEntry; /** * Creates a translatable string with language variants. * @see https://atproto.com/specs/permission */ langString(defaultValue: string, lang: Record): LangString; /** * Creates a permission-set definition. * @see https://atproto.com/specs/permission#permission-sets */ permissionSet(options: PermissionSetOptions): { description?: string | undefined; permissions: PermissionEntry[]; "detail:lang"?: Record | undefined; detail: string; "title:lang"?: Record | undefined; type: "permission-set"; key: "literal:self"; title: string; }; /** * Creates a lexicon schema document. * @see https://atproto.com/specs/lexicon#lexicon-document */ lexicon(id: ID, defs: D): LexiconSchema<{ lexicon: 1; id: ID; defs: D; }>; }; /** helper to pull lexicon from json directly */ declare function fromJSON(json: Lex): LexiconSchema<{ lexicon: 1; id: Lex["id"]; defs: Lex["defs"]; }>; //#endregion export { fromJSON, lx }; //# sourceMappingURL=lib.d.ts.map