1
1
// import libraries //
2
- const axios = require ( 'axios' ) ;
3
- const Joi = require ( 'joi' ) ;
4
-
5
- // define validation schema for payload processing //
6
- const validationSchema = Joi . object ( {
7
- count : Joi . number ( ) . integer ( ) ,
8
- date : Joi . date ( ) ,
9
- end_date : Joi . date ( ) ,
10
- key : Joi . string ( ) . required ( ) ,
11
- start_date : Joi . date ( ) . less ( Joi . ref ( 'end_date' ) ) ,
12
- thumbs : Joi . boolean ( )
13
- } ) ;
14
-
15
- // NeoWs (Near Earth Object Web Service) //
16
- function asteroids ( payload , callback ) {
2
+ const axios = require ( 'axios' )
3
+ const Joi = require ( 'joi' )
4
+
5
+ // import utilities //
6
+ const dateUtils = require ( '../utils/dates.js' )
17
7
18
- // payload logic validation //
19
- if ( payload . end_date && ! payload . start_date ) throw new Error ( 'Start Date must be included if using End Date' ) ;
8
+ // neows (near earth object web service) //
9
+ function asteroids ( payload , callback ) {
10
+ let request , start , end , key , type ;
20
11
21
- let request ;
22
12
// neo request type is feed //
23
13
if ( payload . type == 'feed' ) {
24
14
15
+ // define validation schema //
16
+ const validationSchema = Joi . object ( {
17
+ end_date : Joi . string ( ) . regex ( / \d { 4 } - \d { 2 } - \d { 2 } / , { name : 'endDate' } ) . optional ( ) ,
18
+ key : Joi . string ( ) . required ( ) ,
19
+ start_date : Joi . string ( ) . regex ( / \d { 4 } - \d { 2 } - \d { 2 } / , { name : 'startDate' } ) . optional ( ) ,
20
+ type : Joi . string ( ) . regex ( / \w { 4 , 6 } / , { name : 'requestType' } ) . required ( ) ,
21
+ } )
22
+
23
+ type = payload . type ;
24
+ key = payload . key ;
25
+
26
+ // validate the payload input //
27
+ const { error } = validationSchema . validate ( payload )
28
+ if ( error ) throw error
29
+
30
+ // payload logic validation //
31
+ if ( payload . end_date && ( payload . start_date > payload . end_date ) ) throw new Error ( 'Start date cannot come after the end date provided' )
32
+ if ( payload . end_date && ! payload . start_date ) throw new Error ( 'Start Date must be included if using End Date' )
33
+
25
34
// function to add days to a given date //
26
35
function addDays ( date , days ) {
27
- const res = new Date ( Number ( date ) ) ;
36
+ let res = new Date ( date ) ;
28
37
res . setDate ( date . getDate ( ) + days ) ;
29
38
return res ;
30
39
}
31
40
32
-
33
41
// calculate seven days from the start date //
34
- let endLimit = addDays ( new Date ( payload . start_date ) , 7 ) ;
42
+ let endLimit = addDays ( dateUtils . formatStringToDate ( payload . start_date ) , 7 ) ;
43
+
44
+ // if there is no current end date, set the end date to the end limit (seven days from the start date) //
45
+ if ( ! payload . end_date ) {
46
+ end = endLimit
47
+ } else {
48
+ end = new Date ( payload . end_date ) ;
49
+ }
35
50
36
51
// throw an error if the end date is later than the cutoff date //
37
- if ( ! ( new Date ( payload . end_date ) < endLimit ) ) throw new Error ( 'Start and End dates cannot be more than seven days apart' )
38
-
39
- // define validation schema //
40
- const validationSchema = Joi . object ( {
41
- end_date : Joi . date ( ) ,
42
- key : Joi . string ( ) . required ( ) ,
43
- start_date : Joi . date ( ) . less ( Joi . ref ( 'end_date' ) ) ,
44
- type : Joi . string ( )
45
- } ) ;
46
-
47
- // validate the payload input //
48
- const { error, value } = validationSchema . validate ( payload ) ;
49
- if ( error ) throw error ;
50
-
51
- // calculate seven days from start_date //
52
+ if ( end > endLimit ) {
53
+ throw new Error ( 'Start and End dates cannot be more than seven days apart' )
54
+ }
52
55
56
+ // define url request parameters //
53
57
let requestURLParameters = `api_key=${ payload . key } ` ;
54
58
if ( payload . start_date ) requestURLParameters = requestURLParameters . concat ( `&start_date=${ payload . start_date } ` ) ;
55
59
if ( payload . end_date ) requestURLParameters = requestURLParameters . concat ( `&end_date=${ payload . end_date } ` ) ;
56
60
61
+ // define request url //
57
62
request = `https://api.nasa.gov/neo/rest/v1/feed?${ requestURLParameters } ` ;
58
- }
59
-
63
+ console . log ( request )
64
+ }
60
65
61
66
// neo request type is lookup //
62
67
else if ( payload . type == 'lookup' ) {
@@ -80,7 +85,6 @@ function asteroids(payload, callback) {
80
85
else if ( payload . type == 'browse' ) {
81
86
let requestURLParameters = `api_key=${ payload . key } ` ;
82
87
request = `https://api.nasa.gov/neo/rest/v1/neo/browse/?${ requestURLParameters } ` ;
83
-
84
88
}
85
89
86
90
// api request //
0 commit comments