@medplum/mock 0.9.29 → 0.9.30

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.js CHANGED
@@ -1,8 +1,46 @@
1
1
  (function (global, factory) {
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';
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';
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 __awaiter(thisArg, _arguments, P, generator) {
23
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
24
+ return new (P || (P = Promise))(function (resolve, reject) {
25
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
27
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
28
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
29
+ });
30
+ }
31
+
32
+ function __classPrivateFieldGet(receiver, state, kind, f) {
33
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
34
+ 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");
35
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
36
+ }
37
+
38
+ function __classPrivateFieldSet(receiver, state, value, kind, f) {
39
+ if (kind === "m") throw new TypeError("Private method is not writable");
40
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
41
+ 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");
42
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
43
+ }
6
44
 
7
45
  const ExampleAccessPolicy = {
8
46
  resourceType: 'AccessPolicy',
@@ -7099,34 +7137,6 @@
7099
7137
  ],
7100
7138
  };
7101
7139
 
7102
- /******************************************************************************
7103
- Copyright (c) Microsoft Corporation.
7104
-
7105
- Permission to use, copy, modify, and/or distribute this software for any
7106
- purpose with or without fee is hereby granted.
7107
-
7108
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
7109
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
7110
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
7111
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
7112
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
7113
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
7114
- PERFORMANCE OF THIS SOFTWARE.
7115
- ***************************************************************************** */
7116
-
7117
- function __classPrivateFieldGet(receiver, state, kind, f) {
7118
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
7119
- 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");
7120
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
7121
- }
7122
-
7123
- function __classPrivateFieldSet(receiver, state, value, kind, f) {
7124
- if (kind === "m") throw new TypeError("Private method is not writable");
7125
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
7126
- 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");
7127
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7128
- }
7129
-
7130
7140
  var _MemoryRepository_resources, _MemoryRepository_history;
7131
7141
  class MemoryRepository {
7132
7142
  constructor() {
@@ -7178,18 +7188,21 @@
7178
7188
  }
7179
7189
  patchResource(resourceType, id, patch) {
7180
7190
  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);
7191
+ return __awaiter(this, void 0, void 0, function* () {
7192
+ const resource = this.readResource(resourceType, id);
7193
+ let patchResult;
7194
+ try {
7195
+ const { applyPatch } = yield import('fast-json-patch');
7196
+ patchResult = applyPatch(resource, patch, true);
7197
+ }
7198
+ catch (err) {
7199
+ const patchError = err;
7200
+ 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';
7201
+ throw core.badRequest(message);
7202
+ }
7203
+ const patchedResource = patchResult.newDocument;
7204
+ return this.updateResource(patchedResource);
7205
+ });
7193
7206
  }
7194
7207
  readResource(resourceType, id) {
7195
7208
  var _a, _b;
@@ -7357,25 +7370,27 @@
7357
7370
  this.initMockRepo();
7358
7371
  }
7359
7372
  mockFetch(url, options) {
7360
- const method = options.method || 'GET';
7361
- const path = url.replace('https://example.com/', '');
7362
- if (this.debug) {
7363
- console.log('MockClient', method, path);
7364
- }
7365
- const response = this.mockHandler(method, path, options);
7366
- if (this.debug && !response) {
7367
- console.log('MockClient: not found', method, path);
7368
- }
7369
- if (this.debug) {
7370
- console.log('MockClient', JSON.stringify(response, null, 2));
7371
- }
7372
- if ((response === null || response === void 0 ? void 0 : response.resourceType) === 'OperationOutcome' && !core.isOk(response)) {
7373
- return Promise.reject(response);
7374
- }
7375
- return Promise.resolve({
7376
- ok: true,
7377
- blob: () => Promise.resolve(response),
7378
- json: () => Promise.resolve(response),
7373
+ return __awaiter(this, void 0, void 0, function* () {
7374
+ const method = options.method || 'GET';
7375
+ const path = url.replace('https://example.com/', '');
7376
+ if (this.debug) {
7377
+ console.log('MockClient', method, path);
7378
+ }
7379
+ const response = yield this.mockHandler(method, path, options);
7380
+ if (this.debug && !response) {
7381
+ console.log('MockClient: not found', method, path);
7382
+ }
7383
+ if (this.debug) {
7384
+ console.log('MockClient', JSON.stringify(response, null, 2));
7385
+ }
7386
+ if ((response === null || response === void 0 ? void 0 : response.resourceType) === 'OperationOutcome' && !core.isOk(response)) {
7387
+ return Promise.reject(response);
7388
+ }
7389
+ return Promise.resolve({
7390
+ ok: true,
7391
+ blob: () => Promise.resolve(response),
7392
+ json: () => Promise.resolve(response),
7393
+ });
7379
7394
  });
7380
7395
  }
7381
7396
  mockCreatePdf(docDefinition, tableLayouts, fonts) {
@@ -7388,21 +7403,23 @@
7388
7403
  return Promise.resolve({});
7389
7404
  }
7390
7405
  mockHandler(method, path, options) {
7391
- if (path.startsWith('admin/')) {
7392
- return this.mockAdminHandler(method, path);
7393
- }
7394
- else if (path.startsWith('auth/')) {
7395
- return this.mockAuthHandler(method, path, options);
7396
- }
7397
- else if (path.startsWith('oauth2/')) {
7398
- return this.mockOAuthHandler(method, path, options);
7399
- }
7400
- else if (path.startsWith('fhir/R4')) {
7401
- return this.mockFhirHandler(method, path, options);
7402
- }
7403
- else {
7404
- return null;
7405
- }
7406
+ return __awaiter(this, void 0, void 0, function* () {
7407
+ if (path.startsWith('admin/')) {
7408
+ return this.mockAdminHandler(method, path);
7409
+ }
7410
+ else if (path.startsWith('auth/')) {
7411
+ return this.mockAuthHandler(method, path, options);
7412
+ }
7413
+ else if (path.startsWith('oauth2/')) {
7414
+ return this.mockOAuthHandler(method, path, options);
7415
+ }
7416
+ else if (path.startsWith('fhir/R4')) {
7417
+ return this.mockFhirHandler(method, path, options);
7418
+ }
7419
+ else {
7420
+ return null;
7421
+ }
7422
+ });
7406
7423
  }
7407
7424
  mockAdminHandler(_method, path) {
7408
7425
  if (path.startsWith('admin/projects/123')) {
@@ -7597,64 +7614,66 @@
7597
7614
  DrAliceSmithSlots.forEach((slot) => this.mockRepo.createResource(slot));
7598
7615
  }
7599
7616
  mockFhirHandler(method, url, options) {
7600
- const path = url.includes('?') ? url.substring(0, url.indexOf('?')) : url;
7601
- const match = /fhir\/R4\/?([^/]+)?\/?([^/]+)?\/?([^/]+)?\/?([^/]+)?/.exec(path);
7602
- const resourceType = match === null || match === void 0 ? void 0 : match[1];
7603
- const id = match === null || match === void 0 ? void 0 : match[2];
7604
- const operation = match === null || match === void 0 ? void 0 : match[3];
7605
- const versionId = match === null || match === void 0 ? void 0 : match[4];
7606
- if (path.startsWith('fhir/R4/ValueSet/$expand')) {
7607
- return exampleValueSet;
7608
- }
7609
- else if (path === 'fhir/R4/$graphql') {
7610
- return this.mockFhirGraphqlHandler(method, path, options);
7611
- }
7612
- else if (path === 'not-found' || id === 'not-found') {
7613
- return core.notFound;
7614
- }
7615
- else if (method === 'POST') {
7616
- if (resourceType && id && operation) {
7617
- return core.allOk;
7617
+ return __awaiter(this, void 0, void 0, function* () {
7618
+ const path = url.includes('?') ? url.substring(0, url.indexOf('?')) : url;
7619
+ const match = /fhir\/R4\/?([^/]+)?\/?([^/]+)?\/?([^/]+)?\/?([^/]+)?/.exec(path);
7620
+ const resourceType = match === null || match === void 0 ? void 0 : match[1];
7621
+ const id = match === null || match === void 0 ? void 0 : match[2];
7622
+ const operation = match === null || match === void 0 ? void 0 : match[3];
7623
+ const versionId = match === null || match === void 0 ? void 0 : match[4];
7624
+ if (path.startsWith('fhir/R4/ValueSet/$expand')) {
7625
+ return exampleValueSet;
7618
7626
  }
7619
- else if (resourceType) {
7620
- return this.mockRepo.createResource(JSON.parse(options.body));
7627
+ else if (path === 'fhir/R4/$graphql') {
7628
+ return this.mockFhirGraphqlHandler(method, path, options);
7621
7629
  }
7622
- else {
7623
- return this.mockFhirBatchHandler(method, path, options);
7630
+ else if (path === 'not-found' || id === 'not-found') {
7631
+ return core.notFound;
7624
7632
  }
7625
- }
7626
- else if (method === 'GET') {
7627
- if (resourceType && id && versionId) {
7628
- return this.mockRepo.readVersion(resourceType, id, versionId);
7629
- }
7630
- else if (resourceType && id && operation === '_history') {
7631
- return this.mockRepo.readHistory(resourceType, id);
7633
+ else if (method === 'POST') {
7634
+ if (resourceType && id && operation) {
7635
+ return core.allOk;
7636
+ }
7637
+ else if (resourceType) {
7638
+ return this.mockRepo.createResource(JSON.parse(options.body));
7639
+ }
7640
+ else {
7641
+ return this.mockFhirBatchHandler(method, path, options);
7642
+ }
7632
7643
  }
7633
- else if (resourceType && id === '$csv') {
7634
- return core.allOk;
7644
+ else if (method === 'GET') {
7645
+ if (resourceType && id && versionId) {
7646
+ return this.mockRepo.readVersion(resourceType, id, versionId);
7647
+ }
7648
+ else if (resourceType && id && operation === '_history') {
7649
+ return this.mockRepo.readHistory(resourceType, id);
7650
+ }
7651
+ else if (resourceType && id === '$csv') {
7652
+ return core.allOk;
7653
+ }
7654
+ else if (resourceType && id) {
7655
+ return this.mockRepo.readResource(resourceType, id);
7656
+ }
7657
+ else if (resourceType) {
7658
+ return this.mockRepo.search(core.parseSearchDefinition(url));
7659
+ }
7635
7660
  }
7636
- else if (resourceType && id) {
7637
- return this.mockRepo.readResource(resourceType, id);
7661
+ else if (method === 'PUT') {
7662
+ return this.mockRepo.updateResource(JSON.parse(options.body));
7638
7663
  }
7639
- else if (resourceType) {
7640
- return this.mockRepo.search(core.parseSearchDefinition(url));
7641
- }
7642
- }
7643
- else if (method === 'PUT') {
7644
- return this.mockRepo.updateResource(JSON.parse(options.body));
7645
- }
7646
- else if (method === 'PATCH') {
7647
- if (resourceType && id) {
7648
- return this.mockRepo.patchResource(resourceType, id, JSON.parse(options.body));
7664
+ else if (method === 'PATCH') {
7665
+ if (resourceType && id) {
7666
+ return this.mockRepo.patchResource(resourceType, id, JSON.parse(options.body));
7667
+ }
7649
7668
  }
7650
- }
7651
- else if (method === 'DELETE') {
7652
- if (resourceType && id) {
7653
- this.mockRepo.deleteResource(resourceType, id);
7654
- return core.allOk;
7669
+ else if (method === 'DELETE') {
7670
+ if (resourceType && id) {
7671
+ this.mockRepo.deleteResource(resourceType, id);
7672
+ return core.allOk;
7673
+ }
7655
7674
  }
7656
- }
7657
- throw core.notFound;
7675
+ throw core.notFound;
7676
+ });
7658
7677
  }
7659
7678
  mockFhirGraphqlHandler(_method, _path, options) {
7660
7679
  const { body } = options;
@@ -7677,45 +7696,55 @@
7677
7696
  return GraphQLSchemaResponse;
7678
7697
  }
7679
7698
  mockFhirBatchHandler(_method, _path, options) {
7680
- var _a;
7681
- const { body } = options;
7682
- const request = JSON.parse(body);
7683
- return {
7684
- resourceType: 'Bundle',
7685
- type: 'batch-response',
7686
- entry: (_a = request.entry) === null || _a === void 0 ? void 0 : _a.map((e) => this.handleBatchEntry(e)),
7687
- };
7699
+ return __awaiter(this, void 0, void 0, function* () {
7700
+ const { body } = options;
7701
+ const request = JSON.parse(body);
7702
+ let entry;
7703
+ if (request.entry) {
7704
+ entry = [];
7705
+ for (const input of request.entry) {
7706
+ entry.push(yield this.handleBatchEntry(input));
7707
+ }
7708
+ }
7709
+ return {
7710
+ resourceType: 'Bundle',
7711
+ type: 'batch-response',
7712
+ entry,
7713
+ };
7714
+ });
7688
7715
  }
7689
7716
  handleBatchEntry(input) {
7690
7717
  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') {
7718
+ return __awaiter(this, void 0, void 0, function* () {
7719
+ try {
7720
+ const url = 'fhir/R4/' + ((_a = input.request) === null || _a === void 0 ? void 0 : _a.url);
7721
+ const method = (_b = input.request) === null || _b === void 0 ? void 0 : _b.method;
7722
+ const resource = yield this.mockHandler(method, url, {
7723
+ body: input.resource ? JSON.stringify(input.resource) : undefined,
7724
+ });
7725
+ if ((resource === null || resource === void 0 ? void 0 : resource.resourceType) === 'OperationOutcome') {
7726
+ return {
7727
+ resource,
7728
+ response: { status: core.getStatus(resource).toString() },
7729
+ };
7730
+ }
7731
+ else if (resource) {
7732
+ return { resource, response: { status: '200' } };
7733
+ }
7734
+ else {
7735
+ return { resource: core.notFound, response: { status: '404' } };
7736
+ }
7737
+ }
7738
+ catch (err) {
7739
+ const outcome = err;
7698
7740
  return {
7699
- resource,
7700
- response: { status: core.getStatus(resource).toString() },
7741
+ response: {
7742
+ status: core.getStatus(outcome).toString(),
7743
+ outcome,
7744
+ },
7701
7745
  };
7702
7746
  }
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
- }
7747
+ });
7719
7748
  }
7720
7749
  }
7721
7750