// Subset of @data-client/rest's packages/rest/src/pathTypes.ts (PathArgs and // its helpers); keep those in sync. // The non-`infer` pre-checks are matched without instantiating any types, // so plain keys (the common case) skip the inferring templates entirely. type CleanKey = S extends `"${string}"` ? S extends `"${infer K}"` ? K : S : S; type KeyName = K extends `*${string}` | `${string}}` ? CleanKey< K extends `*${infer N}}` ? N : K extends `*${infer N}` ? N : K extends `${infer N}}` ? N : K > : CleanKey; type KeyVal = K extends `*${string}` ? string[] : string | number; /** Parameters for a given path */ export type PathArgs = unknown extends S ? any : PathKeys extends never ? // unknown is identity for intersection ('&') unknown : KeysToArgs>; /** Computes the union of keys for a path string */ export type PathKeys = string extends S ? string : // cheap (non-inferring) pre-check before the 3-way escape split S extends `${string}\\${string}` ? S extends `${infer A}\\${':' | '*' | '}'}${infer B}` ? PathKeys | PathKeys : ColonSplits | StarSplits : ColonSplits | StarSplits; /** Characters that end a :param or *wildcard token */ type PathDelimiter = '/' | '\\' | '%' | '&' | '*' | ':' | '{' | ';' | ',' | '!' | '@'; /** Token after every ':' in S */ type ColonSplits = S extends `${string}:${infer K}` ? PathToken | ColonSplits : never; /** `*`-prefixed token after every '*' in S */ type StarSplits = S extends `${string}*${infer K}` ? `*${PathToken}` | StarSplits : never; /** Prefix of K up to (excluding) its first PathDelimiter. * * Fast path: no delimiter at all, or the first '/' ends a delimiter-free token. * The delimiter-union templates without `infer` are matched without instantiation. */ type PathToken = K extends `${string}${PathDelimiter}${string}` ? K extends `${infer H}/${string}` ? H extends `${string}${PathDelimiter}${string}` ? PathTokenSlow : H : PathTokenSlow : K; /** Cuts at the first occurrence of each delimiter (union); recursing on each * candidate converges on the shortest, delimiter-free prefix. */ type PathTokenSlow = K extends `${infer H}${PathDelimiter}${string}` ? PathToken : K; type KeysToArgs = OptionalArgs & (RequiredPathKeys extends never ? unknown : RequiredArgs); /** Wide keys (`string`, template patterns) keep the original key-remapping * form so index signatures (and their `keyof`) stay exactly the same. */ type HasWideKey = true extends ( Key extends string ? {} extends { [P in Key]: 1 } ? true : never : never ) ? true : false; // Literal keys: mapped over the computed names without an `as` clause. // `as` clauses get re-instantiated every time TypeScript asks whether the // mapped type is generic (on every relation check of hook/fetch params). // Each value is the KeyVal of the key(s) named N (inlined so errors show the // resolved type rather than an alias). type OptionalArgs = HasWideKey extends true ? { [K in Key as K extends `${string}}` ? KeyName : never]?: KeyVal } : { [N in KeyName>]?: | (N extends KeyName, `*${string}`>> ? string[] : never) | (N extends KeyName, `*${string}`>> ? string | number : never); }; type RequiredArgs = HasWideKey extends true ? { [K in Key as K extends `${string}}` ? never : KeyName]: KeyVal } : { [N in KeyName>]: | (N extends KeyName, `*${string}`>> ? string[] : never) | (N extends KeyName, `*${string}`>> ? string | number : never); }; type OptionalPathKeys = Extract; type RequiredPathKeys = Exclude;