109 lines
2.2 KiB
JavaScript
Raw Normal View History

2019-12-13 17:59:07 +01:00
'use strict';
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 createWebhook(ctx) {
const { name, url, headers, events } = ctx.request.body;
// TODO: validate input
2019-12-17 11:24:14 +01:00
const webhook = await strapi.webhookStore.createWebhook({
2019-12-13 17:59:07 +01:00
name,
url,
headers,
events,
});
strapi.webhookRunner.add(webhook);
2019-12-18 10:36:50 +01:00
ctx.created({ data: webhook });
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
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
},
async updateWebhook(ctx) {
const { id } = ctx.params;
const { body } = ctx.request;
2019-12-13 17:59:07 +01:00
2019-12-17 11:24:14 +01:00
const webhook = await strapi.webhookStore.findWebhook(id);
2019-12-13 17:59:07 +01:00
// TODO: validate input
2019-12-13 17:59:07 +01:00
if (!webhook) {
return ctx.send({ error: 'webhook.notFound' }, 404);
}
const updatedWebhook = {
...webhook,
...body,
};
await strapi.webhookStore.updateWebhook(id, updatedWebhook);
strapi.webhookRunner.update(webhook);
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) {
return ctx.send({ error: 'webhook.notFound' }, 404);
}
2019-12-17 11:24:14 +01:00
await strapi.webhookStore.deleteWebhook(id);
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) {
const webhook = await strapi.webhookStore.findWebhook(id);
if (!webhook) continue;
2019-12-18 13:46:39 +01:00
await strapi.webhookStore.deleteWebhook(id);
strapi.webhookRunner.remove(webhook);
2019-12-18 13:46:39 +01:00
}
2019-12-18 10:36:50 +01:00
ctx.send({ data: ids });
},
async triggerWebhook(ctx) {
const { id } = ctx.params;
const webhook = await strapi.webhookStore.findWebhook(id);
const response = await strapi.webhookRunner.run(
webhook,
'trigger-test',
{}
);
ctx.body = { data: response };
2019-12-13 17:59:07 +01:00
},
};