UNPKG

@microsoft/useragent-sdk

Version:

SDK for building decentralized identity wallets and enterprise agents.

69 lines 2.99 kB
"use strict"; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ Object.defineProperty(exports, "__esModule", { value: true }); require('es6-promise').polyfill(); require("isomorphic-fetch"); const IdentifierDocument_1 = require("../IdentifierDocument"); const UserAgentError_1 = require("../UserAgentError"); /** * Fetches DID Documents from remote resolvers over http * @class * @implements Resolver */ class HttpResolver { /** * Constructs an instance of the HttpResolver class. * @param url of the remote resolver. * @param [options] for configuring the resolver. */ constructor(url, options) { this.url = url; this.options = options; // Format the url const slash = url.endsWith('/') ? '' : '/'; this.url = `${url}${slash}1.0/identifiers/`; this.timeoutInMilliseconds = 1000 * (!this.options || !this.options.timeoutInSeconds ? 30 : this.options.timeoutInSeconds); } /** * Sends a fetch request to the resolver URL including the * specified identifier. * @param identifier to resolve. */ async resolve(identifier) { const query = `${this.url}${identifier.id}`; return new Promise(async (resolve, reject) => { const timer = setTimeout(() => reject(new UserAgentError_1.default('Fetch timed out.')), this.timeoutInMilliseconds); // Now call the actual fetch with the updated options const response = await fetch(query); // Got a response so clear the timer clearTimeout(timer); // Check if the response was OK, and // if not return the appropriate error if (!response.ok) { let error; switch (response.status) { case 404: error = new UserAgentError_1.default(`Identifier document not found for '${identifier.id}'`); break; default: error = new UserAgentError_1.default(`Resolver at '${this.url}' returned an error with '${response.statusText}'`); } // Reject the promise reject(error); return; } const responseJson = await response.json(); const identifierDocument = IdentifierDocument_1.default.fromJSON(responseJson.document || responseJson); resolve(identifierDocument); }); } } exports.default = HttpResolver; //# sourceMappingURL=HttpResolver.js.map