import { ParametersParameter, Attachment, OperationOutcome, Parameters, FhirResource, QuestionnaireResponse, Questionnaire, Bundle } from 'fhir/r4'; /** * Output parameters for the $extract operation * @see {@link http://hl7.org/fhir/uv/sdc/OperationDefinition-QuestionnaireResponse-extract.html} */ interface OutputParameters extends Parameters { parameter: OutputParamArray; } type OutputParamArray = [ReturnParameter, IssuesParameter, CustomDebugInfoParameter] | [ReturnParameter, IssuesParameter] | [ReturnParameter, CustomDebugInfoParameter] | [ReturnParameter]; interface ReturnParameter extends ParametersParameter { name: 'return'; resource: FhirResource; } interface IssuesParameter extends ParametersParameter { name: 'issues'; resource: OperationOutcome; } interface CustomDebugInfoParameter extends ParametersParameter { name: 'debugInfo-custom'; valueAttachment: Attachment; } /** * Result of evaluating a `valueExpression` from the `templateExtractValue` extension. * e.g. `"item.where(linkId = 'family').answer.value.first()"` → `"Doe"` */ interface TemplateExtractValueEvaluation { valueExpression: string; valueResult: any[]; } /** * Represents the full extraction mapping for a single entry in a template, including both context and value extraction paths and expressions. */ interface TemplateExtractPath { /** * A tuple of [contextPath, contextExpression] from the `templateExtractContext` extension. * e.g. `<"Patient.name[0].extension[0]", " item.where(linkId = 'name')">` * `null` when the extraction is based on a standalone value path only. */ contextPathTuple: [string, string] | null; /** * A map of pairs from the `templateExtractValue` extension. * e.g. `<"Patient.name[0]._family.extension[0]", {valueExpression: "item.where(linkId = 'family').answer.value.first()", valueResult: [["Doe"]]}>` */ valuePathMap: Map; } interface TemplateExtractPathJsObject { contextPathTuple: [string, string] | null; valuePathMap: Record; generatedFullId?: string; } type TemplateExtractPathJsObjectTuple = [ string, Record ]; interface TemplateExtractDebugInfo { templateIdToExtractPathTuples: Record; } /** * Input parameters for the $extract operation * @see {@link http://hl7.org/fhir/uv/sdc/OperationDefinition-QuestionnaireResponse-extract.html} */ interface InputParameters extends Parameters { parameter: InputParamArray; } type InputParamOptionalExtras = [] | [CustomQuestionnaireParameter] | [CustomComparisonSourceResponseParameter] | [CustomQuestionnaireParameter, CustomComparisonSourceResponseParameter] | [CustomComparisonSourceResponseParameter, CustomQuestionnaireParameter]; type InputParamArray = [QuestionnaireResponseParameter, ...InputParamOptionalExtras]; interface QuestionnaireResponseParameter extends ParametersParameter { name: 'questionnaire-response'; resource: QuestionnaireResponse; } interface CustomQuestionnaireParameter extends ParametersParameter { name: 'questionnaire'; resource: Questionnaire; } interface CustomComparisonSourceResponseParameter extends ParametersParameter { name: 'comparison-source-response'; resource: QuestionnaireResponse; } interface FetchQuestionnaireRequestConfig { sourceServerUrl: string; [key: string]: any; } /** * To define a method to fetch resources from the FHIR server with a given query string * Method should be able to handle both absolute urls and query strings * * @param query - The query URL of the FHIR resource * @param requestConfig - Questionnaire request configs - must have sourceServerUrl + any key value pair - can be headers, auth tokens or endpoints * @returns A promise of the FHIR resource (or an error)! * * @example * const ABSOLUTE_URL_REGEX = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/; * * export const fetchQuestionnaireCallback: FetchQuestionnaireCallback = async ( * query: string, * requestConfig: FetchQuestionnaireRequestConfig * ) => { * let { sourceServerUrl } = requestConfig; * const { authToken } = requestConfig; * * const headers: Record = { * Accept: 'application/json;charset=utf-8' * }; * * if (authToken) { * headers['Authorization'] = `Bearer ${authToken}`; * } * * if (!sourceServerUrl.endsWith('/')) { * sourceServerUrl += '/'; * } * * const requestUrl = ABSOLUTE_URL_REGEX.test(query) ? query : `${sourceServerUrl}${query}`; * const response = await fetch(requestUrl, { headers }); * * if (!response.ok) { * throw new Error(`HTTP error when performing ${requestUrl}. Status: ${response.status}`); * } * * return response.json(); * }; * * * @author Sean Fong */ interface FetchQuestionnaireCallback { (query: string, requestConfig: FetchQuestionnaireRequestConfig): Promise; } /** * Interface representing a https://build.fhir.org/fhirpatch.html */ interface FhirPatchParameters extends Parameters { resourceType: 'Parameters'; parameter: FhirPatchParameterEntry[]; } type FhirPatchOperationType = 'add' | 'insert' | 'delete' | 'replace' | 'move'; type FhirPatchTypePart = { name: 'type'; valueCode: FhirPatchOperationType; }; type FhirPatchPathPart = { name: 'path'; valueString: string; }; type FhirPatchPart = FhirPatchTypePart | FhirPatchPathPart | { name: 'name'; valueString: string; } | { name: 'value'; [key: string]: unknown; } | { name: 'index'; valueInteger: number; } | { name: 'source'; valueInteger: number; } | { name: 'destination'; valueInteger: number; }; interface FhirPatchParameterEntry extends ParametersParameter { part: FhirPatchPart[]; } declare function extract(inputParameters: InputParameters | QuestionnaireResponse, fetchQuestionnaireCallback: FetchQuestionnaireCallback, fetchQuestionnaireRequestConfig: FetchQuestionnaireRequestConfig): Promise; /** * Create input parameters to be passed to sdc-template-extract extract(). Questionnaire parameter is optional. * Refer to https://build.fhir.org/ig/HL7/sdc/OperationDefinition-QuestionnaireResponse-extract.html. */ declare function createInputParameters(questionnaireResponse: QuestionnaireResponse, questionnaire: Questionnaire | undefined, comparisonSourceResponse: QuestionnaireResponse | undefined): InputParameters; /** * Checks whether a Questionnaire or any of its items contains a valid `sdc-questionnaire-templateExtract` extension with a required `template` slice. * Array.prototype.some() is short-circuiting, so it will return true as soon as it finds a valid extension. * * @param questionnaire - The FHIR Questionnaire to check. * @returns `true` if at least one valid template extract reference exists. */ declare function canBeTemplateExtracted(questionnaire: Questionnaire): boolean; /** * Logs a summary table of all extractable context and value paths from a given template. * Useful for debugging or inspecting the structure of a `TemplateExtractPath` map. * * Each row of the table includes: * - the entry path (FHIRPath to the context element), * - the context location, expression and result * - the value path and corresponding extract expression + result. * * @param {string} templateId - The identifier for the template being logged. * @param {Map} templateExtractPathMap - A map of FHIRPath entries to `TemplateExtractPath` objects, representing extract contexts and value expressions. * * @example * Example output: * * ``` * 🔢 All columns for: PatientTemplate * * | entryPath | contextPath | contextExpression | valuePath | valueExpression | valueResult | * |-----------------------|----------------------------------- |------------------------------------------------- |------------------------------------------ |--------------------------------------------------------------------- |------------------------| * | Patient.identifier[0] | Patient.identifier[0].extension[0] | item.where(linkId = 'ihi').answer.value | Patient.identifier[0]._value.extension[0] | first() | [ "8003608833357361" ] | * | Patient.name[0] | Patient.name[0].extension[0] | item.where(linkId = 'name') | Patient.name[0]._text.extension[0] | item.where(linkId='given' or linkId='family').answer.value.join(' ') | [ "Jane" ] | * | Patient.name[0] | Patient.name[0].extension[0] | item.where(linkId = 'name') | Patient.name[0]._family.extension[0] | item.where(linkId = 'family').answer.value.first() | [ "Doe" ] | * | Patient.telecom[0] | Patient.telecom[0].extension[0] | item.where(linkId = 'mobile-phone').answer.value | Patient.telecom[0]._value.extension[0] | first() | [ "0491 572 665" ] | * | Patient._gender | null | null | Patient._gender.extension[0] | item.where(linkId = 'gender').answer.value.first().code | [ "female" ] | * ``` */ declare function logTemplateExtractPathMapFull(templateId: string, templateExtractPathMap: Map): void; declare function logTemplateExtractPathMapJsObjectFull(templateId: string, templateExtractPathMap: Record): void; /** * Logs a simplified table showing only the entry path, evaluated context result, * and the evaluated value result for each extractable template path. * * This is useful for quickly verifying extracted values without inspecting the full expression logic. * * Each row includes: * - `entryPath`: The FHIRPath location in the resource, * - `valueResult`: The result of evaluating the value expression. * * @param {string} templateId - The identifier for the template being logged. * @param {Map} templateExtractPathMap - A map of FHIRPath entries to `TemplateExtractPath` objects, representing extract contexts and value expressions. * * @example * Example output: * * ``` * 🔹 Result columns for: PatientTemplate * * | entryPath | valueResult | * |-----------------------|------------------------| * | Patient.identifier[0] | [ "8003608833357361" ] | * | Patient.name[0] | [ "Jane" ] | * | Patient.name[0] | [ "Doe" ] | * | Patient.telecom[0] | [ "0491 572 665" ] | * | Patient._gender | [ "female" ] | * ``` */ declare function logTemplateExtractPathMapResults(templateId: string, templateExtractPathMap: Map): void; declare function logTemplateExtractPathMapJsObjectResults(templateId: string, templateExtractPathMap: Record): void; declare function parametersIsFhirPatch(parameters: Parameters): parameters is FhirPatchParameters; type QuestionnaireOrCallback = Questionnaire | FetchQuestionnaireResolver; interface FetchQuestionnaireResolver { fetchQuestionnaireCallback: FetchQuestionnaireCallback; fetchQuestionnaireRequestConfig: FetchQuestionnaireRequestConfig; } interface ExtractResult { extractedBundle: Bundle; issues: OperationOutcome | null; debugInfo: TemplateExtractDebugInfo | null; } interface InAppExtractOutput { extractSuccess: boolean; extractResult: ExtractResult | OperationOutcome; } /** * An abstraction layer over the SDC `extract()` function, which implements the `$extract` operation. * * This utility handles: * - Packing of input parameters (e.g., QuestionnaireResponse, Questionnaire, comparison source). * - Unpacking of output parameters (e.g., return Bundle, issues, debug info). * - Optional fallback handling when a Questionnaire resource is not provided directly. * * @param questionnaireResponse - The primary `QuestionnaireResponse` to extract data from. * @param questionnaireOrCallback - Either a `Questionnaire` resource or a fetch/callback configuration for dynamic retrieval. * @param comparisonSourceResponse - An optional `QuestionnaireResponse` used for comparison when extracting. If this is provided, only "modified" items will be extracted. * * @returns A promise resolving to an `InAppExtractOutput`: * - On success: `{ extractSuccess: true, extractResult: { extractedBundle, issues?, debugInfo? } }` * - On failure: `{ extractSuccess: false, extractResult: OperationOutcome }` */ declare function inAppExtract(questionnaireResponse: QuestionnaireResponse, questionnaireOrCallback: QuestionnaireOrCallback, comparisonSourceResponse: QuestionnaireResponse | null): Promise; declare function extractResultIsOperationOutcome(extractResult: ExtractResult | OperationOutcome): extractResult is OperationOutcome; declare function objIsTemplateExtractDebugInfo(obj: object): obj is TemplateExtractDebugInfo; export { type CustomDebugInfoParameter, type ExtractResult, type FetchQuestionnaireCallback, type FetchQuestionnaireRequestConfig, type FetchQuestionnaireResolver, type FhirPatchParameterEntry, type FhirPatchParameters, type InAppExtractOutput, type InputParameters, type IssuesParameter, type OutputParameters, type QuestionnaireOrCallback, type ReturnParameter, type TemplateExtractDebugInfo, type TemplateExtractPathJsObject, type TemplateExtractPathJsObjectTuple, type TemplateExtractValueEvaluation, canBeTemplateExtracted, createInputParameters, extract, extractResultIsOperationOutcome, inAppExtract, logTemplateExtractPathMapFull, logTemplateExtractPathMapJsObjectFull, logTemplateExtractPathMapJsObjectResults, logTemplateExtractPathMapResults, objIsTemplateExtractDebugInfo, parametersIsFhirPatch };