Socket
Socket
Sign inDemoInstall

swrr

Package Overview
Dependencies
4
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    swrr

<code>swrr</code> <sub>Stale While Revalidate Resource</sub>


Version published
Weekly downloads
7
increased by133.33%
Maintainers
1
Install size
153 kB
Created
Weekly downloads
 

Readme

Source

swrr

Stale While Revalidate Resource

npm add swrr makes resources fast

downloads size size

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

// file: my-worker.esm.ts

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) {
    // ⬇️️ create "container", all resources will batch in this boundary.
    //   ~> you'd probably want this in a middleware.
    const makeResource = swr.make(env.KV_NAMESPACE, ctx);

    // ⬇️ create a resource connected to a handler and name it
    const getLatestPosts = makeResource('posts', getPostsInCategorySince);

    // ... whatever elese

    // ⬇️ run the resource, passing in whatever arguments you'd like
    const posts = await getLatestPosts({ category: 'foobar', since: '2022-01-01' });

    // ~> and one would now find a KV entry for
    //      'posts__cdbdf4617568dc29453d3fee5f5ca79a7713b15f'

    return new Response(posts, { headers: { 'content-type': 'application/json' } });
  },
};

License

MIT © Marais Rossouw

FAQs

Last updated on 01 Mar 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc