Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

response intersepters for axios create

import axios from 'axios';
// const axios = require('axios');

// Step-1: Create a new Axios instance with a custom config.
// The timeout is set to 10s. If the request takes longer than
// that then the request will be aborted.
const customAxios = axios.create({
    baseURL: `https://example.com/api`,
    timeout: 10000, 
    headers: { 'api-key': 'eyJz-CI6Ikp-4pWY-lhdCI6' }
});

// Step-2: Create request, response & error handlers
const requestHandler = request => {
    // Token will be dynamic so we can use any app-specific way to always   
    // fetch the new token before making the call
    request.headers.Authorization = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIwMTIzNDU2Nzg5IiwibmFtZSI6IlNhbXBsZSIsImlhdCI6MTUxNjIzODIzfQ.ZEBwz4pWYGqgFJc6DIi7HdTN0z5Pfs4Lcv4ZNwMr1rs';  
  
    return request;
};

const responseHandler = response => {
    if (response.status === 401) {
        window.location = '/login';
    }

    return response;
};

const errorHandler = error => {
    return Promise.reject(error);
};

// Step-3: Configure/make use of request & response interceptors from Axios
// Note: You can create one method say configureInterceptors, add below in that,
// export and call it in an init function of the application/page.
customAxios.interceptors.request.use(
    (request) => requestHandler(request),
    (error) => errorHandler(error)
);

customAxios.interceptors.response.use(
    (response) => responseHandler(response),
    (error) => errorHandler(error)
 );


// Step-4: Export the newly created Axios instance to be used in different locations.
export default customAxios;
Comment

PREVIOUS NEXT
Code Example
Javascript :: chrome extension contextmenus 
Javascript :: jquery elements which id doesnt contain string 
Javascript :: rewrite expressjs url 
Javascript :: class component in react 
Javascript :: react pdf 
Javascript :: create array of numbers javascript 
Javascript :: spotify uri 
Javascript :: Append to the text in the <p tag 
Javascript :: where to initialize state in react 
Javascript :: smtp testing 
Javascript :: call function javascript from asp net 
Javascript :: last array 
Javascript :: Each then() should return a value or throw 
Javascript :: javascript arrow functions to create methods inside objects 
Javascript :: aframe react 
Javascript :: useEffect react dependency 
Javascript :: delete in array 
Javascript :: save item in array javascript 
Javascript :: create component in react 
Javascript :: regex number 
Javascript :: return response from async call 
Javascript :: how to decode jwt token client side 
Javascript :: iterate table in jquery 
Javascript :: Send Data Using Express With Post 
Javascript :: express get raw query 
Javascript :: javascript settimeout lambda function 
Javascript :: changement image js sur click 
Javascript :: update nested formgroup angular 
Javascript :: switch19 dynamically update js 
Javascript :: convert js to python online 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =