@openfn/language-fhir-4 0.3.2 → 0.4.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
@@ -117,11 +117,11 @@ var logResponse = (response, query) => {
117
117
  }
118
118
  const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
119
119
  if (response instanceof Error) {
120
- console.error(message);
121
- console.error("response body: ");
122
- console.error(response.body || "[no body]");
120
+ logger.error(message);
121
+ logger.error("response body: ");
122
+ logger.error(response.body || "[no body]");
123
123
  } else {
124
- console.log(message);
124
+ logger.log(message);
125
125
  }
126
126
  }
127
127
  return response;
@@ -129,7 +129,7 @@ var logResponse = (response, query) => {
129
129
  var request = (method, path, options) => {
130
130
  (0, import_util.assertRelativeUrl)(path);
131
131
  const { configuration, ...otherOptions } = options;
132
- const fullPath = import_node_path.default.join(configuration.apiPath ?? "", path);
132
+ const fullPath = import_node_path.default.join(configuration.apiPath ?? "/fhir", path);
133
133
  const headers = addAuth(options);
134
134
  const opts = {
135
135
  ...otherOptions,
@@ -144,20 +144,65 @@ var request = (method, path, options) => {
144
144
  baseUrl: configuration.baseUrl,
145
145
  parseAs: "json"
146
146
  };
147
- return (0, import_util.request)(method, fullPath, opts).then(logResponse).catch(async (e) => {
147
+ return (0, import_util.request)(method, fullPath, opts).then(logResponse).then().catch(async (e) => {
148
148
  if (e.headers && "content-type" in e.headers && e.headers["content-type"].match(/fhir\+json/)) {
149
- const error = JSON.parse(e.body);
150
- e.body = error;
151
- if (error.issue && error.issue.length) {
152
- console.error("Error from FHIR server:");
153
- error.issue.forEach((issue) => {
154
- console.error(issue.diagnostics);
155
- });
156
- }
149
+ logValidationErrors(e);
157
150
  }
158
151
  throw e;
159
152
  });
160
153
  };
154
+ function logValidationErrors(response, logger2 = console) {
155
+ const error = JSON.parse(response.body);
156
+ if (error.issue && error.issue.length) {
157
+ delete response.body;
158
+ logger2.log();
159
+ logger2.error("FHIR server reports validation issues:");
160
+ const errCount = error.issue.reduce(
161
+ (count, e) => e.severity === "error" ? count + 1 : count,
162
+ 0
163
+ );
164
+ if (errCount) {
165
+ logger2.error(` - ${errCount} Errors`);
166
+ }
167
+ const warnCount = error.issue.reduce(
168
+ (count, e) => e.severity === "error" ? count + 1 : count,
169
+ 0
170
+ );
171
+ if (warnCount) {
172
+ logger2.error(` - ${warnCount} Warnings`);
173
+ }
174
+ logger2.log();
175
+ const groups = {};
176
+ error.issue.forEach((issue) => {
177
+ var _a, _b;
178
+ try {
179
+ let id2 = issue.location[0];
180
+ if (id2.startsWith("Bundle")) {
181
+ id2 = id2.split("*").at(1);
182
+ }
183
+ groups[id2] ?? (groups[id2] = {});
184
+ (_a = groups[id2])[_b = issue.severity] ?? (_a[_b] = []);
185
+ groups[id2][issue.severity].push(issue.diagnostics);
186
+ } catch (e) {
187
+ logger2.log("error parsing issue at ", issue.location);
188
+ }
189
+ });
190
+ for (const resource in groups) {
191
+ logger2.log(`${resource} issues:`);
192
+ ["error", "warning"].forEach((type) => {
193
+ logger2.log(` ${type}s:`.toUpperCase());
194
+ for (const e of groups[resource][type]) {
195
+ logger2.log(" -", e);
196
+ }
197
+ logger2.log();
198
+ });
199
+ }
200
+ response.validationIssues = groups;
201
+ } else {
202
+ response.body = error;
203
+ }
204
+ return response;
205
+ }
161
206
  function collectRefs(value2) {
162
207
  if (!value2 || typeof value2 !== "object")
163
208
  return [];
package/dist/index.js CHANGED
@@ -75,11 +75,11 @@ var logResponse = (response, query) => {
75
75
  }
76
76
  const message = `${method} ${urlWithQuery} - ${statusCode} in ${duration}ms`;
77
77
  if (response instanceof Error) {
78
- console.error(message);
79
- console.error("response body: ");
80
- console.error(response.body || "[no body]");
78
+ logger.error(message);
79
+ logger.error("response body: ");
80
+ logger.error(response.body || "[no body]");
81
81
  } else {
82
- console.log(message);
82
+ logger.log(message);
83
83
  }
84
84
  }
85
85
  return response;
@@ -87,7 +87,7 @@ var logResponse = (response, query) => {
87
87
  var request = (method, path, options) => {
88
88
  assertRelativeUrl(path);
89
89
  const { configuration, ...otherOptions } = options;
90
- const fullPath = nodepath.join(configuration.apiPath ?? "", path);
90
+ const fullPath = nodepath.join(configuration.apiPath ?? "/fhir", path);
91
91
  const headers = addAuth(options);
92
92
  const opts = {
93
93
  ...otherOptions,
@@ -102,20 +102,65 @@ var request = (method, path, options) => {
102
102
  baseUrl: configuration.baseUrl,
103
103
  parseAs: "json"
104
104
  };
105
- return commonRequest(method, fullPath, opts).then(logResponse).catch(async (e) => {
105
+ return commonRequest(method, fullPath, opts).then(logResponse).then().catch(async (e) => {
106
106
  if (e.headers && "content-type" in e.headers && e.headers["content-type"].match(/fhir\+json/)) {
107
- const error = JSON.parse(e.body);
108
- e.body = error;
109
- if (error.issue && error.issue.length) {
110
- console.error("Error from FHIR server:");
111
- error.issue.forEach((issue) => {
112
- console.error(issue.diagnostics);
113
- });
114
- }
107
+ logValidationErrors(e);
115
108
  }
116
109
  throw e;
117
110
  });
118
111
  };
112
+ function logValidationErrors(response, logger2 = console) {
113
+ const error = JSON.parse(response.body);
114
+ if (error.issue && error.issue.length) {
115
+ delete response.body;
116
+ logger2.log();
117
+ logger2.error("FHIR server reports validation issues:");
118
+ const errCount = error.issue.reduce(
119
+ (count, e) => e.severity === "error" ? count + 1 : count,
120
+ 0
121
+ );
122
+ if (errCount) {
123
+ logger2.error(` - ${errCount} Errors`);
124
+ }
125
+ const warnCount = error.issue.reduce(
126
+ (count, e) => e.severity === "error" ? count + 1 : count,
127
+ 0
128
+ );
129
+ if (warnCount) {
130
+ logger2.error(` - ${warnCount} Warnings`);
131
+ }
132
+ logger2.log();
133
+ const groups = {};
134
+ error.issue.forEach((issue) => {
135
+ var _a, _b;
136
+ try {
137
+ let id2 = issue.location[0];
138
+ if (id2.startsWith("Bundle")) {
139
+ id2 = id2.split("*").at(1);
140
+ }
141
+ groups[id2] ?? (groups[id2] = {});
142
+ (_a = groups[id2])[_b = issue.severity] ?? (_a[_b] = []);
143
+ groups[id2][issue.severity].push(issue.diagnostics);
144
+ } catch (e) {
145
+ logger2.log("error parsing issue at ", issue.location);
146
+ }
147
+ });
148
+ for (const resource in groups) {
149
+ logger2.log(`${resource} issues:`);
150
+ ["error", "warning"].forEach((type) => {
151
+ logger2.log(` ${type}s:`.toUpperCase());
152
+ for (const e of groups[resource][type]) {
153
+ logger2.log(" -", e);
154
+ }
155
+ logger2.log();
156
+ });
157
+ }
158
+ response.validationIssues = groups;
159
+ } else {
160
+ response.body = error;
161
+ }
162
+ return response;
163
+ }
119
164
  function collectRefs(value2) {
120
165
  if (!value2 || typeof value2 !== "object")
121
166
  return [];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@openfn/language-fhir-4",
3
3
  "label": "FHIR r4",
4
- "version": "0.3.2",
4
+ "version": "0.4.0",
5
5
  "description": "OpenFn FHIR r4 adaptor",
6
6
  "author": "Open Function Group",
7
7
  "license": "LGPLv3",