Init webhooks

This commit is contained in:
Alexandre Bodin 2019-12-13 17:59:07 +01:00
parent 4dbbf5722b
commit d390d569d2
2 changed files with 174 additions and 0 deletions

View File

@ -73,6 +73,36 @@
"method": "POST",
"path": "/auth/reset-password",
"handler": "Auth.changePassword"
},
{
"method": "GET",
"path": "/webhooks",
"handler": "Webhooks.listWebhooks"
},
{
"method": "POST",
"path": "/webhooks",
"handler": "Webhooks.createWebhook"
},
{
"method": "GET",
"path": "/webhooks/:id",
"handler": "Webhooks.getWebhook"
},
{
"method": "PUT",
"path": "/webhooks/:id",
"handler": "Webhooks.updateWebhook"
},
{
"method": "DELETE",
"path": "/webhooks/:id",
"handler": "Webhooks.deleteWebhook"
},
{
"method": "POST",
"path": "/webhooks/:id/trigger",
"handler": "Webhooks.triggerWebhook"
}
]
}

View File

@ -0,0 +1,144 @@
'use strict';
// TODO: install uuid
const uuid = require('uuid');
const webhookHandler = {
async findWebhooks() {
const row = await strapi.query('core_store').findOne({
key: 'webhooks',
});
return row ? JSON.parse(row.value) : [];
},
async findWebhook(id) {
const webhooks = await this.findWebhooks();
return webhooks.find(hook => hook.id === id);
},
async createWebhook(data) {
const webhooks = await this.findWebhooks();
const webhook = {
id: uuid(),
...data,
};
const res = await strapi.query('core_store').findOne({
key: 'webhooks',
});
if (!res) {
await strapi
.query('core_store')
.create({ key: 'webhooks', value: JSON.stringify([webhook]) });
} else {
await strapi
.query('core_store')
.update(
{ key: 'webhooks' },
{ value: JSON.stringify(webhooks.concat(webhook)) }
);
}
return webhook;
},
async updateWebhook(id, data) {
const oldWebhook = await this.findWebhook(id);
const webhooks = await this.findWebhooks();
const updatedWebhook = {
...oldWebhook,
...data,
};
const updatedWebhooks = webhooks.map(webhook => {
if (webhook.id === id) {
return updatedWebhook;
}
return webhook;
});
await strapi
.query('core_store')
.update({ key: 'webhooks' }, { value: JSON.stringify(updatedWebhooks) });
return updatedWebhook;
},
async deleteWebhook(id) {
const webhooks = await this.findWebhooks();
const updatedWebhooks = webhooks.filter(webhook => webhook.id !== id);
await strapi
.query('core_store')
.update({ key: 'webhooks' }, { value: JSON.stringify(updatedWebhooks) });
},
};
module.exports = {
async listWebhooks(ctx) {
const webhooks = await webhookHandler.findWebhooks();
ctx.body = { data: webhooks };
},
async createWebhook(ctx) {
const { name, url, headers, events } = ctx.request.body;
const webhook = await webhookHandler.createWebhook({
name,
url,
headers,
events,
});
ctx.body = { data: webhook };
},
async getWebhook(ctx) {
const { id } = ctx.params;
const webhook = await webhookHandler.findWebhook(id);
ctx.body = { data: webhook };
},
async updateWebhook(ctx) {
const { id } = ctx.params;
const { name, url, headers, events } = ctx.request.body;
const webhook = await webhookHandler.findWebhook(id);
if (!webhook) {
return ctx.send({ error: 'webhook.notFound' }, 404);
}
const updatedWebhook = await webhookHandler.updateWebhook(id, {
name,
url,
headers,
events,
});
ctx.body = { data: updatedWebhook };
},
async deleteWebhook(ctx) {
const { id } = ctx.params;
const webhook = await webhookHandler.findWebhook(id);
if (!webhook) {
return ctx.send({ error: 'webhook.notFound' }, 404);
}
await webhookHandler.deleteWebhook(id);
ctx.body = { data: webhook };
},
triggerWebhook(ctx) {
ctx.body = { data: {} };
},
};