Skip to content

Latest commit

 

History

History
99 lines (62 loc) · 2.84 KB

API.md

File metadata and controls

99 lines (62 loc) · 2.84 KB

1.0.0 API Reference

toki

new Toki(options)

Creates a singleton instance of toki and starts the bootstrapping process.

  • options required At minimum, a router is required. In most cases you only need to initialize the bridge itself, which in turn will setup Toki for you. Toki accepts the following:

    • router A configured bridge
    • logger An optional logger to make use of. Should expose log4j style methods (error, warn, info, debug, trace)

events

ready

Fired when toki is ready.

//intantiate toki
const Toki = require('toki');

toki.on('ready', ()=>{
    //Ready  
});

error

Fired for errors.

//instantiate toki
const Toki = require('toki');

toki.on('error', (error)=>{
    //check error to find out what happened    
});

config.changed

toki subscribes to events triggered by toki-config if the underlaying configuration mechanism detects a configuration change. toki will bubble up the event.

//instantiate toki
const Toki = require('toki');

toki.on('config.changed', ()=>{

});

Contracts

Fulfilled by the Bridge

These contracts supply a minimum. Additional properties or methods may exist, but should not be used since requests and responses provided by the bridge are often decorated versions of underlying requests or responses.

Request

Request is a decorated version of the Node http-server request object. It will always have the following:

  • request.query - a parsed query object
  • request.params - an object of any params from passed paths
  • request.path - the current path
  • request.method - the method which called this request
  • request.headers - an object containing all headers

Response

Response is an object which allows you to send data back to the client as well as set status codes, headers and return errors.

  • response.send(payload) where payload is a string, an object or a promise. If payload is an instance of error, it'll be sent to response.error().
  • response.error(error) where payload is an instance of error will send back a default status code as well as show the error.
  • response.code(status) where status is a number will send back that statusCode. It can be called before or after send().
  • response.header(name, value) will set the named header to the new value. It can be called before or after send().