mirror of
https://github.com/tenrok/axios.git
synced 2026-06-20 20:00:40 +03:00
Adding axios.isCancel method
This commit is contained in:
@@ -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:
|
You can create a cancel token using the `CancelToken.source` factory as shown below:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
var Cancel = axios.Cancel;
|
|
||||||
var CancelToken = axios.CancelToken;
|
var CancelToken = axios.CancelToken;
|
||||||
var source = CancelToken.source();
|
var source = CancelToken.source();
|
||||||
|
|
||||||
axios.get('/user/12345', {
|
axios.get('/user/12345', {
|
||||||
cancelToken: source.token
|
cancelToken: source.token
|
||||||
}).catch(function(thrown) {
|
}).catch(function(thrown) {
|
||||||
if (thrown instanceof Cancel) {
|
if (axios.isCancel(thrown)) {
|
||||||
console.log('Request canceled', thrown.message);
|
console.log('Request canceled', thrown.message);
|
||||||
} else {
|
} else {
|
||||||
// handle error
|
// handle error
|
||||||
|
|||||||
Vendored
+2
-1
@@ -120,7 +120,8 @@ export interface AxiosStatic extends AxiosInstance {
|
|||||||
(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
(url: string, config?: AxiosRequestConfig): AxiosPromise;
|
||||||
create(config?: AxiosRequestConfig): AxiosInstance;
|
create(config?: AxiosRequestConfig): AxiosInstance;
|
||||||
Cancel: CancelStatic;
|
Cancel: CancelStatic;
|
||||||
CancelToken: CancelTokenStatic;
|
CancelToken: CancelTokenStatic;
|
||||||
|
isCancel(value: any): boolean;
|
||||||
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
|
all<T>(values: (T | Promise<T>)[]): Promise<T[]>;
|
||||||
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
|
spread<T, R>(callback: (...args: T[]) => R): (array: T[]) => R;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,7 @@ axios.create = function create(defaultConfig) {
|
|||||||
// Expose Cancel & CancelToken
|
// Expose Cancel & CancelToken
|
||||||
axios.Cancel = require('./cancel/Cancel');
|
axios.Cancel = require('./cancel/Cancel');
|
||||||
axios.CancelToken = require('./cancel/CancelToken');
|
axios.CancelToken = require('./cancel/CancelToken');
|
||||||
|
axios.isCancel = require('./cancel/isCancel');
|
||||||
|
|
||||||
// Expose all/spread
|
// Expose all/spread
|
||||||
axios.all = function all(promises) {
|
axios.all = function all(promises) {
|
||||||
|
|||||||
@@ -14,4 +14,6 @@ Cancel.prototype.toString = function toString() {
|
|||||||
return 'Cancel' + (this.message ? ': ' + this.message : '');
|
return 'Cancel' + (this.message ? ': ' + this.message : '');
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Cancel.prototype.__CANCEL__ = true;
|
||||||
|
|
||||||
module.exports = Cancel;
|
module.exports = Cancel;
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
module.exports = function isCancel(value) {
|
||||||
|
return !!(value && value.__CANCEL__);
|
||||||
|
};
|
||||||
@@ -34,6 +34,12 @@ describe('static api', function () {
|
|||||||
it('should have factory method', function () {
|
it('should have factory method', function () {
|
||||||
expect(typeof axios.create).toEqual('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 () {
|
describe('instance api', function () {
|
||||||
|
|||||||
@@ -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);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -16,6 +16,7 @@ describe('instance', function () {
|
|||||||
'create',
|
'create',
|
||||||
'Cancel',
|
'Cancel',
|
||||||
'CancelToken',
|
'CancelToken',
|
||||||
|
'isCancel',
|
||||||
'all',
|
'all',
|
||||||
'spread',
|
'spread',
|
||||||
'default'].indexOf(prop) > -1) {
|
'default'].indexOf(prop) > -1) {
|
||||||
|
|||||||
@@ -230,7 +230,7 @@ const source: CancelTokenSource = axios.CancelToken.source();
|
|||||||
axios.get('/user', {
|
axios.get('/user', {
|
||||||
cancelToken: source.token
|
cancelToken: source.token
|
||||||
}).catch((thrown: AxiosError | Cancel) => {
|
}).catch((thrown: AxiosError | Cancel) => {
|
||||||
if (thrown instanceof axios.Cancel) {
|
if (axios.isCancel(thrown)) {
|
||||||
const cancel: Cancel = thrown;
|
const cancel: Cancel = thrown;
|
||||||
console.log(cancel.message);
|
console.log(cancel.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user