import type { FieldsSchema, ParseSchema } from "./fields"; import type { EntityBase } from "./entities"; /** * Schema takes an object type T and produces a schema for its fields. * It supports: * - Flat fields (primitive types), returning their keys as strings. * - Nested fields (objects), recursively applying Schema to nested objects. * - Arrays of objects, recursively applying Schema to the elements of the array. * * @template T - The object type for which the schema is generated. * * Example: * type Person = { * name: string; * age: number; * address: { * city: string; * country: string; * details: { * zip: string; * street: string; * }; * }; * }; * * type PersonSchema = Schema; * // Resulting type: * // [ * // 'name', * // 'age', * // { address: ['city', 'country', { details: ['zip', 'street'] }] } * // ] */ type Primitive = string | number | boolean | symbol | bigint; type SchemaItem = T extends Primitive ? ToString : SubSchema; type ToString = T extends string | number ? `${T}` : never; type SubSchema = K extends string ? { readonly [P in K]-?: SchemaFromType>; } : never; export type SchemaFromType = { [K in keyof T]-?: T[K] extends Array ? SchemaItem : SchemaItem; }[keyof T] extends infer U ? ReadonlyArray : never; export type Schema = string | SchemaFromType; /** * Converts a `FieldsSchema` into a structured `FilteringSchema`. * * This utility is useful for transforming an array-based schema representation * into a hierarchical object structure, preserving string fields and nested * object fields for filtering purposes. * * @typeParam T - The input schema type, constrained to an array of fields (`FieldsSchema`). * * Example: * ```typescript * type FieldsSchema = [ * "name", * { details: ["zip", "city"] } * ]; * * type Result = ToFilteringSchema; * * // Expected Result: * // { * // name: "name"; * // details: { * // zip: "zip"; * // city: "city"; * // }; * // } * ``` */ export type FilteringSchema = Record; export type FilteringSchemaField = string | { [key: string]: FilteringSchemaField; }; export type ToFilteringSchema = { [K in T[number] as K extends string ? K : K extends Record ? keyof K : never]: K extends string ? K : K extends Record ? ToFilteringSchema : never; } extends infer O ? { [K in keyof O]: O[K]; } : never; /** * FilterFields is a utility type that filters fields of object type T based on a given field schema F. * F can be a mix of flat fields (strings) and nested fields (objects). * It recursively extracts the specified fields, handling both flat and nested objects. * * @template T - The object type to filter. * @template F - A schema (array of fields) describing which fields to extract from T. * * Example: * const person = { * name: 'John', * age: 30, * address: { * city: 'New York', * country: 'USA', * details: { * zip: '10001', * street: '5th Avenue', * } * } * }; * * type FilteredPerson = FilterFields; * // The resulting type will be: * // { * // name: string; * // address: { * // city: string; * // details: { * // zip: string; * // }; * // }; * // } */ type FilterFields = T extends Array ? Array> : T extends object ? { [K in keyof T as K extends keyof Schema ? K : never]: K extends keyof Schema ? Schema[K] extends FilteringSchema ? FilterFields : FilterFields : never; } & (T extends { id: infer IdType; } ? { id: IdType; } : {}) extends infer O ? { [K in keyof O]: O[K]; } : never : T; /** * Entity is a type that represents an entity with optional schema-based filtering. * * @template T - The base entity type (e.g., Agile, Project) from which fields will be filtered. * @template TSchema - A schema describing the fields to include in the resulting entity. * If undefined, the full `EntityBase` schema is used. * * Example: * type Person = { * name: string; * age: number; * address: { * city: string; * country: string; * }; * }; * * type FilteredPerson = Entity; * // Resulting type: * // { * // name: string; * // address: { * // city: string; * // }; * // } * * type FullPerson = Entity; * // Resulting type: Schema */ export type Entity = TSchema extends undefined ? SchemaFromType : TSchema extends FieldsSchema ? FilterFields> : TSchema extends string ? FilterFields>> : never; export type QueryParamBuilder = (value?: T) => string | string[]; export type DeepPartial = { [K in keyof T]?: T[K] extends object ? DeepPartial : T[K]; }; /** * DurationPresentation is a type that enforces valid YouTrack time duration formats * with support for different language unit abbreviations * * Supports the following patterns: * - Single unit format: "90m", "2w", "34h", "5d" * - Multi-unit format: "1w 5d 4h 30m", "2d 4h", "1w 2d", etc. * * Language units can be specified via the generic type parameter: * - Default (English): ["w", "d", "h", "m"] (weeks, days, hours, minutes) * - Other languages can use their own abbreviations * * Example: * ```typescript * // Default English units * const valid1: DurationPresentation = "90m"; * const valid2: DurationPresentation = "2w 5d 4h 30m"; * * // Russian units * type RussianUnits = ["н", "д", "ч", "м"]; * const russianTime: DurationPresentation = "2н 5д 4ч 30м"; * * // German units * type GermanUnits = ["w", "t", "s", "m"]; * const germanTime: DurationPresentation = "2w 5t 4s 30m"; * * // Invalid presentations * const invalid1: DurationPresentation = "90"; // Missing unit * const invalid2: DurationPresentation = "2h 5m 1d"; // Incorrect order * const invalid3: DurationPresentation = "2m 2m"; // Duplicate unit * ``` */ export type EnglishUnits = ["w", "d", "h", "m"]; export type RussianUnits = ["н", "д", "ч", "м"]; export type GermanUnits = ["w", "t", "s", "m"]; export type DurationPresentation = `${number}${T[0]}` | `${number}${T[1]}` | `${number}${T[2]}` | `${number}${T[3]}` | `${number}${T[0]} ${number}${T[1]}` | `${number}${T[0]} ${number}${T[2]}` | `${number}${T[0]} ${number}${T[3]}` | `${number}${T[1]} ${number}${T[2]}` | `${number}${T[1]} ${number}${T[3]}` | `${number}${T[2]} ${number}${T[3]}` | `${number}${T[0]} ${number}${T[1]} ${number}${T[2]}` | `${number}${T[0]} ${number}${T[1]} ${number}${T[3]}` | `${number}${T[0]} ${number}${T[2]} ${number}${T[3]}` | `${number}${T[1]} ${number}${T[2]} ${number}${T[3]}` | `${number}${T[0]} ${number}${T[1]} ${number}${T[2]} ${number}${T[3]}`; /** * AtLeastOne type makes all properties of T optional but requires at least one of them to be present * * @template T - The object type whose properties should be made optional but at least one required * * Example: * ```typescript * interface User { * name: string; * email: string; * phone: string; * } * * // This allows any combination of properties, but at least one must be present * type UserUpdate = AtLeastOne; * * const validUpdate1: UserUpdate = { name: "John" }; // Valid - has name * const validUpdate2: UserUpdate = { email: "john@example.com", phone: "123-456-7890" }; // Valid - has email and phone * const invalidUpdate: UserUpdate = {}; // Invalid - empty object not allowed * ``` */ export type AtLeastOne; }> = Partial & U[keyof U]; export {};