@medplum/core 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.
Files changed (61) hide show
  1. package/dist/cjs/index.cjs +1244 -1101
  2. package/dist/cjs/index.cjs.map +1 -1
  3. package/dist/cjs/index.min.cjs +1 -1
  4. package/dist/esm/base64.mjs +33 -0
  5. package/dist/esm/base64.mjs.map +1 -0
  6. package/dist/esm/cache.mjs +17 -23
  7. package/dist/esm/cache.mjs.map +1 -1
  8. package/dist/esm/client.mjs +500 -413
  9. package/dist/esm/client.mjs.map +1 -1
  10. package/dist/esm/eventtarget.mjs +6 -11
  11. package/dist/esm/eventtarget.mjs.map +1 -1
  12. package/dist/esm/fhirlexer/parse.mjs +15 -23
  13. package/dist/esm/fhirlexer/parse.mjs.map +1 -1
  14. package/dist/esm/fhirlexer/tokenize.mjs +190 -180
  15. package/dist/esm/fhirlexer/tokenize.mjs.map +1 -1
  16. package/dist/esm/fhirmapper/parse.mjs +264 -252
  17. package/dist/esm/fhirmapper/parse.mjs.map +1 -1
  18. package/dist/esm/fhirmapper/tokenize.mjs +0 -1
  19. package/dist/esm/fhirmapper/tokenize.mjs.map +1 -1
  20. package/dist/esm/fhirpath/atoms.mjs +0 -1
  21. package/dist/esm/fhirpath/atoms.mjs.map +1 -1
  22. package/dist/esm/fhirpath/parse.mjs +0 -1
  23. package/dist/esm/fhirpath/parse.mjs.map +1 -1
  24. package/dist/esm/fhirpath/tokenize.mjs +0 -1
  25. package/dist/esm/fhirpath/tokenize.mjs.map +1 -1
  26. package/dist/esm/filter/parse.mjs +0 -2
  27. package/dist/esm/filter/parse.mjs.map +1 -1
  28. package/dist/esm/filter/tokenize.mjs +0 -1
  29. package/dist/esm/filter/tokenize.mjs.map +1 -1
  30. package/dist/esm/format.mjs +24 -15
  31. package/dist/esm/format.mjs.map +1 -1
  32. package/dist/esm/index.min.mjs +1 -1
  33. package/dist/esm/index.mjs +2 -2
  34. package/dist/esm/jwt.mjs +2 -9
  35. package/dist/esm/jwt.mjs.map +1 -1
  36. package/dist/esm/outcomes.mjs +15 -1
  37. package/dist/esm/outcomes.mjs.map +1 -1
  38. package/dist/esm/readablepromise.mjs +18 -23
  39. package/dist/esm/readablepromise.mjs.map +1 -1
  40. package/dist/esm/schema.mjs +146 -123
  41. package/dist/esm/schema.mjs.map +1 -1
  42. package/dist/esm/search/match.mjs +0 -2
  43. package/dist/esm/search/match.mjs.map +1 -1
  44. package/dist/esm/search/search.mjs +15 -9
  45. package/dist/esm/search/search.mjs.map +1 -1
  46. package/dist/esm/storage.mjs +13 -19
  47. package/dist/esm/storage.mjs.map +1 -1
  48. package/dist/types/base64.d.ts +14 -0
  49. package/dist/types/cache.d.ts +3 -1
  50. package/dist/types/client.d.ts +153 -1
  51. package/dist/types/eventtarget.d.ts +1 -1
  52. package/dist/types/fhirlexer/parse.d.ts +5 -2
  53. package/dist/types/fhirlexer/tokenize.d.ts +28 -1
  54. package/dist/types/format.d.ts +3 -1
  55. package/dist/types/outcomes.d.ts +1 -0
  56. package/dist/types/readablepromise.d.ts +4 -1
  57. package/dist/types/schema.d.ts +33 -1
  58. package/dist/types/storage.d.ts +2 -2
  59. package/package.json +1 -1
  60. package/dist/esm/node_modules/tslib/tslib.es6.mjs +0 -30
  61. package/dist/esm/node_modules/tslib/tslib.es6.mjs.map +0 -1
@@ -1,6 +1,6 @@
1
1
  export { convertToTransactionBundle } from './bundle.mjs';
2
2
  export { LRUCache } from './cache.mjs';
3
- export { MEDPLUM_VERSION, MedplumClient } from './client.mjs';
3
+ export { MEDPLUM_VERSION, MedplumClient, OAuthGrantType, OAuthTokenType } from './client.mjs';
4
4
  export { InfixOperatorAtom, Parser, ParserBuilder, PrefixOperatorAtom } from './fhirlexer/parse.mjs';
5
5
  export { Tokenizer } from './fhirlexer/tokenize.mjs';
6
6
  export { parseMappingLanguage } from './fhirmapper/parse.mjs';
@@ -13,7 +13,7 @@ export { FhirFilterComparison, FhirFilterConnective, FhirFilterNegation } from '
13
13
  export { formatAddress, formatCodeableConcept, formatCoding, formatDate, formatDateTime, formatFamilyName, formatGivenName, formatHumanName, formatMoney, formatObservationValue, formatPeriod, formatQuantity, formatRange, formatTime, formatTiming, isValidDate } from './format.mjs';
14
14
  export { Hl7Context, Hl7Field, Hl7Message, Hl7Segment } from './hl7.mjs';
15
15
  export { parseJWTPayload } from './jwt.mjs';
16
- export { OperationOutcomeError, allOk, assertOk, badRequest, created, forbidden, getStatus, gone, isGone, isNotFound, isOk, isOperationOutcome, normalizeErrorString, normalizeOperationOutcome, notFound, notModified, operationOutcomeToString, tooManyRequests, unauthorized, validationError } from './outcomes.mjs';
16
+ export { OperationOutcomeError, accepted, allOk, assertOk, badRequest, created, forbidden, getStatus, gone, isGone, isNotFound, isOk, isOperationOutcome, normalizeErrorString, normalizeOperationOutcome, notFound, notModified, operationOutcomeToString, tooManyRequests, unauthorized, validationError } from './outcomes.mjs';
17
17
  export { ReadablePromise } from './readablepromise.mjs';
18
18
  export { FhirSchemaValidator, checkForNull, createStructureIssue, isResourceType, validateResource, validateResourceType } from './schema.mjs';
19
19
  export { SearchParameterType, getExpressionForResourceType, getSearchParameterDetails } from './search/details.mjs';
