@@ -6,22 +6,66 @@ const ObjectId = require('mongodb').ObjectId;
6
6
const tools = require ( '../tools' ) ;
7
7
const roles = require ( '../roles' ) ;
8
8
const { nextPageCursorSchema, previousPageCursorSchema, pageNrSchema, sessSchema, sessIPSchema } = require ( '../schemas' ) ;
9
+ const { successRes, totalRes, pageRes, previousCursorRes, nextCursorRes } = require ( '../schemas/response/general-schemas' ) ;
9
10
10
11
module . exports = ( db , server ) => {
11
12
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
+ } ,
13
60
tools . responseWrapper ( async ( req , res ) => {
14
61
res . charSet ( 'utf-8' ) ;
15
62
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
25
69
} ) ;
26
70
27
71
const result = schema . validate ( req . params , {
@@ -137,18 +181,46 @@ module.exports = (db, server) => {
137
181
) ;
138
182
139
183
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 : [ / s m t p s ? / , / h t t p s ? / ] , 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
+ } ,
141
215
tools . responseWrapper ( async ( req , res ) => {
142
216
res . charSet ( 'utf-8' ) ;
143
217
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 : [ / s m t p s ? / , / h t t p s ? / ] , 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
152
224
} ) ;
153
225
154
226
const result = schema . validate ( req . params , {
@@ -240,14 +312,34 @@ module.exports = (db, server) => {
240
312
) ;
241
313
242
314
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
+ } ,
244
334
tools . responseWrapper ( async ( req , res ) => {
245
335
res . charSet ( 'utf-8' ) ;
246
336
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
251
343
} ) ;
252
344
253
345
const result = schema . validate ( req . params , {
0 commit comments