@medplum/core 0.9.10 → 0.9.11

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/cody-pdf-test.js CHANGED
@@ -10,7 +10,7 @@ async function main() {
10
10
 
11
11
  console.log('set access token...');
12
12
  medplum.setAccessToken(
13
- 'eyJhbGciOiJSUzI1NiIsImtpZCI6Ijk5MjI2MjNmLTVmMjctNDFiYi04NTQ1LTZkY2IwZmVkODk4MSIsInR5cCI6IkpXVCJ9.eyJsb2dpbl9pZCI6IjE2MDdhZDc0LWNmMTktNDFiZS1iYWIxLWZlNGZmMWI3MDkzYSIsInN1YiI6ImJiNjJmNmQzLTNiYjAtNDRjYS1iN2UzLTBmZDAwYmRmYTYyNCIsInVzZXJuYW1lIjoiYmI2MmY2ZDMtM2JiMC00NGNhLWI3ZTMtMGZkMDBiZGZhNjI0Iiwic2NvcGUiOiJvcGVuaWQiLCJwcm9maWxlIjoiUHJhY3RpdGlvbmVyL2ZkNWFiZjYyLWE5NmEtNDdkYi04MDczLTQ4ZDgxOWI0NWU4NCIsImlhdCI6MTY1NDcxNTA3OSwiaXNzIjoiaHR0cHM6Ly9hcGkubWVkcGx1bS5jb20vIiwiZXhwIjoxNjU0NzE4Njc5fQ.scpSfDKpkMu2V5LlmHoOZ4-grHtG-AsgkbFNkrtAqSjE6jeCzJf_ngHhCtZJsVTE9YFt8zyc1r-8_TRseztrxSQ5_iKa7a5J0Vq64bioJYMmU2cTpTw5q2ADdkVJHrvaM1ZALSpJusC9OlKfeLPEV_IFsIWTQu2lsO1k2mmlgHEZpKZBpH3h1KVajOZHIKj2l8WFiu5YlTUNp2BeCmQAndDBv3M8saHuw4pssS-N_mpAmFZy_2nuS3hNBpKBXhdKQeV0njutdF9MMzBkbMHGk8ZgY8Np5OMJ6XZTdfW9bIp8RK2zFUWpZIvGEWZhuGOdtmYx85DIXLS1o5eQSiz4NA'
13
+ 'eyJhbGciOiJSUzI1NiIsImtpZCI6Ijk5MjI2MjNmLTVmMjctNDFiYi04NTQ1LTZkY2IwZmVkODk4MSIsInR5cCI6IkpXVCJ9.eyJsb2dpbl9pZCI6ImVkNmNiY2Q2LTVjZjQtNDljMS05ODQzLWU1OWIyZmRlN2Q5NCIsInN1YiI6ImJiNjJmNmQzLTNiYjAtNDRjYS1iN2UzLTBmZDAwYmRmYTYyNCIsInVzZXJuYW1lIjoiYmI2MmY2ZDMtM2JiMC00NGNhLWI3ZTMtMGZkMDBiZGZhNjI0Iiwic2NvcGUiOiJvcGVuaWQiLCJwcm9maWxlIjoiUHJhY3RpdGlvbmVyL2ZkNWFiZjYyLWE5NmEtNDdkYi04MDczLTQ4ZDgxOWI0NWU4NCIsImlhdCI6MTY1NDgwOTgzOSwiaXNzIjoiaHR0cHM6Ly9hcGkubWVkcGx1bS5jb20vIiwiZXhwIjoxNjU0ODEzNDM5fQ.acK1yXI8v4_a2UWikBwOaa6JItXhh4fma92pQeIuRTsfvnduWC8eOCGOOemxwpCvtrnECCilG1FNVDHTtGKFSVzStAi9TAnO-pHf69OVRJVH1xMqJvlEi-LRiu3uCflOhiqKcnGRDfpecZ0sTcLhIpSg-Kn2-nj8pGz_DG27JfjKpPIqU2X53S3SumZU-2bRPxoSu7lPXINn_1qMTxUOpV1qY54gqX4GVhim2OTdbARx-QtO9Q7A3JR64CvS9_WLbBzsDUqfKfJJ-X6KOHnM8Dz-dieV5rjQ54Sv3yvl-B5gozMvO2ZYm4P3fQ_IBEosim8CHQEa_23UTFGEo36Axg'
14
14
  );
15
15
 
16
16
  console.log('createpdf...');
package/dist/cjs/index.js CHANGED
@@ -810,106 +810,6 @@
810
810
  }
811
811
  }
812
812
 
