Coder Social home page Coder Social logo

shorty's Introduction

๐Ÿ‘„ My simple worker for short links

I wanted a quick-and-easy way to create links on my domain, so I turned to Cloudflare Workers.
All routes available, apart from the base redirect, return a JSON response paired with the Content-Type: application/json header.
Every aforementioned JSON response follows this rough schema:

  • for successful POST requests:
{
  "success": true,
  "payload": {}
}
  • for successful DELETE and PUT requests:
{
  "success": true,
  "message": "Some message."
}
  • for any unsuccessful request:
{
  "success": false,
  "message": "Error message."
}

Aside from that, the API also requires authentication through the X-Auth-Key header.

๐Ÿ›ฃ๏ธ The routes

Requests to URLs other than / are handled by the worker and fall under the following spec.

GET /:slug

Runs a check against a bound KV namespace called LINKS, and if a slug like the one requested exists, redirects to the associated URL.
If the slug can't be found, a JSON response is returned.

POST /:slug?url={url}

Creates a case-sensitive entry in the KV namespace called slug with the value url.
Requests to the above endpoint are then redirected to corresponding URLs.
Upon successful creation (not a duplicate, etc.), a JSON response is returned:

{
  "success": true,
  "payload": {
    "slug": "The passed slug",
    "url": "The passed url"
  }
}

POST /new?url={url}

Similar to the above, except, this route randomizes the slug using nanoid.
Since the slug is random, it's very important to save it by reading the response:

{
  "success": true,
  "payload": {
    "slug": "The nanoid-generated slug",
    "url": "The passed url"
  }
}

DELETE /:slug

Deletes a link associated with the slug.

PUT /:slug?url={url}

Update an existing slug with a new url. Doesn't return the new link like POST requests.

shorty's People

Contributors

seven7ty avatar deepsource-autofix[bot] avatar deepsourcebot avatar

Stargazers

Bhuvan Sankar Andela avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.