import { Prettify } from "./type-utils.js"; //#region core/infer.d.ts /** * Error message displayed when an object is nested inside another object. * Lexicon definitions do not support inline nested objects — each object * should be its own definition, referenced via lx.ref(). */ type NestedObjectError = '[Nested objects are not supported in lexicon definitions. Per the Lexicon spec, objects can be "nested inside other definitions by reference" (https://atproto.com/specs/lexicon#object). Define each object in its own lexicon def and use lx.ref() instead.]'; /** * Minimal structural CID, compatible with the `CID` class from * [`multiformats`](https://github.com/multiformats/js-multiformats) and the `Cid` interface from * [`@atproto/lex-data`](https://github.com/bluesky-social/atproto/tree/main/packages/lex-data). */ type Cid = { readonly bytes: Uint8Array; }; /** * AT Protocol blob reference. Mirrors `BlobRef` from * [`@atproto/api`](https://github.com/bluesky-social/atproto/tree/main/packages/api) / * [`@atproto/lex-data`](https://github.com/bluesky-social/atproto/tree/main/packages/lex-data), * but defined locally to avoid pulling in those packages as dependencies. * * @see https://atproto.com/specs/data-model#blob-type */ type BlobRef = { $type: "blob"; ref: { $link: string; } | Cid; mimeType: string; size: number; }; /** Resolves property type, returning NestedObjectError for inline nested objects. */ type InferPropertyType = T extends { type: "object"; } ? NestedObjectError : InferType; type InferType = T extends { type: "record"; } ? InferRecord : T extends { type: "query"; } ? InferQuery : T extends { type: "procedure"; } ? InferProcedure : T extends { type: "subscription"; } ? InferSubscription : T extends { type: "object"; } ? InferObject : T extends { type: "array"; } ? InferArray : T extends { type: "params"; } ? InferParams : T extends { type: "permission-set"; } ? InferPermissionSet : T extends { type: "union"; } ? InferUnion : T extends { type: "token"; } ? InferToken : T extends { type: "ref"; } ? InferRef : T extends { type: "unknown"; } ? unknown : T extends { type: "null"; } ? null : T extends { type: "boolean"; } ? boolean : T extends { type: "integer"; } ? number : T extends { type: "string"; } ? T extends { enum: readonly (infer E extends string)[]; } ? E : T extends { knownValues: readonly (infer K extends string)[]; } ? K | (string & {}) : string : T extends { type: "bytes"; } ? Uint8Array : T extends { type: "cid-link"; } ? string : T extends { type: "blob"; } ? BlobRef : never; type InferToken = T extends { enum: readonly (infer U)[]; } ? U : string; type GetRequired = T extends { required: readonly (infer R)[]; } ? R : never; type GetNullable = T extends { nullable: readonly (infer N)[]; } ? N : never; type InferObject & string, Required extends string = GetRequired & string, NullableAndRequired extends string = Required & Nullable & string, Normal extends string = ("properties" extends keyof T ? Exclude & string : never)> = Prettify } & { -readonly [K in Exclude]-?: InferPropertyType } & { -readonly [K in Exclude]?: InferPropertyType | null } & { -readonly [K in NullableAndRequired]: InferPropertyType | null } : {}>; type InferArray = T extends { items: infer Items; } ? InferType[] : never[]; type InferUnion = T extends { refs: readonly (infer R)[]; } ? R extends string ? { $type: R; [key: string]: unknown; } : never : never; type InferRef = T extends { ref: infer R; } ? R extends string ? { $type: R; [key: string]: unknown; } : unknown : unknown; type InferParams = InferObject; type InferPermissionEntry = T extends { resource: "repo"; } ? Prettify<{ type: "permission"; resource: "repo"; collection: string[]; } & (T extends { action: infer A; } ? { action: A; } : {})> : T extends { resource: "rpc"; } ? Prettify<{ type: "permission"; resource: "rpc"; } & (T extends { lxm: infer L; } ? { lxm: L; } : {}) & (T extends { aud: infer A; } ? { aud: A; } : {}) & (T extends { inheritAud: infer I; } ? { inheritAud: I; } : {})> : T extends { resource: "blob"; } ? { type: "permission"; resource: "blob"; accept: string[]; } : T extends { resource: "account"; } ? Prettify<{ type: "permission"; resource: "account"; attr: T extends { attr: infer A; } ? A : string; } & (T extends { action: infer Act; } ? { action: Act; } : {})> : T extends { resource: "identity"; } ? { type: "permission"; resource: "identity"; attr: T extends { attr: infer A; } ? A : string; } : { type: "permission"; resource: string; }; type InferPermissions = T extends readonly [infer Head, ...infer Tail] ? [InferPermissionEntry, ...InferPermissions] : T extends readonly (infer Item)[] ? InferPermissionEntry[] : never; type InferPermissionSet = Prettify<{ title: T extends { title: infer V; } ? V : string; detail: T extends { detail: infer V; } ? V : string; permissions: T extends { permissions: infer P; } ? InferPermissions

: []; } & (T extends { description: infer D; } ? { description: D; } : {})>; type InferRecord = T extends { record: infer R; } ? R extends { type: "object"; } ? InferObject : R extends { type: "union"; } ? InferUnion : unknown : unknown; type InferBody = T extends { schema: infer S; } ? S extends { type: "object"; } ? InferObject : S extends { type: "union"; } ? InferUnion : unknown : unknown; type InferQuery = Prettify<(T extends { parameters: infer P; } ? { parameters: InferType

; } : {}) & (T extends { output: infer O; } ? { output: InferBody; } : {})>; type InferProcedure = Prettify<(T extends { parameters: infer P; } ? { parameters: InferType

; } : {}) & (T extends { input: infer I; } ? { input: InferBody; } : {}) & (T extends { output: infer O; } ? { output: InferBody; } : {})>; type InferSubscription = Prettify<(T extends { parameters: infer P; } ? { parameters: InferType

; } : {}) & (T extends { message: { schema: infer S; }; } ? { message: InferType; } : {})>; /** * Recursively replaces stub references in a type with their actual definitions. * Detects circular references and missing references, returning string literal error messages. */ type ReplaceRefsInType = T extends { $type: `#${infer DefName}`; } ? DefName extends keyof Defs ? DefName extends Visited ? `[Circular reference detected: #${DefName}]` : Prettify & { $type: T["$type"]; }> : `[Reference not found: #${DefName}]` : T extends Uint8Array | BlobRef ? T : T extends readonly (infer Item)[] ? ReplaceRefsInType[] : T extends object ? T extends ((...args: unknown[]) => unknown) ? T : { [K in keyof T]: ReplaceRefsInType } : T; /** * Infers the TypeScript type for a lexicon namespace, returning only the 'main' definition * with all local refs (#user, #post, etc.) resolved to their actual types. */ type Infer; }; }> = Prettify<"main" extends keyof T["json"]["defs"] ? { $type: T["json"]["id"]; } & ReplaceRefsInType, { [K in keyof T["json"]["defs"]]: InferType }> : never>; //#endregion export { BlobRef, Infer }; //# sourceMappingURL=infer.d.ts.map