@kne/fastify-webhook 0.1.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -10
- package/index.js +55 -46
- package/libs/constants.js +81 -0
- package/libs/controllers/_helper.js +16 -0
- package/libs/controllers/application.js +73 -0
- package/libs/controllers/attempt.js +49 -0
- package/libs/controllers/domain.js +40 -0
- package/libs/controllers/endpoint.js +90 -0
- package/libs/controllers/eventType.js +84 -0
- package/libs/controllers/ingest.js +83 -0
- package/libs/controllers/mcp.js +41 -0
- package/libs/controllers/message.js +56 -0
- package/libs/controllers/misc.js +81 -0
- package/libs/controllers/operational.js +26 -0
- package/libs/controllers/play.js +33 -0
- package/libs/controllers/polling.js +48 -0
- package/libs/controllers/portal.js +32 -0
- package/libs/controllers/public.js +47 -0
- package/libs/controllers/stream.js +74 -0
- package/libs/helpers/context.js +50 -0
- package/libs/models/appPortalSession.js +18 -0
- package/libs/models/application.js +21 -0
- package/libs/models/attempt.js +36 -0
- package/libs/models/channel.js +12 -0
- package/libs/models/customDomain.js +15 -0
- package/libs/models/endpoint.js +37 -0
- package/libs/models/environment.js +13 -0
- package/libs/models/eventType.js +22 -0
- package/libs/models/idempotency.js +14 -0
- package/libs/models/inbox.js +17 -0
- package/libs/models/ingestSource.js +17 -0
- package/libs/models/ingestToken.js +15 -0
- package/libs/models/message.js +27 -0
- package/libs/models/operationalSubscription.js +13 -0
- package/libs/models/pollingConsumer.js +15 -0
- package/libs/models/pollingEndpoint.js +13 -0
- package/libs/models/sink.js +20 -0
- package/libs/models/stream.js +13 -0
- package/libs/models/streamEvent.js +14 -0
- package/libs/models/transformation.js +13 -0
- package/libs/schema/response.js +120 -0
- package/libs/services/appPortal.js +87 -0
- package/libs/services/application.js +94 -0
- package/libs/services/attempt.js +151 -0
- package/libs/services/customDomain.js +87 -0
- package/libs/services/delivery.js +297 -0
- package/libs/services/endpoint.js +236 -0
- package/libs/services/eventType.js +173 -0
- package/libs/services/fifo.js +34 -0
- package/libs/services/inbound.js +108 -0
- package/libs/services/ingest.js +294 -0
- package/libs/services/mcp.js +60 -0
- package/libs/services/message.js +200 -0
- package/libs/services/operational.js +70 -0
- package/libs/services/play.js +87 -0
- package/libs/services/polling.js +162 -0
- package/libs/services/retention.js +66 -0
- package/libs/services/scheduler.js +50 -0
- package/libs/services/sink.js +67 -0
- package/libs/services/stream.js +138 -0
- package/libs/services/throttle.js +63 -0
- package/libs/services/transform.js +102 -0
- package/libs/sinks/index.js +105 -0
- package/libs/utils/common.js +184 -0
- package/libs/utils/index.js +10 -0
- package/libs/utils/outbound.js +114 -0
- package/libs/utils/signature.js +160 -0
- package/package.json +21 -11
- package/libs/controllers/main.js +0 -168
- package/libs/models/client.js +0 -58
- package/libs/models/example._js +0 -18
- package/libs/models/invocation.js +0 -40
- package/libs/services/example._js +0 -10
- package/libs/services/main.js +0 -172
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
messageId: definePrimaryType('messageId', { allowNull: false, comment: '消息 ID' }),
|
|
5
|
+
endpointId: definePrimaryType('endpointId', { allowNull: false, comment: '端点 ID' }),
|
|
6
|
+
webhookId: { type: DataTypes.STRING, comment: '投递使用的 webhook-id(重放时沿用)' },
|
|
7
|
+
requestHeaders: { type: DataTypes.JSON, defaultValue: {}, comment: '请求头' },
|
|
8
|
+
requestBody: { type: DataTypes.TEXT, comment: '请求体(原始)' },
|
|
9
|
+
transformedBody: { type: DataTypes.TEXT, comment: '变换后请求体' },
|
|
10
|
+
responseStatus: { type: DataTypes.INTEGER, comment: '响应状态码' },
|
|
11
|
+
responseHeaders: { type: DataTypes.JSON, defaultValue: {}, comment: '响应头' },
|
|
12
|
+
responseBody: { type: DataTypes.TEXT, comment: '响应内容(截断)' },
|
|
13
|
+
error: { type: DataTypes.TEXT, comment: '错误信息' },
|
|
14
|
+
status: {
|
|
15
|
+
type: DataTypes.ENUM('pending', 'sending', 'success', 'failed', 'retrying'),
|
|
16
|
+
defaultValue: 'pending',
|
|
17
|
+
comment: '尝试状态;sending 表示已被某实例抢占执行中'
|
|
18
|
+
},
|
|
19
|
+
startAt: { type: DataTypes.DATE, comment: '开始时间' },
|
|
20
|
+
endAt: { type: DataTypes.DATE, comment: '结束时间' },
|
|
21
|
+
duration: { type: DataTypes.INTEGER, comment: '耗时毫秒' },
|
|
22
|
+
retryCount: { type: DataTypes.INTEGER, defaultValue: 0, comment: '第几次重试(0 为首次)' },
|
|
23
|
+
nextAttemptAt: { type: DataTypes.DATE, comment: '下次重试时间' },
|
|
24
|
+
canceled: { type: DataTypes.BOOLEAN, defaultValue: false, comment: '是否被变换脚本取消' },
|
|
25
|
+
metadata: { type: DataTypes.JSON, defaultValue: {}, comment: '扩展元数据' }
|
|
26
|
+
},
|
|
27
|
+
options: {
|
|
28
|
+
comment: 'Webhook 投递尝试记录',
|
|
29
|
+
indexes: [
|
|
30
|
+
{ fields: ['tenant_id', 'message_id', 'endpoint_id'] },
|
|
31
|
+
{ fields: ['tenant_id', 'status'] },
|
|
32
|
+
{ fields: ['next_attempt_at', 'status'] },
|
|
33
|
+
{ fields: ['endpoint_id', 'created_at'] }
|
|
34
|
+
]
|
|
35
|
+
}
|
|
36
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
applicationId: definePrimaryType('applicationId', { comment: '所属应用 ID(空表示租户级)' }),
|
|
5
|
+
name: { type: DataTypes.STRING, allowNull: false, comment: 'channel 名称(大小写敏感)' },
|
|
6
|
+
description: { type: DataTypes.STRING, comment: '描述' }
|
|
7
|
+
},
|
|
8
|
+
options: {
|
|
9
|
+
comment: 'Webhook Channel',
|
|
10
|
+
indexes: [{ fields: ['tenant_id', 'name'] }, { fields: ['application_id'] }]
|
|
11
|
+
}
|
|
12
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
service: { type: DataTypes.ENUM('ingest', 'portal', 'catalog', 'polling', 'mcp'), allowNull: false, comment: '服务类型' },
|
|
5
|
+
domain: { type: DataTypes.STRING, allowNull: false, comment: '自定义域名' },
|
|
6
|
+
region: { type: DataTypes.STRING, comment: '区域(区域型服务按此路由)' },
|
|
7
|
+
cnameTarget: { type: DataTypes.STRING, comment: 'CNAME 目标' },
|
|
8
|
+
status: { type: DataTypes.ENUM('pending', 'verified', 'failed'), defaultValue: 'pending', comment: '验证状态' },
|
|
9
|
+
tlsStatus: { type: DataTypes.ENUM('pending', 'issued', 'failed'), defaultValue: 'pending', comment: 'TLS 证书状态' }
|
|
10
|
+
},
|
|
11
|
+
options: {
|
|
12
|
+
comment: 'Webhook 自定义域名',
|
|
13
|
+
indexes: [{ fields: ['tenant_id', 'domain'], unique: true }, { fields: ['tenant_id', 'service', 'status'] }]
|
|
14
|
+
}
|
|
15
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
applicationId: definePrimaryType('applicationId', { allowNull: false, comment: '所属应用 ID' }),
|
|
5
|
+
uid: { type: DataTypes.STRING, comment: '外部端点 ID' },
|
|
6
|
+
name: { type: DataTypes.STRING, comment: '端点名称' },
|
|
7
|
+
direction: { type: DataTypes.ENUM('inbound', 'outbound'), defaultValue: 'outbound', comment: '方向:接收/投递' },
|
|
8
|
+
type: { type: DataTypes.STRING, defaultValue: 'http', comment: '端点协议类型' },
|
|
9
|
+
kind: { type: DataTypes.ENUM('http', 'fifo', 'polling', 'sink'), defaultValue: 'http', comment: '端点形态' },
|
|
10
|
+
url: { type: DataTypes.TEXT, comment: '接收 URL' },
|
|
11
|
+
secret: { type: DataTypes.STRING, comment: '签名密钥(加密存储,列表脱敏)' },
|
|
12
|
+
version: { type: DataTypes.INTEGER, defaultValue: 1, comment: '密钥版本' },
|
|
13
|
+
eventTypes: { type: DataTypes.JSON, defaultValue: [], comment: '订阅的事件类型,空表示全部' },
|
|
14
|
+
channels: { type: DataTypes.JSON, defaultValue: [], comment: '订阅的 channels' },
|
|
15
|
+
headers: { type: DataTypes.JSON, defaultValue: {}, comment: '自定义请求头' },
|
|
16
|
+
throttleRate: { type: DataTypes.INTEGER, comment: '端点级限流(messages/sec),覆盖应用级' },
|
|
17
|
+
status: { type: DataTypes.ENUM('active', 'disabled', 'paused'), defaultValue: 'active', comment: '端点状态' },
|
|
18
|
+
rate: { type: DataTypes.DOUBLE, comment: 'FIFO 批量大小 / 发送速率' },
|
|
19
|
+
expireAt: { type: DataTypes.DATE, comment: '过期时间' },
|
|
20
|
+
options: { type: DataTypes.JSON, defaultValue: {}, comment: '扩展配置:mTLS、Basic 认证、强制 HTTPS、重试开关等' },
|
|
21
|
+
disabledReason: { type: DataTypes.STRING, comment: '自动禁用原因' },
|
|
22
|
+
lastFailureAt: { type: DataTypes.DATE, comment: '最近一次失败时间' },
|
|
23
|
+
firstFailureAt: { type: DataTypes.DATE, comment: '自动禁用统计窗口起点' },
|
|
24
|
+
failureCount: { type: DataTypes.INTEGER, defaultValue: 0, comment: '连续失败次数' },
|
|
25
|
+
description: { type: DataTypes.TEXT, comment: '端点描述' },
|
|
26
|
+
metadata: { type: DataTypes.JSON, defaultValue: {}, comment: '扩展元数据' }
|
|
27
|
+
},
|
|
28
|
+
options: {
|
|
29
|
+
comment: 'Webhook 端点',
|
|
30
|
+
indexes: [
|
|
31
|
+
{ fields: ['tenant_id', 'application_id'] },
|
|
32
|
+
{ fields: ['uid'], unique: true },
|
|
33
|
+
{ fields: ['application_id', 'status'] },
|
|
34
|
+
{ fields: ['status'] }
|
|
35
|
+
]
|
|
36
|
+
}
|
|
37
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
name: { type: DataTypes.STRING, allowNull: false, comment: '环境名称' },
|
|
5
|
+
region: { type: DataTypes.STRING, defaultValue: 'default', comment: '部署区域(数据驻留)' },
|
|
6
|
+
uid: { type: DataTypes.STRING, comment: '外部业务键(租户内唯一)' },
|
|
7
|
+
settings: { type: DataTypes.JSON, defaultValue: {}, comment: '环境设置:重试/变换/高级目标/自动禁用开关/保留策略' }
|
|
8
|
+
},
|
|
9
|
+
options: {
|
|
10
|
+
comment: 'Webhook 环境(顶层隔离单元)',
|
|
11
|
+
indexes: [{ fields: ['tenant_id', 'uid'], unique: true }, { fields: ['tenant_id', 'region'] }]
|
|
12
|
+
}
|
|
13
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
name: { type: DataTypes.STRING, allowNull: false, comment: '事件类型名,如 user.signup' },
|
|
5
|
+
displayName: { type: DataTypes.STRING, comment: '展示名称(事件目录公开访问必填)' },
|
|
6
|
+
description: { type: DataTypes.TEXT, comment: '事件描述' },
|
|
7
|
+
descriptionTranslations: { type: DataTypes.JSON, defaultValue: {}, comment: '多语言描述(BCP-47)' },
|
|
8
|
+
schema: { type: DataTypes.JSON, comment: 'JSON Schema Draft 7' },
|
|
9
|
+
examples: { type: DataTypes.JSON, defaultValue: [], comment: '示例 payload 列表' },
|
|
10
|
+
featureFlags: { type: DataTypes.JSON, defaultValue: [], comment: '可见性 feature flags(不影响投递)' },
|
|
11
|
+
groupName: { type: DataTypes.STRING, comment: '事件分组名' },
|
|
12
|
+
status: { type: DataTypes.ENUM('active', 'disabled'), defaultValue: 'active', comment: '状态' },
|
|
13
|
+
metadata: { type: DataTypes.JSON, defaultValue: {}, comment: '扩展元数据' }
|
|
14
|
+
},
|
|
15
|
+
options: {
|
|
16
|
+
comment: 'Webhook 事件类型',
|
|
17
|
+
indexes: [
|
|
18
|
+
{ fields: ['tenant_id', 'name'], unique: true },
|
|
19
|
+
{ fields: ['tenant_id', 'status'] }
|
|
20
|
+
]
|
|
21
|
+
}
|
|
22
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
key: { type: DataTypes.STRING, allowNull: false, comment: '幂等键' },
|
|
4
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
5
|
+
endpoint: { type: DataTypes.STRING, comment: '接口标识,避免不同接口共用 key' },
|
|
6
|
+
fingerprint: { type: DataTypes.STRING, comment: '请求体指纹,防止同 key 不同请求' },
|
|
7
|
+
response: { type: DataTypes.JSON, comment: '缓存的响应' },
|
|
8
|
+
expiresAt: { type: DataTypes.DATE, allowNull: false, comment: '过期时间(12 小时)' }
|
|
9
|
+
},
|
|
10
|
+
options: {
|
|
11
|
+
comment: 'Webhook 幂等记录',
|
|
12
|
+
indexes: [{ fields: ['tenant_id', 'endpoint', 'key'], unique: true }, { fields: ['expires_at'] }]
|
|
13
|
+
}
|
|
14
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
webhookId: { type: DataTypes.STRING, allowNull: false, comment: '入站 webhook-id' },
|
|
5
|
+
source: { type: DataTypes.STRING, comment: '来源:inbound / ingest' },
|
|
6
|
+
type: { type: DataTypes.STRING, comment: '事件类型' },
|
|
7
|
+
status: { type: DataTypes.ENUM('received', 'processed', 'failed'), defaultValue: 'received', comment: '处理状态' },
|
|
8
|
+
requestBody: { type: DataTypes.TEXT, comment: '原始请求体' },
|
|
9
|
+
requestHeaders: { type: DataTypes.JSON, defaultValue: {}, comment: '请求头' },
|
|
10
|
+
error: { type: DataTypes.TEXT, comment: '错误信息' },
|
|
11
|
+
receivedAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, comment: '接收时间' }
|
|
12
|
+
},
|
|
13
|
+
options: {
|
|
14
|
+
comment: 'Webhook 入站去重记录',
|
|
15
|
+
indexes: [{ fields: ['tenant_id', 'webhook_id', 'source'], unique: true }, { fields: ['received_at'] }]
|
|
16
|
+
}
|
|
17
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
name: { type: DataTypes.STRING, allowNull: false, comment: 'Source 名称' },
|
|
5
|
+
uid: { type: DataTypes.STRING, comment: '外部 Source ID' },
|
|
6
|
+
type: { type: DataTypes.STRING, allowNull: false, comment: 'Provider 类型或 genericWebhook' },
|
|
7
|
+
config: { type: DataTypes.JSON, defaultValue: {}, comment: 'Provider 配置:签名密钥、header 位置等' },
|
|
8
|
+
ingestUrl: { type: DataTypes.STRING, comment: '生成的接收 URL' },
|
|
9
|
+
destinations: { type: DataTypes.JSON, defaultValue: [], comment: '转发目标端点 ID 列表' },
|
|
10
|
+
transformation: { type: DataTypes.JSON, comment: 'Ingest transformation 配置' },
|
|
11
|
+
status: { type: DataTypes.ENUM('active', 'disabled'), defaultValue: 'active', comment: '状态' }
|
|
12
|
+
},
|
|
13
|
+
options: {
|
|
14
|
+
comment: 'Webhook Ingest Source',
|
|
15
|
+
indexes: [{ fields: ['tenant_id', 'uid'], unique: true }, { fields: ['tenant_id', 'type'] }]
|
|
16
|
+
}
|
|
17
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
sourceId: definePrimaryType('sourceId', { allowNull: false, comment: 'Source ID' }),
|
|
5
|
+
tokenHash: { type: DataTypes.STRING, allowNull: false, comment: 'token 哈希' },
|
|
6
|
+
version: { type: DataTypes.INTEGER, defaultValue: 1, comment: 'token 版本' },
|
|
7
|
+
rotatedAt: { type: DataTypes.DATE, comment: '轮换时间' },
|
|
8
|
+
expiresAt: { type: DataTypes.DATE, comment: '过期时间(旧 token 保留 24 小时)' },
|
|
9
|
+
status: { type: DataTypes.ENUM('active', 'expired'), defaultValue: 'active', comment: '状态' }
|
|
10
|
+
},
|
|
11
|
+
options: {
|
|
12
|
+
comment: 'Webhook Ingest Token',
|
|
13
|
+
indexes: [{ fields: ['tenant_id', 'source_id', 'token_hash'], unique: true }, { fields: ['tenant_id', 'expires_at'] }]
|
|
14
|
+
}
|
|
15
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
applicationId: definePrimaryType('applicationId', { allowNull: false, comment: '所属应用 ID' }),
|
|
5
|
+
environmentId: definePrimaryType('environmentId', { comment: '所属环境 ID' }),
|
|
6
|
+
uid: { type: DataTypes.STRING, comment: '外部消息 ID' },
|
|
7
|
+
eventId: { type: DataTypes.STRING, comment: '业务事件 ID(应用内唯一,用于映射)' },
|
|
8
|
+
type: { type: DataTypes.STRING, allowNull: false, comment: '事件类型,如 user.signup' },
|
|
9
|
+
channels: { type: DataTypes.JSON, defaultValue: [], comment: '消息 channels' },
|
|
10
|
+
payload: { type: DataTypes.JSON, comment: '消息 payload' },
|
|
11
|
+
rawBody: { type: DataTypes.TEXT, comment: '原始请求体(签名与重放使用)' },
|
|
12
|
+
payloadRetentionPeriod: { type: DataTypes.INTEGER, comment: 'payload 保留天数(覆盖环境默认)' },
|
|
13
|
+
status: { type: DataTypes.ENUM('pending', 'sending', 'success', 'failed', 'aborted'), defaultValue: 'pending', comment: '消息状态' },
|
|
14
|
+
region: { type: DataTypes.STRING, comment: '数据驻留区域' },
|
|
15
|
+
source: { type: DataTypes.STRING, defaultValue: 'api', comment: '消息来源:api / ingest / operational / play' },
|
|
16
|
+
metadata: { type: DataTypes.JSON, defaultValue: {}, comment: '扩展元数据' }
|
|
17
|
+
},
|
|
18
|
+
options: {
|
|
19
|
+
comment: 'Webhook 消息',
|
|
20
|
+
indexes: [
|
|
21
|
+
{ fields: ['tenant_id', 'application_id', 'created_at'] },
|
|
22
|
+
{ fields: ['tenant_id', 'event_id'], unique: true },
|
|
23
|
+
{ fields: ['tenant_id', 'type'] },
|
|
24
|
+
{ fields: ['status'] }
|
|
25
|
+
]
|
|
26
|
+
}
|
|
27
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
url: { type: DataTypes.TEXT, allowNull: false, comment: '运营 webhook 接收 URL' },
|
|
5
|
+
eventTypes: { type: DataTypes.JSON, defaultValue: [], comment: '订阅的运营事件类型' },
|
|
6
|
+
secret: { type: DataTypes.STRING, comment: '签名密钥(加密存储)' },
|
|
7
|
+
status: { type: DataTypes.ENUM('active', 'disabled'), defaultValue: 'active', comment: '状态' }
|
|
8
|
+
},
|
|
9
|
+
options: {
|
|
10
|
+
comment: 'Webhook 运营事件订阅',
|
|
11
|
+
indexes: [{ fields: ['tenant_id', 'status'] }]
|
|
12
|
+
}
|
|
13
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
endpointId: definePrimaryType('endpointId', { allowNull: false, comment: 'Polling 端点 ID' }),
|
|
5
|
+
consumerId: { type: DataTypes.STRING, allowNull: false, comment: '稳定的 consumer ID' },
|
|
6
|
+
// Snowflake attempt.id 超出 Number 安全整数,offset 必须用字符串存,避免 BIGINT→JS Number 丢精度
|
|
7
|
+
offset: { type: DataTypes.STRING, defaultValue: '0', comment: '已确认的消息 offset(十进制字符串)' },
|
|
8
|
+
leaseExpiresAt: { type: DataTypes.DATE, comment: '租约过期时间' },
|
|
9
|
+
leaseOffset: { type: DataTypes.STRING, defaultValue: '0', comment: '租约内已拉取的最大 offset(十进制字符串)' }
|
|
10
|
+
},
|
|
11
|
+
options: {
|
|
12
|
+
comment: 'Webhook Polling Consumer 进度',
|
|
13
|
+
indexes: [{ fields: ['tenant_id', 'endpoint_id', 'consumer_id'], unique: true }]
|
|
14
|
+
}
|
|
15
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
endpointId: definePrimaryType('endpointId', { allowNull: false, comment: '端点 ID' }),
|
|
5
|
+
tokenHash: { type: DataTypes.STRING, comment: '拉取 URL token 哈希' },
|
|
6
|
+
apiKeyHash: { type: DataTypes.STRING, comment: 'API Key 哈希' },
|
|
7
|
+
status: { type: DataTypes.ENUM('active', 'disabled'), defaultValue: 'active', comment: '状态' }
|
|
8
|
+
},
|
|
9
|
+
options: {
|
|
10
|
+
comment: 'Webhook Polling Endpoint 凭证',
|
|
11
|
+
indexes: [{ fields: ['tenant_id', 'endpoint_id'], unique: true }, { fields: ['tenant_id', 'token_hash'] }]
|
|
12
|
+
}
|
|
13
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
streamId: definePrimaryType('streamId', { allowNull: false, comment: 'Stream ID' }),
|
|
4
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
5
|
+
type: { type: DataTypes.STRING, allowNull: false, comment: 'Sink 类型' },
|
|
6
|
+
uid: { type: DataTypes.STRING, comment: '外部 Sink ID' },
|
|
7
|
+
config: { type: DataTypes.JSON, defaultValue: {}, comment: 'Sink 目标配置' },
|
|
8
|
+
batchSize: { type: DataTypes.INTEGER, defaultValue: 100, comment: '批大小' },
|
|
9
|
+
maxWaitSecs: { type: DataTypes.INTEGER, defaultValue: 5, comment: '最大等待秒数' },
|
|
10
|
+
eventTypes: { type: DataTypes.JSON, defaultValue: [], comment: '订阅的事件类型' },
|
|
11
|
+
metadata: { type: DataTypes.JSON, defaultValue: {}, comment: '元数据' },
|
|
12
|
+
transformation: { type: DataTypes.JSON, comment: '批次变换配置' },
|
|
13
|
+
status: { type: DataTypes.ENUM('active', 'disabled'), defaultValue: 'active', comment: '状态' },
|
|
14
|
+
lastProcessedAt: { type: DataTypes.DATE, comment: '上次处理时间' }
|
|
15
|
+
},
|
|
16
|
+
options: {
|
|
17
|
+
comment: 'Webhook Sink',
|
|
18
|
+
indexes: [{ fields: ['tenant_id', 'stream_id', 'uid'], unique: true }, { fields: ['tenant_id', 'stream_id', 'status'] }]
|
|
19
|
+
}
|
|
20
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
name: { type: DataTypes.STRING, allowNull: false, comment: 'Stream 名称' },
|
|
5
|
+
uid: { type: DataTypes.STRING, comment: '外部 Stream ID' },
|
|
6
|
+
description: { type: DataTypes.STRING, comment: '描述' },
|
|
7
|
+
status: { type: DataTypes.ENUM('active', 'disabled'), defaultValue: 'active', comment: '状态' }
|
|
8
|
+
},
|
|
9
|
+
options: {
|
|
10
|
+
comment: 'Webhook Stream',
|
|
11
|
+
indexes: [{ fields: ['tenant_id', 'uid'], unique: true }]
|
|
12
|
+
}
|
|
13
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
streamId: definePrimaryType('streamId', { allowNull: false, comment: 'Stream ID' }),
|
|
4
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
5
|
+
eventType: { type: DataTypes.STRING, allowNull: false, comment: '事件类型' },
|
|
6
|
+
payload: { type: DataTypes.JSON, comment: '事件 payload' },
|
|
7
|
+
consumed: { type: DataTypes.BOOLEAN, defaultValue: false, comment: '是否已被所有 sink 消费' },
|
|
8
|
+
createdAt: { type: DataTypes.DATE, defaultValue: DataTypes.NOW, comment: '写入时间' }
|
|
9
|
+
},
|
|
10
|
+
options: {
|
|
11
|
+
comment: 'Webhook Stream 事件',
|
|
12
|
+
indexes: [{ fields: ['tenant_id', 'stream_id', 'consumed', 'created_at'] }, { fields: ['tenant_id', 'stream_id', 'event_type'] }]
|
|
13
|
+
}
|
|
14
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, definePrimaryType, options }) => ({
|
|
2
|
+
model: {
|
|
3
|
+
tenantId: { type: DataTypes.STRING, allowNull: false, defaultValue: '', comment: '租户 ID,空字符串表示全局' },
|
|
4
|
+
endpointId: definePrimaryType('endpointId', { allowNull: false, comment: '端点 ID' }),
|
|
5
|
+
code: { type: DataTypes.TEXT, comment: 'JavaScript handler 代码' },
|
|
6
|
+
enabled: { type: DataTypes.BOOLEAN, defaultValue: true, comment: '是否启用' },
|
|
7
|
+
variables: { type: DataTypes.JSON, defaultValue: {}, comment: '环境变量' }
|
|
8
|
+
},
|
|
9
|
+
options: {
|
|
10
|
+
comment: 'Webhook Transformation 脚本',
|
|
11
|
+
indexes: [{ fields: ['tenant_id', 'endpoint_id'], unique: true }]
|
|
12
|
+
}
|
|
13
|
+
});
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
// 响应 schema 仅用于 OpenAPI 文档:字段可空、允许额外字段,避免序列化改变实际输出
|
|
2
|
+
const str = description => ({ type: 'string', nullable: true, description });
|
|
3
|
+
const num = description => ({ type: 'number', nullable: true, description });
|
|
4
|
+
const bool = description => ({ type: 'boolean', nullable: true, description });
|
|
5
|
+
const any = description => ({ description });
|
|
6
|
+
const obj = (description, properties = {}) => ({ type: 'object', nullable: true, additionalProperties: true, description, properties });
|
|
7
|
+
const list = (description, items) => ({ type: 'array', nullable: true, description, items });
|
|
8
|
+
const success = (description, schema) => ({ 200: Object.assign({}, schema || { type: 'object', additionalProperties: true }, { description }) });
|
|
9
|
+
const empty = description => success(description || '操作成功,返回空对象');
|
|
10
|
+
|
|
11
|
+
const pagination = (description, itemSchema, itemName) => ({
|
|
12
|
+
type: 'object',
|
|
13
|
+
additionalProperties: true,
|
|
14
|
+
description,
|
|
15
|
+
properties: {
|
|
16
|
+
pageData: list(`${itemName}列表`, itemSchema),
|
|
17
|
+
totalCount: num('符合条件的总数')
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const application = obj('应用(消费者)', {
|
|
22
|
+
id: any('应用 ID'),
|
|
23
|
+
uid: str('外部消费者 ID'),
|
|
24
|
+
name: str('应用名称'),
|
|
25
|
+
description: str('描述'),
|
|
26
|
+
throttleRate: num('应用级限流 messages/sec'),
|
|
27
|
+
status: str('状态:active / disabled'),
|
|
28
|
+
region: str('数据驻留区域'),
|
|
29
|
+
metadata: any('扩展元数据')
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const endpoint = obj('端点', {
|
|
33
|
+
id: any('端点 ID'),
|
|
34
|
+
applicationId: any('应用 ID'),
|
|
35
|
+
uid: str('外部端点 ID'),
|
|
36
|
+
name: str('端点名称'),
|
|
37
|
+
direction: str('方向:inbound / outbound'),
|
|
38
|
+
kind: str('形态:http / fifo / polling / sink'),
|
|
39
|
+
url: str('接收 URL'),
|
|
40
|
+
secret: str('签名密钥(脱敏)'),
|
|
41
|
+
version: num('密钥版本'),
|
|
42
|
+
eventTypes: any('订阅的事件类型'),
|
|
43
|
+
channels: any('订阅的 channels'),
|
|
44
|
+
headers: any('自定义请求头'),
|
|
45
|
+
throttleRate: num('端点级限流'),
|
|
46
|
+
status: str('状态:active / disabled / paused'),
|
|
47
|
+
rate: num('FIFO 批量大小'),
|
|
48
|
+
expireAt: any('过期时间'),
|
|
49
|
+
options: any('扩展配置'),
|
|
50
|
+
metadata: any('扩展元数据')
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const message = obj('消息', {
|
|
54
|
+
id: any('消息 ID'),
|
|
55
|
+
applicationId: any('应用 ID'),
|
|
56
|
+
uid: str('外部消息 ID'),
|
|
57
|
+
eventId: str('业务事件 ID'),
|
|
58
|
+
type: str('事件类型'),
|
|
59
|
+
channels: any('channels'),
|
|
60
|
+
payload: any('payload'),
|
|
61
|
+
status: str('状态:pending / sending / success / failed / aborted'),
|
|
62
|
+
region: str('区域'),
|
|
63
|
+
source: str('来源'),
|
|
64
|
+
metadata: any('扩展元数据'),
|
|
65
|
+
createdAt: any('创建时间')
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const attempt = obj('投递尝试记录', {
|
|
69
|
+
id: any('记录 ID'),
|
|
70
|
+
messageId: any('消息 ID'),
|
|
71
|
+
endpointId: any('端点 ID'),
|
|
72
|
+
webhookId: str('webhook-id'),
|
|
73
|
+
requestHeaders: any('请求头'),
|
|
74
|
+
requestBody: str('原始请求体'),
|
|
75
|
+
responseStatus: num('响应状态码'),
|
|
76
|
+
responseBody: str('响应内容'),
|
|
77
|
+
error: str('错误信息'),
|
|
78
|
+
status: str('状态:pending / success / failed / retrying'),
|
|
79
|
+
startAt: any('开始时间'),
|
|
80
|
+
endAt: any('结束时间'),
|
|
81
|
+
duration: num('耗时毫秒'),
|
|
82
|
+
retryCount: num('重试次数'),
|
|
83
|
+
nextAttemptAt: any('下次重试时间'),
|
|
84
|
+
canceled: bool('是否被变换取消')
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const eventType = obj('事件类型', {
|
|
88
|
+
id: any('ID'),
|
|
89
|
+
name: str('事件类型名'),
|
|
90
|
+
displayName: str('展示名称'),
|
|
91
|
+
description: str('描述'),
|
|
92
|
+
descriptionTranslations: any('多语言描述'),
|
|
93
|
+
schema: any('JSON Schema Draft 7'),
|
|
94
|
+
examples: any('示例列表'),
|
|
95
|
+
featureFlags: any('feature flags'),
|
|
96
|
+
groupName: str('分组'),
|
|
97
|
+
status: str('状态')
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
module.exports = {
|
|
101
|
+
str,
|
|
102
|
+
num,
|
|
103
|
+
bool,
|
|
104
|
+
any,
|
|
105
|
+
obj,
|
|
106
|
+
list,
|
|
107
|
+
success,
|
|
108
|
+
empty,
|
|
109
|
+
pagination,
|
|
110
|
+
application,
|
|
111
|
+
endpoint,
|
|
112
|
+
message,
|
|
113
|
+
attempt,
|
|
114
|
+
eventType,
|
|
115
|
+
applicationList: pagination('应用列表', application, '应用'),
|
|
116
|
+
endpointList: pagination('端点列表', endpoint, '端点'),
|
|
117
|
+
messageList: pagination('消息列表', message, '消息'),
|
|
118
|
+
attemptList: pagination('投递记录列表', attempt, '记录'),
|
|
119
|
+
eventTypeList: pagination('事件类型列表', eventType, '事件类型')
|
|
120
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const fp = require('fastify-plugin');
|
|
4
|
+
const { generateToken } = require('../utils/common');
|
|
5
|
+
const { hashToken } = require('../utils/common');
|
|
6
|
+
|
|
7
|
+
const MAGIC_LINK_TTL = 7 * 24 * 60 * 60 * 1000; // 7 天
|
|
8
|
+
|
|
9
|
+
module.exports = fp(async (fastify, options) => {
|
|
10
|
+
const ctx = fastify[options.name].context;
|
|
11
|
+
const { models, utils, tenantWhere, Op } = ctx;
|
|
12
|
+
|
|
13
|
+
const portalBaseUrl = options.portalBaseUrl || '/api/webhook/portal';
|
|
14
|
+
|
|
15
|
+
/** 生成 magic link,返回一次性 token 与 URL */
|
|
16
|
+
const appPortalAccess = async (authenticatePayload, { applicationId, featureFlags, sessionId, next, branding } = {}) => {
|
|
17
|
+
if (!applicationId) {
|
|
18
|
+
throw new Error('缺少应用 ID');
|
|
19
|
+
}
|
|
20
|
+
const tenantId = utils.resolveTenantId(authenticatePayload);
|
|
21
|
+
const token = generateToken();
|
|
22
|
+
const record = await models.appPortalSession.create({
|
|
23
|
+
applicationId,
|
|
24
|
+
tenantId,
|
|
25
|
+
tokenHash: hashToken(token),
|
|
26
|
+
sessionId: sessionId || null,
|
|
27
|
+
featureFlags: featureFlags || [],
|
|
28
|
+
next: next || null,
|
|
29
|
+
branding: branding || {},
|
|
30
|
+
expiresAt: new Date(Date.now() + MAGIC_LINK_TTL),
|
|
31
|
+
status: 'active'
|
|
32
|
+
});
|
|
33
|
+
const query = new URLSearchParams({ token });
|
|
34
|
+
if (branding) {
|
|
35
|
+
Object.entries(branding).forEach(([key, value]) => {
|
|
36
|
+
if (value != null) {
|
|
37
|
+
query.set(key, String(value));
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return {
|
|
42
|
+
token,
|
|
43
|
+
url: `${portalBaseUrl}?${query.toString()}`,
|
|
44
|
+
expiresAt: record.expiresAt
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/** 校验 magic link token 并返回会话上下文 */
|
|
49
|
+
const verifySession = async (token) => {
|
|
50
|
+
const record = await models.appPortalSession.findOne({
|
|
51
|
+
where: { tokenHash: hashToken(token), status: 'active', expiresAt: { [Op.gt]: new Date() } }
|
|
52
|
+
});
|
|
53
|
+
if (!record) {
|
|
54
|
+
throw new Error('Portal 会话无效或已过期');
|
|
55
|
+
}
|
|
56
|
+
if (!record.usedAt) {
|
|
57
|
+
record.usedAt = new Date();
|
|
58
|
+
await record.save();
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
applicationId: record.applicationId,
|
|
62
|
+
tenantId: record.tenantId,
|
|
63
|
+
featureFlags: record.featureFlags || [],
|
|
64
|
+
sessionId: record.sessionId,
|
|
65
|
+
next: record.next,
|
|
66
|
+
branding: record.branding || {}
|
|
67
|
+
};
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/** 按 sessionId 失效全部会话(Expire All) */
|
|
71
|
+
const expireAll = async (authenticatePayload, { sessionId, applicationId } = {}) => {
|
|
72
|
+
if (!sessionId && !applicationId) {
|
|
73
|
+
throw new Error('必须提供 sessionId 或 applicationId');
|
|
74
|
+
}
|
|
75
|
+
const where = tenantWhere(authenticatePayload, { status: 'active' });
|
|
76
|
+
if (sessionId) {
|
|
77
|
+
where.sessionId = sessionId;
|
|
78
|
+
}
|
|
79
|
+
if (applicationId) {
|
|
80
|
+
where.applicationId = applicationId;
|
|
81
|
+
}
|
|
82
|
+
const [updated] = await models.appPortalSession.update({ status: 'expired' }, { where });
|
|
83
|
+
return { expired: updated };
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
fastify[options.name].services.appPortal = { appPortalAccess, verifySession, expireAll };
|
|
87
|
+
});
|