Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const handleCancellation = (ctx: MiddlewareContext, cancellation: Cancellation) => {
let cancellable = true
return {
cancelToken: new axios.CancelToken((canceller) => {
cancellation.source.token.promise.then(cancel => {
if (cancellable) {
canceller(cancel.message)
}
})
}),
onRequestFinish: () => {
if (!ctx.config.cancelToken) {
// don't have cancelToken: not cancelable
cancellable = false
} else if (!ctx.response) {
// response is not ready: cancelable
cancellable = true
} else if (ctx.config.responseType !== 'stream') {
// response is ready and it is not a stream: not cancelable
cancellable = false
function getCancelToken() {
let cancelExcutor
const cancelToken = new Axios.CancelToken((excutor) => {
cancelExcutor = excutor
})
return {
cancelToken,
cancelExcutor
}
}
function fetch(method, url, data, inConfig = false) {
let cancel = () => {};
const cancelToken = new Axios.CancelToken((c) => {
cancel = c;
});
const promise = (inConfig
? Request[method](url, {
params: data,
cancelToken
})
: Request[method](url, data, {
cancelToken
})
);
return {
promise,
cancel
async getArticleReactions(dataInfo) {
this.getReactionsCancel()
const { data } = await Axios({
url: `${dataInfo.url}/reactions?time=${Date.now()}`,
cancelToken: new CancelToken((c) => {
this.getReactionsCancel = c;
}),
headers: {
'Accept': 'application/vnd.github.squirrel-girl-preview+json'
}
})
this.setState({ reactions: data || [] })
}
async getArticleComments(dataInfo) {
followRequests = () => {
followRequests(mobx.domain, {
cancelToken: new CancelToken(c => (this.cancel = c))
})
.then(res => {
this.setState({
list: this.state.list.concat(res),
loading: false
})
})
.catch(() => {
this.setState({
loading: false
})
})
}
function getCancelToken () {
let cancelExcutor
const cancelToken = new axios.CancelToken(excutor => {
cancelExcutor = excutor
})
return {
cancelToken,
cancelExcutor
}
}
function getCancelToken () {
let cancelExcutor
const cancelToken = new axios.CancelToken(excutor => {
cancelExcutor = excutor
})
return {
cancelToken,
cancelExcutor
}
}
protected async request(config: AxiosRequestConfig, canceler?: Canceler): Promise {
if (canceler) {
config.cancelToken = new axios.CancelToken(function executor(c) {
canceler.setExecutor(c);
});
}
try {
let res = await this.axios.request(config);
return res.data;
} catch (e) {
if (!axios.isCancel(e)) {
throw e;
}
}
return null;
}