@kne/fastify-webhook 0.1.3-alpha.2 → 0.1.3-alpha.5
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
CHANGED
|
@@ -134,10 +134,10 @@ module.exports = fp(async (fastify, options) => {
|
|
|
134
134
|
type: 'object',
|
|
135
135
|
properties: {
|
|
136
136
|
id: { type: 'string' },
|
|
137
|
+
type: { type: 'string' },
|
|
137
138
|
currentPage: { type: 'number', default: 1 },
|
|
138
139
|
perPage: { type: 'number', default: 10 }
|
|
139
|
-
}
|
|
140
|
-
required: ['id']
|
|
140
|
+
}
|
|
141
141
|
}
|
|
142
142
|
}
|
|
143
143
|
},
|
|
@@ -39,14 +39,14 @@ module.exports = ({ DataTypes, options }) => {
|
|
|
39
39
|
defaultValue: 'open'
|
|
40
40
|
}
|
|
41
41
|
},
|
|
42
|
-
associate: ({
|
|
43
|
-
|
|
42
|
+
associate: ({ client }) => {
|
|
43
|
+
client.belongsTo(options.getUserModel(), {
|
|
44
44
|
foreignKey: 'userId',
|
|
45
45
|
comment: '创建人'
|
|
46
46
|
});
|
|
47
47
|
},
|
|
48
48
|
options: {
|
|
49
|
-
comment: 'webhook',
|
|
49
|
+
comment: 'webhook调用客户端',
|
|
50
50
|
indexes: [
|
|
51
51
|
{
|
|
52
52
|
fields: ['name', 'type', 'deleted_at'],
|
|
@@ -15,6 +15,10 @@ module.exports = ({ DataTypes, options }) => {
|
|
|
15
15
|
type: DataTypes.JSON,
|
|
16
16
|
comment: '调用参数'
|
|
17
17
|
},
|
|
18
|
+
type: {
|
|
19
|
+
type: DataTypes.STRING,
|
|
20
|
+
comment: '调用类型'
|
|
21
|
+
},
|
|
18
22
|
status: {
|
|
19
23
|
type: DataTypes.ENUM('success', 'failed'),
|
|
20
24
|
comment: '调用返回状态'
|
|
@@ -24,10 +28,9 @@ module.exports = ({ DataTypes, options }) => {
|
|
|
24
28
|
comment: '调用结果'
|
|
25
29
|
}
|
|
26
30
|
},
|
|
27
|
-
associate: ({ invocation,
|
|
28
|
-
invocation.belongsTo(
|
|
29
|
-
|
|
30
|
-
comment: 'webhook'
|
|
31
|
+
associate: ({ invocation, client }) => {
|
|
32
|
+
invocation.belongsTo(client, {
|
|
33
|
+
comment: 'webhook调用端'
|
|
31
34
|
});
|
|
32
35
|
},
|
|
33
36
|
options: {
|
package/libs/services/main.js
CHANGED
|
@@ -15,12 +15,13 @@ function generateSignature(key, body) {
|
|
|
15
15
|
|
|
16
16
|
module.exports = fp(async (fastify, options) => {
|
|
17
17
|
const { models } = fastify[options.name];
|
|
18
|
+
const { Op } = fastify.sequelize.Sequelize;
|
|
18
19
|
|
|
19
20
|
const create = async ({ name, type, expire, userId, signatureLocation, inputLocation, shouldEncryptVerify }) => {
|
|
20
21
|
if (!options.hooks[type]) {
|
|
21
22
|
throw new Error(`Hook ${type} not allowed`);
|
|
22
23
|
}
|
|
23
|
-
return await models.
|
|
24
|
+
return await models.client.create({
|
|
24
25
|
name,
|
|
25
26
|
type,
|
|
26
27
|
expire,
|
|
@@ -32,7 +33,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
32
33
|
};
|
|
33
34
|
|
|
34
35
|
const detail = async ({ id }) => {
|
|
35
|
-
const webhook = await models.
|
|
36
|
+
const webhook = await models.client.findByPk(id);
|
|
36
37
|
if (!webhook) {
|
|
37
38
|
throw new Error('Webhook not found');
|
|
38
39
|
}
|
|
@@ -40,7 +41,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
40
41
|
};
|
|
41
42
|
|
|
42
43
|
const list = async () => {
|
|
43
|
-
const list = await models.
|
|
44
|
+
const list = await models.client.findAll();
|
|
44
45
|
const mapping = groupBy(
|
|
45
46
|
list.map(webhook => {
|
|
46
47
|
const signature = crypto
|
|
@@ -62,9 +63,13 @@ module.exports = fp(async (fastify, options) => {
|
|
|
62
63
|
);
|
|
63
64
|
};
|
|
64
65
|
|
|
65
|
-
const invokeRecord = async ({ id, currentPage, perPage }) => {
|
|
66
|
+
const invokeRecord = async ({ id, type, currentPage, perPage }) => {
|
|
66
67
|
const { rows, count } = await models.invocation.findAndCountAll({
|
|
67
|
-
where:
|
|
68
|
+
where: id
|
|
69
|
+
? { webhookClientId: id }
|
|
70
|
+
: {
|
|
71
|
+
webhookClientId: { [Op.is]: null, type }
|
|
72
|
+
},
|
|
68
73
|
limit: perPage,
|
|
69
74
|
offset: (currentPage - 1) * perPage
|
|
70
75
|
});
|
|
@@ -88,7 +93,7 @@ module.exports = fp(async (fastify, options) => {
|
|
|
88
93
|
|
|
89
94
|
const invoke = async ({ type, headers, body, rawBody, query }) => {
|
|
90
95
|
//signature, input
|
|
91
|
-
const list = await models.
|
|
96
|
+
const list = await models.client.findAll({ where: { type } });
|
|
92
97
|
|
|
93
98
|
if (list.length === 0) {
|
|
94
99
|
throw new Error('Webhook not found');
|
|
@@ -118,17 +123,31 @@ module.exports = fp(async (fastify, options) => {
|
|
|
118
123
|
}
|
|
119
124
|
}
|
|
120
125
|
}
|
|
126
|
+
|
|
127
|
+
const startTime = new Date();
|
|
121
128
|
if (!currentWebhook) {
|
|
129
|
+
await models.invocation.create({
|
|
130
|
+
input: {
|
|
131
|
+
headers,
|
|
132
|
+
body,
|
|
133
|
+
rawBody,
|
|
134
|
+
query
|
|
135
|
+
},
|
|
136
|
+
type,
|
|
137
|
+
status: 'failed',
|
|
138
|
+
startTime,
|
|
139
|
+
endTime: new Date()
|
|
140
|
+
});
|
|
122
141
|
throw new Error('Webhook not registered');
|
|
123
142
|
}
|
|
124
143
|
const input = { headers, body, query }[currentWebhook.inputLocation];
|
|
125
|
-
const startTime = new Date();
|
|
126
144
|
try {
|
|
127
145
|
const result = await options.hooks[currentWebhook.type]({ input });
|
|
128
146
|
await models.invocation.create({
|
|
129
147
|
input,
|
|
148
|
+
type,
|
|
130
149
|
result,
|
|
131
|
-
|
|
150
|
+
webhookClientId: currentWebhook.id,
|
|
132
151
|
status: 'success',
|
|
133
152
|
startTime,
|
|
134
153
|
endTime: new Date()
|
|
@@ -137,8 +156,9 @@ module.exports = fp(async (fastify, options) => {
|
|
|
137
156
|
} catch (e) {
|
|
138
157
|
await models.invocation.create({
|
|
139
158
|
input,
|
|
159
|
+
type,
|
|
140
160
|
result: e.toString(),
|
|
141
|
-
|
|
161
|
+
webhookClientId: currentWebhook.id,
|
|
142
162
|
status: 'failed',
|
|
143
163
|
startTime,
|
|
144
164
|
endTime: new Date()
|