import { Connection , IConnection , IProductInfo } from './Connection' ; import { RawSignedModel } from '../Cards/RawSignedModel' ; import { generateErrorFromResponse } from './errors' ; const PublishEndpoint = '/card/v5' ; const SearchEndpoint = '/card/v5/actions/search' ; const GetCardEndpoint = ( cardId ) => `/card/v5/${ cardId }` ; const RevokeCardEndpoint = ( cardId ) => `/card/v5/actions/revoke/${ cardId }` ; hidden export interface ICardResult { readonly cardRaw : RawSignedModel ; readonly isOutdated : boolean ; } hidden export class CardClient { private readonly connection : IConnection ; param { IConnection | string } connection - Object implementing the
     *  IConnection interface.
      public constructor ( connection ? , productInfo ? ) { if ( typeof connection === 'string' ) { this . connection = new Connection ( connection , productInfo ) ; } else if ( connection ) { this . connection = connection ; } else { this . connection = new Connection ( 'https://api.virgilsecurity.com' , productInfo ) ; } } param { string [ ] } identities param { string } accessToken returns { Promise < RawSignedModel [ ] > } public async searchCards ( identities , accessToken ) { const response = await this . connection . post ( SearchEndpoint , accessToken , { identities } ) ; if ( ! response . ok ) throw await generateErrorFromResponse ( response ) ; const cardsJson = await response . json ( ) ; if ( cardsJson === null ) return [ ] ; return cardsJson . map ( RawSignedModel . fromJson ) ; } param { string } cardId param { string } accessToken returns { Promise < ICardResult > } public async getCard ( cardId , accessToken ) { if ( ! cardId ) throw new TypeError ( '`cardId` should not be empty' ) ; if ( ! accessToken ) throw new TypeError ( '`accessToken` should not be empty' ) ; const response = await this . connection . get ( GetCardEndpoint ( cardId ) , accessToken ) ; if ( ! response . ok ) { throw await generateErrorFromResponse ( response ) ; } const isOutdated = response . headers . get ( 'X-Virgil-Is-Superseeded' ) === 'true' ; const cardJson = await response . json ( ) ; const cardRaw = RawSignedModel . fromJson ( cardJson ) ; return { cardRaw , isOutdated } ; } param { RawSignedModel } model param { string } accessToken returns { Promise < RawSignedModel > } public async publishCard ( model , accessToken ) { if ( ! model ) throw new TypeError ( '`model` should not be empty' ) ; if ( ! accessToken ) throw new TypeError ( '`accessToken` should not be empty' ) ; const response = await this . connection . post ( PublishEndpoint , accessToken , model ) ; if ( ! response . ok ) { throw await generateErrorFromResponse ( response ) ; } const cardJson = await response . json ( ) ; return RawSignedModel . fromJson ( cardJson ) ; } public async revokeCard ( cardId , accessToken ) { if ( ! cardId ) throw new TypeError ( '`cardId` should not be empty' ) ; if ( ! accessToken ) throw new TypeError ( '`accessToken` should not be empty' ) ; const response = await this . connection . post ( RevokeCardEndpoint ( cardId ) , accessToken ) ; if ( ! response . ok ) { throw await generateErrorFromResponse ( response ) ; } } }