File tree 2 files changed +50
-0
lines changed
2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ "use strict" ;
2
+ exports . __esModule = true ;
3
+ var axios_1 = require ( "axios" ) ;
4
+ var url = "http://jsonplaceholder.typicode.com/todos/1" ;
5
+ axios_1 [ "default" ] . get ( url ) . then ( function ( response ) {
6
+ console . log ( response . data ) ;
7
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import axios from "axios" ;
2
+
3
+ const url = "http://jsonplaceholder.typicode.com/todos/2" ;
4
+
5
+ // interface with purpose to type objects
6
+ interface ITodoData {
7
+ id : number ;
8
+ userId : number ;
9
+ title : string ;
10
+ completed : boolean ;
11
+ }
12
+
13
+ const getTodoData = async ( ) => {
14
+ try {
15
+ await axios . get < ITodoData > ( url ) . then ( ( response ) => {
16
+ // const { id, title, completed } = response.data as ITodoData; typed with type alias
17
+ const { id, title, completed } = response . data ; // typed with generic
18
+ logger ( id , title , completed ) ;
19
+ } ) ;
20
+ } catch ( error ) {
21
+ errorLogger ( error ) ;
22
+ }
23
+ } ;
24
+
25
+ const logger = ( id : number , title : string , completed : boolean ) => {
26
+ console . log (
27
+ `TODO with ID: ${ id } , title: ${ title } and it's ${
28
+ completed ? "completed" : "not completed"
29
+ } `
30
+ ) ;
31
+ } ;
32
+
33
+ const errorLogger = ( error : unknown ) => {
34
+ console . log (
35
+ `OH NO, SOMETHING WENT WRONG ${ error }
36
+ `
37
+ ) ;
38
+ } ;
39
+
40
+ // IIFE Function
41
+ ( ( ) => {
42
+ getTodoData ( ) ;
43
+ } ) ( ) ;
You can’t perform that action at this time.
0 commit comments