fhir-kit-client
Version:
137 lines • 5.44 kB
JavaScript
/**
* Inspect a FHIR CapabilityStatement for server and resource capabilities.
*
* @example
* const capabilities = new CapabilityTool(capabilityStatement);
*
* // Check resource-level interaction
* capabilities.resourceCan('Patient', 'read'); // true | false
*
* // General capability check
* capabilities.supportFor({ resourceType: 'Patient', capabilityType: 'interaction', where: { code: 'read' } });
*/
export class CapabilityTool {
capabilityStatement;
constructor(capabilityStatement) {
this.capabilityStatement = capabilityStatement;
}
/**
* Check whether a server-level interaction is supported.
*
* @param interaction - Interaction code (e.g. `'transaction'`, `'batch'`).
* @returns `true` if the server advertises the interaction.
*/
serverCan(interaction) {
return this.supportFor({ capabilityType: 'interaction', where: { code: interaction } });
}
/**
* Check whether a resource-level interaction is supported.
*
* @param resource - FHIR resource type (e.g. `'Patient'`).
* @param interaction - Interaction code (e.g. `'read'`, `'search-type'`).
* @returns `true` if the resource advertises the interaction.
*/
resourceCan(resource, interaction) {
return this.supportFor({ resourceType: resource, capabilityType: 'interaction', where: { code: interaction } });
}
/**
* Check whether a server-level search parameter is supported.
*
* @param searchParam - Search parameter name.
* @returns `true` if the server advertises the search parameter.
*/
serverSearch(searchParam) {
return this.supportFor({ capabilityType: 'searchParam', where: { name: searchParam } });
}
/**
* Check whether a resource-level search parameter is supported.
*
* @param resource - FHIR resource type.
* @param searchParam - Search parameter name.
* @returns `true` if the resource advertises the search parameter.
*/
resourceSearch(resource, searchParam) {
return this.supportFor({ resourceType: resource, capabilityType: 'searchParam', where: { name: searchParam } });
}
/**
* General capability check. Returns `true` if the specified capability
* exists (optionally filtered by a `where` clause).
*
* @param params - Capability lookup parameters.
* @returns `true` if the capability is found.
*/
supportFor(params) {
const { resourceType, capabilityType, where } = params ?? {};
if (!capabilityType)
return false;
const capabilities = resourceType ? this.resourceCapabilities({ resourceType }) : this.serverCapabilities();
if (!capabilities)
return false;
const capability = capabilities[capabilityType];
if (where && capability) {
const whereKey = Object.keys(where)[0];
return capability.some((item) => item[whereKey] === where[whereKey]);
}
return capability !== undefined;
}
/**
* List interaction codes supported by a resource type.
*
* @param params - `{ resourceType }` — the resource type to inspect.
* @returns Array of interaction code strings (e.g. `['read', 'search-type']`).
*/
interactionsFor(params) {
const { resourceType } = params ?? {};
const caps = resourceType ? this.resourceCapabilities({ resourceType }) : undefined;
if (!caps?.interaction)
return [];
return caps.interaction.map((i) => i.code ?? '');
}
/**
* List search parameter names supported by a resource type.
*
* @param params - `{ resourceType }` — the resource type to inspect.
* @returns Array of search parameter name strings.
*/
searchParamsFor(params) {
const { resourceType } = params ?? {};
const caps = resourceType ? this.resourceCapabilities({ resourceType }) : undefined;
if (!caps?.searchParam)
return [];
return caps.searchParam.map((p) => p.name ?? '');
}
/**
* Return the full capability entry for a given resource type.
*
* @param params - `{ resourceType }` — the resource type to look up.
* @returns The raw capability object, or `undefined` if not found.
*/
resourceCapabilities(params) {
const { resourceType } = params ?? {};
const resources = this.serverCapabilities()?.resource;
return resources?.find((r) => r.type === resourceType);
}
/**
* Return the raw value of a specific capability category for a resource type.
*
* @param params - `{ resourceType, capabilityType }`.
* @returns The raw capability value (e.g. an array of interaction objects), or `undefined`.
*/
capabilityContents(params) {
const { resourceType, capabilityType } = params ?? {};
if (!resourceType || !capabilityType)
return undefined;
const caps = this.resourceCapabilities({ resourceType });
return caps?.[capabilityType];
}
/**
* Return the server-mode REST capability block from the CapabilityStatement.
*
* @returns The server REST capability, or `undefined` if not present.
*/
serverCapabilities() {
const rest = this.capabilityStatement.rest;
return rest?.find((r) => r.mode === 'server');
}
}
//# sourceMappingURL=capability-tool.js.map