Skip to content

Refactor: Move route definitions to routes.json #37

@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

UsersModule calls useDefaultRouteConfig(), then:

  • Removes POST / from the default routes
  • Prepends a /me route with GET/PUT/PATCH → requestHandler() (permissions: read:me, write:me)

Refactoring steps

  1. Create routes.json in the module root. Since this module removes POST from / and adds /me, the full routes need to be declared with useDefaultRoutes: false:
{
  "root": "users",
  "useDefaultRoutes": false,
  "routes": [
    {
      "route": "/me",
      "handlers": { "get": "default", "put": "default", "patch": "default" },
      "permissions": { "get": ["read:me"], "put": ["write:me"], "patch": ["write:me"] }
    },
    {
      "route": "/",
      "handlers": { "get": "default" },
      "permissions": { "get": ["read:{scope}"] }
    },
    {
      "route": "/schema",
      "handlers": { "get": "serveSchema" },
      "permissions": { "get": ["read:schema"] }
    },
    {
      "route": "/:_id",
      "handlers": { "put": "default", "get": "default", "patch": "default", "delete": "default" },
      "permissions": { "put": ["write:{scope}"], "get": ["read:{scope}"], "patch": ["write:{scope}"], "delete": ["write:{scope}"] }
    },
    {
      "route": "/query",
      "validate": false,
      "modifying": false,
      "handlers": { "post": "query" },
      "permissions": { "post": ["read:{scope}"] }
    }
  ]
}
  1. Remove imperative route setup from init():

    • Remove the useDefaultRouteConfig() call
    • Remove the route filter/splice logic
    • Remove the /me route push
  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