import type { ItemIdentity } from "@contfu/core"; import { QueryResultArray, type ContentFormat, type IncludeOption, type MarkdownOptions, type QueryOptions, type RenderOptions, type ResolvedContent, type SortOption } from "@contfu/core"; import type { FileMetadata } from "@contfu/core"; import type { ItemData } from "../infra/types/content-types.js"; import type { and, contains, eq, gt, gte, ItemRef, like, linkedFrom, linksTo, lt, lte, ne, notLike, or } from "@contfu/core"; import type { SystemFieldName } from "@contfu/core"; export type QuerySystemFields = { $id: ItemIdentity; $collection: string; $changedAt: number; $deletedAt?: number; $locale?: string; $scope?: string; $draft?: boolean; }; type CollectionLocale = Props extends { $locale?: infer L; } ? Extract : Props extends { $locale: infer L; } ? Extract : never; export type QueryLocale = [CollectionLocale] extends [never] ? string : CollectionLocale; type ItemExtras = { files?: FileMetadata[]; links: ItemData["links"]; content?: ResolvedContent; }; export type ItemWithRelations = QuerySystemFields & ItemExtras & Props & Rels & Record; export type QueryResult = QueryResultArray>>; export type SelectableField = SystemFieldName | (keyof Props & string); type SelectableShape = QuerySystemFields & Props; export type PickFields = F extends readonly SelectableField[] ? Pick, Extract>> : SelectableShape; export type TypedItem = ItemWithRelations; export type TypedQueryResult = QueryResultArray>; export type TypedWithEntry = { collection?: ChildC; filter?: string | ((self: any) => string); limit?: number; include?: IncludeOption[]; single?: boolean; with?: TypedWithInput; includeDeleted?: boolean; onlyDeleted?: boolean; }; export type TypedWithInput = { [rel: string]: TypedWithEntry; } | ((parent: ItemRef) => { [rel: string]: TypedWithEntry; }); export type FileMode = "local" | "remote"; export type TypedQueryEntry[] | undefined = undefined, CF extends ContentFormat = "object"> = { collection?: C; filter?: string; sort?: SortOption | SortOption[]; limit?: number; offset?: number; search?: string; flat?: boolean; include?: IncludeOption[]; fileMode?: FileMode; fields?: F; with?: TypedWithInput; contentAs?: CF; htmlOptions?: RenderOptions; markdownOptions?: MarkdownOptions; locale?: QueryLocale | false; fallback?: QueryLocale | true | false; includeDeleted?: boolean; onlyDeleted?: boolean; }; type ResolveWithShape = W extends (...args: any[]) => infer R ? R : W; type InferRelValue = Entry extends { collection: infer C; single: true; } ? C extends keyof CMap ? TypedItem | null : TypedItem> | null : Entry extends { collection: infer C; } ? C extends keyof CMap ? TypedItem[] : TypedItem>[] : TypedItem>[]; export type InferRels = { [K in keyof ResolveWithShape]: InferRelValue[K]>; }; export type TypedQueryResultWithRels, CF extends ContentFormat = "object"> = QueryResultArray>; export type EntryOpts = { filter?: string | ((self: ItemRef) => string); sort?: SortOption | SortOption[]; limit?: number; offset?: number; search?: string; flat?: boolean; include?: IncludeOption[]; fileMode?: FileMode; fields?: SelectableField[]; with?: TypedWithInput; contentAs?: CF; htmlOptions?: RenderOptions; markdownOptions?: MarkdownOptions; locale?: QueryLocale | false; fallback?: QueryLocale | true | false; includeDeleted?: boolean; onlyDeleted?: boolean; }; export interface TypedAllFn { (collection: C): { collection: C; }; (collection: C, filter: string): { collection: C; filter: string; }; (collection: C, filter: (self: ItemRef) => string): { collection: C; filter: (self: ItemRef) => string; }; = EntryOpts>(collection: C, opts: O): O & { collection: C; }; } export interface TypedOneOfFn { (collection: C): { collection: C; single: true; }; (collection: C, filter: string): { collection: C; single: true; filter: string; }; (collection: C, filter: (self: ItemRef) => string): { collection: C; single: true; filter: (self: ItemRef) => string; }; = EntryOpts>(collection: C, opts: O): O & { collection: C; single: true; }; } interface TypedContfuClientBase { all: TypedAllFn; oneOf: TypedOneOfFn; eq: typeof eq; ne: typeof ne; gt: typeof gt; gte: typeof gte; lt: typeof lt; lte: typeof lte; like: typeof like; notLike: typeof notLike; contains: typeof contains; and: typeof and; or: typeof or; linksTo: typeof linksTo; linkedFrom: typeof linkedFrom; /** * Returns a new query fn scoped to the given locale. Useful for SSR per-request scoping without mutating the shared client. * The optional `fallback` overrides the client-configured fallback for this scope (`false` disables). */ withLocale(locale: QueryLocale | false, fallback?: QueryLocale | true | false): TypedContfuClient; (collection: C, filter: string): Promise>; (collection: C, filter: (self: ItemRef) => string): Promise>; [] | undefined = undefined, const W extends { [rel: string]: TypedWithEntry; } = { [rel: string]: TypedWithEntry; }, CF extends ContentFormat = "object">(collection: C, opts: Omit, "with" | "fields"> & { fields?: F; with: (parent: ItemRef) => W; }): Promise, InferRels, CF>>; [] | undefined = undefined, const W extends { [rel: string]: TypedWithEntry; } = { [rel: string]: TypedWithEntry; }, CF extends ContentFormat = "object">(collection: C, opts: Omit, "with" | "fields"> & { fields?: F; with: W; }): Promise, InferRels, CF>>; [] | undefined = undefined, CF extends ContentFormat = "object">(collection: C, opts?: Omit, "fields"> & { fields?: F; }): Promise, CF>>; [] | undefined = undefined, const W extends { [rel: string]: TypedWithEntry; } = { [rel: string]: TypedWithEntry; }, CF extends ContentFormat = "object">(options: TypedQueryEntry & { collection: C; with: (parent: ItemRef) => W; }): Promise, InferRels, CF>>; [] | undefined = undefined, const W extends { [rel: string]: TypedWithEntry; } = { [rel: string]: TypedWithEntry; }, CF extends ContentFormat = "object">(options: TypedQueryEntry & { collection: C; with: W; }): Promise, InferRels, CF>>; [] | undefined = undefined, CF extends ContentFormat = "object">(options: TypedQueryEntry & { collection: C; }): Promise, CF>>; (options?: QueryOptions): Promise; } export type TypedContfuClient = TypedContfuClientBase; export {};