UNPKG

@klayr-did/klayr-verifiable-credentials

Version:

A library for working with W3C verifiable credentials (VC) and verifiable presentations (VP) using Klayr DID

66 lines 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.JsonLdDocumentLoader = void 0; class JsonLdDocumentLoader { constructor() { this.documents = new Map(); this.protocolHandlers = new Map(); } build() { return this.documentLoader.bind(this); } addStatic(url, document) { if (!_isString(url)) throw new TypeError('The first parameter (url) must be a string.'); if (!_isObject(document)) throw new TypeError('The second parameter (document) must be an object.'); this.documents.set(url, document); } setProtocolHandler({ protocol, handler }) { this.protocolHandlers.set(protocol, handler); } setDidResolver(didResolver) { this.setProtocolHandler({ protocol: 'did', handler: didResolver }); } setCustomLoader(documentLoader) { this._customLoader = documentLoader; } setCustomResolver(resolver) { this._customResolver = resolver; } async documentLoader(url) { if (!_isString(url)) { throw new TypeError('The "url" parameter must be a string.'); } const result = { contextUrl: null, document: this.documents.get(url), documentUrl: url, tag: undefined, }; if (result.document) result.tag = 'static'; const [protocol] = url.split(':'); if (!result.document && this.protocolHandlers.has(protocol)) { result.document = await this.protocolHandlers.get(protocol).get({ url }); } if (!result.document && this._customResolver !== undefined) { result.document = await this._customResolver.get({ url }); } if (!result.document && this._customLoader !== undefined) { result.document = await this._customLoader(url); } if (result.document) { return result; } throw new Error(`Document not found in document loader: ${url}`); } } exports.JsonLdDocumentLoader = JsonLdDocumentLoader; function _isString(arg) { return typeof arg === 'string'; } function _isObject(arg) { return typeof arg === 'object' && arg !== null; } //# sourceMappingURL=index.js.map