1- import express from "express" ;
2- import axios from "axios" ;
3- const roverRouter = express . Router ( ) ;
4-
5- import dotenv from "dotenv" ;
6- dotenv . config ( { path : './.env' } ) ;
7- const apiKey = process . env . NASA_API_KEY ;
8-
9- enum Cameras {
10- FHAZ = "FHAZ" ,
11- RHAZ = "RHAZ" ,
12- MAST = "MAST" ,
13- CHEMCAM = "CHEMCAM" ,
14- MAHLI = "MAHLI = 5" ,
15- MARDI = "MARDI" ,
16- NAVCAM = "NAVCAM" ,
17- PANCAM = "PANCAM" ,
18- MINITES = "MINITES"
19- }
20-
21- enum Rovers {
22- CURIOSITY = "Curiosity" ,
23- SPIRIT = "Spirit" ,
24- OPPORTUNITY = "Opportunity" ,
25- PERSEVERANCE = "Perseverance"
26- }
27-
28- interface Camera {
29- id ?: number ;
30- name : string ;
31- rover_id ?: number ;
32- full_name : string ;
33- }
34-
35- interface Rover {
36- id : number ;
37- name : string ;
38- landing_date : string ;
39- launch_date : string ;
40- status : string ;
41- max_sol : number ;
42- max_date : string ;
43- total_photos : number ;
44- cameras : Camera [ ] ;
45- }
46-
47- interface Photo {
48- id : number ;
49- sol : number ;
50- camera : Camera ;
51- img_src : string ;
52- earth_date : string ;
53- rover : Rover ;
54- }
55-
56- roverRouter . get ( '/' , ( req : any , res : any ) => {
57- axios . get ( `https://api.nasa.gov/mars-photos/api/v1/rovers?api_key=${ apiKey } ` )
58- . then ( response => {
59- res . send ( response . data ) ;
60- } )
61- . catch ( error => {
62- console . error ( error ) ;
63- res . render ( "error" ) ;
64- } )
65- } ) ;
66-
67- roverRouter . get ( '/:rovername/photos/:camera?' , ( req : any , res : any ) => {
68-
69- const { rovername, camera } = req . params ;
70-
71- console . log ( `rover: ${ rovername } , camera: ${ camera } ` ) ;
72-
73- const nasaUrl = camera ? `https://api.nasa.gov/mars-photos/api/v1/rovers/${ rovername } /photos?sol=1000&camera=${ camera } &api_key=${ apiKey } `
74- : `https://api.nasa.gov/mars-photos/api/v1/rovers/${ rovername } /photos?sol=1000&api_key=${ apiKey } ` ;
75-
76- axios . get ( nasaUrl )
77- . then ( response => {
78- const photos : Photo [ ] = response . data . photos ;
79- const urls : string [ ] = [ ] ;
80- photos . forEach ( photo => {
81- urls . push ( photo . img_src ) ;
82- } ) ;
83- res . send ( urls ) ;
84- } )
85- . catch ( error => {
86- console . error ( error ) ;
87- res . render ( "error" ) ;
88- } )
89- } ) ;
90-
91- export default roverRouter ;
0 commit comments