@medplum/core 0.9.12 → 0.9.15

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/cjs/index.js CHANGED
@@ -572,6 +572,165 @@
572
572
  function isLowerCase(c) {
573
573
  return c === c.toLowerCase();
574
574
  }
575
+ /**
576
+ * Tries to find a code string for a given system within a given codeable concept.
577
+ * @param concept The codeable concept.
578
+ * @param system The system string.
579
+ * @returns The code if found; otherwise undefined.
580
+ */
581
+ function getCodeBySystem(concept, system) {
582
+ var _a, _b;
583
+ return (_b = (_a = concept === null || concept === void 0 ? void 0 : concept.coding) === null || _a === void 0 ? void 0 : _a.find((coding) => coding.system === system)) === null || _b === void 0 ? void 0 : _b.code;
584
+ }
585
+ /**
586
+ * Sets a code for a given system within a given codeable concept.
587
+ * @param concept The codeable concept.
588
+ * @param system The system string.
589
+ * @param code The code value.
590
+ */
591
+ function setCodeBySystem(concept, system, code) {
592
+ var _a, _b;
593
+ if (!concept.coding) {
594
+ concept.coding = [];
595
+ }
596
+ const coding = (_a = concept.coding) === null || _a === void 0 ? void 0 : _a.find((c) => c.system === system);
597
+ if (coding) {
598
+ coding.code = code;
599
+ }
600
+ else {
601
+ (_b = concept.coding) === null || _b === void 0 ? void 0 : _b.push({ system, code });
602
+ }
603
+ }
604
+ /**
605
+ * Tries to find an observation interval for the given patient and value.
606
+ * @param definition The observation definition.
607
+ * @param patient The patient.
608
+ * @param value The observation value.
609
+ * @returns The observation interval if found; otherwise undefined.
610
+ */
611
+ function findObservationInterval(definition, patient, value, category) {
612
+ var _a;
613
+ return (_a = definition.qualifiedInterval) === null || _a === void 0 ? void 0 : _a.find((interval) => {
614
+ var _a;
615
+ return observationIntervalMatchesPatient(interval, patient) &&
616
+ observationIntervalMatchesValue(interval, value, (_a = definition.quantitativeDetails) === null || _a === void 0 ? void 0 : _a.decimalPrecision) &&
617
+ (category === undefined || interval.category === category);
618
+ });
619
+ }
620
+ /**
621
+ * Returns true if the patient matches the observation interval.
622
+ * @param interval The observation interval.
623
+ * @param patient The patient.
624
+ * @returns True if the patient matches the observation interval.
625
+ */
626
+ function observationIntervalMatchesPatient(interval, patient) {
627
+ return observationIntervalMatchesGender(interval, patient) && observationIntervalMatchesAge(interval, patient);
628
+ }
629
+ /**
630
+ * Returns true if the patient gender matches the observation interval.
631
+ * @param interval The observation interval.
632
+ * @param patient The patient.
633
+ * @returns True if the patient gender matches the observation interval.
634
+ */
635
+ function observationIntervalMatchesGender(interval, patient) {
636
+ return !interval.gender || interval.gender === patient.gender;
637
+ }
638
+ /**
639
+ * Returns true if the patient age matches the observation interval.
640
+ * @param interval The observation interval.
641
+ * @param patient The patient.
642
+ * @returns True if the patient age matches the observation interval.
643
+ */
644
+ function observationIntervalMatchesAge(interval, patient) {
645
+ return !interval.age || matchesRange(calculateAge(patient.birthDate).years, interval.age);
646
+ }
647
+ /**
648
+ * Returns true if the value matches the observation interval.
649
+ * @param interval The observation interval.
650
+ * @param value The observation value.
651
+ * @param precision Optional precision in number of digits.
652
+ * @returns True if the value matches the observation interval.
653
+ */
654
+ function observationIntervalMatchesValue(interval, value, precision) {
655
+ return !!interval.range && matchesRange(value, interval.range, precision);
656
+ }
657
+ /**
658
+ * Returns true if the value is in the range accounting for precision.
659
+ * @param value The numeric value.
660
+ * @param range The numeric range.
661
+ * @param precision Optional precision in number of digits.
662
+ * @returns True if the value is within the range.
663
+ */
664
+ function matchesRange(value, range, precision) {
665
+ var _a, _b;
666
+ return ((((_a = range.low) === null || _a === void 0 ? void 0 : _a.value) === undefined || preciseGreaterThanOrEquals(value, range.low.value, precision)) &&
667
+ (((_b = range.high) === null || _b === void 0 ? void 0 : _b.value) === undefined || preciseLessThanOrEquals(value, range.high.value, precision)));
668
+ }
669
+ /**
670
+ * Returns true if the two numbers are equal to the given precision.
671
+ * @param a The first number.
672
+ * @param b The second number.
673
+ * @param precision Optional precision in number of digits.
674
+ * @returns True if the two numbers are equal to the given precision.
675
+ */
676
+ function preciseEquals(a, b, precision) {
677
+ if (precision) {
678
+ return Math.abs(a - b) < Math.pow(10, -precision);
679
+ }
680
+ else {
681
+ return a === b;
682
+ }
683
+ }
684
+ /**
685
+ * Returns true if the first number is less than the second number to the given precision.
686
+ * @param a The first number.
687
+ * @param b The second number.
688
+ * @param precision Optional precision in number of digits.
689
+ * @returns True if the first number is less than the second number to the given precision.
690
+ */
691
+ function preciseLessThan(a, b, precision) {
692
+ if (precision) {
693
+ return a < b && Math.abs(a - b) > Math.pow(10, -precision);
694
+ }
695
+ else {
696
+ return a < b;
697
+ }
698
+ }
699
+ /**
700
+ * Returns true if the first number is greater than the second number to the given precision.
701
+ * @param a The first number.
702
+ * @param b The second number.
703
+ * @param precision Optional precision in number of digits.
704
+ * @returns True if the first number is greater than the second number to the given precision.
705
+ */
706
+ function preciseGreaterThan(a, b, precision) {
707
+ if (precision) {
708
+ return a > b && Math.abs(a - b) > Math.pow(10, -precision);
709
+ }
710
+ else {
711
+ return a > b;
712
+ }
713
+ }
714
+ /**
715
+ * Returns true if the first number is less than or equal to the second number to the given precision.
716
+ * @param a The first number.
717
+ * @param b The second number.
718
+ * @param precision Optional precision in number of digits.
719
+ * @returns True if the first number is less than or equal to the second number to the given precision.
720
+ */
721
+ function preciseLessThanOrEquals(a, b, precision) {
722
+ return preciseLessThan(a, b, precision) || preciseEquals(a, b, precision);
723
+ }
724
+ /**
725
+ * Returns true if the first number is greater than or equal to the second number to the given precision.
726
+ * @param a The first number.
727
+ * @param b The second number.
728
+ * @param precision Optional precision in number of digits.
729
+ * @returns True if the first number is greater than or equal to the second number to the given precision.
730
+ */
731
+ function preciseGreaterThanOrEquals(a, b, precision) {
732
+ return preciseGreaterThan(a, b, precision) || preciseEquals(a, b, precision);
733
+ }
575
734
 
