@asymmetrik/fhir-response-util

中文文档
1.2.4发布于 2020/09/29MITCommonJS零依赖近 30 天下载 2493

Utility for handling the headers, content, and status types for a response.

原始使用文档
文档目录15 节
  1. Install
  2. Usage
  3. read
  4. readOne
  5. create
  6. update
  7. remove
  8. history
  9. Arguments
  10. read
  11. readOne
  12. create
  13. update
  14. remove
  15. history

FHIR Response Utility

Utility for handling the headers, content, and status types for a response

Install

yarn add @asymmetrik/fhir-response-util

Usage

The response utility exposes the following methods which can be used to for different scenarios and different types of responses. They all make certain assumptions about the arguments they receive which are documented below in the arguments section.

This is designed to be used in node-fhir-server-core and similar FHIR servers. There is some pseudocode with each method's description to give an idea of how this might work. If you read the source code for resource controllers in node-fhir-server-core, you can see how we are using this internally.

read

Used when you are returning a Bundle of resources

This will set the correct Content-Type and status code while sending the json back to the client.

const handler = require('@asymmetrik/fhir-response-util');

// Some controller
function getPatientsController(req, res, next) {
  // The user may have some service to fetch results
  service
    .getPatients(req)
    .then((bundle) => {
      // Assuming bundle contains a bunch of patients
      handler.read(req, res, bundle);
    })
    .catch((err) => {
      next(err);
    });
}

readOne

Used when you are returning a single resource of any type.

This will set the correct Content-Type and status code while sending the json back to the client.

const handler = require('@asymmetrik/fhir-response-util');

// Some controller
function getPatientController(req, res, next) {
  // The user may have some service to fetch results
  service
    .getPatient(req)
    .then((patient) => {
      handler.readOne(req, res, patient);
    })
    .catch((err) => {
      next(err);
    });
}

create

Used when you are creating a single resource of any type.

This will set the correct Content-Location, Location, and ETag headers. As well as set the correct status code.

const handler = require('@asymmetrik/fhir-response-util');

// Some controller
function createPatientController(req, res, next) {
  // The user may have some service to fetch results
  service
    .create(req)
    .then((patient) => {
      let json = { id: patient.id, resource_version: patient.meta.versionId };
      let options = { type: 'Patient' };

      handler.create(req, res, json, options);
    })
    .catch((err) => {
      next(err);
    });
}

update

Used when you are updating a single resource of any type

This will set the correct Content-Location, Last-Modified, Location, and ETag headers. As well as set the correct Content-Type and status code.

const handler = require('@asymmetrik/fhir-response-util');

// Some controller
function updatePatientController(req, res, next) {
  // The user may have some service to fetch results
  service
    .update(req)
    .then((patient) => {
      let json = { id: patient.id, resource_version: patient.meta.versionId, created: false };
      let options = { type: 'Patient' };

      handler.update(req, res, json, options);
    })
    .catch((err) => {
      next(err);
    });
}

remove

Used when you are deleting a single resource of any type

This will set the correct ETag header and status code.

const handler = require('@asymmetrik/fhir-response-util');

// Some controller
function removePatientController(req, res, next) {
  // The user may have some service to fetch results
  service
    .remove(req)
    .then((id) => {
      let json = { deleted: id };

      handler.remove(req, res, json);
    })
    .catch((err) => {
      next(err);
    });
}

history

Used when you are querying the history of a resource of any type

This will set the correct Content-Type and status code while sending the json back to the client.

const handler = require('@asymmetrik/fhir-response-util');

// Some controller
function getPatientHistoryController(req, res, next) {
  // The user may have some service to fetch results
  service
    .history(req)
    .then((bundle) => {
      handler.history(req, res, bundle);
    })
    .catch((err) => {
      next(err);
    });
}

Arguments

