@openfn/language-fhir-4 0.4.1 → 0.4.2

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.
Files changed (3) hide show
  1. package/dist/index.cjs +50 -18
  2. package/dist/index.js +1293 -1261
  3. package/package.json +2 -1
package/dist/index.cjs CHANGED
@@ -79,6 +79,7 @@ var import_util2 = require("@openfn/language-common/util");
79
79
 
80
80
  // src/util.ts
81
81
  var import_node_path = __toESM(require("path"), 1);
82
+ var import_lodash_es = __toESM(require("lodash-es"), 1);
82
83
  var import_language_common = require("@openfn/language-common");
83
84
  var import_util = require("@openfn/language-common/util");
84
85
  function assertValidResourceId(id2) {
@@ -102,7 +103,7 @@ function addAuth(options) {
102
103
  return {};
103
104
  }
104
105
  var prepareNextState = (state, response) => {
105
- const { body, ...responseWithoutBody } = response;
106
+ const { body, validationErrors, ...responseWithoutBody } = response;
106
107
  return {
107
108
  ...(0, import_language_common.composeNextState)(state, body),
108
109
  response: responseWithoutBody
@@ -146,12 +147,12 @@ var request = (method, path, options) => {
146
147
  };
147
148
  return (0, import_util.request)(method, fullPath, opts).then(logResponse).then().catch(async (e) => {
148
149
  if (e.headers && "content-type" in e.headers && e.headers["content-type"].match(/fhir\+json/)) {
149
- logValidationErrors(e);
150
+ logValidationErrors(e, opts.body);
150
151
  }
151
152
  throw e;
152
153
  });
153
154
  };
154
- function logValidationErrors(response, logger = console) {
155
+ function logValidationErrors(response, payload, logger = console) {
155
156
  const error = JSON.parse(response.body);
156
157
  if (error.issue && error.issue.length) {
157
158
  delete response.body;
@@ -176,20 +177,35 @@ function logValidationErrors(response, logger = console) {
176
177
  error.issue.forEach((issue) => {
177
178
  var _a, _b, _c;
178
179
  try {
179
- let id2 = "unidentified resource";
180
+ let resourceId = "unidentified resource";
180
181
  if (issue.location) {
181
- id2 = issue.location[0];
182
- if (id2.startsWith("Bundle")) {
183
- id2 = id2.split("*").at(1) ?? id2;
182
+ let idx = issue.location[0].match(/Bundle.entry\[(\d+)\]/);
183
+ if (idx && idx.length >= 2) {
184
+ idx = idx[1];
185
+ const resource = (_a = import_lodash_es.default.get(payload, `entry[${idx}]`)) == null ? void 0 : _a.resource;
186
+ if (resource) {
187
+ resourceId = `${resource.resourceType}/${resource.id}`;
188
+ }
189
+ } else {
190
+ if (payload.resourceType && payload.id) {
191
+ resourceId = `${payload.resourceType}/${payload.id}`;
192
+ }
193
+ }
194
+ }
195
+ const type = `${issue.severity}s`;
196
+ groups[resourceId] ?? (groups[resourceId] = {});
197
+ let path = "*";
198
+ if (issue.location[0].match(/\.resource\./)) {
199
+ path = issue.location[0].split(/\.resource\./)[1];
200
+ } else if (issue.location.includes(`*${resourceId}*`)) {
201
+ const suffix = issue.location[0].split(/\.resource\./)[1];
202
+ if (suffix && suffix.length > 1) {
203
+ path = suffix;
184
204
  }
185
- } else {
186
- console.log({ issue });
187
205
  }
188
- groups[id2] ?? (groups[id2] = {});
189
- (_a = groups[id2])[_b = issue.severity] ?? (_a[_b] = []);
190
- const path = issue.location[0];
191
- (_c = groups[id2][issue.severity])[path] ?? (_c[path] = []);
192
- groups[id2][issue.severity][path].push(issue.diagnostics);
206
+ (_b = groups[resourceId])[type] ?? (_b[type] = {});
207
+ (_c = groups[resourceId][type])[path] ?? (_c[path] = []);
208
+ groups[resourceId][type][path].push(issue.diagnostics);
193
209
  } catch (e) {
194
210
  logger.log("error parsing issue at ", issue.location);
195
211
  console.log(e);
@@ -197,26 +213,36 @@ function logValidationErrors(response, logger = console) {
197
213
  });
198
214
  for (const resource in groups) {
199
215
  logger.log(`${resource} issues:`);
200
- ["error", "warning"].forEach((type) => {
216
+ ["errors", "warnings"].forEach((type) => {
201
217
  if (type in groups[resource]) {
202
- logger.log(` ${type}s:`.toUpperCase());
218
+ logger.log(` ${type}:`.toUpperCase());
203
219
  for (const path in groups[resource][type]) {
204
220
  logger.log();
205
221
  logger.log(" ", path);
206
222
  for (const message of groups[resource][type][path]) {
207
- logger.log(" -", message);
223
+ const fn2 = type === "error" ? console.error : console.warn;
224
+ fn2(" -", message);
208
225
  }
209
226
  }
210
227
  logger.log();
211
228
  }
212
229
  });
213
230
  }
214
- response.validationIssues = groups;
231
+ logger.log("Issues will be written to state.fhirValidationIssues");
232
+ response.$validationIssues = groups;
215
233
  } else {
216
234
  response.body = error;
217
235
  }
218
236
  return response;
219
237
  }
238
+ function cleanResponseObject(state, response) {
239
+ if (response.$validationIssues) {
240
+ state.fhirValidationIssues ?? (state.fhirValidationIssues = {});
241
+ Object.assign(state.fhirValidationIssues, response.$validationIssues);
242
+ delete response.$validationIssues;
243
+ }
244
+ return state;
245
+ }
220
246
  function collectRefs(value2) {
221
247
  if (!value2 || typeof value2 !== "object")
222
248
  return [];
@@ -383,6 +409,9 @@ function create(resource) {
383
409
  const response = await request("POST", $resource.resourceType, {
384
410
  configuration: state.configuration,
385
411
  body: $resource
412
+ }).catch((e) => {
413
+ cleanResponseObject(state, e);
414
+ throw e;
386
415
  });
387
416
  console.log(`Created ${response.body.id}`);
388
417
  return prepareNextState(state, response);
@@ -447,6 +476,9 @@ function uploadBundle(bundle = "bundle") {
447
476
  const response = await request("POST", "/", {
448
477
  configuration: state.configuration,
449
478
  body: data
479
+ }).catch((e) => {
480
+ cleanResponseObject(state, e);
481
+ throw e;
450
482
  });
451
483
  return prepareNextState(state, response);
452
484
  };