UNPKG

@adpt/core

Version:
118 lines 4.45 kB
"use strict"; /* * Copyright 2018-2019 Unbounded Systems, LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const utils_1 = require("@adpt/utils"); const lodash_1 = tslib_1.__importDefault(require("lodash")); const p_settle_1 = tslib_1.__importDefault(require("p-settle")); const ts_custom_error_1 = require("ts-custom-error"); const util_1 = tslib_1.__importDefault(require("util")); const jsx_1 = require("../src/jsx"); class NoStatusAvailable extends ts_custom_error_1.CustomError { constructor(message) { super("No Status Available: " + (message ? message : "<no message>")); } } exports.NoStatusAvailable = NoStatusAvailable; function hasChildren(x) { return "children" in x; } async function noStatusOnError(f) { try { // tslint:disable-next-line:await-promise return (await f()); //FIXME(manishv) update when we fix status types } catch (e) { return errorToNoStatus(e); } } exports.noStatusOnError = noStatusOnError; function errorToNoStatus(err) { if (lodash_1.default.isError(err)) return { noStatus: err.message }; return { noStatus: util_1.default.inspect(err) }; } exports.errorToNoStatus = errorToNoStatus; function gqlGetOriginalErrors(err) { if (!utils_1.isMultiError(err)) return undefined; return err.errors.map((e) => { const orig = utils_1.isObject(e) && e.name === "GraphQLError" && e.originalError; return orig || e; }); } exports.gqlGetOriginalErrors = gqlGetOriginalErrors; async function defaultChildStatus(props, mgr, data) { let childArray = data.origChildren; if (childArray === undefined && hasChildren(props)) { childArray = jsx_1.childrenToArray(props.children); } if (childArray !== undefined) { const children = childArray.filter(jsx_1.isMountedElement); const childStatusP = children.map((c) => noStatusOnError(() => c.status(mgr))); const childStatus = await Promise.all(childStatusP); return { childStatus }; } return { noStatus: "element has no children" }; } exports.defaultChildStatus = defaultChildStatus; async function defaultStatus(props, mgr, data) { const succ = data.successor; if (succ === undefined) return defaultChildStatus(props, mgr, data); if (succ === null) return { noStatus: "successor was null" }; return noStatusOnError(() => succ.status()); } exports.defaultStatus = defaultStatus; function noTransform(val) { if (val == null) return { noStatus: `Error: parent status is null` }; return val; } exports.noTransform = noTransform; async function mergeDefaultChildStatus(props, parentStatus, mgr, data, transformParentStatus = noTransform) { const childrenP = defaultChildStatus(props, mgr, data); const [parentResult, childrenResult] = await p_settle_1.default([Promise.resolve(parentStatus), childrenP]); if (childrenResult.isRejected) throw childrenResult.reason; const children = childrenResult.value; if (children == null) throw new Error(`Error: status for children is null`); let stat; if (parentResult.isFulfilled) { stat = transformParentStatus(parentResult.value); } else { const err = parentResult.reason; if (!lodash_1.default.isError(err)) throw err; stat = (err instanceof utils_1.MultiError && err.errors.length === 1 && err.errors[0].message) ? { noStatus: err.errors[0].message } : stat = { noStatus: err.message }; } if (Array.isArray(children.childStatus) && children.childStatus.length > 0) { stat.childStatus = children.childStatus; } return stat; } exports.mergeDefaultChildStatus = mergeDefaultChildStatus; //# sourceMappingURL=status.js.map