1.0.0 • Published 10 months ago

@ljobse/appsettings-loader v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Appsettings json config loader

A simple tool to load config data

This allows you to automatically map environment variables typed into your application.

  • By overwriting the saved file during the build (CLI, intended for Frontend applications)
  • By setting the values at runtime (import, intended for Backend applications)

Install

# with npm
npm install @ljobse/appsettings-loader

# or with Yarn
yarn add @ljobse/appsettings-loader

CLI

The CLI allows you to overwrite properties in the json file from the current process.env context.

$ appsettings-loader ./config/appsettings.json

Lib

The library allows you to load the current process.env variables into an imported js(on) object.

var newSettings = applyEnvConfig(require("./config/appsettings.json"));
  • Nesting is supported by dot separation . and double underscores __.
  • Types are inherited
  • Case insensitive
  • Omits underscores _

Real world example

Environment on cloud provider or locally is set with the variables you need to load into your application.

#process.env

DATABASE_NAME=server_db
//# src/config/config.json
{
  "databaseName": "postgres"
}
//# src/config/index.ts

import { applyEnvConfig } from "@ljobse/appsettings-loader";
import appsettings from "./config.json";

const config = applyEnvConfig(appsettings);

export { config };
//# src/app.ts

import { config } from "./config";

console.log(config.databaseName);
// => "server_db"

Array support

JSON arrays are supported in the values as well.

In your config you can define the structure of the array.

{
  "app": { "arr": [{ "foo": "bar" }] }
}

The env variable should look like this to override:

APP__ARR=[{"foo":"baz"}]

and then the result will be:

//# src/app.ts

import { config } from "./config";

console.log(config.app.arr);
// => [{ foo: "baz" }]
1.0.0

10 months ago

0.1.7

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.2

4 years ago

0.1.0

4 years ago

0.1.1

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago