seneca-ember-rest-adapter
A Seneca plugin to wrap seneca-jsonrest-api for Embers rest adapter
This seneca plugin provides a wrapper for the jsonrest-api plugin to talk fluently with Ember.js' DS.RESTAdapter. It provides two features:
- A configurable plural form of the endpoint, e.g. /api/foos
- Wrapping and unwrapping of the payloads object root
Installation
npm install seneca-ember-rest-adapter
Example
const seneca = require('seneca')()
.use('entity')
.use('web')
.use('jsonrest-api', {
pin: {name: 'foo'},
prefix: '/api'
})
.use('ember-rest-adapter', {
alias: {'foo': 'foos'}
});
const express = require('express')()
.use(require('body-parser').json())
.use(seneca.export('web'))
.listen(3000);
$ curl -X POST http://localhost:3000/api/foos -H 'Content-Type: application/json' -d '{"foo":{"name":"foo1"}}'
{"foos":{"name":"foo1","id":"880i2f"}}
$ curl -X POST http://localhost:3000/api/foos -H 'Content-Type: application/json' -d '{"foo":{"name":"foo2"}}'
{"foos":{"name":"foo2","id":"dip1d0"}}
$ curl -X GET http://localhost:3000/api/foos
{"foos":[{"name":"foo1","id":"880i2f"},{"name":"foo2","id":"dip1d0"}]}
$ curl -X GET http://localhost:3000/api/foos/dip1d0
{"foos":{"name":"foo2","id":"dip1d0"}}
You can find this example in the example directory and run it:
node example/app.js
Notes
As you can see in the example above, the plugin returns the plural form
(the alias) for GET
requests. This is according to the
object root documentation
of DS.RESTAdapter totally acceptable:
Note that the object root can be pluralized for both a single-object response and an array response: the REST adapter is not strict on this.
Test
To run tests, simply use npm:
npm test