# @frenchex/config-lib

> Flexible Configuration Library based on .json or .js files (raw JSON or promises)

Latest version **0.3.5** (published 2020-12-11) · ISC license · 0 weekly downloads

## Install

```sh
npm install @frenchex/config-lib
pnpm add @frenchex/config-lib
yarn add @frenchex/config-lib
bun add @frenchex/config-lib
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.5 |
| Published | 2020-12-11 |
| First published | 2020-01-18 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 12.14 |
| Dependencies | 5 |
| Unpacked size | 106 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Stéphane Erard |
| Maintainers | serard, s.erard |
| Keywords | configuration, lib, ts |

## Links

- npm: https://www.npmjs.com/package/@frenchex/config-lib
- Repository: https://github.com/French-Exception/node-config-lib
- Homepage: https://github.com/French-Exception/node-config-lib#readme
- Issues: https://github.com/French-Exception/node-config-lib/issues
- npm.io page: https://npm.io/package/@frenchex/config-lib

## Dependencies (5)

- [mkdirp](https://npm.io/package/mkdirp.md) ^1.0.3
- [fs-extra](https://npm.io/package/fs-extra.md) ^8.1.0
- [maybe.ts](https://npm.io/package/maybe.ts.md) ^0.1.0
- [deepmerge](https://npm.io/package/deepmerge.md) ^4.2.2
- [object-walker](https://npm.io/package/object-walker.md) 0.0.4

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 0.3.5 (latest) — 2020-12-11
- 0.3.4 — 2020-11-30
- 0.3.3 — 2020-11-30
- 0.3.2 — 2020-11-30
- 0.3.1 — 2020-11-03
- 0.1.17 — 2020-03-26
- 0.1.15 — 2020-01-29
- 0.1.14 — 2020-01-29
- 0.1.13 — 2020-01-29
- 0.1.12 — 2020-01-21
- 0.1.11 — 2020-01-21
- 0.1.10 — 2020-01-20
- 0.1.9 — 2020-01-20
- 0.1.7 — 2020-01-20
- 0.1.6 — 2020-01-20
- … 4 more at https://npm.io/package/@frenchex/config-lib/versions

## README

[![npm version](https://badge.fury.io/js/%40frenchex%2Fconfig-lib.svg)](https://badge.fury.io/js/%40frenchex%2Fconfig-lib)

# About

Configuration Library is inspired by ```service-container2``` and ```Symfony Dependency Injection Container```

Purpose here is not to provide a full feature mapping from both libs.

# Installation

```bash
npm i --save @frenchex/config-lib
```

# Usage

## Basic JSON file structure 

```typescript
export interface ConfigurationDeclarationInterface {
    imports?: Array<string>
    ns?: string
    $: object
}
```

Example :
```
{
  "imports": ["config_%env%.json"],
  "ns": "example",
  "$": {
    "foo": {
      "bar": "foobar",
      "foobar": "%foo.bar%%foo.bar%"
    }
  }
}

```

## Loading from File

```typescript
const loader = new ConfigurationLoader();
const request = (() => {
    const requestBuilder = new ConfigurationLoaderFromFileRequestBuilder();

    requestBuilder
        .withFile(path.normalize(path.join(__dirname, '..', '..', 'test-res', 'js.js')))
        .withRoot(path.normalize(path.join(__dirname, '..', '..', 'test-res')))
        .withEnv({env: 'dev'})

    return requestBuilder.build();
})();

const config: ConfigurationInterface = await loader.fromFile(request);
const foobar : string = await config.get<string>('foo.bar'); // return Maybe.just('foobar');
```


# Usages examples (from tests)

## Usage 1
```typescript
const c = new Configuration({
    $: {
        foo: {
            bar: 'foobar',
            foobar: '%foo.bar%%foo.bar%'
        }
    }
});

const foobar = await c.get<string>('foo.bar');
const foobar2 = await c.get<string>('foo.foobar');
const foo = await c.get<object>('foo');

expect(foobar).to.be.equal('foobar');
expect(foobar2).to.be.equal('foobarfoobar');
expect(foo).to.be.deep.equal({bar: 'foobar', foobar: 'foobarfoobar'});
```

## Usage 2

```typescript
 const loader = new ConfigurationLoader();

const request = await (async () => {
    const requestBuilder = new ConfigurationLoaderFromFileRequestBuilder();

    requestBuilder
        .withFile(path.normalize(path.join(__dirname, '..', '..', 'test-res', 'js.js')))
        .withRoot(path.normalize(path.join(__dirname, '..', '..', 'test-res')))
        .withEnv({env: 'dev'})

    const request = await requestBuilder.build();

    return request;
})();

const config: ConfigurationInterface = await loader.fromFile(request);

const foobar = await config.get<string>('foo.bar');
const foobar2 = await config.get<string>('foo.foobar');
const foo = await config.get<object>('foo');
const promise = await config.get<object>('promise');

expect(foobar).to.be.equal('foobar');
expect(foobar2).to.be.equal('foobarfoobar');
expect(foo).to.be.deep.equal({bar: 'foobar', foobar: 'foobarfoobar'});
expect(promise).to.be.deep.equal('resolved');
```

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