Skip to content

vshushkov/to-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

to-api

Library to-api generates REST API client with fetch under the hood.

Usage

import create from 'to-api';

const clientApi = create({ baseUrl: 'http://api/users' });

clientApi
  .addHeader('Authorization', 'bearer ...')
  .processResponse(({ data }) => data);

const usersClient = clientApi({
  create: 'POST /',
  updateById: 'PUT /:id',
  deleteById: 'DELETE /:id',
  find: '/',
  findById: 'GET /:id'
});

(async () => {
    const newUser = await usersClient.create({ email: '[email protected]' });
    const user = await usersClient.findById({ id: 'user-id' });
})();

More examples you can find in tests.