@aidbox/sdk-r4

中文文档
1.0.15发布于 2024/10/31ISCTypeScriptCommonJS近 30 天下载 3141

Default Aidbox SDK with HL7-R4 package

原始使用文档
文档目录18 节
  1. How to use
  2. API
  3. resource.list
  4. resource.get
  5. resource.create
  6. resource.update()
  7. ### resource.delete()
  8. createSubscription
  9. sendLog
  10. Bundle request
  11. Task API
  12. Definition
  13. Implementation
  14. Execution
  15. Workflow Engine
  16. Workflow Definition
  17. Workflow Implementation
  18. Workflow Execution

Aidbox JavaScript SDK FHIR-R4

How to use

Important: Before we start we have to make sure that aidbox client is configured and your access policies provide granular access to resources you're trying to reach.

import { Client } from 'aidbox-sdk';

export const aidbox = new Client('<HOST_URL>', {
  auth: {
    method: 'basic',
    credentials: {
      username: '<CLIENT_NAME>',
      password: '<CLIENT_SECRET>',
    },
  },
});

This code creates a new instance of the SDK from the aidbox-sdk package, and sets it up to connect to an Aidbox server running on <HOST_URL>. You would need to replace <CLIENT_NAME> and <CLIENT_SECRET> with the actual client ID and client secret that you configured earlier, also SDK support two types of API (Aidbox, FHIR see difference) just provide one of them as third parameter.

By using aidbox-sdk in your project, you can easily interact with an Aidbox server and perform actions like reading and writing resources, searching for resources, and more.

Then you can use aidboxClient in wherever you want

import { aidbox } from '../aidbox-client';

async function getPatients() {
  return aidbox.resource.list('Patient');
}

API

resource.list

list method accepts the name of the resource and is the basis for the subsequent complication of the request

where

Method where add additional parameters for searching

For example, you want to find all patients with name John, so

resource.list('Patient').where('name', 'John');

Or, you want to find all patients with name John or Steve

resource.list('Patient').where('name', ['John', 'Steve']);

Also, method where support prefixes for numbers and date, just pass it as third parameter

resource.list('Patient').where('birthdate', '2014-06-30', 'gt');

Sort

Method sort add additional parameters for sorting

For example, you want to display the oldest patients

resource.list('Patient').sort('birthdate', 'asc');

And also, you want to sort this data by patients name

resource.list('Patient').sort('birthdate', 'asc').sort('name', 'asc');

Count

Method count used for make limit the number of resource returned by request

resource.list('Patient').count(10);

Summary

To request only a portion of the resources from a server, you can use the summary function. This function allows you to specify the type of summary you want to receive, such as true, false, text, data, or count.

By default, when you make a request to a server, it will return all resource.

The summary function accepts several types of summaries, each of which provides a different level of detail about the data. For example:

  • true - limited subset of elements from the resource

  • false - all parts of the resource

  • text - only the text, id, meta, and top-level mandatory elements

  • data - resources without the text element

  • count - count of the matching resources, without returning the actual matches

    summary("data")

Elements

If you need to retrieve specific elements of a resource from a server, you can use the elements function. This function allows you to specify which parts of the resource you are interested in, and can help to reduce the amount of data returned by the server.

By using the elements function, you can customize your requests to retrieve only the data that you need, rather than requesting the entire dataset. This can be particularly useful when dealing with large datasets or when you only need a small subset of the available data.

To use the elements function, simply pass in the elements you want to retrieve as arguments. For example, if you only need the name and address fields from a resource, you can make a request using the following syntax:

resource.list('Patient').elements(['name', 'address']);

This request will return only the name and address fields for each resource, rather than the entire dataset. By reducing the amount of data returned, you can help to streamline your requests and improve the performance of your application.

resource.get

The get function is a tool for retrieving a single resource by its ID.

To use the getResource function, you must pass in the resource type and ID as arguments. For example, if you want to retrieve a patient resource with the ID some-id, you can make a request using the following syntax:

resource.get('Patient', 'id');

resource.create

The createResource function is used to create a new resource.

The first argument is a resource name, the second one is body of resource

resource.create('Patient', { active: true });

resource.update()

The update function is used to update a specific resource identified by its id with a partial set of data provided in the third parameter. This function allows for partial updates of a resource without having to send the entire resource object.

The first parameter specifies the name of the resource to be updated. The second parameter identifies the specific resource to be updated.

resource.update('Patient', 'c58f29eb-f28d-67c1-0400-9af3aba3d58c', {
  active: false,
});

### resource.delete()

The deleteResource function is used to delete a specific resource identified by its id.

resource.delete('Patient', 'c58f29eb-f28d-67c1-0400-9af3aba3d58c');

createSubscription

Aidbox subscription is a way to subscribe and get notifications about updating resources on the server. See our subscription sample for more details.

await client.subsSubscription.create({
  id: 'patient-created',
  status: 'active',
  trigger: { Patient: { event: ['create'] } },
  channel: { endpoint: 'https://aidbox/patient-created' },
});

sendLog

Aidbox has the ability to extend its logs. There is an endpoint that accepts logs with the defined structure from your application. These logs are ingested into the elastic log.

