This commit is contained in:
Benjamin Toby 2026-02-11 05:05:32 +01:00
parent 42caaecb18
commit 7345010391
5 changed files with 30 additions and 10 deletions

View File

@ -3,13 +3,14 @@ import {
DSQLClientFetchHeader,
DSQLFetchApiOptions,
} from "../../package-shared/types";
import serializeQuery from "../../package-shared/utils/serialize-query";
/**
* # Fetch API
*/
export default async function fetchApi<
T extends { [k: string]: any } = { [k: string]: any },
R extends any = any
R extends any = any,
>(url: string, options?: DSQLFetchApiOptions<T>): Promise<R> {
let data;
@ -21,13 +22,19 @@ export default async function fetchApi<
finalHeaders[options.csrfKey] = options.csrfValue;
}
let finalURL = url;
if (options?.query) {
finalURL += serializeQuery(options.query);
}
if (typeof options === "string") {
try {
let fetchData;
switch (options) {
case "post":
fetchData = await fetch(url, {
fetchData = await fetch(finalURL, {
method: options,
headers: finalHeaders,
} as RequestInit);
@ -35,7 +42,7 @@ export default async function fetchApi<
break;
default:
fetchData = await fetch(url);
fetchData = await fetch(finalURL);
data = fetchData.json();
break;
}
@ -60,7 +67,7 @@ export default async function fetchApi<
fetchOptions = _.merge(fetchOptions, options.fetchOptions);
fetchData = await fetch(url, fetchOptions);
fetchData = await fetch(finalURL, fetchOptions);
data = fetchData.json();
} catch (error: any) {
console.log("FetchAPI error #2:", error.message);
@ -68,7 +75,7 @@ export default async function fetchApi<
}
} else {
try {
let fetchData = await fetch(url);
let fetchData = await fetch(finalURL);
data = await fetchData.json();
} catch (error: any) {
console.log("FetchAPI error #3:", error.message);

View File

@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = fetchApi;
const lodash_1 = __importDefault(require("lodash"));
const serialize_query_1 = __importDefault(require("../../package-shared/utils/serialize-query"));
/**
* # Fetch API
*/
@ -26,19 +27,23 @@ function fetchApi(url, options) {
if ((options === null || options === void 0 ? void 0 : options.csrfKey) && options.csrfValue) {
finalHeaders[options.csrfKey] = options.csrfValue;
}
let finalURL = url;
if (options === null || options === void 0 ? void 0 : options.query) {
finalURL += (0, serialize_query_1.default)(options.query);
}
if (typeof options === "string") {
try {
let fetchData;
switch (options) {
case "post":
fetchData = yield fetch(url, {
fetchData = yield fetch(finalURL, {
method: options,
headers: finalHeaders,
});
data = fetchData.json();
break;
default:
fetchData = yield fetch(url);
fetchData = yield fetch(finalURL);
data = fetchData.json();
break;
}
@ -60,7 +65,7 @@ function fetchApi(url, options) {
}
fetchOptions.headers = lodash_1.default.merge(finalHeaders, options.headers || {});
fetchOptions = lodash_1.default.merge(fetchOptions, options.fetchOptions);
fetchData = yield fetch(url, fetchOptions);
fetchData = yield fetch(finalURL, fetchOptions);
data = fetchData.json();
}
catch (error) {
@ -70,7 +75,7 @@ function fetchApi(url, options) {
}
else {
try {
let fetchData = yield fetch(url);
let fetchData = yield fetch(finalURL);
data = yield fetchData.json();
}
catch (error) {

View File

@ -60,6 +60,7 @@ export interface DSQL_TableSchemaType {
tableNameOld?: string;
childTableDbId?: string | number;
collation?: (typeof MariaDBCollations)[number];
isVector?: boolean;
}
export interface DSQL_ChildrenTablesType {
tableId?: string | number;
@ -127,6 +128,8 @@ export type DSQL_FieldSchemaType = {
moving?: boolean;
code?: boolean;
options?: string[];
isVector?: boolean;
vectorSize?: number;
} & {
[key in (typeof TextFieldTypesArray)[number]["value"]]?: boolean;
};
@ -2381,6 +2384,7 @@ export type DSQLFetchApiOptions<T extends {
csrfValue?: string;
csrfKey?: string;
fetchOptions?: RequestInit;
query?: T;
};
export type AddDbEntryParam<T extends {
[k: string]: any;

View File

@ -107,6 +107,7 @@ export interface DSQL_TableSchemaType {
tableNameOld?: string;
childTableDbId?: string | number;
collation?: (typeof MariaDBCollations)[number];
isVector?: boolean;
}
export interface DSQL_ChildrenTablesType {
@ -158,6 +159,8 @@ export type DSQL_FieldSchemaType = {
moving?: boolean;
code?: boolean;
options?: string[];
isVector?: boolean;
vectorSize?: number;
} & {
[key in (typeof TextFieldTypesArray)[number]["value"]]?: boolean;
};
@ -3069,6 +3072,7 @@ export type DSQLFetchApiOptions<
csrfValue?: string;
csrfKey?: string;
fetchOptions?: RequestInit;
query?: T;
};
export type AddDbEntryParam<

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/datasquirel",
"version": "5.7.34",
"version": "5.7.36",
"description": "Cloud-based SQL data management tool",
"main": "dist/index.js",
"bin": {