Caveat ~ Currently cache backplane is assumed to be
Cloudflare Workers KV. Before we hit 1.0 release, we will
aim to support a generic and abstract backplane for the likes of Redis, Memcached, or other layers.
🚀 Usage
import * as swr from 'swrr';
const getPostsInCategorySince = async ({ category, since }) => {
const posts = await cms.getPostsInCategory(category);
return posts.filter((post) => post.updatedAt > since);
};
export default {
async fetch(req, env, ctx) {
const makeResource = swr.make(env.KV_NAMESPACE, ctx);
const getLatestPosts = makeResource('posts', getPostsInCategorySince);
const posts = await getLatestPosts({ category: 'foobar', since: '2022-01-01' });
return new Response(posts, { headers: { 'content-type': 'application/json' } });
},
};
License
MIT © Marais Rossouw