Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/seven7ty/shorty
Short links 🤷♂️
https://github.com/seven7ty/shorty
api cloudflare cloudflare-workers http javascript web
Last synced: 29 days ago
JSON representation
Short links 🤷♂️
- Host: GitHub
- URL: https://github.com/seven7ty/shorty
- Owner: seven7ty
- License: mit
- Created: 2022-01-24T17:41:05.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-05-11T11:31:07.000Z (over 2 years ago)
- Last Synced: 2024-10-10T00:57:43.187Z (about 1 month ago)
- Topics: api, cloudflare, cloudflare-workers, http, javascript, web
- Language: JavaScript
- Homepage: https://wulf.works
- Size: 51.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# 👄 My simple [`worker`](https://developers.cloudflare.com/workers/) 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:
```json
{
"success": true,
"payload": {}
}
```- for successful `DELETE` and `PUT` requests:
```json
{
"success": true,
"message": "Some message."
}
```- for _any_ **unsuccessful** request:
```json
{
"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:```json
{
"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`](https://www.npmjs.com/package/nanoid).
Since the slug is random, it's very important to save it by reading the response:```json
{
"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.