813
- /*
814
- * This file attempts a unified "generatePdf" function that works both client-side and server-side.
815
- * On client-side, it checks for a global "pdfMake" variable.
816
- * On server-side, it dynamically loads "pdfmake" from the node_modules.
817
- */
818
- function generatePdf(docDefinition, tableLayouts, fonts) {
819
- return __awaiter(this, void 0, void 0, function* () {
820
- // Setup sane defaults
821
- // See: https://pdfmake.github.io/docs/0.1/document-definition-object/styling/
822
- docDefinition.pageSize = docDefinition.pageSize || 'LETTER';
823
- docDefinition.pageMargins = docDefinition.pageMargins || [60, 60, 60, 60];
824
- docDefinition.pageOrientation = docDefinition.pageOrientation || 'portrait';
825
- docDefinition.defaultStyle = docDefinition.defaultStyle || {};
826
- docDefinition.defaultStyle.font = docDefinition.defaultStyle.font || 'Helvetica';
827
- docDefinition.defaultStyle.fontSize = docDefinition.defaultStyle.fontSize || 11;
828
- docDefinition.defaultStyle.lineHeight = docDefinition.defaultStyle.lineHeight || 2.0;
829
- if (typeof window !== 'undefined' && typeof pdfMake !== 'undefined') {
830
- return generatePdfClientSide(docDefinition, tableLayouts, fonts);
831
- }
832
- if (typeof process !== 'undefined' && typeof require !== 'undefined') {
833
- return generatePdfServerSide(docDefinition, tableLayouts, fonts);
834
- }
835
- throw new Error('Unable to determine PDF environment');
836
- });
837
- }
838
- function generatePdfServerSide(docDefinition, tableLayouts, fonts) {
839
- return __awaiter(this, void 0, void 0, function* () {
840
- if (!fonts) {
841
- fonts = {
842
- Helvetica: {
843
- normal: 'Helvetica',
844
- bold: 'Helvetica-Bold',
845
- italics: 'Helvetica-Oblique',
846
- bolditalics: 'Helvetica-BoldOblique',
847
- },
848
- };
849
- }
850
- return new Promise((resolve, reject) => {
851
- try {
852
- // eslint-disable-next-line @typescript-eslint/no-var-requires
853
- const PdfPrinter = require('pdfmake');
854
- const printer = new PdfPrinter(fonts);
855
- const pdfDoc = printer.createPdfKitDocument(docDefinition, { tableLayouts });
856
- const chunks = [];
857
- pdfDoc.on('data', (chunk) => chunks.push(chunk));
858
- pdfDoc.on('end', () => resolve(concat(chunks)));
859
- pdfDoc.on('error', reject);
860
- pdfDoc.end();
861
- }
862
- catch (err) {
863
- reject(err);
864
- }
865
- });
866
- });
867
- }
868
- function generatePdfClientSide(docDefinition, tableLayouts, fonts) {
869
- return __awaiter(this, void 0, void 0, function* () {
870
- if (!fonts) {
871
- fonts = {
872
- Helvetica: {
873
- normal: 'https://static.medplum.com/fonts/Helvetica.ttf',
874
- bold: 'https://static.medplum.com/fonts/Helvetica-bold.ttf',
875
- italics: 'https://static.medplum.com/fonts/Helvetica-italic.ttf',
876
- bolditalics: 'https://static.medplum.com/fonts/Helvetica-bold-italic.ttf',
877
- },
878
- Roboto: {
879
- normal: 'https://static.medplum.com/fonts/Roboto-Regular.ttf',
880
- bold: 'https://static.medplum.com/fonts/Roboto-Medium.ttf',
881
- italics: 'https://static.medplum.com/fonts/Roboto-Italic.ttf',
882
- bolditalics: 'https://static.medplum.com/fonts/Roboto-MediumItalic.ttf',
883
- },
884
- Avenir: {
885
- normal: 'https://static.medplum.com/fonts/avenir.ttf',
886
- },
887
- };
888
- }
889
- return new Promise((resolve) => {
890
- pdfMake.createPdf(docDefinition, tableLayouts, fonts).getBlob(resolve);
891
- });
892
- });
893
- }
894
- /**
895
- * Concatenates an array of Uint8Arrays into a single Uint8Array.
896
- * @param arrays An array of arrays of bytes.
897
- * @returns A single array of bytes.
898
- */
899
- function concat(arrays) {
900
- let len = 0;
901
- for (const array of arrays) {
902
- len += array.length;
903
- }
904
- const result = new Uint8Array(len);
905
- let index = 0;
906
- for (const array of arrays) {
907
- result.set(array, index);
908
- index += array.length;
909
- }
910
- return result;
911
- }
912
-
913
813
  var _ReadablePromise_suspender, _ReadablePromise_status, _ReadablePromise_response, _ReadablePromise_error, _a;
