import ErrorHelpers from '../error_handling/ErrorHelpers' ; import { DidDocument , IDidResolver , IDidResolveResult , ValidationError } from '../index' ; import FetchRequest from '../tracing/FetchRequest' ; import IFetchRequest from '../tracing/IFetchRequest' ; const errorCode = ( error ) => ErrorHelpers . errorCode ( 'VCSDKMARE' , error ) ; require ( 'es6-promise' ) . polyfill ( ) ; require ( 'isomorphic-fetch' ) ; class extends DidResolver export default class ManagedHttpResolver implements IDidResolver { /**
     * String to hold the formatted resolver url
     */ public readonly resolverUrl : string ; param universalResolverUrl param fetchRequest constructor ( universalResolverUrl , public fetchRequest ? ) { const slash = universalResolverUrl . endsWith ( '/' ) ? '' : '/' ; this . resolverUrl = `${ universalResolverUrl }${ slash }` ; } inheritdoc public async resolve ( did ) { const query = `${ this . resolverUrl }${ did }` ; let response ; if ( ! this . fetchRequest ) { this . fetchRequest = new FetchRequest ( ) ; } response = await this . fetchRequest . fetch ( query , 'DIDResolve' , { method : 'GET' , headers : { } } ) ; if ( response . status >= 200 && response . status < 300 ) { const didDocument = await response . json ( ) ; return { didDocument : new DidDocument ( didDocument . didDocument ) , metadata : didDocument . resolverMetadata } as IDidResolveResult ; } return Promise . reject ( new ValidationError ( `Could not resolve ${ query }` , errorCode ( 1 ) ) ) ; } }