2022-07-02 08:09:45 +02:00
|
|
|
export const METHOD_NAME_ALL = 'ALL' as const
|
|
|
|
export const METHOD_NAME_ALL_LOWERCASE = 'all' as const
|
2023-06-03 02:07:33 +02:00
|
|
|
export const METHODS = ['get', 'post', 'put', 'delete', 'options', 'patch'] as const
|
2022-07-02 08:09:45 +02:00
|
|
|
|
|
|
|
export interface Router<T> {
|
2023-05-17 17:05:28 +02:00
|
|
|
name: string
|
2022-07-02 08:09:45 +02:00
|
|
|
add(method: string, path: string, handler: T): void
|
2023-10-16 01:39:37 +02:00
|
|
|
match(method: string, path: string): Result<T>
|
2022-07-02 08:09:45 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 01:39:37 +02:00
|
|
|
export type ParamIndexMap = Record<string, number>
|
|
|
|
export type ParamStash = string[]
|
|
|
|
export type Params = Record<string, string>
|
|
|
|
export type Result<T> = [[T, ParamIndexMap][], ParamStash] | [[T, Params][]]
|
|
|
|
/*
|
|
|
|
The router returns the result of `match` in either format.
|
|
|
|
|
|
|
|
[[handler, paramIndexMap][], paramArray]
|
|
|
|
e.g.
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[middlewareA, {}], // '*'
|
|
|
|
[funcA, {'id': 0}], // '/user/:id/*'
|
|
|
|
[funcB, {'id': 0, 'action': 1}], // '/user/:id/:action'
|
|
|
|
],
|
|
|
|
['123', 'abc']
|
|
|
|
]
|
|
|
|
|
|
|
|
[[handler, params][]]
|
|
|
|
e.g.
|
|
|
|
[
|
|
|
|
[
|
|
|
|
[middlewareA, {}], // '*'
|
|
|
|
[funcA, {'id': '123'}], // '/user/:id/*'
|
|
|
|
[funcB, {'id': '123', 'action': 'abc'}], // '/user/:id/:action'
|
|
|
|
]
|
|
|
|
]
|
|
|
|
*/
|
2022-09-12 13:49:18 +02:00
|
|
|
|
|
|
|
export class UnsupportedPathError extends Error {}
|