GithubHelp home page GithubHelp logo

resource-parser's Introduction

Resource-parser

Parse a JSON resource file or object using .env file and process.env. This module allows you to refer to environment variables or keys in the object itself. References are searched for in all strings of the object. A string can contain one or more references.

Install

npm i --save resource-parser

Load resource

When resource loaded, it will be parser from .env and process.env data

Default usage

const resourceParser = require('resource-parser');

/* 
    Resource can be a relative/absolute path, or an object
    If resource is a relative path or an object with "imports" key, you must provide a dirpath as second argument
*/
const config = resourceParser('./config.json');

Express usage

const resourceParser = require('resource-parser');
const express = require('express');

// ...

const app = express();

/* 
    will add "config" key to "req" object
    Config data is parsed during app loading and will be cloned for each request
*/
app.use(resourceParser.middleware('config.json'));

config.json

{
    "database": {
        "name": "{DB_NAME}"
    },
    "homeDir": "{HOME}/app",
    "publicDir": "{homeDir}/public"
}

Load resource with import resources

const resourceParser = require('resource-parser');

const config = resourceParser('config.json');

config.json

{
    "imports": [
        "parameters.json"
    ],
    "database": {
        "name": "{DB_NAME}"
    },
    "publicDir": "{parameters.homeDir}/public"
}

parameters.json

{
    "imports": [
        
    ],
    "database": {
        "host": "{DB_HOST || localhost}"
    },
    "parameters": {
        "homeDir": "{HOME}/app"
    }
}

Load resource with extra data

const resourceParser = require('resource-parser');

const config = resourceParser('config.json'), {
    key: 'value'
};

API

Express middleware

resourceParser.middleware(config: object, dirpath?: string, refs?: object): function

resourceParser.middleware(config: string, dirpath?: string, refs?: object): function

resourceParser.middleware(config: string, refs?: object): function

Parse from file or object

resourceParser.parse(config: object, dirpath?: string, refs?: object): object

resourceParser.parse(config: string, dirpath?: string, refs?: object): object

resourceParser.parse(config: string, refs?: object): object

Parse object using refs

resourceParser.parseObject(obj: object, refs: object, isArray?: boolean): object

Parse string value using refs

resourceParser.parseString(value: string, refs: object): string

Test if value contains one or more references

resourceParser.containReferences(value: string): boolean

Extract references from value

resourceParser.getReferences(value: string): Array<string>

resource-parser's People

Contributors

nbonneau avatar

Watchers

James Cloos 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.