From 91ae52b680a679106275618efa34ff5b9c9c59ed Mon Sep 17 00:00:00 2001 From: Emelia Smith Date: Wed, 27 May 2026 14:22:40 +0200 Subject: [PATCH] fix: Router should be Macroable --- src/router/main.ts | 4 +++- tests/router/router.spec.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/src/router/main.ts b/src/router/main.ts index 44dc7d5..612b485 100644 --- a/src/router/main.ts +++ b/src/router/main.ts @@ -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' @@ -60,7 +61,7 @@ import { * }) * ``` */ -export class Router { +export class Router extends Macroable { /** * Flag to avoid re-comitting routes to the store */ @@ -152,6 +153,7 @@ export class Router { * @param qsParser - Query string parser for URL generation */ constructor(app: Application, encryption: Encryption, qsParser: Qs) { + super() this.#app = app this.#encryption = encryption this.qs = qsParser diff --git a/tests/router/router.spec.ts b/tests/router/router.spec.ts index 280a71a..3f577d8 100644 --- a/tests/router/router.spec.ts +++ b/tests/router/router.spec.ts @@ -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', () => { @@ -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) + }) +})