UNPKG

@serenity-js/core

Version:

The core Serenity/JS framework, providing the Screenplay Pattern interfaces, as well as the test reporting and integration infrastructure

37 lines 1.42 kB
import { ensure, isDefined, isString, Predicate, TinyType } from 'tiny-types'; import { LogicError } from '../errors/index.js'; import * as artifacts from './artifacts/index.js'; export class Artifact extends TinyType { base64EncodedValue; static fromJSON(o) { const recognisedTypes = Object.keys(artifacts), type = Artifact.ofType(o.type); if (!type) { throw new LogicError(` Couldn't de-serialise artifact of an unknown type. ${o.type} is not one of the recognised types: ${recognisedTypes.join(', ')} `); } return new type(o.base64EncodedValue); } static ofType(name) { const types = Object.keys(artifacts), type = types.find(constructorName => constructorName === name); return artifacts[type]; } constructor(base64EncodedValue) { super(); this.base64EncodedValue = base64EncodedValue; ensure(this.constructor.name, base64EncodedValue, isDefined(), isString(), looksLikeBase64Encoded()); } // todo: serialise on call toJSON() { return ({ type: this.constructor.name, base64EncodedValue: this.base64EncodedValue, }); } } function looksLikeBase64Encoded() { const regex = /^[\d+/=A-Za-z]+$/; return Predicate.to(`be base64-encoded`, (value) => regex.test(value)); } //# sourceMappingURL=Artifact.js.map