914
814
  /**
915
815
  * The ReadablePromise class wraps a request promise suitable for React Suspense.
@@ -986,206 +886,6 @@
986
886
  }
987
887
  _ReadablePromise_suspender = new WeakMap(), _ReadablePromise_status = new WeakMap(), _ReadablePromise_response = new WeakMap(), _ReadablePromise_error = new WeakMap(), _a = Symbol.toStringTag;
988
888
 
989
- const DEFAULT_SEARCH_COUNT = 20;
990
- /**
991
- * Search operators.
992
- * These operators represent "modifiers" and "prefixes" in FHIR search.
993
- * See: https://www.hl7.org/fhir/search.html
994
- */
995
- exports.Operator = void 0;
996
- (function (Operator) {
997
- Operator["EQUALS"] = "eq";
998
- Operator["NOT_EQUALS"] = "ne";
999
- // Numbers
1000
- Operator["GREATER_THAN"] = "gt";
1001
- Operator["LESS_THAN"] = "lt";
1002
- Operator["GREATER_THAN_OR_EQUALS"] = "ge";
1003
- Operator["LESS_THAN_OR_EQUALS"] = "le";
1004
- // Dates
1005
- Operator["STARTS_AFTER"] = "sa";
1006
- Operator["ENDS_BEFORE"] = "eb";
1007
- Operator["APPROXIMATELY"] = "ap";
1008
- // String
1009
- Operator["CONTAINS"] = "contains";
1010
- Operator["EXACT"] = "exact";
1011
- // Token
1012
- Operator["TEXT"] = "text";
1013
- Operator["ABOVE"] = "above";
1014
- Operator["BELOW"] = "below";
1015
- Operator["IN"] = "in";
1016
- Operator["NOT_IN"] = "not-in";
1017
- Operator["OF_TYPE"] = "of-type";
1018
- })(exports.Operator || (exports.Operator = {}));
1019
- const MODIFIER_OPERATORS = [
1020
- exports.Operator.CONTAINS,
1021
- exports.Operator.EXACT,
1022
- exports.Operator.TEXT,
1023
- exports.Operator.ABOVE,
1024
- exports.Operator.BELOW,
1025
- exports.Operator.IN,
1026
- exports.Operator.NOT_IN,
1027
- exports.Operator.OF_TYPE,
1028
- ];
1029
- const PREFIX_OPERATORS = [
1030
- exports.Operator.NOT_EQUALS,
1031
- exports.Operator.GREATER_THAN,
1032
- exports.Operator.LESS_THAN,
1033
- exports.Operator.GREATER_THAN_OR_EQUALS,
1034
- exports.Operator.LESS_THAN_OR_EQUALS,
1035
- exports.Operator.STARTS_AFTER,
1036
- exports.Operator.ENDS_BEFORE,
1037
- exports.Operator.APPROXIMATELY,
1038
- ];
1039
- /**
1040
- * Parses a URL into a SearchRequest.
1041
- *
1042
- * See the FHIR search spec: http://hl7.org/fhir/r4/search.html
1043
- *
1044
- * @param url The URL to parse.
1045
- * @returns Parsed search definition.
1046
- */
1047
- function parseSearchDefinition(url) {
1048
- const location = new URL(url, 'https://example.com/');
1049
- const resourceType = location.pathname
1050
- .replace(/(^\/)|(\/$)/g, '') // Remove leading and trailing slashes
1051
- .split('/')
1052
- .pop();
1053
- const params = new URLSearchParams(location.search);
1054
- let filters = undefined;
1055
- let sortRules = undefined;
1056
- let fields = undefined;
1057
- let offset = undefined;
1058
- let count = undefined;
1059
- let total = undefined;
1060
- params.forEach((value, key) => {
1061
- if (key === '_fields') {
1062
- fields = value.split(',');
1063
- }
1064
- else if (key === '_offset') {
1065
- offset = parseInt(value);
1066
- }
1067
- else if (key === '_count') {
1068
- count = parseInt(value);
1069
- }
1070
- else if (key === '_total') {
1071
- total = value;
1072
- }
1073
- else if (key === '_sort') {
1074
- sortRules = sortRules || [];
1075
- sortRules.push(parseSortRule(value));
1076
- }
1077
- else {
1078
- filters = filters || [];
1079
- filters.push(parseSearchFilter(key, value));
1080
- }
1081
- });
1082
- return {
1083
- resourceType,
1084
- filters,
1085
- fields,
1086
- offset,
1087
- count,
1088
- total,
1089
- sortRules,
1090
- };
1091
- }
1092
- /**
1093
- * Parses a URL query parameter into a sort rule.
1094
- *
1095
- * By default, the sort rule is the field name.
1096
- *
1097
- * Sort rules can be reversed into descending order by prefixing the field name with a minus sign.
1098
- *
1099
- * See sorting: http://hl7.org/fhir/r4/search.html#_sort
1100
- *
1101
- * @param value The URL parameter value.
1102
- * @returns The parsed sort rule.
1103
- */
1104
- function parseSortRule(value) {
1105
- if (value.startsWith('-')) {
1106
- return { code: value.substring(1), descending: true };
1107
- }
1108
- else {
1109
- return { code: value };
1110
- }
1111
- }
1112
- /**
1113
- * Parses a URL query parameter into a search filter.
1114
- *
1115
- * FHIR search filters can be specified as modifiers or prefixes.
1116
- *
1117
- * For string properties, modifiers are appended to the key, e.g. "name:contains=eve".
1118
- *
1119
- * For date and numeric properties, prefixes are prepended to the value, e.g. "birthdate=gt2000".
1120
- *
1121
- * See the FHIR search spec: http://hl7.org/fhir/r4/search.html
1122
- *
1123
- * @param key The URL parameter key.
1124
- * @param value The URL parameter value.
1125
- * @returns The parsed search filter.
1126
- */
1127
- function parseSearchFilter(key, value) {
1128
- let code = key;
1129
- let operator = exports.Operator.EQUALS;
1130
- for (const modifier of MODIFIER_OPERATORS) {
1131
- const modifierIndex = code.indexOf(':' + modifier);
1132
- if (modifierIndex !== -1) {
1133
- operator = modifier;
1134
- code = code.substring(0, modifierIndex);
1135
- }
1136
- }
1137
- for (const prefix of PREFIX_OPERATORS) {
1138
- if (value.match(new RegExp('^' + prefix + '\\d'))) {
1139
- operator = prefix;
1140
- value = value.substring(prefix.length);
1141
- }
1142
- }
1143
- return { code, operator, value };
1144
- }
1145
- /**
1146
- * Formats a search definition object into a query string.
1147
- * Note: The return value does not include the resource type.
1148
- * @param {!SearchRequest} definition The search definition.
1149
- * @returns Formatted URL.
1150
- */
1151
- function formatSearchQuery(definition) {
1152
- const params = [];
1153
- if (definition.fields) {
1154
- params.push('_fields=' + definition.fields.join(','));
1155
- }
1156
- if (definition.filters) {
1157
- definition.filters.forEach((filter) => params.push(formatFilter(filter)));
1158
- }
1159
- if (definition.sortRules && definition.sortRules.length > 0) {
1160
- params.push(formatSortRules(definition.sortRules));
1161
- }
1162
- if (definition.offset !== undefined) {
1163
- params.push('_offset=' + definition.offset);
1164
- }
1165
- if (definition.count !== undefined) {
1166
- params.push('_count=' + definition.count);
1167
- }
1168
- if (definition.total !== undefined) {
1169
- params.push('_total=' + definition.total);
1170
- }
1171
- if (params.length === 0) {
1172
- return '';
1173
- }
1174
- params.sort();
1175
- return '?' + params.join('&');
1176
- }
1177
- function formatFilter(filter) {
1178
- const modifier = MODIFIER_OPERATORS.includes(filter.operator) ? ':' + filter.operator : '';
1179
- const prefix = PREFIX_OPERATORS.includes(filter.operator) ? filter.operator : '';
1180
- return `${filter.code}${modifier}=${prefix}${encodeURIComponent(filter.value)}`;
1181
- }
1182
- function formatSortRules(sortRules) {
1183
- if (!sortRules || sortRules.length === 0) {
1184
- return '';
1185
- }
1186
- return '_sort=' + sortRules.map((sr) => (sr.descending ? '-' + sr.code : sr.code)).join(',');
1187
- }
1188
-
1189
889
  var _ClientStorage_storage, _MemoryStorage_data;