576
735
  /**
577
736
  * Returns a cryptographically secure random string.
@@ -661,155 +820,6 @@
661
820
  return decodePayload(payload);
662
821
  }
663
822
 
664
- const OK_ID = 'ok';
665
- const CREATED_ID = 'created';
666
- const GONE_ID = 'gone';
667
- const NOT_MODIFIED_ID = 'not-modified';
668
- const NOT_FOUND_ID = 'not-found';
669
- const ACCESS_DENIED = 'access-denied';
670
- const allOk = {
671
- resourceType: 'OperationOutcome',
672
- id: OK_ID,
673
- issue: [
674
- {
675
- severity: 'information',
676
- code: 'information',
677
- details: {
678
- text: 'All OK',
679
- },
680
- },
681
- ],
682
- };
683
- const created = {
684
- resourceType: 'OperationOutcome',
685
- id: CREATED_ID,
686
- issue: [
687
- {
688
- severity: 'information',
689
- code: 'information',
690
- details: {
691
- text: 'Created',
692
- },
693
- },
694
- ],
695
- };
696
- const notModified = {
697
- resourceType: 'OperationOutcome',
698
- id: NOT_MODIFIED_ID,
699
- issue: [
700
- {
701
- severity: 'information',
702
- code: 'information',
703
- details: {
704
- text: 'Not Modified',
705
- },
706
- },
707
- ],
708
- };
709
- const notFound = {
710
- resourceType: 'OperationOutcome',
711
- id: NOT_FOUND_ID,
712
- issue: [
713
- {
714
- severity: 'error',
715
- code: 'not-found',
716
- details: {
717
- text: 'Not found',
718
- },
719
- },
720
- ],
721
- };
722
- const gone = {
723
- resourceType: 'OperationOutcome',
724
- id: GONE_ID,
725
- issue: [
726
- {
727
- severity: 'error',
728
- code: 'gone',
729
- details: {
730
- text: 'Gone',
731
- },
732
- },
733
- ],
734
- };
735
- const accessDenied = {
736
- resourceType: 'OperationOutcome',
737
- id: ACCESS_DENIED,
738
- issue: [
739
- {
740
- severity: 'error',
741
- code: 'access-denied',
742
- details: {
743
- text: 'Access Denied',
744
- },
745
- },
746
- ],
747
- };
748
- function badRequest(details, expression) {
749
- return {
750
- resourceType: 'OperationOutcome',
751
- issue: [
752
- {
753
- severity: 'error',
754
- code: 'invalid',
755
- details: {
756
- text: details,
757
- },
758
- expression: expression ? [expression] : undefined,
759
- },
760
- ],
761
- };
762
- }
763
- function isOk(outcome) {
764
- return outcome.id === OK_ID || outcome.id === CREATED_ID || outcome.id === NOT_MODIFIED_ID;
765
- }
766
- function isNotFound(outcome) {
767
- return outcome.id === NOT_FOUND_ID;
768
- }
769
- function isGone(outcome) {
770
- return outcome.id === GONE_ID;
771
- }
772
- function getStatus(outcome) {
773
- if (outcome.id === OK_ID) {
774
- return 200;
775
- }
776
- else if (outcome.id === CREATED_ID) {
777
- return 201;
778
- }
779
- else if (outcome.id === NOT_MODIFIED_ID) {
780
- return 304;
781
- }
782
- else if (outcome.id === ACCESS_DENIED) {
783
- return 403;
784
- }
785
- else if (outcome.id === NOT_FOUND_ID) {
786
- return 404;
787
- }
788
- else if (outcome.id === GONE_ID) {
789
- return 410;
790
- }
791
- else {
792
- return 400;
793
- }
794
- }
795
- /**
796
- * Asserts that the operation completed successfully and that the resource is defined.
797
- * @param outcome The operation outcome.
798
- * @param resource The resource that may or may not have been returned.
799
- */
800
- function assertOk(outcome, resource) {
801
- if (!isOk(outcome) || resource === undefined) {
802
- throw new OperationOutcomeError(outcome);
803
- }
804
- }
805
- class OperationOutcomeError extends Error {
806
- constructor(outcome) {
807
- var _a, _b;
808
- super((_b = (_a = outcome === null || outcome === void 0 ? void 0 : outcome.issue) === null || _a === void 0 ? void 0 : _a[0].details) === null || _b === void 0 ? void 0 : _b.text);
809
- this.outcome = outcome;
810
- }
811
- }
812
-
813
823
  var _ReadablePromise_suspender, _ReadablePromise_status, _ReadablePromise_response, _ReadablePromise_error, _a;
