Skip to content
Myr

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

5 files+140 added−32 removed

modified:src/utils/url.test.ts+29 added-13 removed
modified:src/utils/url.ts+11 added-1 removed
modified:src/hono.ts+31 added-18 removed
49 unmodified lines
50 return this.node.search(method, path)51 }52}5354type Init = {55 strict?: boolean56}5758export class Hono {59 router: Router<Handler[]>60 middlewareRouters: Router<MiddlewareHandler>[]61 tempPath: string62 strict: boolean6359 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 }7071 /* 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 }7771 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 }8376 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 }8981 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 }9586 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 }10191 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 }10796 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 lines
127 search128 connect129 */130119 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 lines
166 return this.router.match(method, path)167 }168169 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.method172173 const result = await this.matchRoute(method, path)174175 // Methods for Request object
81 unmodified lines
modified:test/hono.test.ts+59 added
48 unmodified lines
49 expect(res.status).toBe(404)50 })51})5253describe('strict parameter', () => {54 describe('strict is true with not slash', () => {55 const app = new Hono()5657 app.get('/hello', (c) => {58 return c.text('/hello')59 })6061 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 })7273 describe('strict is true with slash', () => {74 const app = new Hono()7576 app.get('/hello/', (c) => {77 return c.text('/hello/')78 })7980 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 })9192 describe('strict is false', () => {93 const app = new Hono({ strict: false })9495 app.get('/hello', (c) => {96 return c.text('/hello')97 })9899 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})111112describe('Routing', () => {113 const app = new Hono()114115 it('Return it self', async () => {
269 unmodified lines
modified:README.md+10 added
122 unmodified lines
123 return c.text('Custom 404 Error', 404)124})125```126127### no strict128129If `strict` is set `false`, `/hello`and`/hello/` are treated the same:130131```js132const app = new Hono({ strict: false })133134app.get('/hello', (c) => c.text('/hello or /hello/'))135```136137## async/await138139```js140app.get('/fetch-url', async (c) => {
319 unmodified lines