import axios from 'axios' ; import { invertMapKeysAndValues } from './utility' ; import { AuthHeaders , AuthResponse , DeviceStorage , SingleLayerStringMap , } from '../types' ; const authHeaderKeys = [ 'access-token' , 'token-type' , 'client' , 'expiry' , 'uid' , ] ; export const setAuthHeaders = ( headers ) : void => { authHeaderKeys . forEach ( ( key ) => { axios . defaults . headers . common [ key ] = headers [ key ] ; } ) ; } ; export const persistAuthHeadersInDeviceStorage = ( Storage , headers ) : void => { authHeaderKeys . forEach ( ( key ) => { Storage . setItem ( key , headers [ key ] ) ; } ) ; } ; export const deleteAuthHeaders = ( ) : void => { authHeaderKeys . forEach ( ( key ) => { delete axios . defaults . headers . common [ key ] ; } ) ; } ; export const deleteAuthHeadersFromDeviceStorage = async ( Storage ) : Promise < void > => { authHeaderKeys . forEach ( ( key ) => { Storage . removeItem ( key ) ; } ) ; } ; export const getUserAttributesFromResponse = ( userAttributes , response ) : SingleLayerStringMap => { const invertedUserAttributes = invertMapKeysAndValues ( userAttributes ) ; const userAttributesBackendKeys = Object . keys ( invertedUserAttributes ) ; const userAttributesToReturn = { } ; Object . keys ( response . data . data ) . forEach ( ( key ) => { if ( userAttributesBackendKeys . indexOf ( key ) !== - 1 ) { userAttributesToReturn [ invertedUserAttributes [ key ] ] = response . data . data [ key ] ; } } ) ; return userAttributesToReturn ; } ;