@proofkit/fmodata
Version:
FileMaker OData API client
114 lines (113 loc) • 3.67 kB
JavaScript
import { getBaseTableConfig, getFieldId, isUsingEntityIds, getTableId, getFieldName } from "./orm/table.js";
const WHITESPACE_SPLIT_REGEX = /\s+/;
function transformFieldNamesToIds(data, table) {
const config = getBaseTableConfig(table);
if (!config.fmfIds) {
return data;
}
const transformed = {};
for (const [fieldName, value] of Object.entries(data)) {
const fieldId = getFieldId(table, fieldName);
transformed[fieldId] = value;
}
return transformed;
}
function transformResponseFields(data, table, expandConfigs) {
const config = getBaseTableConfig(table);
if (!config.fmfIds) {
return data;
}
if (data === null || data === void 0) {
return data;
}
if (data.value && Array.isArray(data.value)) {
return {
...data,
// biome-ignore lint/suspicious/noExplicitAny: Dynamic record transformation
value: data.value.map((record) => transformSingleRecord(record, table, expandConfigs))
};
}
if (Array.isArray(data)) {
return data.map((record) => transformSingleRecord(record, table, expandConfigs));
}
return transformSingleRecord(data, table, expandConfigs);
}
function transformSingleRecord(record, table, expandConfigs) {
if (!record || typeof record !== "object") {
return record;
}
const transformed = {};
for (const [key, value] of Object.entries(record)) {
if (key.startsWith("@")) {
transformed[key] = value;
continue;
}
let expandConfig = expandConfigs == null ? void 0 : expandConfigs.find((ec) => ec.relation === key);
if (!expandConfig && key.startsWith("FMTID:")) {
expandConfig = expandConfigs == null ? void 0 : expandConfigs.find(
(ec) => ec.table && isUsingEntityIds(ec.table) && getTableId(ec.table) === key
);
}
if (expandConfig == null ? void 0 : expandConfig.table) {
const relationKey = expandConfig.relation;
if (Array.isArray(value)) {
if (!expandConfig.table) {
transformed[relationKey] = value;
continue;
}
const nestedTable = expandConfig.table;
transformed[relationKey] = value.map(
(nestedRecord) => transformSingleRecord(
nestedRecord,
nestedTable,
void 0
// Don't pass nested expand configs for now
)
);
} else if (value && typeof value === "object") {
if (!expandConfig.table) {
transformed[relationKey] = value;
continue;
}
transformed[relationKey] = transformSingleRecord(value, expandConfig.table, void 0);
} else {
transformed[relationKey] = value;
}
continue;
}
const fieldName = getFieldName(table, key);
transformed[fieldName] = value;
}
return transformed;
}
function transformFieldNamesArray(fieldNames, table) {
const config = getBaseTableConfig(table);
if (!config.fmfIds) {
return fieldNames;
}
return fieldNames.map((fieldName) => getFieldId(table, fieldName));
}
function transformOrderByField(orderByString, table) {
if (!table) {
return orderByString;
}
const config = getBaseTableConfig(table);
if (!(config == null ? void 0 : config.fmfIds)) {
return orderByString;
}
const parts = orderByString.trim().split(WHITESPACE_SPLIT_REGEX);
const fieldName = parts[0];
if (!fieldName) {
return orderByString;
}
const direction = parts[1];
const fieldId = getFieldId(table, fieldName);
return direction ? `${fieldId} ${direction}` : fieldId;
}
export {
transformFieldNamesArray,
transformFieldNamesToIds,
transformOrderByField,
transformResponseFields
};
//# sourceMappingURL=transform.js.map