@healthcloudai/hc-safe-cdx 0.0.1 → 0.1.1

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