This commit is contained in:
Benjamin Toby 2026-01-01 09:22:51 +01:00
parent b8e5d45762
commit 8277ff3e41
8 changed files with 31 additions and 35 deletions

View File

@ -19,6 +19,7 @@ export default async function clientCrudFetch<
method = "GET", method = "GET",
apiOrigin, apiOrigin,
headers, headers,
...options
}: ClientCrudFetchParams<T, P>) { }: ClientCrudFetchParams<T, P>) {
try { try {
let pathname = basePath || ``; let pathname = basePath || ``;
@ -46,6 +47,7 @@ export default async function clientCrudFetch<
method, method,
body, body,
headers, headers,
...options,
}); });
return res; return res;

View File

@ -46,26 +46,15 @@ export default async function fetchApi<
} else if (typeof options === "object") { } else if (typeof options === "object") {
try { try {
let fetchData; let fetchData;
let fetchOptions: RequestInit = {};
if (options.body && typeof options.body === "object") { if (options.body && typeof options.body === "object") {
let oldOptionsBody = _.cloneDeep(options.body); fetchOptions.body = JSON.stringify(options.body);
options.body = JSON.stringify(oldOptionsBody);
} }
if (options.headers) { fetchOptions.headers = _.merge(finalHeaders, options.headers || {});
options.headers = _.merge(options.headers, finalHeaders);
const finalOptions: any = { ...options };
fetchData = await fetch(url, finalOptions);
} else {
const finalOptions = {
...options,
headers: finalHeaders,
} as RequestInit;
fetchData = await fetch(url, finalOptions);
}
fetchData = await fetch(url, 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);

View File

@ -7,4 +7,4 @@ export default function clientCrudFetch<T extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}>({ table, basePath, body, query, targetId, method, apiOrigin, headers, }: ClientCrudFetchParams<T, P>): Promise<APIResponseObject<PostInsertReturn | R[]>>; }>({ table, basePath, body, query, targetId, method, apiOrigin, headers, ...options }: ClientCrudFetchParams<T, P>): Promise<APIResponseObject<PostInsertReturn | R[]>>;

View File

@ -8,6 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next()); step((generator = generator.apply(thisArg, _arguments || [])).next());
}); });
}; };
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
var __importDefault = (this && this.__importDefault) || function (mod) { var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
@ -16,7 +27,8 @@ exports.default = clientCrudFetch;
const serialize_query_1 = __importDefault(require("../../package-shared/utils/serialize-query")); const serialize_query_1 = __importDefault(require("../../package-shared/utils/serialize-query"));
const fetch_1 = __importDefault(require("../fetch")); const fetch_1 = __importDefault(require("../fetch"));
function clientCrudFetch(_a) { function clientCrudFetch(_a) {
return __awaiter(this, arguments, void 0, function* ({ table, basePath, body, query, targetId, method = "GET", apiOrigin, headers, }) { return __awaiter(this, void 0, void 0, function* () {
var { table, basePath, body, query, targetId, method = "GET", apiOrigin, headers } = _a, options = __rest(_a, ["table", "basePath", "body", "query", "targetId", "method", "apiOrigin", "headers"]);
try { try {
let pathname = basePath || ``; let pathname = basePath || ``;
pathname += `/${String(table)}`; pathname += `/${String(table)}`;
@ -30,11 +42,9 @@ function clientCrudFetch(_a) {
pathname = apiOrigin pathname = apiOrigin
? `${apiOrigin}/${pathname}`.replace(/([^:]\/)\/+/g, "$1") ? `${apiOrigin}/${pathname}`.replace(/([^:]\/)\/+/g, "$1")
: pathname; : pathname;
const res = yield (0, fetch_1.default)(pathname, { const res = yield (0, fetch_1.default)(pathname, Object.assign({ method,
method,
body, body,
headers, headers }, options));
});
return res; return res;
} }
catch (error) { catch (error) {

View File

@ -51,19 +51,12 @@ function fetchApi(url, options) {
else if (typeof options === "object") { else if (typeof options === "object") {
try { try {
let fetchData; let fetchData;
let fetchOptions = {};
if (options.body && typeof options.body === "object") { if (options.body && typeof options.body === "object") {
let oldOptionsBody = lodash_1.default.cloneDeep(options.body); fetchOptions.body = JSON.stringify(options.body);
options.body = JSON.stringify(oldOptionsBody);
}
if (options.headers) {
options.headers = lodash_1.default.merge(options.headers, finalHeaders);
const finalOptions = Object.assign({}, options);
fetchData = yield fetch(url, finalOptions);
}
else {
const finalOptions = Object.assign(Object.assign({}, options), { headers: finalHeaders });
fetchData = yield fetch(url, finalOptions);
} }
fetchOptions.headers = lodash_1.default.merge(finalHeaders, options.headers || {});
fetchData = yield fetch(url, fetchOptions);
data = fetchData.json(); data = fetchData.json();
} }
catch (error) { catch (error) {

View File

@ -2305,7 +2305,7 @@ export type ClientCrudFetchParams<T extends {
[k: string]: any; [k: string]: any;
} = { } = {
[k: string]: any; [k: string]: any;
}, P = string> = { }, P = string> = DSQLFetchApiOptions<T> & {
table: P; table: P;
method?: "GET" | "POST" | "PUT" | "DELETE"; method?: "GET" | "POST" | "PUT" | "DELETE";
query?: APIPathsQuery<T>; query?: APIPathsQuery<T>;
@ -2325,4 +2325,5 @@ export type DSQLFetchApiOptions<T extends {
headers?: DSQLClientFetchHeader; headers?: DSQLClientFetchHeader;
csrfValue?: string; csrfValue?: string;
csrfKey?: string; csrfKey?: string;
fetchOptions?: RequestInit;
}; };

View File

@ -2970,7 +2970,7 @@ export type APIPathsData<
export type ClientCrudFetchParams< export type ClientCrudFetchParams<
T extends { [k: string]: any } = { [k: string]: any }, T extends { [k: string]: any } = { [k: string]: any },
P = string P = string
> = { > = DSQLFetchApiOptions<T> & {
table: P; table: P;
method?: "GET" | "POST" | "PUT" | "DELETE"; method?: "GET" | "POST" | "PUT" | "DELETE";
query?: APIPathsQuery<T>; query?: APIPathsQuery<T>;
@ -2999,4 +2999,5 @@ export type DSQLFetchApiOptions<
headers?: DSQLClientFetchHeader; headers?: DSQLClientFetchHeader;
csrfValue?: string; csrfValue?: string;
csrfKey?: string; csrfKey?: string;
fetchOptions?: RequestInit;
}; };

View File

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