Skip to content

Refactor: Move route definitions to routes.json #84

@taylortom

Description

@taylortom

Context

Route definitions are moving from imperative code to declarative routes.json files. The loadRouteConfig function in adapt-authoring-server (PR #58) now handles reading routes.json, resolving handler strings to bound methods, validating against a schema, and merging with default routes.

AbstractApiModule (PR #77) and AbstractAuthModule (PR #68) both call loadRouteConfig in setValues() with a defaults option, so consumer modules that provide a routes.json automatically get the base default routes prepended.

Current state

ContentPluginModule calls useDefaultRouteConfig() then:

  • Removes POST / and PUT /:_id from defaults
  • Adds POST /installinstallHandler (permission: install:contentplugins, validate: false)
  • Adds POST /:_id/updateupdateHandler (permission: update:contentplugins)
  • Adds GET /:_id/usesusesHandler (permission: read:contentplugins)

Refactoring steps

  1. Create routes.json in the module root. Since default routes are modified (POST / and PUT /:_id removed), use useDefaultRoutes: false and declare the full set:
{
  "root": "contentplugins",
  "useDefaultRoutes": false,
  "routes": [
    {
      "route": "/",
      "handlers": { "get": "default" },
      "permissions": { "get": ["read:{scope}"] }
    },
    {
      "route": "/schema",
      "handlers": { "get": "serveSchema" },
      "permissions": { "get": ["read:schema"] }
    },
    {
      "route": "/:_id",
      "handlers": { "get": "default", "patch": "default", "delete": "default" },
      "permissions": { "get": ["read:{scope}"], "patch": ["write:{scope}"], "delete": ["write:{scope}"] }
    },
    {
      "route": "/query",
      "validate": false,
      "modifying": false,
      "handlers": { "post": "query" },
      "permissions": { "post": ["read:{scope}"] }
    },
    {
      "route": "/install",
      "validate": false,
      "handlers": { "post": "installHandler" },
      "permissions": { "post": ["install:contentplugins"] }
    },
    {
      "route": "/:_id/update",
      "handlers": { "post": "updateHandler" },
      "permissions": { "post": ["update:contentplugins"] }
    },
    {
      "route": "/:_id/uses",
      "handlers": { "get": "usesHandler" },
      "permissions": { "get": ["read:contentplugins"] }
    }
  ]
}
  1. Remove imperative route setup from init():

    • Remove the useDefaultRouteConfig() call
    • Remove route deletion and push logic
  2. Test by running npm test and verifying routes are registered correctly at startup

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions