mp-mini-axios
Version:
MiniAxios 是一个轻量级 HTTP 客户端库,专为【微信小程序】设计,提供类似 Axios 的 API 接口。它支持请求/响应拦截器、取消请求、自动重试、文件上传/下载等功能。构建后大小11KB,能有效节省小程序包大小。
52 lines (44 loc) • 964 B
JavaScript
// CancelToken.js
class Cancel {
constructor(message) {
this.message = message;
this.name = 'Cancel';
}
toString() {
return this.message ? `${this.name}: ${this.message}` : this.name;
}
}
Cancel.prototype.__CANCEL__ = true;
export class CancelToken {
constructor(executor) {
if (typeof executor !== 'function') {
throw new Error('executor must be a function');
}
let resolvePromise;
this.promise = new Promise(resolve => {
resolvePromise = resolve;
});
executor(message => {
if (this.reason) {
return;
}
this.reason = new Cancel(message);
resolvePromise(this.reason);
});
}
throwIfRequested() {
if (this.reason) {
throw this.reason;
}
}
static source() {
let cancel;
const token = new CancelToken(c => {
cancel = c;
});
return {
token,
cancel
};
}
}