@medplum/mock 0.9.28 → 0.9.29

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/esm/index.js CHANGED
@@ -1,4 +1,5 @@
1
- import { createReference, getReferenceString, evalFhirPath, Operator, MedplumClient, badRequest, isOk, allOk, notFound, parseSearchDefinition, getStatus } from '@medplum/core';
1
+ import { createReference, getReferenceString, deepClone, badRequest, notFound, evalFhirPath, Operator, MedplumClient, isOk, allOk, parseSearchDefinition, getStatus } from '@medplum/core';
2
+ import { applyPatch } from 'fast-json-patch';
2
3
 
3
4
  const ExampleAccessPolicy = {
4
5
  resourceType: 'AccessPolicy',
@@ -7133,7 +7134,7 @@ class MemoryRepository {
7133
7134
  }
7134
7135
  createResource(resource) {
7135
7136
  var _a, _b;
7136
- const result = JSON.parse(JSON.stringify(resource));
7137
+ const result = deepClone(resource);
7137
7138
  if (!result.id) {
7138
7139
  result.id = this.generateId();
7139
7140
  }
@@ -7158,7 +7159,7 @@ class MemoryRepository {
7158
7159
  }
7159
7160
  __classPrivateFieldGet(this, _MemoryRepository_resources, "f")[resourceType][id] = result;
7160
7161
  __classPrivateFieldGet(this, _MemoryRepository_history, "f")[resourceType][id].push(result);
7161
- return result;
7162
+ return deepClone(result);
7162
7163
  }
7163
7164
  updateResource(resource) {
7164
7165
  const result = JSON.parse(JSON.stringify(resource));
@@ -7172,23 +7173,48 @@ class MemoryRepository {
7172
7173
  }
7173
7174
  return this.createResource(result);
7174
7175
  }
7176
+ patchResource(resourceType, id, patch) {
7177
+ var _a, _b;
7178
+ const resource = this.readResource(resourceType, id);
7179
+ let patchResult;
7180
+ try {
7181
+ patchResult = applyPatch(resource, patch, true);
7182
+ }
7183
+ catch (err) {
7184
+ const patchError = err;
7185
+ const message = ((_b = (_a = patchError.message) === null || _a === void 0 ? void 0 : _a.split('\n')) === null || _b === void 0 ? void 0 : _b[0]) || 'JSONPatch error';
7186
+ throw badRequest(message);
7187
+ }
7188
+ const patchedResource = patchResult.newDocument;
7189
+ return this.updateResource(patchedResource);
7190
+ }
7175
7191
  readResource(resourceType, id) {
7176
7192
  var _a, _b;
7177
- return (_b = (_a = __classPrivateFieldGet(this, _MemoryRepository_resources, "f")) === null || _a === void 0 ? void 0 : _a[resourceType]) === null || _b === void 0 ? void 0 : _b[id];
7193
+ const resource = (_b = (_a = __classPrivateFieldGet(this, _MemoryRepository_resources, "f")) === null || _a === void 0 ? void 0 : _a[resourceType]) === null || _b === void 0 ? void 0 : _b[id];
7194
+ if (!resource) {
7195
+ throw notFound;
7196
+ }
7197
+ return deepClone(resource);
7178
7198
  }
7179
7199
  readHistory(resourceType, id) {
7180
7200
  var _a, _b, _c;
7201
+ this.readResource(resourceType, id);
7181
7202
  return {
7182
7203
  resourceType: 'Bundle',
7183
7204
  type: 'history',
7184
7205
  entry: ((_c = (_b = (_a = __classPrivateFieldGet(this, _MemoryRepository_history, "f")) === null || _a === void 0 ? void 0 : _a[resourceType]) === null || _b === void 0 ? void 0 : _b[id]) !== null && _c !== void 0 ? _c : [])
7185
- .sort((version1, version2) => { var _a, _b, _c; return -((_b = (_a = version1.meta) === null || _a === void 0 ? void 0 : _a.lastUpdated) === null || _b === void 0 ? void 0 : _b.localeCompare((_c = version2.meta) === null || _c === void 0 ? void 0 : _c.lastUpdated)); })
7186
- .map((version) => ({ resource: version })),
7206
+ .reverse()
7207
+ .map((version) => ({ resource: deepClone(version) })),
7187
7208
  };
7188
7209
  }
7189
7210
  readVersion(resourceType, id, versionId) {
7190
7211
  var _a, _b, _c;
7191
- return (_c = (_b = (_a = __classPrivateFieldGet(this, _MemoryRepository_history, "f")) === null || _a === void 0 ? void 0 : _a[resourceType]) === null || _b === void 0 ? void 0 : _b[id]) === null || _c === void 0 ? void 0 : _c.find((v) => { var _a; return ((_a = v.meta) === null || _a === void 0 ? void 0 : _a.versionId) === versionId; });
7212
+ this.readResource(resourceType, id);
7213
+ const version = (_c = (_b = (_a = __classPrivateFieldGet(this, _MemoryRepository_history, "f")) === null || _a === void 0 ? void 0 : _a[resourceType]) === null || _b === void 0 ? void 0 : _b[id]) === null || _c === void 0 ? void 0 : _c.find((v) => { var _a; return ((_a = v.meta) === null || _a === void 0 ? void 0 : _a.versionId) === versionId; });
7214
+ if (!version) {
7215
+ throw notFound;
7216
+ }
7217
+ return deepClone(version);
7192
7218
  }
7193
7219
  search(searchRequest) {
7194
7220
  var _a;
@@ -7198,7 +7224,7 @@ class MemoryRepository {
7198
7224
  return {
7199
7225
  resourceType: 'Bundle',
7200
7226
  type: 'searchset',
7201
- entry: result.map((resource) => ({ resource })),
7227
+ entry: result.map((resource) => ({ resource: deepClone(resource) })),
7202
7228
  total: result.length,
7203
7229
  };
7204
7230
  }
@@ -7257,9 +7283,21 @@ function matchesSearchFilter(resource, filter) {
7257
7283
  function matchesSearchFilterValue(resource, filter, filterValue) {
7258
7284
  const expression = filter.code.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
7259
7285
  const values = evalFhirPath(expression, resource);
7260
- const result = filterValue === '' ||
7261
- values.some((value) => JSON.stringify(value).toLowerCase().includes(filterValue.toLowerCase()));
7262
- return filter.operator === Operator.NOT_EQUALS ? !result : result;
7286
+ switch (filter.operator) {
7287
+ case Operator.GREATER_THAN:
7288
+ return values.some((value) => value.toString() > filterValue);
7289
+ case Operator.GREATER_THAN_OR_EQUALS:
7290
+ return values.some((value) => value.toString() >= filterValue);
7291
+ case Operator.LESS_THAN:
7292
+ return values.some((value) => value.toString() < filterValue);
7293
+ case Operator.LESS_THAN_OR_EQUALS:
7294
+ return values.some((value) => value.toString() <= filterValue);
7295
+ default: {
7296
+ const result = filterValue === '' ||
7297
+ values.some((value) => JSON.stringify(value).toLowerCase().includes(filterValue.toLowerCase()));
7298
+ return filter.operator === Operator.NOT_EQUALS ? !result : result;
7299
+ }
7300
+ }
7263
7301
  }
7264
7302
 
7265
7303
  class MockClient extends MedplumClient {
@@ -7573,7 +7611,7 @@ class MockFetchClient {
7573
7611
  }
7574
7612
  else if (method === 'POST') {
7575
7613
  if (resourceType && id && operation) {
7576
- return {};
7614
+ return allOk;
7577
7615
  }
7578
7616
  else if (resourceType) {
7579
7617
  return this.mockRepo.createResource(JSON.parse(options.body));
@@ -7589,6 +7627,9 @@ class MockFetchClient {
7589
7627
  else if (resourceType && id && operation === '_history') {
7590
7628
  return this.mockRepo.readHistory(resourceType, id);
7591
7629
  }
7630
+ else if (resourceType && id === '$csv') {
7631
+ return allOk;
7632
+ }
7592
7633
  else if (resourceType && id) {
7593
7634
  return this.mockRepo.readResource(resourceType, id);
7594
7635
  }
@@ -7599,14 +7640,18 @@ class MockFetchClient {
7599
7640
  else if (method === 'PUT') {
7600
7641
  return this.mockRepo.updateResource(JSON.parse(options.body));
7601
7642
  }
7602
- else if (method === 'DELETE') {
7643
+ else if (method === 'PATCH') {
7603
7644
  if (resourceType && id) {
7604
- return this.mockRepo.deleteResource(resourceType, id);
7645
+ return this.mockRepo.patchResource(resourceType, id, JSON.parse(options.body));
7605
7646
  }
7606
- else {
7607
- return notFound;
7647
+ }
7648
+ else if (method === 'DELETE') {
7649
+ if (resourceType && id) {
7650
+ this.mockRepo.deleteResource(resourceType, id);
7651
+ return allOk;
7608
7652
  }
7609
7653
  }
7654
+ throw notFound;
7610
7655
  }
7611
7656
  mockFhirGraphqlHandler(_method, _path, options) {
7612
7657
  const { body } = options;
@@ -7635,23 +7680,40 @@ class MockFetchClient {
7635
7680
  return {
7636
7681
  resourceType: 'Bundle',
7637
7682
  type: 'batch-response',
7638
- entry: (_a = request.entry) === null || _a === void 0 ? void 0 : _a.map((e) => {
7639
- var _a, _b;
7640
- const url = 'fhir/R4/' + ((_a = e === null || e === void 0 ? void 0 : e.request) === null || _a === void 0 ? void 0 : _a.url);
7641
- const method = (_b = e === null || e === void 0 ? void 0 : e.request) === null || _b === void 0 ? void 0 : _b.method;
7642
- const resource = this.mockHandler(method, url, null);
7643
- if ((resource === null || resource === void 0 ? void 0 : resource.resourceType) === 'OperationOutcome') {
7644
- return { resource, response: { status: getStatus(resource).toString() } };
7645
- }
7646
- else if (resource) {
7647
- return { resource, response: { status: '200' } };
7648
- }
7649
- else {
7650
- return { resource: notFound, response: { status: '404' } };
7651
- }
7652
- }),
7683
+ entry: (_a = request.entry) === null || _a === void 0 ? void 0 : _a.map((e) => this.handleBatchEntry(e)),
7653
7684
  };
7654
7685
  }
7686
+ handleBatchEntry(input) {
7687
+ var _a, _b;
7688
+ try {
7689
+ const url = 'fhir/R4/' + ((_a = input.request) === null || _a === void 0 ? void 0 : _a.url);
7690
+ const method = (_b = input.request) === null || _b === void 0 ? void 0 : _b.method;
7691
+ const resource = this.mockHandler(method, url, {
7692
+ body: input.resource ? JSON.stringify(input.resource) : undefined,
7693
+ });
7694
+ if ((resource === null || resource === void 0 ? void 0 : resource.resourceType) === 'OperationOutcome') {
7695
+ return {
7696
+ resource,
7697
+ response: { status: getStatus(resource).toString() },
7698
+ };
7699
+ }
7700
+ else if (resource) {
7701
+ return { resource, response: { status: '200' } };
7702
+ }
7703
+ else {
7704
+ return { resource: notFound, response: { status: '404' } };
7705
+ }
7706
+ }
7707
+ catch (err) {
7708
+ const outcome = err;
7709
+ return {
7710
+ response: {
7711
+ status: getStatus(outcome).toString(),
7712
+ outcome,
7713
+ },
7714
+ };
7715
+ }
7716
+ }
7655
7717
  }
7656
7718
 
7657
7719
  export { BartSimpson, DifferentOrganization, DrAliceSmith, DrAliceSmithPreviousVersion, DrAliceSmithSchedule, DrAliceSmithSlots, ExampleAccessPolicy, ExampleAuditEvent, ExampleBot, ExampleClient, ExampleQuestionnaire, ExampleQuestionnaireResponse, ExampleStatusValueSet, ExampleSubscription, ExampleUserConfiguration, ExampleWorkflowPlanDefinition, ExampleWorkflowQuestionnaire1, ExampleWorkflowQuestionnaire2, ExampleWorkflowQuestionnaire3, ExampleWorkflowQuestionnaireResponse1, ExampleWorkflowRequestGroup, ExampleWorkflowTask1, ExampleWorkflowTask2, ExampleWorkflowTask3, GraphQLSchemaResponse, HomerCommunication, HomerDiagnosticReport, HomerEncounter, HomerMedia, HomerObservation1, HomerObservation2, HomerObservation3, HomerObservation4, HomerObservation5, HomerObservation6, HomerServiceRequest, HomerSimpson, HomerSimpsonPreviousVersion, HomerSimpsonSpecimen, MockClient, PatientSearchParameters, TestOrganization, exampleValueSet };