@nsshunt/stsfhirclient 2.0.29 → 2.0.31

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.
@@ -9166,325 +9166,6 @@ function splitUnderBase(pathname, basePath) {
9166
9166
  return pathname.slice(basePath.length).split("/").filter(Boolean);
9167
9167
  }
9168
9168
  //#endregion
9169
- //#region src/retryAxiosClient.ts
9170
- function sleep(ms) {
9171
- return new Promise((resolve) => setTimeout(resolve, ms));
9172
- }
9173
- function getJitteredDelay(base, jitter) {
9174
- return base + Math.floor(Math.random() * jitter);
9175
- }
9176
- function createRetryAxiosClient(retryConfig) {
9177
- const { maxRetries = 3, retryDelayMs = 300, retryJitterMs = 100, retryMethods = [
9178
- "get",
9179
- "head",
9180
- "put",
9181
- "delete",
9182
- "options"
9183
- ], retryStatusCodes = [
9184
- 408,
9185
- 429,
9186
- 500,
9187
- 502,
9188
- 503,
9189
- 504
9190
- ], retryErrorCodes = [
9191
- "ECONNRESET",
9192
- "ETIMEDOUT",
9193
- "EAI_AGAIN",
9194
- "ECONNREFUSED",
9195
- "ENOTFOUND",
9196
- "EHOSTUNREACH",
9197
- "EPIPE"
9198
- ], maxRetryDurationMs = 1e4, onRetryAttempt, stsAxiosConfig } = retryConfig || {};
9199
- const instance = axios.default.create();
9200
- instance.interceptors.request.use((config) => {
9201
- const retryConfig = config;
9202
- if (!retryConfig.metadata) retryConfig.metadata = {
9203
- startTime: Date.now(),
9204
- __retryCount: 0
9205
- };
9206
- if (stsAxiosConfig) Object.assign(config, stsAxiosConfig.config);
9207
- return config;
9208
- });
9209
- instance.interceptors.response.use((response) => response, async (error) => {
9210
- const config = error.config;
9211
- if (!config || !config.method || !config.metadata) return Promise.reject(error);
9212
- const elapsed = Date.now() - config.metadata.startTime;
9213
- const method = config.method.toLowerCase();
9214
- const status = error.response?.status;
9215
- const code = error.code ?? "";
9216
- const isRetryableMethod = retryMethods.includes(method);
9217
- const isRetryableStatus = retryStatusCodes.includes(status ?? 0);
9218
- const isRetryableCode = retryErrorCodes.includes(code);
9219
- if (isRetryableMethod && (isRetryableStatus || isRetryableCode) && config.metadata.__retryCount < maxRetries && elapsed < maxRetryDurationMs) {
9220
- config.metadata.__retryCount += 1;
9221
- const delayMs = getJitteredDelay(retryDelayMs * Math.pow(2, config.metadata.__retryCount - 1), retryJitterMs);
9222
- onRetryAttempt?.(config.metadata.__retryCount, error, delayMs);
9223
- await sleep(delayMs);
9224
- return instance(config);
9225
- }
9226
- return Promise.reject(error);
9227
- });
9228
- return instance;
9229
- }
9230
- //#endregion
9231
- //#region src/cv2/fhirRESTClient.ts
9232
- var FhirRESTClient = class {
9233
- #options;
9234
- constructor(options) {
9235
- this.#options = options;
9236
- }
9237
- BaseUrl() {
9238
- return `${this.#options.endpoint}:${this.#options.stsfhirport}${this.#options.stsfhirapiroot}`;
9239
- }
9240
- LogError = (method, error) => {
9241
- this.#options.logger.error(`FhirRESTClient:${method}(): Error: [${error}]`);
9242
- this.#options.logger.error(error);
9243
- console.error(error);
9244
- process.exit(0);
9245
- };
9246
- LogWarning = (method, message) => {
9247
- this.#options.logger.warn(`FhirRESTClient:${method}(): Warn: [${message}]`);
9248
- };
9249
- /**
9250
- *
9251
- * @param url
9252
- * @param httpVerb
9253
- * @param versionIdETag Format "W/<versionNumber", e.g. "W/3"
9254
- * @param resource
9255
- * @returns
9256
- */
9257
- #InvokeResourceAPI = async (url, httpVerb, versionIdETag, resource, contentType) => {
9258
- const accessToken = await this.#options.GetAccessToken();
9259
- let requestConfig;
9260
- const headers = {
9261
- Prefer: "return=representation",
9262
- Accept: "application/fhir+json"
9263
- };
9264
- if (versionIdETag && versionIdETag !== "") headers["If-Match"] = versionIdETag;
9265
- if (httpVerb === "patch") {
9266
- requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationMergePatchAndJsonContentTypeHeaders();
9267
- if (resource && contentType) {
9268
- if (contentType.localeCompare("application/merge-patch+json") === 0) requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationMergePatchAndJsonContentTypeHeaders();
9269
- else if (contentType.localeCompare("application/json-patch+json") === 0) requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationJsonPatchAndJsonContentTypeHeaders();
9270
- }
9271
- } else requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationFhirAndJsonContentTypeHeaders();
9272
- requestConfig.withAuthHeaders(accessToken, this.#options.user).withHeaders(headers);
9273
- if (resource) requestConfig.withData(resource);
9274
- if (this.#options.agentManager) requestConfig.withAgentManager(this.#options.agentManager);
9275
- return await createRetryAxiosClient({
9276
- maxRetries: 4,
9277
- retryDelayMs: 300,
9278
- retryJitterMs: 150,
9279
- maxRetryDurationMs: 5e3,
9280
- onRetryAttempt: (attempt, error, delayMs) => {
9281
- this.LogWarning("#InvokeResourceAPI", `Retry #${attempt} after ${delayMs}ms due to ${error.code || error.response?.status}`);
9282
- if (this.#options.onRetryAttempt) this.#options.onRetryAttempt(attempt, error, delayMs);
9283
- }
9284
- })(url, requestConfig.config);
9285
- };
9286
- ProcessBatchOrTransactionResources = async (bundle) => {
9287
- try {
9288
- let url = `${this.BaseUrl()}`;
9289
- const retVal = await this.#InvokeResourceAPI(url, "post", null, bundle);
9290
- if (retVal) return {
9291
- status: retVal.status,
9292
- body: retVal.data
9293
- };
9294
- else throw new Error(`FhirRESTClient:ProcessBatchOrTransactionResources(): No response from #InvokeResourceAPI`);
9295
- } catch (error) {
9296
- this.LogError("ProcessBatchOrTransactionResources", error);
9297
- throw error;
9298
- }
9299
- };
9300
- GetResources = async (searchUrl) => {
9301
- try {
9302
- const retVal = await this.#InvokeResourceAPI(searchUrl.toString(), "get", null, null);
9303
- if (retVal) return {
9304
- status: retVal.status,
9305
- body: retVal.data
9306
- };
9307
- else throw new Error(`FhirRESTClient:GetResources(): No response from #InvokeResourceAPI`);
9308
- } catch (error) {
9309
- this.LogError("GetResources", error);
9310
- throw error;
9311
- }
9312
- };
9313
- GetHistoryFhirResources = async (searchUrl) => {
9314
- try {
9315
- return await this.GetResources(searchUrl);
9316
- } catch (error) {
9317
- this.LogError("GetHistoryFhirResources", error);
9318
- throw error;
9319
- }
9320
- };
9321
- GetHistoryFhirResource = async (searchUrl) => {
9322
- try {
9323
- return await this.GetResources(searchUrl);
9324
- } catch (error) {
9325
- this.LogError("GetHistoryFhirResource", error);
9326
- throw error;
9327
- }
9328
- };
9329
- GetAllFhirResources = async (searchUrl) => {
9330
- try {
9331
- return await this.GetResources(searchUrl);
9332
- } catch (error) {
9333
- this.LogError("GetAllFhirResources", error);
9334
- throw error;
9335
- }
9336
- };
9337
- GetResource = async (resourceType, resourceId) => {
9338
- try {
9339
- let url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}`;
9340
- const retVal = await this.#InvokeResourceAPI(url, "get", null, null);
9341
- if (retVal) return {
9342
- status: retVal.status,
9343
- body: retVal.data
9344
- };
9345
- else throw new Error(`FhirRESTClient:GetResource(): No response from #InvokeResourceAPI`);
9346
- } catch (error) {
9347
- if (axios.default.isAxiosError(error)) {
9348
- const axiosError = error;
9349
- return {
9350
- status: axiosError.response.status,
9351
- body: axiosError.response.data,
9352
- headers: { ETag: axiosError.response.headers["etag"] }
9353
- };
9354
- } else throw error;
9355
- }
9356
- };
9357
- PutResource = async (resourceType, domainResource, versionIdETag) => {
9358
- try {
9359
- let url = `${`${this.BaseUrl()}/${resourceType}`}/${domainResource.id}`;
9360
- const retVal = await this.#InvokeResourceAPI(url, "put", versionIdETag, domainResource);
9361
- if (retVal) return {
9362
- status: retVal.status,
9363
- body: retVal.data
9364
- };
9365
- else throw new Error(`FhirRESTClient:PutResource(): No response from #InvokeResourceAPI`);
9366
- } catch (error) {
9367
- this.LogError("PutResource", error);
9368
- throw error;
9369
- }
9370
- };
9371
- PatchResource = async (resourceType, resourceId, domainResource, contentType, versionIdETag) => {
9372
- try {
9373
- let url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}`;
9374
- const retVal = await this.#InvokeResourceAPI(url, "patch", versionIdETag, domainResource, contentType);
9375
- if (retVal) return {
9376
- status: retVal.status,
9377
- body: retVal.data
9378
- };
9379
- else throw new Error(`FhirRESTClient:PatchResource(): No response from #InvokeResourceAPI`);
9380
- } catch (error) {
9381
- this.LogError("PatchResource", error);
9382
- throw error;
9383
- }
9384
- };
9385
- PostResource = async (resourceType, domainResource) => {
9386
- try {
9387
- const url = `${this.BaseUrl()}/${resourceType}`;
9388
- const retVal = await this.#InvokeResourceAPI(url, "post", null, domainResource);
9389
- if (retVal) return {
9390
- status: retVal.status,
9391
- body: retVal.data
9392
- };
9393
- else throw new Error(`FhirRESTClient:PostResource(): No response from #InvokeResourceAPI`);
9394
- } catch (error) {
9395
- this.LogError("PostResource", error);
9396
- throw error;
9397
- }
9398
- };
9399
- GetHistoryInstanceFhirResource = async (resourceType, resourceId, versionId) => {
9400
- try {
9401
- const url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}/_history/${versionId.toString()}`;
9402
- const retVal = await this.#InvokeResourceAPI(url, "get", null, null);
9403
- if (retVal) return {
9404
- status: retVal.status,
9405
- body: retVal.data
9406
- };
9407
- else throw new Error(`FhirRESTClient:GetHistoryInstanceFhirResource(): No response from #InvokeResourceAPI`);
9408
- } catch (error) {
9409
- if (axios.default.isAxiosError(error)) {
9410
- const axiosError = error;
9411
- return {
9412
- status: axiosError.response.status,
9413
- body: axiosError.response.data,
9414
- headers: { ETag: axiosError.response.headers["etag"] }
9415
- };
9416
- } else throw error;
9417
- }
9418
- };
9419
- ExtractNumber(str) {
9420
- if (typeof str !== "string") return null;
9421
- const match = str.match(/^\s*(?:W\/\s*)?"\s*([\d.]+)\s*"$/i);
9422
- return match ? Number(match[1]) : null;
9423
- }
9424
- DeleteResource = async (resourceType, resourceId, versionIdETag) => {
9425
- try {
9426
- let url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}`;
9427
- const retVal = await this.#InvokeResourceAPI(url, "delete", versionIdETag, null);
9428
- if (retVal) return {
9429
- status: retVal.status,
9430
- body: retVal.data
9431
- };
9432
- else throw new Error(`FhirRESTClient:DeleteResource(): No response from #InvokeResourceAPI`);
9433
- } catch (error) {
9434
- if (axios.default.isAxiosError(error)) {
9435
- const axiosError = error;
9436
- const versionNumber = this.ExtractNumber(versionIdETag) + 1;
9437
- return {
9438
- status: axiosError.response.status,
9439
- body: axiosError.response.data,
9440
- headers: { ETag: `W/"${versionNumber}"` }
9441
- };
9442
- } else throw error;
9443
- }
9444
- };
9445
- GetMetadata = async () => {
9446
- try {
9447
- const resourceUrl = `${this.BaseUrl()}/metadata`;
9448
- const retVal = await this.#InvokeResourceAPI(resourceUrl, "get", null, null);
9449
- if (retVal) return {
9450
- status: retVal.status,
9451
- body: retVal.data
9452
- };
9453
- else throw new Error(`FhirRESTClient:GetMetadata(): No response from #InvokeResourceAPI`);
9454
- } catch (error) {
9455
- this.LogError("GetMetadata", error);
9456
- throw error;
9457
- }
9458
- };
9459
- GetLatency = async (params) => {
9460
- try {
9461
- const url = `${this.BaseUrl()}/latency`;
9462
- const retVal = await this.#InvokeResourceAPI(url, "get", null, null);
9463
- if (retVal) return {
9464
- status: retVal.status,
9465
- body: retVal.data
9466
- };
9467
- else throw new Error(`FhirRESTClient:GetLatency(): No response from #InvokeResourceAPI`);
9468
- } catch (error) {
9469
- this.LogError("GetLatency", error);
9470
- throw error;
9471
- }
9472
- };
9473
- GenericSearchTesting = async (resourceType, params) => {
9474
- try {
9475
- const retVal = await this.#InvokeResourceAPI(`${this.BaseUrl()}/${resourceType}/_genericSearchTesting`, "post", null, params);
9476
- if (retVal) return {
9477
- status: retVal.status,
9478
- body: retVal.data
9479
- };
9480
- else throw new Error(`FhirRESTClient:GenericSearchTesting(): No response from #InvokeResourceAPI`);
9481
- } catch (error) {
9482
- this.LogError("GenericSearchTesting", error);
9483
- throw error;
9484
- }
9485
- };
9486
- };
9487
- //#endregion
9488
9169
  //#region node_modules/chalk/source/vendor/ansi-styles/index.js
