82 lines
3.0 KiB
TypeScript
82 lines
3.0 KiB
TypeScript
import { HttpClient } from '@angular/common/http';
|
|
import { inject, Injectable } from '@angular/core';
|
|
import type { Bundle, CodeableConcept, Extension, FhirResource } from 'fhir/r4';
|
|
import { firstValueFrom } from 'rxjs';
|
|
import { ApiConfiguration } from './api/api-configuration';
|
|
|
|
/** Our extensions and code systems (core.fhir). */
|
|
export const EXT = 'https://isssteleon.org/fhir/StructureDefinition/';
|
|
|
|
export type Params = Record<string, string | number | readonly (string | number)[] | undefined>;
|
|
|
|
/** The domain's FHIR base (`/{domain}/fhir`): reads and searches, typed as R4. */
|
|
@Injectable({ providedIn: 'root' })
|
|
export class Fhir {
|
|
private readonly http = inject(HttpClient);
|
|
private readonly root = inject(ApiConfiguration).rootUrl;
|
|
|
|
read<T extends FhirResource>(domain: string, type: T['resourceType'], id: string): Promise<T> {
|
|
return firstValueFrom(this.http.get<T>(`${this.root}/${domain}/fhir/${type}/${id}`));
|
|
}
|
|
|
|
/** One page of a search. */
|
|
search(domain: string, type: string, params: Params = {}): Promise<Bundle> {
|
|
const query = Object.fromEntries(
|
|
Object.entries(params).filter(([, v]) => v !== undefined),
|
|
) as Record<string, string | number | readonly (string | number)[]>;
|
|
return firstValueFrom(
|
|
this.http.get<Bundle>(`${this.root}/${domain}/fhir/${type}`, { params: query }),
|
|
);
|
|
}
|
|
|
|
/** The page a bundle's `next` (or `previous`) link points to, through our own base. */
|
|
follow(domain: string, bundle: Bundle, relation: 'next' | 'previous'): Promise<Bundle> | null {
|
|
const link = bundle.link?.find((l) => l.relation === relation)?.url;
|
|
if (!link) return null;
|
|
const url = new URL(link);
|
|
const type = url.pathname.split('/').pop();
|
|
return firstValueFrom(
|
|
this.http.get<Bundle>(`${this.root}/${domain}/fhir/${type}${url.search}`),
|
|
);
|
|
}
|
|
|
|
/** Every page of a search. */
|
|
async all(domain: string, type: string, params: Params = {}): Promise<Bundle[]> {
|
|
const pages = [await this.search(domain, type, params)];
|
|
for (let next = this.follow(domain, pages[0], 'next'); next;) {
|
|
const page = await next;
|
|
pages.push(page);
|
|
next = this.follow(domain, page, 'next');
|
|
}
|
|
return pages;
|
|
}
|
|
}
|
|
|
|
/** The resources of a bundle (or of several pages), by search mode. */
|
|
export function resources<T extends FhirResource>(
|
|
bundles: Bundle | Bundle[],
|
|
type: T['resourceType'],
|
|
mode?: 'match' | 'include',
|
|
): T[] {
|
|
return [bundles]
|
|
.flat()
|
|
.flatMap((b) => b.entry ?? [])
|
|
.filter((e) => e.resource?.resourceType === type && (!mode || e.search?.mode === mode))
|
|
.map((e) => e.resource as T);
|
|
}
|
|
|
|
export function ext(
|
|
element: { extension?: Extension[] } | undefined,
|
|
name: string,
|
|
): Extension | undefined {
|
|
return element?.extension?.find((e) => e.url === EXT + name);
|
|
}
|
|
|
|
export function code(concept: CodeableConcept | undefined): string | undefined {
|
|
return concept?.coding?.[0]?.code;
|
|
}
|
|
|
|
export function label(concept: CodeableConcept | undefined): string {
|
|
return concept?.text ?? concept?.coding?.[0]?.display ?? concept?.coding?.[0]?.code ?? '';
|
|
}
|