import { fetch , Headers } from '../Lib/fetch' ; import { VirgilAgent } from './VirgilAgent' ; /**
 * Information about product for SDK usage statistic.
 */ export type IProductInfo = { product : string ; version : string ; } ; hidden export interface IConnection { get ( endpoint , accessToken ) : Promise < Response > ; post ( endpoint , accessToken , data ? ) : Promise < Response > ; } hidden export class Connection implements IConnection { private virgilAgentValue : string ; param { string } prefix param { VirgilAgentValue } virgilAgentValue public constructor ( private readonly prefix , info ? ) { if ( ! info ) info = { product : 'sdk' , version : process . env . VERSION ! } ; this . virgilAgentValue = new VirgilAgent ( info . product , info . version ) . value ; } param { string } endpoint param { string } accessToken returns { Promise < Response > } public get ( endpoint , accessToken ) { const headers = this . createHeaders ( accessToken ) ; return this . send ( endpoint , 'GET' , { headers } ) ; } param { string } endpoint param { string } accessToken param { object } data returns { Promise < Response > } public post ( endpoint , accessToken , data = { } ) { const headers = this . createHeaders ( accessToken ) ; headers . set ( 'Content-Type' , 'application/json' ) ; return this . send ( endpoint , 'POST' , { headers : headers , body : JSON . stringify ( data ) } ) ; } private send ( endpoint , method , params ) { return fetch ( this . prefix + endpoint , { method , ... params } ) ; } private createHeaders ( accessToken ) { const headers = new Headers ( ) ; headers . set ( 'Authorization' , `Virgil ${ accessToken }` ) ; headers . set ( 'Virgil-Agent' , this . virgilAgentValue ) ; return headers ; } }