diff --git a/providers/izzy_provider.ts b/providers/izzy_provider.ts index ff2685a..412889a 100644 --- a/providers/izzy_provider.ts +++ b/providers/izzy_provider.ts @@ -5,9 +5,11 @@ * For the full license information, please view the LICENSE file that was distributed with this source code. */ -import type { ApplicationService } from '@adonisjs/core/types' +import type { ApplicationService, HttpRouterService } from '@adonisjs/core/types' import type { SerializedRoute } from '../src/types/manifest.js' import { serializeRoute } from '../src/serialize_route.js' +import type { RouteJSON } from '@adonisjs/core/types/http' +import type { Route, RouteResource, RouteGroup, BriskRoute } from '@adonisjs/core/http' declare global { namespace globalThis { @@ -39,6 +41,10 @@ export default class IzzyRouteProvider { }) } + if (routesJSON.length === 0) { + routesJSON = await this.#getTestRoutes(router) + } + const exists = routesJSON.find((route) => route.domain === 'root') if (exists) { @@ -47,6 +53,41 @@ export default class IzzyRouteProvider { } } + async #getTestRoutes(router: HttpRouterService) { + const routes = this.#routesToJSON(router.routes) + const domains = [...new Set(routes.map((route) => route.domain)).values()] + const routesJSON: { domain: string; routes: SerializedRoute[] }[] = [] + + for (let domain of domains) { + const domainRoutes = routes.filter((route) => route.domain === domain) + const serializedDomainRoutes = await Promise.all(domainRoutes.map(serializeRoute)) + + routesJSON.push({ + domain, + routes: serializedDomainRoutes, + }) + } + + return routesJSON + } + + #routesToJSON(routes: (Route | RouteResource | RouteGroup | BriskRoute)[]): RouteJSON[] { + return routes + .map((route) => { + if ('route' in route) { + return route.route?.toJSON() + } + + if ('routes' in route) { + return this.#routesToJSON(route.routes) + } + + return route.toJSON() + }) + .filter((route): route is RouteJSON => route !== null) + .flat(Number.POSITIVE_INFINITY) + } + /** * Registers edge plugin when edge is installed */