@aehrc/sdc-populate

中文文档
4.7.2发布于 2026/09/10Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 4.3万

Performs the $populate operation from the HL7 FHIR SDC (Structured Data Capture) specification: http://hl7.org/fhir/uv/sdc

原始使用文档
文档目录4 节
  1. Usage
  2. Using it in a web app
  3. Using it in a backend service
  4. Local development notes

SDC-Populate

A Typescript reference implementation of the $populate operation from the HL7 FHIR Structured Data Capture Specification designed for Form Population.

Check out the API Reference for technical specifications.

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 populateQuestionnaire(), which performs an in-app population. This means that the app is not sending data to an external server, but rather using the library to populate the questionnaire in the app itself.

const { populateSuccess, populateResult } = await populateQuestionnaire({
  questionnaire: yourQuestionnaireResource,
  fetchResourceCallback: yourFetchResourceCallbackFunction,
  fetchResourceRequestConfig: yourFetchResourceRequestConfig,
  patient: yourPatient,
});

// Pre-population is successful
if (populateSuccess && populateResult !== null) {
  const questionnaireResponse = populateResult.populatedResponse;
  
  // Do things with the pre-populated questionnaireResponse...
}

You will also need to define your own fetchResourceCallback function and fetchResourceRequestConfig. This function is responsible for fetching resources from your FHIR server. It should return a promise that resolves to the resource you want to fetch.

import { FetchResourceRequestConfig, FetchResourceCallback } from '@aehrc/sdc-populate';

const ABSOLUTE_URL_REGEX = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;

export const yourFetchResourceRequestConfig: FetchResourceRequestConfig = {
  sourceServerUrl: 'https://proxy.smartforms.io/fhir',
  authToken: string | null
};

export const yourFetchResourceCallbackFunction: FetchResourceCallback = async (
  query: string,
  requestConfig: FetchResourceRequestConfig
) => {
  let { sourceServerUrl } = requestConfig;
  const { authToken } = requestConfig;

  const headers: Record<string, string> = {
    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();
};

Available parameters for populateQuestionnaire():

ParameterDescription
questionnaireQuestionnaire to populate
fetchResourceCallbackA callback function to fetch resources from your FHIR server
fetchResourceRequestConfigAny request configuration to be passed to the fetchResourceCallback (e.g., headers, auth, etc.)
patientPatient resource as patient in context
userPractitioner resource as user in context (optional)
encounterEncounter resource as encounter in context (optional)
fetchTerminologyCallbackA callback function to fetch terminology resources (optional)
fetchTerminologyRequestConfigAny request configuration to be passed to the fetchTerminologyCallback (e.g., headers, auth, etc.) (optional)

Using it in a backend service

Using this library in a backend service requires more pre-configuration. Due to how the $populate works, you will need to provide a bunch of input parameters to the populate() function.

There is a sample implementation of how to use the populate() function in https://github.com/aehrc/smart-forms/blob/main/services/populate-express/src/index.ts.

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.

版本与发布记录

@aehrc/sdc-populate 共发布 53 个版本,首次发布于 2023/09/01;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
4.7.2latest2026/09/101 个月前
4.7.12026/07/034 个月前
4.7.02026/03/257 个月前
4.6.32026/03/117 个月前
4.6.22025/08/071.3 年前
4.6.12025/08/071.3 年前

其他发布通道:beta → 4.0.0-beta.2。

关于 @aehrc/sdc-populate 的常见问题

@aehrc/sdc-populate 是什么?

Performs the $populate operation from the HL7 FHIR SDC (Structured Data Capture) specification: http://hl7.org/fhir/uv/sdc

如何安装 @aehrc/sdc-populate?

在项目目录运行 npm install @aehrc/sdc-populate,或使用 pnpm add / yarn add / bun add。

@aehrc/sdc-populate 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aehrc/sdc-populate 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aehrc/sdc-populate 可以商用吗?

@aehrc/sdc-populate 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@aehrc/sdc-populate 的下载量有多少?

根据 npm 官方下载统计,@aehrc/sdc-populate 在 2026/10/05 – 2026/11/03 这 30 天内被下载 43,469 次,日均约 1,449 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@aehrc/sdc-populate 还在维护吗?

最新版本 4.7.2 发布于 2026/09/10(1 个月前);至今共发布 53 个版本,其中过去 12 个月发布了 4 个;首次发布于 2023/09/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。