@metriport/fhir-sdk 1.3.7 → 1.4.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/README.md +173 -8
- package/dist/__tests__/careplan.test.d.ts +2 -0
- package/dist/__tests__/careplan.test.d.ts.map +1 -0
- package/dist/__tests__/careplan.test.js +544 -0
- package/dist/__tests__/careplan.test.js.map +1 -0
- package/dist/__tests__/coding-utilities.test.d.ts +2 -0
- package/dist/__tests__/coding-utilities.test.d.ts.map +1 -0
- package/dist/__tests__/coding-utilities.test.js +482 -0
- package/dist/__tests__/coding-utilities.test.js.map +1 -0
- package/dist/__tests__/type-guards.test.d.ts +2 -0
- package/dist/__tests__/type-guards.test.d.ts.map +1 -0
- package/dist/__tests__/type-guards.test.js +163 -0
- package/dist/__tests__/type-guards.test.js.map +1 -0
- package/dist/demo-reverse-references.d.ts +7 -0
- package/dist/demo-reverse-references.d.ts.map +1 -0
- package/dist/demo-reverse-references.js +207 -0
- package/dist/demo-reverse-references.js.map +1 -0
- package/dist/fhir-bundle-sdk.d.ts +50 -47
- package/dist/fhir-bundle-sdk.d.ts.map +1 -1
- package/dist/fhir-bundle-sdk.js +10 -2
- package/dist/fhir-bundle-sdk.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -1
- package/dist/internal/coding-systems.d.ts +42 -0
- package/dist/internal/coding-systems.d.ts.map +1 -0
- package/dist/internal/coding-systems.js +47 -0
- package/dist/internal/coding-systems.js.map +1 -0
- package/dist/internal/coding-utilities.d.ts +27 -0
- package/dist/internal/coding-utilities.d.ts.map +1 -0
- package/dist/internal/coding-utilities.js +297 -0
- package/dist/internal/coding-utilities.js.map +1 -0
- package/dist/internal/graph-traversal.js +1 -1
- package/dist/internal/graph-traversal.js.map +1 -1
- package/dist/internal/llm-context.d.ts.map +1 -1
- package/dist/internal/llm-context.js +3 -1
- package/dist/internal/llm-context.js.map +1 -1
- package/dist/internal/reference-resolution.d.ts.map +1 -1
- package/dist/internal/reference-resolution.js +99 -11
- package/dist/internal/reference-resolution.js.map +1 -1
- package/dist/internal/transparent-proxy.d.ts +12 -0
- package/dist/internal/transparent-proxy.d.ts.map +1 -0
- package/dist/internal/transparent-proxy.js +81 -0
- package/dist/internal/transparent-proxy.js.map +1 -0
- package/dist/type-guards.d.ts +183 -0
- package/dist/type-guards.d.ts.map +1 -0
- package/dist/type-guards.js +108 -0
- package/dist/type-guards.js.map +1 -0
- package/dist/types/coding-fields.d.ts +292 -0
- package/dist/types/coding-fields.d.ts.map +1 -0
- package/dist/types/coding-fields.js +3 -0
- package/dist/types/coding-fields.js.map +1 -0
- package/dist/types/smart-resources.d.ts +152 -6
- package/dist/types/smart-resources.d.ts.map +1 -1
- package/dist/types/smart-resources.js +35 -3
- package/dist/types/smart-resources.js.map +1 -1
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -38,7 +38,96 @@ const encounters = sdk.getEncounters();
|
|
|
38
38
|
const conditions = sdk.getConditions();
|
|
39
39
|
```
|
|
40
40
|
|
|
41
|
-
### 2.
|
|
41
|
+
### 2. Coding System Utilities
|
|
42
|
+
|
|
43
|
+
All resources with CodeableConcept fields automatically have enhanced coding utilities for working with standard medical coding systems (LOINC, ICD-10, SNOMED, RxNorm, NDC).
|
|
44
|
+
|
|
45
|
+
**Important:** Use the specific Smart type aliases (`SmartObservation`, `SmartCondition`, etc.) instead of the generic `Smart<T>` pattern to get full TypeScript support for coding utilities.
|
|
46
|
+
|
|
47
|
+
```typescript
|
|
48
|
+
import { SmartObservation, SmartCondition } from "@metriport/fhir-sdk";
|
|
49
|
+
|
|
50
|
+
// ✅ GOOD - Use specific Smart type for full TypeScript support
|
|
51
|
+
const obs: SmartObservation = sdk.getObservationById("obs-123")!;
|
|
52
|
+
|
|
53
|
+
// Check if the observation uses LOINC coding
|
|
54
|
+
if (obs.code?.hasLoinc()) {
|
|
55
|
+
const loincCode = obs.code.getLoincCode(); // Get first LOINC code
|
|
56
|
+
console.log("LOINC code:", loincCode);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Check for specific codes
|
|
60
|
+
if (obs.code?.hasLoincCode("2339-0")) {
|
|
61
|
+
console.log("Found glucose measurement!");
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Work with ICD-10 codes in conditions
|
|
65
|
+
const condition: SmartCondition = sdk.getConditionById("cond-456")!;
|
|
66
|
+
const icd10Code = condition.code?.getIcd10Code();
|
|
67
|
+
const allIcd10 = condition.code?.getIcd10Codes(); // Get all ICD-10 codes
|
|
68
|
+
|
|
69
|
+
// Check if condition has specific ICD-10 codes
|
|
70
|
+
if (condition.code?.hasSomeIcd10(["E11.9", "E10.9"])) {
|
|
71
|
+
console.log("Patient has diabetes");
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// Find specific coding with custom logic
|
|
75
|
+
const diabetesCoding = condition.code?.findIcd10Coding(
|
|
76
|
+
code => code.startsWith("E11") || code.startsWith("E10")
|
|
77
|
+
);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Available Coding Systems:**
|
|
81
|
+
|
|
82
|
+
Each `SmartCodeableConcept` provides methods for these coding systems:
|
|
83
|
+
|
|
84
|
+
- **LOINC**: `hasLoinc()`, `getLoinc()`, `getLoincCode()`, `getLoincCodes()`, `hasLoincCode()`, `hasSomeLoinc()`, `findLoincCoding()`
|
|
85
|
+
- **ICD-10**: `hasIcd10()`, `getIcd10()`, `getIcd10Code()`, `getIcd10Codes()`, `hasIcd10Code()`, `hasSomeIcd10()`, `findIcd10Coding()`
|
|
86
|
+
- **SNOMED**: `hasSnomed()`, `getSnomed()`, `getSnomedCode()`, `getSnomedCodes()`, `hasSnomedCode()`, `hasSomeSnomed()`, `findSnomedCoding()`
|
|
87
|
+
- **RxNorm**: `hasRxNorm()`, `getRxNorm()`, `getRxNormCode()`, `getRxNormCodes()`, `hasRxNormCode()`, `hasSomeRxNorm()`, `findRxNormCoding()`
|
|
88
|
+
- **NDC**: `hasNdc()`, `getNdc()`, `getNdcCode()`, `getNdcCodes()`, `hasNdcCode()`, `hasSomeNdc()`, `findNdcCoding()`
|
|
89
|
+
|
|
90
|
+
**Smart Resource Types with Coding Utilities:**
|
|
91
|
+
|
|
92
|
+
```typescript
|
|
93
|
+
import {
|
|
94
|
+
SmartObservation,
|
|
95
|
+
SmartCondition,
|
|
96
|
+
SmartProcedure,
|
|
97
|
+
SmartAllergyIntolerance,
|
|
98
|
+
SmartEncounter,
|
|
99
|
+
SmartDiagnosticReport,
|
|
100
|
+
SmartImmunization,
|
|
101
|
+
SmartMedication,
|
|
102
|
+
SmartMedicationRequest,
|
|
103
|
+
SmartMedicationAdministration,
|
|
104
|
+
SmartMedicationDispense,
|
|
105
|
+
SmartMedicationStatement,
|
|
106
|
+
SmartFamilyMemberHistory,
|
|
107
|
+
SmartRelatedPerson,
|
|
108
|
+
SmartRiskAssessment,
|
|
109
|
+
SmartServiceRequest,
|
|
110
|
+
SmartCarePlan,
|
|
111
|
+
SmartPatient,
|
|
112
|
+
SmartPractitioner,
|
|
113
|
+
SmartOrganization,
|
|
114
|
+
SmartLocation,
|
|
115
|
+
SmartComposition,
|
|
116
|
+
SmartCoverage,
|
|
117
|
+
SmartDocumentReference,
|
|
118
|
+
} from "@metriport/fhir-sdk";
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
All SDK getter methods return the appropriate Smart type automatically:
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
// These all return the specific Smart types with coding utilities
|
|
125
|
+
const observations: SmartObservation[] = sdk.getObservations();
|
|
126
|
+
const conditions: SmartCondition[] = sdk.getConditions();
|
|
127
|
+
const procedures: SmartProcedure[] = sdk.getProcedures();
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### 3. Smart Reference Resolution
|
|
42
131
|
|
|
43
132
|
Resources include typed getter methods for convenient and type-safe reference traversal:
|
|
44
133
|
|
|
@@ -49,7 +138,7 @@ const observation = sdk.getObservationById("obs-123");
|
|
|
49
138
|
const patient = observation.getSubject<Patient>();
|
|
50
139
|
```
|
|
51
140
|
|
|
52
|
-
|
|
141
|
+
**Smart Resource Methods:**
|
|
53
142
|
|
|
54
143
|
All smart resources include these utility methods:
|
|
55
144
|
|
|
@@ -106,7 +195,7 @@ const refs = sdk.getResourcesReferencedBy(observation);
|
|
|
106
195
|
|
|
107
196
|
This method automatically discovers and returns all resources referenced by the current resource based on the configured reference methods in `REFERENCE_METHOD_MAPPING`. It handles both single references and array references.
|
|
108
197
|
|
|
109
|
-
###
|
|
198
|
+
### 4. Bundle Export
|
|
110
199
|
|
|
111
200
|
You can export subsets of your bundle or export by resource type to quickly create custom bundles.
|
|
112
201
|
|
|
@@ -118,7 +207,7 @@ const subset = sdk.exportSubset(["patient-uuid-1", "obs-uuid-1"]);
|
|
|
118
207
|
const observationBundle = sdk.exportByType("Observation");
|
|
119
208
|
```
|
|
120
209
|
|
|
121
|
-
###
|
|
210
|
+
### 5. Validation
|
|
122
211
|
|
|
123
212
|
Validate your bundle to ensure there are no broken references. A broken reference is a reference that points to a resource that does not exist in the bundle.
|
|
124
213
|
|
|
@@ -128,7 +217,7 @@ console.log(result.hasBrokenReferences); // true if at least one broken referenc
|
|
|
128
217
|
console.log(result.brokenReferences); // All broken references in bundle
|
|
129
218
|
```
|
|
130
219
|
|
|
131
|
-
###
|
|
220
|
+
### 6. Reverse Reference Lookup
|
|
132
221
|
|
|
133
222
|
Find all resources that reference a given resource (reverse lookup). This is useful for discovering what refers to a specific resource.
|
|
134
223
|
|
|
@@ -164,7 +253,7 @@ const encounteredObs = patient.getReferencingResources({
|
|
|
164
253
|
- **Smart resources** - returns fully functional Smart resources
|
|
165
254
|
- **Bi-directional** - complements forward reference navigation
|
|
166
255
|
|
|
167
|
-
###
|
|
256
|
+
### 7. Date Range Search
|
|
168
257
|
|
|
169
258
|
Search for resources by date range using an interval tree index. This is useful for filtering resources by clinical dates efficiently.
|
|
170
259
|
|
|
@@ -225,7 +314,7 @@ The following resource types and date fields are automatically indexed:
|
|
|
225
314
|
- **Coverage**: period
|
|
226
315
|
- **AllergyIntolerance**: onsetDateTime, onsetPeriod, lastOccurrence, recordedDate
|
|
227
316
|
|
|
228
|
-
###
|
|
317
|
+
### 8. Reference Walking (BFS Traversal)
|
|
229
318
|
|
|
230
319
|
Walk the reference graph starting from any resource using breadth-first search (BFS). This is useful for discovering all resources connected to a specific resource.
|
|
231
320
|
|
|
@@ -245,7 +334,7 @@ const limitedResult = sdk.walkReferences(observation, { maxDepth: 2 });
|
|
|
245
334
|
const withoutStart = sdk.walkReferences(observation, { includeStartResource: false });
|
|
246
335
|
```
|
|
247
336
|
|
|
248
|
-
###
|
|
337
|
+
### 9. LLM Context Generation
|
|
249
338
|
|
|
250
339
|
Generate comprehensive, LLM-friendly context from a resource and its related resources. This automatically performs BFS traversal, strips non-clinical metadata, and formats the output for optimal LLM consumption.
|
|
251
340
|
|
|
@@ -299,8 +388,84 @@ const sdk2 = FhirBundleSdk.createSync(bundle); // Synchronous
|
|
|
299
388
|
|
|
300
389
|
This is especially useful in REPL sessions where you want quick access to utility functions without creating a full SDK instance.
|
|
301
390
|
|
|
391
|
+
### 10. Type Guards
|
|
392
|
+
|
|
393
|
+
Filter mixed arrays of FHIR resources to specific types with full TypeScript type safety:
|
|
394
|
+
|
|
395
|
+
```typescript
|
|
396
|
+
import { Resource } from "@medplum/fhirtypes";
|
|
397
|
+
import { isPatient, isObservation, isDiagnosticReport } from "@metriport/fhir-sdk";
|
|
398
|
+
|
|
399
|
+
// Filter mixed resource arrays
|
|
400
|
+
const resources: Resource[] = [
|
|
401
|
+
{ resourceType: "Patient", id: "patient-1" },
|
|
402
|
+
{ resourceType: "Observation", id: "obs-1", status: "final", code: { text: "test" } },
|
|
403
|
+
{ resourceType: "DiagnosticReport", id: "report-1", status: "final", code: { text: "lab" } },
|
|
404
|
+
];
|
|
405
|
+
|
|
406
|
+
const patients = resources.filter(isPatient); // Patient[]
|
|
407
|
+
const observations = resources.filter(isObservation); // Observation[]
|
|
408
|
+
|
|
409
|
+
// TypeScript knows the exact type after filtering
|
|
410
|
+
patients.forEach(patient => {
|
|
411
|
+
const name = patient.name; // ✅ Type-safe access to Patient fields
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
// Works with Smart resources too
|
|
415
|
+
const allObs = sdk.getObservations();
|
|
416
|
+
allObs.forEach(obs => {
|
|
417
|
+
if (isObservation(obs)) {
|
|
418
|
+
const subject = obs.getSubject(); // ✅ Smart methods available
|
|
419
|
+
const status = obs.status; // ✅ Observation fields available
|
|
420
|
+
}
|
|
421
|
+
});
|
|
422
|
+
```
|
|
423
|
+
|
|
424
|
+
**Available type guards:**
|
|
425
|
+
Use the `is<ResourceType>` type guard to check if a resource is of a specific type. There is one for every fhir resource type. Use them whenever possible over writing your own type guards.
|
|
426
|
+
|
|
302
427
|
## Example Use Cases
|
|
303
428
|
|
|
429
|
+
### Filter by Medical Codes
|
|
430
|
+
|
|
431
|
+
```typescript
|
|
432
|
+
import { SmartObservation, SmartCondition } from "@metriport/fhir-sdk";
|
|
433
|
+
|
|
434
|
+
// Find all glucose observations using LOINC codes
|
|
435
|
+
const glucoseObs = sdk.getObservations().filter(
|
|
436
|
+
obs =>
|
|
437
|
+
obs.code?.hasLoincCode("2339-0") || // Glucose [Mass/volume] in Blood
|
|
438
|
+
obs.code?.hasLoincCode("2345-7") // Glucose [Mass/volume] in Serum or Plasma
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
// Find all diabetes conditions using ICD-10 codes
|
|
442
|
+
const diabetesConditions = sdk.getConditions().filter(cond =>
|
|
443
|
+
cond.code?.findIcd10Coding(
|
|
444
|
+
code =>
|
|
445
|
+
code.startsWith("E10") || // Type 1 diabetes
|
|
446
|
+
code.startsWith("E11") // Type 2 diabetes
|
|
447
|
+
)
|
|
448
|
+
);
|
|
449
|
+
|
|
450
|
+
// Find medications by RxNorm code
|
|
451
|
+
const metforminMeds = sdk
|
|
452
|
+
.getMedicationRequests()
|
|
453
|
+
.filter(medReq => medReq.medicationCodeableConcept?.hasRxNormCode("6809"));
|
|
454
|
+
|
|
455
|
+
// Complex filtering with multiple coding systems
|
|
456
|
+
const labResults = sdk.getObservations().filter(obs => {
|
|
457
|
+
if (obs.code?.hasLoinc()) {
|
|
458
|
+
const loincCode = obs.code.getLoincCode();
|
|
459
|
+
// Filter for specific lab panels
|
|
460
|
+
return (
|
|
461
|
+
loincCode?.startsWith("24331") || // Lipid panel
|
|
462
|
+
loincCode?.startsWith("24362")
|
|
463
|
+
); // Complete blood count
|
|
464
|
+
}
|
|
465
|
+
return false;
|
|
466
|
+
});
|
|
467
|
+
```
|
|
468
|
+
|
|
304
469
|
### Patient Summary
|
|
305
470
|
|
|
306
471
|
```typescript
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"careplan.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/careplan.test.ts"],"names":[],"mappings":""}
|