9489
9170
  var ANSI_BACKGROUND_OFFSET = 10;
9490
9171
  var wrapAnsi16 = (offset = 0) => (code) => `\u001B[${code + offset}m`;
@@ -9775,50 +9456,315 @@ var proto = Object.defineProperties(() => {}, {
9775
9456
  set(level) {
9776
9457
  this[GENERATOR].level = level;
9777
9458
  }
9778
- }
9779
- });
9780
- var createStyler = (open, close, parent) => {
9781
- let openAll;
9782
- let closeAll;
9783
- if (parent === void 0) {
9784
- openAll = open;
9785
- closeAll = close;
9786
- } else {
9787
- openAll = parent.openAll + open;
9788
- closeAll = close + parent.closeAll;
9789
- }
9790
- return {
9791
- open,
9792
- close,
9793
- openAll,
9794
- closeAll,
9795
- parent
9459
+ }
9460
+ });
9461
+ var createStyler = (open, close, parent) => {
9462
+ let openAll;
9463
+ let closeAll;
9464
+ if (parent === void 0) {
9465
+ openAll = open;
9466
+ closeAll = close;
9467
+ } else {
9468
+ openAll = parent.openAll + open;
9469
+ closeAll = close + parent.closeAll;
9470
+ }
9471
+ return {
9472
+ open,
9473
+ close,
9474
+ openAll,
9475
+ closeAll,
9476
+ parent
9477
+ };
9478
+ };
9479
+ var createBuilder = (self, _styler, _isEmpty) => {
9480
+ const builder = (...arguments_) => applyStyle(builder, arguments_.length === 1 ? "" + arguments_[0] : arguments_.join(" "));
9481
+ Object.setPrototypeOf(builder, proto);
9482
+ builder[GENERATOR] = self;
9483
+ builder[STYLER] = _styler;
9484
+ builder[IS_EMPTY] = _isEmpty;
9485
+ return builder;
9486
+ };
9487
+ var applyStyle = (self, string) => {
9488
+ if (self.level <= 0 || !string) return self[IS_EMPTY] ? "" : string;
9489
+ let styler = self[STYLER];
9490
+ if (styler === void 0) return string;
9491
+ const { openAll, closeAll } = styler;
9492
+ if (string.includes("\x1B")) while (styler !== void 0) {
9493
+ string = stringReplaceAll(string, styler.close, styler.open);
9494
+ styler = styler.parent;
9495
+ }
9496
+ const lfIndex = string.indexOf("\n");
9497
+ if (lfIndex !== -1) string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
9498
+ return openAll + string + closeAll;
9499
+ };
9500
+ Object.defineProperties(createChalk.prototype, styles);
9501
+ var chalk = createChalk();
9502
+ createChalk({ level: stderrColor ? stderrColor.level : 0 });
9503
+ //#endregion
9504
+ //#region src/cv2/fhirRESTClient.ts
9505
+ var FhirRESTClient = class {
9506
+ #options;
9507
+ constructor(options) {
9508
+ this.#options = options;
9509
+ }
9510
+ BaseUrl() {
9511
+ return `${this.#options.endpoint}:${this.#options.stsfhirport}${this.#options.stsfhirapiroot}`;
9512
+ }
9513
+ HandleError = (error) => {
9514
+ this.#options.logger.error(chalk.red(`HandleError(): Error: [${error}]`));
9515
+ if (axios.default.isAxiosError(error)) {
9516
+ const axiosError = error;
9517
+ if (axiosError.response) {
9518
+ this.#options.logger.error(chalk.red(`AXIOS Error Response.Status = [${axiosError.response.status}]`));
9519
+ if (axiosError.response.headers) this.#options.logger.error(chalk.red(` headers: [${JSON.stringify(axiosError.response.headers)}]`));
9520
+ if (axiosError.response.data) this.#options.logger.error(chalk.red(` data: [${JSON.stringify(axiosError.response.data)}]`));
9521
+ try {
9522
+ if (axiosError.response.config) this.#options.logger.error(chalk.red(` config: [${JSON.stringify(axiosError.response.config)}]`));
9523
+ } catch (innererror) {
9524
+ this.#options.logger.error(chalk.red(` could not get response config, error: [${innererror}]`));
9525
+ }
9526
+ } else this.#options.logger.error(chalk.red(`AXIOS Error = [${axiosError}]`));
9527
+ }
9528
+ };
9529
+ LogError = (method, error) => {
9530
+ this.#options.logger.error(`FhirRESTClient:${method}(): Error: [${error}]`);
9531
+ this.#options.logger.error(error);
9532
+ console.error(error);
9533
+ this.HandleError(error);
9534
+ process.exit(0);
9535
+ };
9536
+ LogWarning = (method, message) => {
9537
+ this.#options.logger.warn(`FhirRESTClient:${method}(): Warn: [${message}]`);
9538
+ };
9539
+ /**
9540
+ *
9541
+ * @param url
9542
+ * @param httpVerb
9543
+ * @param versionIdETag Format "W/<versionNumber", e.g. "W/3"
9544
+ * @param resource
9545
+ * @returns
9546
+ */
9547
+ #InvokeResourceAPI = async (url, httpVerb, versionIdETag, resource, contentType) => {
9548
+ const accessToken = await this.#options.GetAccessToken();
9549
+ let requestConfig;
9550
+ const headers = {
9551
+ Prefer: "return=representation",
9552
+ Accept: "application/fhir+json"
9553
+ };
9554
+ if (versionIdETag && versionIdETag !== "") headers["If-Match"] = versionIdETag;
9555
+ if (httpVerb === "patch") {
9556
+ requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationMergePatchAndJsonContentTypeHeaders();
9557
+ if (resource && contentType) {
9558
+ if (contentType.localeCompare("application/merge-patch+json") === 0) requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationMergePatchAndJsonContentTypeHeaders();
9559
+ else if (contentType.localeCompare("application/json-patch+json") === 0) requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationJsonPatchAndJsonContentTypeHeaders();
9560
+ }
9561
+ } else requestConfig = new _nsshunt_stsutils.STSAxiosConfig(url, httpVerb).withApplicationFhirAndJsonContentTypeHeaders();
9562
+ requestConfig.withAuthHeaders(accessToken, this.#options.user).withHeaders(headers);
9563
+ if (resource) requestConfig.withData(resource);
9564
+ if (this.#options.agentManager) requestConfig.withAgentManager(this.#options.agentManager);
9565
+ return await (0, axios.default)(requestConfig.config);
9566
+ };
9567
+ ProcessBatchOrTransactionResources = async (bundle) => {
9568
+ try {
9569
+ let url = `${this.BaseUrl()}`;
9570
+ const retVal = await this.#InvokeResourceAPI(url, "post", null, bundle);
9571
+ if (retVal) return {
9572
+ status: retVal.status,
9573
+ body: retVal.data
9574
+ };
9575
+ else throw new Error(`FhirRESTClient:ProcessBatchOrTransactionResources(): No response from #InvokeResourceAPI`);
9576
+ } catch (error) {
9577
+ this.LogError("ProcessBatchOrTransactionResources", error);
9578
+ throw error;
9579
+ }
9580
+ };
9581
+ GetResources = async (searchUrl) => {
9582
+ try {
9583
+ const retVal = await this.#InvokeResourceAPI(searchUrl.toString(), "get", null, null);
9584
+ if (retVal) return {
9585
+ status: retVal.status,
9586
+ body: retVal.data
9587
+ };
9588
+ else throw new Error(`FhirRESTClient:GetResources(): No response from #InvokeResourceAPI`);
9589
+ } catch (error) {
9590
+ this.LogError("GetResources", error);
9591
+ throw error;
9592
+ }
9593
+ };
9594
+ GetHistoryFhirResources = async (searchUrl) => {
9595
+ try {
9596
+ return await this.GetResources(searchUrl);
9597
+ } catch (error) {
9598
+ this.LogError("GetHistoryFhirResources", error);
9599
+ throw error;
9600
+ }
9601
+ };
9602
+ GetHistoryFhirResource = async (searchUrl) => {
9603
+ try {
9604
+ return await this.GetResources(searchUrl);
9605
+ } catch (error) {
9606
+ this.LogError("GetHistoryFhirResource", error);
9607
+ throw error;
9608
+ }
9609
+ };
9610
+ GetAllFhirResources = async (searchUrl) => {
9611
+ try {
9612
+ return await this.GetResources(searchUrl);
9613
+ } catch (error) {
9614
+ this.LogError("GetAllFhirResources", error);
9615
+ throw error;
9616
+ }
9796
9617
  };
9797
- };
9798
- var createBuilder = (self, _styler, _isEmpty) => {
9799
- const builder = (...arguments_) => applyStyle(builder, arguments_.length === 1 ? "" + arguments_[0] : arguments_.join(" "));
9800
- Object.setPrototypeOf(builder, proto);
9801
- builder[GENERATOR] = self;
9802
- builder[STYLER] = _styler;
9803
- builder[IS_EMPTY] = _isEmpty;
9804
- return builder;
9805
- };
9806
- var applyStyle = (self, string) => {
9807
- if (self.level <= 0 || !string) return self[IS_EMPTY] ? "" : string;
9808
- let styler = self[STYLER];
9809
- if (styler === void 0) return string;
9810
- const { openAll, closeAll } = styler;
9811
- if (string.includes("\x1B")) while (styler !== void 0) {
9812
- string = stringReplaceAll(string, styler.close, styler.open);
9813
- styler = styler.parent;
9618
+ GetResource = async (resourceType, resourceId) => {
9619
+ try {
9620
+ let url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}`;
9621
+ const retVal = await this.#InvokeResourceAPI(url, "get", null, null);
9622
+ if (retVal) return {
9623
+ status: retVal.status,
9624
+ body: retVal.data
9625
+ };
9626
+ else throw new Error(`FhirRESTClient:GetResource(): No response from #InvokeResourceAPI`);
9627
+ } catch (error) {
9628
+ if (axios.default.isAxiosError(error)) {
9629
+ const axiosError = error;
9630
+ return {
9631
+ status: axiosError.response.status,
9632
+ body: axiosError.response.data,
9633
+ headers: { ETag: axiosError.response.headers["etag"] }
9634
+ };
9635
+ } else throw error;
9636
+ }
9637
+ };
9638
+ PutResource = async (resourceType, domainResource, versionIdETag) => {
9639
+ try {
9640
+ let url = `${`${this.BaseUrl()}/${resourceType}`}/${domainResource.id}`;
9641
+ const retVal = await this.#InvokeResourceAPI(url, "put", versionIdETag, domainResource);
9642
+ if (retVal) return {
9643
+ status: retVal.status,
9644
+ body: retVal.data
9645
+ };
9646
+ else throw new Error(`FhirRESTClient:PutResource(): No response from #InvokeResourceAPI`);
9647
+ } catch (error) {
9648
+ this.LogError("PutResource", error);
9649
+ throw error;
9650
+ }
9651
+ };
9652
+ PatchResource = async (resourceType, resourceId, domainResource, contentType, versionIdETag) => {
9653
+ try {
9654
+ let url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}`;
9655
+ const retVal = await this.#InvokeResourceAPI(url, "patch", versionIdETag, domainResource, contentType);
9656
+ if (retVal) return {
9657
+ status: retVal.status,
9658
+ body: retVal.data
9659
+ };
9660
+ else throw new Error(`FhirRESTClient:PatchResource(): No response from #InvokeResourceAPI`);
9661
+ } catch (error) {
9662
+ this.LogError("PatchResource", error);
9663
+ throw error;
9664
+ }
9665
+ };
9666
+ PostResource = async (resourceType, domainResource) => {
9667
+ try {
9668
+ const url = `${this.BaseUrl()}/${resourceType}`;
9669
+ const retVal = await this.#InvokeResourceAPI(url, "post", null, domainResource);
9670
+ if (retVal) return {
9671
+ status: retVal.status,
9672
+ body: retVal.data
9673
+ };
9674
+ else throw new Error(`FhirRESTClient:PostResource(): No response from #InvokeResourceAPI`);
9675
+ } catch (error) {
9676
+ this.LogError("PostResource", error);
9677
+ throw error;
9678
+ }
9679
+ };
9680
+ GetHistoryInstanceFhirResource = async (resourceType, resourceId, versionId) => {
9681
+ try {
9682
+ const url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}/_history/${versionId.toString()}`;
9683
+ const retVal = await this.#InvokeResourceAPI(url, "get", null, null);
9684
+ if (retVal) return {
9685
+ status: retVal.status,
9686
+ body: retVal.data
9687
+ };
9688
+ else throw new Error(`FhirRESTClient:GetHistoryInstanceFhirResource(): No response from #InvokeResourceAPI`);
9689
+ } catch (error) {
9690
+ if (axios.default.isAxiosError(error)) {
9691
+ const axiosError = error;
9692
+ return {
9693
+ status: axiosError.response.status,
9694
+ body: axiosError.response.data,
9695
+ headers: { ETag: axiosError.response.headers["etag"] }
9696
+ };
9697
+ } else throw error;
9698
+ }
9699
+ };
9700
+ ExtractNumber(str) {
9701
+ if (typeof str !== "string") return null;
9702
+ const match = str.match(/^\s*(?:W\/\s*)?"\s*([\d.]+)\s*"$/i);
9703
+ return match ? Number(match[1]) : null;
9814
9704
  }
