datasquirel/dist/client/crud-fetch/index.js
2026-01-01 09:22:51 +01:00

58 lines
2.7 KiB
JavaScript

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
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 };
};
Object.defineProperty(exports, "__esModule", { value: true });
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, 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)}`;
if (targetId) {
pathname += `/${String(targetId)}`;
}
if (query) {
pathname = `${pathname}${(0, serialize_query_1.default)(query)}`;
}
pathname = pathname.replace(/\/{2,}/g, "/");
pathname = apiOrigin
? `${apiOrigin}/${pathname}`.replace(/([^:]\/)\/+/g, "$1")
: pathname;
const res = yield (0, fetch_1.default)(pathname, Object.assign({ method,
body,
headers }, options));
return res;
}
catch (error) {
return {
success: false,
msg: `API ERROR => ${error.message}`,
};
}
});
}