UNPKG

@bitrix24/b24jssdk

Version:

Bitrix24 REST API JavaScript SDK

155 lines (151 loc) • 6.39 kB
/** * @package @bitrix24/b24jssdk * @version 3.0.0 * @copyright (c) 2026 Bitrix24 * @license MIT * @see https://github.com/bitrix24/b24jssdk * @see https://bitrix24.github.io/b24jssdk/ */ 'use strict'; const sdkError = require('../../sdk-error.cjs'); const _walkBounds = require('../_walk-bounds.cjs'); const _cursorStalled = require('../_cursor-stalled.cjs'); const _cursorProgress = require('../_cursor-progress.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); function assertArrayFilter(filter, action) { if (filter === void 0 || Array.isArray(filter)) { return; } throw new sdkError.SdkError({ code: "JSSDK_ACTION_V3_LIST_FILTER_NOT_ARRAY", description: isPortalFilterGroup(filter) ? `${action}: \`filter\` must be an array here. A logic group is a valid v3 filter \u2014 the portal accepts one on its own \u2014 but this action appends its page condition to \`filter\` on every request, and only an array can be extended. Wrap it: filter: [FilterV3.or(...)].` : `${action}: \`filter\` must be the restApi:v3 array form, e.g. [['id', '>', 100]] or FilterV3.build(...). The restApi:v2 object dialect ({ '>id': 100 }) is not accepted by the portal: a filter element carrying none of type/logic/conditions/negative is read positionally, so a map keyed by an operator prefix matches no condition it knows.`, status: 500 }); } __name(assertArrayFilter, "assertArrayFilter"); const PORTAL_GROUP_KEYS = ["type", "logic", "conditions", "negative"]; function isPortalFilterGroup(value) { if (typeof value !== "object" || value === null || Array.isArray(value)) { return false; } return PORTAL_GROUP_KEYS.some((key) => key in value); } __name(isPortalFilterGroup, "isPortalFilterGroup"); const MAX_FILTER_DEPTH = 32; function filterMentionsField(filter, field, depth = MAX_FILTER_DEPTH) { return walkFilterForField(filter, field, depth, /* @__PURE__ */ new WeakMap()); } __name(filterMentionsField, "filterMentionsField"); function walkFilterForField(filter, field, depth, seen) { if (depth <= 0) { return false; } if (typeof filter === "object" && filter !== null) { const exploredWith = seen.get(filter); if (exploredWith !== void 0 && exploredWith >= depth) { return false; } seen.set(filter, depth); } if (Array.isArray(filter)) { if (typeof filter[0] === "string") { return filter[0] === field; } return filter.some((node) => walkFilterForField(node, field, depth - 1, seen)); } if (typeof filter === "object" && filter !== null && "conditions" in filter) { return walkFilterForField(filter.conditions, field, depth - 1, seen); } return false; } __name(walkFilterForField, "walkFilterForField"); function assertTailFilter(filter, action) { if (filter === void 0 || Array.isArray(filter) || isPortalFilterGroup(filter)) { return; } throw new sdkError.SdkError({ code: "JSSDK_ACTION_V3_TAIL_FILTER_INVALID", description: `${action}: \`filter\` must be the restApi:v3 array form, e.g. [['id', '>', 100]] or FilterV3.build(...), or a logic group from FilterV3.or() / and() / not(). The restApi:v2 object dialect ({ '>id': 100 }) is not accepted by the portal: a filter element carrying none of type/logic/conditions/negative is read positionally, so a map keyed by an operator prefix matches no condition it knows. Rejected here rather than one round trip later, where the server reports it in wording that never mentions the dialect.`, status: 500 }); } __name(assertTailFilter, "assertTailFilter"); class KeysetPaginationError extends Error { static { __name(this, "KeysetPaginationError"); } errors; messages; constructor(errors, messages) { super(messages.join("; ")); this.name = "KeysetPaginationError"; this.errors = errors; this.messages = messages; } } async function* keysetPaginate(b24, logger, strategy) { let cursor = strategy.initialCursor; let maxPageSize = 0; let pages = 0; const maxPages = _walkBounds.resolveMaxPages(strategy.actionLabel, strategy.maxPages); while (true) { _walkBounds.assertNotAborted(strategy.signal, strategy.actionLabel, strategy.method); const response = await b24.actions.v3.call.make({ method: strategy.method, params: strategy.buildParams(cursor), requestId: strategy.requestId }); if (!response.isSuccess) { logger.error(strategy.errorLabel, { method: strategy.method, requestId: strategy.requestId, messages: response.getErrorMessages() }).catch(() => { }); throw new KeysetPaginationError(response.errors, response.getErrorMessages()); } const responseData = response.getData(); if (!responseData) { break; } const resultData = responseData.result[strategy.customKeyForResult]; if (!Array.isArray(resultData) || resultData.length === 0) { break; } pages += 1; yield resultData; _walkBounds.assertNotAborted(strategy.signal, strategy.actionLabel, strategy.method); maxPageSize = Math.max(maxPageSize, resultData.length); const isShortPage = resultData.length < maxPageSize; const lastItem = resultData[resultData.length - 1]; const next = lastItem ? strategy.readNextCursor(lastItem) : null; if (typeof next !== "number" && typeof next !== "string") { if (!isShortPage) { logger.warning(strategy.noCursorWarning).catch(() => { }); } break; } if (next === cursor) { throw _cursorStalled.cursorStalledError(strategy.actionLabel, strategy.stalledCursorHint); } if (!_cursorProgress.cursorProgressed(next, cursor, strategy.cursorDirection)) { throw _cursorStalled.cursorWentBackwardsError(strategy.actionLabel, strategy.stalledCursorHint); } if (isShortPage) { break; } if (pages >= maxPages) { throw _walkBounds.maxPagesExceededError(strategy.actionLabel, strategy.method, maxPages); } cursor = next; } } __name(keysetPaginate, "keysetPaginate"); exports.KeysetPaginationError = KeysetPaginationError; exports.assertArrayFilter = assertArrayFilter; exports.assertTailFilter = assertTailFilter; exports.filterMentionsField = filterMentionsField; exports.keysetPaginate = keysetPaginate; //# sourceMappingURL=_keyset-paginate.cjs.map