Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

producthunt/duxy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Duxy

npm version License Build Status

RESTful resources.

Table of Contents

  1. Installation
  2. Usage 1. Setup 1. Usage 1. DSL
    1. resources
    2. resource
    3. namespace
    4. get
    5. post
    6. put
    7. patch
    8. del 1. 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 |
                ||     ||