{"_id":"@autotelic/fastify-swr","_rev":"1-965d07e430466e730e3a65e04f803dd5","name":"@autotelic/fastify-swr","dist-tags":{"latest":"0.2.0"},"versions":{"0.1.0":{"name":"@autotelic/fastify-swr","version":"0.1.0","description":"stale-while-revalidate caching handler for fastify","main":"index.js","scripts":{"test":"tap --100 -j1 -R classic test/*.test.js","lint":"eslint . --ext .js","fix":"npm run lint -- --fix","validate":"npm run lint && npm run test","dev":"fastify start example/index.js -w -P -l debug"},"keywords":["fastify","plugin","caching","stale-while-revalidate","swr"],"author":{"name":"Holden Whitehead","email":"holden@autotelic.com"},"license":"MIT","devDependencies":{"eslint":"^8.32.0","eslint-config-standard":"^17.0.0","eslint-plugin-import":"^2.27.5","eslint-plugin-n":"^15.6.1","eslint-plugin-promise":"^6.1.1","fastify":"^4.11.0","fastify-cli":"^5.7.1","sinon":"^15.0.1","tap":"^16.3.4","undici":"^5.20.0"},"dependencies":{"fastify-plugin":"^4.5.0"},"gitHead":"c21ab9fa6e42d31efef3e0b57a9740e907b24a83","_id":"@autotelic/fastify-swr@0.1.0","_nodeVersion":"18.14.0","_npmVersion":"9.3.1","dist":{"integrity":"sha512-Yd7UoM8kzjq+E3XdQyyGAo92jvCfCE/u+AXNiF9PEc87nAPBJB1bcgDFE18Qu4fKxOdrjHuG9zSObNO1XbyExA==","shasum":"56a505edd1d3a6ed34c146efdb961f848c6a2918","tarball":"https://registry.npmjs.org/@autotelic/fastify-swr/-/fastify-swr-0.1.0.tgz","fileCount":8,"unpackedSize":10432,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHyDbpRTbQqu0yE6VD4snP7C0X+e/zVkfoAYiCCQ0tIvAiEAzf72jn2jRzAh75ikceE9XJoKmfJ9tiW+8jSqAUE4Xv0="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJj9o/aACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmoy3A/+PSfeX2R9RbTM7vhv+pHkvFWEPYsV7H71vI7aiyVZFO48QIbh\r\nYi+XdHc72Ij2xPykBWgdNuwiBz2IaluTqRxpKM3qf83KlA3U/dPIpbjSMktB\r\noE2/y+Ip8+0cKZ6QOVusLTAFfM2uDVxY6Dek931Tbhtf8nJNqGgpcfrMov6o\r\nwRBK4pMO9ihr7y1dskB5FEiBfWRDt8iaewzt76vC37PQc4zORLv6BTAF/JX6\r\nqEuNVhACCdzxNb6T0ZGvu1GiH0bvv6s/eNNKfQ+i6/xyz3T42aFTzTLcz/Ba\r\n3/kJpnILlA39PwDEb0mv3aCSejHXA+KuivpgatPAH0KTtaZb6GQQD/5OA4O7\r\n/N8x7gea980MvNMtCcBgbuxAk9INOMOKAcAq2KOXi5WuvsfqNg+1sFiY99qD\r\nGxKrkoDUWkJdkQ6vJcbocsAzsI2bdaiNEffLOCYLV4mhFb2Ge7HsVNGa9Aj9\r\n2L8mXJycQpYM1G3eCAA/L1XGCnwxc18Wj9kPjTiqW2G1knfaVP9kHsREFBR7\r\nmTmum34xSFxfeJByvE1ZdeQfLyBPD9CddZ1gJrI6bdHtBLn2gKBsbfsHcvWv\r\niPH7N1qR9wwjo82780YqdKIfbOVjLk0zSplHI5EVLtczim4zOtHsxoc4PwYS\r\nF1BXgLARxq4Ek/9oL2ntjWwDnmPSkOU65xc=\r\n=IoQr\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"autotelic","email":"info+npm@autotelic.com"},"directories":{},"maintainers":[{"name":"autotelic","email":"info+npm@autotelic.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fastify-swr_0.1.0_1677103066722_0.7711522094803975"},"_hasShrinkwrap":false},"0.2.0":{"name":"@autotelic/fastify-swr","version":"0.2.0","description":"stale-while-revalidate caching handler for fastify","main":"index.js","scripts":{"test":"tap --100 -j1 -R classic test/*.test.js","lint":"eslint . --ext .js","fix":"npm run lint -- --fix","validate":"npm run lint && npm run test","dev":"fastify start example/index.js -w -P -l debug"},"keywords":["fastify","plugin","caching","stale-while-revalidate","swr"],"author":{"name":"Holden Whitehead","email":"holden@autotelic.com"},"license":"MIT","devDependencies":{"eslint":"^8.32.0","eslint-config-standard":"^17.0.0","eslint-plugin-import":"^2.27.5","eslint-plugin-n":"^15.6.1","eslint-plugin-promise":"^6.1.1","fastify":"^4.11.0","fastify-cli":"^5.7.1","sinon":"^15.0.1","tap":"^16.3.4","undici":"^5.20.0"},"dependencies":{"fastify-plugin":"^4.5.0"},"gitHead":"e6cc2768904927472814e432c7d8007a2d786f4d","_id":"@autotelic/fastify-swr@0.2.0","_nodeVersion":"18.16.0","_npmVersion":"9.5.1","dist":{"integrity":"sha512-6UfJF1xnz96E789Zu9ng/zIvVVQ/T0meUm1dfw9kDK4hWP0VolWbpaFXO9qohbNihJ12kv9SRC2OqLtR9ZvtHQ==","shasum":"483265390d7e697fa1ebc2db226f6f9783a2ec21","tarball":"https://registry.npmjs.org/@autotelic/fastify-swr/-/fastify-swr-0.2.0.tgz","fileCount":8,"unpackedSize":12891,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHptK6FzDtQRp7S42+Q/BpjmrjS/2SPXbHo6tQ2lPaMuAiAXEii4WOwIn5CwYSFB7PQWcrZQp3xCuwsuT62BNLtAoA=="}]},"_npmUser":{"name":"autotelic","email":"info+npm@autotelic.com"},"directories":{},"maintainers":[{"name":"autotelic","email":"info+npm@autotelic.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fastify-swr_0.2.0_1684273798648_0.4024206746300838"},"_hasShrinkwrap":false}},"time":{"created":"2023-02-22T21:57:46.523Z","0.1.0":"2023-02-22T21:57:46.911Z","modified":"2023-05-16T21:49:58.951Z","0.2.0":"2023-05-16T21:49:58.818Z"},"maintainers":[{"name":"autotelic","email":"info+npm@autotelic.com"}],"description":"stale-while-revalidate caching handler for fastify","keywords":["fastify","plugin","caching","stale-while-revalidate","swr"],"author":{"name":"Holden Whitehead","email":"holden@autotelic.com"},"license":"MIT","readme":"# Fastify SWR\n\nstale-while-revalidate caching and request de-duplication for **fastify**\n\n## Getting Started\n\n### Install\n\n```sh\nnpm i @autotelic/fastify-swr\n```\n\n### Usage\n\n```js\nconst fastifySwr = require('@autotelic/fastify-swr')\n\nasync function myClient (path, opts = {}) {\n  const res = await fetch(new URL(`https://example.com/${path}`), opts)\n  return res.json()\n}\n\nasync function rootPlugin (fastify, opts) {\n  await fastify.register(fastifySwr, { handler: myClient, interval: 10 })\n\n  fastify.get('/', async function routeHandler (request, reply) {\n    // By default, the handler's name will be used to name the decorator (see API for mor details)\n    const content = await fastify.myClient('/endpoint', {\n      headers: {\n        accept: 'application/json'\n      }\n    })\n    return content\n  })\n}\n```\n\n#### Example App\n\nTo run the example app clone this repo locally, `npm i`, and then run the following\n\n```sh\nnpm run dev\n```\n\nOnce running, navigate to `localhost:3000` in the browser\n\n## API\n\n### `fastifySwr`: `FastifyPluginAsync<PluginOpts>`\n\n#### `PluginOpts`\n\n`fastifySwr` accepts a `PluginOpts` object with the following properties:\n\n- **`handler`: `(key: string, ...args: any[]) => any`** - *Required*. The function to be wrapped by the plugin. The return value of the `handler` will be cached using a stale-while-revalidate caching strategy. The `handler` will be called only when the cache is empty or stale. The wrapped handler is accessible as a decorator on the Fastify instance. By default, the handler's name will be used to name the decorator (ie. `fastify.myHandler`). A custom decorator name can configured using the `name` option.\n\n- **`interval`: `number`** - The interval in seconds used to determine when a cache will be deemed stale. *Defaults to `60` seconds*.\n\n- **`name`: `string`** - A custom name for the decorator added by this plugin. By default, the name property of the `handler` will be used.\n\n- **`cache`: `Pick<Map, 'get' | 'set' | 'delete'>`** - The cache instance for the plugin to use. Must contain `get`, `set`, and `delete` methods matching the signature of a `Map`. Method names can be changed using the `cacheClientMapping` option below. *Defaults to `new Map()`*.\n\n- **`cacheClientMapping`: `{ get: string, set: string, delete: string }`** - An object used to map custom client method names to the default `cache` method names. For example, if using a Redis client you would pass in `{ delete: 'del' }`.\n\n- **`logger`: `{ debug: (log: string) => void }`** - A logger to be used for the plugin's debug logs. *Defaults to `fastify.log` (Note: the log-level must be set to 'debug' to view the plugin's logs)*.\n","readmeFilename":"README.md"}