@kne/fastify-webhook 0.1.0-alpha.0 → 0.1.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/libs/controllers/main.js +59 -17
- package/libs/services/main.js +21 -11
- package/package.json +1 -1
package/libs/controllers/main.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const fp = require('fastify-plugin');
|
|
2
2
|
|
|
3
3
|
module.exports = fp(async (fastify, options) => {
|
|
4
|
-
const { services } = fastify.
|
|
4
|
+
const { services } = fastify[options.name];
|
|
5
5
|
const userAuthenticate = options.getUserAuthenticate(),
|
|
6
6
|
adminAuthenticate = options.getAdminAuthenticate();
|
|
7
7
|
fastify.post(
|
|
@@ -61,14 +61,14 @@ module.exports = fp(async (fastify, options) => {
|
|
|
61
61
|
`${options.prefix}/invoke/:type`,
|
|
62
62
|
{
|
|
63
63
|
schema: {
|
|
64
|
-
summary: '调用webhook post请求'
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
64
|
+
summary: '调用webhook post请求',
|
|
65
|
+
params: {
|
|
66
|
+
type: 'object',
|
|
67
|
+
properties: {
|
|
68
|
+
type: { type: 'string' }
|
|
69
|
+
},
|
|
70
|
+
required: ['type']
|
|
71
|
+
}
|
|
72
72
|
}
|
|
73
73
|
},
|
|
74
74
|
async request => {
|
|
@@ -91,14 +91,14 @@ module.exports = fp(async (fastify, options) => {
|
|
|
91
91
|
`${options.prefix}/invoke/:type`,
|
|
92
92
|
{
|
|
93
93
|
schema: {
|
|
94
|
-
summary: '调用webhook get请求'
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
94
|
+
summary: '调用webhook get请求',
|
|
95
|
+
params: {
|
|
96
|
+
type: 'object',
|
|
97
|
+
properties: {
|
|
98
|
+
type: { type: 'string' }
|
|
99
|
+
},
|
|
100
|
+
required: ['type']
|
|
101
|
+
}
|
|
102
102
|
}
|
|
103
103
|
},
|
|
104
104
|
async request => {
|
|
@@ -116,4 +116,46 @@ module.exports = fp(async (fastify, options) => {
|
|
|
116
116
|
);
|
|
117
117
|
}
|
|
118
118
|
);
|
|
119
|
+
|
|
120
|
+
fastify.get(
|
|
121
|
+
`${options.prefix}/invoke-record`,
|
|
122
|
+
{
|
|
123
|
+
onRequest: [userAuthenticate, adminAuthenticate],
|
|
124
|
+
schema: {
|
|
125
|
+
summary: '获取webhook调用记录',
|
|
126
|
+
query: {
|
|
127
|
+
type: 'object',
|
|
128
|
+
properties: {
|
|
129
|
+
id: { type: 'string' },
|
|
130
|
+
currentPage: { type: 'number', default: 1 },
|
|
131
|
+
perPage: { type: 'number', default: 10 }
|
|
132
|
+
},
|
|
133
|
+
required: ['id']
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
async request => {
|
|
138
|
+
return services.invokeRecord(request.query);
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
fastify.post(
|
|
143
|
+
`${options.prefix}/remove`,
|
|
144
|
+
{
|
|
145
|
+
onRequest: [userAuthenticate, adminAuthenticate],
|
|
146
|
+
schema: {
|
|
147
|
+
summary: '删除webhook',
|
|
148
|
+
body: {
|
|
149
|
+
type: 'object',
|
|
150
|
+
properties: {
|
|
151
|
+
id: { type: 'string' }
|
|
152
|
+
},
|
|
153
|
+
required: ['id']
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
async request => {
|
|
158
|
+
return services.remove(request.body);
|
|
159
|
+
}
|
|
160
|
+
);
|
|
119
161
|
});
|
package/libs/services/main.js
CHANGED
|
@@ -30,11 +30,12 @@ module.exports = fp(async (fastify, options) => {
|
|
|
30
30
|
const list = async () => {
|
|
31
31
|
const list = await models.webhook.findAll();
|
|
32
32
|
const mapping = groupBy(
|
|
33
|
-
list.map(
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
list.map(webhook => {
|
|
34
|
+
const signature = crypto
|
|
35
|
+
.createHash('md5')
|
|
36
|
+
.update(`${webhook.name}|${webhook.expire ? webhook.expire.getTime() : 0}`)
|
|
37
|
+
.digest('hex');
|
|
38
|
+
return Object.assign({}, webhook.get({ plain: true }), {
|
|
38
39
|
signature
|
|
39
40
|
});
|
|
40
41
|
}),
|
|
@@ -49,10 +50,17 @@ module.exports = fp(async (fastify, options) => {
|
|
|
49
50
|
);
|
|
50
51
|
};
|
|
51
52
|
|
|
52
|
-
const invokeRecord = async ({ id }) => {
|
|
53
|
-
|
|
54
|
-
where: { webhookId: id }
|
|
53
|
+
const invokeRecord = async ({ id, currentPage, perPage }) => {
|
|
54
|
+
const { rows, count } = await models.invocation.findAndCountAll({
|
|
55
|
+
where: { webhookId: id },
|
|
56
|
+
limit: perPage,
|
|
57
|
+
offset: (currentPage - 1) * perPage
|
|
55
58
|
});
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
pageData: rows,
|
|
62
|
+
totalCount: count
|
|
63
|
+
};
|
|
56
64
|
};
|
|
57
65
|
|
|
58
66
|
const setStatus = async ({ id, status }) => {
|
|
@@ -77,9 +85,11 @@ module.exports = fp(async (fastify, options) => {
|
|
|
77
85
|
let currentWebhook = null;
|
|
78
86
|
for (const webhook of list) {
|
|
79
87
|
const signature = get({ headers, body, query }, webhook.signatureLocation);
|
|
80
|
-
const
|
|
81
|
-
|
|
82
|
-
|
|
88
|
+
const md5 = crypto
|
|
89
|
+
.createHash('md5')
|
|
90
|
+
.update(`${webhook.name}|${webhook.expire ? webhook.expire.getTime() : 0}`)
|
|
91
|
+
.digest('hex');
|
|
92
|
+
if (md5 === signature) {
|
|
83
93
|
currentWebhook = webhook;
|
|
84
94
|
break;
|
|
85
95
|
}
|