package/dist/esm/jwt.mjs CHANGED
@@ -1,3 +1,5 @@
1
+ import { decodeBase64 } from './base64.mjs';
2
+
1
3
  /**
2
4
  * Decodes a section of a JWT.
3
5
  * See: https://tools.ietf.org/html/rfc7519
@@ -13,15 +15,6 @@ function decodePayload(payload) {
13
15
  const jsonPayload = decodeURIComponent(uriEncodedPayload);
14
16
  return JSON.parse(jsonPayload);
15
17
  }
16
- function decodeBase64(data) {
17
- if (typeof window !== 'undefined') {
18
- return window.atob(data);
19
- }
20
- if (typeof Buffer !== 'undefined') {
21
- return Buffer.from(data, 'base64').toString('binary');
22
- }
23
- throw new Error('Unable to decode base64');
24
- }
25
18
  /**
26
19
  * Parses the JWT payload.
27
20
  * @param token JWT token
@@ -1 +1 @@
1
- {"version":3,"file":"jwt.mjs","sources":["../../src/jwt.ts"],"sourcesContent":["/**\n * Decodes a section of a JWT.\n * See: https://tools.ietf.org/html/rfc7519\n * @param payload\n */\nfunction decodePayload(payload: string): Record<string, number | string> {\n const cleanedPayload = payload.replace(/-/g, '+').replace(/_/g, '/');\n const decodedPayload = decodeBase64(cleanedPayload);\n const uriEncodedPayload = Array.from(decodedPayload).reduce((acc, char) => {\n const uriEncodedChar = ('00' + char.charCodeAt(0).toString(16)).slice(-2);\n return `${acc}%${uriEncodedChar}`;\n }, '');\n const jsonPayload = decodeURIComponent(uriEncodedPayload);\n return JSON.parse(jsonPayload);\n}\n\nfunction decodeBase64(data: string): string {\n if (typeof window !== 'undefined') {\n return window.atob(data);\n }\n if (typeof Buffer !== 'undefined') {\n return Buffer.from(data, 'base64').toString('binary');\n }\n throw new Error('Unable to decode base64');\n}\n\n/**\n * Parses the JWT payload.\n * @param token JWT token\n */\nexport function parseJWTPayload(token: string): Record<string, number | string> {\n const [_header, payload, _signature] = token.split('.');\n return decodePayload(payload);\n}\n"],"names":[],"mappings":"AAAA;;;;AAIG;AACH,SAAS,aAAa,CAAC,OAAe,EAAA;AACpC,IAAA,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AACrE,IAAA,MAAM,cAAc,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;AACpD,IAAA,MAAM,iBAAiB,GAAG,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,KAAI;QACxE,MAAM,cAAc,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1E,QAAA,OAAO,CAAG,EAAA,GAAG,CAAI,CAAA,EAAA,cAAc,EAAE,CAAC;KACnC,EAAE,EAAE,CAAC,CAAC;AACP,IAAA,MAAM,WAAW,GAAG,kBAAkB,CAAC,iBAAiB,CAAC,CAAC;AAC1D,IAAA,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACjC,CAAC;AAED,SAAS,YAAY,CAAC,IAAY,EAAA;AAChC,IAAA,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;AACjC,QAAA,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC1B,KAAA;AACD,IAAA,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE;AACjC,QAAA,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;AACvD,KAAA;AACD,IAAA,MAAM,IAAI,KAAK,CAAC,yBAAyB,CAAC,CAAC;AAC7C,CAAC;AAED;;;AAGG;AACG,SAAU,eAAe,CAAC,KAAa,EAAA;AAC3C,IAAA,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AACxD,IAAA,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC;AAChC;;;;"}
1
+ {"version":3,"file":"jwt.mjs","sources":["../../src/jwt.ts"],"sourcesContent":["import { decodeBase64 } from './base64';\n\n/**\n * Decodes a section of a JWT.\n * See: https://tools.ietf.org/html/rfc7519\n * @param payload\n */\nfunction decodePayload(payload: string): Record<string, number | string> {\n const cleanedPayload = payload.replace(/-/g, '+').replace(/_/g, '/');\n const decodedPayload = decodeBase64(cleanedPayload);\n const uriEncodedPayload = Array.from(decodedPayload).reduce((acc, char) => {\n const uriEncodedChar = ('00' + char.charCodeAt(0).toString(16)).slice(-2);\n return `${acc}%${uriEncodedChar}`;\n }, '');\n const jsonPayload = decodeURIComponent(uriEncodedPayload);\n return JSON.parse(jsonPayload);\n}\n\n/**\n * Parses the JWT payload.\n * @param token JWT token\n */\nexport function parseJWTPayload(token: string): Record<string, number | string> {\n const [_header, payload, _signature] = token.split('.');\n return decodePayload(payload);\n}\n"],"names":[],"mappings":";;AAEA;;;;AAIG;AACH,SAAS,aAAa,CAAC,OAAe,EAAA;AACpC,IAAA,MAAM,cAAc,GAAG,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC;AACrE,IAAA,MAAM,cAAc,GAAG,YAAY,CAAC,cAAc,CAAC,CAAC;AACpD,IAAA,MAAM,iBAAiB,GAAG,KAAK,CAAC,IAAI,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,IAAI,KAAI;QACxE,MAAM,cAAc,GAAG,CAAC,IAAI,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;AAC1E,QAAA,OAAO,CAAG,EAAA,GAAG,CAAI,CAAA,EAAA,cAAc,EAAE,CAAC;KACnC,EAAE,EAAE,CAAC,CAAC;AACP,IAAA,MAAM,WAAW,GAAG,kBAAkB,CAAC,iBAAiB,CAAC,CAAC;AAC1D,IAAA,OAAO,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;AACjC,CAAC;AAED;;;AAGG;AACG,SAAU,eAAe,CAAC,KAAa,EAAA;AAC3C,IAAA,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,UAAU,CAAC,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AACxD,IAAA,OAAO,aAAa,CAAC,OAAO,CAAC,CAAC;AAChC;;;;"}
@@ -6,6 +6,7 @@ const NOT_FOUND_ID = 'not-found';
6
6
  const UNAUTHORIZED_ID = 'unauthorized';
7
7
  const FORBIDDEN_ID = 'forbidden';
8
8
  const TOO_MANY_REQUESTS_ID = 'too-many-requests';
9
+ const ACCEPTED_ID = 'accepted';
9
10
  const allOk = {
10
11
  resourceType: 'OperationOutcome',
11
12
  id: OK_ID,
@@ -110,6 +111,19 @@ const tooManyRequests = {
110
111
  },
111
112
  ],
112
113
  };
