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, DSQLClientFetchHeader,
DSQLFetchApiOptions, DSQLFetchApiOptions,
} from "../../package-shared/types"; } from "../../package-shared/types";
import serializeQuery from "../../package-shared/utils/serialize-query";
/** /**
* # Fetch API * # Fetch API
*/ */
export default async function fetchApi< export default async function fetchApi<
T extends { [k: string]: any } = { [k: string]: any }, T extends { [k: string]: any } = { [k: string]: any },
R extends any = any R extends any = any,
>(url: string, options?: DSQLFetchApiOptions<T>): Promise<R> { >(url: string, options?: DSQLFetchApiOptions<T>): Promise<R> {
let data; let data;
@ -21,13 +22,19 @@ export default async function fetchApi<
finalHeaders[options.csrfKey] = options.csrfValue; finalHeaders[options.csrfKey] = options.csrfValue;
} }
let finalURL = url;
if (options?.query) {
finalURL += serializeQuery(options.query);
}
if (typeof options === "string") { if (typeof options === "string") {
try { try {
let fetchData; let fetchData;
switch (options) { switch (options) {
case "post": case "post":
fetchData = await fetch(url, { fetchData = await fetch(finalURL, {
method: options, method: options,
headers: finalHeaders, headers: finalHeaders,
} as RequestInit); } as RequestInit);
@ -35,7 +42,7 @@ export default async function fetchApi<
break; break;
default: default:
fetchData = await fetch(url); fetchData = await fetch(finalURL);
data = fetchData.json(); data = fetchData.json();
break; break;
} }
@ -60,7 +67,7 @@ export default async function fetchApi<
fetchOptions = _.merge(fetchOptions, options.fetchOptions); fetchOptions = _.merge(fetchOptions, options.fetchOptions);
fetchData = await fetch(url, fetchOptions); fetchData = await fetch(finalURL, fetchOptions);
data = fetchData.json(); data = fetchData.json();
} catch (error: any) { } catch (error: any) {
console.log("FetchAPI error #2:", error.message); console.log("FetchAPI error #2:", error.message);
@ -68,7 +75,7 @@ export default async function fetchApi<
} }
} else { } else {
try { try {
let fetchData = await fetch(url); let fetchData = await fetch(finalURL);
data = await fetchData.json(); data = await fetchData.json();
} catch (error: any) { } catch (error: any) {
console.log("FetchAPI error #3:", error.message); 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 }); Object.defineProperty(exports, "__esModule", { value: true });
exports.default = fetchApi; exports.default = fetchApi;
const lodash_1 = __importDefault(require("lodash")); const lodash_1 = __importDefault(require("lodash"));
const serialize_query_1 = __importDefault(require("../../package-shared/utils/serialize-query"));
/** /**
* # Fetch API * # Fetch API
*/ */
@ -26,19 +27,23 @@ function fetchApi(url, options) {
if ((options === null || options === void 0 ? void 0 : options.csrfKey) && options.csrfValue) { if ((options === null || options === void 0 ? void 0 : options.csrfKey) && options.csrfValue) {
finalHeaders[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") { if (typeof options === "string") {
try { try {
let fetchData; let fetchData;
switch (options) { switch (options) {
case "post": case "post":
fetchData = yield fetch(url, { fetchData = yield fetch(finalURL, {
method: options, method: options,
headers: finalHeaders, headers: finalHeaders,
}); });
data = fetchData.json(); data = fetchData.json();
break; break;
default: default:
fetchData = yield fetch(url); fetchData = yield fetch(finalURL);
data = fetchData.json(); data = fetchData.json();
break; break;
} }
@ -60,7 +65,7 @@ function fetchApi(url, options) {
} }
fetchOptions.headers = lodash_1.default.merge(finalHeaders, options.headers || {}); fetchOptions.headers = lodash_1.default.merge(finalHeaders, options.headers || {});
fetchOptions = lodash_1.default.merge(fetchOptions, options.fetchOptions); fetchOptions = lodash_1.default.merge(fetchOptions, options.fetchOptions);
fetchData = yield fetch(url, fetchOptions); fetchData = yield fetch(finalURL, fetchOptions);
data = fetchData.json(); data = fetchData.json();
} }
catch (error) { catch (error) {
@ -70,7 +75,7 @@ function fetchApi(url, options) {
} }
else { else {
try { try {
let fetchData = yield fetch(url); let fetchData = yield fetch(finalURL);
data = yield fetchData.json(); data = yield fetchData.json();
} }
catch (error) { catch (error) {

View File

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

View File

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

View File

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