9815
- const lfIndex = string.indexOf("\n");
9816
- if (lfIndex !== -1) string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
9817
- return openAll + string + closeAll;
9705
+ DeleteResource = async (resourceType, resourceId, versionIdETag) => {
9706
+ try {
9707
+ let url = `${`${this.BaseUrl()}/${resourceType}`}/${resourceId}`;
9708
+ const retVal = await this.#InvokeResourceAPI(url, "delete", versionIdETag, null);
9709
+ if (retVal) return {
9710
+ status: retVal.status,
9711
+ body: retVal.data
9712
+ };
9713
+ else throw new Error(`FhirRESTClient:DeleteResource(): No response from #InvokeResourceAPI`);
9714
+ } catch (error) {
9715
+ if (axios.default.isAxiosError(error)) {
9716
+ const axiosError = error;
9717
+ const versionNumber = this.ExtractNumber(versionIdETag) + 1;
9718
+ return {
9719
+ status: axiosError.response.status,
9720
+ body: axiosError.response.data,
9721
+ headers: { ETag: `W/"${versionNumber}"` }
9722
+ };
9723
+ } else throw error;
9724
+ }
9725
+ };
9726
+ GetMetadata = async () => {
9727
+ try {
9728
+ const resourceUrl = `${this.BaseUrl()}/metadata`;
9729
+ const retVal = await this.#InvokeResourceAPI(resourceUrl, "get", null, null);
9730
+ if (retVal) return {
9731
+ status: retVal.status,
9732
+ body: retVal.data
9733
+ };
9734
+ else throw new Error(`FhirRESTClient:GetMetadata(): No response from #InvokeResourceAPI`);
9735
+ } catch (error) {
9736
+ this.LogError("GetMetadata", error);
9737
+ throw error;
9738
+ }
9739
+ };
9740
+ GetLatency = async (params) => {
9741
+ try {
9742
+ const url = `${this.BaseUrl()}/latency`;
9743
+ const retVal = await this.#InvokeResourceAPI(url, "get", null, null);
9744
+ if (retVal) return {
9745
+ status: retVal.status,
9746
+ body: retVal.data
9747
+ };
9748
+ else throw new Error(`FhirRESTClient:GetLatency(): No response from #InvokeResourceAPI`);
9749
+ } catch (error) {
9750
+ this.LogError("GetLatency", error);
9751
+ throw error;
9752
+ }
9753
+ };
9754
+ GenericSearchTesting = async (resourceType, params) => {
9755
+ try {
9756
+ const retVal = await this.#InvokeResourceAPI(`${this.BaseUrl()}/${resourceType}/_genericSearchTesting`, "post", null, params);
9757
+ if (retVal) return {
9758
+ status: retVal.status,
9759
+ body: retVal.data
9760
+ };
9761
+ else throw new Error(`FhirRESTClient:GenericSearchTesting(): No response from #InvokeResourceAPI`);
9762
+ } catch (error) {
9763
+ this.LogError("GenericSearchTesting", error);
9764
+ throw error;
9765
+ }
9766
+ };
9818
9767
  };