1190
890
  /**
1191
891
  * The ClientStorage class is a utility class for storing strings and objects.
@@ -1474,7 +1174,7 @@
1474
1174
 
1475
1175
  // PKCE auth ased on:
1476
1176
  // https://aws.amazon.com/blogs/security/how-to-add-authentication-single-page-web-application-with-amazon-cognito-oauth2-implementation/
1477
- var _MedplumClient_instances, _MedplumClient_fetch, _MedplumClient_storage, _MedplumClient_schema, _MedplumClient_requestCache, _MedplumClient_baseUrl, _MedplumClient_clientId, _MedplumClient_authorizeUrl, _MedplumClient_tokenUrl, _MedplumClient_logoutUrl, _MedplumClient_onUnauthenticated, _MedplumClient_accessToken, _MedplumClient_refreshToken, _MedplumClient_refreshPromise, _MedplumClient_profilePromise, _MedplumClient_profile, _MedplumClient_config, _MedplumClient_addLogin, _MedplumClient_refreshProfile, _MedplumClient_request, _MedplumClient_addFetchOptionsDefaults, _MedplumClient_setRequestContentType, _MedplumClient_setRequestBody, _MedplumClient_handleUnauthenticated, _MedplumClient_startPkce, _MedplumClient_requestAuthorization, _MedplumClient_refresh, _MedplumClient_fetchTokens, _MedplumClient_verifyTokens, _MedplumClient_setupStorageListener;
1177
+ var _MedplumClient_instances, _MedplumClient_fetch, _MedplumClient_createPdf, _MedplumClient_storage, _MedplumClient_schema, _MedplumClient_requestCache, _MedplumClient_baseUrl, _MedplumClient_clientId, _MedplumClient_authorizeUrl, _MedplumClient_tokenUrl, _MedplumClient_logoutUrl, _MedplumClient_onUnauthenticated, _MedplumClient_accessToken, _MedplumClient_refreshToken, _MedplumClient_refreshPromise, _MedplumClient_profilePromise, _MedplumClient_profile, _MedplumClient_config, _MedplumClient_addLogin, _MedplumClient_refreshProfile, _MedplumClient_request, _MedplumClient_addFetchOptionsDefaults, _MedplumClient_setRequestContentType, _MedplumClient_setRequestBody, _MedplumClient_handleUnauthenticated, _MedplumClient_startPkce, _MedplumClient_requestAuthorization, _MedplumClient_refresh, _MedplumClient_fetchTokens, _MedplumClient_verifyTokens, _MedplumClient_setupStorageListener;
1478
1178
  const DEFAULT_BASE_URL = 'https://api.medplum.com/';
1479
1179
  const DEFAULT_SCOPE = 'launch/patient openid fhirUser offline_access user/*.*';
1480
1180
  const DEFAULT_RESOURCE_CACHE_SIZE = 1000;
@@ -1534,6 +1234,7 @@
1534
1234
  super();
1535
1235
  _MedplumClient_instances.add(this);
1536
1236
  _MedplumClient_fetch.set(this, void 0);
1237
+ _MedplumClient_createPdf.set(this, void 0);
1537
1238
  _MedplumClient_storage.set(this, void 0);
1538
1239
  _MedplumClient_schema.set(this, void 0);
1539
1240
  _MedplumClient_requestCache.set(this, void 0);
@@ -1558,6 +1259,7 @@
1558
1259
  }
1559
1260
  }
1560
1261
  __classPrivateFieldSet(this, _MedplumClient_fetch, (options === null || options === void 0 ? void 0 : options.fetch) || window.fetch.bind(window), "f");
1262
+ __classPrivateFieldSet(this, _MedplumClient_createPdf, options === null || options === void 0 ? void 0 : options.createPdf, "f");
1561
1263
  __classPrivateFieldSet(this, _MedplumClient_storage, new ClientStorage(), "f");
1562
1264
  __classPrivateFieldSet(this, _MedplumClient_schema, createSchema(), "f");
1563
1265
  __classPrivateFieldSet(this, _MedplumClient_requestCache, new LRUCache((_a = options === null || options === void 0 ? void 0 : options.resourceCacheSize) !== null && _a !== void 0 ? _a : DEFAULT_RESOURCE_CACHE_SIZE), "f");
@@ -1802,12 +1504,9 @@
1802
1504
  * @param query The FHIR search query or structured query object.
1803
1505
  * @returns The well-formed FHIR URL.
1804
1506
  */
