@duffel/components

中文文档
3.17.0发布于 2026/06/24MITTypeScriptCommonJS近 30 天下载 24.7万

Component library to build your travel product with Duffel.

原始使用文档
文档目录8 节
  1. Getting started
  2. Integrating React component
  3. Integrating HTML custom element
  4. Integrating custom element without node
  5. FAQ
  6. Are there integration guides?
  7. What components are available through npm?
  8. What components are available through the CDN?

@duffel/components

This package is a component library to help you build your travel product using the Duffel API.

Getting started

There are 3 different ways to integrate the components into your website. This will depend on which technology you are using. We'll use the ancillaries component as an example, but the same steps will apply for other components.

Integrating React component

  1. Install the package:

    yarn add @duffel/components
    # -- or --
    npm i @duffel/components
    
  2. Import the component from @duffel/components

    import { DuffelAncillaries } from "@duffel/components";
    
  3. Render the component with the desired props

    <DuffelAncillaries
      offer={offerFromYourBackend} // The offer object from your backend
      seat_maps={seatMapsFromYourBackend}
      services={["bags", "seats"]}
      passengers={[...]}
      onPayloadReady={console.log}
    />
    

Integrating HTML custom element

If you are not using React but still in a node environment, you can:

  1. Install the package:

    yarn add @duffel/components
    # -- or --
    npm i @duffel/components
    
  2. Import the component render function and event listeners from @duffel/components/custom-elements

    import {
      renderDuffelAncillariesCustomElement,
      onDuffelAncillariesPayloadReady,
    } from "@duffel/components/custom-elements";
    
  3. Include the custom element in your HTML

    <duffel-ancillaries></duffel-ancillaries>
    
  4. Call the render function with the right properties to render the custom element:

    renderDuffelAncillariesCustomElement({
      offer: offerFromYourBackend,
      seat_maps: seatMapsFromYourBackend,
      services: ["bags", "seats"],
      passengers: [...],
    });
    
  5. Set up listeners for events triggered by the component:

    onDuffelAncillariesPayloadReady((data, metadata) => {
      console.table(data);
      console.table(metadata);
    });
    

Integrating custom element without node

If you are not in a node environment and can't rely on npm to install the package, we make it available through our CDN. Here's how to integrate it:

  1. Include a script tag

    <!--
      Replace VERSION with the desired version.
      You can find them all on https://www.npmjs.com/package/@duffel/components?activeTab=versions
    
      Replace COMPONENT with the desired component you'd like to use.
      You can find the components available in the `./cdn-dist` directory after running `yarn build-and-publish --dry-run`
    
      For example, for the duffel ancillaries component on version 3.3.1, use:
      https://assets.duffel.com/components/3.3.1/duffel-ancillaries.js
    -->
    
    <script src="https://assets.duffel.com/components/VERSION/COMPONENT.js"></script>
    
  2. Include the custom element tag in your HTML:

    <duffel-ancillaries></duffel-ancillaries>
    
  3. Render the component with the required data. You can safely call this function as many times as you want, e.g., when your passenger data changes.

    const duffelAncillariesElement = document.querySelector("duffel-ancillaries");
    
    duffelAncillariesElement.render({
      offer: offerFromYourBackend,
      seat_maps: seatMapsFromYourBackend,
      services: ["bags", "seats"],
      passengers: [...],
    });
    
  4. Listen to the 'onPayloadReady' event on the component. event.detail.data contains the payload you need to send to Duffel's API to create an order.

    const duffelAncillariesElement =
      document.querySelector("duffel-ancillaries");
    
    duffelAncillariesElement.addEventListener("onPayloadReady", (event) =>
      console.log("onPayloadReady\n", event.detail),
    );
    

FAQ

Are there integration guides?

More guides are coming soon.

The examples folder is a great way to get started quickly and see fully functioning examples for every component.

What components are available through npm?

The list of React components can be found in src/index.ts. If you are using custom elements, you can find all render functions and event listeners in src/custom-elements.ts.

What components are available through the CDN?

Please check entryPoints in config/esbuild.base.config.js. It lists all the components we'll build and upload to the CDN.

版本与发布记录

@duffel/components 共发布 269 个版本,首次发布于 2021/07/22;过去 12 个月发布了 11 个版本。

版本发布日期距今
3.17.0latest2026/06/244 个月前
3.16.12026/05/086 个月前
3.16.02026/04/286 个月前
3.15.02026/04/276 个月前
3.14.02026/04/206 个月前
3.13.2-canary.0canary2026/02/238 个月前

其他发布通道:beta → 1.3.1-beta.0,igorp1-prototype → 3.1.3--prototype.12,prototype → 3.1.3--prototype.14,canary → 3.13.2-canary.0。

关于 @duffel/components 的常见问题

@duffel/components 是什么?

Component library to build your travel product with Duffel.

如何安装 @duffel/components?

在项目目录运行 npm install @duffel/components,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-dom(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@duffel/components 支持 TypeScript 吗?

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

@duffel/components 支持 ESM 还是 CommonJS?

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

@duffel/components 需要什么版本的 Node.js?

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

@duffel/components 可以商用吗?

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

@duffel/components 的下载量有多少?

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

@duffel/components 还在维护吗?

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