RESTful resources.
- Installation
- Usage 1. Setup 1. Usage 1. DSL
- Development
- Contributing
- License
$ npm install duxy --save
// api.js
import duxy from 'duxy';
import duxySuperagent from 'duxy-superagent';
import request from 'superagent';
const http = duxySuperagent(request)(() => {});
export default duxy({ http }, ({ get, resources, resource }) => {
get('about');
resources('posts', { only: ['findAll', 'findOne'] }, () => {
resource('followers', { only: ['create', 'delete'] });
});
});
import api from 'ph/api';
const { body } = api.about(); // GET /about
const { body } = api.posts.findOne({ id: 1 }); // GET /posts/1
const { body } = api.posts.findAll({ limit: 10 }); // GET /posts?limit=10
export default duxy(options, ({ get, resources }) => {
resources('users', () => {
get('moreInfo', { path: 'info' });
});
});
api.users.findAll({ id: 1 }); // GET /users
api.users.findOne({ id: 1 }); // GET /users/1
api.users.create({ id: 1, name }); // POST /users/1
api.users.update({ id: 1, name }); // PUT /users/1
api.users.delete({ id: 1 }); // DELETE /users/1
api.users.moreInfo({ userId: 1 }); // GET /users/1/info
export default duxy(options, ({ resources, resource }) => {
resources('users', { only: ['findOne'] }, () => {
resource('followers', { path: 'following', only: ['findOne', 'create', 'delete'] });
});
});
api.users.followers.findOne({ userId: 1 }); // GET /users/1/following
api.users.followers.create({ userId: 1 }); // POST /users/1/following
api.users.followers.delete({ userId: 1 }); // DELETE /users/1/following
export default duxy(options, ({ get, patch, namespace }) => {
namespace('my', () => {
get('profile');
namespace('settings', () => {
patch('edit');
});
});
});
api.my.profile(); // GET /my/profile
api.my.settings.edit(); // PATCH /my/settings/edit
export default duxy(options, ({ get }) => {
get('root', { path: '/' });
get('search');
});
api.root(); // GET /
api.search({ query: 'foo' }); // GET /search?query=foo
export default duxy(options, ({ post }) => {
post('create');
post('createPost', { path: '/posts' });
});
try {
const { body } = await api.createPost({ title, name }); // POST /posts
// handle response
} catch(e) {
const { response: { body: { errors } } } = e;
// handle errors
}
export default duxy(options, ({ put }) => {
put('update');
put('updatePost', { path: '/posts' });
});
try {
const { body } = await api.updatePost({ id, title, name }); // PUT /posts
// handle response
} catch(e) {
const { response: { body: { errors } } } = e;
// handle errors
}
export default duxy(options, ({ patch }) => {
patch('update');
patch('updatePost', { path: '/posts' });
});
try {
const { body } = await api.updatePost({ id, title, name }); // PATCH /posts
// handle response
} catch(e) {
const { response: { body: { errors } } } = e;
// handle errors
}
export default duxy(options, ({ del }) => {
del('posts');
});
api.del(); // DELETE /posts
const http = ({ method, url, body, query }) => {
return new Promise((resolve, reject) => {
// should be:
// resolve({ body: responseBody });
// reject({ request: { body: responseBody } });
});
};
export default duxy({ http }, definition);
Superagent adapter
$ npm install duxy-superagent --save
// api.js
import duxy from 'duxy';
import duxySuperagent from 'duxy-superagent';
import request from 'superagent';
const http = duxySuperagent(request)(() => {});
export default duxy({ http }, definition);
$ git clone <this repo>
$ cd duxy
$ npm install
Linters:
$ npm run test:lint
Tests:
$ npm run test:unit
All:
$ npm test
We want to make this assertion library as robust and complete as possible. If you think that there are missing features/assertions, please open a GitHub issue or even better - a PR.
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
_________________
< The MIT License >
-----------------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||