@bearstudio/lunalink
Version:
Lightweight TypeScript library to efficiently maintain and build URLs
37 lines (35 loc) • 1.64 kB
text/typescript
type ExplicitAny = any;
/** Accepted types for path parameters */
type ParamValue = string | number | boolean;
/** Type to extract params from a path */
type ExtractParams<Path extends string> = Path extends `${string}:${infer Param}?` ? {
[K in Param]: ParamValue;
} : Path extends `${string}:${infer Param}/${infer Rest}` ? {
[K in Param]: ParamValue;
} & ExtractParams<`/${Rest}`> : Path extends `${string}:${infer Param}.${infer Rest}` ? {
[K in Param]: ParamValue;
} & ExtractParams<`${Rest}`> : Path extends `${string}:${infer Param}` ? {
[K in Param]: ParamValue;
} : {};
/** Type to add more params */
type ParamsDefaultType = Record<string, ExplicitAny>;
type Config = {
baseURL?: string | URL;
/**
* Escape the parameters so the query parameters are safely injected.
* @default encodeURIComponent
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
* @param value The value to encode
* @returns the stringified and escaped value
*/
encodeURIComponent?: (value: string | number | boolean) => string;
};
declare function lunalink<Path extends string>(path: Path, params: ExtractParams<Path> & ParamsDefaultType, config?: Config): string;
/**
* @param path1 first path to join (can be the start or the end)
* @param path2 second path to join (can be the start or the end)
* @param separator The separator to use (default to `/`)
* @returns The joined paths using the provided separator
*/
declare function join(path1: string, path2: string, separator?: string): string;
export { type ExtractParams, join, lunalink };