@medplum/core 2.0.13 → 2.0.15

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 (59) hide show
  1. package/dist/cjs/index.cjs +1259 -1065
  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/bundle.mjs +36 -0
  7. package/dist/esm/bundle.mjs.map +1 -0
  8. package/dist/esm/cache.mjs +17 -23
  9. package/dist/esm/cache.mjs.map +1 -1
  10. package/dist/esm/client.mjs +523 -385
  11. package/dist/esm/client.mjs.map +1 -1
  12. package/dist/esm/eventtarget.mjs +6 -11
  13. package/dist/esm/eventtarget.mjs.map +1 -1
  14. package/dist/esm/fhirlexer/parse.mjs +15 -23
  15. package/dist/esm/fhirlexer/parse.mjs.map +1 -1
  16. package/dist/esm/fhirlexer/tokenize.mjs +190 -180
  17. package/dist/esm/fhirlexer/tokenize.mjs.map +1 -1
  18. package/dist/esm/fhirmapper/parse.mjs +264 -252
  19. package/dist/esm/fhirmapper/parse.mjs.map +1 -1
  20. package/dist/esm/fhirmapper/tokenize.mjs +2 -4
  21. package/dist/esm/fhirmapper/tokenize.mjs.map +1 -1
  22. package/dist/esm/fhirpath/atoms.mjs +13 -20
  23. package/dist/esm/fhirpath/atoms.mjs.map +1 -1
  24. package/dist/esm/fhirpath/parse.mjs +0 -1
  25. package/dist/esm/fhirpath/parse.mjs.map +1 -1
  26. package/dist/esm/fhirpath/tokenize.mjs +0 -1
  27. package/dist/esm/fhirpath/tokenize.mjs.map +1 -1
  28. package/dist/esm/filter/parse.mjs +1 -4
  29. package/dist/esm/filter/parse.mjs.map +1 -1
  30. package/dist/esm/filter/tokenize.mjs +2 -4
  31. package/dist/esm/filter/tokenize.mjs.map +1 -1
  32. package/dist/esm/index.min.mjs +1 -1
  33. package/dist/esm/index.mjs +1 -0
  34. package/dist/esm/index.mjs.map +1 -1
  35. package/dist/esm/jwt.mjs +2 -9
  36. package/dist/esm/jwt.mjs.map +1 -1
  37. package/dist/esm/readablepromise.mjs +18 -23
  38. package/dist/esm/readablepromise.mjs.map +1 -1
  39. package/dist/esm/schema.mjs +149 -127
  40. package/dist/esm/schema.mjs.map +1 -1
  41. package/dist/esm/search/match.mjs +1 -4
  42. package/dist/esm/search/match.mjs.map +1 -1
  43. package/dist/esm/storage.mjs +13 -19
  44. package/dist/esm/storage.mjs.map +1 -1
  45. package/dist/types/base64.d.ts +14 -0
  46. package/dist/types/bundle.d.ts +11 -0
  47. package/dist/types/cache.d.ts +3 -1
  48. package/dist/types/client.d.ts +164 -1
  49. package/dist/types/eventtarget.d.ts +1 -1
  50. package/dist/types/fhirlexer/parse.d.ts +5 -2
  51. package/dist/types/fhirlexer/tokenize.d.ts +28 -1
  52. package/dist/types/fhirpath/atoms.d.ts +1 -1
  53. package/dist/types/index.d.ts +1 -0
  54. package/dist/types/readablepromise.d.ts +4 -1
  55. package/dist/types/schema.d.ts +33 -1
  56. package/dist/types/storage.d.ts +2 -2
  57. package/package.json +1 -1
  58. package/dist/esm/node_modules/tslib/tslib.es6.mjs +0 -30
  59. package/dist/esm/node_modules/tslib/tslib.es6.mjs.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"schema.mjs","sources":["../../src/schema.ts"],"sourcesContent":["import { ElementDefinition, OperationOutcomeIssue, Resource } from '@medplum/fhirtypes';\nimport { getTypedPropertyValue, toTypedValue } from './fhirpath';\nimport { OperationOutcomeError, validationError } from './outcomes';\nimport { globalSchema, PropertyType, TypedValue } from './types';\nimport { capitalize, getExtensionValue, isEmpty, isLowerCase } from './utils';\n\n/*\n * This file provides schema validation utilities for FHIR JSON objects.\n *\n * See: [JSON Representation of Resources](https://hl7.org/fhir/json.html)\n * See: [FHIR Data Types](https://www.hl7.org/fhir/datatypes.html)\n */\n\nconst fhirTypeToJsType: Record<string, string> = {\n base64Binary: 'string',\n boolean: 'boolean',\n canonical: 'string',\n code: 'string',\n date: 'string',\n dateTime: 'string',\n decimal: 'number',\n id: 'string',\n instant: 'string',\n integer: 'number',\n markdown: 'string',\n oid: 'string',\n positiveInt: 'number',\n string: 'string',\n time: 'string',\n unsignedInt: 'number',\n uri: 'string',\n url: 'string',\n uuid: 'string',\n xhtml: 'string',\n 'http://hl7.org/fhirpath/System.String': 'string',\n};\n\nconst baseResourceProperties = new Set<string>([\n // Resource\n 'resourceType',\n 'id',\n 'meta',\n 'implicitRules',\n 'language',\n\n // DomainResource\n 'text',\n 'contained',\n 'extension',\n 'modifierExtension',\n]);\n\n/**\n * Returns true if the given string is a valid FHIR resource type.\n *\n * ```ts\n * isResourceType('Patient'); // true\n * isResourceType('XYZ'); // false\n * ```\n *\n * Note that this depends on globalSchema, which is populated by the StructureDefinition loader.\n *\n * In a server context, you can load all schema definitions:\n *\n * ```ts\n * import { indexStructureDefinitionBundle } from '@medplum/core';\n * import { readJson } from '@medplum/definitions';\n * import { Bundle } from '@medplum/fhirtypes';\n *\n * indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);\n * ```\n *\n * In a client context, you can load the schema definitions using MedplumClient:\n *\n * ```ts\n * import { MedplumClient } from '@medplum/core';\n *\n * const medplum = new MedplumClient();\n * await medplum.requestSchema('Patient');\n * ```\n *\n * @param resourceType The candidate resource type string.\n * @returns True if the resource type is a valid FHIR resource type.\n */\nexport function isResourceType(resourceType: string): boolean {\n const typeSchema = globalSchema.types[resourceType];\n return (\n typeSchema &&\n typeSchema.structureDefinition.id === resourceType &&\n typeSchema.structureDefinition.kind === 'resource'\n );\n}\n\n/**\n * Validates that the given string is a valid FHIR resource type.\n * On success, silently returns void.\n * On failure, throws an OperationOutcomeError.\n *\n * ```ts\n * validateResourceType('Patient'); // nothing\n * validateResourceType('XYZ'); // throws OperationOutcomeError\n * ```\n *\n * Note that this depends on globalSchema, which is populated by the StructureDefinition loader.\n *\n * In a server context, you can load all schema definitions:\n *\n * ```ts\n * import { indexStructureDefinitionBundle } from '@medplum/core';\n * import { readJson } from '@medplum/definitions';\n * import { Bundle } from '@medplum/fhirtypes';\n *\n * indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);\n * ```\n *\n * In a client context, you can load the schema definitions using MedplumClient:\n *\n * ```ts\n * import { MedplumClient } from '@medplum/core';\n *\n * const medplum = new MedplumClient();\n * await medplum.requestSchema('Patient');\n * ```\n *\n * @param resourceType The candidate resource type string.\n * @returns True if the resource type is a valid FHIR resource type.\n */\nexport function validateResourceType(resourceType: string): void {\n if (!resourceType) {\n throw new OperationOutcomeError(validationError('Resource type is null'));\n }\n if (!isResourceType(resourceType)) {\n throw new OperationOutcomeError(validationError('Unknown resource type'));\n }\n}\n\n/**\n * Validates a candidate FHIR resource object.\n * On success, silently returns void.\n * On failure, throws an OperationOutcomeError with issues for each violation.\n *\n * ```ts\n * validateResource({ resourceType: 'Patient' }); // nothing\n * validateResource({ resourceType: 'XYZ' }); // throws OperationOutcomeError\n * ```\n *\n * Note that this depends on globalSchema, which is populated by the StructureDefinition loader.\n *\n * In a server context, you can load all schema definitions:\n *\n * ```ts\n * import { indexStructureDefinitionBundle } from '@medplum/core';\n * import { readJson } from '@medplum/definitions';\n * import { Bundle } from '@medplum/fhirtypes';\n *\n * indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);\n * ```\n *\n * In a client context, you can load the schema definitions using MedplumClient:\n *\n * ```ts\n * import { MedplumClient } from '@medplum/core';\n *\n * const medplum = new MedplumClient();\n * await medplum.requestSchema('Patient');\n * ```\n *\n * @param resourceType The candidate resource type string.\n * @returns True if the resource type is a valid FHIR resource type.\n */\nexport function validateResource<T extends Resource>(resource: T): void {\n new FhirSchemaValidator(resource).validate();\n}\n\nexport class FhirSchemaValidator<T extends Resource> {\n readonly #issues: OperationOutcomeIssue[];\n readonly #root: T;\n\n constructor(root: T) {\n this.#issues = [];\n this.#root = root;\n }\n\n validate(): void {\n const resource = this.#root;\n if (!resource) {\n throw new OperationOutcomeError(validationError('Resource is null'));\n }\n\n const resourceType = resource.resourceType;\n if (!resourceType) {\n throw new OperationOutcomeError(validationError('Missing resource type'));\n }\n\n // Check for \"null\" once for the entire object hierarchy\n checkForNull(resource, '', this.#issues);\n\n this.#validateObject(toTypedValue(resource), resourceType);\n\n if (this.#issues.length > 0) {\n throw new OperationOutcomeError({\n resourceType: 'OperationOutcome',\n issue: this.#issues,\n });\n }\n }\n\n #validateObject(typedValue: TypedValue, path: string): void {\n const definition = globalSchema.types[typedValue.type];\n if (!definition) {\n throw new OperationOutcomeError(validationError('Unknown type: ' + typedValue.type));\n }\n\n const propertyDefinitions = definition.properties;\n this.#checkProperties(path, propertyDefinitions, typedValue);\n this.#checkAdditionalProperties(path, typedValue, propertyDefinitions);\n }\n\n #checkProperties(path: string, propertyDefinitions: Record<string, ElementDefinition>, typedValue: TypedValue): void {\n for (const [key, elementDefinition] of Object.entries(propertyDefinitions)) {\n this.#checkProperty(path + '.' + key, elementDefinition, typedValue);\n }\n }\n\n #checkProperty(path: string, elementDefinition: ElementDefinition, typedValue: TypedValue): void {\n const propertyName = path.split('.').pop() as string;\n const value = getTypedPropertyValue(typedValue, propertyName);\n\n if (isEmpty(value)) {\n if (elementDefinition.min !== undefined && elementDefinition.min > 0) {\n this.#issues.push(createStructureIssue(path, 'Missing required property'));\n }\n return;\n }\n\n if (elementDefinition.max === '*') {\n if (!Array.isArray(value)) {\n this.#issues.push(createStructureIssue(path, 'Expected array for property'));\n return;\n }\n for (const item of value) {\n this.#checkPropertyValue(path, elementDefinition, item);\n }\n } else {\n if (Array.isArray(value)) {\n this.#issues.push(createStructureIssue(path, 'Expected single value for property'));\n return;\n }\n this.#checkPropertyValue(path, elementDefinition, value as TypedValue);\n }\n }\n\n #checkPropertyValue(path: string, elementDefinition: ElementDefinition, typedValue: TypedValue): void {\n if (typedValue.value === null) {\n // Null handled separately\n return;\n }\n\n if (isLowerCase(typedValue.type.charAt(0))) {\n this.#validatePrimitiveType(elementDefinition, typedValue);\n } else {\n this.#validateObject(typedValue, path);\n }\n }\n\n #validatePrimitiveType(elementDefinition: ElementDefinition, typedValue: TypedValue): void {\n const { type, value } = typedValue;\n\n if (value === null) {\n // Null handled separately, so this code should never be reached\n // Leaving this check in place for now, in case we change the null handling\n return;\n }\n\n // First, make sure the value is the correct JS type\n const expectedType = fhirTypeToJsType[typedValue.type];\n if (typeof value !== expectedType) {\n this.#createIssue(elementDefinition, 'Invalid type for ' + type);\n return;\n }\n\n // Then, perform additional checks for specialty types\n if (expectedType === 'string') {\n this.#validateString(elementDefinition, type as PropertyType, value as string);\n } else if (expectedType === 'number') {\n this.#validateNumber(elementDefinition, type as PropertyType, value as number);\n }\n }\n\n #validateString(elementDefinition: ElementDefinition, type: PropertyType, value: string): void {\n if (!value.trim()) {\n this.#createIssue(elementDefinition, 'Invalid empty string');\n return;\n }\n\n // Try to get the regex\n const valueDefinition = globalSchema.types[type]?.properties?.['value'];\n if (valueDefinition?.type) {\n const regex = getExtensionValue(valueDefinition.type[0], 'http://hl7.org/fhir/StructureDefinition/regex');\n if (regex) {\n if (!value.match(new RegExp(regex))) {\n this.#createIssue(elementDefinition, 'Invalid ' + type + ' format');\n }\n }\n }\n }\n\n #validateNumber(elementDefinition: ElementDefinition, type: PropertyType, value: number): void {\n if (isNaN(value) || !isFinite(value)) {\n this.#createIssue(elementDefinition, 'Invalid ' + type + ' value');\n return;\n }\n\n if (isIntegerType(type) && !Number.isInteger(value)) {\n this.#createIssue(elementDefinition, 'Number is not an integer');\n }\n\n if (type === PropertyType.positiveInt && value <= 0) {\n this.#createIssue(elementDefinition, 'Number is less than or equal to zero');\n }\n\n if (type === PropertyType.unsignedInt && value < 0) {\n this.#createIssue(elementDefinition, 'Number is negative');\n }\n }\n\n #checkAdditionalProperties(\n path: string,\n typedValue: TypedValue,\n propertyDefinitions: Record<string, ElementDefinition>\n ): void {\n const object = typedValue.value as Record<string, unknown>;\n for (const key of Object.keys(object)) {\n this.#checkAdditionalProperty(path, key, typedValue, propertyDefinitions);\n }\n }\n\n /**\n * Checks if the given property is allowed on the given object.\n * @param path The path of the current object.\n * @param key The key of a property to check.\n * @param typedValue The current object.\n * @param propertyDefinitions The property definitions of the current object.\n */\n #checkAdditionalProperty(\n path: string,\n key: string,\n typedValue: TypedValue,\n propertyDefinitions: Record<string, ElementDefinition>\n ): void {\n if (\n !baseResourceProperties.has(key) &&\n !(key in propertyDefinitions) &&\n !isChoiceOfType(key, typedValue, propertyDefinitions) &&\n !this.#checkPrimitiveElement(path, key, typedValue)\n ) {\n const expression = `${path}.${key}`;\n this.#issues.push(createStructureIssue(expression, `Invalid additional property \"${expression}\"`));\n }\n }\n\n /**\n * Checks the element for a primitive.\n *\n * FHIR elements with primitive data types are represented in two parts:\n * 1) A JSON property with the name of the element, which has a JSON type of number, boolean, or string\n * 2) a JSON property with _ prepended to the name of the element, which, if present, contains the value's id and/or extensions\n *\n * See: https://hl7.org/fhir/json.html#primitive\n *\n * @param path The path to the property\n * @param key\n * @param typedValue\n */\n #checkPrimitiveElement(path: string, key: string, typedValue: TypedValue): boolean {\n // Primitive element starts with underscore\n if (!key.startsWith('_')) {\n return false;\n }\n\n // Validate the non-underscore property exists\n const primitiveKey = key.slice(1);\n if (!(primitiveKey in typedValue.value)) {\n return false;\n }\n\n // Then validate the element\n this.#validateObject({ type: 'Element', value: typedValue.value[key] }, path);\n return true;\n }\n\n #createIssue(elementDefinition: ElementDefinition, message: string): void {\n this.#issues.push(createStructureIssue(elementDefinition.path as string, message));\n }\n}\n\nfunction isIntegerType(propertyType: PropertyType): boolean {\n return (\n propertyType === PropertyType.integer ||\n propertyType === PropertyType.positiveInt ||\n propertyType === PropertyType.unsignedInt\n );\n}\n\nfunction isChoiceOfType(\n key: string,\n typedValue: TypedValue,\n propertyDefinitions: Record<string, ElementDefinition>\n): boolean {\n for (const propertyName of Object.keys(propertyDefinitions)) {\n if (!propertyName.endsWith('[x]')) {\n continue;\n }\n const basePropertyName = propertyName.replace('[x]', '');\n if (!key.startsWith(basePropertyName)) {\n continue;\n }\n let typedPropertyValue = getTypedPropertyValue(typedValue, propertyName);\n if (!typedPropertyValue) {\n continue;\n }\n if (Array.isArray(typedPropertyValue)) {\n // At present, there are no choice types that are arrays in the FHIR spec\n // Leaving this here to make TypeScript happy, and in case that changes\n typedPropertyValue = typedPropertyValue[0];\n }\n if (typedPropertyValue && key === basePropertyName + capitalize(typedPropertyValue.type)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Recursively checks for null values in an object.\n *\n * Note that \"null\" is a special value in JSON that is not allowed in FHIR.\n *\n * @param value Input value of any type.\n * @param path Path string to the value for OperationOutcome.\n * @param issues Output list of issues.\n */\nexport function checkForNull(value: unknown, path: string, issues: OperationOutcomeIssue[]): void {\n if (value === null) {\n issues.push(createStructureIssue(path, 'Invalid null value'));\n } else if (Array.isArray(value)) {\n checkArrayForNull(value, path, issues);\n } else if (typeof value === 'object') {\n checkObjectForNull(value as Record<string, unknown>, path, issues);\n }\n}\n\nfunction checkArrayForNull(array: unknown[], path: string, issues: OperationOutcomeIssue[]): void {\n for (let i = 0; i < array.length; i++) {\n if (array[i] === undefined) {\n issues.push(createStructureIssue(`${path}[${i}]`, 'Invalid undefined value'));\n } else {\n checkForNull(array[i], `${path}[${i}]`, issues);\n }\n }\n}\n\nfunction checkObjectForNull(obj: Record<string, unknown>, path: string, issues: OperationOutcomeIssue[]): void {\n for (const [key, value] of Object.entries(obj)) {\n checkForNull(value, `${path}${path ? '.' : ''}${key}`, issues);\n }\n}\n\nexport function createStructureIssue(expression: string, details: string): OperationOutcomeIssue {\n return {\n severity: 'error',\n code: 'structure',\n details: {\n text: details,\n },\n expression: [expression],\n };\n}\n"],"names":[],"mappings":";;;;;;;;;;;AAMA;;;;;AAKG;AAEH,MAAM,gBAAgB,GAA2B;AAC/C,IAAA,YAAY,EAAE,QAAQ;AACtB,IAAA,OAAO,EAAE,SAAS;AAClB,IAAA,SAA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CAAA,EAAE,yBAAyB,CAAC,CAAC,CAAC;AAC/E,SAAA;AAAM,aAAA;AACL,YAAA,YAAY,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAA,EAAG,IAAI,CAAA,CAAA,EAAI,CAAC,CAAA,CAAA,CAAG,EAAE,MAAM,CAAC,CAAC;AACjD,SAAA;AACF,KAAA;AACH,CAAC;AAED,SAAS,kBAAkB,CAAC,GAA4B,EAAE,IAAY,EAAE,MAA+B,EAAA;AACrG,IAAA,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC9C,YAAY,CAAC,KAAK,EAAE,CAAA,EAAG,IAAI,CAAG,EAAA,IAAI,GAAG,GAAG,GAAG,EAAE,CAAA,EAAG,GAAG,CAAE,CAAA,EAAE,MAAM,CAAC,CAAC;AAChE,KAAA;AACH,CAAC;AAEe,SAAA,oBAAoB,CAAC,UAAkB,EAAE,OAAe,EAAA;IACtE,OAAO;AACL,QAAA,QAAQ,EAAE,OAAO;AACjB,QAAA,IAAI,EAAE,WAAW;AACjB,QAAA,OAAO,EAAE;AACP,YAAA,IAAI,EAAE,OAAO;AACd,SAAA;QACD,UAAU,EAAE,CAAC,UAAU,CAAC;KACzB,CAAC;AACJ;;;;"}
