# feature-helper

> Wrapper library that helps widgets verify enabled features on the app from various sources.

Latest version **1.0.1** (published 2018-06-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install feature-helper
pnpm add feature-helper
yarn add feature-helper
bun add feature-helper
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2018-06-27 |
| First published | 2018-06-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 99.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Can Nahum |
| Maintainers | cannahum |
| Keywords | feature, flag, library, typescript |

## Links

- npm: https://www.npmjs.com/package/feature-helper
- Repository: git@github.intuit.com:cnahum/feature-helper
- npm.io page: https://npm.io/package/feature-helper

## Dependencies (4)

- [chai](https://npm.io/package/chai.md) ^4.1.2
- [mocha](https://npm.io/package/mocha.md) ^5.0.0
- [ts-node](https://npm.io/package/ts-node.md) ^4.1.0
- [es7-object-polyfill](https://npm.io/package/es7-object-polyfill.md) ^0.0.5

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2018-06-27
- 1.0.0 — 2018-06-26

## README

# feature-helper

This library is a helper to abstract all different sources of feature flag services on your application.
Regardless of any feature flag source, format, or type, it can help you connect to one API in order to work with feature flags in runtime.

The library is written in TypeScript and is compatible for use with JavaScript as well.

##Installation

```bash
yarn add @bills/feature-helper
```

or 

```bash
npm i @bills/feature-helper --save
```


## Usage
TypeScript syntax - *You can import the interfaces as well*
```typescript
import { FeatureFlagHelper, IFeatureSource, TFetcher, TTransformer } from '@bills/feature-helper';
```
EcmaScript 2015 syntax
```ecmascript 6
import { FeatureFlagHelper } from '@bills/feature-helper';
```
EcmaScript5 syntax
```javascript
var FeatureHelper = require('@bills/feature-helper');
var FeatureFlagHelper = FeatureHelper.FeatureFlagHelper;
```
Pass your feature flag sources to the constructor method in an array. The Flag sources need to implement the **IFeatureSource** interface.
```typescript
interface IFeatureSource<T> {
    sourceName: string;
    fetcher: TFetcher<T>;
    transformer?: TTransformer<T>;
}
```
#### fetcher - TFetcher`<T>`
```typescript
type TFetcher<T> = () => Promise<T>;
```
The Fetcher Function is a function that can asynchronously retrieve all feature flag data. This may be a remote call, a database read, etc...
What it is does not matter to the library. All that matters is that is returns a promise, and the type that gets resolved in the promise is of generic type T.
Type T is important for the Transformer function, explained below.

#### transformer - TTransfomer`<T>`
```typescript
type TTransformer<T> = (featureObj: T) => TFeatures;
type TFeatures = IFeature[];
interface IFeature {
    name: string;
    source: string;
    value: any;
}
```
The Transformer Function takes the result of the fetcher call (the type was generic T, remember?), and translates it into an array of IFeatures.
IFeature is pretty simple: `name` of the feature flag, the `source` it came from, and the `value`. Most likely,
the value will be a boolean, but some feature flags do have other types such as string, number, or object.

#### So...
Construct the class like the following
```typescript
const featureSource = {
  name: 'Local Properties',
  fetcher: async () => { /*...*/ },
  transfrormer: (response: T) => { /*...*/ }
};

const FeatureFlags = new FeatureFlagHelper(featureSource);
```
### NOTE: Validation
The *IFeatureSource* fields are extremely important and they will be validated at the constructor. If they are invalid, the constructor will throw an Error.
You can check it by calling ```FeatureFlagHelper.isValidFeatureSource(mySource)``` and retrieve a boolean.


### Usage cont'd...

---
_Source: https://npm.io/package/feature-helper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
