平面文件节点 API 库
Flatfile Node.js库提供从JavaScript/TypeScript获取Flatfile API的方便访问.
文档
API 备有参考文件 这里。 。 。 .
安装
npm install --save @flatfile/api
# or
yarn add @flatfile/api
使用量
import { FlatfileClient } from '@flatfile/api';
async function main() {
const flatfile = new FlatfileClient({
// This is usually the environment specific "Secret Key" that can be found
// on the Getting Started page in the Flatfile dashboard.
token: 'YOUR_API_KEY',
});
const workbook = await flatfile.workbooks.create({
name: 'SDK Example',
sheets: [
{
name: 'Contacts',
slug: 'contacts',
fields: [
{
key: 'firstName',
type: 'string',
label: 'First Name',
},
{
key: 'lastName',
type: 'string',
label: 'Last Name',
},
{
key: 'email',
type: 'string',
label: 'Email',
},
],
actions: [
{
slug: 'submitAction',
label: 'Submit',
type: 'string',
description: 'Submit data to webhook.site',
primary: true,
},
],
},
],
});
console.log('Created workbook with id:', workbook.data.id);
}
处理错误
当 API 返回一个非成功状态代码( 4xx 或 5xx 响应) 时, 一个子类 : 平面文件错误 将扔出 :
try {
await client.agents.get("environment-id", "agent-id");
} catch (err) {
if (err instanceof FlatfileError) {
console.log(err.statusCode); // 400
console.log(err.message); // "BadRequestError"
console.log(err.body); // list of errors
}
}
错误代码如下:
| 身份代码 | 错误类型 |
|---|---|
| 400个 | BadRequestError |
| 404 (韩语) | NotFoundError |
处理事件
平面文件平台发出不同事件(例如. user:added, (中文(简体) ), webhook:removed) (中文(简体) ). SDK使用过 歧视工会 这样就容易处理具体事件.
const event = eventResponse.data;
if (event.type === 'job:ready') {
console.log(event.payload.operation); // FILE
console.log(event.payload.type); // PIPELINE
} else if (event.type === 'records:created') {
console.log(event.payload.count) // 100
}
字段
SDK使用过 歧视工会 这使得它很容易 透视不同的领域.
for (const field of sheet.config.fields) {
if (field.type === 'boolean') {
console.log(field.config?.allowIndeterminate); // false
} else if (field.type === "number") {
console.log(field.config?.decimalPlaces); // 2
} else if (field.type === "enum") {
console.log(field.config.allowCustom); // true
}
}
捐款
虽然我们重视对SDK的开源贡献,但这个库是程序生成的. 直接添加到这个库的代码必须移到我们的代号上,否则它们会在下一个生成的版本被覆盖. 随便打开公关作为概念的证明 但知道我们无法将公关合并 我们建议 打开一个问题 先和我们讨论!
另一方面,对 " README " 的贡献总是非常受欢迎的!
许可证
这个项目根据麻省理工学院许可证颁发——见 自由主义 详细文件.