@objectstack/rest 12.1.0 → 12.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -667,9 +667,9 @@ function runImport(opts) {
667
667
  resolveRef
668
668
  });
669
669
  if (errors.length > 0) {
670
- const first = errors[0];
670
+ const first2 = errors[0];
671
671
  errCount++;
672
- results.push({ row: rowNo, ok: false, action: "failed", field: first.field, code: first.code, error: first.message });
672
+ results.push({ row: rowNo, ok: false, action: "failed", field: first2.field, code: first2.code, error: first2.message });
673
673
  } else {
674
674
  let existing = "none";
675
675
  let handled = false;
@@ -747,6 +747,116 @@ function runImport(opts) {
747
747
  })();
748
748
  }
749
749
 
750
+ // src/import-mapping.ts
751
+ function unwrapEnvelope(r) {
752
+ if (r && typeof r === "object" && "item" in r) {
753
+ return r.item;
754
+ }
755
+ return r;
756
+ }
757
+ async function resolveNamedMapping(p, opts) {
758
+ const { mappingName, objectName, detectedFormat } = opts;
759
+ if (typeof p?.getMetaItem !== "function") {
760
+ return { ok: false, status: 500, code: "INTERNAL", error: "Metadata protocol unavailable; cannot resolve mappingName" };
761
+ }
762
+ let artifact;
763
+ try {
764
+ artifact = unwrapEnvelope(await p.getMetaItem({ type: "mapping", name: mappingName }));
765
+ } catch {
766
+ }
767
+ if (!artifact || typeof artifact !== "object" || !Array.isArray(artifact.fieldMapping)) {
768
+ return { ok: false, status: 404, code: "MAPPING_NOT_FOUND", error: `No mapping artifact named "${mappingName}" is registered` };
769
+ }
770
+ if (artifact.targetObject !== objectName) {
771
+ return {
772
+ ok: false,
773
+ status: 400,
774
+ code: "MAPPING_TARGET_MISMATCH",
775
+ error: `Mapping "${mappingName}" targets object "${artifact.targetObject}", not "${objectName}"`
776
+ };
777
+ }
778
+ const declared = artifact.sourceFormat;
779
+ if (declared === "xml" || declared === "sql") {
780
+ return {
781
+ ok: false,
782
+ status: 400,
783
+ code: "MAPPING_FORMAT_UNSUPPORTED",
784
+ error: `Mapping "${mappingName}" declares sourceFormat "${declared}", which the import endpoint does not accept (csv/json/xlsx)`
785
+ };
786
+ }
787
+ const compatible = declared === void 0 || declared === "json" && detectedFormat === "json" || declared === "csv" && (detectedFormat === "csv" || detectedFormat === "xlsx");
788
+ if (!compatible) {
789
+ return {
790
+ ok: false,
791
+ status: 400,
792
+ code: "MAPPING_FORMAT_MISMATCH",
793
+ error: `Mapping "${mappingName}" declares sourceFormat "${declared}" but the payload is "${detectedFormat}"`
794
+ };
795
+ }
796
+ for (const entry of artifact.fieldMapping) {
797
+ if (entry?.transform === "javascript") {
798
+ return {
799
+ ok: false,
800
+ status: 400,
801
+ code: "UNSUPPORTED_TRANSFORM",
802
+ error: `Mapping "${mappingName}" uses transform "javascript", which the import path does not execute (no server-side sandbox; see framework#2611)`
803
+ };
804
+ }
805
+ }
806
+ return { ok: true, artifact };
807
+ }
808
+ var first = (v) => Array.isArray(v) ? v[0] : v;
809
+ function applyMappingToRows(rows, artifact) {
810
+ const out = [];
811
+ for (const row of rows) {
812
+ const mapped = {};
813
+ for (const entry of artifact.fieldMapping) {
814
+ const transform = entry.transform ?? "none";
815
+ const sep = entry.params?.separator ?? " ";
816
+ switch (transform) {
817
+ case "none":
818
+ case "lookup": {
819
+ mapped[first(entry.target)] = row[first(entry.source)];
820
+ break;
821
+ }
822
+ case "constant": {
823
+ mapped[first(entry.target)] = entry.params?.value;
824
+ break;
825
+ }
826
+ case "map": {
827
+ const raw = row[first(entry.source)];
828
+ const valueMap = entry.params?.valueMap ?? {};
829
+ mapped[first(entry.target)] = typeof raw === "string" && raw in valueMap ? valueMap[raw] : raw;
830
+ break;
831
+ }
832
+ case "split": {
833
+ const raw = row[first(entry.source)];
834
+ const targets = Array.isArray(entry.target) ? entry.target : [entry.target];
835
+ const parts = typeof raw === "string" ? raw.split(sep) : [];
836
+ targets.forEach((t, i) => {
837
+ mapped[t] = parts[i]?.trim();
838
+ });
839
+ break;
840
+ }
841
+ case "join": {
842
+ const sources = Array.isArray(entry.source) ? entry.source : [entry.source];
843
+ mapped[first(entry.target)] = sources.map((s) => row[s]).filter((v) => v !== void 0 && v !== null && v !== "").join(sep);
844
+ break;
845
+ }
846
+ default:
847
+ return {
848
+ ok: false,
849
+ status: 400,
850
+ code: "UNSUPPORTED_TRANSFORM",
851
+ error: `Mapping "${artifact.name}" uses unknown transform "${transform}"`
852
+ };
853
+ }
854
+ }
855
+ out.push(mapped);
856
+ }
857
+ return { ok: true, rows: out };
858
+ }
859
+
750
860
  // src/rest-server.ts
