Skip to content

Commit c612795

Browse files
author
Pratik Das
committed
added ts code
1 parent bd8c084 commit c612795

File tree

6 files changed

+380
-4
lines changed

6 files changed

+380
-4
lines changed

nodejs/axios/apiserver/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ app.get('/products/:name/itemsInStock', (req, res) => {
4646

4747

4848
app.listen(port, () => {
49-
console.log(`Example server listening on port ${port}`)
49+
console.log(`API server listening on port ${port}`)
5050
})

nodejs/axios/serversideapp/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ app.get('/products', (request, response) => {
5252
})
5353

5454
app.get('/products/deals', (request, response) => {
55-
const instance = axios.create({
55+
const new_instance = axios.create({
5656
baseURL: 'http://localhost:3002/products',
5757
timeout: 1000,
5858
headers: {
5959
'Accept': 'application/json',
6060
'Authorization': 'XXXXXX'
6161
}
6262
})
63-
instance({
63+
new_instance({
6464
method: 'get',
6565
url: '/deals'
6666
}).then(apiResponse=>{
@@ -166,5 +166,5 @@ app.get('/products/:productName/inventory', (request, response) => {
166166

167167

168168
app.listen(port, () => {
169-
console.log(`Example server listening on port ${port}`)
169+
console.log(`server side app listening on port ${port}`)
170170
})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import axios from 'axios';
2+
3+
type Product = {
4+
id: number;
5+
email: string;
6+
first_name: string;
7+
};
8+
9+
type GetProductsResponse = {
10+
data: Product[];
11+
};
12+
13+
async function getProducts() {
14+
try {
15+
// 👇️ fetch products from an API
16+
const { data, status } = await axios.get<GetProductsResponse>(
17+
'http://localhost:3002/products',
18+
{
19+
headers: {
20+
Accept: 'application/json',
21+
},
22+
},
23+
);
24+
25+
console.log(JSON.stringify(data, null, 4));
26+
27+
console.log(`response status is: ${status}`);
28+
29+
return data;
30+
} catch (error) {
31+
if (axios.isAxiosError(error)) {
32+
console.log(`error message: ${error.message}`);
33+
return error.message;
34+
} else {
35+
console.log(`unexpected error: ${error}`);
36+
return 'An unexpected error occurred';
37+
}
38+
}
39+
}
40+
41+
getProducts();

nodejs/axios/serversideapp_ts/package-lock.json

Lines changed: 311 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)