9819
- Object.defineProperties(createChalk.prototype, styles);
9820
- var chalk = createChalk();
9821
- createChalk({ level: stderrColor ? stderrColor.level : 0 });
9822
9768
  //#endregion
9823
9769
  //#region node_modules/@nsshunt/stssocketioutils/dist/tiny-emitter-DcZ69ZvJ.js
9824
9770
  var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
@@ -13751,6 +13697,68 @@ var FhirSocketClientIndividual = class extends FhirSocketClient {
13751
13697
  };
13752
13698
  };
13753
13699
  //#endregion
13700
+ //#region src/retryAxiosClient.ts
13701
+ function sleep(ms) {
13702
+ return new Promise((resolve) => setTimeout(resolve, ms));
13703
+ }
13704
+ function getJitteredDelay(base, jitter) {
13705
+ return base + Math.floor(Math.random() * jitter);
13706
+ }
13707
+ function createRetryAxiosClient(retryConfig) {
13708
+ const { maxRetries = 3, retryDelayMs = 300, retryJitterMs = 100, retryMethods = [
13709
+ "get",
13710
+ "head",
13711
+ "put",
13712
+ "delete",
13713
+ "options"
13714
+ ], retryStatusCodes = [
13715
+ 408,
13716
+ 429,
13717
+ 500,
13718
+ 502,
13719
+ 503,
13720
+ 504
13721
+ ], retryErrorCodes = [
13722
+ "ECONNRESET",
13723
+ "ETIMEDOUT",
13724
+ "EAI_AGAIN",
13725
+ "ECONNREFUSED",
13726
+ "ENOTFOUND",
13727
+ "EHOSTUNREACH",
13728
+ "EPIPE"
13729
+ ], maxRetryDurationMs = 1e4, onRetryAttempt, stsAxiosConfig } = retryConfig || {};
13730
+ const instance = axios.default.create();
13731
+ instance.interceptors.request.use((config) => {
13732
+ const retryConfig = config;
13733
+ if (!retryConfig.metadata) retryConfig.metadata = {
13734
+ startTime: Date.now(),
13735
+ __retryCount: 0
13736
+ };
13737
+ if (stsAxiosConfig) Object.assign(config, stsAxiosConfig.config);
13738
+ return config;
13739
+ });
13740
+ instance.interceptors.response.use((response) => response, async (error) => {
13741
+ const config = error.config;
13742
+ if (!config || !config.method || !config.metadata) return Promise.reject(error);
13743
+ const elapsed = Date.now() - config.metadata.startTime;
13744
+ const method = config.method.toLowerCase();
13745
+ const status = error.response?.status;
13746
+ const code = error.code ?? "";
13747
+ const isRetryableMethod = retryMethods.includes(method);
13748
+ const isRetryableStatus = retryStatusCodes.includes(status ?? 0);
13749
+ const isRetryableCode = retryErrorCodes.includes(code);
13750
+ if (isRetryableMethod && (isRetryableStatus || isRetryableCode) && config.metadata.__retryCount < maxRetries && elapsed < maxRetryDurationMs) {
13751
+ config.metadata.__retryCount += 1;
13752
+ const delayMs = getJitteredDelay(retryDelayMs * Math.pow(2, config.metadata.__retryCount - 1), retryJitterMs);
13753
+ onRetryAttempt?.(config.metadata.__retryCount, error, delayMs);
13754
+ await sleep(delayMs);
13755
+ return instance(config);
13756
+ }
13757
+ return Promise.reject(error);
13758
+ });
13759
+ return instance;
13760
+ }
13761
+ //#endregion
13754
13762
  exports.FhirRESTClient = FhirRESTClient;
13755
13763
  exports.FhirRouteError = FhirRouteError;
13756
13764
  exports.FhirSocketClient = FhirSocketClient;