1805
- fhirSearchUrl(query) {
1806
- if (typeof query === 'string') {
1807
- return this.fhirUrl(query);
1808
- }
1809
- const url = this.fhirUrl(query.resourceType);
1810
- url.search = formatSearchQuery(query);
1507
+ fhirSearchUrl(resourceType, query) {
1508
+ const url = this.fhirUrl(resourceType);
1509
+ url.search = query.toString();
1811
1510
  return url;
1812
1511
  }
1813
1512
  /**
@@ -1864,8 +1563,8 @@
1864
1563
  * @param query The search query as either a string or a structured search object.
1865
1564
  * @returns Promise to the search result bundle.
1866
1565
  */
1867
- search(query, options = {}) {
1868
- return this.get(this.fhirSearchUrl(query), options);
1566
+ search(resourceType, query, options = {}) {
1567
+ return this.get(this.fhirSearchUrl(resourceType, query), options);
1869
1568
  }
1870
1569
  /**
1871
1570
  * Sends a FHIR search request for a single resource.
@@ -1886,17 +1585,18 @@
1886
1585
  * @param query The search query as either a string or a structured search object.
1887
1586
  * @returns Promise to the search result bundle.
1888
1587
  */
1889
- searchOne(query, options = {}) {
1890
- const search = typeof query === 'string' ? parseSearchDefinition(query) : query;
1891
- search.count = 1;
1892
- const cacheKey = this.fhirSearchUrl(query).toString() + '-searchOne';
1588
+ searchOne(resourceType, query, options = {}) {
1589
+ const url = this.fhirSearchUrl(resourceType, query);
1590
+ url.searchParams.set('_count', '1');
1591
+ url.searchParams.sort();
1592
+ const cacheKey = url.toString() + '-searchOne';
1893
1593
  if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1894
1594
  const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1895
1595
  if (cached) {
1896
1596
  return cached;
1897
1597
  }
1898
1598
  }
1899
- const promise = new ReadablePromise(this.search(search, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.resource; }));
1599
+ const promise = new ReadablePromise(this.search(resourceType, url.searchParams, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.resource; }));
1900
1600
  __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1901
1601
  return promise;
1902
1602
  }
