Skip to content

Commit

Permalink
chenged main
Browse files Browse the repository at this point in the history
  • Loading branch information
yash prasad committed Mar 12, 2021
1 parent d664ffb commit 9384eed
Showing 1 changed file with 10 additions and 136 deletions.
146 changes: 10 additions & 136 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,177 +1,51 @@
// AXIOS GLOBALS
axios.defaults.headers.common['X-Auth-Token'] =
'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.error(err));

axios
.get('https://jsonplaceholder.typicode.com/todos?_limit=5', {
timeout: 5000
})
.then(res => showOutput(res))
.catch(err => console.error(err));
console.log('GET Request');
}

// POST REQUEST
function addTodo() {
axios
.post('https://jsonplaceholder.typicode.com/todos', {
title: 'New Todo',
completed: false
})
.then(res => showOutput(res))
.catch(err => console.error(err));
console.log('POST Request');
}

// PUT/PATCH REQUEST
function updateTodo() {
axios
.patch('https://jsonplaceholder.typicode.com/todos/1', {
title: 'Updated Todo',
completed: true
})
.then(res => showOutput(res))
.catch(err => console.error(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.error(err));
console.log('DELETE Request');
}

// SIMULTANEOUS DATA
function getData() {
axios
.all([
axios.get('https://jsonplaceholder.typicode.com/todos?_limit=5'),
axios.get('https://jsonplaceholder.typicode.com/posts?_limit=5')
])
.then(axios.spread((todos, posts) => showOutput(posts)))
.catch(err => console.error(err));
console.log('Simultaneous Request');
}

// CUSTOM HEADERS
function customHeaders() {
const config = {
headers: {
'Content-Type': 'application/json',
Authorization: 'sometoken'
}
};

axios
.post(
'https://jsonplaceholder.typicode.com/todos',
{
title: 'New Todo',
completed: false
},
config
)
.then(res => showOutput(res))
.catch(err => console.error(err));
console.log('Custom Headers');
}

// TRANSFORMING REQUESTS & RESPONSES
function transformResponse() {
const options = {
method: 'post',
url: 'https://jsonplaceholder.typicode.com/todos',
data: {
title: 'Hello World'
},
transformResponse: axios.defaults.transformResponse.concat(data => {
data.title = data.title.toUpperCase();
return data;
})
};

axios(options).then(res => showOutput(res));
console.log('Transform Response');
}

// ERROR HANDLING
function errorHandling() {
axios
.get('https://jsonplaceholder.typicode.com/todoss', {
// validateStatus: function(status) {
// return status < 500; // Reject only if status is greater or equal to 500
// }
})
.then(res => showOutput(res))
.catch(err => {
if (err.response) {
// Server responded with a status other than 200 range
console.log(err.response.data);
console.log(err.response.status);
console.log(err.response.headers);

if (err.response.status === 404) {
alert('Error: Page Not Found');
}
} else if (err.request) {
// Request was made but no response
console.error(err.request);
} else {
console.error(err.message);
}
});
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 canceled', thrown.message);
}
});

if (true) {
source.cancel('Request canceled!');
}
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 INSTANCE
const axiosInstance = axios.create({
// Other custom settings
baseURL: 'https://jsonplaceholder.typicode.com'
});
// axiosInstance.get('/comments').then(res => showOutput(res));
// AXIOS INSTANCES

// Show output in browser
function showOutput(res) {
Expand Down

0 comments on commit 9384eed

Please sign in to comment.