feat: [BREAKING] deprecate `Route`, use `Hono` instead (#237)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent d8accbc · Browse files · 3 files·+100 added−87 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+100 added−87 removed
- tests:+79 added−32 removed
- other:+21 added−55 removed
Provenance
- Commit
ea1fd00- Tree
603c6f5- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
d8accbc
3 files changed+100 added−87 removed
modified:src/hono.test.ts+79 added-32 removed
1 import type { Context } from './context'2 import type { Next } from './hono'3-import { Hono, Route } from './hono'3+import { Hono } from './hono'4 import { poweredBy } from './middleware/powered-by'5 6 describe('GET Request', () => {7 const app = new Hono()563 unmodified lines571 expect(res.headers.get('X-Header-2')).toBe('bar')572 })573 })574 575-describe('`Route` with app.route', () => {576- const app = new Hono()575+describe('Hono with `app.route`', () => {576 describe('Basic', () => {578- const route = new Route()579- route.get('/post', (c) => c.text('GET /POST'))580- route.post('/post', (c) => c.text('POST /POST'))581- app.route('/v1', route)577+ const app = new Hono()578+ const api = new Hono()579+ const middleware = new Hono()580+ api.get('/posts', (c) => c.text('List'))581+ api.post('/posts', (c) => c.text('Create'))582+ api.get('/posts/:id', (c) => c.text(`GET ${c.req.param('id')}`))583+ api.use('*', async (c, next) => {584+ await next()585+ c.res.headers.append('x-custom-a', 'a')586+ })587+ app.route('/api', api)588 589+ middleware.use('*', async (c, next) => {590+ await next()591+ c.res.headers.append('x-custom-b', 'b')592+ })593+594+ app.route('/api', middleware)595+583- it('Should return 200 response - GET /v1/post', async () => {584- const res = await app.request('http://localhost/v1/post')596+ it('Should return not found response', async () => {597+ const res = await app.request('http://localhost/')598+ expect(res.status).toBe(404)599+ })600+601+ it('Should return not found response', async () => {602+ const res = await app.request('http://localhost/posts')603+ expect(res.status).toBe(404)604+ })605+606+ test('GET /api/posts', async () => {607+ const res = await app.request('http://localhost/api/posts')608 expect(res.status).toBe(200)586- expect(await res.text()).toBe('GET /POST')609+ expect(await res.text()).toBe('List')610 })611 589- it('Should return 200 response - POST /v1/post', async () => {590- const res = await app.request('http://localhost/v1/post', { method: 'POST' })612+ test('Custom header by middleware', async () => {613+ const res = await app.request('http://localhost/api/posts')614 expect(res.status).toBe(200)592- expect(await res.text()).toBe('POST /POST')615+ expect(res.headers.get('x-custom-a')).toBe('a')616+ expect(res.headers.get('x-custom-b')).toBe('b')617 })618 595- it('Should return 404 response - DELETE /v1/post', async () => {596- const res = await app.request('http://localhost/v1/post', { method: 'DELETE' })597- expect(res.status).toBe(404)619+ test('POST /api/posts', async () => {620+ const res = await app.request('http://localhost/api/posts', { method: 'POST' })621+ expect(res.status).toBe(200)622+ expect(await res.text()).toBe('Create')623 })624 600- it('Should return 404 response - GET /post', async () => {601- const res = await app.request('http://localhost/post')602- expect(res.status).toBe(404)625+ test('GET /api/posts/123', async () => {626+ const res = await app.request('http://localhost/api/posts/123')627+ expect(res.status).toBe(200)628+ expect(await res.text()).toBe('GET 123')629 })630 })631 632 describe('Chaining', () => {607- const route = new Route()633+ const app = new Hono()634+ const route = new Hono()635 route.get('/post', (c) => c.text('GET /POST v2')).post((c) => c.text('POST /POST v2'))636 app.route('/v2', route)637 638 it('Should return 200 response - GET /v2/post', async () => {13 unmodified lines652 expect(res.status).toBe(404)653 })654 })655 629- describe('Named parameter', () => {630- const route = new Route()631- route.get('/post/:id', (c) => {632- const id = c.req.param('id')633- return c.text(`GET /post/${id} v3`)656+ describe('Nested', () => {657+ const app = new Hono()658+ const api = new Hono()659+ const book = new Hono()660+661+ book.get('/', (c) => c.text('list books'))662+ book.get('/:id', (c) => c.text(`book ${c.req.param('id')}`))663+664+ api.get('/', (c) => c.text('this is API'))665+ api.route('/book', book)666+667+ app.get('/', (c) => c.text('root'))668+ app.route('/v2', api)669+670+ it('Should return 200 response - GET /', async () => {671+ const res = await app.request('http://localhost/')672+ expect(res.status).toBe(200)673+ expect(await res.text()).toBe('root')674 })635- app.route('/v3', route)675 637- it('Should return 200 response - GET /v3/post/1', async () => {638- const res = await app.request('http://localhost/v3/post/1')676+ it('Should return 200 response - GET /v2', async () => {677+ const res = await app.request('http://localhost/v2')678 expect(res.status).toBe(200)640- expect(await res.text()).toBe('GET /post/1 v3')679+ expect(await res.text()).toBe('this is API')680 })681+642- it('Should return 404 response - GET /v3/post', async () => {643- const res = await app.request('http://localhost/v3/post/abc/def')644- expect(res.status).toBe(404)682+ it('Should return 200 response - GET /v2/book', async () => {683+ const res = await app.request('http://localhost/v2/book')684+ expect(res.status).toBe(200)685+ expect(await res.text()).toBe('list books')686 })687+688+ it('Should return 200 response - GET /v2/book/123', async () => {689+ const res = await app.request('http://localhost/v2/book/123')690+ expect(res.status).toBe(200)691+ expect(await res.text()).toBe('book 123')692+ })693 })694 })695 696 describe('Multiple handler', () => {80 unmodified linesmodified:src/hono.ts+20 added-54 removed
68 unmodified lines69 } {70 return class {} as any71 }72 73-interface Routing<E extends Env> {73+interface Route<E extends Env> {74 path: string75- method: Methods75+ method: string76 handler: Handler<string, E>77 }78 79-export class Route<E = Env, P extends string = ''> extends defineDynamicClass()<E, P, Route<E, P>> {80- routes: Routing<E>[] = []81- #path: string = ''82-83- constructor() {84- super()85- const allMethods = [...methods, METHOD_NAME_ALL_LOWERCASE]86-87- allMethods.map((method) => {88- this[method] = <Path extends string>(89- args1: Path | Handler<ParamKeys<Path>, E>,90- ...args: [Handler<ParamKeys<Path>, E>]91- ) => {92- if (typeof args1 === 'string') {93- this.#path = args194- } else {95- this.add(method, this.#path, args1)96- }97- args.map((handler) => {98- if (typeof handler !== 'string') {99- this.add(method, this.#path, handler)100- }101- })102- return this103- }104- })105- }106-107- private add(method: Methods, path: string, handler: Handler<string, E>): Route<E, P> {108- const r: Routing<E> = { path: path, method: method, handler: handler }109- this.routes.push(r)110- return this111- }112-}113-79 export class Hono<E = Env, P extends string = ''> extends defineDynamicClass()<E, P, Hono<E, P>> {80 readonly routerClass: { new (): Router<any> } = TrieRouter81 readonly strict: boolean = true // strict routing - default is true117- #router: Router<Handler<string, E>>118- #tempPath: string82+ private _router: Router<Handler<string, E>>83+ private _tempPath: string84 private path: string85 86+ routes: Route<E>[] = []87+88 constructor(init: Partial<Pick<Hono, 'routerClass' | 'strict'>> = {}) {89 super()90 91 const allMethods = [...methods, METHOD_NAME_ALL_LOWERCASE]17 unmodified lines109 })110 111 Object.assign(this, init)112 146- this.#router = new this.routerClass()147- this.#tempPath = null113+ this._router = new this.routerClass()114+ this._tempPath = null115 }116 117 private notFoundHandler: NotFoundHandler = (c: Context) => {118 const message = '404 Not Found'5 unmodified lines124 const message = 'Internal Server Error'125 return c.text(message, 500)126 }127 161- route(path: string, route?: Route<any>): Hono<E, P> {162- const newHono: Hono<E, P> = new Hono()163- newHono.#tempPath = path164- newHono.#router = this.#router165-166- if (route) {167- route.routes.map((r) => {168- newHono.addRoute(r.method, r.path, r.handler)128+ route(path: string, app?: Hono<any>): Hono<E, P> {129+ this._tempPath = path130+ if (app) {131+ app.routes.map((r) => {132+ this.addRoute(r.method, r.path, r.handler)133 })134 }135 172- return newHono136+ return this137 }138 139 use(path: string, ...middleware: Handler<string, E>[]): Hono<E, P>140 use(...middleware: Handler<string, E>[]): Hono<E, P>20 unmodified lines161 }162 163 private addRoute(method: string, path: string, handler: Handler<string, E>): void {164 method = method.toUpperCase()201- if (this.#tempPath) {202- path = mergePath(this.#tempPath, path)165+ if (this._tempPath) {166+ path = mergePath(this._tempPath, path)167 }204- this.#router.add(method, path, handler)168+ this._router.add(method, path, handler)169+ const r: Route<E> = { path: path, method: method, handler: handler }170+ this.routes.push(r)171 }172 173 private async matchRoute(method: string, path: string): Promise<Result<Handler<string, E>>> {208- return this.#router.match(method, path)174+ return this._router.match(method, path)175 }176 177 private async dispatch(request: Request, event?: FetchEvent, env?: E): Promise<Response> {178 const path = getPathFromURL(request.url, { strict: this.strict })51 unmodified linesmodified:src/index.ts+1 added-1 removed
11234export { Hono, Route } from './hono'export { Hono } from './hono'export type { Handler, Next } from './hono'export { Context } from './context'export type { Env } from './context'