@flatfile/adapter

中文文档
2.9.6发布于 2021/12/02MITTypeScriptESM + CJS近 30 天下载 43万

A lightweight TypeScript/JavaScript adapter for working with Flatfile's Portal

原始使用文档
文档目录7 节
  1. Using NPM
  2. Using CDN
  3. Quickstart
  4. ES6 / Babel
  5. Data hooks
  6. Themes
  7. Promise Overrides

Flatfile.com CSV Importer Adapter

Build Status Standard Version dependencies Status devDependencies Status

A simple adapter for elegantly importing data (CSV, XLS & more) via flatfile.com (Typescript, ES6, Browser)

Important note: While the below info is a basic way to get up and running, we recommend reading the developer docs → https://flatfile.com/developers/javascript/getting-started

Another note: If you are using Angular or React, we have specific packages for those. Check out our React package on GitHub and Angular package on GitHub.

License Key

In order to setup, you need to create or sign in to your flatfile.com account and obtain a license key.

Changelog

To view information about the latest releases and any minor/major changes, check out the changelog here.

Note: In version 2.8, previously available "deep-imports" (for Interfaces) have been moved to the root level of @flatfile/adapter.

Using NPM

If you don't like external dependencies, or you have a nice build system like Webpack in place. You can install and use Flatfile as an npm package.

npm i @flatfile/adapter --save

Using CDN

The latest version of the package is available via CDN so you can just drop it into your website and start using it.

https://unpkg.com/@flatfile/adapter/build/dist/index.min.js

Quickstart

Add the following code before the ending </body> tag in your html.

<script src="https://unpkg.com/@flatfile/adapter/build/dist/index.min.js"></script>

<script>
  const LICENSE_KEY = '00000000-0000-0000-0000-000000000000' // replace this with your license key

  const importer = new FlatfileImporter(LICENSE_KEY, {
    type: 'Robot',
    fields: [
      {
        label: 'Name',
        key: 'name',
        validators: [ { validate: 'unique' } ]
      },
      {
        label: 'Phone',
        key: 'phone',
        alternates: ['number', 'tel'],
        validators: [
          {
            validate: 'regex_matches',
            regex: '^\d{10}$'
          }
        ]
      },
      {
        label: 'Country',
        key: 'country',
        type: 'select',
        options: [
          { value: 'US', label: 'United States' },
          { value: 'CA', label: 'Canada' }
        ]
      }
    ]
  })

  // More info: https://flatfile.com/developers/javascript/getting-started/#the-basics
  importer.setCustomer({
    userId: '1'
  })

  importer.requestDataFromUser().then((results) => {
    importer.displayLoader('Please wait while your data is loading')

    // do something with your data
    setTimeout(() => {
      // console.log(results)

      importer.displaySuccess('You are all done!')
    }, 1000)
  })
</script>

ES6 / Babel

const LICENSE_KEY = '00000000-0000-0000-0000-000000000000' // replace this with your license key

const importer = new FlatfileImporter(LICENSE_KEY, {
  type: 'Robot',
  fields: [
    {
      label: 'Name',
      key: 'name'
    }
  ]
})

// More info: https://flatfile.com/developers/javascript/getting-started/#the-basics
importer.setCustomer({
  userId: '1'
})

document.querySelector('button').addEventListener('click', async () => {
  try{
    const results = await importer.requestDataFromUser()

    importer.displayLoader('Please wait while your data is loading')

    // do something with your data
    setTimeout(() => {
      // console.log(results)

      importer.displaySuccess('You are all done!')
    }, 1000)
  }catch(e){
    // handle a failed upload
  }
})

Data hooks

Flatfile's Data Hooks are a useful data healing element to re-format, validate and/or correct data automatically during the import without the user having to correct manually.

More information: Getting started with Data Hooks

const importer = new FlatfileImporter(LICENSE_KEY, {
  type: 'Robot',
  fields: [
    {
      label: 'Name',
      key: 'name'
    }
  ]
})

importer.setCustomer({
  userId: '1'
})

// adding a data hook that will add 'Jr.' to the name in each row
importer.registerRecordHook((row) => {
  // Example row: {name: 'John'}
  const result = {};

  if (row.name) {
    result.name = {
      value: `${row.name} Jr.`
    };
  }

  return result;
});

Themes

Theming gives you independent control over the look and feel of Flatfile Portal. With this, you can adjust both a global styles and individual components within the importer, as well as control the CSS pseudo-class :hover & :focus on buttons.

More information: Custom Themes for Flatfile Portal

const importer = new FlatfileImporter(LICENSE_KEY, {
  type: 'Robot',
  fields: [
    {
      label: 'Name',
      key: 'name'
    }
  ],
  theme: {
    global: {
      backgroundColor: '#212327',
      textColor: '#c2c3c3',
      primaryTextColor: '#c2c3c3',
      secondaryTextColor: '#c2c3c3',
      successColor: '#c2c3c3',
      warningColor: '#c2c3c3'
    },
    // other keys below
  }
})

Promise Overrides

Flatfile includes a basic native compatible Promise shim for IE support. You can override this with your preferred library by using the following global setting:

FlatfileImporter.Promise = Bluebird.Promise

版本与发布记录

@flatfile/adapter 共发布 41 个版本,首次发布于 2020/08/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.9.6latest2021/12/024 年前
2.9.52021/11/304 年前
2.9.42021/11/244 年前
2.9.32021/11/055 年前
2.9.22021/11/025 年前
2.9.12021/09/085 年前

其他发布通道:beta → 2.8.0-beta.1。

关于 @flatfile/adapter 的常见问题

@flatfile/adapter 是什么?

A lightweight TypeScript/JavaScript adapter for working with Flatfile's Portal

如何安装 @flatfile/adapter?

在项目目录运行 npm install @flatfile/adapter,或使用 pnpm add / yarn add / bun add。

@flatfile/adapter 支持 TypeScript 吗?

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

@flatfile/adapter 支持 ESM 还是 CommonJS?

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

@flatfile/adapter 需要什么版本的 Node.js?

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

@flatfile/adapter 可以商用吗?

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

@flatfile/adapter 的下载量有多少?

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

@flatfile/adapter 还在维护吗?

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