@proofkit/fmodata
Version:
FileMaker OData API client
137 lines (136 loc) • 4.83 kB
JavaScript
import { InvalidLocationHeaderError, BuilderInvariantError } from "../../errors.js";
import { getTableName } from "../../orm/table.js";
import { resolveTableId } from "./table-utils.js";
const ROWID_MATCH_REGEX = /ROWID=(\d+)/;
const PAREN_VALUE_REGEX = /\(['"]?([^'"]+)['"]?\)/;
function isRowIdRecordLocator(recordLocator) {
return typeof recordLocator === "object" && recordLocator !== null && "ROWID" in recordLocator;
}
function escapeODataStringLiteral(value) {
return value.replaceAll("'", "''");
}
function buildRecordLocatorSegment(recordLocator) {
if (isRowIdRecordLocator(recordLocator)) {
return `(ROWID=${recordLocator.ROWID})`;
}
return `('${escapeODataStringLiteral(String(recordLocator))}')`;
}
function buildRecordPath(pathPrefix, recordLocator) {
return `${pathPrefix}${buildRecordLocatorSegment(recordLocator)}`;
}
function mergeMutationExecuteOptions(options, databaseUseEntityIds, databaseIncludeSpecialColumns) {
return {
...options,
useEntityIds: (options == null ? void 0 : options.useEntityIds) ?? databaseUseEntityIds,
includeSpecialColumns: (options == null ? void 0 : options.includeSpecialColumns) ?? databaseIncludeSpecialColumns
};
}
function mergePreferHeaderValues(...values) {
const merged = [];
const seen = /* @__PURE__ */ new Set();
for (const value of values) {
if (!value) {
continue;
}
for (const part of value.split(",")) {
const normalized = part.trim();
if (!normalized || seen.has(normalized)) {
continue;
}
seen.add(normalized);
merged.push(normalized);
}
}
return merged.length > 0 ? merged.join(", ") : void 0;
}
function resolveMutationTableId(table, useEntityIds, builderName) {
if (!table) {
throw new BuilderInvariantError(builderName, "table occurrence is required");
}
return resolveTableId(table, getTableName(table), useEntityIds);
}
function buildMutationUrl(config) {
const { databaseName, tableId, tableName, mode, recordLocator, queryBuilder, useEntityIds, builderName } = config;
if (mode === "byId") {
if (recordLocator === void 0 || recordLocator === null || recordLocator === "") {
throw new BuilderInvariantError(builderName, "recordLocator is required for byId mode");
}
return `/${databaseName}/${buildRecordPath(tableId, recordLocator)}`;
}
if (!queryBuilder) {
throw new BuilderInvariantError(builderName, "query builder is required for filter mode");
}
const queryString = queryBuilder.getQueryString({ useEntityIds });
const queryParams = stripTablePathPrefix(queryString, tableId, tableName);
return `/${databaseName}/${tableId}${queryParams}`;
}
function stripTablePathPrefix(queryString, tableId, tableName) {
if (queryString.startsWith(`/${tableId}`)) {
return queryString.slice(`/${tableId}`.length);
}
if (queryString.startsWith(`/${tableName}`)) {
return queryString.slice(`/${tableName}`.length);
}
return queryString;
}
function extractAffectedRows(response, headers, fallback = 0, countKey) {
const headerValue = headers == null ? void 0 : headers.get("fmodata.affected_rows");
if (headerValue) {
const parsed = Number.parseInt(headerValue, 10);
if (!Number.isNaN(parsed)) {
return parsed;
}
}
if (typeof response === "number") {
return response;
}
if (response && typeof response === "object") {
if (countKey && countKey in response) {
const count = Number(response[countKey]);
if (!Number.isNaN(count)) {
return count;
}
}
const affected = Number(response["fmodata.affected_rows"]);
if (!Number.isNaN(affected)) {
return affected;
}
}
return fallback;
}
function parseRowIdFromLocationHeader(locationHeader) {
if (!locationHeader) {
throw new InvalidLocationHeaderError("Location header is required but was not provided");
}
const rowidMatch = locationHeader.match(ROWID_MATCH_REGEX);
if (rowidMatch == null ? void 0 : rowidMatch[1]) {
return Number.parseInt(rowidMatch[1], 10);
}
const parenMatch = locationHeader.match(PAREN_VALUE_REGEX);
if (parenMatch == null ? void 0 : parenMatch[1]) {
const value = Number.parseInt(parenMatch[1], 10);
if (!Number.isNaN(value)) {
return value;
}
}
throw new InvalidLocationHeaderError(
`Could not extract ROWID from Location header: ${locationHeader}`,
locationHeader
);
}
function getLocationHeader(headers) {
return headers.get("Location") || headers.get("location") || void 0;
}
export {
buildMutationUrl,
buildRecordLocatorSegment,
buildRecordPath,
extractAffectedRows,
getLocationHeader,
mergeMutationExecuteOptions,
mergePreferHeaderValues,
parseRowIdFromLocationHeader,
resolveMutationTableId,
stripTablePathPrefix
};
//# sourceMappingURL=mutation-helpers.js.map