Skip to content

Commit c0666f7

Browse files
committed
feat: first lesson with axios
1 parent 9a5863c commit c0666f7

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

lesson1/fetchjson/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
});

lesson1/fetchjson/index.ts

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
})();

0 commit comments

Comments
 (0)