periodicjs.core.responder
Customizable CMS platform
Last updated 3 years ago by yawetse .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install periodicjs.core.responder 
SYNC missed versions from official npm registry.

periodicjs.core.responder

Build Status NPM version Coverage Status Join the chat at https://gitter.im/typesettin/periodicjs.core.data

Description

Core responder is a component of periodicjs.core.controller that provides adapters for different formatting responses across different content types. Each adapter may have options specific to the content type, but after being constructed the same methods are exposed. Depending on content type adapters do everything from wrapping JSON data in a standard way to rendering HTML from templates.

Full Documentation

Usage (basic)

const AdapterInterface = require('periodicjs.core.responder');
var adapter = AdapterInterface.create({ adapter: 'json' });
//Basic usage (JSON)
//With no callback param returns a Promise
adapter.render({ foo: 'bar' })
    .then(result => {
        /*
          {
              "result": "success",
              "status": 200,
              "data": {
                  "foo": "bar"
              }
          }
        */
    });
//With a callback param
adapter.render({ foo: 'bar' }, (err, result) => {
    /*
      {
          "result": "success",
          "status": 200,
          "data": {
              "foo": "bar"
          }
      }
    */
});
//Second param can also be configurable options sync: true will handle operation synchronously
let result = adapter.render({ foo: 'bar' }, { sync: true });
/*
  {
      "result": "success",
      "status": 200,
      "data": {
          "foo": "bar"
      }
  }
*/
//Formatting can also be customized
let result = adapter.render({ foo: 'bar' }, {
    formatRender: function (data, options) {
        return {
            custom: 'field',
            data
        };
    }
});
/*
  {
    "custom": "field",
    "data": {
        "foo": "bar"
    }
  }
*/
//Basic usage (XML)
adapter = AdapterInterface.create({ adapter: 'xml', xml_root: 'example' });
adapter.render({ foo: 'bar' })
    .then(result => {
        /*
         <?xml version="1.0">
         <example>
            <foo>bar</foo>
         </example>
        */
    });
//configuration for XML rendering can also be passed
adapter.render({ foo: 'bar' }, {
    declaration: { encoding: 'UTF-8' }
}, (err, result) => {
    /*
     <?xml version="1.0" encoding="UTF=8">
     <example>
        <foo>bar</foo>
     </example>
    */
});
//Basic usage (HTML)
//Since template rendering is inherently async the sync option is ignored
adapter = AdapterInterface.create({ adapter: 'html', viewname: 'user.ejs' });
//By default HTML adapter renders EJS templates
adapter.render({ user: 'Jim' })
    .then(result => {
        //Hello <%- user %>! => Hello Jim!
    });
//Custom template engines can also be used as long as they expose a .render method
adapter = AdapterInterface.create({ adapter: 'html', viewname: 'user.pug', engine: require('pug') });
adapter.render({ user: 'Jim' })
    .then(result => {
        //Hello ${user}! => Hello Jim!
    });

Usage (HTML Adapter Advanced)

const AdapterInterface = require('periodicjs.core.responder');
const mustache = require('mustache');
const path = require('path');
//Because the adapter uses the .render method of the template engine you can overwite this .render method to further customize behavior
var render = mustache.render.bind(mustache);
mustache.render = function (template, data, options) {
    mustache.parse(template);
    return render(template, data, options);
};
const adapter = AdapterInteraface.create({ adapter: 'html', engine: mustache, viewname: 'user.mustache' });
//You can customize which directories .render will search for the template by passing .dirname. This also optimizes .render because custom directories are searched ahead of the theme/extension directories
adapter.render({ user: 'Jim' }, { dirname: '/some/path/to/dir' })
    .then(result => {
        //Hello {{ user }}! => Hello Jim!
    });

Usage (with custom adapter)

const AdapterInterface = require('periodicjs.core.responder');
const template = function (data) {
    return `Hello ${ data }!`;
};
const CustomAdapter = class {
    constructor (options) {
        ...
    },
    render (data, options) {
        return template(data);
    },
    error (err, options) {
        return `There was an error: ${ err.message } :(`;
    }
};
var adapter = AdapterInterface.create({ adapter: CustomAdapter });
adapter.render('Jim'); //Hello Jim!

Development

Make sure you have grunt installed

$ npm install -g grunt-cli jsdoc-to-markdown

For generating documentation

$ grunt doc
$ jsdoc2md adapters/**/*.js index.js > doc/api.md

Notes

Testing

$ npm i
$ grunt test

Contributing

License

MIT

Current Tags

  • 0.3.7                                ...           latest (a year ago)

19 Versions

  • 0.3.7                                ...           a year ago
  • 0.3.6                                ...           3 years ago
  • 0.3.5                                ...           3 years ago
  • 0.3.4                                ...           3 years ago
  • 0.3.3                                ...           3 years ago
  • 0.3.2                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.0                                ...           4 years ago
  • 0.1.1                                ...           4 years ago
  • 0.1.0                                ...           4 years ago
  • 0.0.8                                ...           4 years ago
  • 0.0.7                                ...           4 years ago
  • 0.0.6                                ...           4 years ago
  • 0.0.5                                ...           4 years ago
  • 0.0.4                                ...           4 years ago
  • 0.0.3                                ...           4 years ago
  • 0.0.2                                ...           4 years ago
  • 0.0.1                                ...           4 years ago
Downloads
Today 0
This Week 0
This Month 2
Last Day 0
Last Week 21
Last Month 23
Dependencies (2)
Dev Dependencies (22)

Copyright 2014 - 2016 © taobao.org |