@bsaqqa/requestsgenerator
Version:
Generate requests without need writing full request every time
53 lines (43 loc) • 1.49 kB
Markdown
# @bsaqqa/requestsgenerator
[](https://github.com/Baraa-AlSaqqa/requestsgenerator/blob/master/LICENSE)
[](https://www.npmjs.com/package/@bsaqqa/requestsgenerator)
Generate requests without need writing full request every time
## Install
```
npm i @bsaqqa/requestsgenerator
```
or
```
yarn add @bsaqqa/requestsgenerator
```
## Usage
```js
import API from '@bsaqqa/requestsgenerator';
API.setUrl('http://example.com/api/');
API.http.getProjects({page:1})
.then(response=>response.json())
.then(resp=>{
console.log(resp);
}).catch(err=>{
throw new Error('Response is inncorrect.');
});
```
more options
```js
// GET /
API.http.get()
// GET /users
API.http.getUsers()
// GET /users/1234/likes
API.http.getUsers$Likes('1234')
// GET /users/1234/likes?page=2
API.http.getUsers$Likes('1234', { page: 2 })
// POST /items with body
API.http.postItems({ name: 'Item name' })
// other usage
API.setUrl('http://example.com/api/', '?attr=abc' )
// GET http://example.com/api/posts/123?attr=abc
API.http.getPosts$('1234')
// POST http://example.com/api/posts/123?attr=abc
API.http.postPosts$('1234', {name: 'user name'})
```