| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  | const _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2021-04-29 13:51:12 +02:00
										 |  |  | const { yup, formatYupErrors, webhook: webhookUtils } = require('@strapi/utils'); | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-22 10:49:55 +02:00
										 |  |  | const urlRegex = /^(?:([a-z0-9+.-]+):\/\/)(?:\S+(?::\S*)?@)?(?:(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9_]-*)*[a-z\u00a1-\uffff0-9_]+)(?:\.(?:[a-z\u00a1-\uffff0-9_]-*)*[a-z\u00a1-\uffff0-9_]+)*\.?)(?::\d{2,5})?(?:[/?#]\S*)?$/; | 
					
						
							| 
									
										
										
										
											2020-01-08 11:33:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  | const webhookValidator = yup | 
					
						
							|  |  |  |   .object({ | 
					
						
							|  |  |  |     name: yup.string().required(), | 
					
						
							| 
									
										
										
										
											2020-01-08 11:33:59 +01:00
										 |  |  |     url: yup | 
					
						
							|  |  |  |       .string() | 
					
						
							|  |  |  |       .matches(urlRegex, 'url must be a valid URL') | 
					
						
							|  |  |  |       .required(), | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     headers: yup.lazy(data => { | 
					
						
							|  |  |  |       if (typeof data !== 'object') { | 
					
						
							|  |  |  |         return yup.object().required(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return yup | 
					
						
							|  |  |  |         .object( | 
					
						
							|  |  |  |           _.mapValues(data, () => { | 
					
						
							|  |  |  |             yup | 
					
						
							|  |  |  |               .string() | 
					
						
							|  |  |  |               .min(1) | 
					
						
							|  |  |  |               .required(); | 
					
						
							|  |  |  |           }) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         .required(); | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |     events: yup | 
					
						
							|  |  |  |       .array() | 
					
						
							|  |  |  |       .of( | 
					
						
							|  |  |  |         yup | 
					
						
							|  |  |  |           .string() | 
					
						
							| 
									
										
										
										
											2020-08-31 16:23:43 +02:00
										 |  |  |           .oneOf(_.values(webhookUtils.webhookEvents)) | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |           .required() | 
					
						
							|  |  |  |       ) | 
					
						
							|  |  |  |       .min(1) | 
					
						
							|  |  |  |       .required(), | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  |   .noUnknown(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const updateWebhookValidator = webhookValidator.shape({ | 
					
						
							|  |  |  |   isEnabled: yup.boolean(), | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  | module.exports = { | 
					
						
							|  |  |  |   async listWebhooks(ctx) { | 
					
						
							| 
									
										
										
										
											2019-12-17 11:24:14 +01:00
										 |  |  |     const webhooks = await strapi.webhookStore.findWebhooks(); | 
					
						
							| 
									
										
										
										
											2019-12-18 10:36:50 +01:00
										 |  |  |     ctx.send({ data: webhooks }); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async getWebhook(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2019-12-17 11:24:14 +01:00
										 |  |  |     const webhook = await strapi.webhookStore.findWebhook(id); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-23 16:38:42 +01:00
										 |  |  |     if (!webhook) { | 
					
						
							|  |  |  |       return ctx.notFound('webhook.notFound'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 10:36:50 +01:00
										 |  |  |     ctx.send({ data: webhook }); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |   async createWebhook(ctx) { | 
					
						
							|  |  |  |     const { body } = ctx.request; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       await webhookValidator.validate(body, { | 
					
						
							|  |  |  |         strict: true, | 
					
						
							|  |  |  |         abortEarly: false, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							| 
									
										
										
										
											2020-01-07 20:21:03 +01:00
										 |  |  |       return ctx.badRequest('ValidationError', { | 
					
						
							|  |  |  |         errors: formatYupErrors(error), | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const webhook = await strapi.webhookStore.createWebhook(body); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     strapi.webhookRunner.add(webhook); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ctx.created({ data: webhook }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |   async updateWebhook(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  |     const { body } = ctx.request; | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     try { | 
					
						
							|  |  |  |       await updateWebhookValidator.validate(body, { | 
					
						
							|  |  |  |         strict: true, | 
					
						
							|  |  |  |         abortEarly: false, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     } catch (error) { | 
					
						
							| 
									
										
										
										
											2020-01-07 20:21:03 +01:00
										 |  |  |       return ctx.badRequest('ValidationError', { | 
					
						
							|  |  |  |         errors: formatYupErrors(error), | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     const webhook = await strapi.webhookStore.findWebhook(id); | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |     if (!webhook) { | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |       return ctx.notFound('webhook.notFound'); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     const updatedWebhook = await strapi.webhookStore.updateWebhook(id, { | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  |       ...webhook, | 
					
						
							|  |  |  |       ...body, | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     if (!updatedWebhook) { | 
					
						
							|  |  |  |       return ctx.notFound('webhook.notFound'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |     strapi.webhookRunner.update(updatedWebhook); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 10:36:50 +01:00
										 |  |  |     ctx.send({ data: updatedWebhook }); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async deleteWebhook(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							| 
									
										
										
										
											2019-12-17 11:24:14 +01:00
										 |  |  |     const webhook = await strapi.webhookStore.findWebhook(id); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!webhook) { | 
					
						
							| 
									
										
										
										
											2019-12-24 14:49:44 +01:00
										 |  |  |       return ctx.notFound('webhook.notFound'); | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-17 11:24:14 +01:00
										 |  |  |     await strapi.webhookStore.deleteWebhook(id); | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     strapi.webhookRunner.remove(webhook); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |     ctx.body = { data: webhook }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 10:36:50 +01:00
										 |  |  |   async deleteWebhooks(ctx) { | 
					
						
							| 
									
										
										
										
											2019-12-18 14:31:49 +01:00
										 |  |  |     const { ids } = ctx.request.body; | 
					
						
							| 
									
										
										
										
											2019-12-18 10:36:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (!Array.isArray(ids) || ids.length === 0) { | 
					
						
							|  |  |  |       return ctx.badRequest('ids must be an array of id'); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 13:46:39 +01:00
										 |  |  |     for (const id of ids) { | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  |       const webhook = await strapi.webhookStore.findWebhook(id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (!webhook) continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 13:46:39 +01:00
										 |  |  |       await strapi.webhookStore.deleteWebhook(id); | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  |       strapi.webhookRunner.remove(webhook); | 
					
						
							| 
									
										
										
										
											2019-12-18 13:46:39 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-18 10:36:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.send({ data: ids }); | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  |   async triggerWebhook(ctx) { | 
					
						
							|  |  |  |     const { id } = ctx.params; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const webhook = await strapi.webhookStore.findWebhook(id); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-04-15 19:15:21 +02:00
										 |  |  |     const response = await strapi.webhookRunner.run(webhook, 'trigger-test', {}); | 
					
						
							| 
									
										
										
										
											2019-12-18 16:10:42 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ctx.body = { data: response }; | 
					
						
							| 
									
										
										
										
											2019-12-13 17:59:07 +01:00
										 |  |  |   }, | 
					
						
							|  |  |  | }; |