124 lines
4.4 KiB
JavaScript
124 lines
4.4 KiB
JavaScript
"use strict";
|
|
exports.id = 6729;
|
|
exports.ids = [6729];
|
|
exports.modules = {
|
|
|
|
/***/ 6729:
|
|
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
|
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
/* harmony export */ "Z": () => (/* binding */ fetchApi)
|
|
/* harmony export */ });
|
|
// @ts-check
|
|
/**
|
|
* ==============================================================================
|
|
* Fetch Function
|
|
* ==============================================================================
|
|
* @async
|
|
*
|
|
* @param {string} url - Admin or Site page
|
|
* @param {{
|
|
* method: "POST" | "GET" | "DELETE" | "PUT" | "PATCH" | "post" | "get" | "delete" | "put" | "patch",
|
|
* body: object | string,
|
|
* headers?: HeadersInit,
|
|
* } | string} [options] - options object or string: **optional
|
|
* @param {boolean} [csrf] - Add CSRF?
|
|
*
|
|
* @returns {Promise<*>}
|
|
*/ async function fetchApi(url, options, csrf) {
|
|
/** ********************* Initialize data variable */ let data;
|
|
const finalUrl = url.match(/\?/) ? url : url + window.location.search;
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
if (typeof options === "string") {
|
|
try {
|
|
let fetchData;
|
|
switch(options){
|
|
case "post":
|
|
fetchData = await fetch(finalUrl, {
|
|
method: options,
|
|
// @ts-ignore
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"x-csrf-auth": csrf ? localStorage.getItem("csrf") : ""
|
|
}
|
|
});
|
|
data = fetchData.json();
|
|
break;
|
|
default:
|
|
fetchData = await fetch(finalUrl);
|
|
data = fetchData.json();
|
|
break;
|
|
}
|
|
} catch (error) {
|
|
data = null;
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} else if (typeof options === "object") {
|
|
try {
|
|
let fetchData1;
|
|
/** ********************* Convert body to JSON if not JSON */ if (options.body && typeof options.body === "object") {
|
|
let oldOptionsBody = options.body;
|
|
options.body = JSON.stringify(oldOptionsBody);
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
if (options.headers) {
|
|
////////////////////////////////////////
|
|
// @ts-ignore
|
|
options.headers["x-csrf-auth"] = csrf ? localStorage.getItem("csrf") : "";
|
|
/** @type {any} */ const finalOptions = {
|
|
...options
|
|
};
|
|
fetchData1 = await fetch(finalUrl, finalOptions);
|
|
////////////////////////////////////////
|
|
} else {
|
|
fetchData1 = await fetch(finalUrl, {
|
|
...options,
|
|
// @ts-ignore
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
"x-csrf-auth": csrf ? localStorage.getItem("csrf") : ""
|
|
}
|
|
});
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
data = fetchData1.json();
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} catch (error1) {
|
|
data = null;
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} else {
|
|
try {
|
|
let fetchData2 = await fetch(finalUrl);
|
|
data = fetchData2.json();
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} catch (error2) {
|
|
data = null;
|
|
}
|
|
}
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
return data;
|
|
}
|
|
var FETCH = fetchApi;
|
|
|
|
|
|
/***/ })
|
|
|
|
};
|
|
; |