1
+ {"version":3,"file":"schema.mjs","sources":["../../src/schema.ts"],"sourcesContent":["import { ElementDefinition, OperationOutcomeIssue, Resource } from '@medplum/fhirtypes';\nimport { getTypedPropertyValue, toTypedValue } from './fhirpath';\nimport { OperationOutcomeError, validationError } from './outcomes';\nimport { globalSchema, PropertyType, TypedValue } from './types';\nimport { capitalize, getExtensionValue, isEmpty, isLowerCase } from './utils';\n\n/*\n * This file provides schema validation utilities for FHIR JSON objects.\n *\n * See: [JSON Representation of Resources](https://hl7.org/fhir/json.html)\n * See: [FHIR Data Types](https://www.hl7.org/fhir/datatypes.html)\n */\n\nconst fhirTypeToJsType: Record<string, string> = {\n base64Binary: 'string',\n boolean: 'boolean',\n canonical: 'string',\n code: 'string',\n date: 'string',\n dateTime: 'string',\n decimal: 'number',\n id: 'string',\n instant: 'string',\n integer: 'number',\n markdown: 'string',\n oid: 'string',\n positiveInt: 'number',\n string: 'string',\n time: 'string',\n unsignedInt: 'number',\n uri: 'string',\n url: 'string',\n uuid: 'string',\n xhtml: 'string',\n 'http://hl7.org/fhirpath/System.String': 'string',\n};\n\nconst baseResourceProperties = new Set<string>([\n // Resource\n 'resourceType',\n 'id',\n 'meta',\n 'implicitRules',\n 'language',\n\n // DomainResource\n 'text',\n 'contained',\n 'extension',\n 'modifierExtension',\n]);\n\n/**\n * Returns true if the given string is a valid FHIR resource type.\n *\n * ```ts\n * isResourceType('Patient'); // true\n * isResourceType('XYZ'); // false\n * ```\n *\n * Note that this depends on globalSchema, which is populated by the StructureDefinition loader.\n *\n * In a server context, you can load all schema definitions:\n *\n * ```ts\n * import { indexStructureDefinitionBundle } from '@medplum/core';\n * import { readJson } from '@medplum/definitions';\n * import { Bundle } from '@medplum/fhirtypes';\n *\n * indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);\n * ```\n *\n * In a client context, you can load the schema definitions using MedplumClient:\n *\n * ```ts\n * import { MedplumClient } from '@medplum/core';\n *\n * const medplum = new MedplumClient();\n * await medplum.requestSchema('Patient');\n * ```\n *\n * @param resourceType The candidate resource type string.\n * @returns True if the resource type is a valid FHIR resource type.\n */\nexport function isResourceType(resourceType: string): boolean {\n const typeSchema = globalSchema.types[resourceType];\n return (\n typeSchema &&\n typeSchema.structureDefinition.id === resourceType &&\n typeSchema.structureDefinition.kind === 'resource'\n );\n}\n\n/**\n * Validates that the given string is a valid FHIR resource type.\n * On success, silently returns void.\n * On failure, throws an OperationOutcomeError.\n *\n * ```ts\n * validateResourceType('Patient'); // nothing\n * validateResourceType('XYZ'); // throws OperationOutcomeError\n * ```\n *\n * Note that this depends on globalSchema, which is populated by the StructureDefinition loader.\n *\n * In a server context, you can load all schema definitions:\n *\n * ```ts\n * import { indexStructureDefinitionBundle } from '@medplum/core';\n * import { readJson } from '@medplum/definitions';\n * import { Bundle } from '@medplum/fhirtypes';\n *\n * indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);\n * ```\n *\n * In a client context, you can load the schema definitions using MedplumClient:\n *\n * ```ts\n * import { MedplumClient } from '@medplum/core';\n *\n * const medplum = new MedplumClient();\n * await medplum.requestSchema('Patient');\n * ```\n *\n * @param resourceType The candidate resource type string.\n * @returns True if the resource type is a valid FHIR resource type.\n */\nexport function validateResourceType(resourceType: string): void {\n if (!resourceType) {\n throw new OperationOutcomeError(validationError('Resource type is null'));\n }\n if (!isResourceType(resourceType)) {\n throw new OperationOutcomeError(validationError('Unknown resource type'));\n }\n}\n\n/**\n * Validates a candidate FHIR resource object.\n * On success, silently returns void.\n * On failure, throws an OperationOutcomeError with issues for each violation.\n *\n * ```ts\n * validateResource({ resourceType: 'Patient' }); // nothing\n * validateResource({ resourceType: 'XYZ' }); // throws OperationOutcomeError\n * ```\n *\n * Note that this depends on globalSchema, which is populated by the StructureDefinition loader.\n *\n * In a server context, you can load all schema definitions:\n *\n * ```ts\n * import { indexStructureDefinitionBundle } from '@medplum/core';\n * import { readJson } from '@medplum/definitions';\n * import { Bundle } from '@medplum/fhirtypes';\n *\n * indexStructureDefinitionBundle(readJson('fhir/r4/profiles-resources.json') as Bundle);\n * ```\n *\n * In a client context, you can load the schema definitions using MedplumClient:\n *\n * ```ts\n * import { MedplumClient } from '@medplum/core';\n *\n * const medplum = new MedplumClient();\n * await medplum.requestSchema('Patient');\n * ```\n *\n * @param resourceType The candidate resource type string.\n * @returns True if the resource type is a valid FHIR resource type.\n */\nexport function validateResource<T extends Resource>(resource: T): void {\n new FhirSchemaValidator(resource).validate();\n}\n\nexport class FhirSchemaValidator<T extends Resource> {\n private readonly issues: OperationOutcomeIssue[];\n private readonly root: T;\n\n constructor(root: T) {\n this.issues = [];\n this.root = root;\n }\n\n validate(): void {\n const resource = this.root;\n if (!resource) {\n throw new OperationOutcomeError(validationError('Resource is null'));\n }\n\n const resourceType = resource.resourceType;\n if (!resourceType) {\n throw new OperationOutcomeError(validationError('Missing resource type'));\n }\n\n // Check for \"null\" once for the entire object hierarchy\n checkForNull(resource, '', this.issues);\n\n this.validateObject(toTypedValue(resource), resourceType);\n\n if (this.issues.length > 0) {\n throw new OperationOutcomeError({\n resourceType: 'OperationOutcome',\n issue: this.issues,\n });\n }\n }\n\n private validateObject(typedValue: TypedValue, path: string): void {\n const definition = globalSchema.types[typedValue.type];\n if (!definition) {\n throw new OperationOutcomeError(validationError('Unknown type: ' + typedValue.type));\n }\n\n const propertyDefinitions = definition.properties;\n this.checkProperties(path, propertyDefinitions, typedValue);\n this.checkAdditionalProperties(path, typedValue, propertyDefinitions);\n }\n\n private checkProperties(\n path: string,\n propertyDefinitions: Record<string, ElementDefinition>,\n typedValue: TypedValue\n ): void {\n for (const [key, elementDefinition] of Object.entries(propertyDefinitions)) {\n this.checkProperty(path + '.' + key, elementDefinition, typedValue);\n }\n }\n\n private checkProperty(path: string, elementDefinition: ElementDefinition, typedValue: TypedValue): void {\n const propertyName = path.split('.').pop() as string;\n const value = getTypedPropertyValue(typedValue, propertyName);\n\n if (isEmpty(value)) {\n if (elementDefinition.min !== undefined && elementDefinition.min > 0) {\n this.issues.push(createStructureIssue(path, 'Missing required property'));\n }\n return;\n }\n\n if (elementDefinition.max === '*') {\n if (!Array.isArray(value)) {\n this.issues.push(createStructureIssue(path, 'Expected array for property'));\n return;\n }\n for (const item of value) {\n this.checkPropertyValue(path, elementDefinition, item);\n }\n } else {\n if (Array.isArray(value)) {\n this.issues.push(createStructureIssue(path, 'Expected single value for property'));\n return;\n }\n this.checkPropertyValue(path, elementDefinition, value as TypedValue);\n }\n }\n\n private checkPropertyValue(path: string, elementDefinition: ElementDefinition, typedValue: TypedValue): void {\n if (typedValue.value === null) {\n // Null handled separately\n return;\n }\n\n if (isLowerCase(typedValue.type.charAt(0))) {\n this.validatePrimitiveType(elementDefinition, typedValue);\n } else {\n this.validateObject(typedValue, path);\n }\n }\n\n private validatePrimitiveType(elementDefinition: ElementDefinition, typedValue: TypedValue): void {\n const { type, value } = typedValue;\n\n if (value === null) {\n // Null handled separately, so this code should never be reached\n // Leaving this check in place for now, in case we change the null handling\n return;\n }\n\n // First, make sure the value is the correct JS type\n const expectedType = fhirTypeToJsType[typedValue.type];\n if (typeof value !== expectedType) {\n this.createIssue(elementDefinition, 'Invalid type for ' + type);\n return;\n }\n\n // Then, perform additional checks for specialty types\n if (expectedType === 'string') {\n this.validateString(elementDefinition, type as PropertyType, value as string);\n } else if (expectedType === 'number') {\n this.validateNumber(elementDefinition, type as PropertyType, value as number);\n }\n }\n\n private validateString(elementDefinition: ElementDefinition, type: PropertyType, value: string): void {\n if (!value.trim()) {\n this.createIssue(elementDefinition, 'Invalid empty string');\n return;\n }\n\n // Try to get the regex\n const valueDefinition = globalSchema.types[type]?.properties?.['value'];\n if (valueDefinition?.type) {\n const regex = getExtensionValue(valueDefinition.type[0], 'http://hl7.org/fhir/StructureDefinition/regex');\n if (regex) {\n if (!value.match(new RegExp(regex))) {\n this.createIssue(elementDefinition, 'Invalid ' + type + ' format');\n }\n }\n }\n }\n\n private validateNumber(elementDefinition: ElementDefinition, type: PropertyType, value: number): void {\n if (isNaN(value) || !isFinite(value)) {\n this.createIssue(elementDefinition, 'Invalid ' + type + ' value');\n return;\n }\n\n if (isIntegerType(type) && !Number.isInteger(value)) {\n this.createIssue(elementDefinition, 'Number is not an integer');\n }\n\n if (type === PropertyType.positiveInt && value <= 0) {\n this.createIssue(elementDefinition, 'Number is less than or equal to zero');\n }\n\n if (type === PropertyType.unsignedInt && value < 0) {\n this.createIssue(elementDefinition, 'Number is negative');\n }\n }\n\n private checkAdditionalProperties(\n path: string,\n typedValue: TypedValue,\n propertyDefinitions: Record<string, ElementDefinition>\n ): void {\n const object = typedValue.value as Record<string, unknown>;\n for (const key of Object.keys(object)) {\n this.checkAdditionalProperty(path, key, typedValue, propertyDefinitions);\n }\n }\n\n /**\n * Checks if the given property is allowed on the given object.\n * @param path The path of the current object.\n * @param key The key of a property to check.\n * @param typedValue The current object.\n * @param propertyDefinitions The property definitions of the current object.\n */\n private checkAdditionalProperty(\n path: string,\n key: string,\n typedValue: TypedValue,\n propertyDefinitions: Record<string, ElementDefinition>\n ): void {\n if (\n !baseResourceProperties.has(key) &&\n !(key in propertyDefinitions) &&\n !isChoiceOfType(key, typedValue, propertyDefinitions) &&\n !this.checkPrimitiveElement(path, key, typedValue)\n ) {\n const expression = `${path}.${key}`;\n this.issues.push(createStructureIssue(expression, `Invalid additional property \"${expression}\"`));\n }\n }\n\n /**\n * Checks the element for a primitive.\n *\n * FHIR elements with primitive data types are represented in two parts:\n * 1) A JSON property with the name of the element, which has a JSON type of number, boolean, or string\n * 2) a JSON property with _ prepended to the name of the element, which, if present, contains the value's id and/or extensions\n *\n * See: https://hl7.org/fhir/json.html#primitive\n *\n * @param path The path to the property\n * @param key\n * @param typedValue\n */\n private checkPrimitiveElement(path: string, key: string, typedValue: TypedValue): boolean {\n // Primitive element starts with underscore\n if (!key.startsWith('_')) {\n return false;\n }\n\n // Validate the non-underscore property exists\n const primitiveKey = key.slice(1);\n if (!(primitiveKey in typedValue.value)) {\n return false;\n }\n\n // Then validate the element\n this.validateObject({ type: 'Element', value: typedValue.value[key] }, path);\n return true;\n }\n\n private createIssue(elementDefinition: ElementDefinition, message: string): void {\n this.issues.push(createStructureIssue(elementDefinition.path as string, message));\n }\n}\n\nfunction isIntegerType(propertyType: PropertyType): boolean {\n return (\n propertyType === PropertyType.integer ||\n propertyType === PropertyType.positiveInt ||\n propertyType === PropertyType.unsignedInt\n );\n}\n\nfunction isChoiceOfType(\n key: string,\n typedValue: TypedValue,\n propertyDefinitions: Record<string, ElementDefinition>\n): boolean {\n for (const propertyName of Object.keys(propertyDefinitions)) {\n if (!propertyName.endsWith('[x]')) {\n continue;\n }\n const basePropertyName = propertyName.replace('[x]', '');\n if (!key.startsWith(basePropertyName)) {\n continue;\n }\n let typedPropertyValue = getTypedPropertyValue(typedValue, propertyName);\n if (!typedPropertyValue) {\n continue;\n }\n if (Array.isArray(typedPropertyValue)) {\n // At present, there are no choice types that are arrays in the FHIR spec\n // Leaving this here to make TypeScript happy, and in case that changes\n typedPropertyValue = typedPropertyValue[0];\n }\n if (typedPropertyValue && key === basePropertyName + capitalize(typedPropertyValue.type)) {\n return true;\n }\n }\n return false;\n}\n\n/**\n * Recursively checks for null values in an object.\n *\n * Note that \"null\" is a special value in JSON that is not allowed in FHIR.\n *\n * @param value Input value of any type.\n * @param path Path string to the value for OperationOutcome.\n * @param issues Output list of issues.\n */\nexport function checkForNull(value: unknown, path: string, issues: OperationOutcomeIssue[]): void {\n if (value === null) {\n issues.push(createStructureIssue(path, 'Invalid null value'));\n } else if (Array.isArray(value)) {\n checkArrayForNull(value, path, issues);\n } else if (typeof value === 'object') {\n checkObjectForNull(value as Record<string, unknown>, path, issues);\n }\n}\n\nfunction checkArrayForNull(array: unknown[], path: string, issues: OperationOutcomeIssue[]): void {\n for (let i = 0; i < array.length; i++) {\n if (array[i] === undefined) {\n issues.push(createStructureIssue(`${path}[${i}]`, 'Invalid undefined value'));\n } else {\n checkForNull(array[i], `${path}[${i}]`, issues);\n }\n }\n}\n\nfunction checkObjectForNull(obj: Record<string, unknown>, path: string, issues: OperationOutcomeIssue[]): void {\n for (const [key, value] of Object.entries(obj)) {\n checkForNull(value, `${path}${path ? '.' : ''}${key}`, issues);\n }\n}\n\nexport function createStructureIssue(expression: string, details: string): OperationOutcomeIssue {\n return {\n severity: 'error',\n code: 'structure',\n details: {\n text: details,\n },\n expression: [expression],\n };\n}\n"],"names":[],"mappings":";;;;;;AAMA;;;;;AAKG;AAEH,MAAM,gBAAgB,GAA2B;AAC/C,IAAA,YAAY,EAAE,QAAQ;AACtB,IAAA,OAAO,EAAE,SAAS;AAClB,IAAA,SAAS,EAAE,QAAQ;AACnB,IAAA,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OAAO,EAAE;AACP,YAAA,IAAI,EAAE,OAAO;AACd,SAAA;QACD,UAAU,EAAE,CAAC,UAAU,CAAC;KACzB,CAAC;AACJ;;;;"}
@@ -1,9 +1,6 @@
1
- import '../fhirpath/atoms.mjs';
2
- import { evalFhirPath } from '../fhirpath/parse.mjs';
3
- import '../fhirlexer/parse.mjs';
4
- import '../fhirlexer/tokenize.mjs';
5
1
  import { globalSchema } from '../types.mjs';