814
824
  /**
815
825
  * The ReadablePromise class wraps a request promise suitable for React Suspense.
@@ -1651,7 +1661,13 @@
1651
1661
  */
1652
1662
  getCachedReference(reference) {
1653
1663
  const refString = reference.reference;
1664
+ if (!refString) {
1665
+ return undefined;
1666
+ }
1654
1667
  const [resourceType, id] = refString.split('/');
1668
+ if (!resourceType || !id) {
1669
+ return undefined;
1670
+ }
1655
1671
  return this.getCached(resourceType, id);
1656
1672
  }
1657
1673
  /**
@@ -1697,6 +1713,9 @@
1697
1713
  return new ReadablePromise(Promise.reject(new Error('Missing reference')));
1698
1714
  }
1699
1715
  const [resourceType, id] = refString.split('/');
1716
+ if (!resourceType || !id) {
1717
+ return new ReadablePromise(Promise.reject(new Error('Invalid reference')));
1718
+ }
1700
1719
  return this.readResource(resourceType, id);
1701
1720
  }
1702
1721
  /**
@@ -2234,8 +2253,8 @@
2234
2253
  return undefined;
2235
2254
  }
2236
2255
  const obj = yield response.json();
2237
- if ((obj === null || obj === void 0 ? void 0 : obj.resourceType) === 'OperationOutcome' && !isOk(obj)) {
2238
- return Promise.reject(obj);
2256
+ if (response.status >= 400) {
2257
+ throw obj;
2239
2258
  }
2240
2259
  return obj;
2241
2260
  });
@@ -5024,6 +5043,155 @@
5024
5043
  }
5025
5044
  }
5026
5045
 
5046
+ const OK_ID = 'ok';
5047
+ const CREATED_ID = 'created';
5048
+ const GONE_ID = 'gone';
5049
+ const NOT_MODIFIED_ID = 'not-modified';
5050
+ const NOT_FOUND_ID = 'not-found';
5051
+ const ACCESS_DENIED = 'access-denied';
5052
+ const allOk = {
5053
+ resourceType: 'OperationOutcome',
5054
+ id: OK_ID,
5055
+ issue: [
5056
+ {
5057
+ severity: 'information',
5058
+ code: 'information',
5059
+ details: {
5060
+ text: 'All OK',
5061
+ },
5062
+ },
5063
+ ],
5064
+ };
5065
+ const created = {
5066
+ resourceType: 'OperationOutcome',
5067
+ id: CREATED_ID,
5068
+ issue: [
5069
+ {
5070
+ severity: 'information',
5071
+ code: 'information',
5072
+ details: {
5073
+ text: 'Created',
5074
+ },
5075
+ },
5076
+ ],
5077
+ };
5078
+ const notModified = {
5079
+ resourceType: 'OperationOutcome',
5080
+ id: NOT_MODIFIED_ID,
5081
+ issue: [
5082
+ {
5083
+ severity: 'information',
5084
+ code: 'information',
5085
+ details: {
5086
+ text: 'Not Modified',
5087
+ },
5088
+ },
5089
+ ],
5090
+ };
5091
+ const notFound = {
5092
+ resourceType: 'OperationOutcome',
5093
+ id: NOT_FOUND_ID,
5094
+ issue: [
5095
+ {
5096
+ severity: 'error',
5097
+ code: 'not-found',
5098
+ details: {
5099
+ text: 'Not found',
5100
+ },
5101
+ },
5102
+ ],
5103
+ };
5104
+ const gone = {
5105
+ resourceType: 'OperationOutcome',
5106
+ id: GONE_ID,
5107
+ issue: [
5108
+ {
5109
+ severity: 'error',
5110
+ code: 'gone',
5111
+ details: {
5112
+ text: 'Gone',
5113
+ },
5114
+ },
5115
+ ],
5116
+ };
5117
+ const accessDenied = {
5118
+ resourceType: 'OperationOutcome',
5119
+ id: ACCESS_DENIED,
5120
+ issue: [
5121
+ {
5122
+ severity: 'error',
5123
+ code: 'access-denied',
5124
+ details: {
5125
+ text: 'Access Denied',
5126
+ },
5127
+ },
5128
+ ],
5129
+ };
5130
+ function badRequest(details, expression) {
5131
+ return {
5132
+ resourceType: 'OperationOutcome',
5133
+ issue: [
5134
+ {
5135
+ severity: 'error',
5136
+ code: 'invalid',
5137
+ details: {
5138
+ text: details,
5139
+ },
5140
+ expression: expression ? [expression] : undefined,
5141
+ },
5142
+ ],
5143
+ };
5144
+ }
5145
+ function isOk(outcome) {
5146
+ return outcome.id === OK_ID || outcome.id === CREATED_ID || outcome.id === NOT_MODIFIED_ID;
5147
+ }
5148
+ function isNotFound(outcome) {
5149
+ return outcome.id === NOT_FOUND_ID;
5150
+ }
5151
+ function isGone(outcome) {
5152
+ return outcome.id === GONE_ID;
5153
+ }
5154
+ function getStatus(outcome) {
5155
+ if (outcome.id === OK_ID) {
5156
+ return 200;
5157
+ }
5158
+ else if (outcome.id === CREATED_ID) {
5159
+ return 201;
5160
+ }
5161
+ else if (outcome.id === NOT_MODIFIED_ID) {
5162
+ return 304;
5163
+ }
5164
+ else if (outcome.id === ACCESS_DENIED) {
5165
+ return 403;
5166
+ }
5167
+ else if (outcome.id === NOT_FOUND_ID) {
5168
+ return 404;
5169
+ }
5170
+ else if (outcome.id === GONE_ID) {
5171
+ return 410;
5172
+ }
5173
+ else {
5174
+ return 400;
5175
+ }
5176
+ }
5177
+ /**
5178
+ * Asserts that the operation completed successfully and that the resource is defined.
5179
+ * @param outcome The operation outcome.
5180
+ * @param resource The resource that may or may not have been returned.
5181
+ */
5182
+ function assertOk(outcome, resource) {
5183
+ if (!isOk(outcome) || resource === undefined) {
5184
+ throw new OperationOutcomeError(outcome);
5185
+ }
5186
+ }
5187
+ class OperationOutcomeError extends Error {
5188
+ constructor(outcome) {
5189
+ var _a, _b;
5190
+ super((_b = (_a = outcome === null || outcome === void 0 ? void 0 : outcome.issue) === null || _a === void 0 ? void 0 : _a[0].details) === null || _b === void 0 ? void 0 : _b.text);
5191
+ this.outcome = outcome;
5192
+ }
5193
+ }
5194
+
5027
5195
  const DEFAULT_SEARCH_COUNT = 20;
