Bugfix
This commit is contained in:
parent
7bf1e3a0f9
commit
df967a4ffb
@ -1,27 +1,30 @@
|
|||||||
|
// @ts-check
|
||||||
|
|
||||||
|
const _ = require("lodash");
|
||||||
|
|
||||||
/** @type {import("../../package-shared/types").FetchApiFn} */
|
/** @type {import("../../package-shared/types").FetchApiFn} */
|
||||||
async function clientFetch() {
|
async function clientFetch(url, options, contentType) {
|
||||||
let data;
|
let data;
|
||||||
|
let finalUrl = url;
|
||||||
|
|
||||||
if (typeof options === "string") {
|
if (typeof options === "string") {
|
||||||
try {
|
try {
|
||||||
let fetchData;
|
let fetchData;
|
||||||
const csrfValue = localStorage.getItem("csrf");
|
|
||||||
|
|
||||||
switch (options) {
|
switch (options) {
|
||||||
case "post":
|
case "post":
|
||||||
fetchData = await fetch(url, {
|
fetchData = await fetch(finalUrl, {
|
||||||
method: options,
|
method: options,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"x-csrf-auth": csrf ? csrfValue : "",
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
data = fetchData.json();
|
data = await fetchData.json();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fetchData = await fetch(url);
|
fetchData = await fetch(finalUrl);
|
||||||
data = fetchData.json();
|
data = await fetchData.json();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
} catch (/** @type {any} */ error) {
|
} catch (/** @type {any} */ error) {
|
||||||
@ -32,7 +35,25 @@ async function clientFetch() {
|
|||||||
try {
|
try {
|
||||||
let fetchData;
|
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") {
|
if (options.body && typeof options.body === "object") {
|
||||||
let oldOptionsBody = _.cloneDeep(options.body);
|
let oldOptionsBody = _.cloneDeep(options.body);
|
||||||
@ -40,29 +61,27 @@ async function clientFetch() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (options.headers) {
|
if (options.headers) {
|
||||||
options.headers["x-csrf-auth"] = csrf ? csrfValue : "";
|
/** @type {any} */
|
||||||
|
|
||||||
const finalOptions = { ...options };
|
const finalOptions = { ...options };
|
||||||
fetchData = await fetch(url, finalOptions);
|
|
||||||
|
fetchData = await fetch(finalUrl, finalOptions);
|
||||||
} else {
|
} else {
|
||||||
fetchData = await fetch(url, {
|
fetchData = await fetch(finalUrl, {
|
||||||
...options,
|
...options,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"x-csrf-auth": csrf ? csrfValue : "",
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
data = await fetchData.json();
|
||||||
data = fetchData.json();
|
|
||||||
} catch (/** @type {any} */ error) {
|
} catch (/** @type {any} */ error) {
|
||||||
console.log("FetchAPI error #2:", error.message);
|
console.log("FetchAPI error #2:", error.message);
|
||||||
data = null;
|
data = null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
let fetchData = await fetch(url);
|
let fetchData = await fetch(finalUrl);
|
||||||
data = fetchData.json();
|
data = await fetchData.json();
|
||||||
} catch (/** @type {any} */ error) {
|
} catch (/** @type {any} */ error) {
|
||||||
console.log("FetchAPI error #3:", error.message);
|
console.log("FetchAPI error #3:", error.message);
|
||||||
data = null;
|
data = null;
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -1,12 +1,12 @@
|
|||||||
{
|
{
|
||||||
"name": "datasquirel",
|
"name": "datasquirel",
|
||||||
"version": "2.3.5",
|
"version": "2.4.6",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "datasquirel",
|
"name": "datasquirel",
|
||||||
"version": "2.3.5",
|
"version": "2.4.6",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/ace": "^0.0.52",
|
"@types/ace": "^0.0.52",
|
||||||
|
7
package-shared/types/index.d.ts
vendored
7
package-shared/types/index.d.ts
vendored
@ -1100,10 +1100,10 @@ export type CheckApiCredentialsFnParam = {
|
|||||||
export type FetchApiFn = (
|
export type FetchApiFn = (
|
||||||
url: string,
|
url: string,
|
||||||
options?: FetchApiOptions,
|
options?: FetchApiOptions,
|
||||||
csrf?: boolean
|
contentType?: "json" | "text" | "html" | "blob" | "file"
|
||||||
) => Promise<any>;
|
) => Promise<any>;
|
||||||
|
|
||||||
type FetchApiOptions = {
|
export type FetchApiOptions = RequestInit & {
|
||||||
method:
|
method:
|
||||||
| "POST"
|
| "POST"
|
||||||
| "GET"
|
| "GET"
|
||||||
@ -1117,12 +1117,15 @@ type FetchApiOptions = {
|
|||||||
| "patch";
|
| "patch";
|
||||||
body?: object | string;
|
body?: object | string;
|
||||||
headers?: FetchHeader;
|
headers?: FetchHeader;
|
||||||
|
query?: { [key: string]: any };
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AuthCsrfHeaderName = "x-csrf-auth";
|
export type AuthCsrfHeaderName = "x-csrf-auth";
|
||||||
|
|
||||||
type FetchHeader = HeadersInit & {
|
type FetchHeader = HeadersInit & {
|
||||||
[key in AuthCsrfHeaderName]?: string | null;
|
[key in AuthCsrfHeaderName]?: string | null;
|
||||||
|
} & {
|
||||||
|
[key: string]: any;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type FetchApiReturn = {
|
export type FetchApiReturn = {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "datasquirel",
|
"name": "datasquirel",
|
||||||
"version": "2.4.6",
|
"version": "2.4.7",
|
||||||
"description": "Cloud-based SQL data management tool",
|
"description": "Cloud-based SQL data management tool",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
|
Loading…
Reference in New Issue
Block a user