@@ -1919,15 +1619,16 @@
1919
1619
  * @param query The search query as either a string or a structured search object.
1920
1620
  * @returns Promise to the search result bundle.
1921
1621
  */
1922
- searchResources(query, options = {}) {
1923
- const cacheKey = this.fhirSearchUrl(query).toString() + '-searchResources';
1622
+ searchResources(resourceType, query, options = {}) {
1623
+ const url = this.fhirSearchUrl(resourceType, query);
1624
+ const cacheKey = url.toString() + '-searchResources';
1924
1625
  if (!(options === null || options === void 0 ? void 0 : options.cache)) {
1925
1626
  const cached = __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").get(cacheKey);
1926
1627
  if (cached) {
1927
1628
  return cached;
1928
1629
  }
1929
1630
  }
1930
- const promise = new ReadablePromise(this.search(query, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a.map((e) => e.resource)) !== null && _b !== void 0 ? _b : []; }));
1631
+ const promise = new ReadablePromise(this.search(resourceType, query, options).then((b) => { var _a, _b; return (_b = (_a = b.entry) === null || _a === void 0 ? void 0 : _a.map((e) => e.resource)) !== null && _b !== void 0 ? _b : []; }));
1931
1632
  __classPrivateFieldGet(this, _MedplumClient_requestCache, "f").set(cacheKey, promise);
1932
1633
  return promise;
1933
1634
  }
@@ -2184,7 +1885,7 @@
2184
1885
  createResourceIfNoneExist(resource, query) {
2185
1886
  var _a;
2186
1887
  return __awaiter(this, void 0, void 0, function* () {
2187
- return (_a = (yield this.searchOne(`${resource.resourceType}?${query}`))) !== null && _a !== void 0 ? _a : this.createResource(resource);
1888
+ return ((_a = (yield this.searchOne(resource.resourceType, query))) !== null && _a !== void 0 ? _a : this.createResource(resource));
2188
1889
  });
2189
1890
  }
2190
1891
  /**
@@ -2240,7 +1941,10 @@
2240
1941
  */
2241
1942
  createPdf(docDefinition, filename, tableLayouts, fonts) {
2242
1943
  return __awaiter(this, void 0, void 0, function* () {
2243
- const blob = yield generatePdf(docDefinition, tableLayouts, fonts);
1944
+ if (!__classPrivateFieldGet(this, _MedplumClient_createPdf, "f")) {
1945
+ throw new Error('PDF creation not enabled');
1946
+ }
1947
+ const blob = yield __classPrivateFieldGet(this, _MedplumClient_createPdf, "f").call(this, docDefinition, tableLayouts, fonts);
2244
1948
  return this.createBinary(blob, filename, 'application/pdf');
2245
1949
  });
2246
1950
  }
@@ -2503,7 +2207,7 @@
2503
2207
  });
2504
2208
  }
2505
2209
  }
2506
- _MedplumClient_fetch = new WeakMap(), _MedplumClient_storage = new WeakMap(), _MedplumClient_schema = new WeakMap(), _MedplumClient_requestCache = new WeakMap(), _MedplumClient_baseUrl = new WeakMap(), _MedplumClient_clientId = new WeakMap(), _MedplumClient_authorizeUrl = new WeakMap(), _MedplumClient_tokenUrl = new WeakMap(), _MedplumClient_logoutUrl = new WeakMap(), _MedplumClient_onUnauthenticated = new WeakMap(), _MedplumClient_accessToken = new WeakMap(), _MedplumClient_refreshToken = new WeakMap(), _MedplumClient_refreshPromise = new WeakMap(), _MedplumClient_profilePromise = new WeakMap(), _MedplumClient_profile = new WeakMap(), _MedplumClient_config = new WeakMap(), _MedplumClient_instances = new WeakSet(), _MedplumClient_addLogin = function _MedplumClient_addLogin(newLogin) {
2210
+ _MedplumClient_fetch = new WeakMap(), _MedplumClient_createPdf = new WeakMap(), _MedplumClient_storage = new WeakMap(), _MedplumClient_schema = new WeakMap(), _MedplumClient_requestCache = new WeakMap(), _MedplumClient_baseUrl = new WeakMap(), _MedplumClient_clientId = new WeakMap(), _MedplumClient_authorizeUrl = new WeakMap(), _MedplumClient_tokenUrl = new WeakMap(), _MedplumClient_logoutUrl = new WeakMap(), _MedplumClient_onUnauthenticated = new WeakMap(), _MedplumClient_accessToken = new WeakMap(), _MedplumClient_refreshToken = new WeakMap(), _MedplumClient_refreshPromise = new WeakMap(), _MedplumClient_profilePromise = new WeakMap(), _MedplumClient_profile = new WeakMap(), _MedplumClient_config = new WeakMap(), _MedplumClient_instances = new WeakSet(), _MedplumClient_addLogin = function _MedplumClient_addLogin(newLogin) {
2507
2211
  const logins = this.getLogins().filter((login) => { var _a, _b; return ((_a = login.profile) === null || _a === void 0 ? void 0 : _a.reference) !== ((_b = newLogin.profile) === null || _b === void 0 ? void 0 : _b.reference); });
2508
2212
  logins.push(newLogin);
2509
2213
  __classPrivateFieldGet(this, _MedplumClient_storage, "f").setObject('logins', logins);
@@ -5332,6 +5036,206 @@
5332
5036
  }
5333
5037
  }
