@medplum/fhir-router 2.0.14 → 2.0.16
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.cjs +134 -149
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.min.cjs +1 -1
- package/dist/cjs/index.min.cjs.map +1 -1
- package/dist/esm/index.min.mjs +1 -1
- package/dist/esm/index.min.mjs.map +1 -1
- package/dist/esm/index.mjs +135 -150
- package/dist/esm/index.mjs.map +1 -1
- package/dist/types/repo.d.ts +2 -1
- package/package.json +1 -1
package/dist/cjs/index.cjs
CHANGED
|
@@ -4,35 +4,6 @@
|
|
|
4
4
|
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.medplum = global.medplum || {}, global.medplum.fhirRouter = {}), global.medplum.core, global.dataloader, global.rfc6902));
|
|
5
5
|
})(this, (function (exports, core, DataLoader, rfc6902) { 'use strict';
|
|
6
6
|
|
|
7
|
-
/******************************************************************************
|
|
8
|
-
Copyright (c) Microsoft Corporation.
|
|
9
|
-
|
|
10
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
11
|
-
purpose with or without fee is hereby granted.
|
|
12
|
-
|
|
13
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
14
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
15
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
16
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
17
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
18
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
19
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
20
|
-
***************************************************************************** */
|
|
21
|
-
|
|
22
|
-
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
23
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
24
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
25
|
-
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
29
|
-
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
30
|
-
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
31
|
-
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
32
|
-
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
var _BatchProcessor_instances, _BatchProcessor_ids, _BatchProcessor_processBatchEntry, _BatchProcessor_validateEntry, _BatchProcessor_parsePatchBody, _BatchProcessor_addReplacementId, _BatchProcessor_rewriteIds, _BatchProcessor_rewriteIdsInArray, _BatchProcessor_rewriteIdsInObject, _BatchProcessor_rewriteIdsInString;
|
|
36
7
|
/**
|
|
37
8
|
* Processes a FHIR batch request.
|
|
38
9
|
*
|
|
@@ -58,12 +29,10 @@
|
|
|
58
29
|
* @param bundle The input bundle.
|
|
59
30
|
*/
|
|
60
31
|
constructor(router, repo, bundle) {
|
|
61
|
-
_BatchProcessor_instances.add(this);
|
|
62
32
|
this.router = router;
|
|
63
33
|
this.repo = repo;
|
|
64
34
|
this.bundle = bundle;
|
|
65
|
-
|
|
66
|
-
__classPrivateFieldSet(this, _BatchProcessor_ids, {}, "f");
|
|
35
|
+
this.ids = {};
|
|
67
36
|
}
|
|
68
37
|
/**
|
|
69
38
|
* Processes a FHIR batch request.
|
|
@@ -85,9 +54,9 @@
|
|
|
85
54
|
}
|
|
86
55
|
const resultEntries = [];
|
|
87
56
|
for (const entry of entries) {
|
|
88
|
-
const rewritten =
|
|
57
|
+
const rewritten = this.rewriteIdsInObject(entry);
|
|
89
58
|
try {
|
|
90
|
-
resultEntries.push(await
|
|
59
|
+
resultEntries.push(await this.processBatchEntry(rewritten));
|
|
91
60
|
}
|
|
92
61
|
catch (err) {
|
|
93
62
|
resultEntries.push(buildBundleResponse(core.normalizeOperationOutcome(err)));
|
|
@@ -99,102 +68,108 @@
|
|
|
99
68
|
entry: resultEntries,
|
|
100
69
|
};
|
|
101
70
|
}
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
71
|
+
/**
|
|
72
|
+
* Processes a single entry from a FHIR batch request.
|
|
73
|
+
* @param entry The bundle entry.
|
|
74
|
+
* @returns The bundle entry response.
|
|
75
|
+
*/
|
|
76
|
+
async processBatchEntry(entry) {
|
|
77
|
+
this.validateEntry(entry);
|
|
78
|
+
const request = entry.request;
|
|
79
|
+
if (entry.resource?.resourceType && request.ifNoneExist) {
|
|
80
|
+
const baseUrl = `https://example.com/${entry.resource.resourceType}`;
|
|
81
|
+
const searchUrl = new URL('?' + request.ifNoneExist, baseUrl);
|
|
82
|
+
const searchBundle = await this.repo.search(core.parseSearchUrl(searchUrl));
|
|
83
|
+
const entries = searchBundle?.entry;
|
|
84
|
+
if (entries.length > 1) {
|
|
85
|
+
return buildBundleResponse(core.badRequest('Multiple matches'));
|
|
86
|
+
}
|
|
87
|
+
if (entries.length === 1) {
|
|
88
|
+
const matchingResource = entries[0].resource;
|
|
89
|
+
if (entry.fullUrl) {
|
|
90
|
+
this.addReplacementId(entry.fullUrl, matchingResource);
|
|
91
|
+
}
|
|
92
|
+
return buildBundleResponse(core.allOk, matchingResource);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
let body = entry.resource;
|
|
96
|
+
if (request.method === 'PATCH') {
|
|
97
|
+
body = this.parsePatchBody(entry);
|
|
98
|
+
}
|
|
99
|
+
// Pass in dummy host for parsing purposes.
|
|
100
|
+
// The host is ignored.
|
|
101
|
+
const url = new URL(request.url, 'https://example.com/');
|
|
102
|
+
const result = await this.router.handleRequest({
|
|
103
|
+
method: request.method,
|
|
104
|
+
pathname: url.pathname,
|
|
105
|
+
params: Object.create(null),
|
|
106
|
+
query: Object.fromEntries(url.searchParams),
|
|
107
|
+
body,
|
|
108
|
+
}, this.repo);
|
|
109
|
+
if (entry.fullUrl && result.length === 2) {
|
|
110
|
+
this.addReplacementId(entry.fullUrl, result[1]);
|
|
111
|
+
}
|
|
112
|
+
return buildBundleResponse(result[0], result[1]);
|
|
113
|
+
}
|
|
114
|
+
validateEntry(entry) {
|
|
115
|
+
if (!entry.request) {
|
|
116
|
+
throw new core.OperationOutcomeError(core.badRequest('Missing entry.request'));
|
|
117
|
+
}
|
|
118
|
+
if (!entry.request.method) {
|
|
119
|
+
throw new core.OperationOutcomeError(core.badRequest('Missing entry.request.method'));
|
|
120
|
+
}
|
|
121
|
+
if (!entry.request.url) {
|
|
122
|
+
throw new core.OperationOutcomeError(core.badRequest('Missing entry.request.url'));
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
parsePatchBody(entry) {
|
|
126
|
+
const patchResource = entry.resource;
|
|
127
|
+
if (!patchResource) {
|
|
128
|
+
throw new core.OperationOutcomeError(core.badRequest('Missing entry.resource'));
|
|
129
|
+
}
|
|
130
|
+
if (patchResource.resourceType !== 'Binary') {
|
|
131
|
+
throw new core.OperationOutcomeError(core.badRequest('Patch resource must be a Binary'));
|
|
119
132
|
}
|
|
120
|
-
if (
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
133
|
+
if (!patchResource.data) {
|
|
134
|
+
throw new core.OperationOutcomeError(core.badRequest('Missing entry.resource.data'));
|
|
135
|
+
}
|
|
136
|
+
return JSON.parse(Buffer.from(patchResource.data, 'base64').toString('utf8'));
|
|
137
|
+
}
|
|
138
|
+
addReplacementId(fullUrl, resource) {
|
|
139
|
+
if (fullUrl?.startsWith('urn:uuid:')) {
|
|
140
|
+
this.ids[fullUrl] = resource;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
rewriteIds(input) {
|
|
144
|
+
if (Array.isArray(input)) {
|
|
145
|
+
return this.rewriteIdsInArray(input);
|
|
146
|
+
}
|
|
147
|
+
if (typeof input === 'string') {
|
|
148
|
+
return this.rewriteIdsInString(input);
|
|
149
|
+
}
|
|
150
|
+
if (typeof input === 'object') {
|
|
151
|
+
return this.rewriteIdsInObject(input);
|
|
152
|
+
}
|
|
153
|
+
return input;
|
|
154
|
+
}
|
|
155
|
+
rewriteIdsInArray(input) {
|
|
156
|
+
return input.map((item) => this.rewriteIds(item));
|
|
157
|
+
}
|
|
158
|
+
rewriteIdsInObject(input) {
|
|
159
|
+
return Object.fromEntries(Object.entries(input).map(([k, v]) => [k, this.rewriteIds(v)]));
|
|
160
|
+
}
|
|
161
|
+
rewriteIdsInString(input) {
|
|
162
|
+
const matches = input.match(/urn:uuid:\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/);
|
|
163
|
+
if (matches) {
|
|
164
|
+
const fullUrl = matches[0];
|
|
165
|
+
const resource = this.ids[fullUrl];
|
|
166
|
+
if (resource) {
|
|
167
|
+
return input.replaceAll(fullUrl, core.getReferenceString(resource));
|
|
124
168
|
}
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (request.method === 'PATCH') {
|
|
130
|
-
body = __classPrivateFieldGet(this, _BatchProcessor_instances, "m", _BatchProcessor_parsePatchBody).call(this, entry);
|
|
131
|
-
}
|
|
132
|
-
// Pass in dummy host for parsing purposes.
|
|
133
|
-
// The host is ignored.
|
|
134
|
-
const url = new URL(request.url, 'https://example.com/');
|
|
135
|
-
const result = await this.router.handleRequest({
|
|
136
|
-
method: request.method,
|
|
137
|
-
pathname: url.pathname,
|
|
138
|
-
params: Object.create(null),
|
|
139
|
-
query: Object.fromEntries(url.searchParams),
|
|
140
|
-
body,
|
|
141
|
-
}, this.repo);
|
|
142
|
-
if (entry.fullUrl && result.length === 2) {
|
|
143
|
-
__classPrivateFieldGet(this, _BatchProcessor_instances, "m", _BatchProcessor_addReplacementId).call(this, entry.fullUrl, result[1]);
|
|
144
|
-
}
|
|
145
|
-
return buildBundleResponse(result[0], result[1]);
|
|
146
|
-
}, _BatchProcessor_validateEntry = function _BatchProcessor_validateEntry(entry) {
|
|
147
|
-
if (!entry.request) {
|
|
148
|
-
throw new core.OperationOutcomeError(core.badRequest('Missing entry.request'));
|
|
149
|
-
}
|
|
150
|
-
if (!entry.request.method) {
|
|
151
|
-
throw new core.OperationOutcomeError(core.badRequest('Missing entry.request.method'));
|
|
152
|
-
}
|
|
153
|
-
if (!entry.request.url) {
|
|
154
|
-
throw new core.OperationOutcomeError(core.badRequest('Missing entry.request.url'));
|
|
155
|
-
}
|
|
156
|
-
}, _BatchProcessor_parsePatchBody = function _BatchProcessor_parsePatchBody(entry) {
|
|
157
|
-
const patchResource = entry.resource;
|
|
158
|
-
if (!patchResource) {
|
|
159
|
-
throw new core.OperationOutcomeError(core.badRequest('Missing entry.resource'));
|
|
160
|
-
}
|
|
161
|
-
if (patchResource.resourceType !== 'Binary') {
|
|
162
|
-
throw new core.OperationOutcomeError(core.badRequest('Patch resource must be a Binary'));
|
|
163
|
-
}
|
|
164
|
-
if (!patchResource.data) {
|
|
165
|
-
throw new core.OperationOutcomeError(core.badRequest('Missing entry.resource.data'));
|
|
166
|
-
}
|
|
167
|
-
return JSON.parse(Buffer.from(patchResource.data, 'base64').toString('utf8'));
|
|
168
|
-
}, _BatchProcessor_addReplacementId = function _BatchProcessor_addReplacementId(fullUrl, resource) {
|
|
169
|
-
if (fullUrl?.startsWith('urn:uuid:')) {
|
|
170
|
-
__classPrivateFieldGet(this, _BatchProcessor_ids, "f")[fullUrl] = resource;
|
|
171
|
-
}
|
|
172
|
-
}, _BatchProcessor_rewriteIds = function _BatchProcessor_rewriteIds(input) {
|
|
173
|
-
if (Array.isArray(input)) {
|
|
174
|
-
return __classPrivateFieldGet(this, _BatchProcessor_instances, "m", _BatchProcessor_rewriteIdsInArray).call(this, input);
|
|
175
|
-
}
|
|
176
|
-
if (typeof input === 'string') {
|
|
177
|
-
return __classPrivateFieldGet(this, _BatchProcessor_instances, "m", _BatchProcessor_rewriteIdsInString).call(this, input);
|
|
178
|
-
}
|
|
179
|
-
if (typeof input === 'object') {
|
|
180
|
-
return __classPrivateFieldGet(this, _BatchProcessor_instances, "m", _BatchProcessor_rewriteIdsInObject).call(this, input);
|
|
181
|
-
}
|
|
182
|
-
return input;
|
|
183
|
-
}, _BatchProcessor_rewriteIdsInArray = function _BatchProcessor_rewriteIdsInArray(input) {
|
|
184
|
-
return input.map((item) => __classPrivateFieldGet(this, _BatchProcessor_instances, "m", _BatchProcessor_rewriteIds).call(this, item));
|
|
185
|
-
}, _BatchProcessor_rewriteIdsInObject = function _BatchProcessor_rewriteIdsInObject(input) {
|
|
186
|
-
return Object.fromEntries(Object.entries(input).map(([k, v]) => [k, __classPrivateFieldGet(this, _BatchProcessor_instances, "m", _BatchProcessor_rewriteIds).call(this, v)]));
|
|
187
|
-
}, _BatchProcessor_rewriteIdsInString = function _BatchProcessor_rewriteIdsInString(input) {
|
|
188
|
-
const matches = input.match(/urn:uuid:\w{8}-\w{4}-\w{4}-\w{4}-\w{12}/);
|
|
189
|
-
if (matches) {
|
|
190
|
-
const fullUrl = matches[0];
|
|
191
|
-
const resource = __classPrivateFieldGet(this, _BatchProcessor_ids, "f")[fullUrl];
|
|
192
|
-
if (resource) {
|
|
193
|
-
return input.replaceAll(fullUrl, core.getReferenceString(resource));
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
return input;
|
|
197
|
-
};
|
|
169
|
+
}
|
|
170
|
+
return input;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
198
173
|
function buildBundleResponse(outcome, resource) {
|
|
199
174
|
return {
|
|
200
175
|
response: {
|
|
@@ -13971,7 +13946,6 @@ spurious results.`);
|
|
|
13971
13946
|
}
|
|
13972
13947
|
}
|
|
13973
13948
|
|
|
13974
|
-
var _MemoryRepository_resources, _MemoryRepository_history;
|
|
13975
13949
|
class BaseRepository {
|
|
13976
13950
|
/**
|
|
13977
13951
|
* Searches for a single FHIR resource.
|
|
@@ -14009,10 +13983,8 @@ spurious results.`);
|
|
|
14009
13983
|
class MemoryRepository extends BaseRepository {
|
|
14010
13984
|
constructor() {
|
|
14011
13985
|
super();
|
|
14012
|
-
|
|
14013
|
-
|
|
14014
|
-
__classPrivateFieldSet(this, _MemoryRepository_resources, {}, "f");
|
|
14015
|
-
__classPrivateFieldSet(this, _MemoryRepository_history, {}, "f");
|
|
13986
|
+
this.resources = new Map();
|
|
13987
|
+
this.history = new Map();
|
|
14016
13988
|
}
|
|
14017
13989
|
async createResource(resource) {
|
|
14018
13990
|
const result = core.deepClone(resource);
|
|
@@ -14029,17 +14001,23 @@ spurious results.`);
|
|
|
14029
14001
|
result.meta.lastUpdated = new Date().toISOString();
|
|
14030
14002
|
}
|
|
14031
14003
|
const { resourceType, id } = result;
|
|
14032
|
-
|
|
14033
|
-
|
|
14004
|
+
let resources = this.resources.get(resourceType);
|
|
14005
|
+
if (!resources) {
|
|
14006
|
+
resources = new Map();
|
|
14007
|
+
this.resources.set(resourceType, resources);
|
|
14034
14008
|
}
|
|
14035
|
-
|
|
14036
|
-
|
|
14009
|
+
resources.set(id, result);
|
|
14010
|
+
let resourceTypeHistory = this.history.get(resourceType);
|
|
14011
|
+
if (!resourceTypeHistory) {
|
|
14012
|
+
resourceTypeHistory = new Map();
|
|
14013
|
+
this.history.set(resourceType, resourceTypeHistory);
|
|
14037
14014
|
}
|
|
14038
|
-
|
|
14039
|
-
|
|
14015
|
+
let resourceHistory = resourceTypeHistory.get(id);
|
|
14016
|
+
if (!resourceHistory) {
|
|
14017
|
+
resourceHistory = [];
|
|
14018
|
+
resourceTypeHistory.set(id, resourceHistory);
|
|
14040
14019
|
}
|
|
14041
|
-
|
|
14042
|
-
__classPrivateFieldGet(this, _MemoryRepository_history, "f")[resourceType][id].push(result);
|
|
14020
|
+
resourceHistory.push(result);
|
|
14043
14021
|
return core.deepClone(result);
|
|
14044
14022
|
}
|
|
14045
14023
|
updateResource(resource) {
|
|
@@ -14068,7 +14046,7 @@ spurious results.`);
|
|
|
14068
14046
|
return this.updateResource(resource);
|
|
14069
14047
|
}
|
|
14070
14048
|
async readResource(resourceType, id) {
|
|
14071
|
-
const resource =
|
|
14049
|
+
const resource = this.resources.get(resourceType)?.get(id);
|
|
14072
14050
|
if (!resource) {
|
|
14073
14051
|
throw new core.OperationOutcomeError(core.notFound);
|
|
14074
14052
|
}
|
|
@@ -14089,14 +14067,17 @@ spurious results.`);
|
|
|
14089
14067
|
return {
|
|
14090
14068
|
resourceType: 'Bundle',
|
|
14091
14069
|
type: 'history',
|
|
14092
|
-
entry: (
|
|
14070
|
+
entry: (this.history.get(resourceType)?.get(id) || [])
|
|
14093
14071
|
.reverse()
|
|
14094
14072
|
.map((version) => ({ resource: core.deepClone(version) })),
|
|
14095
14073
|
};
|
|
14096
14074
|
}
|
|
14097
14075
|
async readVersion(resourceType, id, versionId) {
|
|
14098
14076
|
await this.readResource(resourceType, id);
|
|
14099
|
-
const version =
|
|
14077
|
+
const version = this.history
|
|
14078
|
+
.get(resourceType)
|
|
14079
|
+
?.get(id)
|
|
14080
|
+
?.find((v) => v.meta?.versionId === versionId);
|
|
14100
14081
|
if (!version) {
|
|
14101
14082
|
throw new core.OperationOutcomeError(core.notFound);
|
|
14102
14083
|
}
|
|
@@ -14104,8 +14085,13 @@ spurious results.`);
|
|
|
14104
14085
|
}
|
|
14105
14086
|
async search(searchRequest) {
|
|
14106
14087
|
const { resourceType } = searchRequest;
|
|
14107
|
-
const resources =
|
|
14108
|
-
const result =
|
|
14088
|
+
const resources = this.resources.get(resourceType) || new Map();
|
|
14089
|
+
const result = [];
|
|
14090
|
+
for (const resource of resources.values()) {
|
|
14091
|
+
if (core.matchesSearchRequest(resource, searchRequest)) {
|
|
14092
|
+
result.push(resource);
|
|
14093
|
+
}
|
|
14094
|
+
}
|
|
14109
14095
|
let entry = result.map((resource) => ({ resource: core.deepClone(resource) }));
|
|
14110
14096
|
if (searchRequest.sortRules) {
|
|
14111
14097
|
for (const sortRule of searchRequest.sortRules) {
|
|
@@ -14126,13 +14112,12 @@ spurious results.`);
|
|
|
14126
14112
|
};
|
|
14127
14113
|
}
|
|
14128
14114
|
async deleteResource(resourceType, id) {
|
|
14129
|
-
if (!
|
|
14115
|
+
if (!this.resources.get(resourceType)?.get(id)) {
|
|
14130
14116
|
throw new core.OperationOutcomeError(core.notFound);
|
|
14131
14117
|
}
|
|
14132
|
-
delete
|
|
14118
|
+
this.resources.get(resourceType)?.delete(id);
|
|
14133
14119
|
}
|
|
14134
14120
|
}
|
|
14135
|
-
_MemoryRepository_resources = new WeakMap(), _MemoryRepository_history = new WeakMap();
|
|
14136
14121
|
const sortComparator = (a, b, sortRule) => {
|
|
14137
14122
|
const searchParam = core.globalSchema.types[a.resourceType]?.searchParams?.[sortRule.code];
|
|
14138
14123
|
const expression = searchParam?.expression;
|