# @advanced-rest-client/variables-evaluator

> Variables evaluator for the Advanced REST Client

Latest version **4.0.1** (published 2020-05-12) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install @advanced-rest-client/variables-evaluator
pnpm add @advanced-rest-client/variables-evaluator
yarn add @advanced-rest-client/variables-evaluator
bun add @advanced-rest-client/variables-evaluator
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 4.0.1 |
| Published | 2020-05-12 |
| First published | 2019-03-30 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 96.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | jarrodek, lbauret, twoplustwoone |
| Keywords | web-components, variables, environments, evaluator, advanced-rest-client |

## Links

- npm: https://www.npmjs.com/package/@advanced-rest-client/variables-evaluator
- Repository: https://github.com/advanced-rest-client/variables-evaluator
- Homepage: https://github.com/advanced-rest-client/variables-evaluator#readme
- Issues: https://github.com/advanced-rest-client/variables-evaluator/issues
- npm.io page: https://npm.io/package/@advanced-rest-client/variables-evaluator

## Dependencies (4)

- [@types/jexl](https://npm.io/package/@types/jexl.md) ^2.2.0
- [lit-element](https://npm.io/package/lit-element.md) ^2.3.1
- [@open-wc/dedupe-mixin](https://npm.io/package/@open-wc/dedupe-mixin.md) ^1.2.17
- [@advanced-rest-client/events-target-mixin](https://npm.io/package/@advanced-rest-client/events-target-mixin.md) ^3.2.0

## Alternatives

- [replicas-cli](https://npm.io/package/replicas-cli.md) — 3.0K weekly downloads
- [env-contract](https://npm.io/package/env-contract.md) — 133 weekly downloads
- [@openveo/api](https://npm.io/package/@openveo/api.md) — 61 weekly downloads
- [@ryniaubenpm2/cumque-error-reiciendis](https://npm.io/package/@ryniaubenpm2/cumque-error-reiciendis.md) — 54 weekly downloads
- [ts-global-type-extra](https://npm.io/package/ts-global-type-extra.md) — 11 weekly downloads

## Recent versions

- 4.0.1 (latest) — 2020-05-12
- 4.0.0 — 2020-05-07
- 3.1.0 — 2020-04-14
- 3.0.0 — 2019-08-03
- 3.0.0-preview.3 — 2019-04-15
- 3.0.0-preview.2 — 2019-03-30
- 3.0.0-preview.1 — 2019-03-30

## README

[![Published on NPM](https://img.shields.io/npm/v/@advanced-rest-client/variables-evaluator.svg)](https://www.npmjs.com/package/@advanced-rest-client/variables-evaluator)

[![Build Status](https://travis-ci.com/advanced-rest-client/variables-evaluator.svg)](https://travis-ci.com/advanced-rest-client/variables-evaluator)

[![Published on webcomponents.org](https://img.shields.io/badge/webcomponents.org-published-blue.svg)](https://www.webcomponents.org/element/advanced-rest-client/variables-evaluator)

## variables-evaluator

Variables evaluator for Advanced REST Client and API components.

The element handles `evaluate-variable` custom event to evaluate a variable against current
environment variables.

The component queries for current environment dispatching `environment-current` custom event.
The event should be handled and values should be set within the same event loop.
The handler must set `variables` property which is an array of variables with the following properties:

```javascript
{
  enabled: true,
  variable: 'variable-name',
  value: 'replacement value'
}
```

The `evaluate-variable` event will have a `result` property set on the `detail` object of the event.
The property has a promise object resolved to evaluated property.

```javascript
const e = new CustomEvent('evaluate-variable', {
  cancelable: true,
  bubbles: true,
  detail: {
    value: 'string with a ${variable}'
  }
});
document.body.dispatchEvent(e);
e.detail.result.then((value) => {
  console.log(value);
});
```

When `context` property on the `detail` object is specified then the component skips querying for `environment-current`
and use this value as the context. It is a map for variables with values.

Additionally the event can carry `override` property on the detail object. When set
the final context values (whether received from `environment-current` event or via `context` property)
are overwritten values in this map.

```javascript
const e = new CustomEvent('evaluate-variable', {
  cancelable: true,
  bubbles: true,
  detail: {
    value: '${a} ${b} ${c}',
    context: {
      a: 'context value a',
      b: 'context value b'
    },
    override: {
      b: 'override value b',
      c: 'override value c'
    }
  }
});
document.body.dispatchEvent(e);
e.detail.result.then((value) => {
  console.log(value);
  // "context value a override value b override value c"
});
```

## Jexl dependency

Previous versions of this component included Jexl library. This version do not have Jexl as a dependency but it is required to run the component.

You must install [Jexl](https://github.com/TomFrost/Jexl) on your project, and build it for browser. See `dev-lib/` folder for an example of such a build.

Finally you have to either pass the pointer to Jexl library to `jexl` property or point to a relative in the `window` object.

Setting Jexl reference:

```javascript
const eval = document.querySelector('variables-evaluator');
eval.jexl = myJexlVariable;
```

Setting path to Jexl:

```html
<variables-evaluator jexlpath="ArcVariables.JexlDev"></variables-evaluator>
```
This expects the Jexl library to be under `window.ArcVariables.JexlDev` variable.

## Usage

### Installation
```
npm install --save @advanced-rest-client/variables-evaluator
```

### In an html file

```html
<html>
  <head>
    <script type="module">
      import '@advanced-rest-client/variables-evaluator/variables-evaluator.js';
    </script>
    <script src="jexl.min.js"></script>
  </head>
  <body>
    <variables-evaluator jexlpath="jexl"></variables-evaluator>
  </body>
</html>
```

### In a LitElement template

```javascript
import { LitElement, html } from 'lit-element';
import '@advanced-rest-client/variables-evaluator/variables-evaluator.js';

class SampleElement extends LitElement {
  render() {
    return html`
    <variables-evaluator jexl="${this.jexlRef}"></variables-evaluator>
    `;
  }

  async evaluate() {
    const node = this.shadowRoot.querySelector('variables-evaluator');
    // clears previously set context and cache
    node.reset();
    const result = await element.evaluateVariable('some ${str}', {
      str: 'value'
    });
    console.log(result); // some value
  }
}
customElements.define('sample-element', SampleElement);
```

## Development

```sh
git clone https://github.com/advanced-rest-client/variables-evaluator
cd variables-evaluator
npm install
```

### Running the demo locally

```sh
npm start
```

### Running the tests

```sh
npm test
```

---
_Source: https://npm.io/package/@advanced-rest-client/variables-evaluator · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