5334
5038
 
5039
+ const DEFAULT_SEARCH_COUNT = 20;
5040
+ /**
5041
+ * Search operators.
5042
+ * These operators represent "modifiers" and "prefixes" in FHIR search.
5043
+ * See: https://www.hl7.org/fhir/search.html
5044
+ */
5045
+ exports.Operator = void 0;
5046
+ (function (Operator) {
5047
+ Operator["EQUALS"] = "eq";
5048
+ Operator["NOT_EQUALS"] = "ne";
5049
+ // Numbers
5050
+ Operator["GREATER_THAN"] = "gt";
5051
+ Operator["LESS_THAN"] = "lt";
5052
+ Operator["GREATER_THAN_OR_EQUALS"] = "ge";
5053
+ Operator["LESS_THAN_OR_EQUALS"] = "le";
5054
+ // Dates
5055
+ Operator["STARTS_AFTER"] = "sa";
5056
+ Operator["ENDS_BEFORE"] = "eb";
5057
+ Operator["APPROXIMATELY"] = "ap";
5058
+ // String
5059
+ Operator["CONTAINS"] = "contains";
5060
+ Operator["EXACT"] = "exact";
5061
+ // Token
5062
+ Operator["TEXT"] = "text";
5063
+ Operator["ABOVE"] = "above";
5064
+ Operator["BELOW"] = "below";
5065
+ Operator["IN"] = "in";
5066
+ Operator["NOT_IN"] = "not-in";
5067
+ Operator["OF_TYPE"] = "of-type";
5068
+ })(exports.Operator || (exports.Operator = {}));
5069
+ const MODIFIER_OPERATORS = [
5070
+ exports.Operator.CONTAINS,
5071
+ exports.Operator.EXACT,
5072
+ exports.Operator.TEXT,
5073
+ exports.Operator.ABOVE,
5074
+ exports.Operator.BELOW,
5075
+ exports.Operator.IN,
5076
+ exports.Operator.NOT_IN,
5077
+ exports.Operator.OF_TYPE,
5078
+ ];
5079
+ const PREFIX_OPERATORS = [
5080
+ exports.Operator.NOT_EQUALS,
5081
+ exports.Operator.GREATER_THAN,
5082
+ exports.Operator.LESS_THAN,
5083
+ exports.Operator.GREATER_THAN_OR_EQUALS,
5084
+ exports.Operator.LESS_THAN_OR_EQUALS,
5085
+ exports.Operator.STARTS_AFTER,
5086
+ exports.Operator.ENDS_BEFORE,
5087
+ exports.Operator.APPROXIMATELY,
5088
+ ];
5089
+ /**
5090
+ * Parses a URL into a SearchRequest.
5091
+ *
5092
+ * See the FHIR search spec: http://hl7.org/fhir/r4/search.html
5093
+ *
5094
+ * @param url The URL to parse.
5095
+ * @returns Parsed search definition.
5096
+ */
5097
+ function parseSearchDefinition(url) {
5098
+ const location = new URL(url, 'https://example.com/');
5099
+ const resourceType = location.pathname
5100
+ .replace(/(^\/)|(\/$)/g, '') // Remove leading and trailing slashes
5101
+ .split('/')
5102
+ .pop();
5103
+ const params = new URLSearchParams(location.search);
5104
+ let filters = undefined;
5105
+ let sortRules = undefined;
5106
+ let fields = undefined;
5107
+ let offset = undefined;
5108
+ let count = undefined;
5109
+ let total = undefined;
5110
+ params.forEach((value, key) => {
5111
+ if (key === '_fields') {
5112
+ fields = value.split(',');
5113
+ }
5114
+ else if (key === '_offset') {
5115
+ offset = parseInt(value);
5116
+ }
5117
+ else if (key === '_count') {
5118
+ count = parseInt(value);
5119
+ }
5120
+ else if (key === '_total') {
5121
+ total = value;
5122
+ }
5123
+ else if (key === '_sort') {
5124
+ sortRules = sortRules || [];
5125
+ sortRules.push(parseSortRule(value));
5126
+ }
5127
+ else {
5128
+ filters = filters || [];
5129
+ filters.push(parseSearchFilter(key, value));
5130
+ }
5131
+ });
5132
+ return {
5133
+ resourceType,
5134
+ filters,
5135
+ fields,
5136
+ offset,
5137
+ count,
5138
+ total,
5139
+ sortRules,
5140
+ };
5141
+ }
5142
+ /**
5143
+ * Parses a URL query parameter into a sort rule.
5144
+ *
5145
+ * By default, the sort rule is the field name.
5146
+ *
5147
+ * Sort rules can be reversed into descending order by prefixing the field name with a minus sign.
5148
+ *
5149
+ * See sorting: http://hl7.org/fhir/r4/search.html#_sort
5150
+ *
5151
+ * @param value The URL parameter value.
5152
+ * @returns The parsed sort rule.
5153
+ */
5154
+ function parseSortRule(value) {
5155
+ if (value.startsWith('-')) {
5156
+ return { code: value.substring(1), descending: true };
5157
+ }
5158
+ else {
5159
+ return { code: value };
5160
+ }
5161
+ }
5162
+ /**
5163
+ * Parses a URL query parameter into a search filter.
5164
+ *
5165
+ * FHIR search filters can be specified as modifiers or prefixes.
5166
+ *
5167
+ * For string properties, modifiers are appended to the key, e.g. "name:contains=eve".
5168
+ *
5169
+ * For date and numeric properties, prefixes are prepended to the value, e.g. "birthdate=gt2000".
5170
+ *
5171
+ * See the FHIR search spec: http://hl7.org/fhir/r4/search.html
5172
+ *
5173
+ * @param key The URL parameter key.
5174
+ * @param value The URL parameter value.
5175
+ * @returns The parsed search filter.
5176
+ */
5177
+ function parseSearchFilter(key, value) {
5178
+ let code = key;
5179
+ let operator = exports.Operator.EQUALS;
5180
+ for (const modifier of MODIFIER_OPERATORS) {
5181
+ const modifierIndex = code.indexOf(':' + modifier);
5182
+ if (modifierIndex !== -1) {
5183
+ operator = modifier;
5184
+ code = code.substring(0, modifierIndex);
5185
+ }
5186
+ }
5187
+ for (const prefix of PREFIX_OPERATORS) {
5188
+ if (value.match(new RegExp('^' + prefix + '\\d'))) {
5189
+ operator = prefix;
5190
+ value = value.substring(prefix.length);
5191
+ }
5192
+ }
5193
+ return { code, operator, value };
5194
+ }
5195
+ /**
5196
+ * Formats a search definition object into a query string.
5197
+ * Note: The return value does not include the resource type.
5198
+ * @param {!SearchRequest} definition The search definition.
5199
+ * @returns Formatted URL.
5200
+ */
5201
+ function formatSearchQuery(definition) {
5202
+ const params = [];
5203
+ if (definition.fields) {
5204
+ params.push('_fields=' + definition.fields.join(','));
5205
+ }
5206
+ if (definition.filters) {
5207
+ definition.filters.forEach((filter) => params.push(formatFilter(filter)));
5208
+ }
5209
+ if (definition.sortRules && definition.sortRules.length > 0) {
5210
+ params.push(formatSortRules(definition.sortRules));
5211
+ }
5212
+ if (definition.offset !== undefined) {
5213
+ params.push('_offset=' + definition.offset);
5214
+ }
5215
+ if (definition.count !== undefined) {
5216
+ params.push('_count=' + definition.count);
5217
+ }
5218
+ if (definition.total !== undefined) {
5219
+ params.push('_total=' + definition.total);
5220
+ }
5221
+ if (params.length === 0) {
5222
+ return '';
5223
+ }
5224
+ params.sort();
5225
+ return '?' + params.join('&');
5226
+ }
5227
+ function formatFilter(filter) {
5228
+ const modifier = MODIFIER_OPERATORS.includes(filter.operator) ? ':' + filter.operator : '';
5229
+ const prefix = PREFIX_OPERATORS.includes(filter.operator) ? filter.operator : '';
5230
+ return `${filter.code}${modifier}=${prefix}${encodeURIComponent(filter.value)}`;
5231
+ }
5232
+ function formatSortRules(sortRules) {
5233
+ if (!sortRules || sortRules.length === 0) {
5234
+ return '';
5235
+ }
5236
+ return '_sort=' + sortRules.map((sr) => (sr.descending ? '-' + sr.code : sr.code)).join(',');
5237
+ }
5238
+
5335
5239
  exports.SearchParameterType = void 0;
5336
5240
  (function (SearchParameterType) {
5337
5241
  SearchParameterType["BOOLEAN"] = "BOOLEAN";