114
+ const accepted = {
115
+ resourceType: 'OperationOutcome',
116
+ id: ACCEPTED_ID,
117
+ issue: [
118
+ {
119
+ severity: 'information',
120
+ code: 'informational',
121
+ details: {
122
+ text: 'Accepted',
123
+ },
124
+ },
125
+ ],
126
+ };
113
127
  function badRequest(details, expression) {
114
128
  return {
115
129
  resourceType: 'OperationOutcome',
@@ -250,5 +264,5 @@ function operationOutcomeToString(outcome) {
250
264
  return strs.length > 0 ? strs.join('; ') : 'Unknown error';
251
265
  }
252
266
 
253
- export { OperationOutcomeError, allOk, assertOk, badRequest, created, forbidden, getStatus, gone, isGone, isNotFound, isOk, isOperationOutcome, normalizeErrorString, normalizeOperationOutcome, notFound, notModified, operationOutcomeToString, tooManyRequests, unauthorized, validationError };
267
+ export { OperationOutcomeError, accepted, allOk, assertOk, badRequest, created, forbidden, getStatus, gone, isGone, isNotFound, isOk, isOperationOutcome, normalizeErrorString, normalizeOperationOutcome, notFound, notModified, operationOutcomeToString, tooManyRequests, unauthorized, validationError };
254
268
  //# sourceMappingURL=outcomes.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"outcomes.mjs","sources":["../../src/outcomes.ts"],"sourcesContent":["import { OperationOutcome } from '@medplum/fhirtypes';\n\nconst OK_ID = 'ok';\nconst CREATED_ID = 'created';\nconst GONE_ID = 'gone';\nconst NOT_MODIFIED_ID = 'not-modified';\nconst NOT_FOUND_ID = 'not-found';\nconst UNAUTHORIZED_ID = 'unauthorized';\nconst FORBIDDEN_ID = 'forbidden';\nconst TOO_MANY_REQUESTS_ID = 'too-many-requests';\n\nexport const allOk: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: OK_ID,\n issue: [\n {\n severity: 'information',\n code: 'informational',\n details: {\n text: 'All OK',\n },\n },\n ],\n};\n\nexport const created: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: CREATED_ID,\n issue: [\n {\n severity: 'information',\n code: 'informational',\n details: {\n text: 'Created',\n },\n },\n ],\n};\n\nexport const notModified: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: NOT_MODIFIED_ID,\n issue: [\n {\n severity: 'information',\n code: 'informational',\n details: {\n text: 'Not Modified',\n },\n },\n ],\n};\n\nexport const notFound: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: NOT_FOUND_ID,\n issue: [\n {\n severity: 'error',\n code: 'not-found',\n details: {\n text: 'Not found',\n },\n },\n ],\n};\n\nexport const unauthorized: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: UNAUTHORIZED_ID,\n issue: [\n {\n severity: 'error',\n code: 'login',\n details: {\n text: 'Unauthorized',\n },\n },\n ],\n};\n\nexport const forbidden: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: FORBIDDEN_ID,\n issue: [\n {\n severity: 'error',\n code: 'forbidden',\n details: {\n text: 'Forbidden',\n },\n },\n ],\n};\n\nexport const gone: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: GONE_ID,\n issue: [\n {\n severity: 'error',\n code: 'deleted',\n details: {\n text: 'Gone',\n },\n },\n ],\n};\n\nexport const tooManyRequests: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: TOO_MANY_REQUESTS_ID,\n issue: [\n {\n severity: 'error',\n code: 'throttled',\n details: {\n text: 'Too Many Requests',\n },\n },\n ],\n};\n\nexport function badRequest(details: string, expression?: string): OperationOutcome {\n return {\n resourceType: 'OperationOutcome',\n issue: [\n {\n severity: 'error',\n code: 'invalid',\n details: {\n text: details,\n },\n expression: expression ? [expression] : undefined,\n },\n ],\n };\n}\n\nexport function validationError(details: string): OperationOutcome {\n return {\n resourceType: 'OperationOutcome',\n issue: [\n {\n severity: 'error',\n code: 'structure',\n details: {\n text: details,\n },\n },\n ],\n };\n}\n\nexport function isOperationOutcome(value: unknown): value is OperationOutcome {\n return typeof value === 'object' && value !== null && (value as any).resourceType === 'OperationOutcome';\n}\n\nexport function isOk(outcome: OperationOutcome): boolean {\n return outcome.id === OK_ID || outcome.id === CREATED_ID || outcome.id === NOT_MODIFIED_ID;\n}\n\nexport function isNotFound(outcome: OperationOutcome): boolean {\n return outcome.id === NOT_FOUND_ID;\n}\n\nexport function isGone(outcome: OperationOutcome): boolean {\n return outcome.id === GONE_ID;\n}\n\nexport function getStatus(outcome: OperationOutcome): number {\n if (outcome.id === OK_ID) {\n return 200;\n } else if (outcome.id === CREATED_ID) {\n return 201;\n } else if (outcome.id === NOT_MODIFIED_ID) {\n return 304;\n } else if (outcome.id === UNAUTHORIZED_ID) {\n return 401;\n } else if (outcome.id === FORBIDDEN_ID) {\n return 403;\n } else if (outcome.id === NOT_FOUND_ID) {\n return 404;\n } else if (outcome.id === GONE_ID) {\n return 410;\n } else if (outcome.id === TOO_MANY_REQUESTS_ID) {\n return 429;\n } else {\n return 400;\n }\n}\n\n/**\n * Asserts that the operation completed successfully and that the resource is defined.\n * @param outcome The operation outcome.\n * @param resource The resource that may or may not have been returned.\n */\nexport function assertOk<T>(outcome: OperationOutcome, resource: T | undefined): asserts resource is T {\n if (!isOk(outcome) || resource === undefined) {\n throw new OperationOutcomeError(outcome);\n }\n}\n\nexport class OperationOutcomeError extends Error {\n readonly outcome: OperationOutcome;\n\n constructor(outcome: OperationOutcome, cause?: unknown) {\n super(operationOutcomeToString(outcome));\n this.outcome = outcome;\n this.cause = cause;\n }\n}\n\n/**\n * Normalizes an error object into an OperationOutcome.\n * @param error The error value which could be a string, Error, OperationOutcome, or other unknown type.\n * @returns The normalized OperationOutcome.\n */\nexport function normalizeOperationOutcome(error: unknown): OperationOutcome {\n if (error instanceof OperationOutcomeError) {\n return error.outcome;\n }\n if (isOperationOutcome(error)) {\n return error;\n }\n return badRequest(normalizeErrorString(error));\n}\n\n/**\n * Normalizes an error object into a displayable error string.\n * @param error The error value which could be a string, Error, OperationOutcome, or other unknown type.\n * @returns A display string for the error.\n */\nexport function normalizeErrorString(error: unknown): string {\n if (!error) {\n return 'Unknown error';\n }\n if (typeof error === 'string') {\n return error;\n }\n if (error instanceof Error) {\n return error.message;\n }\n if (isOperationOutcome(error)) {\n return operationOutcomeToString(error);\n }\n return JSON.stringify(error);\n}\n\n/**\n * Returns a string represenation of the operation outcome.\n * @param outcome The operation outcome.\n * @returns The string representation of the operation outcome.\n */\nexport function operationOutcomeToString(outcome: OperationOutcome): string {\n const strs = [];\n if (outcome.issue) {\n for (const issue of outcome.issue) {\n let issueStr = issue.details?.text || 'Unknown error';\n if (issue.expression?.length) {\n issueStr += ` (${issue.expression.join(', ')})`;\n }\n strs.push(issueStr);\n }\n }\n return strs.length > 0 ? strs.join('; ') : 'Unknown error';\n}\n"],"names":[],"mappings":"AAEA,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,MAAM,UAAU,GAAG,SAAS,CAAC;AAC7B,MAAM,OAAO,GAAG,MAAM,CAAC;AACvB,MAAM,eAAe,GAAG,cAAc,CAAC;AACvC,MAAM,YAAY,GAAG,WAAW,CAAC;AACjC,MAAM,eAAe,GAAG,cAAc,CAAC;AACvC,MAAM,YAAY,GAAG,WAAW,CAAC;AACjC,MAAM,oBAAoB,GAAG,mBAAmB,CAAC;AAEpC,MAAA,KAAK,GAAqB;AACrC,IAAA,YAAY,EAAE,kBAAkB;AAChC,IAAA,EAAE,EAAE,KAAK;AACT,IAAA,KAAK,EAAE;AACL,QAAA;AACE,YAAA,QAAQ,EAAE,aAAa;AACvB,YAAA,IAAI,EAAE,eAAe;AACrB,YAAA,OAAO,EAAE;AACP,gBAAA,IAAI,EAAE,QAAQ;AACf,aAAA;AACF,SAAA;AACF,KAAA;EACD;AAEW,MAAA,OAAO,GAAqB;AACvC,IAAA,YAAY,EAAE,kBAAkB;AAChC,IAAA,EAAE,EAAE,UAAU;AACd,IAAA,KAAK,EAAE;AACL,QAAA;AACE,YAAA,QAAQ,EAAE,aAAa;AACvB,YA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;AACtB,KAAA;AACD,IAAA,IAAI,kBAAkB,CAAC,KAAK,CAAC,EAAE;AAC7B,QAAA,OAAO,wBAAwB,CAAC,KAAK,CAAC,CAAC;AACxC,KAAA;AACD,IAAA,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;AAC/B,CAAC;AAED;;;;AAIG;AACG,SAAU,wBAAwB,CAAC,OAAyB,EAAA;IAChE,MAAM,IAAI,GAAG,EAAE,CAAC;IAChB,IAAI,OAAO,CAAC,KAAK,EAAE;AACjB,QAAA,KAAK,MAAM,KAAK,IAAI,OAAO,CAAC,KAAK,EAAE;YACjC,IAAI,QAAQ,GAAG,KAAK,CAAC,OAAO,EAAE,IAAI,IAAI,eAAe,CAAC;AACtD,YAAA,IAAI,KAAK,CAAC,UAAU,EAAE,MAAM,EAAE;gBAC5B,QAAQ,IAAI,CAAK,EAAA,EAAA,KAAK,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA,CAAA,CAAG,CAAC;AACjD,aAAA;AACD,YAAA,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;AACrB,SAAA;AACF,KAAA;AACD,IAAA,OAAO,IAAI,CAAC,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,eAAe,CAAC;AAC7D;;;;"}
1
+ {"version":3,"file":"outcomes.mjs","sources":["../../src/outcomes.ts"],"sourcesContent":["import { OperationOutcome } from '@medplum/fhirtypes';\n\nconst OK_ID = 'ok';\nconst CREATED_ID = 'created';\nconst GONE_ID = 'gone';\nconst NOT_MODIFIED_ID = 'not-modified';\nconst NOT_FOUND_ID = 'not-found';\nconst UNAUTHORIZED_ID = 'unauthorized';\nconst FORBIDDEN_ID = 'forbidden';\nconst TOO_MANY_REQUESTS_ID = 'too-many-requests';\nconst ACCEPTED_ID = 'accepted';\n\nexport const allOk: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: OK_ID,\n issue: [\n {\n severity: 'information',\n code: 'informational',\n details: {\n text: 'All OK',\n },\n },\n ],\n};\n\nexport const created: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: CREATED_ID,\n issue: [\n {\n severity: 'information',\n code: 'informational',\n details: {\n text: 'Created',\n },\n },\n ],\n};\n\nexport const notModified: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: NOT_MODIFIED_ID,\n issue: [\n {\n severity: 'information',\n code: 'informational',\n details: {\n text: 'Not Modified',\n },\n },\n ],\n};\n\nexport const notFound: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: NOT_FOUND_ID,\n issue: [\n {\n severity: 'error',\n code: 'not-found',\n details: {\n text: 'Not found',\n },\n },\n ],\n};\n\nexport const unauthorized: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: UNAUTHORIZED_ID,\n issue: [\n {\n severity: 'error',\n code: 'login',\n details: {\n text: 'Unauthorized',\n },\n },\n ],\n};\n\nexport const forbidden: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: FORBIDDEN_ID,\n issue: [\n {\n severity: 'error',\n code: 'forbidden',\n details: {\n text: 'Forbidden',\n },\n },\n ],\n};\n\nexport const gone: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: GONE_ID,\n issue: [\n {\n severity: 'error',\n code: 'deleted',\n details: {\n text: 'Gone',\n },\n },\n ],\n};\n\nexport const tooManyRequests: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: TOO_MANY_REQUESTS_ID,\n issue: [\n {\n severity: 'error',\n code: 'throttled',\n details: {\n text: 'Too Many Requests',\n },\n },\n ],\n};\n\nexport const accepted: OperationOutcome = {\n resourceType: 'OperationOutcome',\n id: ACCEPTED_ID,\n issue: [\n {\n severity: 'information',\n code: 'informational',\n details: {\n text: 'Accepted',\n },\n },\n ],\n};\n\nexport function badRequest(details: string, expression?: string): OperationOutcome {\n return {\n resourceType: 'OperationOutcome',\n issue: [\n {\n severity: 'error',\n code: 'invalid',\n details: {\n text: details,\n },\n expression: expression ? [expression] : undefined,\n },\n ],\n };\n}\n\nexport function validationError(details: string): OperationOutcome {\n return {\n resourceType: 'OperationOutcome',\n issue: [\n {\n severity: 'error',\n code: 'structure',\n details: {\n text: details,\n },\n },\n ],\n };\n}\n\nexport function isOperationOutcome(value: unknown): value is OperationOutcome {\n return typeof value === 'object' && value !== null && (value as any).resourceType === 'OperationOutcome';\n}\n\nexport function isOk(outcome: OperationOutcome): boolean {\n return outcome.id === OK_ID || outcome.id === CREATED_ID || outcome.id === NOT_MODIFIED_ID;\n}\n\nexport function isNotFound(outcome: OperationOutcome): boolean {\n return outcome.id === NOT_FOUND_ID;\n}\n\nexport function isGone(outcome: OperationOutcome): boolean {\n return outcome.id === GONE_ID;\n}\n\nexport function getStatus(outcome: OperationOutcome): number {\n if (outcome.id === OK_ID) {\n return 200;\n } else if (outcome.id === CREATED_ID) {\n return 201;\n } else if (outcome.id === NOT_MODIFIED_ID) {\n return 304;\n } else if (outcome.id === UNAUTHORIZED_ID) {\n return 401;\n } else if (outcome.id === FORBIDDEN_ID) {\n return 403;\n } else if (outcome.id === NOT_FOUND_ID) {\n return 404;\n } else if (outcome.id === GONE_ID) {\n return 410;\n } else if (outcome.id === TOO_MANY_REQUESTS_ID) {\n return 429;\n } else {\n return 400;\n }\n}\n\n/**\n * Asserts that the operation completed successfully and that the resource is defined.\n * @param outcome The operation outcome.\n * @param resource The resource that may or may not have been returned.\n */\nexport function assertOk<T>(outcome: OperationOutcome, resource: T | undefined): asserts resource is T {\n if (!isOk(outcome) || resource === undefined) {\n throw new OperationOutcomeError(outcome);\n }\n}\n\nexport class OperationOutcomeError extends Error {\n readonly outcome: OperationOutcome;\n\n constructor(outcome: OperationOutcome, cause?: unknown) {\n super(operationOutcomeToString(outcome));\n this.outcome = outcome;\n this.cause = cause;\n }\n}\n\n/**\n * Normalizes an error object into an OperationOutcome.\n * @param error The error value which could be a string, Error, OperationOutcome, or other unknown type.\n * @returns The normalized OperationOutcome.\n */\nexport function normalizeOperationOutcome(error: unknown): OperationOutcome {\n if (error instanceof OperationOutcomeError) {\n return error.outcome;\n }\n if (isOperationOutcome(error)) {\n return error;\n }\n return badRequest(normalizeErrorString(error));\n}\n\n/**\n * Normalizes an error object into a displayable error string.\n * @param error The error value which could be a string, Error, OperationOutcome, or other unknown type.\n * @returns A display string for the error.\n */\nexport function normalizeErrorString(error: unknown): string {\n if (!error) {\n return 'Unknown error';\n }\n if (typeof error === 'string') {\n return error;\n }\n if (error instanceof Error) {\n return error.message;\n }\n if (isOperationOutcome(error)) {\n return operationOutcomeToString(error);\n }\n return JSON.stringify(error);\n}\n\n/**\n * Returns a string represenation of the operation outcome.\n * @param outcome The operation outcome.\n * @returns The string representation of the operation outcome.\n */\nexport function operationOutcomeToString(outcome: OperationOutcome): string {\n const strs = [];\n if (outcome.issue) {\n for (const issue of outcome.issue) {\n let issueStr = issue.details?.text || 'Unknown error';\n if (issue.expression?.length) {\n issueStr += ` (${issue.expression.join(', ')})`;\n }\n strs.push(issueStr);\n }\n }\n return strs.length > 0 ? strs.join('; ') : 'Unknown error';\n}\n"],"names":[],"mappings":"AAEA,MAAM,KAAK,GAAG,IAAI,CAAC;AACnB,MAAM,UAAU,GAAG,SAAS,CAAC;AAC7B,MAAM,OAAO,GAAG,MAAM,CAAC;AACvB,MAAM,eAAe,GAAG,cAAc,CAAC;AACvC,MAAM,YAAY,GAAG,WAAW,CAAC;AACjC,MAAM,eAAe,GAAG,cAAc,CAAC;AACvC,MAAM,YAAY,GAAG,WAAW,CAAC;AACjC,MAAM,oBAAoB,GAAG,mBAAmB,CAAC;AACjD,MAAM,WAAW,GAAG,UAAU,CAAC;AAElB,MAAA,KAAK,GAAqB;AACrC,IAAA,YAAY,EAAE,kBAAkB;AAChC,IAAA,EAAE,EAAE,KAAK;AACT,IAAA,KAAK,EAAE;AACL,QAAA;AACE,YAAA,QAAQ,EAAE,aAAa;AACvB,YAAA,IAAI,EAAE,eAAe;AACrB,YAAA,OAAO,EAAE;AACP,gBAAA,I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MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,eAAe,CAAC;AAC7D;;;;"}
@@ -1,6 +1,4 @@
1
- import { __classPrivateFieldSet, __classPrivateFieldGet } from './node_modules/tslib/tslib.es6.mjs';
2
-
3
- var _ReadablePromise_suspender, _ReadablePromise_status, _ReadablePromise_response, _ReadablePromise_error, _a;
1
+ var _a;
4
2
  /**
5
3
  * The ReadablePromise class wraps a request promise suitable for React Suspense.
6
4
  * See: https://blog.logrocket.com/react-suspense-data-fetching/#wrappromise-js
@@ -9,33 +7,30 @@ var _ReadablePromise_suspender, _ReadablePromise_status, _ReadablePromise_respon
9
7
  class ReadablePromise {
10
8
  constructor(requestPromise) {
11
9
  this[_a] = 'ReadablePromise';
12
- _ReadablePromise_suspender.set(this, void 0);
13
- _ReadablePromise_status.set(this, 'pending');
14
- _ReadablePromise_response.set(this, void 0);
15
- _ReadablePromise_error.set(this, void 0);
16
- __classPrivateFieldSet(this, _ReadablePromise_suspender, requestPromise.then((res) => {
17
- __classPrivateFieldSet(this, _ReadablePromise_status, 'success', "f");
18
- __classPrivateFieldSet(this, _ReadablePromise_response, res, "f");
10
+ this.status = 'pending';
11
+ this.suspender = requestPromise.then((res) => {
12
+ this.status = 'success';
13
+ this.response = res;
19
14
  return res;
20
15
  }, (err) => {
21
- __classPrivateFieldSet(this, _ReadablePromise_status, 'error', "f");
22
- __classPrivateFieldSet(this, _ReadablePromise_error, err, "f");
16
+ this.status = 'error';
17
+ this.error = err;
23
18
  throw err;
24
- }), "f");
19
+ });
25
20
  }
26
21
  /**
27
22
  * Returns true if the promise is pending.
28
23
  * @returns True if the Promise is pending.
29
24
  */
30
25
  isPending() {
31
- return __classPrivateFieldGet(this, _ReadablePromise_status, "f") === 'pending';
26
+ return this.status === 'pending';
32
27
  }
33
28
  /**
34
29
  * Returns true if the promise resolved successfully.
35
30
  * @returns True if the Promise resolved successfully.
36
31
  */
37
32
  isOk() {
38
- return __classPrivateFieldGet(this, _ReadablePromise_status, "f") === 'success';
33
+ return this.status === 'success';
39
34
  }
40
35
  /**
41
36
  * Attempts to read the value of the promise.
@@ -45,13 +40,13 @@ class ReadablePromise {
45
40
  * @returns The resolved value of the Promise.
46
41
  */
47
42
  read() {
48
- switch (__classPrivateFieldGet(this, _ReadablePromise_status, "f")) {
43
+ switch (this.status) {
49
44
  case 'pending':
50
- throw __classPrivateFieldGet(this, _ReadablePromise_suspender, "f");
45
+ throw this.suspender;
51
46
  case 'error':
52
- throw __classPrivateFieldGet(this, _ReadablePromise_error, "f");
47
+ throw this.error;
53
48
  default:
54
- return __classPrivateFieldGet(this, _ReadablePromise_response, "f");
49
+ return this.response;
55
50
  }
56
51
  }
57
52
  /**
@@ -61,7 +56,7 @@ class ReadablePromise {
61
56
  * @returns A Promise for the completion of which ever callback is executed.
62
57
  */
63
58
  then(onfulfilled, onrejected) {
64
- return __classPrivateFieldGet(this, _ReadablePromise_suspender, "f").then(onfulfilled, onrejected);
59
+ return this.suspender.then(onfulfilled, onrejected);
65
60
  }
66
61
  /**
67
62
  * Attaches a callback for only the rejection of the Promise.
@@ -69,7 +64,7 @@ class ReadablePromise {
69
64
  * @returns A Promise for the completion of the callback.
70
65
  */
71
66
  catch(onrejected) {
72
- return __classPrivateFieldGet(this, _ReadablePromise_suspender, "f").catch(onrejected);
67
+ return this.suspender.catch(onrejected);
73
68
  }
74
69
  /**
75
70
  * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The
@@ -78,10 +73,10 @@ class ReadablePromise {
78
73
  * @returns A Promise for the completion of the callback.
79
74
  */
80
75
  finally(onfinally) {
81
- return __classPrivateFieldGet(this, _ReadablePromise_suspender, "f").finally(onfinally);
76
+ return this.suspender.finally(onfinally);
82
77
  }
83
78
  }
84
- _ReadablePromise_suspender = new WeakMap(), _ReadablePromise_status = new WeakMap(), _ReadablePromise_response = new WeakMap(), _ReadablePromise_error = new WeakMap(), _a = Symbol.toStringTag;
79
+ _a = Symbol.toStringTag;
85
80
 
86
81
  export { ReadablePromise };
87
82
  //# sourceMappingURL=readablepromise.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"readablepromise.mjs","sources":["../../src/readablepromise.ts"],"sourcesContent":["/**\n * The ReadablePromise class wraps a request promise suitable for React Suspense.\n * See: https://blog.logrocket.com/react-suspense-data-fetching/#wrappromise-js\n * See: https://github.com/ovieokeh/suspense-data-fetching/blob/master/lib/api/wrapPromise.js\n */\nexport class ReadablePromise<T> implements Promise<T> {\n readonly [Symbol.toStringTag]: string = 'ReadablePromise';\n #suspender: Promise<T>;\n #status: 'pending' | 'error' | 'success' = 'pending';\n #response: T | undefined;\n #error: Error | undefined;\n\n constructor(requestPromise: Promise<T>) {\n this.#suspender = requestPromise.then(\n (res: T) => {\n this.#status = 'success';\n this.#response = res;\n return res;\n },\n (err: any) => {\n this.#status = 'error';\n this.#error = err;\n throw err;\n }\n );\n }\n\n /**\n * Returns true if the promise is pending.\n * @returns True if the Promise is pending.\n */\n isPending(): boolean {\n return this.#status === 'pending';\n }\n\n /**\n * Returns true if the promise resolved successfully.\n * @returns True if the Promise resolved successfully.\n */\n isOk(): boolean {\n return this.#status === 'success';\n }\n\n /**\n * Attempts to read the value of the promise.\n * If the promise is pending, this method will throw a promise.\n * If the promise rejected, this method will throw the rejection reason.\n * If the promise resolved, this method will return the resolved value.\n * @returns The resolved value of the Promise.\n */\n read(): T {\n switch (this.#status) {\n case 'pending':\n throw this.#suspender;\n case 'error':\n throw this.#error;\n default:\n return this.#response as T;\n }\n }\n\n /**\n * Attaches callbacks for the resolution and/or rejection of the Promise.\n * @param onfulfilled The callback to execute when the Promise is resolved.\n * @param onrejected The callback to execute when the Promise is rejected.\n * @returns A Promise for the completion of which ever callback is executed.\n */\n then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,\n onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null\n ): Promise<TResult1 | TResult2> {\n return this.#suspender.then(onfulfilled, onrejected);\n }\n\n /**\n * Attaches a callback for only the rejection of the Promise.\n * @param onrejected The callback to execute when the Promise is rejected.\n * @returns A Promise for the completion of the callback.\n */\n catch<TResult = never>(\n onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null\n ): Promise<T | TResult> {\n return this.#suspender.catch(onrejected);\n }\n\n /**\n * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The\n * resolved value cannot be modified from the callback.\n * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).\n * @returns A Promise for the completion of the callback.\n */\n finally(onfinally?: (() => void) | undefined | null): Promise<T> {\n return this.#suspender.finally(onfinally);\n }\n}\n"],"names":[],"mappings":";;;AAAA;;;;AAIG;MACU,eAAe,CAAA;AAO1B,IAAA,WAAA,CAAY,cAA0B,EAAA;QAN7B,IAAoB,CAAA,EAAA,CAAA,GAAW,iBAAiB,CAAC;QAC1D,0BAAuB,CAAA,GAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA,CAAA;AACvB,QAAA,uBAAA,CAAA,GAAA,CAAA,IAAA,EAA2C,SAAS,CAAC,CAAA;QACrD,yBAAyB,CAAA,GAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA,CAAA;QACzB,sBAA0B,CAAA,GAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA,CAAA;QAGxB,sBAAA,CAAA,IAAI,8BAAc,cAAc,CAAC,IAAI,CACnC,CAAC,GAAM,KAAI;AACT,YAAA,sBAAA,CAAA,IAAI,EAAA,uBAAA,EAAW,SAAS,EAAA,GAAA,CAAA,CAAC;AACzB,YAAA,sBAAA,CAAA,IAAI,EAAA,yBAAA,EAAa,GAAG,EAAA,GAAA,CAAA,CAAC;AACrB,YAAA,OAAO,GAAG,CAAC;AACb,SAAC,EACD,CAAC,GAAQ,KAAI;AACX,YAAA,sBAAA,CAAA,IAAI,EAAA,uBAAA,EAAW,OAAO,EAAA,GAAA,CAAA,CAA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SAAS,CAAC,CAAC;KAC3C;AACF,CAAA;AAxFW,0BAAA,GAAA,IAAA,OAAA,EAAA,EAAA,uBAAA,GAAA,IAAA,OAAA,EAAA,EAAA,yBAAA,GAAA,IAAA,OAAA,EAAA,EAAA,sBAAA,GAAA,IAAA,OAAA,EAAA,EAAA,EAAA,GAAA,MAAM,CAAC,WAAW;;;;"}
1
+ {"version":3,"file":"readablepromise.mjs","sources":["../../src/readablepromise.ts"],"sourcesContent":["/**\n * The ReadablePromise class wraps a request promise suitable for React Suspense.\n * See: https://blog.logrocket.com/react-suspense-data-fetching/#wrappromise-js\n * See: https://github.com/ovieokeh/suspense-data-fetching/blob/master/lib/api/wrapPromise.js\n */\nexport class ReadablePromise<T> implements Promise<T> {\n readonly [Symbol.toStringTag]: string = 'ReadablePromise';\n private suspender: Promise<T>;\n private status: 'pending' | 'error' | 'success' = 'pending';\n private response: T | undefined;\n private error: Error | undefined;\n\n constructor(requestPromise: Promise<T>) {\n this.suspender = requestPromise.then(\n (res: T) => {\n this.status = 'success';\n this.response = res;\n return res;\n },\n (err: any) => {\n this.status = 'error';\n this.error = err;\n throw err;\n }\n );\n }\n\n /**\n * Returns true if the promise is pending.\n * @returns True if the Promise is pending.\n */\n isPending(): boolean {\n return this.status === 'pending';\n }\n\n /**\n * Returns true if the promise resolved successfully.\n * @returns True if the Promise resolved successfully.\n */\n isOk(): boolean {\n return this.status === 'success';\n }\n\n /**\n * Attempts to read the value of the promise.\n * If the promise is pending, this method will throw a promise.\n * If the promise rejected, this method will throw the rejection reason.\n * If the promise resolved, this method will return the resolved value.\n * @returns The resolved value of the Promise.\n */\n read(): T {\n switch (this.status) {\n case 'pending':\n throw this.suspender;\n case 'error':\n throw this.error;\n default:\n return this.response as T;\n }\n }\n\n /**\n * Attaches callbacks for the resolution and/or rejection of the Promise.\n * @param onfulfilled The callback to execute when the Promise is resolved.\n * @param onrejected The callback to execute when the Promise is rejected.\n * @returns A Promise for the completion of which ever callback is executed.\n */\n then<TResult1 = T, TResult2 = never>(\n onfulfilled?: ((value: T) => TResult1 | PromiseLike<TResult1>) | undefined | null,\n onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | undefined | null\n ): Promise<TResult1 | TResult2> {\n return this.suspender.then(onfulfilled, onrejected);\n }\n\n /**\n * Attaches a callback for only the rejection of the Promise.\n * @param onrejected The callback to execute when the Promise is rejected.\n * @returns A Promise for the completion of the callback.\n */\n catch<TResult = never>(\n onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | undefined | null\n ): Promise<T | TResult> {\n return this.suspender.catch(onrejected);\n }\n\n /**\n * Attaches a callback that is invoked when the Promise is settled (fulfilled or rejected). The\n * resolved value cannot be modified from the callback.\n * @param onfinally The callback to execute when the Promise is settled (fulfilled or rejected).\n * @returns A Promise for the completion of the callback.\n */\n finally(onfinally?: (() => void) | undefined | null): Promise<T> {\n return this.suspender.finally(onfinally);\n }\n}\n"],"names":[],"mappings":";AAAA;;;;AAIG;MACU,eAAe,CAAA;AAO1B,IAAA,WAAA,CAAY,cAA0B,EAAA;QAN7B,IAAoB,CAAA,EAAA,CAAA,GAAW,iBAAiB,CAAC;QAElD,IAAM,CAAA,MAAA,GAAoC,SAAS,CAAC;QAK1D,IAAI,CAAC,SAAS,GAAG,cAAc,CAAC,IAAI,CAClC,CAAC,GAAM,KAAI;AACT,YAAA,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC;AACxB,YAAA,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC;AACpB,YAAA,OAAO,GAAG,CAAC;AACb,SAAC,EACD,CAAC,GAAQ,KAAI;AACX,YAAA,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;AACtB,YAAA,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC;AACjB,YAAA,MAAM,GAAG,CAAC;AACZ,SAAC,CACF,CAAC;KACH;AAED;;;AAGG;IACH,SAAS,GAAA;AACP,QAAA,OAAO,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC;KAClC;AAED;;;AAGG;IACH,IAAI,GAAA;AACF,QAAA,OAAO,IAAI,CAAC,MAAM,KAAK,SAAS,CAAC;KAClC;AAED;;;;;;AAMG;IACH,IAAI,GAAA;QACF,QAAQ,IAAI,CAAC,MAAM;AACjB,YAAA,KAAK,SAAS;gBACZ,MAAM,IAAI,CAAC,SAAS,CAAC;AACvB,YAAA,KAAK,OAAO;gBACV,MAAM,IAAI,CAAC,KAAK,CAAC;AACnB,YAAA;gBACE,OAAO,IAAI,CAAC,QAAa,CAAC;AAC7B,SAAA;KACF;AAED;;;;;AAKG;IACH,IAAI,CACF,WAAiF,EACjF,UAAmF,EAAA;QAEnF,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,EAAE,UAAU,CAAC,CAAC;KACrD;AAED;;;;AAIG;AACH,IAAA,KAAK,CACH,UAAiF,EAAA;QAEjF,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;KACzC;AAED;;;;;AAKG;AACH,IAAA,OAAO,CAAC,SAA2C,EAAA;QACjD,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;KAC1C;AACF,CAAA;AAxFW,EAAA,GAAA,MAAM,CAAC,WAAW;;;;"}
@@ -1,13 +1,9 @@
1
- import { __classPrivateFieldSet, __classPrivateFieldGet } from './node_modules/tslib/tslib.es6.mjs';
2
- import './fhirlexer/parse.mjs';
3
- import './fhirlexer/tokenize.mjs';
4
1
  import { globalSchema, PropertyType } from './types.mjs';
5
2
  import { isEmpty, isLowerCase, getExtensionValue, capitalize } from './utils.mjs';
6
3
  import { toTypedValue, getTypedPropertyValue } from './fhirpath/utils.mjs';
7
4
  import './fhirpath/parse.mjs';
8
5
  import { OperationOutcomeError, validationError } from './outcomes.mjs';
9
6
 
10
- var _FhirSchemaValidator_instances, _FhirSchemaValidator_issues, _FhirSchemaValidator_root, _FhirSchemaValidator_validateObject, _FhirSchemaValidator_checkProperties, _FhirSchemaValidator_checkProperty, _FhirSchemaValidator_checkPropertyValue, _FhirSchemaValidator_validatePrimitiveType, _FhirSchemaValidator_validateString, _FhirSchemaValidator_validateNumber, _FhirSchemaValidator_checkAdditionalProperties, _FhirSchemaValidator_checkAdditionalProperty, _FhirSchemaValidator_checkPrimitiveElement, _FhirSchemaValidator_createIssue;
11
7
  /*
12
8
  * This file provides schema validation utilities for FHIR JSON objects.
13
9
  *
@@ -169,14 +165,11 @@ function validateResource(resource) {
169
165
  }
170
166
  class FhirSchemaValidator {
171
167
  constructor(root) {
172
- _FhirSchemaValidator_instances.add(this);
173
- _FhirSchemaValidator_issues.set(this, void 0);
174
- _FhirSchemaValidator_root.set(this, void 0);
175
- __classPrivateFieldSet(this, _FhirSchemaValidator_issues, [], "f");
176
- __classPrivateFieldSet(this, _FhirSchemaValidator_root, root, "f");
168
+ this.issues = [];
169
+ this.root = root;
177
170
  }
178
171
  validate() {
179
- const resource = __classPrivateFieldGet(this, _FhirSchemaValidator_root, "f");
172
+ const resource = this.root;
180
173
  if (!resource) {
181
174
  throw new OperationOutcomeError(validationError('Resource is null'));
182
175
  }
@@ -185,142 +178,172 @@ class FhirSchemaValidator {
185
178
  throw new OperationOutcomeError(validationError('Missing resource type'));
186
179
  }
187
180
  // Check for "null" once for the entire object hierarchy
188
- checkForNull(resource, '', __classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f"));
189
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_validateObject).call(this, toTypedValue(resource), resourceType);
190
- if (__classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f").length > 0) {
181
+ checkForNull(resource, '', this.issues);
182
+ this.validateObject(toTypedValue(resource), resourceType);
183
+ if (this.issues.length > 0) {
191
184
  throw new OperationOutcomeError({
192
185
  resourceType: 'OperationOutcome',
193
- issue: __classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f"),
186
+ issue: this.issues,
194
187
  });
195
188
  }
196
189
  }
197
- }
198
- _FhirSchemaValidator_issues = new WeakMap(), _FhirSchemaValidator_root = new WeakMap(), _FhirSchemaValidator_instances = new WeakSet(), _FhirSchemaValidator_validateObject = function _FhirSchemaValidator_validateObject(typedValue, path) {
199
- const definition = globalSchema.types[typedValue.type];
200
- if (!definition) {
201
- throw new OperationOutcomeError(validationError('Unknown type: ' + typedValue.type));
202
- }
203
- const propertyDefinitions = definition.properties;
204
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_checkProperties).call(this, path, propertyDefinitions, typedValue);
205
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_checkAdditionalProperties).call(this, path, typedValue, propertyDefinitions);
206
- }, _FhirSchemaValidator_checkProperties = function _FhirSchemaValidator_checkProperties(path, propertyDefinitions, typedValue) {
207
- for (const [key, elementDefinition] of Object.entries(propertyDefinitions)) {
208
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_checkProperty).call(this, path + '.' + key, elementDefinition, typedValue);
190
+ validateObject(typedValue, path) {
191
+ const definition = globalSchema.types[typedValue.type];
192
+ if (!definition) {
193
+ throw new OperationOutcomeError(validationError('Unknown type: ' + typedValue.type));
194
+ }
195
+ const propertyDefinitions = definition.properties;
196
+ this.checkProperties(path, propertyDefinitions, typedValue);
197
+ this.checkAdditionalProperties(path, typedValue, propertyDefinitions);
209
198
  }
210
- }, _FhirSchemaValidator_checkProperty = function _FhirSchemaValidator_checkProperty(path, elementDefinition, typedValue) {
211
- const propertyName = path.split('.').pop();
212
- const value = getTypedPropertyValue(typedValue, propertyName);
213
- if (isEmpty(value)) {
214
- if (elementDefinition.min !== undefined && elementDefinition.min > 0) {
215
- __classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f").push(createStructureIssue(path, 'Missing required property'));
216
- }
217
- return;
199
+ checkProperties(path, propertyDefinitions, typedValue) {
200
+ for (const [key, elementDefinition] of Object.entries(propertyDefinitions)) {
201
+ this.checkProperty(path + '.' + key, elementDefinition, typedValue);
202
+ }
218
203
  }
219
- if (elementDefinition.max === '*') {
220
- if (!Array.isArray(value)) {
221
- __classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f").push(createStructureIssue(path, 'Expected array for property'));
204
+ checkProperty(path, elementDefinition, typedValue) {
205
+ const propertyName = path.split('.').pop();
206
+ const value = getTypedPropertyValue(typedValue, propertyName);
207
+ if (isEmpty(value)) {
208
+ if (elementDefinition.min !== undefined && elementDefinition.min > 0) {
209
+ this.issues.push(createStructureIssue(path, 'Missing required property'));
210
+ }
222
211
  return;
223
212
  }
224
- for (const item of value) {
225
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_checkPropertyValue).call(this, path, elementDefinition, item);
213
+ if (elementDefinition.max === '*') {
214
+ if (!Array.isArray(value)) {
215
+ this.issues.push(createStructureIssue(path, 'Expected array for property'));
216
+ return;
217
+ }
218
+ for (const item of value) {
219
+ this.checkPropertyValue(path, elementDefinition, item);
220
+ }
221
+ }
222
+ else {
223
+ if (Array.isArray(value)) {
224
+ this.issues.push(createStructureIssue(path, 'Expected single value for property'));
225
+ return;
226
+ }
227
+ this.checkPropertyValue(path, elementDefinition, value);
226
228
  }
227
229
  }
228
- else {
229
- if (Array.isArray(value)) {
230
- __classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f").push(createStructureIssue(path, 'Expected single value for property'));
230
+ checkPropertyValue(path, elementDefinition, typedValue) {
231
+ if (typedValue.value === null) {
232
+ // Null handled separately
231
233
  return;
232
234
  }
233
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_checkPropertyValue).call(this, path, elementDefinition, value);
234
- }
235
- }, _FhirSchemaValidator_checkPropertyValue = function _FhirSchemaValidator_checkPropertyValue(path, elementDefinition, typedValue) {
236
- if (typedValue.value === null) {
237
- // Null handled separately
238
- return;
239
- }
240
- if (isLowerCase(typedValue.type.charAt(0))) {
241
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_validatePrimitiveType).call(this, elementDefinition, typedValue);
242
- }
243
- else {
244
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_validateObject).call(this, typedValue, path);
245
- }
246
- }, _FhirSchemaValidator_validatePrimitiveType = function _FhirSchemaValidator_validatePrimitiveType(elementDefinition, typedValue) {
247
- const { type, value } = typedValue;
248
- if (value === null) {
249
- // Null handled separately, so this code should never be reached
250
- // Leaving this check in place for now, in case we change the null handling
251
- return;
252
- }
253
- // First, make sure the value is the correct JS type
254
- const expectedType = fhirTypeToJsType[typedValue.type];
255
- if (typeof value !== expectedType) {
256
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_createIssue).call(this, elementDefinition, 'Invalid type for ' + type);
257
- return;
258
- }
259
- // Then, perform additional checks for specialty types
260
- if (expectedType === 'string') {
261
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_validateString).call(this, elementDefinition, type, value);
262
- }
263
- else if (expectedType === 'number') {
264
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_validateNumber).call(this, elementDefinition, type, value);
235
+ if (isLowerCase(typedValue.type.charAt(0))) {
236
+ this.validatePrimitiveType(elementDefinition, typedValue);
237
+ }
238
+ else {
239
+ this.validateObject(typedValue, path);
240
+ }
265
241
  }
266
- }, _FhirSchemaValidator_validateString = function _FhirSchemaValidator_validateString(elementDefinition, type, value) {
267
- if (!value.trim()) {
268
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_createIssue).call(this, elementDefinition, 'Invalid empty string');
269
- return;
242
+ validatePrimitiveType(elementDefinition, typedValue) {
243
+ const { type, value } = typedValue;
244
+ if (value === null) {
245
+ // Null handled separately, so this code should never be reached
246
+ // Leaving this check in place for now, in case we change the null handling
247
+ return;
248
+ }
249
+ // First, make sure the value is the correct JS type
250
+ const expectedType = fhirTypeToJsType[typedValue.type];
251
+ if (typeof value !== expectedType) {
252
+ this.createIssue(elementDefinition, 'Invalid type for ' + type);
253
+ return;
254
+ }
255
+ // Then, perform additional checks for specialty types
256
+ if (expectedType === 'string') {
257
+ this.validateString(elementDefinition, type, value);
258
+ }
259
+ else if (expectedType === 'number') {
260
+ this.validateNumber(elementDefinition, type, value);
261
+ }
270
262
  }
271
- // Try to get the regex
272
- const valueDefinition = globalSchema.types[type]?.properties?.['value'];
273
- if (valueDefinition?.type) {
274
- const regex = getExtensionValue(valueDefinition.type[0], 'http://hl7.org/fhir/StructureDefinition/regex');
275
- if (regex) {
276
- if (!value.match(new RegExp(regex))) {
277
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_createIssue).call(this, elementDefinition, 'Invalid ' + type + ' format');
263
+ validateString(elementDefinition, type, value) {
264
+ if (!value.trim()) {
265
+ this.createIssue(elementDefinition, 'Invalid empty string');
266
+ return;
267
+ }
268
+ // Try to get the regex
269
+ const valueDefinition = globalSchema.types[type]?.properties?.['value'];
270
+ if (valueDefinition?.type) {
271
+ const regex = getExtensionValue(valueDefinition.type[0], 'http://hl7.org/fhir/StructureDefinition/regex');
272
+ if (regex) {
273
+ if (!value.match(new RegExp(regex))) {
274
+ this.createIssue(elementDefinition, 'Invalid ' + type + ' format');
275
+ }
278
276
  }
279
277
  }
280
278
  }
281
- }, _FhirSchemaValidator_validateNumber = function _FhirSchemaValidator_validateNumber(elementDefinition, type, value) {
282
- if (isNaN(value) || !isFinite(value)) {
283
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_createIssue).call(this, elementDefinition, 'Invalid ' + type + ' value');
284
- return;
285
- }
286
- if (isIntegerType(type) && !Number.isInteger(value)) {
287
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_createIssue).call(this, elementDefinition, 'Number is not an integer');
288
- }
289
- if (type === PropertyType.positiveInt && value <= 0) {
290
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_createIssue).call(this, elementDefinition, 'Number is less than or equal to zero');
291
- }
292
- if (type === PropertyType.unsignedInt && value < 0) {
293
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_createIssue).call(this, elementDefinition, 'Number is negative');
279
+ validateNumber(elementDefinition, type, value) {
280
+ if (isNaN(value) || !isFinite(value)) {
281
+ this.createIssue(elementDefinition, 'Invalid ' + type + ' value');
282
+ return;
283
+ }
284
+ if (isIntegerType(type) && !Number.isInteger(value)) {
285
+ this.createIssue(elementDefinition, 'Number is not an integer');
286
+ }
287
+ if (type === PropertyType.positiveInt && value <= 0) {
288
+ this.createIssue(elementDefinition, 'Number is less than or equal to zero');
289
+ }
290
+ if (type === PropertyType.unsignedInt && value < 0) {
291
+ this.createIssue(elementDefinition, 'Number is negative');
292
+ }
294
293
  }
295
- }, _FhirSchemaValidator_checkAdditionalProperties = function _FhirSchemaValidator_checkAdditionalProperties(path, typedValue, propertyDefinitions) {
296
- const object = typedValue.value;
297
- for (const key of Object.keys(object)) {
298
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_checkAdditionalProperty).call(this, path, key, typedValue, propertyDefinitions);
294
+ checkAdditionalProperties(path, typedValue, propertyDefinitions) {
295
+ const object = typedValue.value;
296
+ for (const key of Object.keys(object)) {
297
+ this.checkAdditionalProperty(path, key, typedValue, propertyDefinitions);
298
+ }
299
299
  }
300
- }, _FhirSchemaValidator_checkAdditionalProperty = function _FhirSchemaValidator_checkAdditionalProperty(path, key, typedValue, propertyDefinitions) {
301
- if (!baseResourceProperties.has(key) &&
302
- !(key in propertyDefinitions) &&
303
- !isChoiceOfType(key, typedValue, propertyDefinitions) &&
304
- !__classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_checkPrimitiveElement).call(this, path, key, typedValue)) {
305
- const expression = `${path}.${key}`;
306
- __classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f").push(createStructureIssue(expression, `Invalid additional property "${expression}"`));
300
+ /**
301
+ * Checks if the given property is allowed on the given object.
302
+ * @param path The path of the current object.
303
+ * @param key The key of a property to check.
304
+ * @param typedValue The current object.
305
+ * @param propertyDefinitions The property definitions of the current object.
306
+ */
307
+ checkAdditionalProperty(path, key, typedValue, propertyDefinitions) {
308
+ if (!baseResourceProperties.has(key) &&
309
+ !(key in propertyDefinitions) &&
310
+ !isChoiceOfType(key, typedValue, propertyDefinitions) &&
311
+ !this.checkPrimitiveElement(path, key, typedValue)) {
312
+ const expression = `${path}.${key}`;
313
+ this.issues.push(createStructureIssue(expression, `Invalid additional property "${expression}"`));
314
+ }
307
315
  }
308
- }, _FhirSchemaValidator_checkPrimitiveElement = function _FhirSchemaValidator_checkPrimitiveElement(path, key, typedValue) {
309
- // Primitive element starts with underscore
310
- if (!key.startsWith('_')) {
311
- return false;
316
+ /**
317
+ * Checks the element for a primitive.
318
+ *
319
+ * FHIR elements with primitive data types are represented in two parts:
320
+ * 1) A JSON property with the name of the element, which has a JSON type of number, boolean, or string
321
+ * 2) a JSON property with _ prepended to the name of the element, which, if present, contains the value's id and/or extensions
322
+ *
323
+ * See: https://hl7.org/fhir/json.html#primitive
324
+ *
325
+ * @param path The path to the property
326
+ * @param key
327
+ * @param typedValue
328
+ */
329
+ checkPrimitiveElement(path, key, typedValue) {
330
+ // Primitive element starts with underscore
331
+ if (!key.startsWith('_')) {
332
+ return false;
333
+ }
334
+ // Validate the non-underscore property exists
335
+ const primitiveKey = key.slice(1);
336
+ if (!(primitiveKey in typedValue.value)) {
337
+ return false;
338
+ }
339
+ // Then validate the element
340
+ this.validateObject({ type: 'Element', value: typedValue.value[key] }, path);
341
+ return true;
312
342
  }
313
- // Validate the non-underscore property exists
314
- const primitiveKey = key.slice(1);
315
- if (!(primitiveKey in typedValue.value)) {
316
- return false;
343
+ createIssue(elementDefinition, message) {
344
+ this.issues.push(createStructureIssue(elementDefinition.path, message));
317
345
  }
318
- // Then validate the element
319
- __classPrivateFieldGet(this, _FhirSchemaValidator_instances, "m", _FhirSchemaValidator_validateObject).call(this, { type: 'Element', value: typedValue.value[key] }, path);
320
- return true;
321
- }, _FhirSchemaValidator_createIssue = function _FhirSchemaValidator_createIssue(elementDefinition, message) {
322
- __classPrivateFieldGet(this, _FhirSchemaValidator_issues, "f").push(createStructureIssue(elementDefinition.path, message));
323
- };
346
+ }
324
347
  function isIntegerType(propertyType) {
325
348
  return (propertyType === PropertyType.integer ||
326
349
  propertyType === PropertyType.positiveInt ||