Minibase
:rocket: Minimal framework for creating highly modular and composable Node.js applications powered by plugins
Install / Use
/learn @node-minibase/MinibaseREADME
minibase
[![npm total downloads][downloads-img]][downloads-url]
Minimalist alternative for Base. Build complex APIs with small units called plugins. Works well with most of the already existing [base][] plugins.
[![code climate][codeclimate-img]][codeclimate-url] [![standard code style][standard-img]][standard-url] [![linux build status][travis-img]][travis-url] [![windows build status][appveyor-img]][appveyor-url] [![coverage status][coveralls-img]][coveralls-url] [![dependency status][david-img]][david-url]
You might also be interested in base.
Table of Contents
(TOC generated by verb using markdown-toc)
Install
Install with npm
$ npm install minibase --save
or install using yarn
$ yarn add minibase
Usage
For more use-cases see the tests
const minibase = require('minibase')
API
MiniBase
Creates an instance of
MiniBasewith optionaloptionsobject - if given, otherwise theminibase.optionsdefaults to empty object. Never throws - emit events!™
Params
[options]{Object}: optional, written tothis.options
Example
const MiniBase = require('minibase').MiniBase
// main export is instance
const app = require('minibase')
app.once('error', (err) => {
console.log('error:', err)
})
app.use((self) => {
// this === self === app
console.log(self.use) // => 'function'
console.log(self.define) // => 'function'
self.define('foo', 'bar')
})
app.use(() => {
throw new Error('qux')
})
.delegate
Copy properties from
providertothisinstance ofMiniBase, using [delegate-properties][] lib.
Params
<provider>{Object}: object providing propertiesreturns{Object}: Returns instance for chaining
Example
const minibase = require('minibase')
minibase.use((app) => {
// `app` is `minibase`
app.delegate({
foo: 'bar',
qux: (name) => {
console.log(`hello ${name}`)
}
})
})
// or directly use `.delegate`,
// not through plugin
minibase.delegate({ cats: 'dogs' })
console.log(minibase.cats) // => 'dogs'
console.log(minibase.foo) // => 'bar'
console.log(minibase.qux('kitty!')) // => 'hello kitty!'
.define
Used for adding non-enumerable property
keywithvalueon the instance, using [define-property][] lib.
Params
key{String}: name of the property to be defined or modifiedvalue{any}: descriptor for the property being defined or modifiedreturns{Object}: Returns instance for chaining
Example
const minibase = require('minibase')
minibase.use(function (app) {
// `app` and `this` are instance of `MiniBase`,
// and so `minibase`
this.define('set', function set (key, value) {
this.cache = this.cache || {}
this.cache[key] = value
return this
})
app.define('get', function get (key) {
return this.cache[key]
})
})
minibase
.set('foo', 'bar')
.set('qux', 123)
.set('baz', { a: 'b' })
// or directly use `.define`,
// not through plugin
minibase.define('hi', 'kitty')
console.log(minibase.hi) // => 'kitty'
console.log(minibase.get('foo')) // => 'bar'
console.log(minibase.get('qux')) // => 123
console.log(minibase.get('baz')) // => { a: 'b' }
// or access the cache directly
console.log(minimist.cache.baz) // => { a: 'b' }
console.log(minimist.cache.qux) // => 123
.use
Define a synchronous plugin
fnfunction to be called immediately upon init. Never throws - emit events!™
Params
fn{Function}: plugin passed withctxwhich is the instancereturns{Object}: Returns instance for chaining
Events
emits:errorwhen pluginfnthrows an error
Example
const MiniBase = require('minibase').MiniBase
const app = MiniBase({ silent: true, foo: 'bar' })
app
.once('error', (err) => console.error(err.stack || err))
.use((app) => {
console.log(app.options) // => { silent: true, foo: 'bar' }
return 555
})
.use(function () {
console.log(this.options) // => { silent: true, foo: 'bar' }
// intentionally
foo bar
})
#delegate
Static method to delegate properties from
providertoreceiverand make them non-enumerable.
See [delegate-properties][] for more details, it is exact mirror.
Params
receiver{Object}: object receiving propertiesprovider{Object}: object providing properties
Example
const MiniBase = require('minibase').MiniBase
const obj = { foo: 'bar' }
MiniBase.delegate(obj, {
qux: 123
})
console.log(obj.foo) // => 'bar'
console.log(obj.qux) // => 123
#define
Static method to define a non-enumerable property on an object.
See [define-property][] for more details, it is exact mirror.
Params
obj{Object}: The object on which to define the propertyprop{Object}: The name of the property to be defined or modifieddescriptor{any}: The descriptor for the property being defined or modified
Example
const MiniBase = require('minibase').MiniBase
const obj = {}
MiniBase.define(obj, 'foo', 123)
MiniBase.define(obj, 'bar', () => console.log('qux'))
console.log(obj.foo) // => 123
console.log(obj.bar()) // => 'qux'
#extend
Static method for inheriting the prototype and static methods of the
MiniBaseclass. This method greatly simplifies the process of creating inheritance-based applications.
See [static-extend][] for more details.
Params
Ctor{Function}: constructor to extendmethods{Object}: optional prototype properties to mix in
Example
const MiniBase = require('minibase').MiniBase
function MyApp (options) {
MiniBase.call(this, options)
}
MiniBase.extend(MyApp)
console.log(MyApp.extend) // => function
console.log(MyApp.define) // => function
console.log(MyApp.delegate) // => function
const app = new MyApp()
console.log(app.use) // => function
console.log(app.define) // => function
console.log(app.delegate) // => function
Related
- base: base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common… more | homepage
- generate: Command line tool and developer framework for scaffolding out new GitHub projects. Generate offers the robustness and configurability of Yeoman… more | homepage
- lazy-cache: Cache requires to be lazy-loaded when needed. | homepage
- minibase-better-define: Plugin for [base][] and [minibase][] that overrides the core
.definemethod to be more better. | homepage - minibase-create-plugin: Utility for [minibase][] and [base][] that helps you create plugins | homepage
- minibase-is-registered: Plugin for [minibase][] and [base][], that adds
isRegisteredmethod to your application to detect if plugin is already registered and… more | homepage - minibase-visit: Plugin for [minibase][] and [base][], that adds
.visitmethod to your application to visit a method over the items in… more | [homepage](https://github.com/node-minibase/minibase-visit#readme "Plugin for [minibase][] and [base][], that adds.visitmet
