| 1 | /** |
| 2 | * A radix-tree HTTP router used by the unstable HTTP routing modules. |
| 3 | * |
| 4 | * @stability unstable |
| 5 | * @since 4.0.0 |
| 6 | */ |
| 7 | import * as internal from "./FindMyWay/internal/router.ts" |
| 8 | |
| 9 | /* |
| 10 | * MIT License |
| 11 | * |
| 12 | * Copyright (c) 2017-2019 Tomas Della Vedova |
| 13 | * |
| 14 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 15 | * of this software and associated documentation files (the "Software"), to deal |
| 16 | * in the Software without restriction, including without limitation the rights |
| 17 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 18 | * copies of the Software, and to permit persons to whom the Software is |
| 19 | * furnished to do so, subject to the following conditions: |
| 20 | * |
| 21 | * The above copyright notice and this permission notice shall be included in all |
| 22 | * copies or substantial portions of the Software. |
| 23 | * |
| 24 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 25 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 26 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 27 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 28 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 29 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 30 | * SOFTWARE. |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * Configuration for router path matching. |
| 35 | * |
| 36 | * @stability unstable |
| 37 | * @category models |
| 38 | * @since 4.0.0 |
| 39 | */ |
| 40 | export interface RouterConfig { |
| 41 | readonly ignoreTrailingSlash: boolean |
| 42 | readonly ignoreDuplicateSlashes: boolean |
| 43 | readonly caseSensitive: boolean |
| 44 | readonly maxParamLength: number |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Route path accepted by the router. |
| 49 | * |
| 50 | * @stability unstable |
| 51 | * @category models |
| 52 | * @since 4.0.0 |
| 53 | */ |
| 54 | export type PathInput = `/${string}` | "*" |
| 55 | |
| 56 | /** |
| 57 | * Mutable router used to register and resolve route handlers. |
| 58 | * |
| 59 | * @stability unstable |
| 60 | * @category models |
| 61 | * @since 4.0.0 |
| 62 | */ |
| 63 | export interface Router<A> { |
| 64 | readonly on: (method: string | Iterable<string>, path: PathInput, handler: A) => void |
| 65 | readonly all: (path: PathInput, handler: A) => void |
| 66 | readonly find: (method: string, url: string) => FindResult<A> | undefined |
| 67 | readonly has: (method: string, url: string) => boolean |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Result of a successful route lookup. |
| 72 | * |
| 73 | * @stability unstable |
| 74 | * @category models |
| 75 | * @since 4.0.0 |
| 76 | */ |
| 77 | export interface FindResult<A> { |
| 78 | readonly handler: A |
| 79 | readonly params: Record<string, string | undefined> |
| 80 | readonly searchParams: Record<string, string | Array<string>> |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Creates an empty mutable router. |
| 85 | * |
| 86 | * @stability unstable |
| 87 | * @category constructors |
| 88 | * @since 4.0.0 |
| 89 | */ |
| 90 | export const make: <A>(options?: Partial<RouterConfig>) => Router<A> = internal.make |