6
2
  import '../utils.mjs';
3
+ import { evalFhirPath } from '../fhirpath/parse.mjs';
7
4
  import { getSearchParameterDetails, SearchParameterType } from './details.mjs';
8
5
  import { Operator } from './search.mjs';
9
6
 
@@ -1 +1 @@
1
- {"version":3,"file":"match.mjs","sources":["../../../src/search/match.ts"],"sourcesContent":["import { Reference, Resource, SearchParameter } from '@medplum/fhirtypes';\nimport { evalFhirPath } from '../fhirpath';\nimport { globalSchema } from '../types';\nimport { getSearchParameterDetails, SearchParameterType } from './details';\nimport { Filter, Operator, SearchRequest } from './search';\n\n/**\n * Determines if the resource matches the search request.\n * @param resource The resource that was created or updated.\n * @param searchRequest The subscription criteria as a search request.\n * @returns True if the resource satisfies the search request.\n */\nexport function matchesSearchRequest(resource: Resource, searchRequest: SearchRequest): boolean {\n if (searchRequest.resourceType !== resource.resourceType) {\n return false;\n }\n if (searchRequest.filters) {\n for (const filter of searchRequest.filters) {\n if (!matchesSearchFilter(resource, searchRequest, filter)) {\n return false;\n }\n }\n }\n return true;\n}\n\n/**\n * Determines if the resource matches the search filter.\n * @param resource The resource that was created or updated.\n * @param filter One of the filters of a subscription criteria.\n * @returns True if the resource satisfies the search filter.\n */\nfunction matchesSearchFilter(resource: Resource, searchRequest: SearchRequest, filter: Filter): boolean {\n const searchParam = globalSchema.types[searchRequest.resourceType]?.searchParams?.[filter.code];\n switch (searchParam?.type) {\n case 'reference':\n return matchesReferenceFilter(resource, filter, searchParam);\n case 'string':\n case 'uri':\n return matchesStringFilter(resource, filter, searchParam);\n case 'token':\n return matchesTokenFilter(resource, filter, searchParam);\n case 'date':\n return matchesDateFilter(resource, filter, searchParam);\n }\n // Unknown search parameter or search parameter type\n // Default fail the check\n return false;\n}\n\nfunction matchesReferenceFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const values = evalFhirPath(searchParam.expression as string, resource) as (Reference | string)[];\n const negated = isNegated(filter.operator);\n\n if (filter.value === '' && values.length === 0) {\n // If the filter operator is \"equals\", then the filter matches.\n // If the filter operator is \"not equals\", then the filter does not match.\n return filter.operator === Operator.EQUALS;\n }\n\n // Normalize the values array into reference strings\n const references = values.map((value) => (typeof value === 'string' ? value : value.reference));\n\n for (const filterValue of filter.value.split(',')) {\n let match = references.includes(filterValue);\n if (!match && filter.code === '_compartment') {\n // Backwards compability for compartment search parameter\n // In previous versions, the resource type was not required in compartment values\n // So, \"123\" would match \"Patient/123\"\n // We need to maintain this behavior for backwards compatibility\n match = references.some((reference) => reference?.endsWith('/' + filterValue));\n }\n if (match) {\n return !negated;\n }\n }\n // If \"not equals\" and no matches, then return true\n // If \"equals\" and no matches, then return false\n return negated;\n}\n\nfunction matchesTokenFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const details = getSearchParameterDetails(resource.resourceType, searchParam);\n if (details.type === SearchParameterType.BOOLEAN) {\n return matchesBooleanFilter(resource, filter, searchParam);\n } else {\n return matchesStringFilter(resource, filter, searchParam, true);\n }\n}\n\nfunction matchesBooleanFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const values = evalFhirPath(searchParam.expression as string, resource);\n const expected = filter.value === 'true';\n const result = values.includes(expected);\n return isNegated(filter.operator) ? !result : result;\n}\n\nfunction matchesStringFilter(\n resource: Resource,\n filter: Filter,\n searchParam: SearchParameter,\n asToken?: boolean\n): boolean {\n const resourceValues = evalFhirPath(searchParam.expression as string, resource);\n const filterValues = filter.value.split(',');\n const negated = isNegated(filter.operator);\n for (const resourceValue of resourceValues) {\n for (const filterValue of filterValues) {\n const match = matchesStringValue(resourceValue, filter.operator, filterValue, asToken);\n if (match) {\n return !negated;\n }\n }\n }\n // If \"not equals\" and no matches, then return true\n // If \"equals\" and no matches, then return false\n return negated;\n}\n\nfunction matchesStringValue(\n resourceValue: unknown,\n operator: Operator,\n filterValue: string,\n asToken?: boolean\n): boolean {\n if (asToken && filterValue.includes('|')) {\n const [system, code] = filterValue.split('|');\n return (\n matchesStringValue(resourceValue, operator, system, false) &&\n (!code || matchesStringValue(resourceValue, operator, code, false))\n );\n }\n let str = '';\n if (resourceValue) {\n if (typeof resourceValue === 'string') {\n str = resourceValue;\n } else if (typeof resourceValue === 'object') {\n str = JSON.stringify(resourceValue);\n }\n }\n return str.toLowerCase().includes(filterValue.toLowerCase());\n}\n\nfunction matchesDateFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const resourceValues = evalFhirPath(searchParam.expression as string, resource);\n const filterValues = filter.value.split(',');\n const negated = isNegated(filter.operator);\n for (const resourceValue of resourceValues) {\n for (const filterValue of filterValues) {\n const match = matchesDateValue(resourceValue as string, filter.operator, filterValue);\n if (match) {\n return !negated;\n }\n }\n }\n // If \"not equals\" and no matches, then return true\n // If \"equals\" and no matches, then return false\n return negated;\n}\n\nfunction matchesDateValue(resourceValue: string, operator: Operator, filterValue: string): boolean {\n switch (operator) {\n case Operator.STARTS_AFTER:\n case Operator.GREATER_THAN:\n return resourceValue > filterValue;\n case Operator.GREATER_THAN_OR_EQUALS:\n return resourceValue >= filterValue;\n case Operator.ENDS_BEFORE:\n case Operator.LESS_THAN:\n return resourceValue < filterValue;\n case Operator.LESS_THAN_OR_EQUALS:\n return resourceValue <= filterValue;\n case Operator.EQUALS:\n case Operator.NOT_EQUALS:\n return resourceValue === filterValue;\n }\n return false;\n}\n\nfunction isNegated(operator: Operator): boolean {\n return operator === Operator.NOT_EQUALS || operator === Operator.NOT;\n}\n"],"names":[],"mappings":";;;;;;;;;AAMA;;;;;AAKG;AACa,SAAA,oBAAoB,CAAC,QAAkB,EAAE,aAA4B,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,EAAE;gBACT,OAAO,CAAC,OAAO,CAAC;AACjB,aAAA;AACF,SAAA;AACF,KAAA;;;AAGD,IAAA,OAAO,OAAO,CAAC;AACjB,CAAC;AAED,SAAS,gBAAgB,CAAC,aAAqB,EAAE,QAAkB,EAAE,WAAmB,EAAA;AACtF,IAAA,QAAQ,QAAQ;QACd,KAAK,QAAQ,CAAC,YAAY,CAAC;QAC3B,KAAK,QAAQ,CAAC,YAAY;YACxB,OAAO,aAAa,GAAG,WAAW,CAAC;QACrC,KAAK,QAAQ,CAAC,sBAAsB;YAClC,OAAO,aAAa,IAAI,WAAW,CAAC;QACtC,KAAK,QAAQ,CAAC,WAAW,CAAC;QAC1B,KAAK,QAAQ,CAAC,SAAS;YACrB,OAAO,aAAa,GAAG,WAAW,CAAC;QACrC,KAAK,QAAQ,CAAC,mBAAmB;YAC/B,OAAO,aAAa,IAAI,WAAW,CAAC;QACtC,KAAK,QAAQ,CAAC,MAAM,CAAC;QACrB,KAAK,QAAQ,CAAC,UAAU;YACtB,OAAO,aAAa,KAAK,WAAW,CAAC;AACxC,KAAA;AACD,IAAA,OAAO,KAAK,CAAC;AACf,CAAC;AAED,SAAS,SAAS,CAAC,QAAkB,EAAA;IACnC,OAAO,QAAQ,KAAK,QAAQ,CAAC,UAAU,IAAI,QAAQ,KAAK,QAAQ,CAAC,GAAG,CAAC;AACvE;;;;"}
1
+ {"version":3,"file":"match.mjs","sources":["../../../src/search/match.ts"],"sourcesContent":["import { Reference, Resource, SearchParameter } from '@medplum/fhirtypes';\nimport { evalFhirPath } from '../fhirpath';\nimport { globalSchema } from '../types';\nimport { getSearchParameterDetails, SearchParameterType } from './details';\nimport { Filter, Operator, SearchRequest } from './search';\n\n/**\n * Determines if the resource matches the search request.\n * @param resource The resource that was created or updated.\n * @param searchRequest The subscription criteria as a search request.\n * @returns True if the resource satisfies the search request.\n */\nexport function matchesSearchRequest(resource: Resource, searchRequest: SearchRequest): boolean {\n if (searchRequest.resourceType !== resource.resourceType) {\n return false;\n }\n if (searchRequest.filters) {\n for (const filter of searchRequest.filters) {\n if (!matchesSearchFilter(resource, searchRequest, filter)) {\n return false;\n }\n }\n }\n return true;\n}\n\n/**\n * Determines if the resource matches the search filter.\n * @param resource The resource that was created or updated.\n * @param filter One of the filters of a subscription criteria.\n * @returns True if the resource satisfies the search filter.\n */\nfunction matchesSearchFilter(resource: Resource, searchRequest: SearchRequest, filter: Filter): boolean {\n const searchParam = globalSchema.types[searchRequest.resourceType]?.searchParams?.[filter.code];\n switch (searchParam?.type) {\n case 'reference':\n return matchesReferenceFilter(resource, filter, searchParam);\n case 'string':\n case 'uri':\n return matchesStringFilter(resource, filter, searchParam);\n case 'token':\n return matchesTokenFilter(resource, filter, searchParam);\n case 'date':\n return matchesDateFilter(resource, filter, searchParam);\n }\n // Unknown search parameter or search parameter type\n // Default fail the check\n return false;\n}\n\nfunction matchesReferenceFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const values = evalFhirPath(searchParam.expression as string, resource) as (Reference | string)[];\n const negated = isNegated(filter.operator);\n\n if (filter.value === '' && values.length === 0) {\n // If the filter operator is \"equals\", then the filter matches.\n // If the filter operator is \"not equals\", then the filter does not match.\n return filter.operator === Operator.EQUALS;\n }\n\n // Normalize the values array into reference strings\n const references = values.map((value) => (typeof value === 'string' ? value : value.reference));\n\n for (const filterValue of filter.value.split(',')) {\n let match = references.includes(filterValue);\n if (!match && filter.code === '_compartment') {\n // Backwards compability for compartment search parameter\n // In previous versions, the resource type was not required in compartment values\n // So, \"123\" would match \"Patient/123\"\n // We need to maintain this behavior for backwards compatibility\n match = references.some((reference) => reference?.endsWith('/' + filterValue));\n }\n if (match) {\n return !negated;\n }\n }\n // If \"not equals\" and no matches, then return true\n // If \"equals\" and no matches, then return false\n return negated;\n}\n\nfunction matchesTokenFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const details = getSearchParameterDetails(resource.resourceType, searchParam);\n if (details.type === SearchParameterType.BOOLEAN) {\n return matchesBooleanFilter(resource, filter, searchParam);\n } else {\n return matchesStringFilter(resource, filter, searchParam, true);\n }\n}\n\nfunction matchesBooleanFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const values = evalFhirPath(searchParam.expression as string, resource);\n const expected = filter.value === 'true';\n const result = values.includes(expected);\n return isNegated(filter.operator) ? !result : result;\n}\n\nfunction matchesStringFilter(\n resource: Resource,\n filter: Filter,\n searchParam: SearchParameter,\n asToken?: boolean\n): boolean {\n const resourceValues = evalFhirPath(searchParam.expression as string, resource);\n const filterValues = filter.value.split(',');\n const negated = isNegated(filter.operator);\n for (const resourceValue of resourceValues) {\n for (const filterValue of filterValues) {\n const match = matchesStringValue(resourceValue, filter.operator, filterValue, asToken);\n if (match) {\n return !negated;\n }\n }\n }\n // If \"not equals\" and no matches, then return true\n // If \"equals\" and no matches, then return false\n return negated;\n}\n\nfunction matchesStringValue(\n resourceValue: unknown,\n operator: Operator,\n filterValue: string,\n asToken?: boolean\n): boolean {\n if (asToken && filterValue.includes('|')) {\n const [system, code] = filterValue.split('|');\n return (\n matchesStringValue(resourceValue, operator, system, false) &&\n (!code || matchesStringValue(resourceValue, operator, code, false))\n );\n }\n let str = '';\n if (resourceValue) {\n if (typeof resourceValue === 'string') {\n str = resourceValue;\n } else if (typeof resourceValue === 'object') {\n str = JSON.stringify(resourceValue);\n }\n }\n return str.toLowerCase().includes(filterValue.toLowerCase());\n}\n\nfunction matchesDateFilter(resource: Resource, filter: Filter, searchParam: SearchParameter): boolean {\n const resourceValues = evalFhirPath(searchParam.expression as string, resource);\n const filterValues = filter.value.split(',');\n const negated = isNegated(filter.operator);\n for (const resourceValue of resourceValues) {\n for (const filterValue of filterValues) {\n const match = matchesDateValue(resourceValue as string, filter.operator, filterValue);\n if (match) {\n return !negated;\n }\n }\n }\n // If \"not equals\" and no matches, then return true\n // If \"equals\" and no matches, then return false\n return negated;\n}\n\nfunction matchesDateValue(resourceValue: string, operator: Operator, filterValue: string): boolean {\n switch (operator) {\n case Operator.STARTS_AFTER:\n case Operator.GREATER_THAN:\n return resourceValue > filterValue;\n case Operator.GREATER_THAN_OR_EQUALS:\n return resourceValue >= filterValue;\n case Operator.ENDS_BEFORE:\n case Operator.LESS_THAN:\n return resourceValue < filterValue;\n case Operator.LESS_THAN_OR_EQUALS:\n return resourceValue <= filterValue;\n case Operator.EQUALS:\n case Operator.NOT_EQUALS:\n return resourceValue === filterValue;\n }\n return false;\n}\n\nfunction isNegated(operator: Operator): boolean {\n return operator === Operator.NOT_EQUALS || operator === Operator.NOT;\n}\n"],"names":[],"mappings":";;;;;;AAMA;;;;;AAKG;AACa,SAAA,oBAAoB,CAAC,QAAkB,EAAE,aAA4B,EAAA;AACnF,IAAA,IAAI,aAAa,CAAC,YAAY,KAAK,QAAQ,CAAC,YAAY,EAAE;AACxD,QAAA,OAAO,KAAK,CAAC;AACd,KAAA;IACD,IAAI,aAAa,CAAC,OAAO,EAAE;AACzB,QAAA,KAAK,MAAM,MAAM,IAAI,aAAa,CAAC,OAAO,EAAE;YAC1C,IAAI,CAAC,mBAAmB,CAAC,QAAQ,EAAE,aAAa,EAAE,MAAM,CAAC,EAAE;AACzD,gBAAA,OAAO,KAAK,CAAC;AACd,aAAA;AACF,SAAA;AACF,KAAA;AACD,IAAA,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;;;AAKG;AACH,SAAS,mBAAmB,CAAC,QAAkB,EAAE,aAA4B,EAAE,MAAc,EAAA;AAC3F,IAAA,MAAM,WAAW,GAAG,YAAY,CAAC,KAAK,CAAC,aAAa,CAAC,YAAY,CAAC,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IAChG,QAAQ,WAAW,EAAE,IAAI;AACvB,QAAA,KAAK,WAAW;YACd,OAAO,sBAAsB,CAAC,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;AAC/D,QAAA,KAAK,QAAQ,CAAC;AACd,QAAA,KAAK,KAAK;YACR,OAAO,mBAAmB,CAA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I,QAAQ,KAAK,QAAQ,CAAC,GAAG,CAAC;AACvE;;;;"}
@@ -1,7 +1,5 @@
1
- import { __classPrivateFieldSet, __classPrivateFieldGet } from './node_modules/tslib/tslib.es6.mjs';
2
1
  import { stringify } from './utils.mjs';
