geostyler
Version:
Framework for styling geodata
46 lines (45 loc) • 1.53 kB
JavaScript
import m from "@ungap/url-search-params";
class f {
/**
* A method that performs a fetch request with some predefined configs.
*
* @param {Object} Options The options object to configure the post request.
* It can contain these keys:
* {String} url The url we want to send the post to.
* {Object} params The post params we want to send.
* Default is {}.
* {Object} additionalHeaders An object with additional headers as kvp.
* Default is {}.
* {Object} additionalFetchOptions An object containing additional options
* for the fetch API. Compare https://mdn.io/fetch Default is {}.
* {Boolean} sameOriginCredentials A flag to control whether the credentials
* option to 'same-origin' will be set or left undefined. Default is true.
* {Boolean} asForm A flag to set the Content-Type header to
* 'application/x-www-form-urlencoded'. Default is true.
*/
static async post(r) {
const {
url: n,
params: t = {},
additionalHeaders: a = {},
additionalFetchOptions: i = {},
sameOriginCredentials: c = !0,
asForm: s = !0
} = r, e = new Headers();
s && e.set("Content-Type", "application/x-www-form-urlencoded"), Object.keys(a).forEach((o) => {
const l = a[o];
e.set(o, l);
});
const d = {
method: "POST",
credentials: c ? "same-origin" : void 0,
body: s ? new m(t) : t,
headers: e,
...i
};
return fetch(n, d);
}
}
export {
f as default
};