Library to-api
generates REST API client with fetch
under the hood.
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.