@@ -3,6 +3,7 @@ import internalAccessList from "../../internal/access-list.js";
33import jwtdecode from "../../lib/express/jwt-decode.js" ;
44import apiValidator from "../../lib/validator/api.js" ;
55import validator from "../../lib/validator/index.js" ;
6+ import { express as logger } from "../../logger.js" ;
67import { getValidationSchema } from "../../schema/index.js" ;
78
89const router = express . Router ( {
@@ -26,47 +27,47 @@ router
2627 *
2728 * Retrieve all access-lists
2829 */
29- . get ( ( req , res , next ) => {
30- validator (
31- {
32- additionalProperties : false ,
33- properties : {
34- expand : {
35- $ref : "common#/properties/expand" ,
36- } ,
37- query : {
38- $ref : "common#/properties/query" ,
30+ . get ( async ( req , res , next ) => {
31+ try {
32+ const data = await validator (
33+ {
34+ additionalProperties : false ,
35+ properties : {
36+ expand : {
37+ $ref : "common#/properties/expand" ,
38+ } ,
39+ query : {
40+ $ref : "common#/properties/query" ,
41+ } ,
3942 } ,
4043 } ,
41- } ,
42- {
43- expand : typeof req . query . expand === "string" ? req . query . expand . split ( "," ) : null ,
44- query : typeof req . query . query === "string" ? req . query . query : null ,
45- } ,
46- )
47- . then ( ( data ) => {
48- return internalAccessList . getAll ( res . locals . access , data . expand , data . query ) ;
49- } )
50- . then ( ( rows ) => {
51- res . status ( 200 ) . send ( rows ) ;
52- } )
53- . catch ( next ) ;
44+ {
45+ expand : typeof req . query . expand === "string" ? req . query . expand . split ( "," ) : null ,
46+ query : typeof req . query . query === "string" ? req . query . query : null ,
47+ } ,
48+ ) ;
49+ const rows = await internalAccessList . getAll ( res . locals . access , data . expand , data . query ) ;
50+ res . status ( 200 ) . send ( rows ) ;
51+ } catch ( err ) {
52+ logger . debug ( `${ req . method . toUpperCase ( ) } ${ req . path } : ${ err } ` ) ;
53+ next ( err ) ;
54+ }
5455 } )
5556
5657 /**
5758 * POST /api/nginx/access-lists
5859 *
5960 * Create a new access-list
6061 */
61- . post ( ( req , res , next ) => {
62- apiValidator ( getValidationSchema ( "/nginx/access-lists" , "post" ) , req . body )
63- . then ( ( payload ) => {
64- return internalAccessList . create ( res . locals . access , payload ) ;
65- } )
66- . then ( ( result ) => {
67- res . status ( 201 ) . send ( result ) ;
68- } )
69- . catch ( next ) ;
62+ . post ( async ( req , res , next ) => {
63+ try {
64+ const payload = await apiValidator ( getValidationSchema ( "/nginx/access-lists" , "post" ) , req . body ) ;
65+ const result = await internalAccessList . create ( res . locals . access , payload ) ;
66+ res . status ( 201 ) . send ( result ) ;
67+ } catch ( err ) {
68+ logger . debug ( ` ${ req . method . toUpperCase ( ) } ${ req . path } : ${ err } ` ) ;
69+ next ( err ) ;
70+ }
7071 } ) ;
7172
7273/**
@@ -86,66 +87,69 @@ router
8687 *
8788 * Retrieve a specific access-list
8889 */
89- . get ( ( req , res , next ) => {
90- validator (
91- {
92- required : [ "list_id" ] ,
93- additionalProperties : false ,
94- properties : {
95- list_id : {
96- $ref : "common#/properties/id" ,
97- } ,
98- expand : {
99- $ref : "common#/properties/expand" ,
90+ . get ( async ( req , res , next ) => {
91+ try {
92+ const data = await validator (
93+ {
94+ required : [ "list_id" ] ,
95+ additionalProperties : false ,
96+ properties : {
97+ list_id : {
98+ $ref : "common#/properties/id" ,
99+ } ,
100+ expand : {
101+ $ref : "common#/properties/expand" ,
102+ } ,
100103 } ,
101104 } ,
102- } ,
103- {
104- list_id : req . params . list_id ,
105- expand : typeof req . query . expand === "string" ? req . query . expand . split ( "," ) : null ,
106- } ,
107- )
108- . then ( ( data ) => {
109- return internalAccessList . get ( res . locals . access , {
110- id : Number . parseInt ( data . list_id , 10 ) ,
111- expand : data . expand ,
112- } ) ;
113- } )
114- . then ( ( row ) => {
115- res . status ( 200 ) . send ( row ) ;
116- } )
117- . catch ( next ) ;
105+ {
106+ list_id : req . params . list_id ,
107+ expand : typeof req . query . expand === "string" ? req . query . expand . split ( "," ) : null ,
108+ } ,
109+ ) ;
110+ const row = await internalAccessList . get ( res . locals . access , {
111+ id : Number . parseInt ( data . list_id , 10 ) ,
112+ expand : data . expand ,
113+ } ) ;
114+ res . status ( 200 ) . send ( row ) ;
115+ } catch ( err ) {
116+ logger . debug ( `${ req . method . toUpperCase ( ) } ${ req . path } : ${ err } ` ) ;
117+ next ( err ) ;
118+ }
118119 } )
119120
120121 /**
121122 * PUT /api/nginx/access-lists/123
122123 *
123124 * Update and existing access-list
124125 */
125- . put ( ( req , res , next ) => {
126- apiValidator ( getValidationSchema ( "/nginx/access-lists/{listID}" , "put" ) , req . body )
127- . then ( ( payload ) => {
128- payload . id = Number . parseInt ( req . params . list_id , 10 ) ;
129- return internalAccessList . update ( res . locals . access , payload ) ;
130- } )
131- . then ( ( result ) => {
132- res . status ( 200 ) . send ( result ) ;
133- } )
134- . catch ( next ) ;
126+ . put ( async ( req , res , next ) => {
127+ try {
128+ const payload = await apiValidator ( getValidationSchema ( "/nginx/access-lists/{listID}" , "put" ) , req . body ) ;
129+ payload . id = Number . parseInt ( req . params . list_id , 10 ) ;
130+ const result = await internalAccessList . update ( res . locals . access , payload ) ;
131+ res . status ( 200 ) . send ( result ) ;
132+ } catch ( err ) {
133+ logger . debug ( ` ${ req . method . toUpperCase ( ) } ${ req . path } : ${ err } ` ) ;
134+ next ( err ) ;
135+ }
135136 } )
136137
137138 /**
138139 * DELETE /api/nginx/access-lists/123
139140 *
140141 * Delete and existing access-list
141142 */
142- . delete ( ( req , res , next ) => {
143- internalAccessList
144- . delete ( res . locals . access , { id : Number . parseInt ( req . params . list_id , 10 ) } )
145- . then ( ( result ) => {
146- res . status ( 200 ) . send ( result ) ;
147- } )
148- . catch ( next ) ;
143+ . delete ( async ( req , res , next ) => {
144+ try {
145+ const result = await internalAccessList . delete ( res . locals . access , {
146+ id : Number . parseInt ( req . params . list_id , 10 ) ,
147+ } ) ;
148+ res . status ( 200 ) . send ( result ) ;
149+ } catch ( err ) {
150+ logger . debug ( `${ req . method . toUpperCase ( ) } ${ req . path } : ${ err } ` ) ;
151+ next ( err ) ;
152+ }
149153 } ) ;
150154
151155export default router ;
0 commit comments