interface IMineMeldAPIPromise { resource : angular . resource . IResource < any > ; resolved : boolean ; cancelled : boolean ; cancellable : boolean ; } export interface IMineMeldAPIResource extends angular . resource . IResourceClass < angular . resource . IResource < any > > { } export interface IMineMeldAPIService { getAPIResource ( url , paramDefaults ? , actions ? , cancellable ? ) : IMineMeldAPIResource ; logIn ( username , password ) : angular . IPromise < any > ; logOut ( ) : angular . IPromise < any > ; onLogin ( listener ) : void ; onLogout ( listener ) : void ; isLoggedIn ( ) : boolean ; cancelAPICalls ( ) : void ; } interface IMineMeldAPIResourceLogIn extends IMineMeldAPIResource { post ( params , data , success ? , error ? ) : angular . resource . IResource < any > ; } interface IMineMeldAPIResourceLogOut extends IMineMeldAPIResource { } export class MineMeldAPIService implements IMineMeldAPIService { outstanding : IMineMeldAPIPromise [ ] = [ ] ; $resource : angular . resource . IResourceService ; $httpParamSerializer : angular . IHttpParamSerializer ; $state : angular . ui . IStateService ; $cookies : angular . cookies . ICookiesService ; $rootScope : angular . IRootScopeService ; loggedIn : boolean = false ; ngInject constructor ( $resource , $state , $httpParamSerializer , $cookies , $rootScope ) { this . $resource = $resource ; this . $httpParamSerializer = $httpParamSerializer ; this . $state = $state ; this . $cookies = $cookies ; this . $rootScope = $rootScope ; if ( $cookies . get ( 'mm-ec-login' ) ) { this . loggedIn = true ; } } public getAPIResource ( url , paramDefaults ? , actions ? , cancellable ? ) { var result ; var vm = this ; if ( typeof cancellable === 'undefined' ) { cancellable = true ; } angular . forEach ( actions , ( action , actionName ) => { action . cancellable = true ; action . timeout = 45000 ; if ( typeof action . headers === 'undefined' ) { action . headers = { } ; } action . headers [ 'If-Modified-Since' ] = 'Mon, 26 Jul 1997 05:00:00 GMT' ; action . headers [ 'Cache-Control' ] = 'no-cache' ; action . headers [ 'Pragma' ] = 'no-cache' ; if ( typeof action . params === 'undefined' ) { action . params = { } ; } action . params [ '_' ] = ( ) : string => { return '' + Math . floor ( Date . now ( ) / 1000 ) ; } ; } ) ; result = < IMineMeldAPIResource > this . $resource ( url , paramDefaults , actions ) ; angular . forEach ( actions , ( action , actionName ) => { var origAction = result [ actionName ] ; result [ actionName ] = function ( ... args ) { var promise ; var origResult ; origResult = origAction . apply ( null , args ) ; promise = { resource : origResult , cancellable : cancellable , cancelled : false , resolved : false } ; origResult . $promise . then ( ( ) => { vm . setLoggedIn ( true ) ; } , ( error ) => { if ( error . status === 401 ) { vm . $state . go ( 'login' ) ; vm . setLoggedIn ( false ) ; throw error ; } if ( promise . cancelled ) { error . cancelled = true ; } else { if ( error . status === - 1 ) { error . statusText = 'Timeout' ; } } throw error ; } ) . finally ( ( ) => { promise . resolved = true ; } ) ; vm . outstanding = vm . outstanding . filter ( ( value ) => { return ! value . resolved ; } ) ; vm . outstanding . push ( promise ) ; return origResult ; } ; } ) ; return result ; } public logIn ( username , password ) { var loginResource ; var vm = this ; this . setLoggedIn ( false ) ; loginResource = < IMineMeldAPIResourceLogIn > this . getAPIResource ( '/login' , { } , { post : { method : 'POST' , headers : { 'Content-Type' : 'application/x-www-form-urlencoded' } , transformRequest : function ( data ) { return vm . $httpParamSerializer ( data ) ; } } } ) ; return loginResource . post ( { } , { u : username , p : password } ) . $promise . then ( ( ) => { this . setLoggedIn ( true ) ; } ) ; } public logOut ( ) { var logoutResource ; logoutResource = < IMineMeldAPIResourceLogOut > this . $resource ( '/logout' , { } , { get : { method : 'GET' } } ) ; return logoutResource . get ( ) . $promise . finally ( ( ) => { this . setLoggedIn ( false ) ; } ) ; } public isLoggedIn ( ) { return this . loggedIn ; } public cancelAPICalls ( ) { this . outstanding . forEach ( ( value ) => { if ( ! value . resolved && value . cancellable ) { value . cancelled = true ; value . resource . $cancelRequest ( ) ; } } ) ; } public onLogin ( listener ) { this . $rootScope . $on ( 'mm-login' , listener ) ; } public onLogout ( listener ) { this . $rootScope . $on ( 'mm-logout' , listener ) ; } private setLoggedIn ( status ) { if ( status == this . loggedIn ) { return ; } this . loggedIn = status ; if ( status ) { this . $cookies . put ( 'mm-ec-login' , '1' ) ; this . $rootScope . $emit ( 'mm-login' ) ; } else { this . $cookies . remove ( 'mm-ec-login' ) ; this . $cookies . remove ( 'mm-session' ) ; this . $rootScope . $emit ( 'mm-logout' ) ; } } }