mitto is a small library that helps package creators search the file directory of the application consuming their package for its relevant config.
Package creators can even drop a .mitto
file into their root folder and specify required
and optional
parameters with type-checking (including descriptive error messages for package consumers if a required parameter is not present or is of the wrong type).
- Quick and painless to start writing your package with a user-provided config in mind
required
oroptional
parameter enforcement with type-checking for your configs- Descriptive error messages for the user when something is misconfigured
.mitto
gives your package consumers a common area to see what configurations are available and/or expected
npm install mitto --save
var mitto = require('mitto');
var myConfig = mitto.loadConfig('config_i_need.json');
if (myConfig) {
//DO STUFF
} else {
//YELL AT USER AND CONFIGURE THINGS MYSELF
}
Create a .mitto
config file in your root folder:
{
"name" : "name_of_the_config_file_you_expect_the_user_to_provide.json",
"required" : {
"areWeHavingFun" : {
"type" : "boolean", //valid types: "undefined", "object", "boolean", "number", "string", "symbol", "function"
"description" : "boolean that represents if we're having fun" // **Optional**, you don't have to include "description"
},
"maximumBeerCount" : {
"type" : "number"
}
},
"optional" : {
"partyResponsibly" : {
"type" : "boolean",
"description" : "boolean indicating if we should party responsibly",
"default" : false // **Optional**, the default value if the user doesn't provide a value or doesn't have a configuration at all
}
}
}
The code we write will be exactly the same, except anything we get from the user will be compared with your .mitto
and type-checked:
var mitto = require('mitto');
var myConfig = mitto.loadConfig('config_i_need.json');
if (myConfig) {
//DO STUFF
} else {
//CONFIGURE THINGS MYSELF
}
.mitto has required attributes? |
User has a configuration file? | Error handling behavior |
---|---|---|
Yes | Yes | Only throws errors if user is missing a required parameter or has a parameter of an invalid type. |
Yes | No | Throws an error that the configuration is missing. |
No | Yes | Does not throw errors. loadConfig() will return NULL or an object with defaults. |
No | No | Does not throw errors. loadConfig() will return NULL or an object with defaults. |
npm test
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
- 0.3.0 Unified API under loadConfig. mitto now "requires" the object for you.
.mitto
is now more explicit and detailed. - 0.2.0 Switch from object-templating to .mitto templating, separate opinionated call from non-opiniated.
- 0.1.0 Initial release
MIT License