import { message } from "antd" ; import * as _ from "lodash" ; import moment from "moment" ; import { history } from "../../routing/history" ; import { Routes } from "../../routing/Routes" ; import { authStore } from "../../stores/AuthStore" ; import { APIErrors } from "./APIErrors" ; export interface IGenericAPIResponse { error : boolean ; message : string ; } message . config ( { top : 12 , duration : 4 , maxCount : 3 } ) ; let LAST_SESSION_EXPIRED_MESSAGE_SHOWN ; const APIUtils = { saveTokenFromResponseIfAvailable : ( response ) : void => { const accessToken = response . headers . get ( "access-token" ) ; const client = response . headers . get ( "client" ) ; if ( accessToken ) { authStore . accessToken = accessToken ; } if ( client ) { authStore . client = client ; } } , /**
     * Handles errors like an invalid access token.
     */ handleErrors : ( response ) => { if ( response . errors ) { console . error ( "API Errors:" , response . errors ) ; if ( Array . isArray ( response . errors ) ) { response . errors . forEach ( ( error ) => { if ( error . code === APIErrors . INVALID_ACCESS_TOKEN ) { if ( ! LAST_SESSION_EXPIRED_MESSAGE_SHOWN || moment ( ) . isAfter ( LAST_SESSION_EXPIRED_MESSAGE_SHOWN . add ( 5 , "minutes" ) ) ) { LAST_SESSION_EXPIRED_MESSAGE_SHOWN = moment ( ) ; authStore . resetAuth ( ) ; message . info ( "Your session expired. Please log in again." ) ; history . push ( Routes . AUTH . LOGIN ) ; return ; } } } ) ; } } return response ; } , getIncludedObject ( object , included ) { if ( ! object ) { return null ; } if ( ! object . id || ! object . type ) { console . error ( "Couldn't find property 'id' or 'type'." ) ; } return _ . find ( included , ( o ) => { return o . id === object . id && o . type === object . type ; } ) ; } , /**
     * Converts a data URI to a blob.
     * See https://github.com/graingert/datauritoblob/blob/master/dataURItoBlob.js.
     */ dataURItoBlob ( dataURI ) { const byteString = atob ( dataURI . split ( "," ) [ 1 ] ) ; const mimeString = dataURI . split ( "," ) [ 0 ] . split ( ":" ) [ 1 ] . split ( ";" ) [ 0 ] ; const ab = new ArrayBuffer ( byteString . length ) ; const dw = new DataView ( ab ) ; for ( let i = 0 ; i < byteString . length ; i ++ ) { dw . setUint8 ( i , byteString . charCodeAt ( i ) ) ; } return new Blob ( [ ab ] , { type : mimeString } ) ; } } ; export { APIUtils } ;