Format code, add package scripts
This commit is contained in:
parent
66110f3db9
commit
dfd2712663
3 changed files with 18 additions and 4 deletions
|
|
@ -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).
|
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
|
## Running locally
|
||||||
|
|
||||||
If you want to run this locally (Not on Cloudflare), you can use [miniflare](https://github.com/mrbbot/miniflare):
|
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`
|
- `miniflare index.js -k kv -b WORKERLINKS_SECRET=putyoursecrethere`
|
||||||
|
|
||||||
|
or
|
||||||
|
|
||||||
|
- `WORKERLINKS_SECRET=putyoursecrethere npm run local`
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Once deployed, interacting with the API should be rather simple. It's based on headers, specifically with the `Authorization` and `URL` headers.
|
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:
|
To create a short URL with a random URL, send a `POST` to `/` with `Authorization` and `URL` headers:
|
||||||
|
|
|
||||||
8
index.js
8
index.js
|
|
@ -1,7 +1,7 @@
|
||||||
// Set this in your worker's environment. wrangler.toml or cloudflare dashboard.
|
// Set this in your worker's environment. wrangler.toml or cloudflare dashboard.
|
||||||
let secret = WORKERLINKS_SECRET
|
let secret = WORKERLINKS_SECRET
|
||||||
|
|
||||||
addEventListener('fetch', (event) => {
|
addEventListener('fetch', event => {
|
||||||
event.respondWith(handleRequest(event.request))
|
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
|
shorturl = new URL(request.url).origin + key
|
||||||
return await putLink(
|
return await putLink(
|
||||||
request.headers.get('Authorization'),
|
request.headers.get('Authorization'),
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,10 @@
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"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",
|
"author": "Erisa A",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue