@medplum/core 3.0.3 → 3.0.5

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.
@@ -142,6 +142,33 @@ export declare class AndAtom extends BooleanInfixOperatorAtom {
142
142
 
143
143
  export declare function append<T>(array: T[] | undefined, value: T): T[];
144
144
 
145
+ /**
146
+ * Adds default values to `existingValue` for the given `key` and its children. If `key` is undefined,
147
+ * default values are added to all elements in `elements`. Default values consist of all fixed and pattern
148
+ * values defined in the relevant elements.
149
+ * @param existingValue - The
150
+ * @param elements - The elements to which default values should be added.
151
+ * @param key - (optional) The key of the element(s) for which default values should be added. Elements with nested
152
+ * keys are also included. If undefined, default values for all elements are added.
153
+ * @returns `existingValue` with default values added
154
+ */
155
+ export declare function applyDefaultValuesToElement(existingValue: object, elements: Record<string, InternalSchemaElement>, key?: string): object;
156
+
157
+ export declare function applyDefaultValuesToElementWithVisitor(existingValue: any, path: string, element: InternalSchemaElement, elements: Record<string, InternalSchemaElement>, schema: InternalTypeSchema): any;
158
+
159
+ /**
160
+ * Adds default values to `resource` based on the supplied `schema`. Default values includes all required fixed and pattern
161
+ * values specified on elements in the schema. If an element has a fixed/pattern value but is optional, i.e.
162
+ * `element.min === 0`, the default value is not added.
163
+ *
164
+ * @param resource - The resource to which default values should be added.
165
+ * @param schema - The schema to use for adding default values.
166
+ * @returns A clone of `resource` with default values added.
167
+ */
168
+ export declare function applyDefaultValuesToResource(resource: Resource, schema: InternalTypeSchema): Resource;
169
+
170
+ export declare function applyFixedOrPatternValue(inputValue: any, key: string, element: InternalSchemaElement, elements: Record<string, InternalSchemaElement>): any;
171
+
145
172
  export declare class ArithemticOperatorAtom extends BooleanInfixOperatorAtom {
146
173
  readonly impl: (x: number, y: number) => number | boolean;
147
174
  constructor(operator: string, left: Atom, right: Atom, impl: (x: number, y: number) => number | boolean);
@@ -237,8 +264,25 @@ export declare interface BotEvent<T = Resource | Hl7Message | string | Record<st
237
264
  readonly contentType: string;
238
265
  readonly input: T;
239
266
  readonly secrets: Record<string, ProjectSecret>;
267
+ readonly traceId?: string;
240
268
  }
241
269
 
270
+ export declare function buildElementsContext({ parentContext, path, elements, profileUrl, debugMode, }: {
271
+ /** The most recent `ElementsContextType` in which this context is being built. */
272
+ parentContext: ElementsContextType | undefined;
273
+ /** The FHIR path from the root resource to which the keys of `elements` are relative. */
274
+ path: string;
275
+ /**
276
+ * The mapping of keys to `InternalSchemaElement` at the current `path` relative to the
277
+ * root resource. This should be either `InternalTypeSchema.elements` or `SliceDefinition.elements`.
278
+ */
279
+ elements: Record<string, InternalSchemaElement>;
280
+ /** The URL, if any, of the resource profile or extension from which the `elements` collection originated. */
281
+ profileUrl?: string;
282
+ /** Whether debug logging is enabled */
283
+ debugMode?: boolean;
284
+ }): ElementsContextType | undefined;
285
+
242
286
  export declare function buildTypeName(components: string[]): string;
243
287
 
244
288
  /**
@@ -520,6 +564,30 @@ export declare class DotAtom extends InfixOperatorAtom {
520
564
  toString(): string;
521
565
  }
522
566
 
567
+ /**
568
+ * Information for the set of elements at a given path within in a resource. This mostly exists to
569
+ * normalize access to elements regardless of whether they are from a profile, extension, or slice.
570
+ */
571
+ export declare type ElementsContextType = {
572
+ /** The FHIR path from the root resource to which the keys of `elements` are relative. */
573
+ path: string;
574
+ /**
575
+ * The mapping of keys to `InternalSchemaElement` at the current `path` relative to the
576
+ * root resource. `elements` originate from either `InternalTypeSchema.elements` or
577
+ * `SliceDefinition.elements` when the elements context is created within a slice.
578
+ */
579
+ elements: Record<string, InternalSchemaElement>;
580
+ /**
581
+ * Similar mapping as `elements`, but with keys being the full path from the root resource rather
582
+ * than relative to `path`, in other words, the keys of the Record are `${path}.${key}`.
583
+ */
584
+ elementsByPath: Record<string, InternalSchemaElement>;
585
+ /** The URL, if any, of the resource profile or extension from which the `elements` collection originated. */
586
+ profileUrl: string | undefined;
587
+ /** Whether debug logging is enabled */
588
+ debugMode: boolean;
589
+ };
590
+
523
591
  export declare interface ElementType {
524
592
  code: string;
525
593
  targetProfile?: string[];
@@ -1187,6 +1255,8 @@ export declare function getDataType(type: string, profileUrl?: string): Internal
1187
1255
  */
1188
1256
  export declare function getDateProperty(date: string | undefined): Date | undefined;
1189
1257
 
1258
+ export declare function getDefaultValuesForNewSliceEntry(key: string, slice: SliceDefinition, slicing: SlicingRules, schema: InternalTypeSchema): Resource;
1259
+
1190
1260
  /**
1191
1261
  * Returns a display string for the resource.
1192
1262
  * @param resource - The input resource.
@@ -1261,6 +1331,14 @@ export declare function getNestedProperty(value: TypedValue, key: string, option
1261
1331
  profileUrl?: string;
1262
1332
  }): (TypedValue | TypedValue[] | undefined)[];
1263
1333
 
1334
+ /**
1335
+ * Returns the difference between two paths which is often suitable to use as a key in a `Record<string, InternalSchemaElement>`
1336
+ * @param parentPath - The parent path that will be removed from `path`.
1337
+ * @param path - The element path that should be a child of `parentPath`.
1338
+ * @returns - The difference between `path` and `parentPath` or `undefined` if `path` is not a child of `parentPath`.
1339
+ */
1340
+ export declare function getPathDifference(parentPath: string, path: string): string | undefined;
1341
+
1264
1342
  /**
1265
1343
  * Returns a human friendly display name for a FHIR element definition path.
1266
1344
  * @param path - The FHIR element definition path.
@@ -1361,6 +1439,10 @@ export declare interface GetTypedPropertyValueOptions {
1361
1439
  */
1362
1440
  export declare function getTypedPropertyValueWithSchema(typedValue: TypedValue, path: string, element: InternalSchemaElement): TypedValue[] | TypedValue | undefined;
1363
1441
 
1442
+ export declare function getValueSliceName(value: any, slices: SliceDefinitionWithTypes[], discriminators: SliceDiscriminator[], profileUrl: string | undefined): string | undefined;
1443
+
1444
+ export declare function getWebSocketUrl(path: string, baseUrl: URL | string): string;
1445
+
1364
1446
  /**
1365
1447
  * Global schema singleton.
1366
1448
  */
@@ -1782,6 +1864,8 @@ export declare class IsAtom extends BooleanInfixOperatorAtom {
1782
1864
 
1783
1865
  export declare function isCompletedSubscriptionRequest(subscriptionRequest: SubscriptionRequest | PendingSubscriptionRequest): subscriptionRequest is SubscriptionRequest;
1784
1866
 
1867
+ export declare function isComplexTypeCode(code: string): boolean;
1868
+
1785
1869
  export declare function isContextVersionRequired(event: string): event is FhircastEventVersionRequired;
1786
1870
 
1787
1871
  export declare function isCreated(outcome: OperationOutcome): boolean;
@@ -1921,6 +2005,8 @@ export declare function isResourceType(resourceType: string): boolean;
1921
2005
  */
1922
2006
  export declare function isResourceTypeSchema(typeSchema: InternalTypeSchema): boolean;
1923
2007
 
2008
+ export declare function isSliceDefinitionWithTypes(slice: SliceDefinition): slice is SliceDefinitionWithTypes;
2009
+
1924
2010
  /**
1925
2011
  * Returns true if the input array is an array of strings.
1926
2012
  * @param arr - Input array.
@@ -2212,6 +2298,7 @@ export declare class MedplumClient extends EventTarget_2 {
2212
2298
  private readonly onUnauthenticated?;
2213
2299
  private readonly autoBatchTime;
2214
2300
  private readonly autoBatchQueue;
2301
+ private subscriptionManager?;
2215
2302
  private medplumServer?;
2216
2303
  private clientId?;
2217
2304
  private clientSecret?;
@@ -3333,10 +3420,11 @@ export declare class MedplumClient extends EventTarget_2 {
3333
3420
  * @param method - The HTTP method (GET, POST, etc).
3334
3421
  * @param url - The target URL.
3335
3422
  * @param options - Optional fetch request init options.
3423
+ * @param state - Optional request state.
3336
3424
  * @returns The JSON content body if available.
3337
3425
  */
3338
3426
  private request;
3339
- private parseResponse;
3427
+ private parseBody;
3340
3428
  private fetchWithRetry;
3341
3429
  private logRequest;
3342
3430
  private logResponse;
@@ -3546,6 +3634,70 @@ export declare class MedplumClient extends EventTarget_2 {
3546
3634
  */
3547
3635
  private setupStorageListener;
3548
3636
  private retryCatch;
3637
+ /**
3638
+ * Gets the `SubscriptionManager` for WebSocket subscriptions.
3639
+ *
3640
+ * @category Subscriptions
3641
+ * @returns the `SubscriptionManager` for this client.
3642
+ */
3643
+ getSubscriptionManager(): SubscriptionManager;
3644
+ /**
3645
+ * Subscribes to a given criteria, listening to notifications over WebSockets.
3646
+ *
3647
+ * This uses Medplum's `WebSocket Subscriptions` under the hood.
3648
+ *
3649
+ * A `SubscriptionEmitter` is returned from this function, which can be used to listen for updates to resources described by the given criteria.
3650
+ *
3651
+ * When subscribing to the same criteria multiple times, the same `SubscriptionEmitter` will be returned, and a reference count will be incremented.
3652
+ *
3653
+ * -----
3654
+ * @example
3655
+ * ```ts
3656
+ * const emitter = medplum.subscribeToCriteria('Communication');
3657
+ *
3658
+ * emitter.addEventListener('message', (bundle: Bundle) => {
3659
+ * // Called when a `Communication` resource is created or modified
3660
+ * console.log(bundle?.entry?.[1]?.resource); // Logs the `Communication` resource that was updated
3661
+ * });
3662
+ * ```
3663
+ *
3664
+ * @category Subscriptions
3665
+ * @param criteria - The criteria to subscribe to.
3666
+ * @returns a `SubscriptionEmitter` that emits `Bundle` resources containing changes to resources based on the given criteria.
3667
+ */
3668
+ subscribeToCriteria(criteria: string): SubscriptionEmitter;
3669
+ /**
3670
+ * Unsubscribes from the given criteria.
3671
+ *
3672
+ * When called the same amount of times as proceeding calls to `subscribeToCriteria` on a given `criteria`,
3673
+ * the criteria is fully removed from the `SubscriptionManager`.
3674
+ *
3675
+ * @category Subscriptions
3676
+ * @param criteria - The criteria to unsubscribe from.
3677
+ */
3678
+ unsubscribeFromCriteria(criteria: string): void;
3679
+ /**
3680
+ * Get the master `SubscriptionEmitter` for the `SubscriptionManager`.
3681
+ *
3682
+ * The master `SubscriptionEmitter` gets messages for all subscribed `criteria` as well as WebSocket errors, `connect` and `disconnect` events, and the `close` event.
3683
+ *
3684
+ * It can also be used to listen for `heartbeat` messages.
3685
+ *
3686
+ *------
3687
+ * @example
3688
+ * ### Listening for `heartbeat`:
3689
+ * ```ts
3690
+ * const masterEmitter = medplum.getMasterSubscriptionEmitter();
3691
+ *
3692
+ * masterEmitter.addEventListener('heartbeat', (bundle: Bundle<SubscriptionStatus>) => {
3693
+ * console.log(bundle?.entry?.[0]?.resource); // A `SubscriptionStatus` of type `heartbeat`
3694
+ * });
3695
+ *
3696
+ * ```
3697
+ * @category Subscriptions
3698
+ * @returns the master `SubscriptionEmitter` from the `SubscriptionManager`.
3699
+ */
3700
+ getMasterSubscriptionEmitter(): SubscriptionEmitter;
3549
3701
  }
3550
3702
 
3551
3703
  /**
@@ -4110,8 +4262,9 @@ export declare class OrAtom extends BooleanInfixOperatorAtom {
4110
4262
  * FHIR criteria strings are found on resources such as Subscription.
4111
4263
  * @param criteria - The FHIR criteria string.
4112
4264
  * @returns Parsed search definition.
4265
+ * @deprecated Use parseSearchRequest instead.
4113
4266
  */
4114
- export declare function parseCriteriaAsSearchRequest(criteria: string): SearchRequest;
4267
+ export declare function parseCriteriaAsSearchRequest<T extends Resource = Resource>(criteria: string): SearchRequest<T>;
4115
4268
 
4116
4269
  /**
4117
4270
  * Parses a FHIRPath expression into an AST.
@@ -4198,21 +4351,23 @@ export declare function parseReference(reference: Reference | undefined): [Resou
4198
4351
  * Parses a URL string into a SearchRequest.
4199
4352
  * @param url - The URL to parse.
4200
4353
  * @returns Parsed search definition.
4354
+ * @deprecated Use parseSearchRequest instead.
4201
4355
  */
4202
4356
  export declare function parseSearchDefinition<T extends Resource = Resource>(url: string): SearchRequest<T>;
4203
4357
 
4204
4358
  /**
4205
4359
  * Parses a search URL into a search request.
4206
- * @param resourceType - The FHIR resource type.
4207
- * @param query - The collection of query string parameters.
4360
+ * @param url - The original search URL or the FHIR resource type.
4361
+ * @param query - Optional collection of additional query string parameters.
4208
4362
  * @returns A parsed SearchRequest.
4209
4363
  */
4210
- export declare function parseSearchRequest<T extends Resource = Resource>(resourceType: T['resourceType'], query: Record<string, string[] | string | undefined>): SearchRequest<T>;
4364
+ export declare function parseSearchRequest<T extends Resource = Resource>(url: T['resourceType'] | URL | string, query?: Record<string, string[] | string | undefined>): SearchRequest<T>;
4211
4365
 
4212
4366
  /**
4213
4367
  * Parses a search URL into a search request.
4214
4368
  * @param url - The search URL.
4215
4369
  * @returns A parsed SearchRequest.
4370
+ * @deprecated Use parseSearchRequest instead.
4216
4371
  */
4217
4372
  export declare function parseSearchUrl<T extends Resource = Resource>(url: URL): SearchRequest<T>;
4218
4373
 
@@ -4516,6 +4671,26 @@ export declare interface ResourceVisitor {
4516
4671
 
4517
4672
  export declare type ResourceWithCode = Resource & Code;
4518
4673
 
4674
+ export declare class RobustWebSocket extends TypedEventTarget<RobustWebSocketEventMap> {
4675
+ private ws;
4676
+ private messageBuffer;
4677
+ bufferedAmount: number;
4678
+ extensions: string;
4679
+ constructor(url: string);
4680
+ get readyState(): number;
4681
+ close(): void;
4682
+ send(message: string): void;
4683
+ }
4684
+
4685
+ export declare type RobustWebSocketEventMap = {
4686
+ open: {
4687
+ type: 'open';
4688
+ };
4689
+ message: MessageEvent;
4690
+ error: Event;
4691
+ close: CloseEvent;
4692
+ };
4693
+
4519
4694
  export declare const RXNORM = "http://www.nlm.nih.gov/research/umls/rxnorm";
4520
4695
 
4521
4696
  /**
@@ -4559,6 +4734,8 @@ export declare interface SearchRequest<T extends Resource = Resource> {
4559
4734
  include?: IncludeTarget[];
4560
4735
  revInclude?: IncludeTarget[];
4561
4736
  summary?: 'true' | 'text' | 'data';
4737
+ format?: string;
4738
+ pretty?: boolean;
4562
4739
  }
4563
4740
 
4564
4741
  /**
@@ -4618,6 +4795,11 @@ export declare interface SliceDefinition {
4618
4795
  binding?: ElementDefinitionBinding;
4619
4796
  }
4620
4797
 
4798
+ export declare type SliceDefinitionWithTypes = SliceDefinition & {
4799
+ type: NonNullable<SliceDefinition['type']>;
4800
+ typeSchema?: InternalTypeSchema;
4801
+ };
4802
+
4621
4803
  export declare interface SliceDiscriminator {
4622
4804
  path: string;
4623
4805
  type: string;
@@ -4689,6 +4871,85 @@ export declare type StringMap = {
4689
4871
  */
4690
4872
  export declare function structureMapTransform(structureMap: StructureMap, input: TypedValue[], loader?: (url: string) => StructureMap[]): TypedValue[];
4691
4873
 
4874
+ /**
4875
+ * An `EventTarget` that emits events when new subscription notifications come in over WebSockets.
4876
+ *
4877
+ * -----
4878
+ *
4879
+ * ### Events emitted:
4880
+ *
4881
+ * - `connect` - A new subscription is connected to the `SubscriptionManager` and `message` events for this subscription can be expected.
4882
+ * - `disconnect` - The specified subscription is no longer being monitored by the `SubscriptionManager`.
4883
+ * - `error` - An error has occurred.
4884
+ * - `message` - A message containing a notification `Bundle` has been received.
4885
+ * - `close` - The WebSocket has been closed.
4886
+ * - `heartbeat` - A `heartbeat` message has been received.
4887
+ */
4888
+ export declare class SubscriptionEmitter extends TypedEventTarget<SubscriptionEventMap> {
4889
+ private criteria;
4890
+ constructor(...criteria: string[]);
4891
+ getCriteria(): Set<string>;
4892
+ /**
4893
+ * @internal
4894
+ * @param criteria - The criteria to add to this `SubscriptionEmitter`.
4895
+ */
4896
+ _addCriteria(criteria: string): void;
4897
+ /**
4898
+ * @internal
4899
+ * @param criteria - The criteria to remove from this `SubscriptionEmitter`.
4900
+ */
4901
+ _removeCriteria(criteria: string): void;
4902
+ }
4903
+
4904
+ export declare type SubscriptionEventMap = {
4905
+ connect: {
4906
+ type: 'connect';
4907
+ payload: {
4908
+ subscriptionId: string;
4909
+ };
4910
+ };
4911
+ disconnect: {
4912
+ type: 'disconnect';
4913
+ payload: {
4914
+ subscriptionId: string;
4915
+ };
4916
+ };
4917
+ error: {
4918
+ type: 'error';
4919
+ payload: Error;
4920
+ };
4921
+ message: {
4922
+ type: 'message';
4923
+ payload: Bundle;
4924
+ };
4925
+ close: {
4926
+ type: 'close';
4927
+ };
4928
+ heartbeat: {
4929
+ type: 'heartbeat';
4930
+ payload: Bundle;
4931
+ };
4932
+ };
4933
+
4934
+ export declare class SubscriptionManager {
4935
+ private readonly medplum;
4936
+ private ws;
4937
+ private masterSubEmitter?;
4938
+ private criteriaEntries;
4939
+ private criteriaEntriesBySubscriptionId;
4940
+ private wsClosed;
4941
+ constructor(medplum: MedplumClient, wsUrl: URL | string);
4942
+ private setupWebSocketListeners;
4943
+ private emitConnect;
4944
+ private emitError;
4945
+ private getTokenForCriteria;
4946
+ addCriteria(criteria: string): SubscriptionEmitter;
4947
+ removeCriteria(criteria: string): void;
4948
+ closeWebSocket(): void;
4949
+ getCriteriaCount(): number;
4950
+ getMasterEmitter(): SubscriptionEmitter;
4951
+ }
4952
+
4692
4953
  /**
4693
4954
  * A `FHIRcast` subscription request.
4694
4955
  *