wretch
Version:
A tiny wrapper built around fetch with an intuitive syntax.
74 lines • 2.23 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Adds the ability to abort requests using AbortController and signals under the hood.
*
* ```js
* import AbortAddon from "wretch/addons/abort"
*
* const [c, w] = wretch("...")
* .addon(AbortAddon())
* .get()
* .onAbort((_) => console.log("Aborted !"))
* .controller();
*
* w.text((_) => console.log("should never be called"));
* c.abort();
*
* // Or :
*
* const controller = new AbortController();
*
* wretch("...")
* .addon(AbortAddon())
* .signal(controller)
* .get()
* .onAbort((_) => console.log("Aborted !"))
* .text((_) => console.log("should never be called"));
*
* controller.abort();
* ```
*/
const abort = () => {
return {
beforeRequest(wretch, options, state) {
const fetchController = new AbortController();
if (!options["signal"]) {
options["signal"] = fetchController.signal;
}
const timeout = {
ref: null,
clear() {
if (timeout.ref) {
clearTimeout(timeout.ref);
timeout.ref = null;
}
}
};
state.abort = {
timeout,
fetchController
};
return wretch;
},
wretch: {
signal(controller) {
return { ...this, _options: { ...this._options, signal: controller.signal } };
},
},
resolver: {
setTimeout(time, options = {}) {
var _a;
const controller = (_a = options.controller) !== null && _a !== void 0 ? _a : this._sharedState.abort.fetchController;
const { timeout } = this._sharedState.abort;
timeout.clear();
timeout.ref = setTimeout(() => controller.abort(), time);
return this;
},
controller() { return [this._sharedState.abort.fetchController, this]; },
onAbort(cb) { return this.error("AbortError", cb); }
},
};
};
exports.default = abort;
//# sourceMappingURL=abort.js.map