-
Notifications
You must be signed in to change notification settings - Fork 244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add CancelToken
and isCancel
to axios instance
#292
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #292 +/- ##
===================================
Coverage 100% 100%
===================================
Files 1 1
Lines 28 28
Branches 13 13
===================================
Hits 28 28 Continue to review full report at Codecov.
|
Please make this happen. |
The plugin (generated) by axios module (@nuxt/axios) is inside |
@pi0 would that help ? it's quoted from axios docs |
docs/usage.md
Outdated
|
||
```js | ||
const CancelToken = this.$axios.CancelToken; | ||
const source = CancelToken.source(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we just use it once:
const source = CancelToken.source(); | |
const source = thus.$axios.CancelToken.source(); |
docs/usage.md
Outdated
|
||
this.$axios.$get('/user/12345', { | ||
cancelToken: source.token | ||
}).catch(function (thrown) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
}).catch(function (thrown) { | |
}).catch((error) => { |
docs/usage.md
Outdated
cancelToken: source.token | ||
}).catch(function (thrown) { | ||
if (this.$axios.isCancel(thrown)) { | ||
console.log('Request canceled', thrown.message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log('Request canceled', thrown.message); | |
console.log('Request canceled', error.message); |
docs/usage.md
Outdated
You can create a cancel token using the `CancelToken.source` factory as shown below: | ||
|
||
```js | ||
const CancelToken = this.$axios.CancelToken; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const CancelToken = this.$axios.CancelToken; |
docs/usage.md
Outdated
cancelToken: source.token | ||
}).catch(function (thrown) { | ||
if (this.$axios.isCancel(thrown)) { | ||
console.log('Request canceled', thrown.message); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
console.log('Request canceled', thrown.message); | |
console.error('Request canceled:', error); |
docs/usage.md
Outdated
You can also create a cancel token by passing an executor function to the `CancelToken` constructor: | ||
|
||
```js | ||
const CancelToken = this.$axios.CancelToken; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const CancelToken = this.$axios.CancelToken; | |
const { CancelToken } = this.$axios; |
docs/usage.md
Outdated
let cancel; | ||
|
||
this.$axios.$get('/user/12345', { | ||
cancelToken: new CancelToken(function executor(c) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we simply instantiate CancelToken outside?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are the examples in the axios docs.
Personally I'd leave it as is and state that you're quoting rather than treating these doc as part of this project.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK :) But i would suggest update function to arrow
Thanks @Amrmak . Just left some minor comments. |
Thanks @pi0 for the feedback. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Amrmak minor comments
Please remove all ;
docs/usage.md
Outdated
You can create a cancel token using the `CancelToken.source` factory as shown below: | ||
|
||
```js | ||
const { CancelToken } = this.$axios; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need this line
docs/usage.md
Outdated
} else { | ||
// handle error | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting:
this.$axios.$get('/user/12345', {
cancelToken: source.token
}).catch(error => {
if (this.$axios.isCancel(error)) {
console.log('Request canceled', error)
} else {
// handle error
}
})
docs/usage.md
Outdated
{ | ||
cancelToken: source.token, | ||
}, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Formatting
this.$axios.$post('/user/12345', {
name: 'new name'
}, {
cancelToken: source.token
})
Please don't. The no- semi-colon movement is going to make it impossible to optimise JITs someday soon. Python is a whitespace-organised language. JavaScript isn't and it's going to cause trouble trying to make it one. |
@SaulIO We are following standardJS with eslint-config and also docs and examples so it is appreciated to keep it consistent. But of course, it is 100% optional. PS: I promise no perf advantages are there when code is compiled/transpiled :) |
Sad to see the StandardJS movement adding another victim. :P As I understand it, ambiguity in syntax makes JITing harder and consistency makes it easier, and global consistencies allow global optimisations: See discussion a here re Dart's choices: dart-lang/sdk#30347 (comment) But OK, I didn't realise it was project standards, even if I wish you'd gone the other way. |
(off-topic) @SaulIO JIT executes the final bundle. No matters how we write original code (with or without semi), webpack, babel and terser transform it to the same output :) |
(continuing off-topic) Right, but shouldn't we have a "run anywhere" standard for portability? Ie, we'd want it to behave the same in tooled vs untooled environments. |
docs/usage.md
Outdated
this.$axios.$post('/user/12345', { | ||
name: 'new name' | ||
}, { | ||
cancelToken: source.token |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix indentation
lib/plugin.js
Outdated
@@ -186,6 +186,8 @@ export default (ctx, inject) => { | |||
|
|||
// Create new axios instance | |||
const axios = Axios.create(axiosOptions) | |||
axios.CancelToken = Axios.CancelToken; | |||
axios.isCancel = Axios.isCancel; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove ;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work ❤️
CancelToken
and isCancel
to axios instance
Providing a solution for #271 by adding CancelToken and isCancel to the exported axios instance.
Reference: axios/axios#1330 (comment)