import { AbstractSession , ImperativeExpect , Logger , ImperativeError , RestConstants , SessConstants } from "@zowe/imperative" ; import { ZosmfRestClient } from "../rest/ZosmfRestClient" ; import { LogoutConstants } from "./LogoutConstants" ; export class export class Logout { static param { AbstractSession } session returns memberof public static async apimlLogout ( session ) { Logger . getAppLogger ( ) . trace ( "Logout.logout()" ) ; ImperativeExpect . toNotBeNullOrUndefined ( session , "Required session must be defined" ) ; ImperativeExpect . toBeEqual ( session . ISession ?. tokenType , "apimlAuthenticationToken" , "Token type for API ML logout must be apimlAuthenticationToken." ) ; ImperativeExpect . toNotBeNullOrUndefined ( session . ISession ?. tokenValue , "Session token not populated. Unable to log out." ) ; const client = new ZosmfRestClient ( session ) ; try { await client . request ( { request : "POST" , resource : LogoutConstants . APIML_V1_RESOURCE } ) ; } catch ( err ) { if ( ! err . message . includes ( LogoutConstants . APIML_V1_TOKEN_EXP_ERR ) ) { throw err ; } } if ( client . response . statusCode !== RestConstants . HTTP_STATUS_204 ) { if ( ! ( client . response . statusCode === RestConstants . HTTP_STATUS_500 && client . dataString . includes ( LogoutConstants . APIML_V1_TOKEN_EXP_ERR ) ) ) { throw new ImperativeError ( ( client as any ) . populateError ( { msg : `REST API Failure with HTTP(S) status ${ client . response . statusCode }` , causeErrors : client . dataString , source : SessConstants . HTTP_PROTOCOL } ) ) ; } } } }