{"_id":"@sozialhelden/fetch-cache","_rev":"5-24c1a21ca7c51cf5a1cd7fb1ae3a6be9","name":"@sozialhelden/fetch-cache","dist-tags":{"latest":"2.0.1"},"versions":{"1.1.0":{"name":"@sozialhelden/fetch-cache","private":false,"version":"1.1.0","license":"MIT","main":"dist/index.js","module":"dist/index.mjs","author":{"name":"Sebastian Felix Zappe","email":"sebastian@sozialhelden.de"},"repository":{"type":"git","url":"git+https://github.com/sozialhelden/fetch-cache.git"},"types":"dist/index.d.ts","bundleDependencies":[],"deprecated":false,"description":"A cached WhatWG fetch with URLs as cache keys, featuring eviction based on LRU, maximal item count and/or TTL.","devDependencies":{"@babel/parser":"^7.5.5","@babel/plugin-proposal-class-properties":"^7.5.5","@babel/plugin-transform-runtime":"^7.5.5","@babel/polyfill":"^7.4.4","@babel/preset-env":"^7.5.5","@babel/preset-typescript":"^7.3.3","@babel/runtime":"^7.5.5","@babel/types":"^7.5.5","@types/express":"^4.17.0","@types/jest":"^24.0.15","@types/node-fetch":"^2.5.0","abort-controller":"^3.0.0","babel-jest":"^24.8.0","commitizen":"^4.0.3","core-js":"^2.6.9","coveralls":"^3.0.0","cross-env":"^5.1.1","cz-conventional-changelog":"^2.1.0","express":"^4.17.1","jest":"^24.8.0","jest-cli":"^24.8.0","node-fetch":"^2.6.0","normalize-url":"^4.3.0","rimraf":"^2.6.1","semantic-release":"^12.0.0","ts-jest":"^24.0.2","tslint":"^5.5.0","tslint-config-airbnb":"^5.11.1","tslint-config-prettier":"^1.6.0","tslint-config-standard":"^7.0.0","typescript":"^3.3.3333"},"scripts":{"prebuild":"rimraf dist","build-mjs":"tsc -d && mv dist/index.js dist/index.mjs","build-cjs":"tsc -m commonjs","build":"npm run build-mjs && npm run build-cjs && rm dist/*.spec.*","start":"tsc -w","test":"jest","lint":"tslint tslint.json","test:watch":"jest --watch","test:prod":"npm run lint && npm run test -- --coverage --no-cache","deploy-docs":"ts-node tools/gh-pages-publish","report-coverage":"cat ./coverage/lcov.info | coveralls","commit":"git-cz","semantic-release":"semantic-release pre && npm publish && semantic-release post","semantic-release-prepare":"ts-node tools/semantic-release-prepare","format":"tslint --fix src/**/*.ts","prettier":"npx prettier --fix src/**/*.ts test/**/*.ts --write --single-quote","deploy":"npm run build && npm run test:prod && npm publish --access public"},"dependencies":{"@sozialhelden/hamster-cache":"^1.0.0"},"gitHead":"77aebed77beab2948bde360211a1d3ccee02454f","bugs":{"url":"https://github.com/sozialhelden/fetch-cache/issues"},"homepage":"https://github.com/sozialhelden/fetch-cache#readme","_id":"@sozialhelden/fetch-cache@1.1.0","_nodeVersion":"10.11.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-bfuiFS/Z901Xj18jXtg5ejjNQZJ1QwsKyUACI2wpIXF81ia53SOscygHJ+GfeHnV3YgsGrJDHjnKeqgvxKXMBQ==","shasum":"59c4046a62c09671f509ad1c719cc1f2880abfc2","tarball":"https://registry.npmjs.org/@sozialhelden/fetch-cache/-/fetch-cache-1.1.0.tgz","fileCount":16,"unpackedSize":17304,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdQLfPCRA9TVsSAnZWagAAGvIP/jKv7B7ymqjkRkpiL4QJ\n04tkLoK2d2Mjfyd5SFNexFAicc1+EQDlLk4UFA5P46JMMuj8GrMKnBiiUtYP\ny7HmaDinFIestPQJL+T0f4vVcCgHE7pCDwpzN2Bvb7T58nLkp+r7GPBhp0nf\nMaGDWGlJNmH1+cU7Ca4/bhLXUyLREfyNdb/XbCOTDMigk9xZb9lLS7KfJUmk\ngmJ+xD+FHkr53uCR6cKlnc/gIrBlUDgifUL/U0MSA/vKxytu8ufrpXFpR2ox\nMehW3Ku+7Y9Kod10+4M0BvV330GULY9PV3YFlmXxn/rLSReiDzWFtbImeQhB\nKGBQSanxqQj5Cmi5awaNJRo29A9D1s/aDSK1NvlmYCkIbj64oitvcdpG6t1l\n9lNRV8qrdE6vM3FV5obVPho8wY4L9EGkaXpQXfiT+d8FgIi1ahfoha7O3elR\nM/osynLedO4zKEykteG1KymSBEA5Bkhr+LefCDHzU3IXSeH/sGG6IEMAh3Jo\nd/qVeekjQpH/syplKffGmgCHw9RwrQJLIFAwAZWFV/mxYwIQY9MbpLGeKTcG\na12nwKsbcVLLODLOeSn9JSAjJZUKTcvMnZqfOGMuROam8pPq6wySZP77dQ3F\nqxy7E7O+Mvz3IMNsPu6XDNQubQRVOL9Wbjquhaq3D1QuOPVEPscXCaFIDKuU\ng19X\r\n=WZos\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDPa7ptZjFSLjWEcLNjzxcL4QWzTJIWpswmEZ1giHU/hgIgd1btR79sx832A+DjJPQJP180Rnkg3D7DkT7XlQYaufE="}]},"maintainers":[{"name":"hypo808","email":"npm@felixzappe.com"},{"name":"mutaphysis","email":"henrik@mutaphysis.de"}],"_npmUser":{"name":"hypo808","email":"npm@felixzappe.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fetch-cache_1.1.0_1564522446418_0.05574744237461071"},"_hasShrinkwrap":false},"2.0.1":{"name":"@sozialhelden/fetch-cache","private":false,"version":"2.0.1","license":"MIT","main":"dist/index.js","module":"dist/index.mjs","author":{"name":"Sebastian Felix Zappe","email":"sebastian@sozialhelden.de"},"repository":{"type":"git","url":"git+https://github.com/sozialhelden/fetch-cache.git"},"types":"dist/index.d.ts","bundleDependencies":[],"deprecated":false,"description":"A cached WhatWG fetch with URLs as cache keys, featuring eviction based on LRU, maximal item count and/or TTL.","devDependencies":{"@babel/parser":"^7.5.5","@babel/plugin-proposal-class-properties":"^7.5.5","@babel/plugin-transform-runtime":"^7.5.5","@babel/polyfill":"^7.4.4","@babel/preset-env":"^7.5.5","@babel/preset-typescript":"^7.3.3","@babel/runtime":"^7.5.5","@babel/types":"^7.5.5","@types/express":"^4.17.0","@types/jest":"^24.0.15","@types/node-fetch":"^2.5.0","abort-controller":"^3.0.0","babel-jest":"^24.8.0","commitizen":"^4.0.3","core-js":"^2.6.9","coveralls":"^3.0.0","cross-env":"^5.1.1","cz-conventional-changelog":"^2.1.0","express":"^4.17.1","jest":"^24.8.0","jest-cli":"^24.8.0","node-fetch":"^2.6.0","normalize-url":"^4.3.0","rimraf":"^2.6.1","semantic-release":"^12.0.0","ts-jest":"^24.0.2","tslint":"^5.5.0","tslint-config-airbnb":"^5.11.1","tslint-config-prettier":"^1.6.0","tslint-config-standard":"^7.0.0","typescript":"^3.3.3333"},"scripts":{"prebuild":"rimraf dist","build-mjs":"tsc -d && mv dist/index.js dist/index.mjs","build-cjs":"tsc -m commonjs","build":"npm run build-mjs && npm run build-cjs && rm dist/*.spec.*","start":"tsc -w","test":"jest","lint":"tslint tslint.json","test:watch":"jest --watch","test:prod":"npm run lint && npm run test -- --coverage --no-cache","deploy-docs":"ts-node tools/gh-pages-publish","report-coverage":"cat ./coverage/lcov.info | coveralls","commit":"git-cz","semantic-release":"semantic-release pre && npm publish && semantic-release post","semantic-release-prepare":"ts-node tools/semantic-release-prepare","format":"tslint --fix src/**/*.ts","prettier":"npx prettier --fix src/**/*.ts test/**/*.ts --write --single-quote","deploy":"npm run build && npm run test:prod && npm publish --access public"},"dependencies":{"@sozialhelden/hamster-cache":"^1.0.0"},"gitHead":"7372bf5b56997a2a00b126a728485db63b08ccdf","bugs":{"url":"https://github.com/sozialhelden/fetch-cache/issues"},"homepage":"https://github.com/sozialhelden/fetch-cache#readme","_id":"@sozialhelden/fetch-cache@2.0.1","_nodeVersion":"10.11.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-vMlsdT5JQCGjx1fcFxmMNh7ZKppjjsfUAeZEhhNwhEL7GaqbZXsD1OXEyx2IcRa25ZuZtvJSV6Q3rE77VRdLvg==","shasum":"ab7072c68e8d63da1ef90ae59f677b3a2b47d412","tarball":"https://registry.npmjs.org/@sozialhelden/fetch-cache/-/fetch-cache-2.0.1.tgz","fileCount":16,"unpackedSize":17568,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJdQWWcCRA9TVsSAnZWagAA0IQQAJ7aXwheCYGljWjXUzzo\n4S+E8EJ8yt1S2z0RcDPZfJ/RwjGwiE5mOJTuj4sbTwFS1GS5VHd2xI9Ff+6D\nvWk0MM1v2WmaIK3TA0hcbPWdYJhqe4igUeftaoYn7Fg61tFoQwQFyMV+89os\n1Cj1nq+9MIBjSbv0mkiWasoo6FToodIO17Ad9VD/gpDKATbpBLt0S6ir0Iph\nj8sYH8boLz/kd+AweEyAOmnhyw4VArXyx7JZn5xmmySt6alKVMhfe0mtcR/S\nJ2r2DBVuoScGzjT+v7gG3AEADJZt///FeAgVY/v3THLmtPcsEaFQnrq9CB3p\nMA8Q+mXYDiib5mRd4/mxjE8QA68XjTq/+7OpYrC/ocpT4uQT+4O6Zzc6t5Ng\nzPsNtrBIw8ojau6Uonl8AbXnNpEXN1Sfy5kgwvWn+RJ1j0ytvNmrfXi2h8Wg\nQfMT0Q3OndlntBTzn+SRa+HU+cWeCoUxbBRvIgPisEsH9t0XEXqPS078z3/Q\nLtMxcBOGALkCQN9m75VduGt2T9Q2ktxIizAi5+YWcGmcjiWhg/LOZUmNS118\nFIvzyNh21WfHj9oaYOsjFsPOpYbEU8w1nQp60iy88zJabj2eY2zFFsO4hgql\nOjbWOjoHf2z3Qi+B7tnoD524LAyusr007VdLGUBVR651qYlqHZ00W13pYqj5\nEJEQ\r\n=3BG+\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCICjBb6zQflLECOusWAG9GC/Jkzm4zaaMS3xySny5EIPkAiEA2amfwR/NPFfaFdWTa/7bTAkd8svndVoXBvwwQRoEgb4="}]},"maintainers":[{"name":"hypo808","email":"npm@felixzappe.com"},{"name":"mutaphysis","email":"henrik@mutaphysis.de"}],"_npmUser":{"name":"hypo808","email":"npm@felixzappe.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/fetch-cache_2.0.1_1564566939493_0.1735238893373714"},"_hasShrinkwrap":false}},"time":{"created":"2019-07-30T21:34:06.273Z","1.1.0":"2019-07-30T21:34:06.626Z","modified":"2022-04-07T01:21:26.233Z","2.0.1":"2019-07-31T09:55:39.633Z"},"maintainers":[{"email":"developers@sozialhelden.de","name":"sozialhelden_developer"}],"description":"A cached WhatWG fetch with URLs as cache keys, featuring eviction based on LRU, maximal item count and/or TTL.","homepage":"https://github.com/sozialhelden/fetch-cache#readme","repository":{"type":"git","url":"git+https://github.com/sozialhelden/fetch-cache.git"},"author":{"name":"Sebastian Felix Zappe","email":"sebastian@sozialhelden.de"},"bugs":{"url":"https://github.com/sozialhelden/fetch-cache/issues"},"license":"MIT","readme":"# fetch-cache 🐕\n\nA cache for WhatWG fetch calls.\n\n- Supports TypeScript\n- Uses normalized URLs as cache keys\n- Can normalize URLs for better performance (you can configure how)\n- Does not request the same resource twice if the first request is still loading\n- Customizable TTLs per request, dependent on HTTP status code or in case of network errors\n- Supports all [Hamster Cache](https://github.com/sozialhelden/hamster-cache) features, e.g. eviction based on LRU, maximal cached item count and/or per-item TTL.\n- Runs in NodeJS, but should be isometric && browser-compatible (not tested yet! try at your own risk 🙃)\n\n## Installation\n\n```bash\nnpm install --save @sozialhelden/fetch-cache\n#or\nyarn add @sozialhelden/fetch-cache\n```\n\n## Usage examples\n\n### Initialization\n\nBring your own `fetch` - for example:\n\n- your modern browser's [fetch function](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API)\n- [node-fetch](https://github.com/bitinn/node-fetch), a NodeJS implementation\n- [fetch-retry](https://github.com/jonbern/fetch-retry) for automatic request retrying with exponential backoff\n- [isomorphic-unfetch](https://github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch), an isometric implementation for browsers (legacy and modern) and Node.js\n\nConfigure the cache and use `cache.fetch()` as if you would call `fetch()` directly:\n\n```typescript\nimport FetchCache from '@sozialhelden/fetch-cache';\n\nconst fetch = require('node-fetch'); // in NodeJS\n// or\nconst fetch = window.fetch; // in newer browsers\n\nconst fetchCache = new FetchCache({\n  fetch,\n  cacheOptions: {\n    // Don't save more than 100 responses in the cache. Allows infinite responses by default\n    maximalItemCount: 100,\n    // When should the cache evict responses when its full?\n    evictExceedingItemsBy: 'lru', // Valid values: 'lru' or 'age'\n    // ...see https://github.com/sozialhelden/hamster-cache for all possible options\n  },\n});\n\n// either fetches a response over the network,\n// or returns a cached promise with the same URL (if available)\nconst url = 'https://jsonplaceholder.typicode.com/todos/1';\nfetchCache\n  .fetch(url, fetchOptions)\n  .then(response => response.body())\n  .then(console.log)\n  .catch(console.log);\n```\n\n### Basic caching operations\n\n```typescript\n// Add an external response promise and cache it for 10 seconds\nconst response = fetch('https://api.example.com');\n\n// Insert a response you got from somewhere else\nfetchCache.cache.set('http://example.com', response);\n\n// Set a custom TTL of 10 seconds for this specific response\nfetchCache.cache.set('http://example.com', response, { ttl: 10000 });\n\n// gets the cached response without side effects\nfetchCache.cache.peek(url);\n\n// `true` if a response exists in the cache, `false` otherwise\nfetchCache.cache.has(url);\n\n// same as `peek`, but returns response with meta information\nfetchCache.cache.peekItem(url);\n\n// same as `get`, but returns response with meta information\nfetchCache.cache.getItem(url);\n\n// Let the cache collect garbage to save memory, for example in fixed time intervals\nfetchCache.cache.evictExpiredItems();\n\n// removes a response from the cache\nfetchCache.cache.delete(url);\n\n// forgets all cached responses\nfetchCache.cache.clear();\n```\n\n### Vary TTLs depending on HTTP response code, headers, and more\n\nWhile the cache tries to [guess working TTLs for most use cases](./src/defaultTTL.ts), you might\nwant to customize how long a response (or rejected promise) should stay in the cache before it\nmakes a new request when you fetch the same URL again.\n\nFor example, you could set the TTL to one second, no matter if a request succeeds or fails (please\ndon't really do this, except you have a good reason):\n\n```typescript\nconst fetchCache = new FetchCache({ fetch, ttl: () => 1000 });\n```\n\n…or configure varying TTLs for specific HTTP response status codes (better):\n\n```typescript\nconst fetchCache = new FetchCache({\n  fetch,\n  ttl: ({ response, state, error }) => {\n    // state is 'running', 'resolved' or 'rejected' here.\n    if (response) {\n      // If a response is successful, keep it in the cache for 2 minutes\n      if (response.status === 200) return 2 * 60 * 1000;\n      // If a response is successful, keep it in the cache for 10 seconds so it shows up if the\n      // resource begins to exist in the meantime\n      if (response.status === 404) return 10 * 1000;\n    }\n    // If you return `undefined` here, the cache will use default TTL values for all other cases.\n  },\n});\n```\n\nFor an overview about more cases, consult [the default implementation](./src/defaultTTL.ts).\n\n### Normalize URLs\n\nYou can improve caching performance by letting the cache know if more than one URL points to the\nsame server-side resource. For this, provide a `normalizeURL` function that builds a canonical URL\nfrom a given one.\n\nThe cache will only hold one response per canonical URL then. This saves memory and network\nbandwidth.\n\n`normalize-url` is a helpful NPM package implementing real-world normalization rules like SSL\nenforcement and `www.` vs. non-`www.`-domain names. You can use it as normalization function:\n\n```bash\n# Install the package with\nnpm install normalize-url\n# or\nyarn add normalize-url\n```\n\n```typescript\nimport normalizeURL from 'normalize-url';\nimport fetch from 'node-fetch';\n\n// See https://github.com/sindresorhus/normalize-url#readme for all available normalization options\nconst cache = new FetchCache({\n  fetch,\n  normalizeURL(url) {\n    return normalizeURL(url, { forceHttps: true });\n  },\n});\n```\n\n## Contributors\n\n- [@dakeyras7](https://github.com/dakeyras7)\n- [@lennerd](https://github.com/lennerd)\n- [@mutaphysis](https://github.com/mutaphysis)\n- [@opyh](https://github.com/opyh)\n\nSupported by\n\n<img src='./doc/sozialhelden-logo.svg' width=\"200\">.\n","readmeFilename":"README.md"}