import { Merge } from "./utils.cjs"; import { ItemType, RelationalFields } from "./schema.cjs"; import { LiteralFields } from "./fields.cjs"; //#region src/types/functions.d.ts /** * Available query functions */ type DateTimeFunctions = "year" | "month" | "week" | "day" | "weekday" | "hour" | "minute" | "second"; type DateFunctions = "year" | "month" | "week" | "day" | "weekday"; type TimeFunctions = "hour" | "minute" | "second"; type ArrayFunctions = "count"; /** First arg is constrained to json fields on Item; path is a runtime string so the response alias cannot be statically resolved. */ type JsonFunctionField = `json(${Extract, string>}, ${string})`; /** * Concrete prefix strings used to drive IDE completions. Each json-typed field contributes * `json(fieldName, ` as a specific (non-template) literal so the completer enumerates it and * positions the cursor ready for the path argument. These are a subset of JsonFunctionField * (${string} matches the empty suffix) so validation is unaffected. */ type JsonFunctionFieldPartial = `json(${Extract, string>}, `; /** Replace all occurrences of `From` with `To` in string `S` */ type ReplaceChar = S extends `${infer B}${From}${infer A}` ? `${B}${To}${ReplaceChar}` : S; /** Collapse consecutive underscores to a single one: `a__b` → `a_b` */ type CollapseUnderscores = S extends `${infer B}__${infer A}` ? CollapseUnderscores<`${B}_${A}`> : S; /** Strip a trailing underscore: `foo_` → `foo` */ type StripTrailingUnderscore = S extends `${infer B}_` ? B : S; /** * Normalize a json path to its response-alias form. * Replaces `.`, `[`, `]` with `_`, then collapses consecutive underscores and strips any trailing one. * e.g. `tags[0]` → `tags_0`, `items[0].name` → `items_0_name` */ type NormalizeJsonPath = StripTrailingUnderscore, "[", "_">, "]", "_">>>; /** * Given a literal `json(field, path)` string, compute the server-generated response alias. * e.g. `json(metadata, settings.theme)` → `metadata_settings_theme_json` * e.g. `json(metadata, tags[0])` → `metadata_tags_0_json` */ type JsonFieldAlias = S extends `json(${infer Field}, ${infer Path})` ? `${Field}_${NormalizeJsonPath}_json` : never; type QueryFunctions = { datetime: DateTimeFunctions; date: DateFunctions; time: TimeFunctions; json: ArrayFunctions; csv: ArrayFunctions; }; /** * Permute [function, field] combinations */ type PermuteFields = Fields extends string ? Funcs extends string ? [Fields, Funcs] : never : never; /** * Get all many relations on an item */ type RelationalFunctions = keyof { [Key in RelationalFields as Extract> extends any[] ? Key : never]: Key }; /** * Create a map of function fields and their resulting output names */ type TranslateFunctionFields = { [F in PermuteFields as `${F[1]}(${F[0]})`]: `${F[0]}_${F[1]}` }; /** * Combine the various function types */ type FunctionFields = { [Type in keyof QueryFunctions]: TypeFunctionFields }[keyof QueryFunctions] | keyof TranslateFunctionFields, ArrayFunctions> | JsonFunctionFieldPartial | JsonFunctionField; /** * */ type TypeFunctionFields = keyof TranslateFunctionFields, QueryFunctions[Type$1]>; /** * Json function fields are dropped from the static output type because the server-generated alias * ({field}_{path}_json) cannot be computed from the type-level string. Access extracted values * with a cast or typed helper after the request. */ type JsonFunctionFieldAliases = { [K in JsonFunctionField]: never }; /** * Map json function fields to their original field name (the first argument) */ type JsonFunctionFieldOrigins = { [K in JsonFunctionField]: K extends `json(${infer F}, ${string})` ? F : never }; /** * Map all possible function fields on an item */ type MappedFunctionFields = Merge, ArrayFunctions>, TranslateFunctionFields, DateTimeFunctions> & TranslateFunctionFields, DateFunctions> & TranslateFunctionFields, TimeFunctions> & TranslateFunctionFields, ArrayFunctions> & JsonFunctionFieldAliases>; /** * Create a map of function fields with its original field name */ type FunctionFieldNames = { [F in PermuteFields as `${F[1]}(${F[0]})`]: F[0] }; /** * Map all possible function fields to name on an item */ type MappedFieldNames = Merge, ArrayFunctions>, FunctionFieldNames, DateTimeFunctions> & FunctionFieldNames, DateFunctions> & FunctionFieldNames, TimeFunctions> & FunctionFieldNames, ArrayFunctions> & JsonFunctionFieldOrigins>; //#endregion export { ArrayFunctions, DateFunctions, DateTimeFunctions, FunctionFields, JsonFieldAlias, JsonFunctionField, MappedFieldNames, MappedFunctionFields, PermuteFields, QueryFunctions, TimeFunctions, TypeFunctionFields }; //# sourceMappingURL=functions.d.cts.map