interfacejs

Simple interfaces for complicated code

npm install interfacejs
1 downloads in the last month

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

npm loves you