2
0
mirror of https://github.com/tenrok/axios.git synced 2026-05-15 11:59:42 +03:00

Adding axios.isCancel method

This commit is contained in:
Nick Uraltsev
2016-09-21 18:47:37 -07:00
parent 920769d0d7
commit 216e2a6787
9 changed files with 31 additions and 4 deletions
+1 -2
View File
@@ -471,14 +471,13 @@ You can cancel a request using a *cancel token*.
You can create a cancel token using the `CancelToken.source` factory as shown below:
```js
var Cancel = axios.Cancel;
var CancelToken = axios.CancelToken;
var source = CancelToken.source();
axios.get('/user/12345', {
cancelToken: source.token
}).catch(function(thrown) {
if (thrown instanceof Cancel) {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
} else {
// handle error
Vendored
+2 -1
View File
@@ -120,7 +120,8 @@ export interface AxiosStatic extends AxiosInstance {
(url: string, config?: AxiosRequestConfig): AxiosPromise;
create(config?: AxiosRequestConfig): AxiosInstance;
Cancel: CancelStatic;
CancelToken: CancelTokenStatic;
CancelToken: CancelTokenStatic;
isCancel(value: any): boolean;
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
}
+1
View File
@@ -37,6 +37,7 @@ axios.create = function create(defaultConfig) {
// Expose Cancel & CancelToken
axios.Cancel = require('./cancel/Cancel');
axios.CancelToken = require('./cancel/CancelToken');
axios.isCancel = require('./cancel/isCancel');
// Expose all/spread
axios.all = function all(promises) {
+2
View File
@@ -14,4 +14,6 @@ Cancel.prototype.toString = function toString() {
return 'Cancel' + (this.message ? ': ' + this.message : '');
};
Cancel.prototype.__CANCEL__ = true;
module.exports = Cancel;
+5
View File
@@ -0,0 +1,5 @@
'use strict';
module.exports = function isCancel(value) {
return !!(value && value.__CANCEL__);
};
+6
View File
@@ -34,6 +34,12 @@ describe('static api', function () {
it('should have factory method', function () {
expect(typeof axios.create).toEqual('function');
});
it('should have Cancel, CancelToken, and isCancel properties', function () {
expect(typeof axios.Cancel).toEqual('function');
expect(typeof axios.CancelToken).toEqual('function');
expect(typeof axios.isCancel).toEqual('function');
});
});
describe('instance api', function () {
+12
View File
@@ -0,0 +1,12 @@
var isCancel = require('../../../lib/cancel/isCancel');
var Cancel = require('../../../lib/cancel/Cancel');
describe('isCancel', function() {
it('returns true if value is a Cancel', function() {
expect(isCancel(new Cancel())).toBe(true);
});
it('returns false if value is not a Cancel', function() {
expect(isCancel({ foo: 'bar' })).toBe(false);
});
});
+1
View File
@@ -16,6 +16,7 @@ describe('instance', function () {
'create',
'Cancel',
'CancelToken',
'isCancel',
'all',
'spread',
'default'].indexOf(prop) > -1) {
+1 -1
View File
@@ -230,7 +230,7 @@ const source: CancelTokenSource = axios.CancelToken.source();
axios.get('/user', {
cancelToken: source.token
}).catch((thrown: AxiosError | Cancel) => {
if (thrown instanceof axios.Cancel) {
if (axios.isCancel(thrown)) {
const cancel: Cancel = thrown;
console.log(cancel.message);
}