5028
5196
  /**
5029
5197
  * Search operators.
@@ -5382,11 +5550,13 @@
5382
5550
  exports.created = created;
5383
5551
  exports.deepEquals = deepEquals$1;
5384
5552
  exports.evalFhirPath = evalFhirPath;
5553
+ exports.findObservationInterval = findObservationInterval;
5385
5554
  exports.formatAddress = formatAddress;
5386
5555
  exports.formatFamilyName = formatFamilyName;
5387
5556
  exports.formatGivenName = formatGivenName;
5388
5557
  exports.formatHumanName = formatHumanName;
5389
5558
  exports.formatSearchQuery = formatSearchQuery;
5559
+ exports.getCodeBySystem = getCodeBySystem;
5390
5560
  exports.getDateProperty = getDateProperty;
5391
5561
  exports.getDisplayString = getDisplayString;
5392
5562
  exports.getExpressionForResourceType = getExpressionForResourceType;
@@ -5409,11 +5579,18 @@
5409
5579
  exports.isProfileResource = isProfileResource;
5410
5580
  exports.isStringArray = isStringArray;
5411
5581
  exports.isUUID = isUUID;
5582
+ exports.matchesRange = matchesRange;
5412
5583
  exports.notFound = notFound;
5413
5584
  exports.notModified = notModified;
5414
5585
  exports.parseFhirPath = parseFhirPath;
5415
5586
  exports.parseSearchDefinition = parseSearchDefinition;
5587
+ exports.preciseEquals = preciseEquals;
5588
+ exports.preciseGreaterThan = preciseGreaterThan;
5589
+ exports.preciseGreaterThanOrEquals = preciseGreaterThanOrEquals;
5590
+ exports.preciseLessThan = preciseLessThan;
5591
+ exports.preciseLessThanOrEquals = preciseLessThanOrEquals;
5416
5592
  exports.resolveId = resolveId;
5593
+ exports.setCodeBySystem = setCodeBySystem;
5417
5594
  exports.stringify = stringify;
5418
5595
  exports.tokenize = tokenize;
5419
5596