Coder Social home page Coder Social logo

jint-configuration's Introduction

Jint.Extensions.Configuration

Add JavaScript modules as your configuration source! Powered by Jint.

Usage

Install this package using your preferred way.

Simple usage

Prepare a JavaScript module that exports an object:

// appsettings.js

export default {
  host: 'localhost',
  port: 5432
}

Then, add a JavaScript module source to Microsoft.Extensions.Configuration.IConfigurationBuilder:

var builder = new ConfigurationBuilder()
    .AddJavaScriptModule("./path/to/appsettings.js");

var configuration = builder.Build();

// Use configuartion
var host = configuration["host"];

The entire JavaScript code will be executed (with Jint), and the exported object will be added to the configuration.

With ASP.NET Core

In ASP.NET Core, you can simply add a JavaScript module source to builder.Configuration:

builder.Configuration.AddJavaScriptModule("appsettings.js");
builder.Configuration.AddJavaScriptModule($"appsettings.{builder.Environment.EnvironmentName}.js");

As an example,

// appsettings.js

const LogLevel = {
  Information: 'Information',
  Warning: 'Warning',
  // ...
}

export default {
  logging: {
    logLevel: {
      default: LogLevel.Information,
      'Microsoft.AspNetCore': LogLevel.Warning,
    },
  },
  allowedHosts: '*',
}

Full ASP.NET Core example can be found here.

Misc

Export separately is also supported, but cannot mix with export default (if a module exports both, export default will be used and others will be ignored):

export const logging = {
  logLevel: {
    default: LogLevel.Information,
    'Microsoft.AspNetCore': LogLevel.Warning,
  },
}

export const allowedHosts = '*'

License

MIT

jint-configuration's People

Contributors

bsdayo avatar

Stargazers

 avatar Marko Lahma avatar Darran Hayes avatar 陈鑫伟 avatar Sipke Schoorstra avatar John Montgomery avatar  avatar rcollina avatar Sébastien Ros avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.