UNPKG

@interopio/browser

Version:

IOConnect Browser client application package

1,348 lines (1,299 loc) 665 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.browser = factory()); })(this, (function () { 'use strict'; function getDefaultExportFromCjs$1 (x) { return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x; } var isMergeableObject = function isMergeableObject(value) { return isNonNullObject(value) && !isSpecial(value) }; function isNonNullObject(value) { return !!value && typeof value === 'object' } function isSpecial(value) { var stringValue = Object.prototype.toString.call(value); return stringValue === '[object RegExp]' || stringValue === '[object Date]' || isReactElement(value) } // see https://github.com/facebook/react/blob/b5ac963fb791d1298e7f396236383bc955f916c1/src/isomorphic/classic/element/ReactElement.js#L21-L25 var canUseSymbol = typeof Symbol === 'function' && Symbol.for; var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for('react.element') : 0xeac7; function isReactElement(value) { return value.$$typeof === REACT_ELEMENT_TYPE } function emptyTarget(val) { return Array.isArray(val) ? [] : {} } function cloneUnlessOtherwiseSpecified(value, options) { return (options.clone !== false && options.isMergeableObject(value)) ? deepmerge(emptyTarget(value), value, options) : value } function defaultArrayMerge(target, source, options) { return target.concat(source).map(function(element) { return cloneUnlessOtherwiseSpecified(element, options) }) } function getMergeFunction(key, options) { if (!options.customMerge) { return deepmerge } var customMerge = options.customMerge(key); return typeof customMerge === 'function' ? customMerge : deepmerge } function getEnumerableOwnPropertySymbols(target) { return Object.getOwnPropertySymbols ? Object.getOwnPropertySymbols(target).filter(function(symbol) { return Object.propertyIsEnumerable.call(target, symbol) }) : [] } function getKeys(target) { return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target)) } function propertyIsOnObject(object, property) { try { return property in object } catch(_) { return false } } // Protects from prototype poisoning and unexpected merging up the prototype chain. function propertyIsUnsafe(target, key) { return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet, && !(Object.hasOwnProperty.call(target, key) // unsafe if they exist up the prototype chain, && Object.propertyIsEnumerable.call(target, key)) // and also unsafe if they're nonenumerable. } function mergeObject(target, source, options) { var destination = {}; if (options.isMergeableObject(target)) { getKeys(target).forEach(function(key) { destination[key] = cloneUnlessOtherwiseSpecified(target[key], options); }); } getKeys(source).forEach(function(key) { if (propertyIsUnsafe(target, key)) { return } if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) { destination[key] = getMergeFunction(key, options)(target[key], source[key], options); } else { destination[key] = cloneUnlessOtherwiseSpecified(source[key], options); } }); return destination } function deepmerge(target, source, options) { options = options || {}; options.arrayMerge = options.arrayMerge || defaultArrayMerge; options.isMergeableObject = options.isMergeableObject || isMergeableObject; // cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge() // implementations can use it. The caller may not replace it. options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified; var sourceIsArray = Array.isArray(source); var targetIsArray = Array.isArray(target); var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray; if (!sourceAndTargetTypesMatch) { return cloneUnlessOtherwiseSpecified(source, options) } else if (sourceIsArray) { return options.arrayMerge(target, source, options) } else { return mergeObject(target, source, options) } } deepmerge.all = function deepmergeAll(array, options) { if (!Array.isArray(array)) { throw new Error('first argument should be an array') } return array.reduce(function(prev, next) { return deepmerge(prev, next, options) }, {}) }; var deepmerge_1 = deepmerge; var cjs = deepmerge_1; var deepmerge$1 = /*@__PURE__*/getDefaultExportFromCjs$1(cjs); /** * Wraps values in an `Ok` type. * * Example: `ok(5) // => {ok: true, result: 5}` */ var ok$2 = function (result) { return ({ ok: true, result: result }); }; /** * Wraps errors in an `Err` type. * * Example: `err('on fire') // => {ok: false, error: 'on fire'}` */ var err$2 = function (error) { return ({ ok: false, error: error }); }; /** * Create a `Promise` that either resolves with the result of `Ok` or rejects * with the error of `Err`. */ var asPromise$2 = function (r) { return r.ok === true ? Promise.resolve(r.result) : Promise.reject(r.error); }; /** * Unwraps a `Result` and returns either the result of an `Ok`, or * `defaultValue`. * * Example: * ``` * Result.withDefault(5, number().run(json)) * ``` * * It would be nice if `Decoder` had an instance method that mirrored this * function. Such a method would look something like this: * ``` * class Decoder<A> { * runWithDefault = (defaultValue: A, json: any): A => * Result.withDefault(defaultValue, this.run(json)); * } * * number().runWithDefault(5, json) * ``` * Unfortunately, the type of `defaultValue: A` on the method causes issues * with type inference on the `object` decoder in some situations. While these * inference issues can be solved by providing the optional type argument for * `object`s, the extra trouble and confusion doesn't seem worth it. */ var withDefault$2 = function (defaultValue, r) { return r.ok === true ? r.result : defaultValue; }; /** * Return the successful result, or throw an error. */ var withException$2 = function (r) { if (r.ok === true) { return r.result; } else { throw r.error; } }; /** * Apply `f` to the result of an `Ok`, or pass the error through. */ var map$2 = function (f, r) { return r.ok === true ? ok$2(f(r.result)) : r; }; /** * Apply `f` to the result of two `Ok`s, or pass an error through. If both * `Result`s are errors then the first one is returned. */ var map2$2 = function (f, ar, br) { return ar.ok === false ? ar : br.ok === false ? br : ok$2(f(ar.result, br.result)); }; /** * Apply `f` to the error of an `Err`, or pass the success through. */ var mapError$2 = function (f, r) { return r.ok === true ? r : err$2(f(r.error)); }; /** * Chain together a sequence of computations that may fail, similar to a * `Promise`. If the first computation fails then the error will propagate * through. If it succeeds, then `f` will be applied to the value, returning a * new `Result`. */ var andThen$2 = function (f, r) { return r.ok === true ? f(r.result) : r; }; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise */ var __assign$2 = function() { __assign$2 = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign$2.apply(this, arguments); }; function __rest$2(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function isEqual$2(a, b) { if (a === b) { return true; } if (a === null && b === null) { return true; } if (typeof (a) !== typeof (b)) { return false; } if (typeof (a) === 'object') { // Array if (Array.isArray(a)) { if (!Array.isArray(b)) { return false; } if (a.length !== b.length) { return false; } for (var i = 0; i < a.length; i++) { if (!isEqual$2(a[i], b[i])) { return false; } } return true; } // Hash table var keys = Object.keys(a); if (keys.length !== Object.keys(b).length) { return false; } for (var i = 0; i < keys.length; i++) { if (!b.hasOwnProperty(keys[i])) { return false; } if (!isEqual$2(a[keys[i]], b[keys[i]])) { return false; } } return true; } } /* * Helpers */ var isJsonArray$2 = function (json) { return Array.isArray(json); }; var isJsonObject$2 = function (json) { return typeof json === 'object' && json !== null && !isJsonArray$2(json); }; var typeString$2 = function (json) { switch (typeof json) { case 'string': return 'a string'; case 'number': return 'a number'; case 'boolean': return 'a boolean'; case 'undefined': return 'undefined'; case 'object': if (json instanceof Array) { return 'an array'; } else if (json === null) { return 'null'; } else { return 'an object'; } default: return JSON.stringify(json); } }; var expectedGot$2 = function (expected, got) { return "expected " + expected + ", got " + typeString$2(got); }; var printPath$2 = function (paths) { return paths.map(function (path) { return (typeof path === 'string' ? "." + path : "[" + path + "]"); }).join(''); }; var prependAt$2 = function (newAt, _a) { var at = _a.at, rest = __rest$2(_a, ["at"]); return (__assign$2({ at: newAt + (at || '') }, rest)); }; /** * Decoders transform json objects with unknown structure into known and * verified forms. You can create objects of type `Decoder<A>` with either the * primitive decoder functions, such as `boolean()` and `string()`, or by * applying higher-order decoders to the primitives, such as `array(boolean())` * or `dict(string())`. * * Each of the decoder functions are available both as a static method on * `Decoder` and as a function alias -- for example the string decoder is * defined at `Decoder.string()`, but is also aliased to `string()`. Using the * function aliases exported with the library is recommended. * * `Decoder` exposes a number of 'run' methods, which all decode json in the * same way, but communicate success and failure in different ways. The `map` * and `andThen` methods modify decoders without having to call a 'run' method. * * Alternatively, the main decoder `run()` method returns an object of type * `Result<A, DecoderError>`. This library provides a number of helper * functions for dealing with the `Result` type, so you can do all the same * things with a `Result` as with the decoder methods. */ var Decoder$2 = /** @class */ (function () { /** * The Decoder class constructor is kept private to separate the internal * `decode` function from the external `run` function. The distinction * between the two functions is that `decode` returns a * `Partial<DecoderError>` on failure, which contains an unfinished error * report. When `run` is called on a decoder, the relevant series of `decode` * calls is made, and then on failure the resulting `Partial<DecoderError>` * is turned into a `DecoderError` by filling in the missing information. * * While hiding the constructor may seem restrictive, leveraging the * provided decoder combinators and helper functions such as * `andThen` and `map` should be enough to build specialized decoders as * needed. */ function Decoder(decode) { var _this = this; this.decode = decode; /** * Run the decoder and return a `Result` with either the decoded value or a * `DecoderError` containing the json input, the location of the error, and * the error message. * * Examples: * ``` * number().run(12) * // => {ok: true, result: 12} * * string().run(9001) * // => * // { * // ok: false, * // error: { * // kind: 'DecoderError', * // input: 9001, * // at: 'input', * // message: 'expected a string, got 9001' * // } * // } * ``` */ this.run = function (json) { return mapError$2(function (error) { return ({ kind: 'DecoderError', input: json, at: 'input' + (error.at || ''), message: error.message || '' }); }, _this.decode(json)); }; /** * Run the decoder as a `Promise`. */ this.runPromise = function (json) { return asPromise$2(_this.run(json)); }; /** * Run the decoder and return the value on success, or throw an exception * with a formatted error string. */ this.runWithException = function (json) { return withException$2(_this.run(json)); }; /** * Construct a new decoder that applies a transformation to the decoded * result. If the decoder succeeds then `f` will be applied to the value. If * it fails the error will propagated through. * * Example: * ``` * number().map(x => x * 5).run(10) * // => {ok: true, result: 50} * ``` */ this.map = function (f) { return new Decoder(function (json) { return map$2(f, _this.decode(json)); }); }; /** * Chain together a sequence of decoders. The first decoder will run, and * then the function will determine what decoder to run second. If the result * of the first decoder succeeds then `f` will be applied to the decoded * value. If it fails the error will propagate through. * * This is a very powerful method -- it can act as both the `map` and `where` * methods, can improve error messages for edge cases, and can be used to * make a decoder for custom types. * * Example of adding an error message: * ``` * const versionDecoder = valueAt(['version'], number()); * const infoDecoder3 = object({a: boolean()}); * * const decoder = versionDecoder.andThen(version => { * switch (version) { * case 3: * return infoDecoder3; * default: * return fail(`Unable to decode info, version ${version} is not supported.`); * } * }); * * decoder.run({version: 3, a: true}) * // => {ok: true, result: {a: true}} * * decoder.run({version: 5, x: 'abc'}) * // => * // { * // ok: false, * // error: {... message: 'Unable to decode info, version 5 is not supported.'} * // } * ``` * * Example of decoding a custom type: * ``` * // nominal type for arrays with a length of at least one * type NonEmptyArray<T> = T[] & { __nonEmptyArrayBrand__: void }; * * const nonEmptyArrayDecoder = <T>(values: Decoder<T>): Decoder<NonEmptyArray<T>> => * array(values).andThen(arr => * arr.length > 0 * ? succeed(createNonEmptyArray(arr)) * : fail(`expected a non-empty array, got an empty array`) * ); * ``` */ this.andThen = function (f) { return new Decoder(function (json) { return andThen$2(function (value) { return f(value).decode(json); }, _this.decode(json)); }); }; /** * Add constraints to a decoder _without_ changing the resulting type. The * `test` argument is a predicate function which returns true for valid * inputs. When `test` fails on an input, the decoder fails with the given * `errorMessage`. * * ``` * const chars = (length: number): Decoder<string> => * string().where( * (s: string) => s.length === length, * `expected a string of length ${length}` * ); * * chars(5).run('12345') * // => {ok: true, result: '12345'} * * chars(2).run('HELLO') * // => {ok: false, error: {... message: 'expected a string of length 2'}} * * chars(12).run(true) * // => {ok: false, error: {... message: 'expected a string, got a boolean'}} * ``` */ this.where = function (test, errorMessage) { return _this.andThen(function (value) { return (test(value) ? Decoder.succeed(value) : Decoder.fail(errorMessage)); }); }; } /** * Decoder primitive that validates strings, and fails on all other input. */ Decoder.string = function () { return new Decoder(function (json) { return typeof json === 'string' ? ok$2(json) : err$2({ message: expectedGot$2('a string', json) }); }); }; /** * Decoder primitive that validates numbers, and fails on all other input. */ Decoder.number = function () { return new Decoder(function (json) { return typeof json === 'number' ? ok$2(json) : err$2({ message: expectedGot$2('a number', json) }); }); }; /** * Decoder primitive that validates booleans, and fails on all other input. */ Decoder.boolean = function () { return new Decoder(function (json) { return typeof json === 'boolean' ? ok$2(json) : err$2({ message: expectedGot$2('a boolean', json) }); }); }; Decoder.constant = function (value) { return new Decoder(function (json) { return isEqual$2(json, value) ? ok$2(value) : err$2({ message: "expected " + JSON.stringify(value) + ", got " + JSON.stringify(json) }); }); }; Decoder.object = function (decoders) { return new Decoder(function (json) { if (isJsonObject$2(json) && decoders) { var obj = {}; for (var key in decoders) { if (decoders.hasOwnProperty(key)) { var r = decoders[key].decode(json[key]); if (r.ok === true) { // tslint:disable-next-line:strict-type-predicates if (r.result !== undefined) { obj[key] = r.result; } } else if (json[key] === undefined) { return err$2({ message: "the key '" + key + "' is required but was not present" }); } else { return err$2(prependAt$2("." + key, r.error)); } } } return ok$2(obj); } else if (isJsonObject$2(json)) { return ok$2(json); } else { return err$2({ message: expectedGot$2('an object', json) }); } }); }; Decoder.array = function (decoder) { return new Decoder(function (json) { if (isJsonArray$2(json) && decoder) { var decodeValue_1 = function (v, i) { return mapError$2(function (err$$1) { return prependAt$2("[" + i + "]", err$$1); }, decoder.decode(v)); }; return json.reduce(function (acc, v, i) { return map2$2(function (arr, result) { return arr.concat([result]); }, acc, decodeValue_1(v, i)); }, ok$2([])); } else if (isJsonArray$2(json)) { return ok$2(json); } else { return err$2({ message: expectedGot$2('an array', json) }); } }); }; Decoder.tuple = function (decoders) { return new Decoder(function (json) { if (isJsonArray$2(json)) { if (json.length !== decoders.length) { return err$2({ message: "expected a tuple of length " + decoders.length + ", got one of length " + json.length }); } var result = []; for (var i = 0; i < decoders.length; i++) { var nth = decoders[i].decode(json[i]); if (nth.ok) { result[i] = nth.result; } else { return err$2(prependAt$2("[" + i + "]", nth.error)); } } return ok$2(result); } else { return err$2({ message: expectedGot$2("a tuple of length " + decoders.length, json) }); } }); }; Decoder.union = function (ad, bd) { var decoders = []; for (var _i = 2; _i < arguments.length; _i++) { decoders[_i - 2] = arguments[_i]; } return Decoder.oneOf.apply(Decoder, [ad, bd].concat(decoders)); }; Decoder.intersection = function (ad, bd) { var ds = []; for (var _i = 2; _i < arguments.length; _i++) { ds[_i - 2] = arguments[_i]; } return new Decoder(function (json) { return [ad, bd].concat(ds).reduce(function (acc, decoder) { return map2$2(Object.assign, acc, decoder.decode(json)); }, ok$2({})); }); }; /** * Escape hatch to bypass validation. Always succeeds and types the result as * `any`. Useful for defining decoders incrementally, particularly for * complex objects. * * Example: * ``` * interface User { * name: string; * complexUserData: ComplexType; * } * * const userDecoder: Decoder<User> = object({ * name: string(), * complexUserData: anyJson() * }); * ``` */ Decoder.anyJson = function () { return new Decoder(function (json) { return ok$2(json); }); }; /** * Decoder identity function which always succeeds and types the result as * `unknown`. */ Decoder.unknownJson = function () { return new Decoder(function (json) { return ok$2(json); }); }; /** * Decoder for json objects where the keys are unknown strings, but the values * should all be of the same type. * * Example: * ``` * dict(number()).run({chocolate: 12, vanilla: 10, mint: 37}); * // => {ok: true, result: {chocolate: 12, vanilla: 10, mint: 37}} * ``` */ Decoder.dict = function (decoder) { return new Decoder(function (json) { if (isJsonObject$2(json)) { var obj = {}; for (var key in json) { if (json.hasOwnProperty(key)) { var r = decoder.decode(json[key]); if (r.ok === true) { obj[key] = r.result; } else { return err$2(prependAt$2("." + key, r.error)); } } } return ok$2(obj); } else { return err$2({ message: expectedGot$2('an object', json) }); } }); }; /** * Decoder for values that may be `undefined`. This is primarily helpful for * decoding interfaces with optional fields. * * Example: * ``` * interface User { * id: number; * isOwner?: boolean; * } * * const decoder: Decoder<User> = object({ * id: number(), * isOwner: optional(boolean()) * }); * ``` */ Decoder.optional = function (decoder) { return new Decoder(function (json) { return (json === undefined || json === null ? ok$2(undefined) : decoder.decode(json)); }); }; /** * Decoder that attempts to run each decoder in `decoders` and either succeeds * with the first successful decoder, or fails after all decoders have failed. * * Note that `oneOf` expects the decoders to all have the same return type, * while `union` creates a decoder for the union type of all the input * decoders. * * Examples: * ``` * oneOf(string(), number().map(String)) * oneOf(constant('start'), constant('stop'), succeed('unknown')) * ``` */ Decoder.oneOf = function () { var decoders = []; for (var _i = 0; _i < arguments.length; _i++) { decoders[_i] = arguments[_i]; } return new Decoder(function (json) { var errors = []; for (var i = 0; i < decoders.length; i++) { var r = decoders[i].decode(json); if (r.ok === true) { return r; } else { errors[i] = r.error; } } var errorsList = errors .map(function (error) { return "at error" + (error.at || '') + ": " + error.message; }) .join('", "'); return err$2({ message: "expected a value matching one of the decoders, got the errors [\"" + errorsList + "\"]" }); }); }; /** * Decoder that always succeeds with either the decoded value, or a fallback * default value. */ Decoder.withDefault = function (defaultValue, decoder) { return new Decoder(function (json) { return ok$2(withDefault$2(defaultValue, decoder.decode(json))); }); }; /** * Decoder that pulls a specific field out of a json structure, instead of * decoding and returning the full structure. The `paths` array describes the * object keys and array indices to traverse, so that values can be pulled out * of a nested structure. * * Example: * ``` * const decoder = valueAt(['a', 'b', 0], string()); * * decoder.run({a: {b: ['surprise!']}}) * // => {ok: true, result: 'surprise!'} * * decoder.run({a: {x: 'cats'}}) * // => {ok: false, error: {... at: 'input.a.b[0]' message: 'path does not exist'}} * ``` * * Note that the `decoder` is ran on the value found at the last key in the * path, even if the last key is not found. This allows the `optional` * decoder to succeed when appropriate. * ``` * const optionalDecoder = valueAt(['a', 'b', 'c'], optional(string())); * * optionalDecoder.run({a: {b: {c: 'surprise!'}}}) * // => {ok: true, result: 'surprise!'} * * optionalDecoder.run({a: {b: 'cats'}}) * // => {ok: false, error: {... at: 'input.a.b.c' message: 'expected an object, got "cats"'} * * optionalDecoder.run({a: {b: {z: 1}}}) * // => {ok: true, result: undefined} * ``` */ Decoder.valueAt = function (paths, decoder) { return new Decoder(function (json) { var jsonAtPath = json; for (var i = 0; i < paths.length; i++) { if (jsonAtPath === undefined) { return err$2({ at: printPath$2(paths.slice(0, i + 1)), message: 'path does not exist' }); } else if (typeof paths[i] === 'string' && !isJsonObject$2(jsonAtPath)) { return err$2({ at: printPath$2(paths.slice(0, i + 1)), message: expectedGot$2('an object', jsonAtPath) }); } else if (typeof paths[i] === 'number' && !isJsonArray$2(jsonAtPath)) { return err$2({ at: printPath$2(paths.slice(0, i + 1)), message: expectedGot$2('an array', jsonAtPath) }); } else { jsonAtPath = jsonAtPath[paths[i]]; } } return mapError$2(function (error) { return jsonAtPath === undefined ? { at: printPath$2(paths), message: 'path does not exist' } : prependAt$2(printPath$2(paths), error); }, decoder.decode(jsonAtPath)); }); }; /** * Decoder that ignores the input json and always succeeds with `fixedValue`. */ Decoder.succeed = function (fixedValue) { return new Decoder(function (json) { return ok$2(fixedValue); }); }; /** * Decoder that ignores the input json and always fails with `errorMessage`. */ Decoder.fail = function (errorMessage) { return new Decoder(function (json) { return err$2({ message: errorMessage }); }); }; /** * Decoder that allows for validating recursive data structures. Unlike with * functions, decoders assigned to variables can't reference themselves * before they are fully defined. We can avoid prematurely referencing the * decoder by wrapping it in a function that won't be called until use, at * which point the decoder has been defined. * * Example: * ``` * interface Comment { * msg: string; * replies: Comment[]; * } * * const decoder: Decoder<Comment> = object({ * msg: string(), * replies: lazy(() => array(decoder)) * }); * ``` */ Decoder.lazy = function (mkDecoder) { return new Decoder(function (json) { return mkDecoder().decode(json); }); }; return Decoder; }()); /* tslint:disable:variable-name */ /** See `Decoder.string` */ var string$2 = Decoder$2.string; /** See `Decoder.number` */ var number$2 = Decoder$2.number; /** See `Decoder.boolean` */ var boolean$2 = Decoder$2.boolean; /** See `Decoder.anyJson` */ var anyJson$2 = Decoder$2.anyJson; /** See `Decoder.unknownJson` */ Decoder$2.unknownJson; /** See `Decoder.constant` */ var constant$2 = Decoder$2.constant; /** See `Decoder.object` */ var object$2 = Decoder$2.object; /** See `Decoder.array` */ var array$2 = Decoder$2.array; /** See `Decoder.tuple` */ Decoder$2.tuple; /** See `Decoder.dict` */ Decoder$2.dict; /** See `Decoder.optional` */ var optional$2 = Decoder$2.optional; /** See `Decoder.oneOf` */ var oneOf$1 = Decoder$2.oneOf; /** See `Decoder.union` */ var union$1 = Decoder$2.union; /** See `Decoder.intersection` */ var intersection = Decoder$2.intersection; /** See `Decoder.withDefault` */ Decoder$2.withDefault; /** See `Decoder.valueAt` */ Decoder$2.valueAt; /** See `Decoder.succeed` */ Decoder$2.succeed; /** See `Decoder.fail` */ Decoder$2.fail; /** See `Decoder.lazy` */ var lazy = Decoder$2.lazy; const defaultConfig = { gateway: { webPlatform: {} }, libraries: [], exposeAPI: true, }; const defaultWidgetConfig = { awaitFactory: true, enable: false, timeout: 5 * 1000, }; const defaultModalsConfig = { alerts: { enabled: false }, dialogs: { enabled: false }, awaitFactory: true, timeout: 5 * 1000, }; const defaultIntentResolverConfig = { enable: false, timeout: 5 * 1000, awaitFactory: true, }; const Glue42CoreMessageTypes = { platformUnload: { name: "platformUnload" }, transportSwitchRequest: { name: "transportSwitchRequest" }, transportSwitchResponse: { name: "transportSwitchResponse" }, getCurrentTransport: { name: "getCurrentTransport" }, getCurrentTransportResponse: { name: "getCurrentTransportResponse" }, checkPreferredLogic: { name: "checkPreferredLogic" }, checkPreferredConnection: { name: "checkPreferredConnection" }, checkPreferredLogicResponse: { name: "checkPreferredLogicResponse" }, checkPreferredConnectionResponse: { name: "checkPreferredConnectionResponse" } }; const webPlatformTransportName = "web-platform"; const latestFDC3Type = "latest_fdc3_type"; const errorChannel = new MessageChannel(); const REQUEST_WIDGET_READY = "requestWidgetFactoryReady"; const REQUEST_MODALS_UI_FACTORY_READY = "requestModalsUIFactoryReady"; const MAX_SET_TIMEOUT_DELAY = 2147483647; const REQUEST_INTENT_RESOLVER_UI_FACTORY_READY = "requestIntentResolverUIFactoryReady"; const extractErrorMsg = (error) => { if (typeof error === "string") { return error; } if (error?.message) { return typeof error.message === "string" ? error.message : JSON.stringify(error.message); } return JSON.stringify(error); }; const runDecoderWithIOError = (decoder, json) => { try { const result = decoder.runWithException(json); return result; } catch (error) { return ioError.raiseError(error, true); } }; const getSupportedOperationsNames = (operations) => { return Object.keys(operations).filter((operation) => (operations)[operation].execute); }; const handleOperationCheck = (supportedOperations, operationName) => { const isSupported = supportedOperations.some((operation) => operation.toLowerCase() === operationName.toLowerCase()); return { isSupported }; }; const getSafeTimeoutDelay = (delay) => { return Math.min(delay, MAX_SET_TIMEOUT_DELAY); }; const wrapPromise = () => { let wrapperResolve; let wrapperReject; const promise = new Promise((resolve, reject) => { wrapperResolve = resolve; wrapperReject = reject; }); return { promise, resolve: wrapperResolve, reject: wrapperReject }; }; class IOError { raiseError(error, shouldThrowOriginalError) { const errorMessage = extractErrorMsg(error); errorChannel.port1.postMessage(errorMessage); if (shouldThrowOriginalError) { throw error; } throw new Error(errorMessage); } } const ioError = new IOError(); const connectBrowserAppProps = ["name", "title", "version", "customProperties", "icon", "caption", "type"]; const fdc3v2AppProps = ["appId", "name", "type", "details", "version", "title", "tooltip", "lang", "description", "categories", "icons", "screenshots", "contactEmail", "moreInfo", "publisher", "customConfig", "hostManifests", "interop", "localizedVersions"]; /** * Wraps values in an `Ok` type. * * Example: `ok(5) // => {ok: true, result: 5}` */ var ok$1 = function (result) { return ({ ok: true, result: result }); }; /** * Wraps errors in an `Err` type. * * Example: `err('on fire') // => {ok: false, error: 'on fire'}` */ var err$1 = function (error) { return ({ ok: false, error: error }); }; /** * Create a `Promise` that either resolves with the result of `Ok` or rejects * with the error of `Err`. */ var asPromise$1 = function (r) { return r.ok === true ? Promise.resolve(r.result) : Promise.reject(r.error); }; /** * Unwraps a `Result` and returns either the result of an `Ok`, or * `defaultValue`. * * Example: * ``` * Result.withDefault(5, number().run(json)) * ``` * * It would be nice if `Decoder` had an instance method that mirrored this * function. Such a method would look something like this: * ``` * class Decoder<A> { * runWithDefault = (defaultValue: A, json: any): A => * Result.withDefault(defaultValue, this.run(json)); * } * * number().runWithDefault(5, json) * ``` * Unfortunately, the type of `defaultValue: A` on the method causes issues * with type inference on the `object` decoder in some situations. While these * inference issues can be solved by providing the optional type argument for * `object`s, the extra trouble and confusion doesn't seem worth it. */ var withDefault$1 = function (defaultValue, r) { return r.ok === true ? r.result : defaultValue; }; /** * Return the successful result, or throw an error. */ var withException$1 = function (r) { if (r.ok === true) { return r.result; } else { throw r.error; } }; /** * Apply `f` to the result of an `Ok`, or pass the error through. */ var map$1 = function (f, r) { return r.ok === true ? ok$1(f(r.result)) : r; }; /** * Apply `f` to the result of two `Ok`s, or pass an error through. If both * `Result`s are errors then the first one is returned. */ var map2$1 = function (f, ar, br) { return ar.ok === false ? ar : br.ok === false ? br : ok$1(f(ar.result, br.result)); }; /** * Apply `f` to the error of an `Err`, or pass the success through. */ var mapError$1 = function (f, r) { return r.ok === true ? r : err$1(f(r.error)); }; /** * Chain together a sequence of computations that may fail, similar to a * `Promise`. If the first computation fails then the error will propagate * through. If it succeeds, then `f` will be applied to the value, returning a * new `Result`. */ var andThen$1 = function (f, r) { return r.ok === true ? f(r.result) : r; }; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ /* global Reflect, Promise */ var __assign$1 = function() { __assign$1 = Object.assign || function __assign(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign$1.apply(this, arguments); }; function __rest$1(s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; } function isEqual$1(a, b) { if (a === b) { return true; } if (a === null && b === null) { return true; } if (typeof (a) !== typeof (b)) { return false; } if (typeof (a) === 'object') { // Array if (Array.isArray(a)) { if (!Array.isArray(b)) { return false; } if (a.length !== b.length) { return false; } for (var i = 0; i < a.length; i++) { if (!isEqual$1(a[i], b[i])) { return false; } } return true; } // Hash table var keys = Object.keys(a); if (keys.length !== Object.keys(b).length) { return false; } for (var i = 0; i < keys.length; i++) { if (!b.hasOwnProperty(keys[i])) { return false; } if (!isEqual$1(a[keys[i]], b[keys[i]])) { return false; } } return true; } } /* * Helpers */ var isJsonArray$1 = function (json) { return Array.isArray(json); }; var isJsonObject$1 = function (json) { return typeof json === 'object' && json !== null && !isJsonArray$1(json); }; var typeString$1 = function (json) { switch (typeof json) { case 'string': return 'a string'; case 'number': return 'a number'; case 'boolean': return 'a boolean'; case 'undefined': return 'undefined'; case 'object': if (json instanceof Array) { return 'an array'; } else if (json === null) { return 'null'; } else { return 'an object'; } default: return JSON.stringify(json); } }; var expectedGot$1 = function (expected, got) { return "expected " + expected + ", got " + typeString$1(got); }; var printPath$1 = function (paths) { return paths.map(function (path) { return (typeof path === 'string' ? "." + path : "[" + path + "]"); }).join(''); }; var prependAt$1 = function (newAt, _a) { var at = _a.at, rest = __rest$1(_a, ["at"]); return (__assign$1({ at: newAt + (at || '') }, rest)); }; /** * Decoders transform json objects with unknown structure into known and * verified forms. You can create objects of type `Decoder<A>` with either the * primitive decoder functions, such as `boolean()` and `string()`, or by * applying higher-order decoders to the primitives, such as `array(boolean())` * or `dict(string())`. * * Each of the decoder functions are available both as a static method on * `Decoder` and as a function alias -- for example the string decoder is * defined at `Decoder.string()`, but is also aliased to `string()`. Using the * function aliases exported with the library is recommended. * * `Decoder` exposes a number of 'run' methods, which all decode json in the * same way, but communicate success and failure in different ways. The `map` * and `andThen` methods modify decoders without having to call a 'run' method. * * Alternatively, the main decoder `run()` method returns an object of type * `Result<A, DecoderError>`. This library provides a number of helper * functions for dealing with the `Result` type, so you can do all the same * things with a `Result` as with the decoder methods. */ var Decoder$1 = /** @class */ (function () { /** * The Decoder class constructor is kept private to separate the internal * `decode` function from the external `run` function. The distinction * between the two functions is that `decode` returns a * `Partial<DecoderError>` on failure, which contains an unfinished error * report. When `run` is called on a decoder, the relevant series of `decode` * calls is made, and then on failure the resulting `Partial<DecoderError>` * is turned into a `DecoderError` by filling in the missing information. * * While hiding the constructor may seem restrictive, leveraging the * provided decoder combinators and helper functions such as * `andThen` and `map` should be enough to build specialized decoders as * needed. */ function Decoder(decode) { var _this = this; this.decode = decode; /** * Run the decoder and return a `Result` with either the decoded value or a * `DecoderError` containing the json input, the location of the error, and * the error message. * * Examples: * ``` * number().run(12) * // => {ok: true, result: 12} * * string().run(9001) * // => * // { * // ok: false, * // error: { * // kind: 'DecoderError', * // input: 9001, * // at: 'input', * // message: 'expected a string, got 9001' * // } * // } * ``` */ this.run = function (json) { return mapError$1(function (error) { return ({ kind: 'DecoderError', input: json, at: 'input' + (error.at || ''), message: error.message || '' }); }, _this.decode(json)); }; /** * Run the decoder as a `Promise`. */ this.runPromise = function (json) { return asPromise$1(_this.run(json)); }; /** * Run the decoder and return the value on success, or throw an exception * with a formatted error string. */ this.runWithException = function (json) { return withException$1(_this.run(json)); }; /** * Construct a new decoder that applies a transformation to the decoded * result. If the decoder succeeds then `f` will be applied to the value. If * it fails the error will propagated through. * * Example: * ``` * number().map(x => x * 5).run(10) * // => {ok: true, result: 50} * ``` */ this.map = function (f) { return new Decoder(function (json) { return map$1(f, _this.decode(json)); }); }; /** * Chain together a sequence of decoders. The first decoder will run, and * then the function will determine what decoder to run second. If the result * of the first decoder succeeds then `f` will be applied to the decoded * value. If it fails the error will propagate through. * * This is a very powerful method -- it can act as both the `map` and `where` * methods, can improve error messages for edge cases, and can be used to * make a decoder for custom types. * * Example of adding an error message: * ``` * const versionDecoder = valueAt(['version'], number()); * const infoDecoder3 = object({a: boolean()}); * * const decoder = versionDecoder.andThen(version => { * switch (version) { * case 3: * return infoDecoder3; * defaul