{"id":16823318,"url":"https://github.com/digitalbrainjs/c-fetch","last_synced_at":"2025-03-17T17:43:14.460Z","repository":{"id":56824041,"uuid":"298613101","full_name":"DigitalBrainJS/c-fetch","owner":"DigitalBrainJS","description":"cancelable fetch with timeout","archived":false,"fork":false,"pushed_at":"2021-05-13T00:26:10.000Z","size":1310,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-08T09:37:41.072Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/DigitalBrainJS.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-09-25T15:36:04.000Z","updated_at":"2022-12-13T18:23:42.000Z","dependencies_parsed_at":"2022-08-26T21:51:46.736Z","dependency_job_id":null,"html_url":"https://github.com/DigitalBrainJS/c-fetch","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fc-fetch","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fc-fetch/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fc-fetch/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DigitalBrainJS%2Fc-fetch/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DigitalBrainJS","download_url":"https://codeload.github.com/DigitalBrainJS/c-fetch/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244080867,"owners_count":20394999,"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-13T11:07:35.938Z","updated_at":"2025-03-17T17:43:14.414Z","avatar_url":"https://github.com/DigitalBrainJS.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"![Travis (.com)](https://img.shields.io/travis/com/DigitalBrainJS/c-fetch)\n[![Coverage Status](https://coveralls.io/repos/github/DigitalBrainJS/c-fetch/badge.svg?branch=master)](https://coveralls.io/github/DigitalBrainJS/c-fetch?branch=master)\n![npm](https://img.shields.io/npm/dm/cp-fetch)\n![npm bundle size](https://img.shields.io/bundlephobia/minzip/cp-fetch)\n![David](https://img.shields.io/david/DigitalBrainJS/c-fetch)\n\n## SYNOPSIS :sparkles:\n\ncpFetch is a simple wrapper around `fetch` with cancelable promise ([c-promise2](https://www.npmjs.com/package/c-promise2)). \nThis lib can be used for both backend and frontend development, platform specific fetch API is provided by \n[cross-fetch package](https://www.npmjs.com/package/cross-fetch).\n\n## Why :question:\nIt's good to have the ability to cancel fetch requests automatically when the related promise is canceling.\nUsing powerful of a promise with cancelation feature lets you automatically manage fetch request cycle, handling timeouts,\nabort the related request when you use concurrent requests.  \n\n## Features / Advantages\n- browser \u0026 node.js support\n- timeouts\n- cancellation (rejecting promise chain \u0026 aborting the related network request)\n- returns [CPromise](https://www.npmjs.com/package/c-promise2) instead of native\n- automatically aborting other requests, passed to the CPromise.all, if one fails.\n- automatically aborting other requests, passed to the CPromise.race, if one of them resolves or fails.\n\n## Installation :hammer:\n\n```bash\n$ npm install cp-fetch\n```\n\n```bash\n$ yarn add cp-fetch\n```\n\n````javascript\n// cross-platform version\nconst cpFetch= require('cpFetch'); \n\n// version that uses the global fetch API, instead of cross-fetch ponyfill \n// (for modern browsers only or in case you're using third-party polyfill)\nimport cpFetch from \"cp-fetch/lib/native\";\n````\n\n#### CDN bundle\nReady for use prebuilt UMD bundles for browser with all dependencies inside.\n- [production UMD cross-platform bundle with fetch ponyfill](http://unpkg.com/cp-fetch/dist/cp-fetch.umd.js) ([minified](https://unpkg.com/cp-fetch/dist/cp-fetch.umd.min.js) ~32KB)\n- [production UMD bundle](http://unpkg.com/cp-fetch/dist/native/index.umd.js) ([minified](http://unpkg.com/cp-fetch/dist/native/index.min.js) ~25KB)\n\nThese module bundles are only suitable to load as a script directly from the html page. \nIf you're using some module bunlder like `webpack` or `rollup`, \nplease import cjs module to avoid potential duplications in your project dependencies tree.\n\nGlobal module export is `cpFetch`.\n\n## Usage examples\n\n#### Live Example\n\n[Live browser example](https://codesandbox.io/s/ancient-glade-1wsnj)\n\n#### Abortable fetch with timeout\n\nA simple example:\n````javascript\nconst cpFetch= require('cp-fetch');\nconst url= 'https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s';\n\nconst chain = cpFetch(url, {timeout: 10000})\n    .then(response =\u003e response.json())\n    .then(data =\u003e console.log(`Done: `, data), err =\u003e console.log(`Error: `, err))\n\nsetTimeout(()=\u003e chain.cancel(), 1000); // abort the request after 1000ms \n\n// you able to call cancel() at any time to cancel the entire chain at any stage\n// Take into account the related network request will also be aborted\n````\n\nThe same using generators as async function:\n\n````javascript\nconst cpFetch= require('cp-fetch');\nconst CPromise= require('c-promise2');\nconst url= 'https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s';\n\nconst chain= CPromise.from(function*(){\n    try{\n        const response= yield cpFetch(url, {timeout: 5000});\n        console.log(`Done: `, yield response.json())\n    }catch(err){\n        console.log(`Error: `, err)\n    }   \n});\n\n setTimeout(()=\u003e chain.cancel(), 1000); // abort the request after 1000ms \n````\n\n#### Abortable concurrent requests\n\n````javascript\nconst cpFetch= require('cp-fetch');\nconst CPromise = require('c-promise2');\n\nconst chain= CPromise.race([\n    cpFetch(\"https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=3s\"),\n    cpFetch(\"https://run.mocky.io/v3/30a97b24-ed0e-46e8-9f78-8f954aead2f8?mocky-delay=5s\")\n]).timeout(10000).then((response)=\u003e {\n    console.log(`Result :`, response.data);\n}, function (err) {\n    console.warn(`We got an error: ${err}`);\n});\n\n// the slower request will be aborted\n\n// setTimeout(()=\u003e chain.cancel(), 1000); // abort the request after 1000ms \n````\n\n## API Reference\n\nThe package exports a wrapped version of the [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) function.\n\n`cFetch(url, {timeout, ...nativeFetchOptions}): CPromise`\n\nOptions:\n\n`timeout`- the timeout before the promise and related request will be rejected/aborted.\n\n`...nativeFetchOptions`- other native options of the fetch function.\n\nLearn more about [CPromise features](https://www.npmjs.com/package/c-promise2) \n\n## Related projects\n\n- [cp-axios](https://www.npmjs.com/package/cp-axios) - a simple axios wrapper that provides an advanced cancellation api \n- [c-promise2](https://www.npmjs.com/package/c-promise2) - promise with cancellation and progress capturing support \n- [use-async-effect2](https://www.npmjs.com/package/use-async-effect2) - cancel async code in functional React components\n- [cp-koa](https://www.npmjs.com/package/cp-koa) - Koa with cancelable middlewares\n\n## License\n\nThe MIT License Copyright (c) 2020 Dmitriy Mozgovoy robotshara@gmail.com\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,\nINCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR\nPURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,\nDAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalbrainjs%2Fc-fetch","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdigitalbrainjs%2Fc-fetch","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdigitalbrainjs%2Fc-fetch/lists"}