UNPKG

@replyke/core

Version:

Replyke: Build interactive apps with social features like comments, votes, feeds, user lists, notifications, and more.

242 lines 12.7 kB
"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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; 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 __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.deleteEntity = exports.updateEntity = exports.createEntity = exports.fetchEntities = exports.useDeleteEntityMutation = exports.useUpdateEntityMutation = exports.useCreateEntityMutation = exports.useLazyFetchEntitiesQuery = exports.useFetchEntitiesQuery = exports.entityListsApi = void 0; var baseApi_1 = require("./baseApi"); var EntityListSortByOptions_1 = require("../../interfaces/EntityListSortByOptions"); // Helper function to serialize objects using bracket notation (like Axios does) var serializeObject = function (obj, prefix) { if (prefix === void 0) { prefix = ''; } var params = {}; var _loop_1 = function (key) { if (obj.hasOwnProperty(key)) { var value = obj[key]; var paramKey_1 = prefix ? "".concat(prefix, "[").concat(key, "]") : key; if (value === null || value === undefined) { return "continue"; } else if (Array.isArray(value)) { value.forEach(function (item, index) { if (item !== null && item !== undefined) { if (typeof item === 'object') { Object.assign(params, serializeObject(item, "".concat(paramKey_1, "[").concat(index, "]"))); } else { params["".concat(paramKey_1, "[").concat(index, "]")] = item; } } }); } else if (typeof value === 'object') { Object.assign(params, serializeObject(value, paramKey_1)); } else { params[paramKey_1] = value; } } }; for (var key in obj) { _loop_1(key); } return params; }; // Helper function to build clean query parameters var buildQueryParams = function (params) { var cleanParams = {}; Object.entries(params).forEach(function (_a) { var key = _a[0], value = _a[1]; // Skip undefined values, but allow null for sourceId if (value === undefined || (value === null && key !== 'sourceId')) { return; } // Skip default values that don't need to be sent if (key === 'followedOnly' && value === false) { return; } // Serialize filter objects using bracket notation (like Axios) if (key.endsWith('Filters') && typeof value === 'object' && value !== null) { Object.assign(cleanParams, serializeObject(value, key)); } else { // Include all other meaningful values as-is cleanParams[key] = value; } }); return cleanParams; }; // Extended API with entity lists endpoints exports.entityListsApi = baseApi_1.baseApi.injectEndpoints({ endpoints: function (builder) { return ({ // Fetch paginated entities with filters fetchEntities: builder.query({ query: function (_a) { var projectId = _a.projectId, page = _a.page, limit = _a.limit, sortBy = _a.sortBy, sortDir = _a.sortDir, sortType = _a.sortType, timeFrame = _a.timeFrame, sourceId = _a.sourceId, userId = _a.userId, followedOnly = _a.followedOnly, keywordsFilters = _a.keywordsFilters, locationFilters = _a.locationFilters, metadataFilters = _a.metadataFilters, titleFilters = _a.titleFilters, contentFilters = _a.contentFilters, attachmentsFilters = _a.attachmentsFilters; if (!sortBy) { throw new Error("sortBy is required for fetching entities"); } // Validate sortBy (especially for metadata.* patterns) (0, EntityListSortByOptions_1.validateSortBy)(sortBy); return { url: "/".concat(projectId, "/entities"), method: "GET", params: buildQueryParams({ page: page, limit: limit, followedOnly: followedOnly, userId: userId, sourceId: sourceId, sortBy: sortBy, sortDir: sortDir, sortType: sortType, timeFrame: timeFrame, keywordsFilters: keywordsFilters, metadataFilters: metadataFilters, titleFilters: titleFilters, contentFilters: contentFilters, attachmentsFilters: attachmentsFilters, locationFilters: locationFilters, }), }; }, providesTags: function (result, error, _a) { var _b; var projectId = _a.projectId, sourceId = _a.sourceId; return __spreadArray([ { type: "Entity", id: "".concat(projectId, "-").concat(sourceId || 'all', "-LIST") } ], ((_b = result === null || result === void 0 ? void 0 : result.map(function (_a) { var id = _a.id; return ({ type: "Entity", id: id, }); })) !== null && _b !== void 0 ? _b : []), true); }, }), // Create entity createEntity: builder.mutation({ query: function (_a) { var projectId = _a.projectId, body = __rest(_a, ["projectId"]); return ({ url: "/".concat(projectId, "/entities"), method: "POST", body: body, }); }, invalidatesTags: function (result, error, _a) { var projectId = _a.projectId, sourceId = _a.sourceId; return __spreadArray([ { type: "Entity", id: "".concat(projectId, "-").concat(sourceId || 'all', "-LIST") } ], (sourceId ? [{ type: "Entity", id: "".concat(projectId, "-all-LIST") }] : []), true); }, }), // Update entity updateEntity: builder.mutation({ query: function (_a) { var projectId = _a.projectId, entityId = _a.entityId, update = _a.update; return ({ url: "/".concat(projectId, "/entities/").concat(entityId), method: "PATCH", body: update, }); }, invalidatesTags: function (result, error, _a) { var projectId = _a.projectId, entityId = _a.entityId; return [ { type: "Entity", id: entityId }, // Invalidate all lists that might contain this entity { type: "Entity", id: "".concat(projectId, "-all-LIST") }, ]; }, }), // Delete entity deleteEntity: builder.mutation({ query: function (_a) { var projectId = _a.projectId, entityId = _a.entityId; return ({ url: "/".concat(projectId, "/entities/").concat(entityId), method: "DELETE", responseHandler: function (response) { return __awaiter(void 0, void 0, void 0, function () { var contentType; return __generator(this, function (_a) { contentType = response.headers.get('content-type') || ''; if (contentType.includes('application/json')) { return [2 /*return*/, response.json()]; } // For text responses, just return void since we don't need the content return [2 /*return*/, response.text().then(function () { return undefined; })]; }); }); }, }); }, invalidatesTags: function (result, error, _a) { var projectId = _a.projectId, entityId = _a.entityId; return [ { type: "Entity", id: entityId }, // Invalidate all lists that might have contained this entity { type: "Entity", id: "".concat(projectId, "-all-LIST") }, ]; }, }), }); }, }); // Export hooks for use in components exports.useFetchEntitiesQuery = exports.entityListsApi.useFetchEntitiesQuery, exports.useLazyFetchEntitiesQuery = exports.entityListsApi.useLazyFetchEntitiesQuery, exports.useCreateEntityMutation = exports.entityListsApi.useCreateEntityMutation, exports.useUpdateEntityMutation = exports.entityListsApi.useUpdateEntityMutation, exports.useDeleteEntityMutation = exports.entityListsApi.useDeleteEntityMutation; // Export for manual cache management exports.fetchEntities = (_a = exports.entityListsApi.endpoints, _a.fetchEntities), exports.createEntity = _a.createEntity, exports.updateEntity = _a.updateEntity, exports.deleteEntity = _a.deleteEntity; //# sourceMappingURL=entityListsApi.js.map