wretch
Version:
A tiny wrapper built around fetch with an intuitive syntax.
146 lines • 5.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.core = void 0;
const utils_js_1 = require("./utils.js");
const constants_js_1 = require("./constants.js");
const resolver_js_1 = require("./resolver.js");
exports.core = {
_url: "",
_options: {},
_catchers: new Map(),
_resolvers: [],
_deferred: [],
_middlewares: [],
_addons: [],
addon(addon) {
const addons = Array.isArray(addon) ? addon : [addon];
const wretchProps = addons.reduce((acc, a) => ({ ...acc, ...a.wretch }), {});
return { ...this, _addons: [...this._addons, ...addons], ...wretchProps };
},
fetchPolyfill(fetchImpl) {
return { ...this, _fetch: fetchImpl };
},
url(_url, replace = false) {
if (replace)
return { ...this, _url };
const idx = this._url.indexOf("?");
return {
...this,
_url: idx > -1 ?
this._url.slice(0, idx) + _url + this._url.slice(idx) :
this._url + _url
};
},
options(options, replace = false) {
return { ...this, _options: replace ? options : (0, utils_js_1.mix)(this._options, options) };
},
headers(headerValues) {
const headers = !headerValues ? {} :
Array.isArray(headerValues) ? Object.fromEntries(headerValues) :
"entries" in headerValues ? Object.fromEntries(headerValues.entries()) :
headerValues;
return { ...this, _options: (0, utils_js_1.mix)(this._options, { headers }) };
},
accept(headerValue) {
return this.headers({ Accept: headerValue });
},
content(headerValue) {
return this.headers({ "Content-Type": headerValue });
},
auth(headerValue) {
return this.headers({ Authorization: headerValue });
},
catcher(ids, catcher) {
const newMap = new Map(this._catchers);
const errorIds = Array.isArray(ids) ? ids : [ids];
for (const errorId of errorIds) {
newMap.set(errorId, catcher);
}
return { ...this, _catchers: newMap };
},
catcherFallback(catcher) {
return this.catcher(constants_js_1.CATCHER_FALLBACK, catcher);
},
customError(transformer) {
return { ...this, _errorTransformer: transformer };
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resolve(resolver, clear = false) {
return { ...this, _resolvers: clear ? [resolver] : [...this._resolvers, resolver] };
},
defer(callback, clear = false) {
return {
...this,
_deferred: clear ? [callback] : [...this._deferred, callback]
};
},
middlewares(middlewares, clear = false) {
return {
...this,
_middlewares: clear ? middlewares : [...this._middlewares, ...middlewares]
};
},
fetch(method = this._options.method, url = "", body = null) {
let base = this.url(url).options({ method });
// "Jsonify" the body if it is an object and if it is likely that the content type targets json.
const contentType = (0, utils_js_1.extractContentType)(base._options.headers);
const jsonify = typeof body === "object" &&
!(body instanceof FormData) &&
(!base._options.headers || !contentType || (0, utils_js_1.isLikelyJsonMime)(contentType));
base =
!body ? base :
jsonify ? base.json(body, contentType) :
base.body(body);
return (0, resolver_js_1.resolver)(base
._deferred
.reduce((acc, curr) => curr(acc, acc._url, acc._options), base));
},
get(url = "") {
return this.fetch("GET", url);
},
delete(url = "") {
return this.fetch("DELETE", url);
},
put(body, url = "") {
return this.fetch("PUT", url, body);
},
post(body, url = "") {
return this.fetch("POST", url, body);
},
patch(body, url = "") {
return this.fetch("PATCH", url, body);
},
head(url = "") {
return this.fetch("HEAD", url);
},
opts(url = "") {
return this.fetch("OPTIONS", url);
},
body(contents) {
return { ...this, _options: { ...this._options, body: contents } };
},
json(jsObject, contentType) {
const currentContentType = (0, utils_js_1.extractContentType)(this._options.headers);
return this.content(contentType ||
(0, utils_js_1.isLikelyJsonMime)(currentContentType) && currentContentType ||
constants_js_1.JSON_MIME).body(JSON.stringify(jsObject));
},
toFetch() {
return (fetchUrl, fetchOptions = {}) => {
return this
.url(fetchUrl)
.options(fetchOptions)
.catcherFallback(error => {
if (error instanceof resolver_js_1.WretchError) {
return error.response;
}
else {
throw error;
}
})
.fetch()
.res();
};
}
};
//# sourceMappingURL=core.js.map