import api from 'teleport/services/api' ; import cfg from 'teleport/config' ; import makeUserContext from './makeUserContext' ; import makeResetToken from './makeResetToken' ; import makeUser , { makeUsers } from './makeUser' ; import { User , UserContext , ResetPasswordType } from './types' ; const cache = { userContext : null as UserContext , } ; const service = { fetchUserContext ( fromCache = true ) { if ( fromCache && cache [ 'userContext' ] ) { return Promise . resolve ( cache [ 'userContext' ] ) ; } return api . get ( cfg . getUserContextUrl ( ) ) . then ( makeUserContext ) . then ( userContext => { cache [ 'userContext' ] = userContext ; return cache [ 'userContext' ] ; } ) ; } , fetchUsers ( ) { return api . get ( cfg . getUsersUrl ( ) ) . then ( makeUsers ) ; } , updateUser ( user ) { return api . put ( cfg . getUsersUrl ( ) , user ) . then ( makeUser ) ; } , createUser ( user ) { return api . post ( cfg . getUsersUrl ( ) , user ) . then ( makeUser ) ; } , createResetPasswordToken ( name , type ) { return api . post ( cfg . api . resetPasswordTokenPath , { name , type } ) . then ( makeResetToken ) ; } , deleteUser ( name ) { return api . delete ( cfg . getUsersDeleteUrl ( name ) ) ; } , } ; export default service ;