Nested Router with @sapphire/plugin-api

Hello guys, iam really new to this framework. i would like to integrate my express to my sapphire project. In my old project i was using nested routers. Here are some references from my old project: This was registering all my controllers like /servers or /users, i know the plugin-api does this automatically.
this.controllers.forEach(controller => {
let router = express.Router();
router.use(controller.path, controller.router);
this.app.use("/api", router);
console.log("Initialized Controller for /api" + controller.path)
})
this.controllers.forEach(controller => {
let router = express.Router();
router.use(controller.path, controller.router);
this.app.use("/api", router);
console.log("Initialized Controller for /api" + controller.path)
})
Then i used a paramRouter to pass other routes after :param(like serverId) for example (/api/servers/99999/config
export default class ServerController implements Controller {
public path = "/servers";
public router = Router();

private paramRouter = Router({ mergeParams: true });

constructor() {
this.router.use("/:id", this.paramRouter)
this.initializeRoutes();
}

private initializeRoutes() {
this.router.get("/", checkAuthorization, this.getAllServers);
this.router.post("/", [checkAuthorization, checkValidation(asRequestSchema({ body: serverSchema }))], this.addServer);

this.paramRouter.get("/config", checkAuthorization, this.getConfigOfServer);
this.paramRouter.patch("/config", [checkAuthorization, checkValidation(asRequestSchema({ body: serverConfigSchema }))], this.updateConfigOfServer)
}
export default class ServerController implements Controller {
public path = "/servers";
public router = Router();

private paramRouter = Router({ mergeParams: true });

constructor() {
this.router.use("/:id", this.paramRouter)
this.initializeRoutes();
}

private initializeRoutes() {
this.router.get("/", checkAuthorization, this.getAllServers);
this.router.post("/", [checkAuthorization, checkValidation(asRequestSchema({ body: serverSchema }))], this.addServer);

this.paramRouter.get("/config", checkAuthorization, this.getConfigOfServer);
this.paramRouter.patch("/config", [checkAuthorization, checkValidation(asRequestSchema({ body: serverConfigSchema }))], this.updateConfigOfServer)
}
so my question is can i do this with the plugin-api?
Solution:
routes/servers/server.ts with a route option in the constructor of servers/:serverId routes/servers/server/config.ts with a route option in the constructor of servers/:serverId/config example: https://github.com/skyra-project/skyra/blob/main/src/routes/guilds...
Jump to solution
2 Replies
Solution
Favna
Favna9mo ago
routes/servers/server.ts with a route option in the constructor of servers/:serverId routes/servers/server/config.ts with a route option in the constructor of servers/:serverId/config example: https://github.com/skyra-project/skyra/blob/main/src/routes/guilds
f1fty
f1ftyOP9mo ago
okay
Want results from more Discord servers?
Add your server