This commit is contained in:
Benjamin Toby 2024-11-08 18:28:55 +01:00
parent 7bf1e3a0f9
commit df967a4ffb
4 changed files with 44 additions and 22 deletions

View File

@ -1,27 +1,30 @@
// @ts-check
const _ = require("lodash");
/** @type {import("../../package-shared/types").FetchApiFn} */
async function clientFetch() {
async function clientFetch(url, options, contentType) {
let data;
let finalUrl = url;
if (typeof options === "string") {
try {
let fetchData;
const csrfValue = localStorage.getItem("csrf");
switch (options) {
case "post":
fetchData = await fetch(url, {
fetchData = await fetch(finalUrl, {
method: options,
headers: {
"Content-Type": "application/json",
"x-csrf-auth": csrf ? csrfValue : "",
},
});
data = fetchData.json();
data = await fetchData.json();
break;
default:
fetchData = await fetch(url);
data = fetchData.json();
fetchData = await fetch(finalUrl);
data = await fetchData.json();
break;
}
} catch (/** @type {any} */ error) {
@ -32,7 +35,25 @@ async function clientFetch() {
try {
let fetchData;
const csrfValue = localStorage.getItem("csrf");
if (options.query) {
let pathSuffix = "";
pathSuffix += "?";
const queryString = Object.keys(options.query)
?.map((queryKey) => {
if (!options.query?.[queryKey]) return undefined;
if (typeof options.query[queryKey] == "object") {
return `${queryKey}=${JSON.stringify(
options.query[queryKey]
)}`;
}
return `${queryKey}=${options.query[queryKey]}`;
})
.filter((prt) => prt)
.join("&");
pathSuffix += queryString;
finalUrl += pathSuffix;
delete options.query;
}
if (options.body && typeof options.body === "object") {
let oldOptionsBody = _.cloneDeep(options.body);
@ -40,29 +61,27 @@ async function clientFetch() {
}
if (options.headers) {
options.headers["x-csrf-auth"] = csrf ? csrfValue : "";
/** @type {any} */
const finalOptions = { ...options };
fetchData = await fetch(url, finalOptions);
fetchData = await fetch(finalUrl, finalOptions);
} else {
fetchData = await fetch(url, {
fetchData = await fetch(finalUrl, {
...options,
headers: {
"Content-Type": "application/json",
"x-csrf-auth": csrf ? csrfValue : "",
},
});
}
data = fetchData.json();
data = await 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();
let fetchData = await fetch(finalUrl);
data = await fetchData.json();
} catch (/** @type {any} */ error) {
console.log("FetchAPI error #3:", error.message);
data = null;

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{
"name": "datasquirel",
"version": "2.3.5",
"version": "2.4.6",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "datasquirel",
"version": "2.3.5",
"version": "2.4.6",
"license": "ISC",
"dependencies": {
"@types/ace": "^0.0.52",

View File

@ -1100,10 +1100,10 @@ export type CheckApiCredentialsFnParam = {
export type FetchApiFn = (
url: string,
options?: FetchApiOptions,
csrf?: boolean
contentType?: "json" | "text" | "html" | "blob" | "file"
) => Promise<any>;
type FetchApiOptions = {
export type FetchApiOptions = RequestInit & {
method:
| "POST"
| "GET"
@ -1117,12 +1117,15 @@ type FetchApiOptions = {
| "patch";
body?: object | string;
headers?: FetchHeader;
query?: { [key: string]: any };
};
export type AuthCsrfHeaderName = "x-csrf-auth";
type FetchHeader = HeadersInit & {
[key in AuthCsrfHeaderName]?: string | null;
} & {
[key: string]: any;
};
export type FetchApiReturn = {

View File

@ -1,6 +1,6 @@
{
"name": "datasquirel",
"version": "2.4.6",
"version": "2.4.7",
"description": "Cloud-based SQL data management tool",
"main": "index.js",
"bin": {