From dfd2712663937d25ee7bcd32e60b5d676a964f66 Mon Sep 17 00:00:00 2001 From: Erisa A Date: Tue, 6 Jul 2021 11:15:39 +0100 Subject: [PATCH] Format code, add package scripts --- README.md | 9 ++++++++- index.js | 8 ++++++-- package.json | 5 ++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6d70c6f..23cb4e4 100644 --- a/README.md +++ b/README.md @@ -12,11 +12,18 @@ To deploy to your Cloudflare Workers account, edit the relevant entries in `wran For debugging, you either can use `wrangler preview`, though note you will need to login and configure a preview KV namespace in `wrangler.toml` - or use Miniflare (See below). ## Running locally + If you want to run this locally (Not on Cloudflare), you can use [miniflare](https://github.com/mrbbot/miniflare): -- `npm install` or `npm install -g miniflare` + +- `npm install -g miniflare` - `miniflare index.js -k kv -b WORKERLINKS_SECRET=putyoursecrethere` +or + +- `WORKERLINKS_SECRET=putyoursecrethere npm run local` + ## Usage + Once deployed, interacting with the API should be rather simple. It's based on headers, specifically with the `Authorization` and `URL` headers. To create a short URL with a random URL, send a `POST` to `/` with `Authorization` and `URL` headers: diff --git a/index.js b/index.js index 9e0ef2e..095217d 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ // Set this in your worker's environment. wrangler.toml or cloudflare dashboard. let secret = WORKERLINKS_SECRET -addEventListener('fetch', (event) => { +addEventListener('fetch', event => { event.respondWith(handleRequest(event.request)) }) @@ -35,7 +35,11 @@ async function handleRequest(request) { }, ) } - key = '/' + Math.random().toString(36).slice(5) + key = + '/' + + Math.random() + .toString(36) + .slice(5) shorturl = new URL(request.url).origin + key return await putLink( request.headers.get('Authorization'), diff --git a/package.json b/package.json index 8b6ab50..bfecfac 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,10 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "format": "prettier --write '**/*.{js,css,json,md}'" + "format": "prettier --write '**/*.{js,css,json,md}'", + "deploy": "wrangler publish", + "debug": "miniflare --debug index.js -k kv -b WORKERLINKS_SECRET=putyoursecrethere", + "local": "miniflare index.js -k kv -b WORKERLINKS_SECRET=$WORKERLINKS_SECRET" }, "author": "Erisa A", "license": "MIT",