0.0.2 • Published 9 years ago

duxy v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

Duxy

npm version License Build Status

RESTful resources.

Table of Contents

  1. Installation
  2. Usage
    1. Setup
    2. Usage
    3. DSL
    4. resources
    5. resource
    6. namespace
    7. get
    8. post
    9. put
    10. patch
    11. del
    12. Adapter
  3. Development
  4. Contributing
  5. License

Installation

$ npm install duxy --save

Usage

Setup

// 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'] });
  });
});

Usage

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

DSL

resources(name, { path, only: ['findOne', 'findAll', 'create', 'update', 'delete']}, fn)
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
resource(name, { path, only: ['findOne', 'create', 'update', 'delete'] }, fn)
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
namespace(name, { path }, fn)
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
get(name, { path })
export default duxy(options, ({ get }) => {
  get('root', { path: '/' });
  get('search');
});
api.root();                   // GET /
api.search({ query: 'foo' }); // GET /search?query=foo
post(name, { path })
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
}
put(name, { path })
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
}
patch(name, { path })
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
}
del(name, { path })
export default duxy(options, ({ del }) => {
  del('posts');
});
api.del();  // DELETE /posts

Adapter

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);

Development

Setup

$ git clone <this repo>
$ cd duxy
$ npm install

Tests

Linters:

$ npm run test:lint

Tests:

$ npm run test:unit

All:

$ npm test

Contributing

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.

License

Product Hunt

 _________________
< The MIT License >
 -----------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||