Skip to content

Commit e9abf85

Browse files
authored
fix(api-webhooks): added all webhook api endpoints to api docs generation ZMS-153 (#686)
* Added submission api endpoint to api docs generation * added Delete a webhook api endpoint to api docs generation * added Create new Webhook api endpoint to api docs generation * Added List registered webhooks api endpoint to api docs generation
1 parent ca57ea4 commit e9abf85

File tree

1 file changed

+116
-24
lines changed

1 file changed

+116
-24
lines changed

lib/api/webhooks.js

+116-24
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,66 @@ const ObjectId = require('mongodb').ObjectId;
66
const tools = require('../tools');
77
const roles = require('../roles');
88
const { nextPageCursorSchema, previousPageCursorSchema, pageNrSchema, sessSchema, sessIPSchema } = require('../schemas');
9+
const { successRes, totalRes, pageRes, previousCursorRes, nextCursorRes } = require('../schemas/response/general-schemas');
910

1011
module.exports = (db, server) => {
1112
server.get(
12-
{ name: 'webhooks', path: '/webhooks' },
13+
{
14+
name: 'webhooks',
15+
path: '/webhooks',
16+
tags: ['Webhooks'],
17+
summary: 'List registered Webhooks',
18+
validationObjs: {
19+
requestBody: {},
20+
queryParams: {
21+
type: Joi.string()
22+
.empty('')
23+
.lowercase()
24+
.max(128)
25+
.description('Prefix or exact match. Prefix match must end with ".*", eg "channel.*". Use "*" for all types'),
26+
user: Joi.string().hex().lowercase().length(24).description('User ID'),
27+
limit: Joi.number().default(20).min(1).max(250).description('How many records to return'),
28+
next: nextPageCursorSchema,
29+
previous: previousPageCursorSchema,
30+
page: pageNrSchema,
31+
sess: sessSchema,
32+
ip: sessIPSchema
33+
},
34+
pathParams: {},
35+
response: {
36+
200: {
37+
description: 'Success',
38+
model: Joi.object({
39+
success: successRes,
40+
total: totalRes,
41+
page: pageRes,
42+
previousCursor: previousCursorRes,
43+
nextCursor: nextCursorRes,
44+
results: Joi.array()
45+
.items(
46+
Joi.object({
47+
id: Joi.string().required().description('Webhooks unique ID (24 byte hex)'),
48+
type: Joi.array().items(Joi.string()).required().description('An array of event types this webhook matches'),
49+
user: Joi.string().required().description('User ID or null'),
50+
url: Joi.string().required().description('Webhook URL')
51+
}).$_setFlag('objectName', 'GetWebhooksResult')
52+
)
53+
.required()
54+
.description('Webhook listing')
55+
})
56+
}
57+
}
58+
}
59+
},
1360
tools.responseWrapper(async (req, res) => {
1461
res.charSet('utf-8');
1562

16-
const schema = Joi.object().keys({
17-
type: Joi.string().empty('').lowercase().max(128),
18-
user: Joi.string().hex().lowercase().length(24),
19-
limit: Joi.number().default(20).min(1).max(250),
20-
next: nextPageCursorSchema,
21-
previous: previousPageCursorSchema,
22-
page: pageNrSchema,
23-
sess: sessSchema,
24-
ip: sessIPSchema
63+
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;
64+
65+
const schema = Joi.object({
66+
...pathParams,
67+
...requestBody,
68+
...queryParams
2569
});
2670

2771
const result = schema.validate(req.params, {
@@ -137,18 +181,46 @@ module.exports = (db, server) => {
137181
);
138182

139183
server.post(
140-
'/webhooks',
184+
{
185+
path: '/webhooks',
186+
tags: ['Webhooks'],
187+
summary: 'Create new Webhook',
188+
validationObjs: {
189+
requestBody: {
190+
type: Joi.array()
191+
.items(Joi.string().trim().max(128).lowercase())
192+
.required()
193+
.description('An array of event types to match. For prefix match use ".*" at the end (eg. "user.*") or "*" for all types'),
194+
user: Joi.string().hex().lowercase().length(24).description('User ID to match (only makes sense for user specific resources)'),
195+
url: Joi.string()
196+
.uri({ scheme: [/smtps?/, /https?/], allowRelative: false, relativeOnly: false })
197+
.required()
198+
.description('URL to POST data to'),
199+
sess: sessSchema,
200+
ip: sessIPSchema
201+
},
202+
queryParams: {},
203+
pathParams: {},
204+
response: {
205+
200: {
206+
description: 'Success',
207+
model: Joi.object({
208+
success: successRes,
209+
id: Joi.string().required().description('ID of the Webhook')
210+
})
211+
}
212+
}
213+
}
214+
},
141215
tools.responseWrapper(async (req, res) => {
142216
res.charSet('utf-8');
143217

144-
const schema = Joi.object().keys({
145-
type: Joi.array().items(Joi.string().trim().max(128).lowercase()).required(),
146-
user: Joi.string().hex().lowercase().length(24),
147-
url: Joi.string()
148-
.uri({ scheme: [/smtps?/, /https?/], allowRelative: false, relativeOnly: false })
149-
.required(),
150-
sess: sessSchema,
151-
ip: sessIPSchema
218+
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;
219+
220+
const schema = Joi.object({
221+
...pathParams,
222+
...requestBody,
223+
...queryParams
152224
});
153225

154226
const result = schema.validate(req.params, {
@@ -240,14 +312,34 @@ module.exports = (db, server) => {
240312
);
241313

242314
server.del(
243-
'/webhooks/:webhook',
315+
{
316+
path: '/webhooks/:webhook',
317+
tags: ['Webhooks'],
318+
summary: 'Delete a webhook',
319+
validationObjs: {
320+
requestBody: {},
321+
queryParams: {
322+
sess: sessSchema,
323+
ip: sessIPSchema
324+
},
325+
pathParams: { webhook: Joi.string().hex().lowercase().length(24).required().description('ID of the Webhook') },
326+
response: {
327+
200: {
328+
description: 'Success',
329+
model: Joi.object({ success: successRes })
330+
}
331+
}
332+
}
333+
},
244334
tools.responseWrapper(async (req, res) => {
245335
res.charSet('utf-8');
246336

247-
const schema = Joi.object().keys({
248-
webhook: Joi.string().hex().lowercase().length(24).required(),
249-
sess: sessSchema,
250-
ip: sessIPSchema
337+
const { pathParams, requestBody, queryParams } = req.route.spec.validationObjs;
338+
339+
const schema = Joi.object({
340+
...pathParams,
341+
...requestBody,
342+
...queryParams
251343
});
252344

253345
const result = schema.validate(req.params, {

0 commit comments

Comments
 (0)