{"id":20597735,"url":"https://github.com/triggerdotdev/fetch-hero","last_synced_at":"2026-02-05T21:02:19.615Z","repository":{"id":43959192,"uuid":"437044078","full_name":"triggerdotdev/fetch-hero","owner":"triggerdotdev","description":"Gives fetch superpowers","archived":false,"fork":false,"pushed_at":"2022-09-23T15:11:20.000Z","size":840,"stargazers_count":4,"open_issues_count":11,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-03T10:39:10.494Z","etag":null,"topics":["cache","caching","fetch-api","http","http-client","javascript","redis","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/triggerdotdev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-12-10T16:29:46.000Z","updated_at":"2024-09-23T08:37:27.000Z","dependencies_parsed_at":"2022-09-15T20:01:24.777Z","dependency_job_id":null,"html_url":"https://github.com/triggerdotdev/fetch-hero","commit_stats":null,"previous_names":["jsonhero-io/fetch-hero"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/triggerdotdev/fetch-hero","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggerdotdev%2Ffetch-hero","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggerdotdev%2Ffetch-hero/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggerdotdev%2Ffetch-hero/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggerdotdev%2Ffetch-hero/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/triggerdotdev","download_url":"https://codeload.github.com/triggerdotdev/fetch-hero/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/triggerdotdev%2Ffetch-hero/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29134225,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T20:50:26.975Z","status":"ssl_error","status_checked_at":"2026-02-05T20:49:26.082Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","caching","fetch-api","http","http-client","javascript","redis","typescript"],"created_at":"2024-11-16T08:23:43.292Z","updated_at":"2026-02-05T21:02:19.160Z","avatar_url":"https://github.com/triggerdotdev.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fetch Hero\n\n\u003e Extends server-side `fetch` with extra features\n\n![Coverage lines](./badges/badge-lines.svg)\n\n## Features\n\n- No magic. Wraps `fetch` and returns a new `fetch` function\n- Enables RFC 7234 and RFC 5861 compliant HTTP caching (using [http-cache-semantics](https://github.com/kornelski/http-cache-semantics))\n- Bypass cache semantics on GET and HEAD requests to set a specific cache TTL\n- Support for multiple storage backends by using [Keyv](https://github.com/jaredwray/keyv)\n- Works with local and shared caches\n- Custom namespaces\n- Handles caching a response body through [json-buffer](https://www.npmjs.com/package/json-buffer)\n- Normalizes urls to increase cache hits\n- Retry failed requests, using fine grained retry semantics powered by [async-retry](https://github.com/vercel/async-retry)\n- Written in strict Typescript\n- BYOF (Bring Your Own Fetch)\n\n## Usage\n\nInstall Fetch Hero\n\n```bash\n$ npm install --save @jsonhero/fetch-hero\n```\n\n`fetchHero` wraps the `fetch` function and returns a new `fetch` function, with the exact same interface\n\n```js\nconst fetchHero = require(\"@jsonhero/fetch-hero\");\nconst nodeFetch = require(\"node-fetch\");\n\nconst fetch = fetchHero(nodeFetch);\n\n// Now use fetch exactly how you would without fetchHero\nconst response = await fetch(\"http://google.com\");\n```\n\nBy default http semantic caching is disabled, you can enable it by setting the `httpCache.enabled` option to `true`\n\n```js\nconst fetch = fetchHero(nodeFetch, { httpCache: { enabled: true } });\n```\n\nThis will use an in-memory store, but you can customize it by supplying an object that implements the [Map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) interface\n\n```js\nconst customMap = new Map();\n\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: { enabled: true, store: customMap },\n});\n```\n\nOr in Typescript:\n\n```typescript\nconst customMap = new Map\u003cany, any\u003e();\n\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: { enabled: true, store: customMap },\n});\n```\n\nYou can also pass any connection string to `cache.store` that [Keyv](https://github.com/jaredwray/keyv).\n\n```bash\n$ npm install --save @keyv/redis\n```\n\n`@keyv/redis` will automatically be imported when using a `redis://` connection string\n\n```js\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: { enabled: true, store: \"redis://user:pass@localhost:6379\" },\n});\n```\n\n### Namespacing\n\nBy default, cache keys will be namespaced by `\"fetch-hero.default\"`\n\n```js\nconst customMap = new Map();\n\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: { enabled: true, store: customMap },\n});\n\nawait fetch(\"http://test.dev/foo\");\n\ncustomMap.has(\"fetch-hero.default:GET:http://test.dev/foo\"); // true\n```\n\nYou can supply a custom namespace using the `cache.namespace` option\n\n```js\nconst customMap = new Map();\n\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: { enabled: true, store: customMap, namespace: \"foobar\" },\n});\n\nawait fetch(\"http://test.dev/foo\");\n\ncustomMap.has(\"fetch-hero.default:GET:http://test.dev/foo\"); // false\ncustomMap.has(\"fetch-hero.foobar:GET:http://test.dev/foo\"); // true\n```\n\n### Shared caches\n\nIf you are using a shared cache (e.g. a Redis instance) and plan to share cached responses with more than 1 user, then you must set `httpCache.shared` to `true` (it is `true` by default, for security reasons)\n\n```js\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: {\n    enabled: true,\n    shared: true,\n    store: \"redis://user:pass@localhost:6379\",\n  },\n});\n\nawait fetch(\"http://test.dev/foo\");\n```\n\nThis will effect which responses will be cached. For example, responses with the `Cache-Control` header set to `private` or with a `s-maxage` directive will not be storable in a shared cache.\n\nIf you are using something like redis for your cache storage, and would like to still cache private responses, then set `httpCache.shared` to `false` and provide a namespace when calling the `fetch` function:\n\n```js\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: {\n    enabled: true,\n    shared: false,\n    store: \"redis://user:pass@localhost:6379\",\n  },\n});\n\n// Using the user identifier so we don't mix cached responses\nawait fetch(\"http://test.dev/private\", {\n  fh: { cache: { namespace: user.identifier } },\n});\n```\n\nAs you can see, the `fetch` function above accepts a non-standard `fh` property, allowing you to customize Fetch Hero behaviour on a per request basis. See the [RequestInitFhProperties]() documentation for more info.\n\n## HTTP Cache semantic bypassing\n\nYou can bypass the HTTP caching semantics on GET and HEAD requests by passing the `bypass` option with a `ttl` in seconds, like so:\n\n```typescript\nconst fetch = fetchHero(nodeFetch, {\n  httpCache: { enabled: true, bypass: { ttl: 120 } }, // 120 seconds\n});\n\nawait fetch(\"http://test.dev/foo\");\n// This will ignore the response headers and return a cached response\nawait fetch(\"http://test.dev/foo\");\n```\n\nThis will force requests to return cached responses for 120 seconds after the first fresh request is made, bypassing the HTTP cache semantics of the response headers.\n\n### Retrying\n\nYou can enable retrying to retry requests with failed responses:\n\n```typescript\nconst fetch = fetchHero(nodeFetch, {\n  retrying: { enabled: true },\n});\n\nawait fetch(\"http://test.dev/foo\"); // Will retry up to 3 times if response is 500, 502, 503, or 504\n```\n\nYou can customize which response codes will be retried:\n\n```typescript\nconst fetch = fetchHero(nodeFetch, {\n  retrying: { enabled: true, retryOn: [429] }, // Only retry when there is a 429 error\n});\n\nawait fetch(\"http://test.dev/foo\"); // Will retry up to 3 times if response is 429\n```\n\nYou can also customize the [async-retry options](https://github.com/vercel/async-retry):\n\n````typescript\nconst fetch = fetchHero(nodeFetch, {\n  retrying: { enabled: true, options: { retries: 10, factor: 1.2, minTimeout: 250, maxTimeout: 10000, randomize: true } }\n});\n\nawait fetch(\"http://test.dev/foo\");\n```\n\n## Storage Adapters\n\nView the [Keyv documentation](https://github.com/jaredwray/keyv) to learn more about the storage adapters that Fetch Hero supports.\n\n## API\n\n### `fetchHero` function\n\n### `FetchHeroOptions` object\n\n### `RequestInitFhProperties` properties\n\nAn object containing FetchHero-specific properties that can be set on the Request object. For example:\n\n```js\n// Disable catching for this request\nfetch(event.request, { fh: { httpCache: { enabled: false } } });\n````\n\n#### `httpCache` _optional_\n\nAn object to customize the http caching behaviour of FetchHero, with the following parameters:\n\n| Parameter   | Type                           | Description                                                                                                                                          |\n| :---------- | :----------------------------- | :--------------------------------------------------------------------------------------------------------------------------------------------------- |\n| `enabled`   | `boolean`                      | Set to false to disable caching for the request. If `fetchHero` was initialized without caching disabled, setting this to `true` will have no effect |\n| `namespace` | `string`                       | Set a custom namespace for the request.                                                                                                              |\n| `options`   | `CachePolicy.Options`          | Set custom [CachePolicy.Options](#cache-policy-options) object for the request                                                                       |\n| `bypass`    | `HTTPSemanticBypassingOptions` | Set custom [HTTPSemanticBypassingOptions](#cache-policy-options) object for the request                                                              |\n\n#### `HTTPSemanticBypassingOptions`\n\nAn object to customize the http semantic caching bypassing behaviour of FetchHero, with the following parameters:\n\n| Parameter | Type     | Description                                             |\n| :-------- | :------- | :------------------------------------------------------ |\n| `ttl`     | `number` | Number of seconds to bypass HTTP caching semantics for. |\n\n## Roadmap\n\n- [ ] Support for [minipass-fetch](https://github.com/npm/minipass-fetch)\n- [ ] Proxy support\n- [ ] GZIP support\n- [ ] Request pooling\n- [ ] Persistent connections\n- [ ] Limit memory usage\n- [ ] Add performance tests\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftriggerdotdev%2Ffetch-hero","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftriggerdotdev%2Ffetch-hero","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftriggerdotdev%2Ffetch-hero/lists"}