@adpt/core
Version:
AdaptJS core library
129 lines • 5.29 kB
JavaScript
;
/*
* 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 graphql_1 = require("graphql");
const ld = tslib_1.__importStar(require("lodash"));
function isObject(x) {
return ld.isObject(x);
}
function reconstituteObservations(observerName, candidate) {
if (!isObject(candidate))
throw new Error(`Stored observation is not an observer response for ${observerName}`);
const reference = { data: {}, context: {} };
const illegalKeys = Object.keys(candidate).filter((key) => !(key in reference));
if (illegalKeys.length !== 0) {
throw new Error(`Illegal keys ${illegalKeys} in observations for observer ${observerName}`);
}
return candidate;
}
exports.reconstituteObservations = reconstituteObservations;
function hasExecutedQueryShape(candidate) {
if (!isObject(candidate))
return false;
if (!("query" in candidate))
return false;
const query = candidate.query;
const vars = candidate.variables;
if (vars && !ld.isObject(vars))
return false;
if (vars && ld.isArray(vars))
return false;
if (!ld.isString(query))
return false;
return true;
}
exports.hasExecutedQueryShape = hasExecutedQueryShape;
function reconstituteExecutedQuery(observerName, candidate) {
if (!isObject(candidate))
throw new Error(`Stored executed query is not a legal query for ${observerName}`);
const reference = { query: undefined, variables: [] };
const illegalKeys = Object.keys(candidate).filter((key) => !(key in reference));
if (illegalKeys.length !== 0) {
throw new Error(`Illegal keys ${illegalKeys} in stored queries for observer ${observerName}`);
}
if (!hasExecutedQueryShape(candidate)) {
throw new Error(`Invalid shape for stored executed query for observer ${observerName}`);
}
const query = graphql_1.parse(candidate.query);
const variables = candidate.variables;
return utils_1.removeUndef({ query, variables });
}
exports.reconstituteExecutedQuery = reconstituteExecutedQuery;
function reconstituteExecutedQueries(observerName, candidate) {
if (!ld.isArray(candidate)) {
throw new Error(`Stored executed queries is not an observer response for ${observerName}`);
}
return candidate.map((val) => reconstituteExecutedQuery(observerName, val));
}
exports.reconstituteExecutedQueries = reconstituteExecutedQueries;
function reconstituteObserverObservations(observerName, candidate) {
if (!isObject(candidate))
throw new Error(`Stored observation is not an observer response for ${observerName}`);
const ret = { observations: {}, queries: [] };
for (const key in candidate) {
if (!Object.hasOwnProperty.call(candidate, key))
continue;
const val = candidate[key];
switch (key) {
case "observations":
ret.observations = reconstituteObservations(observerName, val);
break;
case "queries":
ret.queries = reconstituteExecutedQueries(observerName, val);
break;
default:
throw new Error(`Unknown key ${key} for observations for observer ${observerName}`);
}
}
return ret;
}
exports.reconstituteObserverObservations = reconstituteObserverObservations;
function reconstituteAllObservations(candidate) {
if (!isObject(candidate))
throw new Error(`Stored object is not a set of observations for all observers`);
const ret = {};
for (const key in candidate) {
if (!Object.hasOwnProperty.call(candidate, key))
continue;
ret[key] = reconstituteObserverObservations(key, candidate[key]);
}
return ret;
}
exports.reconstituteAllObservations = reconstituteAllObservations;
function prepareExecutedQuery(query) {
const queryStr = graphql_1.print(query.query);
return utils_1.removeUndef({ query: queryStr, variables: query.variables });
}
exports.prepareExecutedQuery = prepareExecutedQuery;
function prepareAllObservationsForJson(observations) {
const ret = {};
for (const observerName in observations) {
if (!Object.hasOwnProperty.call(observations, observerName))
continue;
const o = observations[observerName];
const obsResp = o.observations;
ret[observerName] = {
observations: obsResp,
queries: o.queries.map((q) => prepareExecutedQuery(q))
};
}
return ret;
}
exports.prepareAllObservationsForJson = prepareAllObservationsForJson;
//# sourceMappingURL=serialize.js.map