751
861
  var import_meta = {};
752
862
  var logError = (...args) => globalThis.console?.error(...args);
@@ -1011,13 +1121,34 @@ async function parseXlsxToRows(buffer, mapping = {}, sheet) {
1011
1121
  async function prepareImportRequest(body, opts) {
1012
1122
  const { p, objectName, environmentId, maxRows } = opts;
1013
1123
  const dryRun = body?.dryRun === true;
1014
- const writeMode = body?.writeMode === "update" || body?.writeMode === "upsert" ? body.writeMode : "insert";
1015
- const matchFields = Array.isArray(body?.matchFields) ? body.matchFields.filter((f) => typeof f === "string" && f.length > 0) : [];
1124
+ let writeMode = body?.writeMode === "update" || body?.writeMode === "upsert" ? body.writeMode : "insert";
1125
+ let matchFields = Array.isArray(body?.matchFields) ? body.matchFields.filter((f) => typeof f === "string" && f.length > 0) : [];
1016
1126
  const runAutomations = body?.runAutomations === true;
1017
1127
  const trimWhitespace = body?.trimWhitespace !== false;
1018
1128
  const nullValues = Array.isArray(body?.nullValues) ? body.nullValues.filter((v) => typeof v === "string") : void 0;
1019
1129
  const createMissingOptions = body?.createMissingOptions === true;
1020
1130
  const skipBlankMatchKey = body?.skipBlankMatchKey === true;
1131
+ const mappingName = typeof body?.mappingName === "string" && body.mappingName.length > 0 ? body.mappingName : void 0;
1132
+ const hasInlineMapping = Array.isArray(body?.mapping) && body.mapping.length > 0 || !Array.isArray(body?.mapping) && body?.mapping && typeof body.mapping === "object" && Object.keys(body.mapping).length > 0;
1133
+ if (mappingName && hasInlineMapping) {
1134
+ return { ok: false, status: 400, code: "CONFLICTING_MAPPING", error: "Provide either mappingName or an inline mapping, not both" };
1135
+ }
1136
+ let mappingArtifact;
1137
+ if (mappingName) {
1138
+ const detectedFormat = body?.format === "json" && Array.isArray(body?.rows) || Array.isArray(body) ? "json" : typeof body?.csv === "string" ? "csv" : typeof body?.xlsxBase64 === "string" ? "xlsx" : void 0;
1139
+ if (!detectedFormat) {
1140
+ return { ok: false, status: 400, code: "INVALID_REQUEST", error: 'Provide format:"csv" with csv text, format:"json" with rows[], or format:"xlsx" with xlsxBase64' };
1141
+ }
1142
+ const resolved = await resolveNamedMapping(p, { mappingName, objectName, detectedFormat });
1143
+ if (!resolved.ok) return resolved;
1144
+ mappingArtifact = resolved.artifact;
1145
+ if (body?.writeMode === void 0 && (mappingArtifact.mode === "update" || mappingArtifact.mode === "upsert")) {
1146
+ writeMode = mappingArtifact.mode;
1147
+ }
1148
+ if (matchFields.length === 0 && Array.isArray(mappingArtifact.upsertKey)) {
1149
+ matchFields = mappingArtifact.upsertKey.filter((f) => typeof f === "string" && f.length > 0);
1150
+ }
1151
+ }
1021
1152
  if (writeMode !== "insert" && matchFields.length === 0) {
1022
1153
  return { ok: false, status: 400, code: "INVALID_REQUEST", error: `writeMode "${writeMode}" requires a non-empty matchFields[]` };
1023
1154
  }
@@ -1059,6 +1190,11 @@ async function prepareImportRequest(body, opts) {
1059
1190
  if (rows.length > maxRows) {
1060
1191
  return { ok: false, status: 413, code: "PAYLOAD_TOO_LARGE", error: `Import limit is ${maxRows} rows per request (got ${rows.length}).` };
1061
1192
  }
1193
+ if (mappingArtifact) {
1194
+ const applied = applyMappingToRows(rows, mappingArtifact);
1195
+ if (!applied.ok) return applied;
1196
+ rows = applied.rows;
1197
+ }
1062
1198
  let metaMap = /* @__PURE__ */ new Map();
1063
1199
  try {
1064
1200
  let schema = void 0;