forked from prasadyash2411/axios-crash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
256 lines (208 loc) · 6.45 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//Axios Global header
axios.defaults.headers.common['OAuth2.0']='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'
// GET REQUEST
function getTodos() {
// axios({
// method: 'get',
// url: 'https://jsonplaceholder.typicode.com/todos',
// params: {
// _limit: 5
// }
// })
// .then(res=>showOutput(res))
// .catch(err=>console.log(err));
// axios.get('https://jsonplaceholder.typicode.com/todos', { params: { _limit: 5 }})
// .then(res=>showOutput(res))
// .catch(err=>console.log(err));
axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5', {timeout: 5000})
.then(res=>showOutput(res))
.catch(err=>console.log(err));
// console.log('GET Request');
}
// POST REQUEST
function addTodo() {
// axios({
// method: 'post',
// url: 'https://jsonplaceholder.typicode.com/todos',
// data: {
// title: 'Hello java script',
// completed: false
// }
// })
// .then(res=>showOutput(res))
// .catch(err=>console.log(err));
axios.post('https://jsonplaceholder.typicode.com/todos',{
title: 'Hello World of java Script',
completed: false
})
.then(res=>showOutput(res))
.catch(err=>console.log(err));
// console.log('POST Request');
}
// PUT/PATCH REQUEST
function updateTodo() {
// axios.put('https://jsonplaceholder.typicode.com/todos/1',{
// title: 'Updated World of java Script',
// completed: false
// })
// .then(res=>showOutput(res))
// .catch(err=>console.log(err));
axios.patch('https://jsonplaceholder.typicode.com/todos/1',{
title: 'Updated Patch World of java Script',
completed: false
})
.then(res=>showOutput(res))
.catch(err=>console.log(err));
// console.log('PUT/PATCH Request');
}
// DELETE REQUEST
function removeTodo() {
axios.delete('https://jsonplaceholder.typicode.com/todos/1')
.then(res=>showOutput(res))
.catch(err=>console.log(err));
// console.log('DELETE Request');
}
// SIMULTANEOUS DATA
function getData() {
// axios.all([
// axios.get('https://jsonplaceholder.typicode.com/todos'),
// axios.get('https://jsonplaceholder.typicode.com/posts')
// ])
// .then(res=>{
// console.log(res[0]);
// console.log(res[1]);
// showOutput(res[1])
// })
// .catch(err=>console.log(err));
axios.all([
axios.get('https://jsonplaceholder.typicode.com/todos'),
axios.get('https://jsonplaceholder.typicode.com/posts')
])
.then(axios.spread((todos,posts)=>showOutput(posts)))
.catch(err=>console.log(err));
// console.log('Simultaneous Request');
}
// CUSTOM HEADERS
function customHeaders() {
const config = {
headers: {
'Content-Type': 'application/json',
Authorization: 'Oauth Token'
}
}
axios.post('https://jsonplaceholder.typicode.com/todos', {
title: 'Hello World of java Script',
completed: false
},
config
)
.then(res=>showOutput(res))
.catch(err=>console.log(err));
// console.log('Custom Headers');
}
// TRANSFORMING REQUESTS & RESPONSES
function transformResponse() {
const option= {
method:'post',
url:'https://jsonplaceholder.typicode.com/todos',
data: {
title:'Welcome to world of javascript'
}, transformResponse: axios.defaults.transformResponse.concat(data=>{
data.title=data.title.toUpperCase();
return data;
})
}
axios(option).then(res=>showOutput(res))
// console.log('Transform Response');
}
// ERROR HANDLING
function errorHandling() {
axios.get('https://jsonplaceholder.typicode.com/todoss')
.then(res=>showOutput(res))
.catch(err=>{
if(err.response)
{
console.log(err.response.data)
console.log(err.response.status)
console.log(err.response.headers)
}
if(err.response.status===404)
{
alert('Page Not Found');
}
});
// console.log('Error Handling');
}
// CANCEL TOKEN
function cancelToken() {
const source=axios.CancelToken.source();
axios.get('https://jsonplaceholder.typicode.com/todos', {
cancelToken:source.token
})
.then(res=>showOutput(res))
.catch(thrown=>{
if(axios.isCancel(thrown))
{
console.log('Request Cancelled', thrown.message);
}
});
if(true)
{
source.cancel('Request Cancelled');
}
// console.log('Cancel Token');
}
// INTERCEPTING REQUESTS & RESPONSES
axios.interceptors.request.use(config=>{
console.log(`${config.method.toUpperCase()} request sent to ${config.url} at ${new Date().getTime()}`)
return config
},error=>{return Promise.reject(error)});
// AXIOS INSTANCES
const axiosInstance=axios.create({
baseURL:'https://jsonplaceholder.typicode.com'
});
axiosInstance.get('/comments').then(res=>showOutput(res));
// Show output in browser
function showOutput(res) {
document.getElementById('res').innerHTML = `
<div class="card card-body mb-4">
<h5>Status: ${res.status}</h5>
</div>
<div class="card mt-3">
<div class="card-header">
Headers
</div>
<div class="card-body">
<pre>${JSON.stringify(res.headers, null, 2)}</pre>
</div>
</div>
<div class="card mt-3">
<div class="card-header">
Data
</div>
<div class="card-body">
<pre>${JSON.stringify(res.data, null, 2)}</pre>
</div>
</div>
<div class="card mt-3">
<div class="card-header">
Config
</div>
<div class="card-body">
<pre>${JSON.stringify(res.config, null, 2)}</pre>
</div>
</div>
`;
}
// Event listeners
document.getElementById('get').addEventListener('click', getTodos);
document.getElementById('post').addEventListener('click', addTodo);
document.getElementById('update').addEventListener('click', updateTodo);
document.getElementById('delete').addEventListener('click', removeTodo);
document.getElementById('sim').addEventListener('click', getData);
document.getElementById('headers').addEventListener('click', customHeaders);
document
.getElementById('transform')
.addEventListener('click', transformResponse);
document.getElementById('error').addEventListener('click', errorHandling);
document.getElementById('cancel').addEventListener('click', cancelToken);