From 216e2a6787fa836d0aeb0b000c2849ba88e2f462 Mon Sep 17 00:00:00 2001 From: Nick Uraltsev Date: Wed, 21 Sep 2016 18:47:37 -0700 Subject: [PATCH] Adding axios.isCancel method --- README.md | 3 +-- axios.d.ts | 3 ++- lib/axios.js | 1 + lib/cancel/Cancel.js | 2 ++ lib/cancel/isCancel.js | 5 +++++ test/specs/api.spec.js | 6 ++++++ test/specs/cancel/isCancel.spec.js | 12 ++++++++++++ test/specs/instance.spec.js | 1 + test/typescript/axios.ts | 2 +- 9 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 lib/cancel/isCancel.js create mode 100644 test/specs/cancel/isCancel.spec.js diff --git a/README.md b/README.md index 862e48a..c325338 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/axios.d.ts b/axios.d.ts index 88d283a..03c7f1b 100644 --- a/axios.d.ts +++ b/axios.d.ts @@ -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(values: (T | Promise)[]): Promise; spread(callback: (...args: T[]) => R): (array: T[]) => R; } diff --git a/lib/axios.js b/lib/axios.js index 3fc1f32..aa741f0 100644 --- a/lib/axios.js +++ b/lib/axios.js @@ -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) { diff --git a/lib/cancel/Cancel.js b/lib/cancel/Cancel.js index 99d01ff..e0de400 100644 --- a/lib/cancel/Cancel.js +++ b/lib/cancel/Cancel.js @@ -14,4 +14,6 @@ Cancel.prototype.toString = function toString() { return 'Cancel' + (this.message ? ': ' + this.message : ''); }; +Cancel.prototype.__CANCEL__ = true; + module.exports = Cancel; diff --git a/lib/cancel/isCancel.js b/lib/cancel/isCancel.js new file mode 100644 index 0000000..051f3ae --- /dev/null +++ b/lib/cancel/isCancel.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = function isCancel(value) { + return !!(value && value.__CANCEL__); +}; diff --git a/test/specs/api.spec.js b/test/specs/api.spec.js index 937f124..e177d07 100644 --- a/test/specs/api.spec.js +++ b/test/specs/api.spec.js @@ -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 () { diff --git a/test/specs/cancel/isCancel.spec.js b/test/specs/cancel/isCancel.spec.js new file mode 100644 index 0000000..e6be40d --- /dev/null +++ b/test/specs/cancel/isCancel.spec.js @@ -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); + }); +}); diff --git a/test/specs/instance.spec.js b/test/specs/instance.spec.js index be34230..fe395f3 100644 --- a/test/specs/instance.spec.js +++ b/test/specs/instance.spec.js @@ -16,6 +16,7 @@ describe('instance', function () { 'create', 'Cancel', 'CancelToken', + 'isCancel', 'all', 'spread', 'default'].indexOf(prop) > -1) { diff --git a/test/typescript/axios.ts b/test/typescript/axios.ts index 39571a9..d3d3198 100644 --- a/test/typescript/axios.ts +++ b/test/typescript/axios.ts @@ -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); }