import { AnyAllRouteInfo, DefaultAllRouteInfo, RouteInfoByPath, } from './routeInfo' import { ParsedLocation, LocationState } from './router' import { Expand, NoInfer, PickRequired, UnionToIntersection, Updater, } from './utils' export type LinkInfo = | { type: 'external' href: string } | { type: 'internal' next: ParsedLocation handleFocus: (e: any) => void handleClick: (e: any) => void handleEnter: (e: any) => void handleLeave: (e: any) => void isActive: boolean disabled?: boolean } type CleanPath = T extends `${infer L}//${infer R}` ? CleanPath<`${CleanPath}/${CleanPath}`> : T extends `${infer L}//` ? `${CleanPath}/` : T extends `//${infer L}` ? `/${CleanPath}` : T export type Split = S extends unknown ? string extends S ? string[] : S extends string ? CleanPath extends '' ? [] : TIncludeTrailingSlash extends true ? CleanPath extends `${infer T}/` ? [...Split, '/'] : CleanPath extends `/${infer U}` ? Split : CleanPath extends `${infer T}/${infer U}` ? [...Split, ...Split] : [S] : CleanPath extends `${infer T}/${infer U}` ? [...Split, ...Split] : S extends string ? [S] : never : never : never export type ParsePathParams = Split[number] extends infer U ? U extends `$${infer V}` ? V : never : never type Join = T extends [] ? '' : T extends [infer L extends string] ? L : T extends [infer L extends string, ...infer Tail extends [...string[]]] ? CleanPath<`${L}/${Join}`> : never export type RelativeToPathAutoComplete< AllPaths extends string, TFrom extends string, TTo extends string, SplitPaths extends string[] = Split, > = TTo extends `..${infer _}` ? SplitPaths extends [ ...Split, false>, ...infer TToRest, ] ? `${CleanPath< Join< [ ...Split, ...( | TToRest | (Split< ResolveRelativePath, false >['length'] extends 1 ? never : ['../']) ), ] > >}` : never : TTo extends `./${infer RestTTo}` ? SplitPaths extends [ ...Split, ...Split, ...infer RestPath, ] ? `${TTo}${Join}` : never : | (TFrom extends `/` ? never : SplitPaths extends [...Split, ...infer RestPath] ? Join extends { length: 0 } ? never : './' : never) | (TFrom extends `/` ? never : '../') | AllPaths export type NavigateOptions< TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.', > = ToOptions & { // Whether to replace the current history stack instead of pushing a new one replace?: boolean } export type ToOptions< TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.', TResolvedTo = ResolveRelativePath>, > = { to?: ToPathOption // The new has string or a function to update it hash?: Updater // State to pass to the history stack state?: LocationState // The source route path. This is automatically set when using route-level APIs, but for type-safe relative routing on the router itself, this is required from?: TFrom // // When using relative route paths, this option forces resolution from the current path, instead of the route API's path or `from` path // fromCurrent?: boolean } & CheckPath, {}> & SearchParamOptions & PathParamOptions export type SearchParamOptions< TAllRouteInfo extends AnyAllRouteInfo, TFrom, TTo, TFromSchema = Expand< UnionToIntersection< TAllRouteInfo['fullSearchSchema'] & RouteInfoByPath extends never ? {} : RouteInfoByPath['fullSearchSchema'] > >, // Find the schema for the new path, and make optional any keys // that are already defined in the current schema TToSchema = Partial< RouteInfoByPath['fullSearchSchema'] > & Omit< RouteInfoByPath['fullSearchSchema'], keyof PickRequired< RouteInfoByPath['fullSearchSchema'] > >, TFromFullSchema = Expand< UnionToIntersection >, TToFullSchema = Expand< UnionToIntersection >, > = keyof PickRequired extends never ? { search?: true | SearchReducer } : { search: SearchReducer } type SearchReducer = | { [TKey in keyof TTo]: TTo[TKey] } | ((current: TFrom) => TTo) export type PathParamOptions< TAllRouteInfo extends AnyAllRouteInfo, TFrom, TTo, TFromSchema = Expand< UnionToIntersection< RouteInfoByPath extends never ? {} : RouteInfoByPath['allParams'] > >, // Find the schema for the new path, and make optional any keys // that are already defined in the current schema TToSchema = Partial['allParams']> & Omit< RouteInfoByPath['allParams'], keyof PickRequired['allParams']> >, TFromFullParams = Expand< UnionToIntersection >, TToFullParams = Expand< UnionToIntersection >, > = keyof PickRequired extends never ? { params?: ParamsReducer } : { params: ParamsReducer } type ParamsReducer = TTo | ((current: TFrom) => TTo) export type ToPathOption< TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.', > = | TTo | RelativeToPathAutoComplete< TAllRouteInfo['routePaths'], NoInfer extends string ? NoInfer : '', NoInfer & string > export type ToIdOption< TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.', > = | TTo | RelativeToPathAutoComplete< TAllRouteInfo['routeIds'], NoInfer extends string ? NoInfer : '', NoInfer & string > export interface ActiveOptions { exact?: boolean includeHash?: boolean } export type LinkOptions< TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, TFrom extends TAllRouteInfo['routePaths'] = '/', TTo extends string = '.', > = NavigateOptions & { // The standard anchor tag target attribute target?: HTMLAnchorElement['target'] // Defaults to `{ exact: false, includeHash: false }` activeOptions?: ActiveOptions // If set, will preload the linked route on hover and cache it for this many milliseconds in hopes that the user will eventually navigate there. preload?: false | 'intent' // When preloaded, the preloaded result will be considered "fresh" for this duration in milliseconds preloadMaxAge?: number // When preloaded and subsequently inactive, the preloaded result will remain in memory for this duration in milliseconds preloadGcMaxAge?: number // Delay intent preloading by this many milliseconds. If the intent exits before this delay, the preload will be cancelled. preloadDelay?: number // If true, will render the link without the href attribute disabled?: boolean } export type CheckRelativePath< TAllRouteInfo extends AnyAllRouteInfo, TFrom, TTo, > = TTo extends string ? TFrom extends string ? ResolveRelativePath extends TAllRouteInfo['routePaths'] ? {} : { Error: `${TFrom} + ${TTo} resolves to ${ResolveRelativePath< TFrom, TTo >}, which is not a valid route path.` 'Valid Route Paths': TAllRouteInfo['routePaths'] } : {} : {} export type CheckPath< TAllRouteInfo extends AnyAllRouteInfo, TPath, TPass, > = Exclude extends never ? TPass : CheckPathError> export type CheckPathError< TAllRouteInfo extends AnyAllRouteInfo, TInvalids, > = Expand<{ Error: `${TInvalids extends string ? TInvalids : never} is not a valid route path.` 'Valid Route Paths': TAllRouteInfo['routePaths'] }> export type CheckId< TAllRouteInfo extends AnyAllRouteInfo, TPath, TPass, > = Exclude extends never ? TPass : CheckIdError> export type CheckIdError< TAllRouteInfo extends AnyAllRouteInfo, TInvalids, > = Expand<{ Error: `${TInvalids extends string ? TInvalids : never} is not a valid route ID.` 'Valid Route IDs': TAllRouteInfo['routeIds'] }> export type ResolveRelativePath = TFrom extends string ? TTo extends string ? TTo extends '.' ? TFrom : TTo extends `./` ? Join<[TFrom, '/']> : TTo extends `./${infer TRest}` ? ResolveRelativePath : TTo extends `/${infer TRest}` ? TTo : Split extends ['..', ...infer ToRest] ? Split extends [...infer FromRest, infer FromTail] ? ToRest extends ['/'] ? Join<[...FromRest, '/']> : ResolveRelativePath, Join> : never : Split extends ['.', ...infer ToRest] ? ToRest extends ['/'] ? Join<[TFrom, '/']> : ResolveRelativePath> : CleanPath, ...Split]>> : never : never export type ValidFromPath< TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo, > = | undefined | (string extends TAllRouteInfo['routePaths'] ? string : TAllRouteInfo['routePaths'])