Awesome
cf-worker-kit
A library to make writing Cloudflare Workers way nicer. It implements routing and a nice Workers KV API for you. With this library you have to write considerably less boilerplate than a standard Cloudflare Worker.
installation
Install by running npm i cf-worker-kit
or yarn add cf-worker-kit
then import:
const workerkit = require('cf-worker-kit');
// or
const { Worker, Router, API } = require('cf-worker-kit');
// or
import workerkit from 'cf-worker-kit';
// or
import { Worker, Router, API } from 'cf-worker-kit';
example
// setup for http handling
const worker = new Worker();
const router = new Router();
// setup for workers kv
const api = new API('cf_account_id', 'cf_api_email', 'cf_api_key');
const BOOKS = api.kv.get('kv_namespace_id');
// hello world function
router.any('.*/hello', req => {
return new Response(`Hello from method ${req.method}!`);
});
// get the IDs of all books in the KV namespace
router.get('.*/books', async () => {
const books = await BOOKS.keys();
return new Response(books.map(book => book.name).join(', '));
});
worker.use(router);
worker.listen();
documentation
notes on Workers KV
If you are using KV in your worker only for get
, set
and delete
operations, you should use the native Worker KV API, because it is considerably faster because it is implemented as a native call in the Cloudflare Worker environment. As more functions become available in the native API, workerkit might start using these.
using the native KV implemenation with typescript
If you want to use the Native KV implementation in Cloudflare Workers and you are using TypeScript, then you can use code like the one below to make TS recognize the KV namespace as such.
// in this case the name of the native KV instance is 'BOOKS'.
import { NativeKV } from 'cf-worker-kit';
declare global {
const BOOKS: NativeKV;
}
soon(tm)
- bulk Workers KV setting and deleting
- query params in req
- make KV give better errors
licence
MIT License Copyright (c) 2019 Luca Casonato Full licence in the LICENCE file.