UNPKG

@proofkit/fmodata

Version:

FileMaker OData API client

86 lines (85 loc) 4.82 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); import { getTableName } from "../../orm/table.js"; import { buildRecordPath } from "../builders/mutation-helpers.js"; import { resolveTableId } from "../builders/table-utils.js"; class QueryUrlBuilder { // biome-ignore lint/suspicious/noExplicitAny: Accepts any FMTable configuration constructor(databaseName, occurrence, useEntityIds) { __publicField(this, "databaseName"); // biome-ignore lint/suspicious/noExplicitAny: Accepts any FMTable configuration __publicField(this, "occurrence"); __publicField(this, "useEntityIds"); this.databaseName = databaseName; this.occurrence = occurrence; this.useEntityIds = useEntityIds; } /** * Builds the full URL for a query request. * * @param queryString - The OData query string (e.g., "?$filter=...&$select=...") * @param options - Options including whether this is a count query, useEntityIds override, and navigation config */ build(queryString, options) { return `/${this.databaseName}${this.buildPath(queryString, options)}`; } /** * Builds a query string path (without database prefix) for getQueryString(). * Used when the full URL is not needed. */ buildPath(queryString, options) { const effectiveUseEntityIds = (options == null ? void 0 : options.useEntityIds) ?? this.useEntityIds; const navigation = options == null ? void 0 : options.navigation; const tableId = resolveTableId(this.occurrence, getTableName(this.occurrence), effectiveUseEntityIds); const suffix = (options == null ? void 0 : options.isCount) ? "/$count" : ""; if ((navigation == null ? void 0 : navigation.recordLocator) !== void 0 && (navigation == null ? void 0 : navigation.relation)) { const sourceTable = effectiveUseEntityIds ? navigation.sourceTableEntityId ?? navigation.sourceTableName : navigation.sourceTableName; const baseRelation = effectiveUseEntityIds ? navigation.baseRelationEntityId ?? navigation.baseRelation : navigation.baseRelation; const relation = effectiveUseEntityIds ? navigation.relationEntityId ?? navigation.relation : navigation.relation; const { recordLocator } = navigation; if (recordLocator === void 0) { throw new Error("recordLocator is required for record navigation"); } const base = baseRelation ? buildRecordPath(`${sourceTable}/${baseRelation}`, recordLocator) : buildRecordPath(sourceTable, recordLocator); return queryString ? `/${base}/${relation}${suffix}${queryString}` : `/${base}/${relation}${suffix}`; } if (navigation == null ? void 0 : navigation.relation) { const sourceTable = effectiveUseEntityIds ? navigation.sourceTableEntityId ?? navigation.sourceTableName : navigation.sourceTableName; const basePath = effectiveUseEntityIds ? navigation.basePathEntityId ?? navigation.basePath : navigation.basePath; const relation = effectiveUseEntityIds ? navigation.relationEntityId ?? navigation.relation : navigation.relation; const base = basePath || sourceTable; return queryString ? `/${base}/${relation}${suffix}${queryString}` : `/${base}/${relation}${suffix}`; } return queryString ? `/${tableId}${suffix}${queryString}` : `/${tableId}${suffix}`; } /** * Build URL for record operations (single record by ID). * Used by RecordBuilder to build URLs like /database/table('id'). * * @param recordLocator - The record locator * @param queryString - The OData query string (e.g., "?$select=...") * @param options - Options including operation type and useEntityIds override */ buildRecordUrl(recordLocator, queryString, options) { const effectiveUseEntityIds = (options == null ? void 0 : options.useEntityIds) ?? this.useEntityIds; const tableId = resolveTableId(this.occurrence, getTableName(this.occurrence), effectiveUseEntityIds); let url; if ((options == null ? void 0 : options.isNavigateFromEntitySet) && options.navigateSourceTableName && options.navigateRelation) { url = `/${this.databaseName}/${buildRecordPath( `${options.navigateSourceTableName}/${options.navigateRelation}`, recordLocator )}`; } else { url = `/${this.databaseName}/${buildRecordPath(tableId, recordLocator)}`; } if ((options == null ? void 0 : options.operation) === "getSingleField" && options.operationParam) { url += `/${options.operationParam}`; } return url + queryString; } } export { QueryUrlBuilder }; //# sourceMappingURL=url-builder.js.map