Skip to content
Myr

feat(app): `app.routerName()` (#1105)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* feat(context): `c.routerName()` * chore: denoify * Enable to get router name via `app.routerName`. (#1110) ---------

authored by Yusuke Wada · committed by GitHub · parent 2cdfcd8 · Browse files · 17 files+44 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

17 files+44 added−2 removed

  • tests:+15 added−0 removed
  • other:+29 added−2 removed

17 files+44 added−2 removed

modified:deno_dist/router/linear-router/router.ts+2 added-1 removed
modified:deno_dist/router/pattern-router/router.ts+1 added
2 unmodified lines
34type Route<T> = [RegExp, string, T] // [pattern, method, handler]56export class PatternRouter<T> implements Router<T> {7 name: string = 'PatternRouter'8 private routes: Route<T>[] = []9 private dNames: Record<string, number> = {} // Short name of duplicatedNames1011 add(method: string, path: string, handler: T) {
59 unmodified lines
modified:deno_dist/router/reg-exp-router/router.ts+1 added
99 unmodified lines
100 return undefined101}102103export class RegExpRouter<T> implements Router<T> {104 name: string = 'RegExpRouter'105 middleware?: Record<string, Record<string, T[]>>106 routes?: Record<string, Record<string, T[]>>107108 constructor() {
151 unmodified lines
modified:deno_dist/router/smart-router/router.ts+4 added
1 unmodified line
2import type { Router, Result } from '../../router.ts'3import { UnsupportedPathError } from '../../router.ts'45export class SmartRouter<T> implements Router<T> {6 name: string = 'SmartRouter'7 routers: Router<T>[] = []8 routes?: [string, string, T][] = []910 constructor(init: Pick<SmartRouter<T>, 'routers'>) {
41 unmodified lines
52 // not found53 throw new Error('Fatal error')54 }5556 // e.g. "SmartRouter + RegExpRouter"57 this.name = `SmartRouter + ${this.activeRouter.name}`5859 return res || null60 }6162 get activeRouter() {
7 unmodified lines
modified:deno_dist/router/trie-router/router.ts+1 added
modified:deno_dist/context.ts+3 added
63 unmodified lines
64 executionCtx?: FetchEvent | ExecutionContext | undefined65 notFoundHandler?: NotFoundHandler<E>66 path?: string67 paramData?: Record<string, string>68 routerName?: string69}7071export class Context<72 // eslint-disable-next-line @typescript-eslint/no-explicit-any
4 unmodified lines
77> {78 env: E['Bindings'] = {}79 finalized: boolean = false80 error: Error | undefined = undefined81 routerName?: string | undefined8283 // eslint-disable-next-line @typescript-eslint/no-explicit-any84 private _req?: HonoRequest<any, any>85 private _status: StatusCode = 200
14 unmodified lines
100 if (options) {101 this._exCtx = options.executionCtx102 this._path = options.path ?? '/'103 this._pData = options.paramData104 this.routerName = options.routerName105 this.env = options.env106 if (options.notFoundHandler) {107 this.notFoundHandler = options.notFoundHandler108 }
294 unmodified lines
modified:deno_dist/hono-base.ts+1 added
230 unmodified lines
231 executionCtx: eventOrExecutionCtx,232 notFoundHandler: this.notFoundHandler,233 path,234 paramData,235 routerName: this.router.name,236 })237238 // Do not `compose` if it has only one handler239 if (result?.handlers.length === 1) {
87 unmodified lines
modified:deno_dist/router.ts+1 added
modified:src/router/linear-router/router.ts+2 added-1 removed
1import type { Router, Result } from '../../router'2import { METHOD_NAME_ALL, UnsupportedPathError } from '../../router'34type RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] };4type RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] }56const splitPathRe = /\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g7const splitByStarRe = /\*/8export class LinearRouter<T> implements Router<T> {9 name: string = 'LinearRouter'10 routes: [string, string, T][] = []1112 add(method: string, path: string, handler: T) {13 if (path.charCodeAt(path.length - 1) === 63) {
131 unmodified lines
modified:src/router/pattern-router/router.ts+1 added
2 unmodified lines
34type Route<T> = [RegExp, string, T] // [pattern, method, handler]56export class PatternRouter<T> implements Router<T> {7 name: string = 'PatternRouter'8 private routes: Route<T>[] = []9 private dNames: Record<string, number> = {} // Short name of duplicatedNames1011 add(method: string, path: string, handler: T) {
59 unmodified lines
modified:src/router/reg-exp-router/router.ts+1 added
99 unmodified lines
100 return undefined101}102103export class RegExpRouter<T> implements Router<T> {104 name: string = 'RegExpRouter'105 middleware?: Record<string, Record<string, T[]>>106 routes?: Record<string, Record<string, T[]>>107108 constructor() {
151 unmodified lines
modified:src/router/smart-router/router.test.ts+2 added
16 unmodified lines
17 expect(res).not.toBeNull()18 expect(res?.handlers).toEqual(['get entry'])19 expect(res?.params['id']).toBe('123')20 expect(router.activeRouter).toBeInstanceOf(RegExpRouter)21 expect(router.name).toBe('SmartRouter + RegExpRouter')22 })2324 it('Wildcard', async () => {25 router.add('GET', '/wild/*/card', 'get wildcard')
56 unmodified lines
82 expect(res?.handlers).toEqual(['get user entries', 'get entry'])83 expect(res?.params['user']).toBe('entry')84 expect(res?.params['name']).toBe('entries')85 expect(router.activeRouter).toBeInstanceOf(TrieRouter)86 expect(router.name).toBe('SmartRouter + TrieRouter')87 })88})
modified:src/router/smart-router/router.ts+4 added
1 unmodified line
2import type { Router, Result } from '../../router'3import { UnsupportedPathError } from '../../router'45export class SmartRouter<T> implements Router<T> {6 name: string = 'SmartRouter'7 routers: Router<T>[] = []8 routes?: [string, string, T][] = []910 constructor(init: Pick<SmartRouter<T>, 'routers'>) {
41 unmodified lines
52 // not found53 throw new Error('Fatal error')54 }5556 // e.g. "SmartRouter + RegExpRouter"57 this.name = `SmartRouter + ${this.activeRouter.name}`5859 return res || null60 }6162 get activeRouter() {
7 unmodified lines
modified:src/router/trie-router/router.ts+1 added
1 unmodified line
2import { checkOptionalParameter } from '../../utils/url'3import { Node } from './node'45export class TrieRouter<T> implements Router<T> {6 name: string = 'TrieRouter'7 node: Node<T>89 constructor() {10 this.node = new Node()
18 unmodified lines
modified:src/hono-base.ts+5 added
192 unmodified lines
193 )194 })195 }196197 get routerName() {198 this.matchRoute('GET', '/')199 return this.router.name200 }201202 private addRoute(method: string, path: string, handler: H) {203 method = method.toUpperCase()204 if (this._basePath) {205 path = mergePath(this._basePath, path)
125 unmodified lines
modified:src/hono.test.ts+13 added
1908 unmodified lines
1909 type: undefined,1910 })1911 })1912})19131914describe('Router Name', () => {1915 it('Should return the correct router name', async () => {1916 const app = new Hono({1917 router: new RegExpRouter(),1918 })1919 app.get('/router-name', (c) => {1920 return c.text(app.routerName ?? 'N/A')1921 })1922 const res = await app.request('/router-name')1923 expect(await res.text()).toBe('RegExpRouter')1924 })1925})
modified:src/router.ts+1 added
1 unmodified line
2export const METHOD_NAME_ALL_LOWERCASE = 'all' as const3export const METHODS = ['get', 'post', 'put', 'delete', 'head', 'options', 'patch'] as const45export interface Router<T> {6 name: string7 add(method: string, path: string, handler: T): void8 match(method: string, path: string): Result<T> | null9}10
6 unmodified lines