await client.sendLog({
    type: "ui",
    message: {event: "YOUR_EVENT", id: "id", ...},
    v: "APP_VERSION",
    fx: "fetchUsers"
})

Bundle request

SDK has helpers to prepare data for bundle request.

client.transformToBundle(
  [
    {
      resourceType: 'Patient',
      name: [
        {
          given: [''],
          family: '',
        },
      ],
    },
  ],
  'POST',
);

Bundle requests could be a transaction or batch type. SDK uses the "transaction" type by default but you can change it by providing it in the second parameter.

const data = ArrayOfPatients.map(client.bundleEntryPost);

await client.bundleRequest(data, 'batch');

Task API

Queues are a valuable tool for achieving reliable, asynchronous, scalable, and retry-safe code execution. By using queues, we can ensure that tasks or messages are processed reliably, even in the face of failures or system disruptions. The asynchronous nature of queues allows tasks to be processed independently, enabling parallelism and reducing wait times.

import { Engine } from 'aidbox-javascript-sdk';

const client = new Engine('http://localhost:8888', {
  username: 'test',
  password: 'secret',
});

Definition

First of all we have to create task's schema with settings that apply restrictions on input and output arguments for entity like this one:

 SendMessage
{:zen/tags #{awf.task/definition zen/schema}
 :type     zen/map
 :keys     {:params {:type    zen/map
                     :require #{:phone :message}
                     :keys    {:phone   {:type zen/string}
                               :message {:type zen/string}}}
            :result {:type    zen/map
                     :require #{:status}
                     :keys    {:status {:type zen/string}}}}}

More information about task definition
Notice: we have to regenerate SDK package each time we made changes into configuration project

Implementation

Next step is creating business logic that will be considered as a worker, the worker will be handling each task, we can

client.task.implement(
  'SendMessage',
  async ({ params, status }) => {
    const { message, phone } = params;

    try {
      await fetch('https://message-sending-server.com/', {
        method: 'POST',
        body: JSON.stringify({ message, phone }),
      });

      return { status: 'success' };
    } catch (error) {
      return { status: 'failure' };
    }
  },
  { batchSize: 5 },
);

Execution

The way to execute a single task with unique context

await client.task.execute('SendMessage', {
  phone: '+1234567890',
  message: 'Hi!',
});

Workflow Engine

Aidbox provides Workflow Engine module, so you're able to define your own sequence of async tasks on top of Task API. In many business processes, certain tasks depend on the completion of other tasks. Defining task dependencies through workflow implementation allows the developer to control the order in which tasks are executed.

import { Engine } from 'aidbox-javascript-sdk';

const client = new Engine('http://localhost:8888', {
  username: 'test',
  password: 'secret',
});

Workflow Definition

 CheckOutWorkflow
{:zen/tags #{awf.workflow/definition zen/schema}
 :type     zen/map
 :pool     decision-pool
 :keys     {:params {:type    zen/map
                     :require #{:clientId}
                     :keys    {:clientId {:type zen/string}}}
            :result {:type zen/map
                     :keys {:messageId {:type zen/string}}}
            :error  {:type zen/map
                     :keys {:message {:type zen/string}}}}}

More information about workflow definition
Notice: we have to regenerate SDK package each time we made changes into configuration project

Workflow Implementation

await client.workflow.implement(
  'CheckOutWorkflow',
  async ({ params }, { execute, complete, fail }) => {
    if (params.event === 'awf.workflow.event/workflow-init') {
      const response = await fetch('https://server.com/get-client');
      const { phone } = response.json();
      return [
        execute({
          definition: 'SendMessage',
          params: { phone, message: 'Hi!' },
        }),
      ];
    }

    try {
      const response = await fetch('https://workflow-state.com');
      const data = response.json();

      if (
        params.event === 'awf.workflow.event/task-completed' &&
        data.step === 1
      ) {
        return [execute({ definition: 'UpdateInformation', params: {} })];
      }

      if (
        params.event === 'awf.workflow.event/task-completed' &&
        data.step === 2
      ) {
        return [complete({})];
      }
    } catch (error) {
      return [fail({ error })];
    }

    return [];
  },
);

Workflow Execution

await client.workflow.execute('CheckOutWorkflow', { clientId: '' });

版本与发布记录

@aidbox/sdk-r4 共发布 16 个版本,首次发布于 2023/06/16;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.15latest2024/10/312 年前
1.0.142024/10/232 年前
1.0.132024/05/222 年前
1.0.122024/05/102 年前
1.0.112024/02/072 年前
1.0.102024/02/072 年前

关于 @aidbox/sdk-r4 的常见问题

@aidbox/sdk-r4 是什么?

Default Aidbox SDK with HL7-R4 package

如何安装 @aidbox/sdk-r4?

在项目目录运行 npm install @aidbox/sdk-r4,或使用 pnpm add / yarn add / bun add。

@aidbox/sdk-r4 支持 TypeScript 吗?

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

@aidbox/sdk-r4 支持 ESM 还是 CommonJS?

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

@aidbox/sdk-r4 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@aidbox/sdk-r4 可以商用吗?

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

@aidbox/sdk-r4 的下载量有多少?

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

@aidbox/sdk-r4 还在维护吗?

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