@kne/fastify-webhook 0.1.0-alpha.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 +27 -0
- package/index.js +49 -0
- package/libs/controllers/main.js +119 -0
- package/libs/models/example._js +18 -0
- package/libs/models/invocation.js +37 -0
- package/libs/models/webhook.js +53 -0
- package/libs/services/example._js +10 -0
- package/libs/services/main.js +117 -0
- package/package.json +52 -0
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
|
|
2
|
+
# fastify-webhook
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
### 描述
|
|
6
|
+
|
|
7
|
+
实现一个webhook功能.
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### 安装
|
|
11
|
+
|
|
12
|
+
```shell
|
|
13
|
+
npm i --save @kne/fastify-webhook
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### 示例
|
|
17
|
+
|
|
18
|
+
#### 示例代码
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### API
|
|
23
|
+
|
|
24
|
+
| 属性名 | 说明 | 类型 | 默认值 |
|
|
25
|
+
|-----|----|----|-----|
|
|
26
|
+
| | | | |
|
|
27
|
+
|
package/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
const path = require('node:path');
|
|
3
|
+
|
|
4
|
+
module.exports = fp(async (fastify, options) => {
|
|
5
|
+
options = Object.assign(
|
|
6
|
+
{},
|
|
7
|
+
{
|
|
8
|
+
prefix: '/api/webhook',
|
|
9
|
+
dbTableNamePrefix: 't_',
|
|
10
|
+
name: 'webhook',
|
|
11
|
+
hooks: {},
|
|
12
|
+
getUserAuthenticate: () => {
|
|
13
|
+
if (!fastify.account) {
|
|
14
|
+
throw new Error('fastify-account plugin must be registered before fastify-trtc,or set options.getUserAuthenticate');
|
|
15
|
+
}
|
|
16
|
+
return fastify.account.authenticate.user;
|
|
17
|
+
},
|
|
18
|
+
getAdminAuthenticate: () => {
|
|
19
|
+
if (!fastify.account) {
|
|
20
|
+
throw new Error('fastify-account plugin must be registered before fastify-trtc,or set options.getAdminAuthenticate');
|
|
21
|
+
}
|
|
22
|
+
return fastify.account.authenticate.admin;
|
|
23
|
+
},
|
|
24
|
+
getUserModel: () => {
|
|
25
|
+
if (!fastify.account) {
|
|
26
|
+
throw new Error('fastify-account plugin must be registered before fastify-trtc,or set options.getUserModel');
|
|
27
|
+
}
|
|
28
|
+
return fastify.account.models.user;
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
options
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
fastify.register(require('@kne/fastify-namespace'), {
|
|
35
|
+
options,
|
|
36
|
+
name: options.name,
|
|
37
|
+
modules: [
|
|
38
|
+
['controllers', path.resolve(__dirname, './libs/controllers')],
|
|
39
|
+
[
|
|
40
|
+
'models',
|
|
41
|
+
await fastify.sequelize.addModels(path.resolve(__dirname, './libs/models'), {
|
|
42
|
+
prefix: options.dbTableNamePrefix,
|
|
43
|
+
getUserModel: options.getUserModel
|
|
44
|
+
})
|
|
45
|
+
],
|
|
46
|
+
['services', path.resolve(__dirname, './libs/services')]
|
|
47
|
+
]
|
|
48
|
+
});
|
|
49
|
+
});
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
|
|
3
|
+
module.exports = fp(async (fastify, options) => {
|
|
4
|
+
const { services } = fastify.project;
|
|
5
|
+
const userAuthenticate = options.getUserAuthenticate(),
|
|
6
|
+
adminAuthenticate = options.getAdminAuthenticate();
|
|
7
|
+
fastify.post(
|
|
8
|
+
`${options.prefix}/create`,
|
|
9
|
+
{
|
|
10
|
+
onRequest: [userAuthenticate, adminAuthenticate],
|
|
11
|
+
schema: {
|
|
12
|
+
summary: '添加webhooks',
|
|
13
|
+
body: {
|
|
14
|
+
type: 'object',
|
|
15
|
+
properties: {
|
|
16
|
+
name: { type: 'string' },
|
|
17
|
+
type: { type: 'string' },
|
|
18
|
+
expire: { type: 'string', format: 'date-time' }
|
|
19
|
+
},
|
|
20
|
+
required: ['name', 'type']
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
async request => {
|
|
25
|
+
return services.create({ ...request.body, userId: request.userInfo.id });
|
|
26
|
+
}
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
fastify.get(
|
|
30
|
+
`${options.prefix}/list`,
|
|
31
|
+
{
|
|
32
|
+
onRequest: [userAuthenticate, adminAuthenticate]
|
|
33
|
+
},
|
|
34
|
+
async () => {
|
|
35
|
+
return services.list();
|
|
36
|
+
}
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
fastify.post(
|
|
40
|
+
`${options.prefix}/set-status`,
|
|
41
|
+
{
|
|
42
|
+
onRequest: [userAuthenticate, adminAuthenticate],
|
|
43
|
+
schema: {
|
|
44
|
+
summary: '设置webhook状态',
|
|
45
|
+
body: {
|
|
46
|
+
type: 'object',
|
|
47
|
+
properties: {
|
|
48
|
+
id: { type: 'number' },
|
|
49
|
+
status: { type: 'string', enum: ['open', 'close'] }
|
|
50
|
+
},
|
|
51
|
+
required: ['id', 'status']
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
async request => {
|
|
56
|
+
return services.setStatus(request.body);
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
fastify.post(
|
|
61
|
+
`${options.prefix}/invoke/:type`,
|
|
62
|
+
{
|
|
63
|
+
schema: {
|
|
64
|
+
summary: '调用webhook post请求'
|
|
65
|
+
},
|
|
66
|
+
params: {
|
|
67
|
+
type: 'object',
|
|
68
|
+
properties: {
|
|
69
|
+
type: { type: 'string' }
|
|
70
|
+
},
|
|
71
|
+
required: ['type']
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
async request => {
|
|
75
|
+
const { type } = request.params;
|
|
76
|
+
return services.invoke(
|
|
77
|
+
Object.assign(
|
|
78
|
+
{},
|
|
79
|
+
{ type },
|
|
80
|
+
{
|
|
81
|
+
body: request.body,
|
|
82
|
+
headers: request.headers,
|
|
83
|
+
query: request.query
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
fastify.get(
|
|
91
|
+
`${options.prefix}/invoke/:type`,
|
|
92
|
+
{
|
|
93
|
+
schema: {
|
|
94
|
+
summary: '调用webhook get请求'
|
|
95
|
+
},
|
|
96
|
+
params: {
|
|
97
|
+
type: 'object',
|
|
98
|
+
properties: {
|
|
99
|
+
type: { type: 'string' }
|
|
100
|
+
},
|
|
101
|
+
required: ['type']
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
async request => {
|
|
105
|
+
const { type } = request.params;
|
|
106
|
+
return services.invoke(
|
|
107
|
+
Object.assign(
|
|
108
|
+
{},
|
|
109
|
+
{ type },
|
|
110
|
+
{
|
|
111
|
+
body: request.body,
|
|
112
|
+
headers: request.headers,
|
|
113
|
+
query: request.query
|
|
114
|
+
}
|
|
115
|
+
)
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
module.exports = ({DataTypes, options}) => {
|
|
2
|
+
return {
|
|
3
|
+
//name: 'modelName',//此处定义modelName,默认根据文件名转驼峰命名,可以缺省
|
|
4
|
+
model: {
|
|
5
|
+
name: {
|
|
6
|
+
type: DataTypes.STRING,
|
|
7
|
+
comment: '名称'
|
|
8
|
+
}
|
|
9
|
+
}, associate: ({/*这里可以获取models*/}, fastify) => {
|
|
10
|
+
//可以这样获取某个namespace的models fastify.account.models
|
|
11
|
+
// options.getUserModel() 可以获取 user model
|
|
12
|
+
}, options: {
|
|
13
|
+
comment: '',
|
|
14
|
+
indexes: []
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, options }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
4
|
+
startTime: {
|
|
5
|
+
type: DataTypes.DATE,
|
|
6
|
+
comment: '调用时间',
|
|
7
|
+
allowNull: false
|
|
8
|
+
},
|
|
9
|
+
endTime: {
|
|
10
|
+
type: DataTypes.DATE,
|
|
11
|
+
comment: '调用结束时间',
|
|
12
|
+
allowNull: false
|
|
13
|
+
},
|
|
14
|
+
input: {
|
|
15
|
+
type: DataTypes.JSON,
|
|
16
|
+
comment: '调用参数'
|
|
17
|
+
},
|
|
18
|
+
status: {
|
|
19
|
+
type: DataTypes.ENUM('success', 'failed'),
|
|
20
|
+
comment: '调用返回状态'
|
|
21
|
+
},
|
|
22
|
+
result: {
|
|
23
|
+
type: DataTypes.JSON,
|
|
24
|
+
comment: '调用结果'
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
associate: ({ invocation, webhook }) => {
|
|
28
|
+
invocation.belongsTo(webhook, {
|
|
29
|
+
allowNull: false,
|
|
30
|
+
comment: 'webhook'
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
options: {
|
|
34
|
+
comment: '调用记录'
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module.exports = ({ DataTypes, options }) => {
|
|
2
|
+
return {
|
|
3
|
+
model: {
|
|
4
|
+
name: {
|
|
5
|
+
type: DataTypes.STRING,
|
|
6
|
+
comment: '名称,需说明调用webhook的端口的身份',
|
|
7
|
+
allowNull: false
|
|
8
|
+
},
|
|
9
|
+
type: {
|
|
10
|
+
type: DataTypes.STRING,
|
|
11
|
+
comment: '在hooks中定义的webhook类型',
|
|
12
|
+
allowNull: false
|
|
13
|
+
},
|
|
14
|
+
signatureLocation: {
|
|
15
|
+
type: DataTypes.STRING,
|
|
16
|
+
comment: '签名所在位置',
|
|
17
|
+
allowNull: false,
|
|
18
|
+
defaultValue: 'headers["x-signature"]'
|
|
19
|
+
},
|
|
20
|
+
inputLocation: {
|
|
21
|
+
type: DataTypes.STRING,
|
|
22
|
+
comment: '输入参数所在位置',
|
|
23
|
+
allowNull: false,
|
|
24
|
+
defaultValue: 'body'
|
|
25
|
+
},
|
|
26
|
+
expire: {
|
|
27
|
+
type: DataTypes.DATE,
|
|
28
|
+
comment: '过期时间,默认为null,不过期',
|
|
29
|
+
defaultValue: null
|
|
30
|
+
},
|
|
31
|
+
status: {
|
|
32
|
+
type: DataTypes.ENUM('open', 'close'),
|
|
33
|
+
comment: '状态',
|
|
34
|
+
defaultValue: 'open'
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
associate: ({ webhook }) => {
|
|
38
|
+
webhook.belongsTo(options.getUserModel(), {
|
|
39
|
+
foreignKey: 'userId',
|
|
40
|
+
comment: '创建人'
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
options: {
|
|
44
|
+
comment: 'webhook',
|
|
45
|
+
indexes: [
|
|
46
|
+
{
|
|
47
|
+
fields: ['name', 'type', 'deleted_at'],
|
|
48
|
+
unique: true
|
|
49
|
+
}
|
|
50
|
+
]
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
const fp = require('fastify-plugin');
|
|
2
|
+
const crypto = require('crypto');
|
|
3
|
+
const transform = require('lodash/transform');
|
|
4
|
+
const groupBy = require('lodash/groupBy');
|
|
5
|
+
const get = require('lodash/get');
|
|
6
|
+
|
|
7
|
+
module.exports = fp(async (fastify, options) => {
|
|
8
|
+
const { models } = fastify[options.name];
|
|
9
|
+
|
|
10
|
+
const create = async ({ name, type, expire, userId }) => {
|
|
11
|
+
if (!options.hooks[type]) {
|
|
12
|
+
throw new Error(`Hook ${type} not allowed`);
|
|
13
|
+
}
|
|
14
|
+
return await models.webhook.create({
|
|
15
|
+
name,
|
|
16
|
+
type,
|
|
17
|
+
expire,
|
|
18
|
+
userId
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
const detail = async ({ id }) => {
|
|
23
|
+
const webhook = await models.webhook.findByPk(id);
|
|
24
|
+
if (!webhook) {
|
|
25
|
+
throw new Error('Webhook not found');
|
|
26
|
+
}
|
|
27
|
+
return webhook;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const list = async () => {
|
|
31
|
+
const list = await models.webhook.findAll();
|
|
32
|
+
const mapping = groupBy(
|
|
33
|
+
list.map(item => {
|
|
34
|
+
const hmac = crypto.createHmac('sha256', item.type);
|
|
35
|
+
hmac.update(`${item.name}|${item.expire ? item.expire.getTime() : 0}`);
|
|
36
|
+
const signature = hmac.digest('hex');
|
|
37
|
+
return Object.assign({}, item.get({ plain: true }), {
|
|
38
|
+
signature
|
|
39
|
+
});
|
|
40
|
+
}),
|
|
41
|
+
'type'
|
|
42
|
+
);
|
|
43
|
+
return transform(
|
|
44
|
+
Object.keys(options.hooks),
|
|
45
|
+
(result, type) => {
|
|
46
|
+
result[type] = mapping[type] || [];
|
|
47
|
+
},
|
|
48
|
+
{}
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const invokeRecord = async ({ id }) => {
|
|
53
|
+
return await models.invocation.findAll({
|
|
54
|
+
where: { webhookId: id }
|
|
55
|
+
});
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const setStatus = async ({ id, status }) => {
|
|
59
|
+
const webhook = await detail({ id });
|
|
60
|
+
webhook.status = status;
|
|
61
|
+
return webhook.save();
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const remove = async ({ id }) => {
|
|
65
|
+
const webhook = await detail({ id });
|
|
66
|
+
await webhook.destroy();
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const invoke = async ({ type, headers, body, query }) => {
|
|
70
|
+
//signature, input
|
|
71
|
+
const list = await models.webhook.findAll({ where: { type } });
|
|
72
|
+
|
|
73
|
+
if (list.length === 0) {
|
|
74
|
+
throw new Error('Webhook not found');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
let currentWebhook = null;
|
|
78
|
+
for (const webhook of list) {
|
|
79
|
+
const signature = get({ headers, body, query }, webhook.signatureLocation);
|
|
80
|
+
const hmac = crypto.createHmac('sha256', type);
|
|
81
|
+
hmac.update(`${webhook.name}|${webhook.expire ? webhook.expire.getTime() : 0}`);
|
|
82
|
+
if (hmac.digest('hex') === signature) {
|
|
83
|
+
currentWebhook = webhook;
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (!currentWebhook) {
|
|
88
|
+
throw new Error('Webhook not registered');
|
|
89
|
+
}
|
|
90
|
+
const input = { headers, body, query }[currentWebhook.inputLocation];
|
|
91
|
+
const startTime = new Date();
|
|
92
|
+
try {
|
|
93
|
+
const result = await options.hooks[currentWebhook.type]({ input });
|
|
94
|
+
await models.invocation.create({
|
|
95
|
+
input,
|
|
96
|
+
result,
|
|
97
|
+
webhookId: currentWebhook.id,
|
|
98
|
+
status: 'success',
|
|
99
|
+
startTime,
|
|
100
|
+
endTime: new Date()
|
|
101
|
+
});
|
|
102
|
+
return {};
|
|
103
|
+
} catch (e) {
|
|
104
|
+
await models.invocation.create({
|
|
105
|
+
input,
|
|
106
|
+
result: e.toString(),
|
|
107
|
+
webhookId: currentWebhook.id,
|
|
108
|
+
status: 'failed',
|
|
109
|
+
startTime,
|
|
110
|
+
endTime: new Date()
|
|
111
|
+
});
|
|
112
|
+
throw e;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
Object.assign(fastify[options.name].services, { create, list, setStatus, remove, invoke, invokeRecord });
|
|
117
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kne/fastify-webhook",
|
|
3
|
+
"version": "0.1.0-alpha.0",
|
|
4
|
+
"description": "实现一个webhook功能.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"init": "husky",
|
|
8
|
+
"build:md": "npx @kne/md-doc",
|
|
9
|
+
"start:md": "npx @kne/md-doc --watch",
|
|
10
|
+
"prettier": "prettier --config .prettierrc --write '{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}'",
|
|
11
|
+
"lint-staged": "npx lint-staged"
|
|
12
|
+
},
|
|
13
|
+
"lint-staged": {
|
|
14
|
+
"{libs/**/*,index}.{js,jsx,ts,tsx,json,css,scss}": [
|
|
15
|
+
"prettier --config .prettierrc --write",
|
|
16
|
+
"git add"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"index.js",
|
|
21
|
+
"libs"
|
|
22
|
+
],
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/kne-union/fastify-webhook.git"
|
|
26
|
+
},
|
|
27
|
+
"keywords": [],
|
|
28
|
+
"author": "linzp",
|
|
29
|
+
"license": "ISC",
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/kne-union/fastify-webhook/issues"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/kne-union/fastify-webhook#readme",
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@kne/fastify-namespace": "*",
|
|
36
|
+
"@kne/fastify-sequelize": "*",
|
|
37
|
+
"fastify-plugin": ">=5"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@fastify/env": "^5.0.2",
|
|
41
|
+
"@kne/fastify-sequelize": "^3.0.6",
|
|
42
|
+
"fastify": "^5.4.0",
|
|
43
|
+
"fastify-plugin": "^5.0.1",
|
|
44
|
+
"husky": "^9.0.11",
|
|
45
|
+
"nodemon": "^3.1.3",
|
|
46
|
+
"prettier": "^3.2.5",
|
|
47
|
+
"sqlite3": "^5.1.7"
|
|
48
|
+
},
|
|
49
|
+
"dependencies": {
|
|
50
|
+
"lodash": "^4.17.21"
|
|
51
|
+
}
|
|
52
|
+
}
|