3
2
 
4
- var _ClientStorage_storage, _MemoryStorage_data;
5
3
  /**
6
4
  * The ClientStorage class is a utility class for storing strings and objects.
7
5
  *
@@ -11,21 +9,20 @@ var _ClientStorage_storage, _MemoryStorage_data;
11
9
  */
12
10
  class ClientStorage {
13
11
  constructor() {
14
- _ClientStorage_storage.set(this, void 0);
15
- __classPrivateFieldSet(this, _ClientStorage_storage, typeof localStorage !== 'undefined' ? localStorage : new MemoryStorage(), "f");
12
+ this.storage = typeof localStorage !== 'undefined' ? localStorage : new MemoryStorage();
16
13
  }
17
14
  clear() {
18
- __classPrivateFieldGet(this, _ClientStorage_storage, "f").clear();
15
+ this.storage.clear();
19
16
  }
20
17
  getString(key) {
21
- return __classPrivateFieldGet(this, _ClientStorage_storage, "f").getItem(key) || undefined;
18
+ return this.storage.getItem(key) || undefined;
22
19
  }
23
20
  setString(key, value) {
24
21
  if (value) {
25
- __classPrivateFieldGet(this, _ClientStorage_storage, "f").setItem(key, value);
22
+ this.storage.setItem(key, value);
26
23
  }
27
24
  else {
28
- __classPrivateFieldGet(this, _ClientStorage_storage, "f").removeItem(key);
25
+ this.storage.removeItem(key);
29
26
  }
30
27
  }
31
28
  getObject(key) {
@@ -36,58 +33,55 @@ class ClientStorage {
36
33
  this.setString(key, value ? stringify(value) : undefined);
37
34
  }
38
35
  }
39
- _ClientStorage_storage = new WeakMap();
40
36
  /**
41
37
  * The MemoryStorage class is a minimal in-memory implementation of the Storage interface.
42
38
  */
43
39
  class MemoryStorage {
44
40
  constructor() {
45
- _MemoryStorage_data.set(this, void 0);
46
- __classPrivateFieldSet(this, _MemoryStorage_data, new Map(), "f");
41
+ this.data = new Map();
47
42
  }
48
43
  /**
49
44
  * Returns the number of key/value pairs.
50
45
  */
51
46
  get length() {
52
- return __classPrivateFieldGet(this, _MemoryStorage_data, "f").size;
47
+ return this.data.size;
53
48
  }
54
49
  /**
55
50
  * Removes all key/value pairs, if there are any.
56
51
  */
57
52
  clear() {
58
- __classPrivateFieldGet(this, _MemoryStorage_data, "f").clear();
53
+ this.data.clear();
59
54
  }
60
55
  /**
61
56
  * Returns the current value associated with the given key, or null if the given key does not exist.
62
57
  */
63
58
  getItem(key) {
64
- return __classPrivateFieldGet(this, _MemoryStorage_data, "f").get(key) ?? null;
59
+ return this.data.get(key) ?? null;
65
60
  }
66
61
  /**
67
62
  * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
68
63
  */
69
64
  setItem(key, value) {
70
65
  if (value) {
71
- __classPrivateFieldGet(this, _MemoryStorage_data, "f").set(key, value);
66
+ this.data.set(key, value);
72
67
  }
73
68
  else {
74
- __classPrivateFieldGet(this, _MemoryStorage_data, "f").delete(key);
69
+ this.data.delete(key);
75
70
  }
76
71
  }
77
72
  /**
78
73
  * Removes the key/value pair with the given key, if a key/value pair with the given key exists.
79
74
  */
80
75
  removeItem(key) {
81
- __classPrivateFieldGet(this, _MemoryStorage_data, "f").delete(key);
76
+ this.data.delete(key);
82
77
  }
83
78
  /**
84
79
  * Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs.
85
80
  */
86
81
  key(index) {
87
- return Array.from(__classPrivateFieldGet(this, _MemoryStorage_data, "f").keys())[index];
82
+ return Array.from(this.data.keys())[index];
88
83
  }
89
84
  }
90
- _MemoryStorage_data = new WeakMap();
91
85
 
92
86
  export { ClientStorage, MemoryStorage };
93
87
  //# sourceMappingURL=storage.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"storage.mjs","sources":["../../src/storage.ts"],"sourcesContent":["import { stringify } from './utils';\n\n/**\n * The ClientStorage class is a utility class for storing strings and objects.\n *\n * When using MedplumClient in the browser, it will be backed by browser localStorage.\n *\n * When Using MedplumClient in the server, it will be backed by the MemoryStorage class.\n */\nexport class ClientStorage {\n readonly #storage: Storage;\n\n constructor() {\n this.#storage = typeof localStorage !== 'undefined' ? localStorage : new MemoryStorage();\n }\n\n clear(): void {\n this.#storage.clear();\n }\n\n getString(key: string): string | undefined {\n return this.#storage.getItem(key) || undefined;\n }\n\n setString(key: string, value: string | undefined): void {\n if (value) {\n this.#storage.setItem(key, value);\n } else {\n this.#storage.removeItem(key);\n }\n }\n\n getObject<T>(key: string): T | undefined {\n const str = this.getString(key);\n return str ? (JSON.parse(str) as T) : undefined;\n }\n\n setObject<T>(key: string, value: T): void {\n this.setString(key, value ? stringify(value) : undefined);\n }\n}\n\n/**\n * The MemoryStorage class is a minimal in-memory implementation of the Storage interface.\n */\nexport class MemoryStorage implements Storage {\n #data: Map<string, string>;\n\n constructor() {\n this.#data = new Map<string, string>();\n }\n\n /**\n * Returns the number of key/value pairs.\n */\n get length(): number {\n return this.#data.size;\n }\n\n /**\n * Removes all key/value pairs, if there are any.\n */\n clear(): void {\n this.#data.clear();\n }\n\n /**\n * Returns the current value associated with the given key, or null if the given key does not exist.\n */\n getItem(key: string): string | null {\n return this.#data.get(key) ?? null;\n }\n\n /**\n * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.\n */\n setItem(key: string, value: string | null): void {\n if (value) {\n this.#data.set(key, value);\n } else {\n this.#data.delete(key);\n }\n }\n\n /**\n * Removes the key/value pair with the given key, if a key/value pair with the given key exists.\n */\n removeItem(key: string): void {\n this.#data.delete(key);\n }\n\n /**\n * Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs.\n */\n key(index: number): string | null {\n return Array.from(this.#data.keys())[index];\n }\n}\n"],"names":[],"mappings":";;;;AAEA;;;;;;AAMG;MACU,aAAa,CAAA;AAGxB,IAAA,WAAA,GAAA;QAFS,sBAAkB,CAAA,GAAA,CAAA,IAAA,EAAA,KAAA,CAAA,CAAA,CAAA;AAGzB,QAAA,sBAAA,CAAA,IAAI,EAAY,sBAAA,EAAA,OAAO,YAAY,KAAK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,QAAA,OAAO,sBAAA,CAAA,IAAI,EAAM,mBAAA,EAAA,GAAA,CAAA,CAAC,IAAI,CAAC;KACxB;AAED;;AAEG;IACH,KAAK,GAAA;AACH,QAAA,sBAAA,CAAA,IAAI,EAAA,mBAAA,EAAA,GAAA,CAAM,CAAC,KAAK,EAAE,CAAC;KACpB;AAED;;AAEG;AACH,IAAA,OAAO,CAAC,GAAW,EAAA;QACjB,OAAO,sBAAA,CAAA,IAAI,EAAA,mBAAA,EAAA,GAAA,CAAM,CAAC,GAAG,CAAC,GAAG,CAAC,IAAI,IAAI,CAAC;KACpC;AAED;;AAEG;IACH,OAAO,CAAC,GAAW,EAAE,KAAoB,EAAA;AACvC,QAAA,IAAI,KAAK,EAAE;YACT,sBAAA,CAAA,IAAI,2BAAM,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AAC5B,SAAA;AAAM,aAAA;AACL,YAAA,sBAAA,CAAA,IAAI,EAAM,mBAAA,EAAA,GAAA,CAAA,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACxB,SAAA;KACF;AAED;;AAEG;AACH,IAAA,UAAU,CAAC,GAAW,EAAA;AACpB,QAAA,sBAAA,CAAA,IAAI,EAAM,mBAAA,EAAA,GAAA,CAAA,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;KACxB;AAED;;AAEG;AACH,IAAA,GAAG,CAAC,KAAa,EAAA;AACf,QAAA,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,EAAA,mBAAA,EAAA,GAAA,CAAM,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;KAC7C;AACF,CAAA;;;;;"}
1
+ {"version":3,"file":"storage.mjs","sources":["../../src/storage.ts"],"sourcesContent":["import { stringify } from './utils';\n\n/**\n * The ClientStorage class is a utility class for storing strings and objects.\n *\n * When using MedplumClient in the browser, it will be backed by browser localStorage.\n *\n * When Using MedplumClient in the server, it will be backed by the MemoryStorage class.\n */\nexport class ClientStorage {\n private readonly storage: Storage;\n\n constructor() {\n this.storage = typeof localStorage !== 'undefined' ? localStorage : new MemoryStorage();\n }\n\n clear(): void {\n this.storage.clear();\n }\n\n getString(key: string): string | undefined {\n return this.storage.getItem(key) || undefined;\n }\n\n setString(key: string, value: string | undefined): void {\n if (value) {\n this.storage.setItem(key, value);\n } else {\n this.storage.removeItem(key);\n }\n }\n\n getObject<T>(key: string): T | undefined {\n const str = this.getString(key);\n return str ? (JSON.parse(str) as T) : undefined;\n }\n\n setObject<T>(key: string, value: T): void {\n this.setString(key, value ? stringify(value) : undefined);\n }\n}\n\n/**\n * The MemoryStorage class is a minimal in-memory implementation of the Storage interface.\n */\nexport class MemoryStorage implements Storage {\n private data: Map<string, string>;\n\n constructor() {\n this.data = new Map<string, string>();\n }\n\n /**\n * Returns the number of key/value pairs.\n */\n get length(): number {\n return this.data.size;\n }\n\n /**\n * Removes all key/value pairs, if there are any.\n */\n clear(): void {\n this.data.clear();\n }\n\n /**\n * Returns the current value associated with the given key, or null if the given key does not exist.\n */\n getItem(key: string): string | null {\n return this.data.get(key) ?? null;\n }\n\n /**\n * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.\n */\n setItem(key: string, value: string | null): void {\n if (value) {\n this.data.set(key, value);\n } else {\n this.data.delete(key);\n }\n }\n\n /**\n * Removes the key/value pair with the given key, if a key/value pair with the given key exists.\n */\n removeItem(key: string): void {\n this.data.delete(key);\n }\n\n /**\n * Returns the name of the nth key, or null if n is greater than or equal to the number of key/value pairs.\n */\n key(index: number): string | null {\n return Array.from(this.data.keys())[index];\n }\n}\n"],"names":[],"mappings":";;AAEA;;;;;;AAMG;MACU,aAAa,CAAA;AAGxB,IAAA,WAAA,GAAA;AACE,QAAA,IAAI,CAAC,OAAO,GAAG,OAAO,YAAY,KAAK,WAAW,GAAG,YAAY,GAAG,IAAI,aAAa,EAAE,CAAC;KACzF;IAED,KAAK,GAAA;AACH,QAAA,IAAI,CAAC,OAAO,CAAC,KAAK,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AoB,EAAA;AACvC,QAAA,IAAI,KAAK,EAAE;YACT,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AAC3B,SAAA;AAAM,aAAA;AACL,YAAA,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACvB,SAAA;KACF;AAED;;AAEG;AACH,IAAA,UAAU,CAAC,GAAW,EAAA;AACpB,QAAA,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;KACvB;AAED;;AAEG;AACH,IAAA,GAAG,CAAC,KAAa,EAAA;AACf,QAAA,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC;KAC5C;AACF;;;;"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Decodes a base64 string.
3
+ * Handles both browser and Node environments.
4
+ * @param data The base-64 encoded input string.
5
+ * @returns The decoded string.
6
+ */
7
+ export declare function decodeBase64(data: string): string;
8
+ /**
9
+ * Encodes a base64 string.
10
+ * Handles both browser and Node environments.
11
+ * @param data The unencoded input string.
12
+ * @returns The base-64 encoded string.
13
+ */
14
+ export declare function encodeBase64(data: string): string;
@@ -0,0 +1,11 @@
1
+ import { Bundle } from '@medplum/fhirtypes';
2
+ /**
3
+ * More on Bundles can be found here
4
+ * http://hl7.org/fhir/R4/bundle.html
5
+ */
6
+ /**
7
+ * Takes a bundle and creates a Transaction Type bundle
8
+ * @param bundle The Bundle object that we'll receive from the search query
9
+ * @returns transaction type bundle
10
+ */
11
+ export declare function convertToTransactionBundle(bundle: Bundle): Bundle;
@@ -3,7 +3,8 @@
3
3
  * Source: https://stackoverflow.com/a/46432113
4
4
  */
5
5
  export declare class LRUCache<T> {
6
- #private;
6
+ private readonly max;
7
+ private readonly cache;
7
8
  constructor(max?: number);
8
9
  /**
9
10
  * Deletes all values from the cache.
@@ -31,4 +32,5 @@ export declare class LRUCache<T> {
31
32
  * @returns The array of keys in the cache.
32
33
  */
33
34
  keys(): IterableIterator<string>;
35
+ private first;
34
36
  }
@@ -30,6 +30,14 @@ export interface MedplumClientOptions {
30
30
  * Use this if you want to use a separate OAuth server.
31
31
  */
32
32
  authorizeUrl?: string;
33
+ /**
34
+ * FHIR URL path.
35
+ *
36
+ * Default value is "fhir/R4/".
37
+ *
38
+ * Use this if you want to use a different path when connecting to a FHIR server.
39
+ */
40
+ fhirUrlPath?: string;
33
41
  /**
34
42
  * OAuth2 token URL.
35
43
  *
@@ -360,7 +368,30 @@ export interface MailOptions {
360
368
 
361
369
  */
362
370
  export declare class MedplumClient extends EventTarget {
363
- #private;
371
+ private readonly fetch;
372
+ private readonly createPdfImpl?;
373
+ private readonly storage;
374
+ private readonly requestCache;
375
+ private readonly cacheTime;
376
+ private readonly baseUrl;
377
+ private readonly fhirBaseUrl;
378
+ private readonly authorizeUrl;
379
+ private readonly tokenUrl;
380
+ private readonly logoutUrl;
381
+ private readonly exchangeUrl;
382
+ private readonly onUnauthenticated?;
383
+ private readonly autoBatchTime;
384
+ private readonly autoBatchQueue;
385
+ private clientId?;
386
+ private clientSecret?;
387
+ private autoBatchTimerId?;
388
+ private accessToken?;
389
+ private refreshToken?;
390
+ private refreshPromise?;
391
+ private profilePromise?;
392
+ private profile?;
393
+ private config?;
394
+ private basicAuth?;
364
395
  constructor(options?: MedplumClientOptions);
365
396
  /**
366
397
  * Returns the current base URL for all API requests.
@@ -548,6 +579,13 @@ export declare class MedplumClient extends EventTarget {
548
579
  * @category Authentication
549
580
  */
550
581
  signInWithExternalAuth(authorizeUrl: string, clientId: string, redirectUri: string, baseLogin: BaseLoginRequest): Promise<void>;
582
+ /**
583
+ * Exchange an external access token for a Medplum access token.
584
+ * @param token The access token that was generated by the external identity provider.
585
+ * @param clientId The ID of the `ClientApplication` in your Medplum project that will be making the exchange request.
586
+ * @category Authentication
587
+ */
588
+ exchangeExternalAccessToken(token: string, clientId?: string): Promise<ProfileResource>;
551
589
  /**
552
590
  * Builds the external identity provider redirect URI.
553
591
  * @param authorizeUrl The external authorization URL.
@@ -670,6 +708,28 @@ export declare class MedplumClient extends EventTarget {
670
708
  * @returns Promise to the array of search results.
671
709
  */
672
710
  searchResources<K extends ResourceType>(resourceType: K, query?: QueryTypes, options?: RequestInit): ReadablePromise<ExtractResource<K>[]>;
711
+ /**
712
+ * Creates an
713
+ * [async generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncGenerator)
714
+ * over a series of FHIR search requests for paginated search results. Each iteration of the generator yields
715
+ * the array of resources on each page.
716
+ *
717
+ *
718
+ * ```typescript
719
+ * for await (const page of medplum.searchResourcePages('Patient', { _count: 10 })) {
720
+ * for (const patient of page) {
721
+ * console.log(`Processing Patient resource with ID: ${patient.id}`);
722
+ * }
723
+ * }
724
+ * ```
725
+ *
726
+ * @category Search
727
+ * @param resourceType The FHIR resource type.
728
+ * @param query Optional FHIR search query or structured query object. Can be any valid input to the URLSearchParams() constructor.
729
+ * @param options Optional fetch options.
730
+ * @returns An async generator, where each result is an array of resources for each page.
731
+ */
732
+ searchResourcePages<K extends ResourceType>(resourceType: K, query?: QueryTypes, options?: RequestInit): AsyncGenerator<ExtractResource<K>[]>;
673
733
  /**
674
734
  * Searches a ValueSet resource using the "expand" operation.
675
735
  * See: https://www.hl7.org/fhir/operation-valueset-expand.html
@@ -1215,6 +1275,8 @@ export declare class MedplumClient extends EventTarget {
1215
1275
  * @category Authentication
1216
1276
  */
1217
1277
  getLogins(): LoginState[];
1278
+ private addLogin;
1279
+ private refreshProfile;
1218
1280
  /**
1219
1281
  * @category Authentication
1220
1282
  */
@@ -1239,6 +1301,71 @@ export declare class MedplumClient extends EventTarget {
1239
1301
  * @returns Promise to the response body as a blob.
1240
1302
  */
1241
1303
  download(url: URL | string, options?: RequestInit): Promise<Blob>;
1304
+ /**
1305
+ * Returns the cache entry if available and not expired.
1306
+ * @param key The cache key to retrieve.
1307
+ * @param options Optional fetch options for cache settings.
1308
+ * @returns The cached entry if found.
1309
+ */
1310
+ private getCacheEntry;
1311
+ /**
1312
+ * Adds a readable promise to the cache.
1313
+ * @param key The cache key to store.
1314
+ * @param value The readable promise to store.
1315
+ */
1316
+ private setCacheEntry;
1317
+ /**
1318
+ * Adds a concrete value as the cache entry for the given resource.
1319
+ * This is used in cases where the resource is loaded indirectly.
1320
+ * For example, when a resource is loaded as part of a Bundle.
1321
+ * @param resource The resource to cache.
1322
+ */
1323
+ private cacheResource;
1324
+ /**
1325
+ * Deletes a cache entry.
1326
+ * @param key The cache key to delete.
1327
+ */
1328
+ private deleteCacheEntry;
1329
+ /**
1330
+ * Makes an HTTP request.
1331
+ * @param {string} method
1332
+ * @param {string} url
1333
+ * @param {string=} contentType
1334
+ * @param {Object=} body
1335
+ */
1336
+ private request;
1337
+ private fetchWithRetry;
1338
+ /**
1339
+ * Executes a batch of requests that were automatically batched together.
1340
+ */
1341
+ private executeAutoBatch;
1342
+ /**
1343
+ * Adds default options to the fetch options.
1344
+ * @param options The options to add defaults to.
1345
+ */
1346
+ private addFetchOptionsDefaults;
1347
+ /**
1348
+ * Sets the "Content-Type" header on fetch options.
1349
+ * @param options The fetch options.
1350
+ * @param contentType The new content type to set.
1351
+ */
1352
+ private setRequestContentType;
1353
+ /**
1354
+ * Sets the body on fetch options.
1355
+ * @param options The fetch options.
1356
+ * @param data The new content body.
1357
+ */
1358
+ private setRequestBody;
1359
+ /**
1360
+ * Handles an unauthenticated response from the server.
1361
+ * First, tries to refresh the access token and retry the request.
1362
+ * Otherwise, calls unauthenticated callbacks and rejects.
1363
+ * @param method The HTTP method of the original request.
1364
+ * @param url The URL of the original request.
1365
+ * @param contentType The content type of the original request.
1366
+ * @param body The body of the original request.
1367
+ */
1368
+ private handleUnauthenticated;
1242
1369
  /**
1243
1370
  * Starts a new PKCE flow.
1244
1371
  * These PKCE values are stateful, and must survive redirects and page refreshes.
@@ -1248,6 +1375,12 @@ export declare class MedplumClient extends EventTarget {
1248
1375
  codeChallengeMethod: string;
1249
1376
  codeChallenge: string;
1250
1377
  }>;
1378
+ /**
1379
+ * Redirects the user to the login screen for authorization.
1380
+ * Clears all auth state including local storage and session storage.
1381
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#AuthorizationEndpoint
1382
+ */
1383
+ private requestAuthorization;
1251
1384
  /**
1252
1385
  * Processes an OAuth authorization code.
1253
1386
  * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenRequest
@@ -1256,6 +1389,11 @@ export declare class MedplumClient extends EventTarget {
1256
1389
  * @category Authentication
1257
1390
  */
1258
1391
  processCode(code: string, loginParams?: Partial<BaseLoginRequest>): Promise<ProfileResource>;
1392
+ /**
1393
+ * Tries to refresh the auth tokens.
1394
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#RefreshTokens
1395
+ */
1396
+ private refresh;
1259
1397
  /**
1260
1398
  * Starts a new OAuth2 client credentials flow.
1261
1399
  * See: https://datatracker.ietf.org/doc/html/rfc6749#section-4.4
@@ -1265,6 +1403,13 @@ export declare class MedplumClient extends EventTarget {
1265
1403
  * @returns Promise that resolves to the client profile.
1266
1404
  */
1267
1405
  startClientLogin(clientId: string, clientSecret: string): Promise<ProfileResource>;
1406
+ /**
1407
+ * Sets the client ID and secret for basic auth.
1408
+ * @category Authentication
1409
+ * @param clientId The client ID.
1410
+ * @param clientSecret The client secret.
1411
+ */
1412
+ setBasicAuth(clientId: string, clientSecret: string): void;
1268
1413
  /**
1269
1414
  * Invite a user to a project.
1270
1415
  * @param projectId The project ID.
@@ -1272,4 +1417,22 @@ export declare class MedplumClient extends EventTarget {
1272
1417
  * @returns Promise that returns an invite result or an operation outcome.
1273
1418
  */
1274
1419
  invite(projectId: string, body: InviteBody): Promise<InviteResult | OperationOutcome>;
1420
+ /**
1421
+ * Makes a POST request to the tokens endpoint.
1422
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
1423
+ * @param formBody Token parameters in URL encoded format.
1424
+ */
1425
+ private fetchTokens;
1426
+ /**
1427
+ * Verifies the tokens received from the auth server.
1428
+ * Validates the JWT against the JWKS.
1429
+ * See: https://openid.net/specs/openid-connect-core-1_0.html#TokenEndpoint
1430
+ * @param tokens
1431
+ */
1432
+ private verifyTokens;
1433
+ /**
1434
+ * Sets up a listener for window storage events.
1435
+ * This synchronizes state across browser windows and browser tabs.
1436
+ */
1437
+ private setupStorageListener;
1275
1438
  }
@@ -4,7 +4,7 @@ interface Event {
4
4
  }
5
5
  type EventListener = (e: Event) => void;
6
6
  export declare class EventTarget {
7
- #private;
7
+ private readonly listeners;
8
8
  constructor();
9
9
  addEventListener(type: string, callback: EventListener): void;
10
10
  removeEventListeneer(type: string, callback: EventListener): void;
@@ -26,7 +26,8 @@ export interface InfixParselet {
26
26
  parse?(parser: Parser, left: Atom, token: Token): Atom;
27
27
  }
28
28
  export declare class ParserBuilder {
29
- #private;
29
+ private readonly prefixParselets;
30
+ private readonly infixParselets;
30
31
  registerInfix(tokenType: string, parselet: InfixParselet): ParserBuilder;
31
32
  registerPrefix(tokenType: string, parselet: PrefixParselet): ParserBuilder;
32
33
  prefix(tokenType: string, precedence: number, builder: (token: Token, right: Atom) => Atom): ParserBuilder;
@@ -34,7 +35,9 @@ export declare class ParserBuilder {
34
35
  construct(input: Token[]): Parser;
35
36
  }
36
37
  export declare class Parser {
37
- #private;
38
+ private tokens;
39
+ private prefixParselets;
40
+ private infixParselets;
38
41
  constructor(tokens: Token[], prefixParselets: Record<string, PrefixParselet>, infixParselets: Record<string, InfixParselet>);
39
42
  hasMore(): boolean;
40
43
  match(expected: string): boolean;