@haste-health/fhir-patch-building 0.127.1 → 0.127.3
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 +58 -2
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -1,3 +1,59 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @haste-health/fhir-patch-building
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Builds JSON Patch (RFC 6902) operations for a FHIR resource from one typed mutation. You describe the change with a [`@haste-health/fhir-pointer`](https://www.npmjs.com/package/@haste-health/fhir-pointer) location and an `add`, `replace` or `remove` op. The builder inspects the current resource and prepends the `add` operations needed to create any missing arrays and objects along the path, so a patch to `name/0/given/0` applies cleanly to a Patient that has no `name` yet.
|
|
4
|
+
|
|
5
|
+
The Haste Health admin app and [`@haste-health/components`](https://www.npmjs.com/package/@haste-health/components) use it to turn form edits into FHIR `PATCH` requests.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @haste-health/fhir-patch-building
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import buildPatches, {
|
|
17
|
+
applyMutation,
|
|
18
|
+
applyMutationImmutable,
|
|
19
|
+
} from "@haste-health/fhir-patch-building";
|
|
20
|
+
import { descend, pointer } from "@haste-health/fhir-pointer";
|
|
21
|
+
import type { Patient, id } from "@haste-health/fhir-types/r4/types";
|
|
22
|
+
import { R4 } from "@haste-health/fhir-types/versions";
|
|
23
|
+
|
|
24
|
+
const patient = { resourceType: "Patient", id: "123" } as Patient;
|
|
25
|
+
const loc = pointer(R4, "Patient", "123" as id);
|
|
26
|
+
const firstGiven = descend(
|
|
27
|
+
descend(descend(descend(loc, "name"), 0), "given"),
|
|
28
|
+
0,
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
buildPatches(patient, { op: "add", path: firstGiven, value: "John" });
|
|
32
|
+
// [
|
|
33
|
+
// { op: "add", path: "/name", value: [] },
|
|
34
|
+
// { op: "add", path: "/name/0", value: {} },
|
|
35
|
+
// { op: "add", path: "/name/0/given", value: [] },
|
|
36
|
+
// { op: "add", path: "/name/0/given/0", value: "John" },
|
|
37
|
+
// ]
|
|
38
|
+
|
|
39
|
+
const named = { ...patient, name: [{ given: ["Bob"] }] } as Patient;
|
|
40
|
+
const renamed = applyMutationImmutable(named, {
|
|
41
|
+
op: "replace",
|
|
42
|
+
path: firstGiven,
|
|
43
|
+
value: "Jake",
|
|
44
|
+
});
|
|
45
|
+
// { resourceType: "Patient", id: "123", name: [{ given: ["Jake"] }] }, `named` unchanged
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
- `buildPatches(resource, mutation)` returns the operations; send them as the body of a FHIR `PATCH`, for example with `client.patch` from `@haste-health/client`.
|
|
49
|
+
- `replace` with an `undefined` or `null` value becomes a `remove`, and `remove` of a path that does not exist produces no operations.
|
|
50
|
+
- `applyMutation` applies the patch in place with `fast-json-patch`; `applyMutationImmutable` uses `immer` and returns a new resource.
|
|
51
|
+
|
|
52
|
+
## Links
|
|
53
|
+
|
|
54
|
+
- [API reference](https://typescript.haste.health/modules/_haste-health_fhir-patch-building.html)
|
|
55
|
+
- [Source](https://github.com/HasteHealth/HasteHealth/tree/main/frontend/packages/fhir-patch-building)
|
|
56
|
+
- [TypeScript SDK overview](https://haste.health/docs/api/sdks/typescript)
|
|
57
|
+
- Related: [@haste-health/fhir-pointer](https://www.npmjs.com/package/@haste-health/fhir-pointer), [@haste-health/client](https://www.npmjs.com/package/@haste-health/client)
|
|
58
|
+
|
|
59
|
+
Part of [Haste Health](https://haste.health), licensed under [Apache-2.0](https://github.com/HasteHealth/HasteHealth/blob/main/LICENSE).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@haste-health/fhir-patch-building",
|
|
3
|
-
"version": "0.127.
|
|
3
|
+
"version": "0.127.3",
|
|
4
4
|
"homepage": "https://haste.health",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,15 +15,15 @@
|
|
|
15
15
|
"jest": "^29.7.0",
|
|
16
16
|
"ts-jest": "^29.4.11",
|
|
17
17
|
"typescript": "6.0.3",
|
|
18
|
-
"@haste-health/fhir-types": "^0.127.
|
|
18
|
+
"@haste-health/fhir-types": "^0.127.3"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
21
|
"fast-json-patch": "^3.1.1",
|
|
22
22
|
"immer": "^10.2.0",
|
|
23
|
-
"@haste-health/fhir-pointer": "^0.127.
|
|
23
|
+
"@haste-health/fhir-pointer": "^0.127.3"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
|
-
"
|
|
26
|
+
"README.md",
|
|
27
27
|
"lib/**",
|
|
28
28
|
"src/**"
|
|
29
29
|
],
|