@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.
@@ -0,0 +1,15 @@
1
+ import { LoginState, MedplumClient, ProfileResource } from '@medplum/core';
2
+ import { Binary } from '@medplum/fhirtypes';
3
+ export interface MockClientOptions {
4
+ readonly debug?: boolean;
5
+ }
6
+ export declare class MockClient extends MedplumClient {
7
+ readonly debug: boolean;
8
+ activeLoginOverride?: LoginState;
9
+ constructor(clientOptions?: MockClientOptions);
10
+ clear(): void;
11
+ getProfile(): ProfileResource;
12
+ setActiveLoginOverride(activeLoginOverride: LoginState): void;
13
+ getActiveLogin(): LoginState | undefined;
14
+ createBinary(_data: any, filename: string, contentType: string): Promise<Binary>;
15
+ }
@@ -0,0 +1,2 @@
1
+ export * from './client';
2
+ export * from './mocks';
package/dist/cjs/index.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@medplum/core')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@medplum/core'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.medplum = global.medplum || {}, global.medplum.mock = {}), global.medplum.core));
5
- })(this, (function (exports, core) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@medplum/core'), require('fast-json-patch')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', '@medplum/core', 'fast-json-patch'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global.medplum = global.medplum || {}, global.medplum.mock = {}), global.medplum.core, global["fast-json-patch"]));
5
+ })(this, (function (exports, core, fastJsonPatch) { 'use strict';
6
6
 
7
7
  const ExampleAccessPolicy = {
8
8
  resourceType: 'AccessPolicy',
@@ -7137,7 +7137,7 @@
7137
7137
  }
7138
7138
  createResource(resource) {
7139
7139
  var _a, _b;
7140
- const result = JSON.parse(JSON.stringify(resource));
7140
+ const result = core.deepClone(resource);
7141
7141
  if (!result.id) {
7142
7142
  result.id = this.generateId();
7143
7143
  }
@@ -7162,7 +7162,7 @@
7162
7162
  }
7163
7163
  __classPrivateFieldGet(this, _MemoryRepository_resources, "f")[resourceType][id] = result;
7164
7164
  __classPrivateFieldGet(this, _MemoryRepository_history, "f")[resourceType][id].push(result);
7165
- return result;
7165
+ return core.deepClone(result);
7166
7166
  }
7167
7167
  updateResource(resource) {
7168
7168
  const result = JSON.parse(JSON.stringify(resource));
@@ -7176,23 +7176,48 @@
7176
7176
  }
7177
7177
  return this.createResource(result);
7178
7178
  }
7179
+ patchResource(resourceType, id, patch) {
7180
+ var _a, _b;
7181
+ const resource = this.readResource(resourceType, id);
7182
+ let patchResult;
7183
+ try {
7184
+ patchResult = fastJsonPatch.applyPatch(resource, patch, true);
7185
+ }
7186
+ catch (err) {
7187
+ const patchError = err;
7188
+ 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';
7189
+ throw core.badRequest(message);
7190
+ }
7191
+ const patchedResource = patchResult.newDocument;
7192
+ return this.updateResource(patchedResource);
7193
+ }
7179
7194
  readResource(resourceType, id) {
7180
7195
  var _a, _b;
7181
- return (_b = (_a = __classPrivateFieldGet(this, _MemoryRepository_resources, "f")) === null || _a === void 0 ? void 0 : _a[resourceType]) === null || _b === void 0 ? void 0 : _b[id];
7196
+ 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];
7197
+ if (!resource) {
7198
+ throw core.notFound;
7199
+ }
7200
+ return core.deepClone(resource);
7182
7201
  }
7183
7202
  readHistory(resourceType, id) {
7184
7203
  var _a, _b, _c;
7204
+ this.readResource(resourceType, id);
7185
7205
  return {
7186
7206
  resourceType: 'Bundle',
7187
7207
  type: 'history',
7188
7208
  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 : [])
7189
- .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)); })
7190
- .map((version) => ({ resource: version })),
7209
+ .reverse()
7210
+ .map((version) => ({ resource: core.deepClone(version) })),
7191
7211
  };
7192
7212
  }
7193
7213
  readVersion(resourceType, id, versionId) {
7194
7214
  var _a, _b, _c;
7195
- 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; });
7215
+ this.readResource(resourceType, id);
7216
+ 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; });
7217
+ if (!version) {
7218
+ throw core.notFound;
7219
+ }
7220
+ return core.deepClone(version);
7196
7221
  }
7197
7222
  search(searchRequest) {
7198
7223
  var _a;
@@ -7202,7 +7227,7 @@
7202
7227
  return {
7203
7228
  resourceType: 'Bundle',
7204
7229
  type: 'searchset',
7205
- entry: result.map((resource) => ({ resource })),
7230
+ entry: result.map((resource) => ({ resource: core.deepClone(resource) })),
7206
7231
  total: result.length,
7207
7232
  };
7208
7233
  }
