diff --git a/client/crud-fetch/index.ts b/client/crud-fetch/index.ts index cdc4786..ef99453 100644 --- a/client/crud-fetch/index.ts +++ b/client/crud-fetch/index.ts @@ -19,6 +19,7 @@ export default async function clientCrudFetch< method = "GET", apiOrigin, headers, + ...options }: ClientCrudFetchParams) { try { let pathname = basePath || ``; @@ -46,6 +47,7 @@ export default async function clientCrudFetch< method, body, headers, + ...options, }); return res; diff --git a/client/fetch/index.ts b/client/fetch/index.ts index 670bd5c..f850808 100644 --- a/client/fetch/index.ts +++ b/client/fetch/index.ts @@ -46,26 +46,15 @@ export default async function fetchApi< } else if (typeof options === "object") { try { let fetchData; + let fetchOptions: RequestInit = {}; if (options.body && typeof options.body === "object") { - let oldOptionsBody = _.cloneDeep(options.body); - options.body = JSON.stringify(oldOptionsBody); + fetchOptions.body = JSON.stringify(options.body); } - if (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); - } + fetchOptions.headers = _.merge(finalHeaders, options.headers || {}); + fetchData = await fetch(url, fetchOptions); data = fetchData.json(); } catch (error: any) { console.log("FetchAPI error #2:", error.message); diff --git a/dist/client/crud-fetch/index.d.ts b/dist/client/crud-fetch/index.d.ts index a9ead9c..8dddbda 100644 --- a/dist/client/crud-fetch/index.d.ts +++ b/dist/client/crud-fetch/index.d.ts @@ -7,4 +7,4 @@ export default function clientCrudFetch({ table, basePath, body, query, targetId, method, apiOrigin, headers, }: ClientCrudFetchParams): Promise>; +}>({ table, basePath, body, query, targetId, method, apiOrigin, headers, ...options }: ClientCrudFetchParams): Promise>; diff --git a/dist/client/crud-fetch/index.js b/dist/client/crud-fetch/index.js index 07f295f..5548085 100644 --- a/dist/client/crud-fetch/index.js +++ b/dist/client/crud-fetch/index.js @@ -8,6 +8,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge 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) { 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 fetch_1 = __importDefault(require("../fetch")); 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 { let pathname = basePath || ``; pathname += `/${String(table)}`; @@ -30,11 +42,9 @@ function clientCrudFetch(_a) { pathname = apiOrigin ? `${apiOrigin}/${pathname}`.replace(/([^:]\/)\/+/g, "$1") : pathname; - const res = yield (0, fetch_1.default)(pathname, { - method, + const res = yield (0, fetch_1.default)(pathname, Object.assign({ method, body, - headers, - }); + headers }, options)); return res; } catch (error) { diff --git a/dist/client/fetch/index.js b/dist/client/fetch/index.js index 28631df..75a89b4 100644 --- a/dist/client/fetch/index.js +++ b/dist/client/fetch/index.js @@ -51,19 +51,12 @@ function fetchApi(url, options) { else if (typeof options === "object") { try { let fetchData; + let fetchOptions = {}; if (options.body && typeof options.body === "object") { - let oldOptionsBody = lodash_1.default.cloneDeep(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.body = JSON.stringify(options.body); } + fetchOptions.headers = lodash_1.default.merge(finalHeaders, options.headers || {}); + fetchData = yield fetch(url, fetchOptions); data = fetchData.json(); } catch (error) { diff --git a/dist/package-shared/types/index.d.ts b/dist/package-shared/types/index.d.ts index 51ea87e..adfbee0 100644 --- a/dist/package-shared/types/index.d.ts +++ b/dist/package-shared/types/index.d.ts @@ -2305,7 +2305,7 @@ export type ClientCrudFetchParams = { +}, P = string> = DSQLFetchApiOptions & { table: P; method?: "GET" | "POST" | "PUT" | "DELETE"; query?: APIPathsQuery; @@ -2325,4 +2325,5 @@ export type DSQLFetchApiOptions = { +> = DSQLFetchApiOptions & { table: P; method?: "GET" | "POST" | "PUT" | "DELETE"; query?: APIPathsQuery; @@ -2999,4 +2999,5 @@ export type DSQLFetchApiOptions< headers?: DSQLClientFetchHeader; csrfValue?: string; csrfKey?: string; + fetchOptions?: RequestInit; }; diff --git a/package.json b/package.json index d8f2eb9..182f7ce 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "5.7.7", + "version": "5.7.8", "description": "Cloud-based SQL data management tool", "main": "dist/index.js", "bin": {