Skip to content

Commit

Permalink
Merge pull request #1 from lahgolz/main
Browse files Browse the repository at this point in the history
fix: retrieve routes in test environment
  • Loading branch information
lncitador authored Sep 19, 2024
2 parents 405eeee + cd20670 commit ca5a5dc
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion providers/izzy_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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) {
Expand All @@ -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
*/
Expand Down

0 comments on commit ca5a5dc

Please sign in to comment.