|
| 1 | + |
| 2 | +[](https://coveralls.io/github/DigitalBrainJS/cp-axios?branch=master) |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | + |
| 7 | +##Table of contents |
| 8 | +- [SYNOPSIS](#synopsis-sparkles) |
| 9 | +- [Installation](#installation-hammer) |
| 10 | +- [CDN bundle](#cdn-bundle) |
| 11 | +- [Usage examples](#usage-examples) |
| 12 | + - [Request aborting using CPromise cancelation API](#request-aborting-using-cpromise-cancelation-api) |
| 13 | + - [Request aborting using AbortController signal](#request-aborting-using-abortcontroller-signal) |
| 14 | + - [Request aborting using Axios cancelToken](#request-aborting-using-axios-canceltoken) |
| 15 | + - [Using generators as async functions](#using-generators-as-async-functions) |
| 16 | + - [Abortable concurrent requests](#abortable-concurrent-requests) |
| 17 | +- [API Reference](#api-reference) |
| 18 | +- [License](#license) |
| 19 | + |
| 20 | + |
| 21 | +## SYNOPSIS :sparkles: |
| 22 | + |
| 23 | +**cpAxios** is a simple [axios](https://www.npmjs.com/package/axios) wrapper that provides an advanced cancellation api. |
| 24 | + |
| 25 | +This library supports three types of the cancelation API that could be used *simultaneously*: |
| 26 | +- Axios [cancelableToken](https://github.com/axios/axios#cancellation) |
| 27 | +- [AbortController](https://developer.mozilla.org/en-US/docs/Web/API/AbortController) signal |
| 28 | +- [CPromise](https://www.npmjs.com/package/c-promise2) promise cancelation API |
| 29 | + |
| 30 | +## Installation :hammer: |
| 31 | + |
| 32 | +```bash |
| 33 | +$ npm install cp-axios |
| 34 | +``` |
| 35 | + |
| 36 | +```bash |
| 37 | +$ yarn add cp-axios |
| 38 | +``` |
| 39 | + |
| 40 | +### CDN bundle |
| 41 | + |
| 42 | +- [production UMD bundle](https://unpkg.com/cp-axios) (or [minified](https://unpkg.com/cp-axios/dist/cp-axios.umd.min.js) ~31KB) |
| 43 | + |
| 44 | +module global export- `cpAxios` |
| 45 | + |
| 46 | +## Usage examples |
| 47 | + |
| 48 | +#### Live Example |
| 49 | + |
| 50 | +[Live browser example](https://codesandbox.io/s/mutable-breeze-wdyp) |
| 51 | + |
| 52 | +#### Request aborting using CPromise cancelation API: |
| 53 | +````javascript |
| 54 | + const cpAxios= require('cp-axios'); |
| 55 | + |
| 56 | + const chain = cpAxios(url) |
| 57 | + .timeout(5000) |
| 58 | + .then(request => { |
| 59 | + console.log(`Done: ${JSON.stringify(request.json())}`) |
| 60 | + }, err => { |
| 61 | + console.warn(`Request failed: ${err}`) |
| 62 | + }); |
| 63 | + |
| 64 | + setTimeout(() => { |
| 65 | + chain.cancel(); |
| 66 | + }, 500); |
| 67 | +```` |
| 68 | + |
| 69 | +#### Request aborting using AbortController signal: |
| 70 | +````javascript |
| 71 | + const cpAxios= require('cp-axios'); |
| 72 | + const CPromise= require('c-promise2'); |
| 73 | + |
| 74 | +// you could to use any other AbortController implementation, but CPromise already provides it |
| 75 | + const abortController = new CPromise.AbortController(); |
| 76 | + const {signal} = abortController; |
| 77 | + |
| 78 | + const chain = cpAxios(url, {signal}) |
| 79 | + .timeout(5000) |
| 80 | + .then(request => { |
| 81 | + console.log(`Done: ${JSON.stringify(request.json())}`) |
| 82 | + }, err => { |
| 83 | + console.warn(`Request failed: ${err}`) |
| 84 | + }); |
| 85 | + |
| 86 | + setTimeout(() => { |
| 87 | + chain.cancel(); |
| 88 | + }, 500); |
| 89 | +```` |
| 90 | + |
| 91 | +#### Request aborting using Axios cancelToken: |
| 92 | +````javascript |
| 93 | + const cpAxios= require('cp-axios'); |
| 94 | + const CPromise= require('c-promise2'); |
| 95 | + |
| 96 | + const source = cpAxios.CancelToken.source(); |
| 97 | + |
| 98 | + const chain = cpAxios(url, {cancelToken: source.token}) |
| 99 | + .timeout(5000) |
| 100 | + .then(request => { |
| 101 | + console.log(`Done: ${JSON.stringify(request.json())}`) |
| 102 | + }, err => { |
| 103 | + console.warn(`Request failed: ${err}`) |
| 104 | + }); |
| 105 | + |
| 106 | + setTimeout(() => { |
| 107 | + source.cancel(); |
| 108 | + }, 500); |
| 109 | +```` |
| 110 | + |
| 111 | +#### Using generators as async functions: |
| 112 | + |
| 113 | +````javascript |
| 114 | +const cpAxios= require('cp-axios'); |
| 115 | +const CPromise= require('c-promise2'); |
| 116 | +const url= 'https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=5s'; |
| 117 | + |
| 118 | +const chain= CPromise.from(function*(){ |
| 119 | + try{ |
| 120 | + const response= yield cpAxios(url).timeout(5000); |
| 121 | + console.log(`Done: `, response.data) |
| 122 | + }catch(err){ |
| 123 | + console.log(`Error: `, err) |
| 124 | + } |
| 125 | +}); |
| 126 | + |
| 127 | + setTimeout(()=> chain.cancel(), 1000); // abort the request after 1000ms |
| 128 | +```` |
| 129 | + |
| 130 | +#### Abortable concurrent requests |
| 131 | + |
| 132 | +````javascript |
| 133 | +const cpAxios= require('cp-axios'); |
| 134 | +const CPromise = require('c-promise2'); |
| 135 | + |
| 136 | +// same as cpAxios.all([...]) |
| 137 | +const chain= CPromise.all([ |
| 138 | + cpFetch("https://run.mocky.io/v3/753aa609-65ae-4109-8f83-9cfe365290f0?mocky-delay=3s"), |
| 139 | + cpFetch("https://run.mocky.io/v3/30a97b24-ed0e-46e8-9f78-8f954aead2f8?mocky-delay=5s") |
| 140 | +]).timeout(10000).then(responses=> { |
| 141 | + console.log(`Results :`, responses); |
| 142 | +}, function (err) { |
| 143 | + console.warn(`We got an error: ${err}`); |
| 144 | +}); |
| 145 | + |
| 146 | +// other request will aborted if one fails |
| 147 | + |
| 148 | +// setTimeout(()=> chain.cancel(), 1000); // abort the request after 1000ms |
| 149 | +```` |
| 150 | + |
| 151 | +## API Reference |
| 152 | + |
| 153 | +The package exports a wrapped version of the axios instance. |
| 154 | +See the axios [documentation](https://www.npmjs.com/package/axios#axios) to gen more information. |
| 155 | + |
| 156 | +`cpAxios(url, {signal, ...nativeFetchOptions}): CPromise` |
| 157 | + |
| 158 | +Options: |
| 159 | + |
| 160 | +- `signal`- the AbortController signal |
| 161 | + |
| 162 | +- `...nativeAxiosOptions`- other options supported by [axios](https://www.npmjs.com/package/axios) package |
| 163 | + |
| 164 | +Learn more about [CPromise](https://www.npmjs.com/package/c-promise2) features |
| 165 | +## License |
| 166 | + |
| 167 | +The MIT License Copyright (c) 2020 Dmitriy Mozgovoy [email protected] |
| 168 | + |
| 169 | +Permission 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: |
| 170 | + |
| 171 | +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 172 | + |
| 173 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 174 | +INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 175 | +PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 176 | +DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 177 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 178 | + |
0 commit comments