# @aehrc/sdc-template-extract

> Performs template-based extraction from the HL7 FHIR SDC (Structured Data Capture) specification: https://build.fhir.org/ig/HL7/sdc/extraction.html#template-based-extraction

Latest version **1.0.16** (published 2026-09-10) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @aehrc/sdc-template-extract
pnpm add @aehrc/sdc-template-extract
yarn add @aehrc/sdc-template-extract
bun add @aehrc/sdc-template-extract
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 1.0.16 |
| Published | 2026-09-10 |
| First published | 2025-05-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 5 |
| Unpacked size | 743.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 61 |
| Author | AEHRC |
| Maintainers | clinnygee, janadh, maryammehdiza, johngrimes, leoniedickson |

## Links

- npm: https://www.npmjs.com/package/@aehrc/sdc-template-extract
- Repository: https://github.com/aehrc/smart-forms
- Homepage: https://github.com/aehrc/smart-forms#readme
- Issues: https://github.com/aehrc/smart-forms/issues
- npm.io page: https://npm.io/package/@aehrc/sdc-template-extract

## Dependencies (5)

- [uuid](https://npm.io/package/uuid.md) ^11.1.0
- [fhirpath](https://npm.io/package/fhirpath.md) ^4.10.1
- [deepmerge](https://npm.io/package/deepmerge.md) ^4.3.1
- [js-base64](https://npm.io/package/js-base64.md) ^3.7.8
- [clean-deep](https://npm.io/package/clean-deep.md) ^3.4.0

## Recent versions

- 1.0.16 (latest) — 2026-09-10
- 1.0.15 — 2026-01-13
- 1.0.14 — 2025-10-23
- 1.0.13 — 2025-10-17
- 1.0.12 — 2025-10-03
- 1.0.11 — 2025-10-02
- 1.0.10 — 2025-10-02
- 1.0.9 — 2025-09-23
- 1.0.8 — 2025-08-29
- 1.0.7 — 2025-08-11
- 1.0.6 — 2025-07-16
- 1.0.5 — 2025-06-25
- 1.0.4 — 2025-06-23
- 1.0.3 — 2025-06-13
- 1.0.2 — 2025-06-13
- … 3 more at https://npm.io/package/@aehrc/sdc-template-extract/versions

## README

# SDC-Template-based Extract

A Typescript reference implementation of the [$extract](https://build.fhir.org/ig/HL7/sdc/OperationDefinition-QuestionnaireResponse-extract.html) operation from the [HL7 FHIR Structured Data Capture Specification](http://hl7.org/fhir/uv/sdc/ImplementationGuide/hl7.fhir.uv.sdc). This only covers [template-based extraction](https://build.fhir.org/ig/HL7/sdc/extraction.html#template-based-extraction).

## Usage
There are two ways to use this package:
1. Using it in a web app
2. Using it in a backend service e.g. ExpressJS

### Using it in a web app
It is recommended to use `inAppExtraction()`, which performs an in-app extraction. This means that the app is not sending data to an external server, but rather using JavaScript to perform the extraction in the browser.

```ts
const responseToExtract = structuredClone(savedResponse); // Good practice to deep clone the response to prevent mutating the original response
const inAppExtractOutput = await inAppExtract(
  responseToExtract,
  sourceQuestionnaire
);

const { extractResult, extractSuccess } = inAppExtractOutput;

// Extraction is successful
if (extractSuccess && !extractResultIsOperationOutcome(extractResult)) {
  const extractedBundle = extractResult.extractedBundle;

  // Do things with the successfully extracted transaction bundle...
}
```

Available parameters for `inAppExtract()`:

| Parameter                      | Description                                                                                                                                                                                      |
|-------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `questionnaireResponse`               | QuestionnaireResponse to extract data from                                                                                                                                                       |
| `questionnaireOrCallback`       | Either a `Questionnaire` resource or a fetch/callback configuration for dynamic retrieval (if module making the inAppExtract() call does not have access to the Questionnaire resource).         |
| `comparisonSourceResponse`  | An optional `QuestionnaireResponse` used for comparison when extracting. If this is provided, only "modified" items will be extracted. In most cases, this likely is the pre-populated response. |


### Using it in a backend service
Using this library in a backend service requires more pre-configuration.
Due to how the [$extract](https://build.fhir.org/ig/HL7/sdc/OperationDefinition-QuestionnaireResponse-extract.html) works, you will need to provide a `Parameters` resource defining the input parameters conforming to the `extract()` function.

sdc-template-extract allows you to specify custom `questionnaire` and `comparison-source-response` parameters, allowing you to pass their respective resources directly to the `extract()` function.
A library function `createInputParameters()` is provided to help you create the `Parameters` resource.

There is no sample implementation of using the `extract()` function in a backend service as of now.


### Local development notes
It's recommended to run this library within a web app or a service if you're doing local development.
This library compiles to both CommonJS and ES Modules, so there is no problems in using it across web frameworks and Node-based backends.

To compile the code, use `npm run compile`.
To watch for changes, use `npm run watch`.

Note: Do not use `tsc` or `tsc -w` as it will only compile to ES Modules, which means it will not work with CommonJS-based implementations.

---
_Source: https://npm.io/package/@aehrc/sdc-template-extract · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
