Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/router/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { Encryption } from '@boringnode/encryption'
import type { Application } from '@adonisjs/application'
import { RuntimeException } from '@poppinss/utils/exception'
import type { Constructor, LazyImport } from '@poppinss/utils/types'
import Macroable from '@poppinss/macroable'

import debug from '../debug.ts'
import type { Qs } from '../qs.ts'
Expand Down Expand Up @@ -60,7 +61,7 @@ import {
* })
* ```
*/
export class Router {
export class Router extends Macroable {
/**
* Flag to avoid re-comitting routes to the store
*/
Expand Down Expand Up @@ -152,6 +153,7 @@ export class Router {
* @param qsParser - Query string parser for URL generation
*/
constructor(app: Application<any>, encryption: Encryption, qsParser: Qs) {
super()
this.#app = app
this.#encryption = encryption
this.qs = qsParser
Expand Down
29 changes: 29 additions & 0 deletions tests/router/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { parse } from '@poppinss/qs'
import { test } from '@japa/runner'
import { EncryptionFactory } from '@boringnode/encryption/factories'

import { Router } from '../../src/router/main.ts'
import { RouterFactory } from '../../factories/router.ts'

test.group('Router | add', () => {
Expand Down Expand Up @@ -1785,3 +1786,31 @@ test.group('Router | generateTypes', () => {
`)
})
})

test.group('Router | macroable', () => {
test('add macro to router', ({ assert }) => {
Router.macro('getRouteCount' as any, function (this: Router) {
return Object.values(this.toJSON()).flat().length
})

const router = new RouterFactory().create()
router.get('/', '#controllers/home.index')
router.commit()

// @ts-expect-error - macro is not typed
assert.equal(router.getRouteCount(), 1)
})

test('add getter to router', ({ assert }) => {
Router.getter('routeCount' as any, function (this: Router) {
return Object.values(this.toJSON()).flat().length
})

const router = new RouterFactory().create()
router.get('/', '#controllers/home.index')
router.commit()

// @ts-expect-error - getter is not typed
assert.equal(router.routeCount, 1)
})
})
Loading