From 254efca8b9d7c5c64652ac1ed54df25dd13add02 Mon Sep 17 00:00:00 2001 From: Nikolai Ovtsinnikov Date: Mon, 6 May 2024 11:55:28 +0300 Subject: [PATCH] Added List registered webhooks api endpoint to api docs generation --- lib/api/webhooks.js | 65 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/lib/api/webhooks.js b/lib/api/webhooks.js index 2e317006..89e893aa 100644 --- a/lib/api/webhooks.js +++ b/lib/api/webhooks.js @@ -6,23 +6,66 @@ const ObjectId = require('mongodb').ObjectId; const tools = require('../tools'); const roles = require('../roles'); const { nextPageCursorSchema, previousPageCursorSchema, pageNrSchema, sessSchema, sessIPSchema } = require('../schemas'); -const { successRes } = require('../schemas/response/general-schemas'); +const { successRes, totalRes, pageRes, previousCursorRes, nextCursorRes } = require('../schemas/response/general-schemas'); module.exports = (db, server) => { server.get( - { name: 'webhooks', path: '/webhooks' }, + { + name: 'webhooks', + path: '/webhooks', + tags: ['Webhooks'], + summary: 'List registered Webhooks', + validationObjs: { + requestBody: {}, + queryParams: { + type: Joi.string() + .empty('') + .lowercase() + .max(128) + .description('Prefix or exact match. Prefix match must end with ".*", eg "channel.*". Use "*" for all types'), + user: Joi.string().hex().lowercase().length(24).description('User ID'), + limit: Joi.number().default(20).min(1).max(250).description('How many records to return'), + next: nextPageCursorSchema, + previous: previousPageCursorSchema, + page: pageNrSchema, + sess: sessSchema, + ip: sessIPSchema + }, + pathParams: {}, + response: { + 200: { + description: 'Success', + model: Joi.object({ + success: successRes, + total: totalRes, + page: pageRes, + previousCursor: previousCursorRes, + nextCursor: nextCursorRes, + results: Joi.array() + .items( + Joi.object({ + id: Joi.string().required().description('Webhooks unique ID (24 byte hex)'), + type: Joi.array().items(Joi.string()).required().description('An array of event types this webhook matches'), + user: Joi.string().required().description('User ID or null'), + url: Joi.string().required().description('Webhook URL') + }).$_setFlag('objectName', 'GetWebhooksResult') + ) + .required() + .description('Webhook listing') + }) + } + } + } + }, tools.responseWrapper(async (req, res) => { res.charSet('utf-8'); - const schema = Joi.object().keys({ - type: Joi.string().empty('').lowercase().max(128), - user: Joi.string().hex().lowercase().length(24), - limit: Joi.number().default(20).min(1).max(250), - next: nextPageCursorSchema, - previous: previousPageCursorSchema, - page: pageNrSchema, - sess: sessSchema, - ip: sessIPSchema + const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs; + + const schema = Joi.object({ + ...pathParams, + ...requestBody, + ...queryParams }); const result = schema.validate(req.params, {