Package Exports
- base
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (base) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
base
Table of contents
About
Why use Base?
Base is a foundation for creating modular, unit testable and highly pluggable server-side node.js APIs.
- Go from zero to working application within minutes
- Use community plugins to add feature-functionality to your application
- Create your own custom plugins to add features
- Like building blocks, plugins are stackable. Allowing you to build sophisticated applications from simple plugins. Moreover, those applications can also be used as plugins themselves.
Most importantly, once you learn Base, you will be familiar with the core API of all applications built on Base. This means you will not only benefit as a developer, but as a user as well.
Guiding principles
The core team follows these principles to help guide API decisions:
Compact API surface: The smaller the API surface, the easier the library will be to learn and use.
Easy to extend: Implementors can use any npm package, and write plugins in pure JavaScript. If you're building complex apps, Base dramatically simplifies inheritance.
Easy to test: No special setup should be required to unit test
Baseor base plugins100% Node.js core style
- No API sugar (left for higher level projects)
- Written in readable vanilla JavaScript
Minimal API surface
The API was designed to provide only the minimum necessary functionality for creating a useful application, with or without plugins.
Base core
Base itself ships with only a handful of useful methods, such as:
.set: for setting values on the instance.get: for getting values from the instance.has: to check if a property exists on the instance.define: for setting non-enumerable values on the instance.use: for adding plugins
Be generic
When deciding on method to add or remove, we try to answer these questions:
- Will all or most Base applications need this method?
- Will this method encourage practices or enforce conventions that are beneficial to implementors?
- Can or should this be done in a plugin instead?
Composability
Plugin system
It couldn't be easier to extend Base with any features or custom functionality you can think of.
Base plugins are just functions that take an instance of Base:
var base = new Base();
function plugin(base) {
// do plugin stuff, in pure JavaScript
}
// use the plugin
base.use(plugin);Add "smart plugin" functionality with the base-plugins plugin.
Inheritance
Easily inherit Base using .extend:
var Base = require('base');
function MyApp() {
Base.call(this);
}
Base.extend(MyApp);
var app = new MyApp();
app.set('a', 'b');
app.get('a');
//=> 'b';Inherit or instantiate with a namespace
By default, the .get, .set and .has methods set and get values from the root of the base instance. You can customize this using the .namespace method exposed on the exported function. For example:
var Base = require('base');
// get and set values on the `base.cache` object
var base = Base.namespace('cache');
var app = base();
app.set('foo', 'bar');
console.log(app.cache.foo);
//=> 'bar'Install
NPM
Install
Install with npm:
$ npm install --save baseyarn
Install with yarn:
$ yarn add base && yarn upgradeUsage
var Base = require('base');
var app = new Base();
// set a value
app.set('foo', 'bar');
console.log(app.foo);
//=> 'bar'
// register a plugin
app.use(function() {
// do stuff (see API docs for ".use")
});API
Base
Create an instance of Base with the given config and options.
Params
cache{Object}: If supplied, this object is passed to cache-base to merge onto the the instance.options{Object}: If supplied, this object is used to initialize thebase.optionsobject.
Example
// initialize with `config` and `options`
const app = new Base({isApp: true}, {abc: true});
app.set('foo', 'bar');
// values defined with the given `config` object will be on the root of the instance
console.log(app.baz); //=> undefined
console.log(app.foo); //=> 'bar'
// or use `.get`
console.log(app.get('isApp')); //=> true
console.log(app.get('foo')); //=> 'bar'
// values defined with the given `options` object will be on `app.options
console.log(app.options.abc); //=> true.is
Set the given name on app._name and app.is* properties. Used for doing lookups in plugins.
Params
name{String}returns{Boolean}
Example
app.is('collection');
console.log(app.type);
//=> 'collection'
console.log(app.isCollection);
//=> true.isRegistered
Returns true if a plugin has already been registered on an instance.
Plugin implementors are encouraged to use this first thing in a plugin to prevent the plugin from being called more than once on the same instance.
Params
name{String}: The plugin name.register{Boolean}: If the plugin if not already registered, to record it as being registered passtrueas the second argument.returns{Boolean}: Returns true if a plugin is already registered.
Events
emits:pluginEmits the name of the plugin being registered. Useful for unit tests, to ensure plugins are only registered once.
Example
const base = new Base();
base.use(function(app) {
if (app.isRegistered('myPlugin')) return;
// do stuff to `app`
});
// to also record the plugin as being registered
base.use(function(app) {
if (app.isRegistered('myPlugin', true)) return;
// do stuff to `app`
});.use
Define a plugin function to be called immediately upon init.
Params
fn{Function}: plugin function to callreturns{Object}: Returns the item instance for chaining.
Example
const app = new Base()
.use(foo)
.use(bar)
.use(baz).define
The .define method is used for adding non-enumerable property on the instance. Dot-notation is not supported with define.
Params
key{String}: The name of the property to define.value{any}returns{Object}: Returns the instance for chaining.
Example
// arbitrary `render` function using lodash `template`
app.define('render', function(str, locals) {
return _.template(str)(locals);
});.base
Getter/setter used when creating nested instances of Base, for storing a reference to the first ancestor instance. This works by setting an instance of Base on the parent property of a "child" instance. The base property defaults to the current instance if no parent property is defined.
Example
// create an instance of `Base`, this is our first ("base") instance
const first = new Base();
first.foo = 'bar'; // arbitrary property, to make it easier to see what's happening later
// create another instance
const second = new Base();
// create a reference to the first instance (`first`)
second.parent = first;
// create another instance
const third = new Base();
// create a reference to the previous instance (`second`)
// repeat this pattern every time a "child" instance is created
third.parent = second;
// we can always access the first instance using the `base` property
console.log(first.base.foo);
//=> 'bar'
console.log(second.base.foo);
//=> 'bar'
console.log(third.base.foo);
//=> 'bar'Base.use
Static method for adding global plugin functions that will be added to an instance when created.
Params
fn{Function}: Plugin function to use on each instance.returns{Object}: Returns theBaseconstructor for chaining
Example
Base.use(function(app) {
app.foo = 'bar';
});
const app = new Base();
console.log(app.foo);
//=> 'bar'Toolkit suite
Base is used as the foundation for all of the applications in the toolkit suite (except for enquirer):
Building blocks
- base: (you are here!) framework for rapidly creating high quality node.js applications, using plugins like building blocks.
- templates: API for managing template collections and rendering templates with any node.js template engine. Can be used as the basis for creating a static site generator, blog framework, documentaton system, and so on.
- enquirer: composable, plugin-based prompt system (Base is used in prompt-base, the core prompt module that powers all prompt plugins)
Lifecycle
Developer frameworks and command line tools that address common phases of the software development lifecycle. Each of these tools can be used entirely standalone, but they work even better together.
- generate: create projects
- assemble: build projects
- verb: document projects
- update: maintain projects
About
Related projects
- base-cwd: Base plugin that adds a getter/setter for the current working directory. | homepage
- base-data: adds a
datamethod to base-methods. | homepage - base-fs: base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file… more | homepage
- base-generators: Adds project-generator support to your
baseapplication. | homepage - base-option: Adds a few options methods to base, like
option,enableanddisable. See the readme… more | homepage - base-pipeline: base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines. | homepage
- base-pkg: Plugin for adding a
pkgmethod that exposes pkg-store to your base application. | homepage - base-plugins: Adds 'smart plugin' support to your base application. | homepage
- base-questions: Plugin for base-methods that adds methods for prompting the user and storing the answers on… more | homepage
- base-store: Plugin for getting and persisting config values with your base-methods application. Adds a 'store' object… more | homepage
- base-task: base plugin that provides a very thin wrapper around https://github.com/doowb/composer for adding task methods to… more | homepage
Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm testContributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
If Base doesn't do what you need, please let us know.
Release History
key
Changelog entries are classified using the following labels from keep-a-changelog:
added: for new featureschanged: for changes in existing functionalitydeprecated: for once-stable features removed in upcoming releasesremoved: for deprecated features removed in this releasefixed: for any bug fixes
Custom labels used in this changelog:
dependencies: bumps dependencieshousekeeping: code re-organization, minor edits, or other changes that don't fit in one of the other categories.
Heads up!
Please let us know if any of the following heading links are broken. Thanks!
0.12.0
Fixed
- ensure
__callbacksandsuper_are non-enumberable
Added
- Now sets
app.typewhenapp.is('foo')is called. This allows Base instances to be used more like AST nodes, which is especially helpful with smart plugins
0.11.0
Major breaking changes!
- Static
.useand.runmethods are now non-enumerable
0.9.0
Major breaking changes!
.isno longer takes a function, a string must be passed- all remaining
.debugcode has been removed app._namespacewas removed (related todebug).plugin,.use, and.defineno longer emit events.assertPluginwas removed.lazywas removed
(Changelog generated by helper-changelog)
Authors
Jon Schlinkert
Brian Woodward
License
Copyright © 2017, Jon Schlinkert. MIT
This file was generated by verb-generate-readme, v0.6.0, on December 21, 2017.