Skip to content

Commit 9384eed

Browse files
author
yash prasad
committed
chenged main
1 parent d664ffb commit 9384eed

File tree

1 file changed

+10
-136
lines changed

1 file changed

+10
-136
lines changed

main.js

Lines changed: 10 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,177 +1,51 @@
1-
// AXIOS GLOBALS
2-
axios.defaults.headers.common['X-Auth-Token'] =
3-
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c';
4-
51
// GET REQUEST
62
function getTodos() {
7-
// axios({
8-
// method: 'get',
9-
// url: 'https://jsonplaceholder.typicode.com/todos',
10-
// params: {
11-
// _limit: 5
12-
// }
13-
// })
14-
// .then(res => showOutput(res))
15-
// .catch(err => console.error(err));
16-
17-
axios
18-
.get('https://jsonplaceholder.typicode.com/todos?_limit=5', {
19-
timeout: 5000
20-
})
21-
.then(res => showOutput(res))
22-
.catch(err => console.error(err));
3+
console.log('GET Request');
234
}
245

256
// POST REQUEST
267
function addTodo() {
27-
axios
28-
.post('https://jsonplaceholder.typicode.com/todos', {
29-
title: 'New Todo',
30-
completed: false
31-
})
32-
.then(res => showOutput(res))
33-
.catch(err => console.error(err));
8+
console.log('POST Request');
349
}
3510

3611
// PUT/PATCH REQUEST
3712
function updateTodo() {
38-
axios
39-
.patch('https://jsonplaceholder.typicode.com/todos/1', {
40-
title: 'Updated Todo',
41-
completed: true
42-
})
43-
.then(res => showOutput(res))
44-
.catch(err => console.error(err));
13+
console.log('PUT/PATCH Request');
4514
}
4615

4716
// DELETE REQUEST
4817
function removeTodo() {
49-
axios
50-
.delete('https://jsonplaceholder.typicode.com/todos/1')
51-
.then(res => showOutput(res))
52-
.catch(err => console.error(err));
18+
console.log('DELETE Request');
5319
}
5420

5521
// SIMULTANEOUS DATA
5622
function getData() {
57-
axios
58-
.all([
59-
axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5'),
60-
axios.get('https://jsonplaceholder.typicode.com/posts?_limit=5')
61-
])
62-
.then(axios.spread((todos, posts) => showOutput(posts)))
63-
.catch(err => console.error(err));
23+
console.log('Simultaneous Request');
6424
}
6525

6626
// CUSTOM HEADERS
6727
function customHeaders() {
68-
const config = {
69-
headers: {
70-
'Content-Type': 'application/json',
71-
Authorization: 'sometoken'
72-
}
73-
};
74-
75-
axios
76-
.post(
77-
'https://jsonplaceholder.typicode.com/todos',
78-
{
79-
title: 'New Todo',
80-
completed: false
81-
},
82-
config
83-
)
84-
.then(res => showOutput(res))
85-
.catch(err => console.error(err));
28+
console.log('Custom Headers');
8629
}
8730

8831
// TRANSFORMING REQUESTS & RESPONSES
8932
function transformResponse() {
90-
const options = {
91-
method: 'post',
92-
url: 'https://jsonplaceholder.typicode.com/todos',
93-
data: {
94-
title: 'Hello World'
95-
},
96-
transformResponse: axios.defaults.transformResponse.concat(data => {
97-
data.title = data.title.toUpperCase();
98-
return data;
99-
})
100-
};
101-
102-
axios(options).then(res => showOutput(res));
33+
console.log('Transform Response');
10334
}
10435

10536
// ERROR HANDLING
10637
function errorHandling() {
107-
axios
108-
.get('https://jsonplaceholder.typicode.com/todoss', {
109-
// validateStatus: function(status) {
110-
// return status < 500; // Reject only if status is greater or equal to 500
111-
// }
112-
})
113-
.then(res => showOutput(res))
114-
.catch(err => {
115-
if (err.response) {
116-
// Server responded with a status other than 200 range
117-
console.log(err.response.data);
118-
console.log(err.response.status);
119-
console.log(err.response.headers);
120-
121-
if (err.response.status === 404) {
122-
alert('Error: Page Not Found');
123-
}
124-
} else if (err.request) {
125-
// Request was made but no response
126-
console.error(err.request);
127-
} else {
128-
console.error(err.message);
129-
}
130-
});
38+
console.log('Error Handling');
13139
}
13240

13341
// CANCEL TOKEN
13442
function cancelToken() {
135-
const source = axios.CancelToken.source();
136-
137-
axios
138-
.get('https://jsonplaceholder.typicode.com/todos', {
139-
cancelToken: source.token
140-
})
141-
.then(res => showOutput(res))
142-
.catch(thrown => {
143-
if (axios.isCancel(thrown)) {
144-
console.log('Request canceled', thrown.message);
145-
}
146-
});
147-
148-
if (true) {
149-
source.cancel('Request canceled!');
150-
}
43+
console.log('Cancel Token');
15144
}
15245

15346
// INTERCEPTING REQUESTS & RESPONSES
154-
axios.interceptors.request.use(
155-
config => {
156-
console.log(
157-
`${config.method.toUpperCase()} request sent to ${
158-
config.url
159-
} at ${new Date().getTime()}`
160-
);
161-
162-
return config;
163-
},
164-
error => {
165-
return Promise.reject(error);
166-
}
167-
);
16847

169-
// AXIOS INSTANCE
170-
const axiosInstance = axios.create({
171-
// Other custom settings
172-
baseURL: 'https://jsonplaceholder.typicode.com'
173-
});
174-
// axiosInstance.get('/comments').then(res => showOutput(res));
48+
// AXIOS INSTANCES
17549

17650
// Show output in browser
17751
function showOutput(res) {

0 commit comments

Comments
 (0)