@kne/fastify-webhook 0.1.3-alpha.1 → 0.1.3-alpha.4

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.
@@ -63,6 +63,9 @@ module.exports = fp(async (fastify, options) => {
63
63
  fastify.post(
64
64
  `${options.prefix}/invoke/:type`,
65
65
  {
66
+ config: {
67
+ rawBody: true
68
+ },
66
69
  schema: {
67
70
  summary: '调用webhook post请求',
68
71
  params: {
@@ -76,13 +79,14 @@ module.exports = fp(async (fastify, options) => {
76
79
  },
77
80
  async request => {
78
81
  const { type } = request.params;
82
+
79
83
  return services.invoke(
80
84
  Object.assign(
81
85
  {},
82
86
  { type },
83
87
  {
84
88
  body: request.body,
85
- rawBody: request[options.rawBodyField || 'rowBody'],
89
+ rawBody: request[options.rawBodyField || 'rawBody'],
86
90
  headers: request.headers,
87
91
  query: request.query
88
92
  }
@@ -112,8 +116,6 @@ module.exports = fp(async (fastify, options) => {
112
116
  {},
113
117
  { type },
114
118
  {
115
- body: request.body,
116
- rawBody: request[options.rawBodyField || 'rowBody'],
117
119
  headers: request.headers,
118
120
  query: request.query
119
121
  }
@@ -134,8 +136,7 @@ module.exports = fp(async (fastify, options) => {
134
136
  id: { type: 'string' },
135
137
  currentPage: { type: 'number', default: 1 },
136
138
  perPage: { type: 'number', default: 10 }
137
- },
138
- required: ['id']
139
+ }
139
140
  }
140
141
  }
141
142
  },
@@ -39,14 +39,14 @@ module.exports = ({ DataTypes, options }) => {
39
39
  defaultValue: 'open'
40
40
  }
41
41
  },
42
- associate: ({ webhook }) => {
43
- webhook.belongsTo(options.getUserModel(), {
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'],
@@ -24,10 +24,9 @@ module.exports = ({ DataTypes, options }) => {
24
24
  comment: '调用结果'
25
25
  }
26
26
  },
27
- associate: ({ invocation, webhook }) => {
28
- invocation.belongsTo(webhook, {
29
- allowNull: false,
30
- comment: 'webhook'
27
+ associate: ({ invocation, client }) => {
28
+ invocation.belongsTo(client, {
29
+ comment: 'webhook调用端'
31
30
  });
32
31
  },
33
32
  options: {
@@ -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.webhook.create({
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.webhook.findByPk(id);
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.webhook.findAll();
44
+ const list = await models.client.findAll();
44
45
  const mapping = groupBy(
45
46
  list.map(webhook => {
46
47
  const signature = crypto
@@ -64,7 +65,11 @@ module.exports = fp(async (fastify, options) => {
64
65
 
65
66
  const invokeRecord = async ({ id, currentPage, perPage }) => {
66
67
  const { rows, count } = await models.invocation.findAndCountAll({
67
- where: { webhookId: id },
68
+ where: id
69
+ ? { webhookClientId: id }
70
+ : {
71
+ webhookClientId: { [Op.is]: null }
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.webhook.findAll({ where: { type } });
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,29 @@ 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
+ status: 'failed',
137
+ startTime,
138
+ endTime: new Date()
139
+ });
122
140
  throw new Error('Webhook not registered');
123
141
  }
124
142
  const input = { headers, body, query }[currentWebhook.inputLocation];
125
- const startTime = new Date();
126
143
  try {
127
144
  const result = await options.hooks[currentWebhook.type]({ input });
128
145
  await models.invocation.create({
129
146
  input,
130
147
  result,
131
- webhookId: currentWebhook.id,
148
+ webhookClientId: currentWebhook.id,
132
149
  status: 'success',
133
150
  startTime,
134
151
  endTime: new Date()
@@ -138,7 +155,7 @@ module.exports = fp(async (fastify, options) => {
138
155
  await models.invocation.create({
139
156
  input,
140
157
  result: e.toString(),
141
- webhookId: currentWebhook.id,
158
+ webhookClientId: currentWebhook.id,
142
159
  status: 'failed',
143
160
  startTime,
144
161
  endTime: new Date()
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-webhook",
3
- "version": "0.1.3-alpha.1",
3
+ "version": "0.1.3-alpha.4",
4
4
  "description": "实现一个webhook功能.",
5
5
  "main": "index.js",
6
6
  "scripts": {