interfacejs

0.0.1 • Public • Published

interfacejs

Define and enforce module interfaces

Install

npm install interfacejs

Usage

We use this by defining an interface in its own file: interfaces/cdn.coffee

module.exports = {
  load: true,
  save: true,
  exists: true
}

Then some file that wants to do stuff with a cdn can require a dependency that meets this interface:

module.exports = interfacejs(require('../interfaces/cdn'), function (cdn) {
  return {
    getFile: function (name, cb) {
      cdn.load(name, cb)
    }
  }
})

in coffeescript:

module.exports = interfacejs require('../interfaces/cdn'), (cdn) ->
  getFile: (name, cb) ->
    cdn.load name, cb

Now there can be many different implementations of cdn, something like:

// this could be made to hit any cdn
module.exports = function (cdnUser, cdnPass) {
  return {
    load: function () {
      // do stuff for loading a file
    },
    save: function () {
      // do stuff for saving a file
    },
    exists: function () {
      // do stuff to check if file exists
    }
  }
}

When someone wants to use our module that uses a cdn, just pass in our preferred cdn:

cdn = require("./cdns/s3")("me", "gusta")
module = require("./module")(cdn)

module.getFile(...)

Running Tests

npm install -g mocha mocha test.js

Readme

Keywords

none

Package Sidebar

Install

npm i interfacejs

Weekly Downloads

1

Version

0.0.1

License

none

Last publish

Collaborators

  • themiddleman