Update Types

This commit is contained in:
Benjamin Toby
2024-11-08 16:41:22 +01:00
parent 2ee812fef5
commit afd50caf42
7 changed files with 175 additions and 11 deletions
+76
View File
@@ -0,0 +1,76 @@
/** @type {import("../../package-shared/types").FetchApiFn} */
async function clientFetch() {
let data;
if (typeof options === "string") {
try {
let fetchData;
const csrfValue = localStorage.getItem("csrf");
switch (options) {
case "post":
fetchData = await fetch(url, {
method: options,
headers: {
"Content-Type": "application/json",
"x-csrf-auth": csrf ? csrfValue : "",
},
});
data = fetchData.json();
break;
default:
fetchData = await fetch(url);
data = fetchData.json();
break;
}
} catch (/** @type {any} */ error) {
console.log("FetchAPI error #1:", error.message);
data = null;
}
} else if (typeof options === "object") {
try {
let fetchData;
const csrfValue = localStorage.getItem("csrf");
if (options.body && typeof options.body === "object") {
let oldOptionsBody = _.cloneDeep(options.body);
options.body = JSON.stringify(oldOptionsBody);
}
if (options.headers) {
options.headers["x-csrf-auth"] = csrf ? csrfValue : "";
const finalOptions = { ...options };
fetchData = await fetch(url, finalOptions);
} else {
fetchData = await fetch(url, {
...options,
headers: {
"Content-Type": "application/json",
"x-csrf-auth": csrf ? csrfValue : "",
},
});
}
data = fetchData.json();
} catch (/** @type {any} */ error) {
console.log("FetchAPI error #2:", error.message);
data = null;
}
} else {
try {
let fetchData = await fetch(url);
data = fetchData.json();
} catch (/** @type {any} */ error) {
console.log("FetchAPI error #3:", error.message);
data = null;
}
}
return data;
}
module.exports = clientFetch;
exports.fetchApi = clientFetch;
+11 -4
View File
@@ -9,6 +9,8 @@ const inputFileToBase64 = require("./media/inputFileToBase64");
const getAccessToken = require("./auth/google/getAccessToken");
const getGithubAccessToken = require("./auth/github/getAccessToken");
const logout = require("./auth/logout");
const { fetchApi } = require("./fetch");
const clientFetch = require("./fetch");
////////////////////////////////////////
////////////////////////////////////////
@@ -37,11 +39,16 @@ const auth = {
};
/**
* Main Export
* Fetch
*/
const datasquirelClient = {
media: media,
auth: auth,
const fetch = {
fetchApi,
clientFetch,
};
/**
* Main Export
*/
const datasquirelClient = { media, auth, fetch };
module.exports = datasquirelClient;