fig-tree-evaluator
Advanced tools
Comparing version 2.4.3 to 2.4.9
@@ -54,3 +54,3 @@ "use strict"; | ||
var evaluatorFunction = function (input, config) { return __awaiter(void 0, void 0, void 0, function () { | ||
var options, operators, operatorAliases, expression, _a, fallback, returnErrorAsString, _b, fragment, parameters, fragmentReplacement, _c, operator, _d, _e, requiredProperties, propertyAliases, evaluate, parseChildren, _f, _g, validationError, _h, _j, _k, result, err_1, _l, outputType, evaluatedOutputType, _m, err_2, _o; | ||
var options, operators, operatorAliases, expression, isOperator, isFragment, _a, fallback, returnErrorAsString, _b, fragment, parameters, fragmentReplacement, _c, operator, _d, _e, requiredProperties, propertyAliases, evaluate, parseChildren, _f, _g, validationError, _h, _j, _k, result, err_1, _l, outputType, evaluatedOutputType, _m, err_2, _o; | ||
var _p, _q, _r; | ||
@@ -68,3 +68,5 @@ return __generator(this, function (_s) { | ||
case 2: | ||
if (!(options.evaluateFullObject && !(0, helpers_1.isOperatorNode)(expression) && !(0, helpers_1.isFragmentNode)(expression))) return [3, 4]; | ||
isOperator = (0, helpers_1.isOperatorNode)(expression); | ||
isFragment = (0, helpers_1.isFragmentNode)(expression); | ||
if (!(options.evaluateFullObject && !isOperator && !isFragment)) return [3, 4]; | ||
_a = helpers_1.replaceAliasNodeValues; | ||
@@ -74,7 +76,10 @@ return [4, (0, helpers_1.evaluateObject)(expression, config)]; | ||
case 4: | ||
if (!(0, helpers_1.isOperatorNode)(expression) && !(0, helpers_1.isFragmentNode)(expression)) | ||
if (!isOperator && !isFragment) { | ||
if (options.supportDeprecatedValueNodes && (0, helpers_1.isObject)(expression) && 'value' in expression) | ||
return [2, expression.value]; | ||
return [2, (0, helpers_1.replaceAliasNodeValues)(expression, config)]; | ||
} | ||
fallback = expression.fallback; | ||
returnErrorAsString = (_p = options === null || options === void 0 ? void 0 : options.returnErrorAsString) !== null && _p !== void 0 ? _p : false; | ||
if (!(0, helpers_1.isFragmentNode)(expression)) return [3, 8]; | ||
if (!isFragment) return [3, 8]; | ||
return [4, (0, _operatorUtils_1.evaluateArray)([expression.fragment, expression.parameters], config)]; | ||
@@ -144,3 +149,3 @@ case 5: | ||
_l = helpers_1.fallbackOrError; | ||
return [4, (0, exports.evaluatorFunction)(fallback, config)]; | ||
return [4, (0, exports.evaluatorFunction)(expression.fallback, config)]; | ||
case 23: | ||
@@ -147,0 +152,0 @@ result = _l.apply(void 0, [_s.sent(), "Operator: ".concat(operator, "\n").concat((0, helpers_1.errorMessage)(err_1)), returnErrorAsString]); |
@@ -23,2 +23,3 @@ import { OutputType, EvaluatorNode, CombinedOperatorNode, Operator, EvaluatorOutput, FigTreeOptions, OperatorNodeUnion, FigTreeConfig } from './types'; | ||
export declare const errorMessage: (err: unknown) => string; | ||
export declare const isObject: (input: unknown) => boolean; | ||
export declare const evaluateObject: (input: EvaluatorNode, config: FigTreeConfig) => Promise<EvaluatorOutput>; |
@@ -50,3 +50,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.evaluateObject = exports.errorMessage = exports.convertOutputMethods = exports.mergeOptions = exports.checkRequiredNodes = exports.replaceAliasNodeValues = exports.evaluateNodeAliases = exports.mapPropertyAliases = exports.fallbackOrError = exports.truncateString = exports.getOperatorName = exports.isFragmentNode = exports.isOperatorNode = exports.parseIfJson = void 0; | ||
exports.evaluateObject = exports.isObject = exports.errorMessage = exports.convertOutputMethods = exports.mergeOptions = exports.checkRequiredNodes = exports.replaceAliasNodeValues = exports.evaluateNodeAliases = exports.mapPropertyAliases = exports.fallbackOrError = exports.truncateString = exports.getOperatorName = exports.isFragmentNode = exports.isOperatorNode = exports.parseIfJson = void 0; | ||
var change_case_1 = require("change-case"); | ||
@@ -182,2 +182,3 @@ var evaluate_1 = require("./evaluate"); | ||
}; | ||
exports.isObject = isObject; | ||
var evaluateObject = function (input, config) { return __awaiter(void 0, void 0, void 0, function () { | ||
@@ -188,3 +189,3 @@ var newObjectEntries, result; | ||
case 0: | ||
if (!isObject(input)) | ||
if (!(0, exports.isObject)(input)) | ||
return [2, input]; | ||
@@ -191,0 +192,0 @@ newObjectEntries = []; |
@@ -127,2 +127,4 @@ "use strict"; | ||
var newPart = curr.slice(startSliceIndex, endSliceIndex); | ||
if (newPart === '') | ||
return acc; | ||
return acc + (index === 0 ? '' : '/') + newPart; | ||
@@ -129,0 +131,0 @@ }, ''); |
@@ -93,12 +93,16 @@ "use strict"; | ||
}); }; | ||
var parseChildren = function (expression) { return __awaiter(void 0, void 0, void 0, function () { | ||
var parseChildren = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, query, _b, url, fieldNames, rest, values, variables, output; | ||
return __generator(this, function (_c) { | ||
_a = expression.children, query = _a[0], _b = _a[1], url = _b === void 0 ? '' : _b, fieldNames = _a[2], rest = _a.slice(3); | ||
values = rest.slice(0, fieldNames.length); | ||
variables = (0, _operatorUtils_1.zipArraysToObject)(fieldNames, values); | ||
output = __assign(__assign({}, expression), { query: query, url: url, variables: variables }); | ||
if (rest.length > fieldNames.length) | ||
output.returnNode = rest.pop(); | ||
return [2, output]; | ||
switch (_c.label) { | ||
case 0: return [4, (0, _operatorUtils_1.evaluateArray)(expression.children, config)]; | ||
case 1: | ||
_a = (_c.sent()), query = _a[0], _b = _a[1], url = _b === void 0 ? '' : _b, fieldNames = _a[2], rest = _a.slice(3); | ||
values = rest.slice(0, fieldNames.length); | ||
variables = (0, _operatorUtils_1.zipArraysToObject)(fieldNames, values); | ||
output = __assign(__assign({}, expression), { query: query, url: url, variables: variables }); | ||
if (rest.length > fieldNames.length) | ||
output.returnNode = rest.pop(); | ||
return [2, output]; | ||
} | ||
}); | ||
@@ -105,0 +109,0 @@ }); }; |
@@ -63,5 +63,5 @@ "use strict"; | ||
var operatorAliases = ['pgSql', 'sql', 'postgres', 'pg', 'pgDb']; | ||
var propertyAliases = { replacements: 'values' }; | ||
var propertyAliases = { replacements: 'values', queryType: 'type' }; | ||
var evaluate = function (expression, config) { return __awaiter(void 0, void 0, void 0, function () { | ||
var _a, query, values, shouldUseCache, result; | ||
var _a, query, type, values, shouldUseCache, result; | ||
var _b; | ||
@@ -71,6 +71,6 @@ var _c, _d, _e; | ||
switch (_f.label) { | ||
case 0: return [4, (0, _operatorUtils_1.evaluateArray)(__spreadArray([expression.query], (expression.values || []), true), config)]; | ||
case 0: return [4, (0, _operatorUtils_1.evaluateArray)(__spreadArray([expression.query, expression.type], (expression.values || []), true), config)]; | ||
case 1: | ||
_a = (_f.sent()), query = _a[0], values = _a.slice(1); | ||
config.typeChecker({ name: 'query', value: query, expectedType: 'string' }, { name: 'values', value: values, expectedType: 'array' }); | ||
_a = (_f.sent()), query = _a[0], type = _a[1], values = _a.slice(2); | ||
config.typeChecker({ name: 'query', value: query, expectedType: 'string' }, { name: 'values', value: values, expectedType: 'array' }, { name: 'type', value: type, expectedType: ['string', 'undefined'] }); | ||
if (!((_c = config.options) === null || _c === void 0 ? void 0 : _c.pgConnection)) | ||
@@ -81,6 +81,6 @@ throw new Error('No Postgres database connection provided'); | ||
result = (_b = config.cache).useCache.apply(_b, __spreadArray([shouldUseCache, | ||
function (query) { | ||
function (query, type) { | ||
var values = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
values[_i - 1] = arguments[_i]; | ||
for (var _i = 2; _i < arguments.length; _i++) { | ||
values[_i - 2] = arguments[_i]; | ||
} | ||
@@ -90,3 +90,3 @@ return __awaiter(void 0, void 0, void 0, function () { | ||
switch (_a.label) { | ||
case 0: return [4, processPgSQL(__spreadArray([query], values, true), config.options.pgConnection, expression === null || expression === void 0 ? void 0 : expression.type)]; | ||
case 0: return [4, processPgSQL(__spreadArray([query], values, true), config.options.pgConnection, type)]; | ||
case 1: return [2, _a.sent()]; | ||
@@ -97,3 +97,4 @@ } | ||
}, | ||
query], values, false)); | ||
query, | ||
type], values, false)); | ||
return [2, result]; | ||
@@ -100,0 +101,0 @@ } |
@@ -31,2 +31,3 @@ import FigTreeCache from './cache'; | ||
maxCacheSize?: number; | ||
supportDeprecatedValueNodes?: boolean; | ||
} | ||
@@ -33,0 +34,0 @@ export interface FigTreeConfig { |
{ | ||
"name": "fig-tree-evaluator", | ||
"version": "2.4.3", | ||
"version": "2.4.9", | ||
"description": "Module to evaluate JSON-structured expression trees", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -108,3 +108,3 @@ # fig-tree-evaluator | ||
Which would be represented diagramatically with the following expression tree: | ||
Which would be represented diagrammatically with the following expression tree: | ||
@@ -139,2 +139,8 @@  | ||
FigTreeEvaluator is written in **Typescript**, and the following types are available to import from the package: | ||
- `FigTreeOptions`: `options` object, as per [options](#available-options) below | ||
- `Operator`: string literal canonical [Operator](#operator-nodes) names (`AND`, `OR`, `EQUAL`, etc.) | ||
- `EvaluatorNode`: Evaluator input | ||
- `EvaluatorOutput` | ||
## Available options | ||
@@ -147,6 +153,6 @@ | ||
- `fragments` -- commonly-used expressions (with optional parameters) that can be re-used in any other expression. See [Fragments](#fragments) | ||
- `pgConnection` -- if you wish to make calls to a Postgres database using the [`pgSQL` operator](#pg_sql), pass a [node-postres](https://node-postgres.com/) connection object here. | ||
- `pgConnection` -- if you wish to make calls to a Postgres database using the [`pgSQL` operator](#pg_sql), pass a [node-postgres](https://node-postgres.com/) connection object here. | ||
- `graphQLConnection` -- a GraphQL connection object, if using the [`graphQL` operator](#graphql). See operator details below. | ||
- `baseEndpoint` -- A general http headers object that will be passed to *all* http-based operators (`GET`, `POST`, `GraphQL`). Useful if all http queries are to a common server -- then each individual node will only require a relative url. See specific operator for more details. | ||
- `headers` -- A general http headers object that will be passed to *all* http-based operators. Useful for authenticatian headers, for example. Each operator and instance can have its own headers, though, so see specific operator reference for details. | ||
- `headers` -- A general http headers object that will be passed to *all* http-based operators. Useful for authentication headers, for example. Each operator and instance can have its own headers, though, so see specific operator reference for details. | ||
- `returnErrorAsString` -- by default the evaluator will throw errors with invalid evaluation expressions (with helpful error messages indicating the node which threw the error and what the problem was). But if you have `returnErrorAsString: true` set, the evaluator will never throw, but instead return error messages as a valid string output. (See also the [`fallback`](#other-common-properties) parameter below) | ||
@@ -1049,3 +1055,3 @@ - `allowJSONStringInput` -- the evaluator is expecting the input expression to be a javascript object. However, it will also accept JSON strings if this option is set to `true`. We have to perform additional logic on every evaluation input to determine if a string is a JSON expression or a standard string, so this is skipped by default for performance reasons. However, if you want to send (for example) user input directly to the evaluator without running it through your own `JSON.parse()`, then enable this option. | ||
- `values` (or `replacements`): (array) -- replacements for the `query` parameters | ||
- `type`: (`"array" | "string" | "number"`) -- determines the shape of the resulting data. To quote `node-postgres`: | ||
- `type` (or `queryType`): (`"array" | "string" | "number"`) -- determines the shape of the resulting data. To quote `node-postgres`: | ||
> By default node-postgres reads rows and collects them into JavaScript objects with the keys matching the column names and the values matching the corresponding row value for each column. If you do not need or do not want this behavior you can pass rowMode: 'array' to a query object. This will inform the result parser to bypass collecting rows into a JavaScript object, and instead will return each row as an array of values. | ||
@@ -1475,2 +1481,4 @@ | ||
Caching is enabled by default for most of the above operators, but this can be overridden by setting `useCache: false` in [options](#available-options), either globally or per expression. If you're querying a database or API that is likely to have a different result for the same request (i.e. data has changed), then you probably want to turn the cache off. | ||
## More examples | ||
@@ -1510,2 +1518,3 @@ | ||
- **v2.4.6**: More backwards compatibility for very old (pre-v1) syntax (undocumented) | ||
- **v2.4.1**: Small bug fix for Fragments edge case | ||
@@ -1512,0 +1521,0 @@ - **v2.4.0**: Implement [Fragments](#fragments) (#74) |
Unpublished package
Supply chain riskPackage version was not found on the registry. It may exist on a different registry and need to be configured to pull from that registry.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
238872
3531
0
1541
1