Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 | /**
* Utility type to allow a type or undefined
*/
type Nullable<T> = T | undefined;
/**
* Supported HTTP methods
*/
type HttpMethod = "get" | "post" | "put" | "delete";
/**
* Schema type for path parameter
*/
type SchemaParam = string | number | ReadonlyArray<string | number> | undefined;
/**
* Schema type for path parameters
*/
type SchemaParams = Record<string, SchemaParam>;
/**
* Schema type of query parameters (non-object values only)
*/
type SchemaQueries = Record<
string,
| boolean
| number
| string
| null
| bigint
| undefined
| ReadonlyArray<boolean | number | string | null | bigint | undefined>
>;
/**
* Schema type for URL construction parameters
*/
export type Options = { params?: SchemaParams; queries?: SchemaQueries; hash?: string };
/**
* Utility type that extracts path parameters from an endpoint string and generates object types with those as keys
*
* @example
* ExtractParams<"/users/[userId]/posts/[postId]">
* // { userId: string | number, postId: string | number }
*
* ExtractParams<"/path/[...params]">
* // { params: (string | number)[] }
*
* ExtractParams<"/path/[[...params]]">
* // { params: (string | number)[] }
*/
type ExtractParams<Endpoint extends string> = Endpoint extends `${infer Before}[[...${infer Param}]]${infer After}`
? { [K in Param]: Extract<SchemaParam, ReadonlyArray<unknown>> } & ExtractParams<Before> & ExtractParams<After>
: Endpoint extends `${infer Before}[...${infer Param}]${infer After}`
? { [K in Param]: Extract<SchemaParam, ReadonlyArray<unknown>> } & ExtractParams<Before> & ExtractParams<After>
: Endpoint extends `${string}[${infer Param}]${infer After}`
? { [K in Param]: Extract<SchemaParam, string | number> } & ExtractParams<After>
: unknown;
/**
* Utility type to determine if a path parameter exists from an endpoint string
*
* @example
* HasParams<"/users/[id]">
* // true
*
* HasParams<"/users">
* // false
*/
type HasParams<Endpoint extends string> = keyof ExtractParams<Endpoint> extends never ? false : true;
/**
* Utility type that defines the type of arguments the method expects, based on the endpoint string.
* - If the endpoint contains a path parameter (`[param]`): the `params` property is required
* - If the endpoint does not contain a path parameter: the `params` property is not allowed
* - `queries` are always optional.
*
* @example
* ExpectedOptions<"/users/[id]">
* // { params: { id: string | number }; queries?: SchemaQueries; hash?: string }
*
* ExpectedOptions<"/users">
* // { queries?: SchemaQueries; hash?: string }
*/
type ExpectedOptions<Endpoint extends string> = HasParams<Endpoint> extends true
? { params: ExtractParams<Endpoint>; queries?: Simplify<SchemaQueries>; hash?: string }
: { queries?: Simplify<SchemaQueries>; hash?: string };
/**
* Utility type to extract only nullable properties
*
* @example
* PickNullable<{ required: string; optional?: string }>
* // { optional?: string }
*/
type PickNullable<T> = {
[P in keyof T as undefined extends T[P] ? P : never]: T[P];
};
/**
* Utility type to extract only Required properties
*
* @example
* PickRequired<{ required: string; optional?: string }>
* // { required: string }
*/
type PickRequired<T> = {
[P in keyof T as undefined extends T[P] ? never : P]: T[P];
};
/**
* Utility type that recursively determines if a required property is present
*
* @example
* HasRequired<{ required: { optional?: string } }>
* // true
*
* HasRequired<{ optional?: { required: string } }>
* // true
*
* HasRequired<{ optional?: { optional?: string } }>
* // false
*/
type HasRequired<T> = (
T extends ReadonlyArray<unknown>
? false
: T extends object
? keyof PickRequired<T> extends never
? HasRequired<T[keyof T]>
: true
: false
) extends false
? false
: true;
/**
* Utility type that makes Nullable properties optional by adding `?` (optional modifier).
*
* @example
* Optional<{ required: string; optional: Nullable<string> }>
* // { required: string; optional?: string }
*/
type Optional<T> = {
[K in keyof PickNullable<T>]?: T[K];
} & {
[K in keyof PickRequired<T>]: T[K];
};
/**
* Utility type that makes all properties of an object readonly
*
* @example
* ReadonlyProperties<{ array: string[] }>
* // { array: readonly string[] }
*/
type ReadonlyProperties<T> = {
[K in keyof T]: Readonly<T[K]>;
};
// biome-ignore lint/complexity/noBannedTypes: Using empty object type for specific purpose
type EmptyObject = {};
/**
* Converts schema definition into optional keys if it has no required fields.
* Readonly is marked to accept as const arguments.
* Please refer to each ActualXXXX type for more details.
*/
type ActualOptions<Schema extends Options, Endpoint extends string> = (Schema["params"] extends Options["params"]
? ActualParams<Schema["params"], Endpoint>
: EmptyObject) &
(Schema["queries"] extends Options["queries"] ? ActualQueries<Schema["queries"]> : EmptyObject) &
(Schema["hash"] extends Options["hash"] ? ActualHash<Schema["hash"]> : EmptyObject);
/**
* Makes path parameters deeply optional if they are optional catch-all parameters.
*
* @example
* ActualParams<{ param1: string; param2: string[] }, "/[param1]/[[...param2]]">
* // { params: { param1: string; param2?: string[] } }
*
* ActualParams<{ param: string[] }, "/[[...param]]">
* // { params?: { param?: string[] } }
*/
type ActualParams<Schema extends Options["params"], Endpoint extends string> = {
[K in keyof Schema as K extends OptionalCatchAllSegments<Endpoint> ? K : never]?: Readonly<Schema[K]>;
} & {
[K in keyof Schema as K extends OptionalCatchAllSegments<Endpoint> ? never : K]: Readonly<Schema[K]>;
} extends infer R
? HasRequired<R> extends true
? { params: R }
: { params?: R }
: never;
/**
* Makes query parameters deeply optional if there are no required fields.
*
* @example
* ActualQueries<{ required: string; optional?: number }>
* // { queries: { required: readonly string; optional?: readonly number } }
*
* ActualQueries<{ optional1?: string; optional2?: number }>
* // { queries?: { optional1?: readonly string; optional2?: readonly number } }
*/
type ActualQueries<Schema extends Options["queries"]> = Optional<ReadonlyProperties<Schema>> extends infer R
? HasRequired<R> extends true
? { queries: R }
: { queries?: R }
: never;
/**
* Keeps hash as is, since it's always optional in the schema definition.
*
* @example
* ActualHash<"hash">
* // { hash?: "hash" }
*/
type ActualHash<Schema extends Options["hash"]> = { hash?: Schema };
/**
* Utility type that extracts optional catch-all parameter names from an endpoint string
*
* @example
* OptionalCatchAllSegments<"/path/[[...params]]/[id]/[[...moreParams]]">
* // "params" | "moreParams"
*/
type OptionalCatchAllSegments<T extends string> = T extends `${string}[[...${infer Param}]]${infer After}`
? Param | OptionalCatchAllSegments<After>
: never;
/**
* Utility type that joins an array of strings or numbers into a single string
*
* @example
* JoinParams<["123", "456"]>
* // "123/456"
*/
type JoinParams<T extends ReadonlyArray<unknown>> = T extends readonly [
infer Head extends Extract<SchemaParam, string | number>,
...infer Tail extends ReadonlyArray<unknown>,
]
? Tail["length"] extends 0
? Head
: `${Head}/${JoinParams<Tail>}`
: T extends Extract<SchemaParam, ReadonlyArray<unknown>>
? T[number]
: never;
/**
* Utility type that trims multiple slashes from a path string
*
* @example
* TrimPath<"/path//to//resource/">
* // "/path/to/resource"
*/
type TrimPath<Path extends string> = Path extends `${infer Before}//${infer After}`
? `${TrimPath<`${Before}/${After}`>}`
: Path extends `${infer Before}/`
? `${Before}`
: Path;
/**
* Replace [param] in path with actual values
*
* @example
* ReplacePathParams<"/users/[userId]/posts/[postId]", { userId: "123"; postId: "456" }>
* // "/users/123/posts/456"
*
* ReplacePathParams<"/path/[...params]", { params: ["123", "456"] }>
* // "/path/123/456"
*
* ReplacePathParams<"/path/[[...params]]", { params: ["123", "456"] }>
* // "/path/123/456"
*/
type ReplacePathParams<
Path extends string,
Params extends SchemaParams,
> = Path extends `${infer Before}[[...${infer Param}]]${infer After}`
? Param extends keyof Params
? Params[Param] extends Extract<SchemaParam, ReadonlyArray<unknown>>
? `${ReplacePathParams<Before, Params>}${JoinParams<Params[Param]>}${ReplacePathParams<After, Params>}`
: never
: `${ReplacePathParams<Before, Params>}${ReplacePathParams<After, Params>}`
: Path extends `${infer Before}[...${infer Param}]${infer After}`
? Param extends keyof Params
? Params[Param] extends Extract<SchemaParam, ReadonlyArray<unknown>>
? `${ReplacePathParams<Before, Params>}${JoinParams<Params[Param]>}${ReplacePathParams<After, Params>}`
: never
: never
: Path extends `${infer Before}[${infer Param}]${infer After}`
? Param extends keyof Params
? Params[Param] extends Extract<SchemaParam, string | number>
? `${Before}${Params[Param]}${ReplacePathParams<After, Params>}`
: never
: never
: Path;
/**
* Utility type that removes optional catch-all parameters from the path
*
* @example
* ExcludeOptionalCatchAll<"/path/[[...params]]">
* // "/path/"
*/
type ExcludeOptionalCatchAll<Path extends string> = Path extends `${infer Before}[[...${string}]]${infer After}`
? `${Before}${ExcludeOptionalCatchAll<After>}`
: Path;
/**
* Applies path parameters if they exist
*
* @example
* ApplyParams<"/users/[userId]", { userId: 123 }>
* // "/users/123"
*
* ApplyParams<"/">
* // "/"
*/
type ApplyParams<Path extends string, Params extends Nullable<Optional<ExtractParams<Path>>>> = TrimPath<
keyof Params extends never
? ExcludeOptionalCatchAll<Path>
: Params extends SchemaParams
? ReplacePathParams<Path, Params>
: never
>;
/**
* Applies query parameters if they exist
*
* @example
* ApplyQueries<"/", { required: string }>
* // `/?${string}`
*
* ApplyQueries<"/", { optional?: string }>
* // `/${string}`
*
* ApplyQueries<"/">
* // "/"
*/
type ApplyQueries<Path extends string, Queries extends Nullable<SchemaQueries>> = keyof Queries extends never
? Path
: Queries extends SchemaQueries
? `${Path}${HasRequired<Queries> extends true ? "?" : ""}${string}`
: Path;
/**
* Applies hash fragment if defined
*
* @example
* ApplyHash<"/path", "hash">
* // "/path#hash"
*
* ApplyHash<"/path">
* // "/path"
*/
type ApplyHash<Path extends string, Hash extends Nullable<string>> = Hash extends string ? `${Path}#${Hash}` : Path;
/**
* Combines path, queries, and hash into a fully typed URL string
*
* @example
* ExtractPath<"/users/[userId]", { userId: 123 }, { search: string }>
* // `/users/123?${string}`
*/
type ExpectedPath<
Path extends string,
Params extends Nullable<Optional<ExtractParams<Path>>> = undefined,
Queries extends Nullable<SchemaQueries> = undefined,
Hash extends Nullable<string> = undefined,
> = ApplyHash<ApplyQueries<ApplyParams<Path, Params>, Queries>, Hash>;
/**
* Combines endpoint and schema definition to generate a concrete return type
* Refine type according to the presence or absence of path and query parameters in the schema definition
*
* @example
* ActualReturn<"/path/[param]", { params: { param: string }, queries: { q: string } }>
* // `/path/${string}?${string}`
*
* ActualReturn<"/path/[param1]/to/[param2]", { params: { param1: string; param2: number } }>
* // `/path/${string}/to/${number}`
*
* ActualReturn<"/path", { queries: { q: string } }>
* // `/path?${string}`
*/
type ActualReturn<Endpoint extends string, Options> = Endpoint extends `${infer Schema}://${infer Path}`
? `${Schema}://${ExpectedPath<
Path,
Options extends { params: infer Params extends Optional<ExtractParams<Path>> } ? Params : undefined,
Options extends { queries?: infer Queries extends Nullable<SchemaQueries> } ? Queries : undefined,
Options extends { hash: infer Hash extends string } ? Hash : undefined
>}`
: ExpectedPath<
Endpoint,
Options extends { params: infer Params extends Optional<ExtractParams<Endpoint>> } ? Params : undefined,
Options extends { queries?: infer Queries extends Nullable<SchemaQueries> } ? Queries : undefined,
Options extends { hash: infer Hash extends string } ? Hash : undefined
>;
/**
* Alias for methods with no parameters
*/
export type Empty = Nullable<Record<string, never>>;
/**
* Utility type that enforces mutual exclusivity between two types
*
* @example
* type T = Exclusive<{ a: string }, { b: number }>
* const x: T = { a: "" } // valid
* const y: T = { b: 0 } // valid
* const z: T = { a: "", b: 0 } // error
*/
type Exclusive<T, U> = (T & { [K in keyof U]?: never }) | (U & { [K in keyof T]?: never });
/**
* Utility type that simplifies a type by flattening its structure
*/
type Simplify<T> = { [K in keyof T]: T[K] } & {};
/**
* Function type that returns a URL string based on the schema definition
* - Accepts parameters depending on whether required fields exist
*/
type Method<BaseUrl extends string, Endpoint extends string, OptionsSchema> = OptionsSchema extends Empty
? () => `${BaseUrl}${Endpoint}`
: OptionsSchema extends Options
? HasRequired<ActualOptions<OptionsSchema, Endpoint>> extends true
? <Options extends ActualOptions<OptionsSchema, Endpoint>>(
options: Simplify<Options>,
) => `${BaseUrl}${ActualReturn<Endpoint, Options>}`
: <Options extends ActualOptions<OptionsSchema, Endpoint>>(
options?: Simplify<Options>,
) => `${BaseUrl}${ActualReturn<Endpoint, Options>}`
: never;
/**
* Expected schema definition:
* - Endpoint string as key
* - Object of HTTP methods as values
* - Each method can have its own schema definition
*/
export type ExpectedSchema<Schema> = {
[EndpointKey in keyof Schema]: EndpointKey extends string
? Schema[EndpointKey] extends Record<string, never>
? never
:
| Exclusive<Partial<Record<HttpMethod, ExpectedOptions<EndpointKey>>>, ExpectedOptions<EndpointKey>>
| (HasParams<EndpointKey> extends false ? Empty : never)
: never;
};
/**
* Actual schema returned from the function:
* - Each method is replaced with a typed URL generator
*/
export type ActualSchema<Schema, BaseUrl extends string> = {
[EndpointKey in keyof Schema]: EndpointKey extends string
? {
[Key in keyof Schema[EndpointKey] as Key extends HttpMethod ? Key : never]: Method<
BaseUrl,
EndpointKey,
Schema[EndpointKey][Key]
>;
} & {
[Key in "get" as Schema[EndpointKey] extends Empty | Options ? Key : never]: Method<
BaseUrl,
EndpointKey,
Schema[EndpointKey]
>;
}
: never;
};
|