- 
                Notifications
    You must be signed in to change notification settings 
- Fork 442
Extended API Guidelines
        Hosung Kim edited this page Apr 17, 2018 
        ·
        2 revisions
      
    Basically, our basic APIs are based on node.js. They will follow same form with node.js because of compatibility.
However, extended APIs need a guideline because they are implemented by many contributor. (for consistent usability)
- The APIs which have similar role should have same API name.
- Basically, all APIs are async API. If you want to make sync API, you need to add Syncas a suffix.
 For example,readSync(),writeSync(), and so on.
- The module object should be generated using open()API for consistent usability.
- 
open()API should have configurable as first argument and callback function as second argument.
 callback function is always optional.
For example, GPIO module generate an object like below:
var gpio = require('gpio');
var gpio10 = gpio.open({pin: 10, direction: gpio.DIRECTION.OUT},
                       function(err){console.log(err);});
gpio10.writeSync(1);- The response of the API call uses callback function.
- Only generate event when user need to know something without any API call.
- The event which has similar role should have same event name.
- 
errorcan be generated in both JS/native side.
- The errorshould be created in the place where it occurs.
- In the asynchronous function, the first parameter of callback indicates an error. If it is null, the function works without error.
For example, error can be generated like below:
In native side,
iotjs_jargs_t jargs = iotjs_jargs_create(2);
if (!result) {
  iotjs_jargs_append_error(&jargs, "GPIO Error");
}In JavaScript side,
if (!util.isNumber(value)) {
  throw new TypeError('Bad arguments');
}- Type checking of API argument is possible both in JS and Native.
- To prevent type checking multiple times, perform type checking on the first function that receives the variable.
- Throw errorwhen type checking is failed.
- If it is possible, use the functions provided by libtuv(File open, read, write, etc.)
- Callback function in API argument should be always optional.