{"id":17472737,"url":"https://github.com/shinnn/wise-fetch","last_synced_at":"2026-07-03T19:32:05.061Z","repository":{"id":57398309,"uuid":"145374280","full_name":"shinnn/wise-fetch","owner":"shinnn","description":"Feature-rich node-fetch","archived":false,"fork":false,"pushed_at":"2019-05-26T18:14:07.000Z","size":195,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-02T19:07:26.902Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shinnn.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":"2018-08-20T06:15:57.000Z","updated_at":"2024-07-01T19:26:01.000Z","dependencies_parsed_at":"2022-08-29T18:51:59.785Z","dependency_job_id":null,"html_url":"https://github.com/shinnn/wise-fetch","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Fwise-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Fwise-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Fwise-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shinnn%2Fwise-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shinnn","download_url":"https://codeload.github.com/shinnn/wise-fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245991582,"owners_count":20706129,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2024-10-18T17:35:08.892Z","updated_at":"2025-10-29T23:35:06.267Z","avatar_url":"https://github.com/shinnn.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# wise-fetch\n\n[![npm version](https://img.shields.io/npm/v/wise-fetch.svg)](https://www.npmjs.com/package/wise-fetch)\n[![Build Status](https://travis-ci.com/shinnn/wise-fetch.svg?branch=master)](https://travis-ci.com/shinnn/wise-fetch)\n[![codecov](https://codecov.io/gh/shinnn/wise-fetch/branch/master/graph/badge.svg)](https://codecov.io/gh/shinnn/wise-fetch)\n\nFeature-rich [node-fetch](https://github.com/bitinn/node-fetch):\n\n* Built-in [RFC compliant response cache](https://tools.ietf.org/html/rfc7234)\n* Proxy support\n* [Abortable](#optionssignal)\n* [Base URL](#optionsbaseurl) support\n* Automatic `Promise` rejection of unsuccessful responses by default\n* Strict URL validation\n\n```javascript\nconst wiseFetch = require('wise-fetch');\n\n(async () =\u003e {\n  const response = await wiseFetch('https://example.org');\n\n  response.status; //=\u003e 200\n  response.headers.get('content-length'); //=\u003e '606'\n\n  const text = await response.text();\n  //=\u003e '\u003c!doctype html\u003e\\n\u003chtml\u003e\\n\u003chead\u003e\\n    \u003ctitle\u003eExample Domain\u003c/title\u003e ...'\n})();\n```\n\n## Installation\n\n[Use](https://docs.npmjs.com/cli/install) [npm](https://docs.npmjs.com/about-npm/).\n\n```\nnpm install wise-fetch\n```\n\n## API\n\n```javascript\nconst wiseFetch = require('wise-fetch');\n```\n\n### wiseFetch(*url* [, *options*])\n\n*url*: `string | URL` (HTTP or HTTPS URL)  \n*options*: `Object`  \nReturn: [`Promise\u003cResponse\u003e`](https://github.com/npm/node-fetch-npm/blob/v2.0.2/src/response.js#L21)\n\nThe API is very similar to the browser [`fetch`](https://developer.mozilla.org/docs/Web/API/WindowOrWorkerGlobalScope/fetch) API. It makes an HTTP or HTTPS request and returns a `Promise` of a [node-fetch-npm](https://github.com/npm/node-fetch-npm) [`Response`](https://github.com/npm/node-fetch-npm#class-response) object that works as if [DOM `Response`](https://developer.mozilla.org/docs/Web/API/Response) but has [additional](https://github.com/npm/node-fetch-npm#bodybuffer) [methods](https://github.com/npm/node-fetch-npm#bodytextconverted).\n\nUnlike the `fetch` API, when the response is unsuccessful, that is, its status code is neither [`2xx`](https://tools.ietf.org/html/rfc7231#section-6.3), [`304`](https://tools.ietf.org/html/rfc7232#section-4.1), it will be rejected with an `Error` with a `response` property.\n\n```javascript\n(async () =\u003e {\n  try {\n    await wiseFetch('https://github.com/shinnn/it_does_not_exist');\n  } catch (err) {\n    err.message; //=\u003e '404 (Not Found) responded by a GET request to https://github.com/shinnn/it_does_not_exist.'\n    err.reponse.status; //=\u003e 404\n    await err.reponse.arrayBuffer(); //=\u003e ArrayBuffer { ... } (the response body)\n  }\n})();\n```\n\nThe response is cached to the [OS's default directory for temporary files](https://nodejs.org/api/os.html#os) in the [RFC 7234](https://tools.ietf.org/html/rfc7234) compliant way.\n\n#### options\n\nIt supports the [all](https://github.com/zkat/make-fetch-happen#--node-fetch-options) [options](https://github.com/zkat/make-fetch-happen#--make-fetch-happen-options) that [make-fetch-happen](https://github.com/zkat/make-fetch-happen) can receives, except for `counter` and `cacheManager`.\n\nWhen the program is running as an [npm script](https://docs.npmjs.com/misc/scripts), note that:\n\n* `proxy` option defaults to the value of [`https-proxy`](https://docs.npmjs.com/misc/config#https-proxy) or [`proxy`](https://docs.npmjs.com/misc/config#proxy) npm config depending on the request protocol.\n* `noProxy` option defaults to [`no-proxy`](https://docs.npmjs.com/misc/config#no-proxy) npm config.\n\nAdditionally, the following wise-fetch specific options are available.\n\n##### options.baseUrl\n\nType: `string | URL`\n\nSet the base URL to resolve against if the request URL is not absolute.\n\n```javascript\n(async () =\u003e {\n  const response = await wiseFetch('~shinnn', {baseUrl: 'https://www.npmjs.com'});\n  response.url; //=\u003e 'https://www.npmjs.com/~shinnn'\n})();\n```\n\n##### options.resolveUnsuccessfulResponse\n\nType: `boolean`  \nDefault: `false`\n\nReturn a resolved `Promise` even if the response is unsuccessful.\n\n```javascript\n(async () =\u003e {\n  const response = await wiseFetch('https://github.com/shinnn/this_does_not_exist', {\n    resolveUnsuccessfulResponse: true\n  });\n\n  response.statusText; //=\u003e 'Not Found'\n})();\n```\n\n##### options.signal\n\nType: [`AbortSignal`](https://developer.mozilla.org/docs/Web/API/AbortSignal)\n\nAllow a user to abort the request via the corresponding [`AbortController`](https://developer.mozilla.org/docs/Web/API/AbortController/AbortController). Read [the article about abortable fetch](https://developers.google.com/web/updates/2017/09/abortable-fetch) for more details.\n\nCurrently Node.js doesn't support [`AbortController`](https://developer.mozilla.org/docs/Web/API/AbortController), so that users need to substitute [the userland implementation](https://github.com/mysticatea/abort-controller) for it.\n\n```javascript\nconst AbortController = require('abort-controller');\n\nconst abortController = new AbortController();\n\n(async () =\u003e {\n  try {\n    await wiseFetch('https://loclahost:5000/very_large_contents', {signal: abortController.signal});\n  } catch (err) {\n    err.message; //=\u003e '... The GET request to https://loclahost:5000/very_large_contents was aborted'\n  }\n})();\n\nsetTimeout(() =\u003e abortController.abort(), 1000);\n```\n\n##### options.userAgent\n\nType: `string`\n\nA shorthand for setting `user-agent` property of `headers` option.\n\n### wiseFetch.create(*baseOptions*)\n\n*baseOptions*: `Object`  \nReturn: `Function`\n\nCreate a new `wiseFetch` with the given defaults.\n\n```javascript\nconst getGithubUserData = wiseFetch.create({\n  baseUrl: 'https://api.github.com/users/',\n  headers: {\n    accept: 'application/vnd.github.v3+json',\n    'user-agent': 'your app name'\n  }\n});\n\n(async () =\u003e {\n  await (await getGithubUserData('shinnn')).json();\n  //=\u003e {login: 'shinnn', id: 1131567, created_at: '2011-10-16T16:36:43Z', ...}\n})();\n```\n\n`headers` of each function call will be merged to the base headers.\n\n```javascript\nconst newWiseFetch = wiseFetch.create({\n  headers: {\n    'header-A': 'old value'\n    'header-B': 'value'\n  }\n});\n\nnewWiseFetch('https://example.org', {\n  headers: {\n    'header-A': 'updated value',\n    'header-C': 'new value'\n  }\n});\n/* The final `header` is {\n  'header-A': 'updated value',\n  'header-B': 'value'\n  'header-C': 'new value'\n}. */\n```\n\n##### baseOptions.frozenOptions\n\nType: `Set\u003cstring\u003e`\n\nMake given options unconfigurable in each function call.\n\n```javascript\nconst alwaysPost = wiseFetch.create({\n  method: 'post',\n  frozenOptions: new Set(['method'])\n});\n\n(async () =\u003e {\n  try {\n    await alwaysPost('https://example.org/api', {method: 'patch'});\n  } catch (err) {\n    err.toString();\n    //=\u003e TypeError: 'method' option is not configurable, but it was tried to be configured.\n  }\n})();\n```\n\n##### baseOptions.urlModifier\n\nType: `Function`\n\nUpdate a request URL to the value this function returns, before applying [`baseUrl` option](#optionsbaseurl) to it.\n\nThis function receives the original URL and is expected to return a `string` or `URL`.\n\n```javascript\n(async () =\u003e {\n  const response = await wiseFetch('https://example.org/', {urlModifier: url =\u003e `${url}?x=1`});\n  response.url; //=\u003e 'https://example.org/?x=1'\n})();\n```\n\n##### baseOptions.additionalOptionValidators\n\nType: `Array\u003cFunction\u003e`\n\nAn array of functions that performs additional option validation. Each functions receive an options `Object` and if at least one of then throws an error, `wiseFetch` will be rejected.\n\n```javascript\nconst {username} = require('os').userInfo();\n\nconst forceYourUA = wiseFetch.create({\n  additionalOptionValidators: [\n    options =\u003e {\n      if (options.userAgent \u0026\u0026 !options.userAgent.includes(username)) {\n        throw new Error('user agent must include your name!');\n      }\n    }\n  ]\n});\n\nforceYourUA('https://example.org', {userAgent: 'nothing'});\n// rejected with an Error: 'user agent must include your name!'\n```\n\n### wiseFetch.ABORT_ERROR_CODE\n\nType: `integer`\n\nAn error [`code`](https://nodejs.org/api/errors.html#errors_error_code) that wiseFetch adds to the error when the request is [aborted](#optionssignal).\n\n```javascript\n(async () =\u003e {\n  try {\n    await wiseFetch('https://example.org/', {signal: someSignal});\n  } catch (err) {\n    if (err.code === wiseFetch.ABORT_ERROR_CODE) {\n      console.log('Canceled');\n    } else {\n      throw err;\n    }\n  }\n})();\n```\n\n### wiseFetch.CACHE_DIR\n\nType: `string`\n\nA path of the directory where wise-fetch saves response cache.\n\nUsers can clear cache by deleting this directory.\n\n## License\n\n[ISC License](./LICENSE) © 2018 - 2019 Shinnosuke Watanabe\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinnn%2Fwise-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshinnn%2Fwise-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshinnn%2Fwise-fetch/lists"}