From df967a4ffb1b866d4537c316be94222ae35fb489 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Fri, 8 Nov 2024 18:28:55 +0100 Subject: [PATCH] Bugfix --- client/fetch/index.js | 53 ++++++++++++++++++++++----------- package-lock.json | 4 +-- package-shared/types/index.d.ts | 7 +++-- package.json | 2 +- 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/client/fetch/index.js b/client/fetch/index.js index 948e787..fc1c006 100644 --- a/client/fetch/index.js +++ b/client/fetch/index.js @@ -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; diff --git a/package-lock.json b/package-lock.json index bb6dfa1..77d2e44 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package-shared/types/index.d.ts b/package-shared/types/index.d.ts index ebcfa70..baea3bd 100644 --- a/package-shared/types/index.d.ts +++ b/package-shared/types/index.d.ts @@ -1100,10 +1100,10 @@ export type CheckApiCredentialsFnParam = { export type FetchApiFn = ( url: string, options?: FetchApiOptions, - csrf?: boolean + contentType?: "json" | "text" | "html" | "blob" | "file" ) => Promise; -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 = { diff --git a/package.json b/package.json index a9ad36b..b3e018e 100644 --- a/package.json +++ b/package.json @@ -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": {