Skip to content

Commit

Permalink
feat: add routes loader (#11592)
Browse files Browse the repository at this point in the history
Fixes: FRMW-2919

This PR adds a new routes loader with a single responsibility of scanning the filesystem and collecting routes. Sorting of routes, merging middleware and registering them with express are going to separate implementations.

The new `RoutesLoader` class allows overriding routes as-well (not recommended though) and this is how routes are de-duplicated.

- When two routes for the exact route pattern/matcher are discovered, the routes loader will only keep the last one.

- Routes files can also override handlers for specific HTTP methods. For example, the original route file exported handlers for `GET` and `POST`, but the overriding one only defines `GET`. In that case, we will continue using the original implementation for the `POST` handler.

- If an overriding route file exports additional configuration like `export const AUTHENTICATION=false`, then this will only impact the handlers exported from this file and not the original handlers.

Routes sorting has been already been implemented in a separate PR and you can visualize it using this URL. https://routes-visualizer.fly.dev/
  • Loading branch information
thetutlage authored Feb 26, 2025
1 parent b42f151 commit 9e2af48
Show file tree
Hide file tree
Showing 9 changed files with 708 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/strange-wasps-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/framework": patch
---

feat: add routes loader
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Request, Response } from "express"

export async function GET(req: Request, res: Response): Promise<void> {
console.log("hello world")
}

export const AUTHENTICATE = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Request, Response } from "express"

export async function GET(req: Request, res: Response): Promise<void> {
console.log("hello world")
}

export async function POST(req: Request, res: Response): Promise<void> {
console.log("hello world")
}

export const AUTHENTICATE = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Request, Response } from "express"

export function GET(req: Request, res: Response) {
/* const customerId = req.params.id;
const orderId = req.params.id;*/
res.send("list customers " + JSON.stringify(req.params))
}
Empty file.
Loading

0 comments on commit 9e2af48

Please sign in to comment.