or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-5/

Resource API with Optional Filtering

Build a simple Koa application that serves resource data with optional filtering capabilities through URL parameters.

Requirements

Create a Koa web server with the following endpoints:

List Resources Endpoint

Create an endpoint that handles requests to /api/resources and /api/resources/:category where:

  • When no category is provided, return all resources
  • When a category is provided, return only resources matching that category
  • The category parameter should be optional
  • Return results as JSON with status 200

Get Resource by ID Endpoint

Create an endpoint that handles requests to /api/resources/:id/details and /api/resources/:id/details/:format where:

  • :id is required and represents the resource ID
  • :format is optional and can be either "json" or "xml"
  • When no format is provided, default to JSON format
  • When format is "json", return {"id": <id>, "format": "json"}
  • When format is "xml", return {"id": <id>, "format": "xml"}
  • Return status 200

Sample Data

Use this sample data for resources:

const resources = [
  { id: 1, name: "Resource A", category: "books" },
  { id: 2, name: "Resource B", category: "movies" },
  { id: 3, name: "Resource C", category: "books" }
];

Implementation

@generates

Test Cases

  • Requesting /api/resources returns all 3 resources with status 200 @test
  • Requesting /api/resources/books returns only resources with category "books" @test
  • Requesting /api/resources/movies returns only resources with category "movies" @test
  • Requesting /api/resources/:id/details with id=1 returns JSON format by default @test
  • Requesting /api/resources/:id/details/xml with id=2 returns XML format @test

API

// Main Koa application
const Koa = require('koa');
const app = new Koa();

// Configure routes and start server

Dependencies { .dependencies }

koa { .dependency }

Web framework for Node.js providing context-based request handling.

koa-route { .dependency }

Provides route middleware with path parameter support.