or run

npx @tessl/cli init
Log in

Version

Files

tile.json

task.mdevals/scenario-6/

URL Shortener API

Build a simple URL shortener API using Koa that manages short links and redirects users to the original URLs.

Requirements

Your API should support the following endpoints:

Create Short Link

POST /shorten - Creates a new short link

  • Request body contains url (the original URL to shorten) and optional customSlug (custom short code)
  • If customSlug is provided and available, use it as the short code
  • If customSlug is not provided, generate a random 6-character alphanumeric code
  • Return JSON with the short code: { "shortCode": "abc123" }
  • Return 400 error if customSlug is already taken

Redirect to Original URL

GET /:shortCode - Redirects to the original URL

  • Extract the short code from the URL path
  • Look up the original URL associated with that short code
  • Redirect the user to the original URL (HTTP 302)
  • Return 404 error if short code doesn't exist

Get Link Statistics

GET /stats/:shortCode - Returns statistics for a short link

  • Extract the short code from the URL path
  • Return JSON with shortCode, originalUrl, and clicks (number of times redirected)
  • Return 404 error if short code doesn't exist

Delete Short Link

DELETE /links/:shortCode - Deletes a short link

  • Extract the short code from the URL path
  • Remove the short link from storage
  • Return 204 No Content on success
  • Return 404 error if short code doesn't exist

Implementation

@generates

Test Cases

  • Creating a short link without a custom slug generates a 6-character code @test
  • Creating a short link with a custom slug uses that slug @test
  • Redirecting with a valid short code performs a 302 redirect @test
  • Getting statistics for a valid short code returns correct data @test
  • Deleting a short link removes it from storage @test

API

// Koa application with routes that handle:
// POST /shorten - create short link
// GET /:shortCode - redirect to original URL
// GET /stats/:shortCode - get link statistics
// DELETE /links/:shortCode - delete short link

Dependencies { .dependencies }

koa { .dependency }

Provides the web framework.

koa-route { .dependency }

Provides routing with parameter extraction from URL paths.

koa-bodyparser { .dependency }

Parses JSON request bodies.