@kevisual/noco
Version:
一个轻量级的 NocoDB API SDK,支持表记录操作和 Base 管理功能。
139 lines (134 loc) • 3.19 kB
text/typescript
import { Query, ResponseList } from '../api.ts';
export type WebhookOptions = {
query: Query;
};
export class Webhook {
private query?: Query;
constructor(options?: WebhookOptions) {
this.query = options?.query;
}
listTableWebhooks(tableId: string): Promise<ResponseList<WebhookItem>> {
return this.query.makeRequest(
`/api/v2/meta/tables/${tableId}/hooks`,
{
method: 'GET',
}
);
}
createTableWebhook(
tableId: string,
data: WebhookItemCore
): Promise<any> {
return this.query.makeRequest(
`/api/v2/meta/tables/${tableId}/hooks`,
{
method: 'POST',
data: {
version: 'v3',
event: 'manual',
...data,
},
}
);
}
updateTableWebhook(
hookId: string,
data: WebhookItemCore
): Promise<any> {
return this.query.makeRequest(
`/api/v2/meta/hooks/${hookId}`,
{
method: 'PUT',
data,
}
);
}
deleteTableWebhook(hookId: string): Promise<any> {
return this.query.makeRequest(
`/api/v2/meta/hooks/${hookId}`,
{
method: 'DELETE',
}
);
}
}
const operations = ['insert', 'update', 'delete', 'bulk_insert', 'bulk_update', 'bulk_delete'];
type WebhookOperation = typeof operations[number];
/**
* Webhook 项目类型定义
*/
export type WebhookItem = {
id: string;
source_id: string;
base_id: string;
fk_model_id: string;
title: string;
description: string;
env: string; // 'all'
type: 'manual' | null;
operation: WebhookOperation[];
async: boolean;
payload: boolean;
url: string;
headers: Record<string, string>;
condition: boolean;
notification: string;
retries: number;
retry_interval: number;
timeout: number;
active: boolean;
created_at: string;
updated_at: string;
version: string; // 'v3'
trigger_field: boolean;
trigger_fields: any[];
};
export type WebhookItemCore = {
id?: string;
title: string;
description?: string | null;
operation: WebhookOperation | WebhookOperation[];
/**
* Webhook 触发的 json的
* 不能为空对象
* @example '{"type":"URL","include_user":false,"payload":{"method":"POST","body":"{{ json event }}","headers":[{"enabled":false,"name":"","value":""}],"parameters":[{"enabled":false,"name":"tableId","value":"mecdgojq151iwk9"}],"path":"https://kevision.xiongxiao.me/api/router","auth":""},"trigger_form":false}'
*/
notification: string | Record<string, any>;
/**
* 内置必填
*/
event: string; // after | 'manual',
/**
* 内置必填
*/
version?: string; // 'v3'
/**
* 是否启用
*/
active?: boolean;
}
export type WebhookNotification = {
type: 'URL' | 'Email' | 'DingTalk' | 'WeCom' | 'FeiShu';
include_user: boolean;
payload: {
method: 'POST' | 'GET' | 'PUT' | 'DELETE' | 'PATCH';
/**
* 请求体
* {{ json event }}
*/
body: string;
headers: Array<{
enabled: boolean;
name: string;
value: string;
}>;
parameters: Array<{
enabled: boolean;
name: string;
value: string;
}>;
path: string;
auth: string;
};
trigger_form: boolean;
}