or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-10/

Admin User Route Handler

A simple Koa.js application that implements route handlers with regex-based path constraints to restrict access to specific user types.

Requirements

Implement a Koa.js application with route handlers that use path parameter constraints to create specialized routes for different user categories. Your implementation should handle the following scenarios:

  1. Admin Routes: Create a GET route at /user/:username that only matches when the username is exactly "admin". When matched, the handler should set ctx.body to { role: "administrator", username: "admin" }.

  2. Power User Routes: Create a GET route at /user/:username that only matches usernames starting with "power" (e.g., "power1", "power2", "poweruser"). When matched, the handler should set ctx.body to { role: "power_user", username: <matched_username> }.

  3. Numeric ID Routes: Create a GET route at /id/:userId that only matches numeric user IDs (one or more digits). When matched, the handler should set ctx.body to { userId: <matched_id>, type: "numeric" }.

  4. Fallback Route: Create a final GET route at /user/:username that matches any username not caught by the previous constraints. This handler should set ctx.body to { role: "regular_user", username: <matched_username> }.

Test Cases

  • Requesting GET /user/admin returns { role: "administrator", username: "admin" } @test
  • Requesting GET /user/power1 returns { role: "power_user", username: "power1" } @test
  • Requesting GET /user/poweruser returns { role: "power_user", username: "poweruser" } @test
  • Requesting GET /id/12345 returns { userId: "12345", type: "numeric" } @test
  • Requesting GET /user/alice returns { role: "regular_user", username: "alice" } @test

Implementation

@generates

API

/**
 * Creates and configures a Koa application with regex-constrained routes.
 * @returns {import('koa')} Configured Koa application instance
 */
function createApp() {
  // Implementation here
}

module.exports = { createApp };

Dependencies { .dependencies }

koa { .dependency }

Provides the web application framework.

koa-route { .dependency }

Provides routing middleware with path parameter constraints.