@@ -5,123 +5,123 @@ const Data = require("./db").getInstance();
5
5
6
6
// remove the system keys from req.body
7
7
const removeNativeKeys = ( req , res , next ) => {
8
- delete req . body . _id ;
9
- delete req . body . _createdOn ;
10
- delete req . body . _updatedOn ;
11
- delete req . body . _collection ;
12
- next ( ) ;
8
+ delete req . body . _id ;
9
+ delete req . body . _createdOn ;
10
+ delete req . body . _updatedOn ;
11
+ delete req . body . _collection ;
12
+ next ( ) ;
13
13
} ;
14
14
15
15
// validator: size of payload should be < 10KB
16
16
const sizeValidator = ( req , res , next ) => {
17
- if ( req . method === "POST" || req . method === "PUT" ) {
18
- if ( Object . keys ( req . body ) . length > 0 ) {
19
- const memorySize = helper . memorySizeOf ( req . body ) ;
20
- req [ "bodySize" ] = memorySize ;
21
-
22
- // memorySize is size in bytes. 10KB => 10 * 1024
23
- if ( memorySize > config . SIZE_LIMIT * 1024 ) {
24
- throwError ( `JSON body is too large. Should be less than ${ config . SIZE_LIMIT } KB` , 413 ) ;
25
- } else if ( Array . isArray ( req . body ) ) {
26
- if ( req . body . length > 1000 ) {
27
- throwError ( "Not more than 1000 records for bulk upload." , 413 ) ;
28
- } else next ( ) ;
29
- } else next ( ) ;
30
- } else throwError ( "Empty body." , 400 ) ;
31
- } else next ( ) ;
17
+ if ( req . method === "POST" || req . method === "PUT" ) {
18
+ if ( Object . keys ( req . body ) . length > 0 ) {
19
+ const memorySize = helper . memorySizeOf ( req . body ) ;
20
+ req [ "bodySize" ] = memorySize ;
21
+
22
+ // memorySize is size in bytes. 10KB => 10 * 1024
23
+ if ( memorySize > config . SIZE_LIMIT * 1024 ) {
24
+ throwError ( `JSON body is too large. Should be less than ${ config . SIZE_LIMIT } KB` , 413 ) ;
25
+ } else if ( Array . isArray ( req . body ) ) {
26
+ if ( req . body . length > 1000 ) {
27
+ throwError ( "Not more than 1000 records for bulk upload." , 413 ) ;
28
+ } else next ( ) ;
29
+ } else next ( ) ;
30
+ } else throwError ( "Empty body." , 400 ) ;
31
+ } else next ( ) ;
32
32
} ;
33
33
34
34
// The Body top level keys should start with an alphabet
35
35
const keysValidator = ( req , res , next ) => {
36
- let validKeys = Array . isArray ( req . body ) ? req . body . every ( helper . isValidKeys ) : helper . isValidKeys ( req . body ) ;
37
- if ( validKeys ) next ( ) ;
38
- else throwError ( "Invalid JSON keys. Keys should start with an alphabet" ) ;
36
+ let validKeys = Array . isArray ( req . body ) ? req . body . every ( helper . isValidKeys ) : helper . isValidKeys ( req . body ) ;
37
+ if ( validKeys ) next ( ) ;
38
+ else throwError ( "Invalid JSON keys. Keys should start with an alphabet" ) ;
39
39
} ;
40
40
41
41
// extract the box, collection, record ids from the path
42
42
const extractParams = ( req , res , next ) => {
43
- const path = req . path ;
44
- const pathParams = path . split ( "/" ) . filter ( ( p ) => ! ! p ) ;
45
- const isHexString = / ^ ( [ 0 - 9 A - F a - f ] ) { 24 } $ / ;
46
- const isValidBoxID = / ^ [ 0 - 9 A - Z a - z _ ] + $ / i;
43
+ const path = req . path ;
44
+ const pathParams = path . split ( "/" ) . filter ( ( p ) => ! ! p ) ;
45
+ const isHexString = / ^ ( [ 0 - 9 A - F a - f ] ) { 24 } $ / ;
46
+ const isValidBoxID = / ^ [ 0 - 9 A - Z a - z _ ] + $ / i;
47
47
48
- req [ "apiKey" ] =
49
- req . headers [ "x-api-key" ] || ( req . headers [ "authorization" ] ? req . headers [ "authorization" ] . split ( " " ) [ 1 ] : null ) ;
48
+ req [ "apiKey" ] =
49
+ req . headers [ "x-api-key" ] || ( req . headers [ "authorization" ] ? req . headers [ "authorization" ] . split ( " " ) [ 1 ] : null ) ;
50
50
51
- if ( pathParams [ 0 ] ) {
52
- req [ "box" ] = isValidBoxID . test ( pathParams [ 0 ] ) ? pathParams [ 0 ] : undefined ;
51
+ if ( pathParams [ 0 ] ) {
52
+ req [ "box" ] = isValidBoxID . test ( pathParams [ 0 ] ) ? pathParams [ 0 ] : undefined ;
53
53
54
- if ( pathParams [ 1 ] ) {
55
- const isObjectId = isHexString . test ( pathParams [ 1 ] ) ;
56
- if ( isObjectId ) req [ "recordId" ] = pathParams [ 1 ] ;
57
- else req [ "collection" ] = isValidBoxID . test ( pathParams [ 1 ] ) ? pathParams [ 1 ] : undefined ;
58
- }
54
+ if ( pathParams [ 1 ] ) {
55
+ const isObjectId = isHexString . test ( pathParams [ 1 ] ) ;
56
+ if ( isObjectId ) req [ "recordId" ] = pathParams [ 1 ] ;
57
+ else req [ "collection" ] = isValidBoxID . test ( pathParams [ 1 ] ) ? pathParams [ 1 ] : undefined ;
58
+ }
59
59
60
- if ( ! req [ "recordId" ] && pathParams [ 2 ] ) {
61
- req [ "recordId" ] = isHexString . test ( pathParams [ 2 ] ) ? pathParams [ 2 ] : undefined ;
62
- }
60
+ if ( ! req [ "recordId" ] && pathParams [ 2 ] ) {
61
+ req [ "recordId" ] = isHexString . test ( pathParams [ 2 ] ) ? pathParams [ 2 ] : undefined ;
62
+ }
63
63
64
- next ( ) ;
65
- } else throwError ( "Box id cannot be empty." ) ;
64
+ next ( ) ;
65
+ } else throwError ( "Box id cannot be empty." ) ;
66
66
} ;
67
67
68
68
// check if all the required parameters is present
69
69
const validateParams = ( req , res , next ) => {
70
- const uuidRegex = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 5 ] [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i;
71
-
72
- if ( ! req . box ) {
73
- throwError ( "Invalid or empty box id" ) ;
74
- } else if ( req . box . length < 20 || req . box . length > 64 ) {
75
- throwError ( "Box id must be atleast 20 chars long & max. 64 chars." ) ;
76
- } else if ( req . collection ? req . collection . length > 20 : false ) {
77
- throwError ( "Collection name can't be more than 20 chars." ) ;
78
- } else if ( req . method === "PUT" || req . method === "DELETE" ) {
79
- if ( ! req . recordId && ! req . query . q ) {
80
- throwError ( "Invalid or empty record id or missing query definition" ) ;
81
- } else if ( Array . isArray ( req . body ) ) {
82
- throwError ( "Bulk update not supported." ) ;
83
- } else next ( ) ;
84
- } else if ( req . apiKey ) {
85
- if ( uuidRegex . test ( req [ "apiKey" ] ) ) next ( ) ;
86
- else throwError ( "Invalid API-KEY. API-KEY Should be a UUID." ) ;
87
- } else next ( ) ;
70
+ const uuidRegex = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 1 - 5 ] [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / i;
71
+
72
+ if ( ! req . box ) {
73
+ throwError ( "Invalid or empty box id" ) ;
74
+ } else if ( req . box . length < 20 || req . box . length > 64 ) {
75
+ throwError ( "Box id must be atleast 20 chars long & max. 64 chars." ) ;
76
+ } else if ( req . collection ? req . collection . length > 20 : false ) {
77
+ throwError ( "Collection name can't be more than 20 chars." ) ;
78
+ } else if ( req . method === "PUT" || req . method === "DELETE" ) {
79
+ if ( ! req . recordId && ! req . query . q ) {
80
+ throwError ( "Invalid or empty record id or missing query definition" ) ;
81
+ } else if ( Array . isArray ( req . body ) ) {
82
+ throwError ( "Bulk update not supported." ) ;
83
+ } else next ( ) ;
84
+ } else if ( req . apiKey ) {
85
+ if ( uuidRegex . test ( req [ "apiKey" ] ) ) next ( ) ;
86
+ else throwError ( "Invalid API-KEY. API-KEY Should be a UUID." ) ;
87
+ } else next ( ) ;
88
88
} ;
89
89
90
90
// Check if the Request has a valid API_KEY
91
91
const authenticateRequest = async ( req , res , next ) => {
92
- try {
93
- if ( req . method === "POST" || req . method === "PUT" || req . method === "DELETE" ) {
94
- const firstRecord = await Data . findOne ( { _box : req . box } )
95
- . select ( "_apiKey" )
96
- . sort ( "-_createdOn" )
97
- . exec ( ) ;
98
- if ( firstRecord ) {
99
- if ( firstRecord . _apiKey ) {
100
- if ( firstRecord . _apiKey == req [ "apiKey" ] ) next ( ) ;
101
- else throwError ( "Invalid API_KEY." , 401 ) ;
102
- } else {
103
- // dont pass API_KEY if the first data does not have key
104
- req [ "apiKey" ] = null ;
105
- next ( ) ;
106
- }
107
- } else next ( ) ;
108
- } else next ( ) ;
109
- } catch ( error ) {
110
- next ( error ) ;
111
- }
92
+ try {
93
+ if ( req . method === "POST" || req . method === "PUT" || req . method === "DELETE" ) {
94
+ const firstRecord = await Data . findOne ( { _box : req . box } )
95
+ . select ( "_apiKey" )
96
+ . sort ( "-_createdOn" )
97
+ . exec ( ) ;
98
+ if ( firstRecord ) {
99
+ if ( firstRecord . _apiKey ) {
100
+ if ( firstRecord . _apiKey == req [ "apiKey" ] ) next ( ) ;
101
+ else throwError ( "Invalid API_KEY." , 401 ) ;
102
+ } else {
103
+ // dont pass API_KEY if the first data does not have key
104
+ req [ "apiKey" ] = null ;
105
+ next ( ) ;
106
+ }
107
+ } else next ( ) ;
108
+ } else next ( ) ;
109
+ } catch ( error ) {
110
+ next ( error ) ;
111
+ }
112
112
} ;
113
113
114
114
const throwError = ( message , code = 400 ) => {
115
- const errorObject = new Error ( message ) ;
116
- errorObject . statusCode = code ;
117
- throw errorObject ;
115
+ const errorObject = new Error ( message ) ;
116
+ errorObject . statusCode = code ;
117
+ throw errorObject ;
118
118
} ;
119
119
120
120
module . exports = {
121
- removeNativeKeys,
122
- sizeValidator,
123
- keysValidator,
124
- extractParams,
125
- validateParams,
126
- authenticateRequest,
121
+ removeNativeKeys,
122
+ sizeValidator,
123
+ keysValidator,
124
+ extractParams,
125
+ validateParams,
126
+ authenticateRequest,
127
127
} ;
0 commit comments