@@ -7261,9 +7286,21 @@
7261
7286
  function matchesSearchFilterValue(resource, filter, filterValue) {
7262
7287
  const expression = filter.code.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
7263
7288
  const values = core.evalFhirPath(expression, resource);
7264
- const result = filterValue === '' ||
7265
- values.some((value) => JSON.stringify(value).toLowerCase().includes(filterValue.toLowerCase()));
7266
- return filter.operator === core.Operator.NOT_EQUALS ? !result : result;
7289
+ switch (filter.operator) {
7290
+ case core.Operator.GREATER_THAN:
7291
+ return values.some((value) => value.toString() > filterValue);
7292
+ case core.Operator.GREATER_THAN_OR_EQUALS:
7293
+ return values.some((value) => value.toString() >= filterValue);
7294
+ case core.Operator.LESS_THAN:
7295
+ return values.some((value) => value.toString() < filterValue);
7296
+ case core.Operator.LESS_THAN_OR_EQUALS:
7297
+ return values.some((value) => value.toString() <= filterValue);
7298
+ default: {
7299
+ const result = filterValue === '' ||
7300
+ values.some((value) => JSON.stringify(value).toLowerCase().includes(filterValue.toLowerCase()));
7301
+ return filter.operator === core.Operator.NOT_EQUALS ? !result : result;
7302
+ }
7303
+ }
7267
7304
  }
7268
7305
 
7269
7306
  class MockClient extends core.MedplumClient {
@@ -7577,7 +7614,7 @@
7577
7614
  }
7578
7615
  else if (method === 'POST') {
7579
7616
  if (resourceType && id && operation) {
7580
- return {};
7617
+ return core.allOk;
7581
7618
  }
7582
7619
  else if (resourceType) {
7583
7620
  return this.mockRepo.createResource(JSON.parse(options.body));
@@ -7593,6 +7630,9 @@
7593
7630
  else if (resourceType && id && operation === '_history') {
7594
7631
  return this.mockRepo.readHistory(resourceType, id);
7595
7632
  }
7633
+ else if (resourceType && id === '$csv') {
7634
+ return core.allOk;
7635
+ }
7596
7636
  else if (resourceType && id) {
7597
7637
  return this.mockRepo.readResource(resourceType, id);
7598
7638
  }
@@ -7603,14 +7643,18 @@
7603
7643
  else if (method === 'PUT') {
7604
7644
  return this.mockRepo.updateResource(JSON.parse(options.body));
7605
7645
  }
7606
- else if (method === 'DELETE') {
7646
+ else if (method === 'PATCH') {
7607
7647
  if (resourceType && id) {
7608
- return this.mockRepo.deleteResource(resourceType, id);
7648
+ return this.mockRepo.patchResource(resourceType, id, JSON.parse(options.body));
7609
7649
  }
7610
- else {
7611
- return core.notFound;
7650
+ }
7651
+ else if (method === 'DELETE') {
7652
+ if (resourceType && id) {
7653
+ this.mockRepo.deleteResource(resourceType, id);
7654
+ return core.allOk;
7612
7655
  }
7613
7656
  }
7657
+ throw core.notFound;
7614
7658
  }
7615
7659
  mockFhirGraphqlHandler(_method, _path, options) {
7616
7660
  const { body } = options;
@@ -7639,23 +7683,40 @@
7639
7683
  return {
7640
7684
  resourceType: 'Bundle',
7641
7685
  type: 'batch-response',
7642
- entry: (_a = request.entry) === null || _a === void 0 ? void 0 : _a.map((e) => {
7643
- var _a, _b;
7644
- const url = 'fhir/R4/' + ((_a = e === null || e === void 0 ? void 0 : e.request) === null || _a === void 0 ? void 0 : _a.url);
7645
- const method = (_b = e === null || e === void 0 ? void 0 : e.request) === null || _b === void 0 ? void 0 : _b.method;
7646
- const resource = this.mockHandler(method, url, null);
7647
- if ((resource === null || resource === void 0 ? void 0 : resource.resourceType) === 'OperationOutcome') {
7648
- return { resource, response: { status: core.getStatus(resource).toString() } };
7649
- }
7650
- else if (resource) {
7651
- return { resource, response: { status: '200' } };
7652
- }
7653
- else {
7654
- return { resource: core.notFound, response: { status: '404' } };
7655
- }
7656
- }),
7686
+ entry: (_a = request.entry) === null || _a === void 0 ? void 0 : _a.map((e) => this.handleBatchEntry(e)),
7657
7687
  };
7658
7688
  }
7689
+ handleBatchEntry(input) {
7690
+ var _a, _b;
7691
+ try {
7692
+ const url = 'fhir/R4/' + ((_a = input.request) === null || _a === void 0 ? void 0 : _a.url);
7693
+ const method = (_b = input.request) === null || _b === void 0 ? void 0 : _b.method;
7694
+ const resource = this.mockHandler(method, url, {
7695
+ body: input.resource ? JSON.stringify(input.resource) : undefined,
7696
+ });
7697
+ if ((resource === null || resource === void 0 ? void 0 : resource.resourceType) === 'OperationOutcome') {
7698
+ return {
7699
+ resource,
7700
+ response: { status: core.getStatus(resource).toString() },
7701
+ };
7702
+ }
7703
+ else if (resource) {
7704
+ return { resource, response: { status: '200' } };
7705
+ }
7706
+ else {
7707
+ return { resource: core.notFound, response: { status: '404' } };
7708
+ }
7709
+ }
7710
+ catch (err) {
7711
+ const outcome = err;
7712
+ return {
7713
+ response: {
7714
+ status: core.getStatus(outcome).toString(),
7715
+ outcome,
7716
+ },
7717
+ };
7718
+ }
7719
+ }
7659
7720
  }
7660
7721
 
7661
7722
  exports.BartSimpson = BartSimpson;