@healthcloudai/hc-safe-cdx 0.0.1 → 0.1.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.
package/dist/index.cjs CHANGED
@@ -3,6 +3,10 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
6
10
  var __copyProps = (to, from, except, desc) => {
7
11
  if (from && typeof from === "object" || typeof from === "function") {
8
12
  for (let key of __getOwnPropNames(from))
@@ -15,4 +19,394 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
15
19
 
16
20
  // src/index.ts
17
21
  var index_exports = {};
22
+ __export(index_exports, {
23
+ ConfigError: () => ConfigError,
24
+ HCSafeCDXClient: () => HCSafeCDXClient
25
+ });
18
26
  module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/client.ts
29
+ var SafeCDXError = class _SafeCDXError extends Error {
30
+ constructor(message, response) {
31
+ super(message);
32
+ this.name = "SafeCDXError";
33
+ this.response = response;
34
+ Object.setPrototypeOf(this, _SafeCDXError.prototype);
35
+ }
36
+ };
37
+ var ENV_HOST = {
38
+ dev: "dev-api-hcs.healthcloud-services.com",
39
+ uat: "uat-api-hcs.healthcloud-services.com",
40
+ prod: "api-hcs.healthcloud-services.com"
41
+ };
42
+ var ROUTE_PREFIX = "api/console/hcservice/safecdx";
43
+ function buildUrl(host, route, query) {
44
+ const url = new URL(
45
+ `https://${host}/${ROUTE_PREFIX}/${route.replace(/^\/+/, "")}`
46
+ );
47
+ if (query) {
48
+ for (const [key, value] of Object.entries(query)) {
49
+ if (value !== void 0 && value !== "") {
50
+ url.searchParams.set(key, value);
51
+ }
52
+ }
53
+ }
54
+ return url.toString();
55
+ }
56
+ function wrapData(payload) {
57
+ return { Data: payload };
58
+ }
59
+ function assertApiResponse(envelope, route) {
60
+ var _a;
61
+ if (!envelope.IsOK) {
62
+ throw new SafeCDXError(
63
+ (_a = envelope.ErrorMessage) != null ? _a : `${route} returned IsOK=false.`,
64
+ envelope
65
+ );
66
+ }
67
+ return envelope;
68
+ }
69
+ var HCSafeCDXClient = class {
70
+ constructor(httpClient, authClient, config) {
71
+ var _a;
72
+ if (!ENV_HOST[config.environment]) {
73
+ throw new SafeCDXError(`Invalid environment: ${config.environment}`);
74
+ }
75
+ this.http = httpClient;
76
+ this.auth = authClient;
77
+ this.host = ENV_HOST[config.environment];
78
+ this.defaultLanguage = config.defaultLanguage;
79
+ this.defaultImageType = (_a = config.defaultImageType) != null ? _a : "jpg";
80
+ }
81
+ setApiKey(headerName, value) {
82
+ const trimmedHeaderName = headerName == null ? void 0 : headerName.trim();
83
+ const trimmedValue = value == null ? void 0 : value.trim();
84
+ if (!trimmedHeaderName) {
85
+ throw new SafeCDXError("API key header name is required.");
86
+ }
87
+ if (!trimmedValue) {
88
+ throw new SafeCDXError("API key value is required.");
89
+ }
90
+ this.apiKeyHeaderName = trimmedHeaderName;
91
+ this.apiKeyValue = trimmedValue;
92
+ }
93
+ // ---------------------------------------------------------------------------
94
+ // Private helpers
95
+ // ---------------------------------------------------------------------------
96
+ getAuthHeaders() {
97
+ return {
98
+ ...this.auth.getAuthHeader(),
99
+ ...this.getApiKeyHeader()
100
+ };
101
+ }
102
+ getJsonHeaders() {
103
+ return {
104
+ ...this.getAuthHeaders(),
105
+ "Content-Type": "application/json"
106
+ };
107
+ }
108
+ getApiKeyHeader() {
109
+ if (!this.apiKeyHeaderName || !this.apiKeyValue) {
110
+ return {};
111
+ }
112
+ return {
113
+ [this.apiKeyHeaderName]: this.apiKeyValue
114
+ };
115
+ }
116
+ url(route, query) {
117
+ return buildUrl(this.host, route, query);
118
+ }
119
+ // ---------------------------------------------------------------------------
120
+ // Scan
121
+ // ---------------------------------------------------------------------------
122
+ /**
123
+ * GET gs1/:gtin
124
+ * Resolves a test profile by GTIN barcode and creates a UserTestResultId.
125
+ */
126
+ async getTestProfileByGTIN(gtin, language) {
127
+ const envelope = await this.http.get(
128
+ this.url(`gs1/${encodeURIComponent(gtin)}`, {
129
+ language: language != null ? language : this.defaultLanguage
130
+ }),
131
+ this.getAuthHeaders()
132
+ );
133
+ return assertApiResponse(envelope, "gs1/:gtin");
134
+ }
135
+ /**
136
+ * POST scan/2ddm
137
+ * Scans a 2D data matrix barcode and resolves a UserTestResultId.
138
+ */
139
+ async scan2Ddm(payload) {
140
+ const envelope = await this.http.post(
141
+ this.url("scan/2ddm"),
142
+ wrapData(payload),
143
+ this.getJsonHeaders()
144
+ );
145
+ return assertApiResponse(envelope, "scan/2ddm");
146
+ }
147
+ // ---------------------------------------------------------------------------
148
+ // Test Profiles
149
+ // ---------------------------------------------------------------------------
150
+ /**
151
+ * POST test/profile
152
+ * Returns the full test profile for a given GTIN.
153
+ */
154
+ async getTestProfile(payload) {
155
+ const envelope = await this.http.post(
156
+ this.url("test/profile"),
157
+ wrapData(payload),
158
+ this.getJsonHeaders()
159
+ );
160
+ return assertApiResponse(envelope, "test/profile");
161
+ }
162
+ /**
163
+ * POST test/profiles/by-account
164
+ * Lists all test profiles available to the authenticated account.
165
+ */
166
+ async getTestProfilesByAccount(payload = {
167
+ IncludeRegisterTestDetails: true
168
+ }) {
169
+ const envelope = await this.http.post(
170
+ this.url("test/profiles/by-account"),
171
+ wrapData(payload),
172
+ this.getJsonHeaders()
173
+ );
174
+ return assertApiResponse(envelope, "test/profiles/by-account");
175
+ }
176
+ // ---------------------------------------------------------------------------
177
+ // Upload
178
+ // ---------------------------------------------------------------------------
179
+ /**
180
+ * POST upload/url
181
+ * Requests a pre-signed S3 URL for image upload.
182
+ * Use the returned Data.preSignedURL and Data.Metadata.UploadId in the next steps.
183
+ */
184
+ async createUploadUrl(userTestResultId, gtin, imageType) {
185
+ const payload = {
186
+ Gtin: gtin,
187
+ UserTestResultId: userTestResultId,
188
+ Metadata: {
189
+ ImageType: imageType != null ? imageType : this.defaultImageType
190
+ }
191
+ };
192
+ const envelope = await this.http.post(
193
+ this.url("upload/url"),
194
+ wrapData(payload),
195
+ this.getJsonHeaders()
196
+ );
197
+ return assertApiResponse(envelope, "upload/url");
198
+ }
199
+ /**
200
+ * PUT preSignedURL — direct S3 upload, not a SafeCDX API route.
201
+ * preSignedURL comes from createUploadUrl() response: Data.preSignedURL.
202
+ *
203
+ * This intentionally does not use the shared HttpClient because the existing
204
+ * HttpClient.put() is JSON-oriented and stringifies the request body.
205
+ */
206
+ async uploadImage(preSignedURL, image, contentType = "image/jpeg") {
207
+ const response = await fetch(preSignedURL, {
208
+ method: "PUT",
209
+ headers: {
210
+ "Content-Type": contentType
211
+ },
212
+ body: image
213
+ });
214
+ if (!response.ok) {
215
+ let body;
216
+ try {
217
+ body = (await response.text()).slice(0, 500);
218
+ } catch {
219
+ }
220
+ throw new SafeCDXError(
221
+ `Image upload failed with HTTP ${response.status}.`,
222
+ {
223
+ status: response.status,
224
+ body
225
+ }
226
+ );
227
+ }
228
+ }
229
+ // ---------------------------------------------------------------------------
230
+ // CVML
231
+ // ---------------------------------------------------------------------------
232
+ /**
233
+ * POST cvml/status
234
+ * Updates the ML processing status for a captured image.
235
+ * NOTE: Returns raw service result — no ApiResponse wrapper on this endpoint.
236
+ */
237
+ async updateCvmlStatus(imageOfCaptureId, cvmlStatus) {
238
+ const payload = {
239
+ ImageOfCaptureId: imageOfCaptureId,
240
+ CvmlStatus: cvmlStatus
241
+ };
242
+ return this.http.post(
243
+ this.url("cvml/status"),
244
+ wrapData(payload),
245
+ this.getJsonHeaders()
246
+ );
247
+ }
248
+ /**
249
+ * GET cvml/results
250
+ * Polls ML analysis results for a captured image.
251
+ * NOTE: Returns raw service result — no ApiResponse wrapper on this endpoint.
252
+ */
253
+ async getCvmlResults(imageOfCaptureId) {
254
+ return this.http.get(
255
+ this.url("cvml/results", {
256
+ imageOfCaptureId
257
+ }),
258
+ this.getAuthHeaders()
259
+ );
260
+ }
261
+ // ---------------------------------------------------------------------------
262
+ // Test Results
263
+ // ---------------------------------------------------------------------------
264
+ /**
265
+ * POST test/result/pending
266
+ * Returns test results in a pending/incomplete state.
267
+ */
268
+ async getPendingResults(payload = {
269
+ ExcludeStatus: "Started"
270
+ }) {
271
+ const envelope = await this.http.post(
272
+ this.url("test/result/pending"),
273
+ wrapData(payload),
274
+ this.getJsonHeaders()
275
+ );
276
+ return assertApiResponse(envelope, "test/result/pending");
277
+ }
278
+ /**
279
+ * POST test/result/last
280
+ * Returns the most recent test result for the authenticated patient.
281
+ */
282
+ async getLastResults(payload = {}) {
283
+ const envelope = await this.http.post(
284
+ this.url("test/result/last"),
285
+ wrapData(payload),
286
+ this.getJsonHeaders()
287
+ );
288
+ return assertApiResponse(envelope, "test/result/last");
289
+ }
290
+ /**
291
+ * POST test/result/history
292
+ * Returns the full test history for the authenticated patient.
293
+ */
294
+ async getTestHistory(payload = {}) {
295
+ const envelope = await this.http.post(
296
+ this.url("test/result/history"),
297
+ wrapData(payload),
298
+ this.getJsonHeaders()
299
+ );
300
+ return assertApiResponse(envelope, "test/result/history");
301
+ }
302
+ /**
303
+ * POST test/result/details
304
+ * Returns detailed result data for a specific test attempt.
305
+ * NOTE: Returns raw service result — no ApiResponse wrapper on this endpoint.
306
+ */
307
+ async getResultDetails(userTestResultId) {
308
+ const payload = {
309
+ UserTestResultId: userTestResultId
310
+ };
311
+ return this.http.post(
312
+ this.url("test/result/details"),
313
+ wrapData(payload),
314
+ this.getJsonHeaders()
315
+ );
316
+ }
317
+ /**
318
+ * POST test/result/pdf
319
+ * Generates a PDF report for a specific test result.
320
+ */
321
+ async getResultPdf(payload) {
322
+ const envelope = await this.http.post(
323
+ this.url("test/result/pdf"),
324
+ wrapData(payload),
325
+ this.getJsonHeaders()
326
+ );
327
+ return assertApiResponse(envelope, "test/result/pdf");
328
+ }
329
+ /**
330
+ * POST test/result/image/capture
331
+ * Returns the image capture URL for a specific test result.
332
+ * NOTE: Returns raw service result — no ApiResponse wrapper on this endpoint.
333
+ */
334
+ async getImageCaptureUrl(userTestResultId) {
335
+ const payload = {
336
+ UserTestResultId: userTestResultId
337
+ };
338
+ return this.http.post(
339
+ this.url("test/result/image/capture"),
340
+ wrapData(payload),
341
+ this.getJsonHeaders()
342
+ );
343
+ }
344
+ /**
345
+ * POST test/result/analytics
346
+ * Returns analytics data for test results.
347
+ */
348
+ async getAnalytics(payload = {}) {
349
+ const envelope = await this.http.post(
350
+ this.url("test/result/analytics"),
351
+ wrapData(payload),
352
+ this.getJsonHeaders()
353
+ );
354
+ return assertApiResponse(envelope, "test/result/analytics");
355
+ }
356
+ // ---------------------------------------------------------------------------
357
+ // Resume / Answers / Finalize
358
+ // ---------------------------------------------------------------------------
359
+ /**
360
+ * POST test/resume
361
+ * Marks a test attempt as resumed (or not).
362
+ */
363
+ async resumeFlow(userTestResultId, resumed = true) {
364
+ const payload = {
365
+ UserTestResultId: userTestResultId,
366
+ Resumed: resumed
367
+ };
368
+ const envelope = await this.http.post(
369
+ this.url("test/resume"),
370
+ wrapData(payload),
371
+ this.getJsonHeaders()
372
+ );
373
+ return assertApiResponse(envelope, "test/resume");
374
+ }
375
+ /**
376
+ * POST test/answers
377
+ * Submits analyte answers for a test attempt.
378
+ */
379
+ async submitAnswers(payload) {
380
+ const envelope = await this.http.post(
381
+ this.url("test/answers"),
382
+ wrapData(payload),
383
+ this.getJsonHeaders()
384
+ );
385
+ return assertApiResponse(envelope, "test/answers");
386
+ }
387
+ /**
388
+ * POST test/finalize
389
+ * Finalizes a test attempt with CTA, indication, and decision results.
390
+ */
391
+ async finalizeTest(payload) {
392
+ const envelope = await this.http.post(
393
+ this.url("test/finalize"),
394
+ wrapData(payload),
395
+ this.getJsonHeaders()
396
+ );
397
+ return assertApiResponse(envelope, "test/finalize");
398
+ }
399
+ };
400
+
401
+ // src/errors.ts
402
+ var ConfigError = class extends Error {
403
+ constructor(message) {
404
+ super(message);
405
+ this.name = "ConfigError";
406
+ }
407
+ };
408
+ // Annotate the CommonJS export names for ESM import in node:
409
+ 0 && (module.exports = {
410
+ ConfigError,
411
+ HCSafeCDXClient
412
+ });