[Node.js] Introduction to apiCheck.js

timeoutLog.js:

var apiCheckFactory = require('api-check');

var myCheck = apiCheckFactory();

module.exports = timeoutLog;

var api = [
  myCheck.number,
  myCheck.shape({
    name: myCheck.string
  }),
  myCheck.func.optional
];

function timeoutLog(time, person, callback) {
  myCheck.throw(api, arguments);

  setTimeout(function() {
    console.log('Time is up for ' + person.name + '!');
    callback && callback(person);
  }, time);
}

 

index.js:

// just giving me some space...
console.log('\n------\n');


var timeoutLog = require('./timeoutLog');

var person = {
  name: 'Jane'
};

timeoutLog(300, {name: 'Joe'}, function(thePerson) {
  console.log(
    'Person is the same? ' + (person === thePerson)
  );
});

 

Error message something like:

Error: apiCheck failed! `Argument 1` passed, `value` at `Argument 2` must be `String`

You passed:
[
  "wHY+jgNSE0HaoLsMgqkF4uGBX33bf6orJcLVGD0MdFx+tPoPnoJ9EreqOxuUixXoEOI2nGwJb8sXFAyrnXSjMpD98OVj0wg2menNJ65LzEtw3YYBsBZ4yzRtI6PD13zUy+R3S7WIhcWI30V+jZShvP5J2rm4=",
  true
]

With the types:
[
  "string",
  "boolean"
]

The API calls for:
[
  "String",
  "String"
]

 

posted @ 2015-06-09 17:26  Zhentiw  阅读(282)  评论(0编辑  收藏  举报