feat: add option for `no` strict routing (#103)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
default is `strict`
authored by Yusuke Wada · committed by GitHub · parent 354dd79 · Browse files · 5 files·+140 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+140 added−32 removed
- tests:+88 added−13 removed
- other:+52 added−19 removed
Provenance
- Commit
c123d5f- Tree
8ab0e94- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
354dd79
5 files changed+140 added−32 removed
modified:src/utils/url.test.ts+29 added-13 removed
26 unmodified lines272829303131323334353637383940414243323334353637383940414243444546474849505152535455565758596061626310 unmodified lines26 unmodified lines expect(res[0]).toBe('id') expect(res[1]).toBe('([0-9]+)') })
describe('getPathFromURL', () => { it('getPathFromURL', () => { let path = getPathFromURL('https://example.com/') expect(path).toBe('/') path = getPathFromURL('https://example.com/hello') expect(path).toBe('/hello') path = getPathFromURL('https://example.com/hello/hey') expect(path).toBe('/hello/hey') path = getPathFromURL('https://example.com/hello?name=foo') expect(path).toBe('/hello') path = getPathFromURL('https://example.com/hello/hey?name=foo&name=bar') expect(path).toBe('/hello/hey') path = getPathFromURL('https://example.com/hello/hey#fragment') expect(path).toBe('/hello/hey') it('getPathFromURL - no trailing slash', () => { let path = getPathFromURL('https://example.com/') expect(path).toBe('/') path = getPathFromURL('https://example.com/hello') expect(path).toBe('/hello') path = getPathFromURL('https://example.com/hello/hey') expect(path).toBe('/hello/hey') path = getPathFromURL('https://example.com/hello?name=foo') expect(path).toBe('/hello') path = getPathFromURL('https://example.com/hello/hey?name=foo&name=bar') expect(path).toBe('/hello/hey') path = getPathFromURL('https://example.com/hello/hey#fragment') expect(path).toBe('/hello/hey') })
it('getPathFromURL - with trailing slash', () => { let path = getPathFromURL('https://example.com/hello/') expect(path).toBe('/hello/') path = getPathFromURL('https://example.com/hello/hey/') expect(path).toBe('/hello/hey/') })
it('getPathFromURL - no strict is false', () => { let path = getPathFromURL('https://example.com/hello/', { strict: false }) expect(path).toBe('/hello') path = getPathFromURL('https://example.com/hello/hey/', { strict: false }) expect(path).toBe('/hello/hey') }) })
describe('isAbsoluteURL', () => { it('absolute url', () => {10 unmodified linesmodified:src/utils/url.ts+11 added-1 removed
21 unmodified lines222324252627282926303132333435363738394010 unmodified lines21 unmodified lines } return null}
type Params = { strict: boolean}
export const getPathFromURL = (url: string): string => {export const getPathFromURL = (url: string, params: Params = { strict: true }): string => { // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same // default is true if (!params.strict && url.endsWith('/')) { url = url.slice(0, -1) }
const match = url.match(URL_REGEXP) if (match) { return match[5] }10 unmodified linesmodified:src/hono.ts+31 added-18 removed
49 unmodified lines50 return this.node.search(method, path)51 }52 }53 54+type Init = {55+ strict?: boolean56+}57+58 export class Hono {59 router: Router<Handler[]>60 middlewareRouters: Router<MiddlewareHandler>[]61 tempPath: string62+ strict: boolean63 59- constructor() {64+ constructor(init: Init = { strict: true }) {65 this.router = new Router()66 this.middlewareRouters = []67 this.tempPath = '/'68+ this.strict = init.strict // strict routing - default is true69 }70 71 /* HTTP METHODS */66- get<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;67- get(arg: Handler<never>, ...args: Handler<never>[]): Hono;72+ get<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono73+ get(arg: Handler<never>, ...args: Handler<never>[]): Hono74 get(arg: string | Handler, ...args: Handler[]): Hono {75 return this.addRoute('get', arg, ...args)76 }77+71- post<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;72- post(arg: Handler, ...args: Handler[]): Hono;78+ post<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono79+ post(arg: Handler, ...args: Handler[]): Hono80 post(arg: string | Handler, ...args: Handler[]): Hono {81 return this.addRoute('post', arg, ...args)82 }83+76- put<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;77- put(arg: Handler, ...args: Handler[]): Hono;84+ put<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono85+ put(arg: Handler, ...args: Handler[]): Hono86 put(arg: string | Handler, ...args: Handler[]): Hono {87 return this.addRoute('put', arg, ...args)88 }89+81- head<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;82- head(arg: Handler, ...args: Handler[]): Hono;90+ head<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono91+ head(arg: Handler, ...args: Handler[]): Hono92 head(arg: string | Handler, ...args: Handler[]): Hono {93 return this.addRoute('head', arg, ...args)94 }95+86- delete<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;87- delete(arg: Handler, ...args: Handler[]): Hono;96+ delete<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono97+ delete(arg: Handler, ...args: Handler[]): Hono98 delete(arg: string | Handler, ...args: Handler[]): Hono {99 return this.addRoute('delete', arg, ...args)100 }101+91- options<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;92- options(arg: Handler, ...args: Handler[]): Hono;102+ options<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono103+ options(arg: Handler, ...args: Handler[]): Hono104 options(arg: string | Handler, ...args: Handler[]): Hono {105 return this.addRoute('options', arg, ...args)106 }107+96- patch<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;97- patch(arg: Handler, ...args: Handler[]): Hono;108+ patch<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono109+ patch(arg: Handler, ...args: Handler[]): Hono110 patch(arg: string | Handler, ...args: Handler[]): Hono {111 return this.addRoute('patch', arg, ...args)112 }113 13 unmodified lines127 search128 connect129 */130 119- all<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]) : Hono;120- all(arg: Handler<never>, ...args: Handler<never>[]): Hono;131+ all<Path extends string>(arg: Path, ...args: Handler<ParamKeys<Path>>[]): Hono132+ all(arg: Handler<never>, ...args: Handler<never>[]): Hono133 all(arg: string | Handler, ...args: Handler[]): Hono {134 return this.addRoute('all', arg, ...args)135 }136 29 unmodified lines166 return this.router.match(method, path)167 }168 169 async dispatch(request: Request, env?: Env, event?: FetchEvent): Promise<Response> {158- const [method, path] = [request.method, getPathFromURL(request.url)]170+ const path = getPathFromURL(request.url, { strict: this.strict })171+ const method = request.method172 173 const result = await this.matchRoute(method, path)174 175 // Methods for Request object81 unmodified linesmodified:test/hono.test.ts+59 added
48 unmodified lines49 expect(res.status).toBe(404)50 })51 })52 53+describe('strict parameter', () => {54+ describe('strict is true with not slash', () => {55+ const app = new Hono()56+57+ app.get('/hello', (c) => {58+ return c.text('/hello')59+ })60+61+ it('/hello/ is not found', async () => {62+ let req = new Request('http://localhost/hello')63+ let res = await app.dispatch(req)64+ expect(res).not.toBeNull()65+ expect(res.status).toBe(200)66+ req = new Request('http://localhost/hello/')67+ res = await app.dispatch(req)68+ expect(res).not.toBeNull()69+ expect(res.status).toBe(404)70+ })71+ })72+73+ describe('strict is true with slash', () => {74+ const app = new Hono()75+76+ app.get('/hello/', (c) => {77+ return c.text('/hello/')78+ })79+80+ it('/hello is not found', async () => {81+ let req = new Request('http://localhost/hello/')82+ let res = await app.dispatch(req)83+ expect(res).not.toBeNull()84+ expect(res.status).toBe(200)85+ req = new Request('http://localhost/hello')86+ res = await app.dispatch(req)87+ expect(res).not.toBeNull()88+ expect(res.status).toBe(404)89+ })90+ })91+92+ describe('strict is false', () => {93+ const app = new Hono({ strict: false })94+95+ app.get('/hello', (c) => {96+ return c.text('/hello')97+ })98+99+ it('/hello and /hello/ are treated as the same', async () => {100+ let req = new Request('http://localhost/hello')101+ let res = await app.dispatch(req)102+ expect(res).not.toBeNull()103+ expect(res.status).toBe(200)104+ req = new Request('http://localhost/hello/')105+ res = await app.dispatch(req)106+ expect(res).not.toBeNull()107+ expect(res.status).toBe(200)108+ })109+ })110+})111+112 describe('Routing', () => {113 const app = new Hono()114 115 it('Return it self', async () => {269 unmodified linesmodified:README.md+10 added
122 unmodified lines123 return c.text('Custom 404 Error', 404)124 })125 ```126 127+### no strict128+129+If `strict` is set `false`, `/hello`and`/hello/` are treated the same:130+131+```js132+const app = new Hono({ strict: false })133+134+app.get('/hello', (c) => c.text('/hello or /hello/'))135+```136+137 ## async/await138 139 ```js140 app.get('/fetch-url', async (c) => {319 unmodified lines