|
1 |
| -// AXIOS GLOBALS |
2 |
| -axios.defaults.headers.common['X-Auth-Token'] = |
3 |
| - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c'; |
4 |
| - |
5 | 1 | // GET REQUEST
|
6 | 2 | 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'); |
23 | 4 | }
|
24 | 5 |
|
25 | 6 | // POST REQUEST
|
26 | 7 | 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'); |
34 | 9 | }
|
35 | 10 |
|
36 | 11 | // PUT/PATCH REQUEST
|
37 | 12 | 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'); |
45 | 14 | }
|
46 | 15 |
|
47 | 16 | // DELETE REQUEST
|
48 | 17 | 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'); |
53 | 19 | }
|
54 | 20 |
|
55 | 21 | // SIMULTANEOUS DATA
|
56 | 22 | 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'); |
64 | 24 | }
|
65 | 25 |
|
66 | 26 | // CUSTOM HEADERS
|
67 | 27 | 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'); |
86 | 29 | }
|
87 | 30 |
|
88 | 31 | // TRANSFORMING REQUESTS & RESPONSES
|
89 | 32 | 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'); |
103 | 34 | }
|
104 | 35 |
|
105 | 36 | // ERROR HANDLING
|
106 | 37 | 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'); |
131 | 39 | }
|
132 | 40 |
|
133 | 41 | // CANCEL TOKEN
|
134 | 42 | 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'); |
151 | 44 | }
|
152 | 45 |
|
153 | 46 | // 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 |
| -); |
168 | 47 |
|
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 |
175 | 49 |
|
176 | 50 | // Show output in browser
|
177 | 51 | function showOutput(res) {
|
|
0 commit comments