@dsptch-work/ api-客户端
一个生成,打入的TypeScript客户端,用于DSPTCH 公共REST API. 换成
手卷式抓取包,每个资源一个类,方法名称匹配
API的软件 operationIds, 完全输入请求/ 响应形状, 以及捆绑的获取客户端
连接基础URL和 X-Api-Key 证明给你.
状态 : 预1.0,向npm公共登记处发布. API还在追上 到 DSPTCH 网络应用程序,所以 释放留在
0.x范围并可能运输破损变化 介于小版本之间0.5.x→0.6.0)——贴上你测试过的版本.
安装
npm install @dsptch-work/api-client # or: pnpm add @dsptch-work/api-client · yarn add @dsptch-work/api-client
这是一个公共软件包:安装不需要注册配置和认证. 提供经认证的API电话确实需要DSPTCH API密钥——参见 使用量。 。 。 .
使用量
import { Jobs } from '@dsptch-work/api-client';
import { client } from '@dsptch-work/api-client/client';
// Configure auth once. The returned string is sent as the `X-Api-Key` header on every
// authenticated request. A function is used so the key can be resolved lazily — e.g.
// read from `process.env` in a Node script, or from a secret store.
client.setConfig({ auth: () => apiKey });
// One class per tag; each method is named by the operation's operationId.
const { data, error } = await Jobs.listJobs({
query: { 'filter[project_id]': projectId },
});
if (error) {
throw new Error(`Failed to list jobs: ${JSON.stringify(error)}`);
}
console.log(data);
基准 URL (https://api.dsptch.app)从 spec's被烤入 servers 输入,所以
无法配置用于生产。 覆盖每个环境
client.setConfig({ baseUrl: '…' })。 。 。 .
上面一个可运行的、经过类型检查的版本生活在
examples/basic-usage.ts。 。 。 .
插入列表终点
列出端点为光标:每个答复带有一个 meta.page.next_cursor,则a
null 光标是最后一页。 而不是用手进行循环,将列表方法交给
paginate 并在每一页上标出每个项目:
import { Jobs, paginate, collect } from '@dsptch-work/api-client';
for await (const job of paginate(Jobs.listJobs, { query: { 'filter[project_id]': projectId } })) {
process(job); // `job` is typed as the Jobs list element
}
// Or, when every page fits in memory, drain it into an array:
const jobs = await collect(paginate(Jobs.listJobs));
从方法推断出项目类型,选项保持方法自己的形状——
当终点需要路径 id 时需要
(单位:千美元)paginate(ApprenticeshipApprentices.listApprentices, { path: { program_id } })),可选
否则. paginate 发送每页 next_cursor 作为下一个 page[cursor] 并投掷,如果
结尾返回出错.
出口的东西
| 导入路径 | 目录 |
|---|---|
@dsptch-work/api-client | 资源类别Jobs, (中文(简体) ), TimeCards),各种类型,以及 paginate 编号 : collect 帮助者 |
@dsptch-work/api-client/client | 配置 client 实例——将其用于 setConfig |
@dsptch-work/api-client/zod | Zod schemas 镜像光谱, 对于想要验证的呼叫者 |
维护客户本身(从光谱中恢复、建筑、释放)的文件记录
用于寄存器中的贡献者: DEVELOPMENT.md 财务报告和审定财务报表 RELEASING.md。 。 。 .