req and res that you see in each method are from express. res is just a normal response, but request must have a base_version parameter(req.params.base_version). Specifically, it should be one of the valid versions we support in our server, which are '1_0_2', '3_0_1', and '4_0_0'. If you would like us to add more versions for this, just let us know, we use this to determine the correct content-type as well as the url of the resource for the response type and response headers.

read

handler.read(req, res, json);

req

Express request.

Type: Express.req
Required: true

res

Express response.

Type: Express.req
Required: true

json

Bundle FHIR resource.

Type: Object
Required: true

readOne

handler.readOne(req, res, json);

req

Express request.

Type: Express.req
Required: true

res

Express response.

Type: Express.req
Required: true

json

Individual FHIR resource.

Type: Object
Required: true

create

handler.create(req, res, json, options);

req

Express request.

Type: Express.req
Required: true

res

Express response.

Type: Express.req
Required: true

json

Properties from the results. Must have an id

Type: Object
Required: true

json.id

ID of the FHIR resource that was created.

Type: String
Required: true

json.resource_version

If we are using history, which version of the resource did we create.

Type: String
Required: false

options

Additional options needed to set the correct headers in the response.

Type: Object
Required: true

options.type

This helps us set the content-location and location headers. For example, handler.create(req, res, json, { type: 'Patient' }) would allow us to set a header like this, res.set('Location', '4_0_0/Patient/patient-identifier').

Type: 'String'
Required: 'true'

update

handler.update(req, res, json, options);

req

Express request.

Type: Express.req
Required: true

res

Express response.

Type: Express.req
Required: true

json

Properties from the results. Must have an id

Type: Object
Required: true

json.id

ID of the FHIR resource that was created.

Type: String
Required: true

json.resource_version

If we are using history, which version of the resource did we create.

Type: String
Required: false

json.created

Did we create a resource or not. If we created a resource, we need to know so that we can set a res.status of 201, otherwise, we will default to a status of 200.

Type: Boolean
Required: false

options

Additional options needed to set the correct headers in the response.

Type: Object
Required: true

options.type

This helps us set the content-location and location headers. For example, handler.create(req, res, json, { type: 'Patient' }) would allow us to set a header like this, res.set('Location', '4_0_0/Patient/patient-identifier').

Type: 'String'
Required: 'true'

remove

handler.remove(req, res, json);

req

Express request.

Type: Express.req
Required: true

res

Express response.

Type: Express.req
Required: true

json

JSON for the response. Not required but contains the optional deleted property.

Type: Object
Required: false

json.deleted

The deleted property should be the id of the element deleted. For example, handler.remove(req, res, { deleted: 'qwerty-123' }). This allows us to add the identifier as a ETag in the response header.

Type: Object
Required: false

history

handler.history(req, res, json);

req

Express request.

Type: Express.req
Required: true

res

Express response.

Type: Express.req
Required: true

json

Bundle FHIR resource.

Type: Object
Required: true

版本与发布记录

@asymmetrik/fhir-response-util 共发布 5 个版本,首次发布于 2019/02/13;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.2.4latest2020/09/296 年前
1.2.32020/09/236 年前
1.2.22020/09/146 年前
1.2.02020/09/026 年前
1.0.02019/02/137 年前

关于 @asymmetrik/fhir-response-util 的常见问题

@asymmetrik/fhir-response-util 是什么?

Utility for handling the headers, content, and status types for a response.

如何安装 @asymmetrik/fhir-response-util?

在项目目录运行 npm install @asymmetrik/fhir-response-util,或使用 pnpm add / yarn add / bun add。

@asymmetrik/fhir-response-util 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/asymmetrik__fhir-response-util 是否存在。

@asymmetrik/fhir-response-util 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@asymmetrik/fhir-response-util 可以商用吗?

@asymmetrik/fhir-response-util 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@asymmetrik/fhir-response-util 的下载量有多少?

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

@asymmetrik/fhir-response-util 还在维护吗?

最新版本 1.2.4 发布于 2020/09/29(6 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/02/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。