1
+ const readline = require ( 'node:readline' )
2
+ const { stdin : input , stdout : output } = require ( 'node:process' )
3
+
4
+ const rl = readline . createInterface ( { input, output } )
5
+
6
+
7
+ interface Products {
8
+ id : number
9
+ title : string
10
+ price : number
11
+ description : string
12
+ category : Category
13
+ images : string [ ]
14
+ }
15
+
16
+ interface Category {
17
+ id : number
18
+ name : string
19
+ image : string
20
+ }
21
+
22
+ async function getProducts ( ) : Promise < Products [ ] > {
23
+ try {
24
+ const response = await fetch ( 'https://api.escuelajs.co/api/v1/products' )
25
+
26
+ if ( ! response . ok ) {
27
+ throw new Error ( `HTTP error! status: ${ response . status } ` )
28
+ }
29
+
30
+ const results : Products [ ] = await response . json ( )
31
+ console . log ( 'Petición exitosa' )
32
+ return results
33
+ } catch ( error ) {
34
+ console . log ( 'Error al obtener productos:' , error )
35
+ return [ ]
36
+ }
37
+ }
38
+
39
+ async function printData ( ) {
40
+ const productsInformation = await getProducts ( )
41
+ console . log ( productsInformation )
42
+ }
43
+
44
+
45
+ /*
46
+ * Extra
47
+ */
48
+
49
+ const baseUrl : string = 'https://pokeapi.co/api/v2/pokemon/'
50
+
51
+ interface Pokemon {
52
+ id : number
53
+ name : string
54
+ weight : number
55
+ height : number
56
+ abilities : Ability [ ]
57
+ types : Type [ ]
58
+ stats : Stat [ ]
59
+ sprites : Sprite
60
+ }
61
+
62
+ interface Ability {
63
+ ability : NamedAPIResource
64
+ is_hidden : boolean
65
+ slot : number
66
+ }
67
+
68
+ interface NamedAPIResource {
69
+ name : string
70
+ url : string
71
+ }
72
+
73
+ interface Type {
74
+ slot : number
75
+ type : NamedAPIResource
76
+ }
77
+
78
+ interface Stat {
79
+ base_stat : number
80
+ effort : number
81
+ stat : NamedAPIResource
82
+ }
83
+
84
+ interface Sprite {
85
+ front_default : string
86
+ back_default ?: string
87
+ }
88
+
89
+ async function getPokemonInformation (
90
+ url : string ,
91
+ pokemonName : string
92
+ ) : Promise < Pokemon | null > {
93
+ try {
94
+ const response = await fetch ( `${ url } ${ pokemonName } ` )
95
+ if ( ! response . ok ) {
96
+ console . log ( `Error en la respuesta de la API: ${ response . status } ` )
97
+ return null
98
+ }
99
+ const result : Pokemon = await response . json ( )
100
+ console . log ( 'Petición exitosa' )
101
+ return result
102
+ } catch ( error ) {
103
+ console . error ( 'Error al realizar la petición:' , error )
104
+ return null
105
+ }
106
+ }
107
+
108
+ rl . question ( 'Ingresa el nombre de un Pokémon: ' , async ( pokemonNameAnswer : string ) => {
109
+ const pokemonInformation = await getPokemonInformation ( baseUrl , pokemonNameAnswer . toLowerCase ( ) )
110
+
111
+ if ( pokemonInformation ) {
112
+ console . log ( `Información de ${ pokemonInformation . name } :` )
113
+ console . log ( `ID: ${ pokemonInformation . id } ` )
114
+ console . log ( `Peso: ${ pokemonInformation . weight } ` )
115
+ console . log ( `Altura: ${ pokemonInformation . height } ` )
116
+ console . log ( 'Tipos:' )
117
+ pokemonInformation . types . forEach ( ( type ) => console . log ( ` - ${ type . type . name } ` ) )
118
+ console . log ( 'Estadísticas:' )
119
+ pokemonInformation . stats . forEach ( ( stat ) => console . log ( ` - ${ stat . stat . name } : ${ stat . base_stat } ` ) )
120
+ } else {
121
+ console . log ( 'No se encontró información para el Pokémon ingresado.' )
122
+ }
123
+
124
+ rl . close ( )
125
+ } )
0 commit comments