0.1.0 • Published 4 years ago
kv4cf v0.1.0
cloudflare-kv-handler
A Modified version of @cloudflare/kv-asset-handler
, with Non-ASCII URL supported.
Installation
npm uninstall @cloudflare/kv-asset-handler
npm install kv4cf
Usage
Nearly exactly the same as @cloudflare/kv-asset-handler
, so please refer to its README.
Only a few differences need to be noticed:
Use CommonJS instead of ESModule
// What you should do to import @cloudflare/kv-asset-handler
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
// What you should do to import kv4cf
const { getAssetFromKV } = require('kv4cf');
Custom Error handling
const { getAssetFromKV } = require('kv4cf');
const { NotFoundError, MethodNotAllowedError, InternalError } = require('kv4cf/lib/error');
addEventListener('fetch', event => {
event.respondWith(handleEvent(event))
})
async function handleEvent(event) {
try {
return await getAssetFromKV(event)
} catch (e) {
if (e instanceof NotFoundError) {
// ...
} else if (e instanceof MethodNotAllowedError) {
// ...
} else if (e instanceof InternalError) {
// ...
} else {
// ...
}
}
}