@oystehr/sdk
Version:
Oystehr SDK
131 lines (109 loc) • 4 kB
text/typescript
import type { Operation } from 'fast-json-patch';
import type {
Binary as BinaryR4B,
Bundle as BundleR4B,
BundleEntry as BundleEntryR4B,
FhirResource as FhirResourceR4B,
OperationOutcome as OperationOutcomeR4B,
} from 'fhir/r4b';
import type {
Binary as BinaryR5,
Bundle as BundleR5,
BundleEntry as BundleEntryR5,
FhirResource as FhirResourceR5,
OperationOutcome as OperationOutcomeR5,
} from 'fhir/r5';
export type FhirResource = FhirResourceR4B | FhirResourceR5;
// We're doing this "Omit dance" with the Bundle types because TypeScript
// produces incorrect BundleEntry<F> types when F is a union of multiple FHIR
// types. The guess is it's something about the conditional generic typing, but
// we haven't found a different way to resolve it.
//
// No "omit dance":
// entry: BundleEntry<Patient>[] | BundleEntry<Practitioner>[] | undefined
// With "omit dance":
// entry: (BundleEntry<Patient> | BundleEntry<Practitioner>)[] | undefined
//
// The latter can be used with array functions like `.find()`.
export type FhirBundle<F extends FhirResource> = F extends FhirResourceR4B ? BundleR4B<F> : BundleR5<F>;
export type EntrylessFhirBundle<F extends FhirResource> = F extends FhirResourceR4B
? Omit<BundleR4B<F>, 'entry'>
: Omit<BundleR5<F>, 'entry'>;
export type Bundle<F extends FhirResource> = EntrylessFhirBundle<F> & {
entry?: Array<BundleEntry<F>> | undefined;
unbundle: (this: { entry?: Array<BundleEntry<F>> }) => F[];
};
export type BundleEntry<F extends FhirResource> = F extends FhirResourceR4B ? BundleEntryR4B<F> : BundleEntryR5<F>;
export type Binary<F extends FhirResource> = F extends FhirResourceR4B ? BinaryR4B : BinaryR5;
export type OperationOutcome = OperationOutcomeR4B | OperationOutcomeR5;
export type SearchParam = { name: string; value: string | number };
export interface FhirSearchParams<T extends FhirResource> {
resourceType: T['resourceType'];
params?: SearchParam[];
}
export interface FhirGetParams<T extends FhirResource> {
resourceType: T['resourceType'];
id: string;
}
export type FhirCreateParams<T extends FhirResource> = T;
export type FhirUpdateParams<T extends FhirResource> = T;
export interface FhirPatchParams<T extends FhirResource> {
resourceType: T['resourceType'];
id: string;
operations: Operation[];
}
export interface FhirDeleteParams<T extends FhirResource> {
resourceType: T['resourceType'];
id: string;
}
export interface FhirHistorySearchParams<T extends FhirResource> {
resourceType: T['resourceType'];
id: string;
}
export interface FhirHistoryGetParams<T extends FhirResource> {
resourceType: T['resourceType'];
id: string;
versionId: string;
}
interface BatchInputBaseRequest {
url: string;
}
export interface BatchInputGetRequest extends BatchInputBaseRequest {
method: 'GET';
}
export interface BatchInputHeadRequest extends BatchInputBaseRequest {
method: 'HEAD';
}
export interface BatchInputDeleteRequest extends BatchInputBaseRequest {
method: 'DELETE';
}
export interface BatchInputPutRequest<F extends FhirResource> extends BatchInputBaseRequest {
method: 'PUT';
resource: F;
}
export interface BatchInputPostRequest<F extends FhirResource> extends BatchInputBaseRequest {
method: 'POST';
resource: F;
fullUrl?: string;
}
export interface BatchInputBinaryPatchRequest<F extends FhirResource> extends BatchInputBaseRequest {
method: 'PATCH';
resource: Binary<F>;
}
export interface BatchInputJSONPatchRequest extends BatchInputBaseRequest {
method: 'PATCH';
operations: Operation[];
}
export type BatchInputPatchRequest<F extends FhirResource> =
| BatchInputBinaryPatchRequest<F>
| BatchInputJSONPatchRequest;
export type BatchInputRequest<F extends FhirResource> =
| BatchInputGetRequest
| BatchInputHeadRequest
| BatchInputPutRequest<F>
| BatchInputPatchRequest<F>
| BatchInputPostRequest<F>
| BatchInputDeleteRequest;
export interface BatchInput<F extends FhirResource> {
requests: BatchInputRequest<F>[];
}