https://github.com/zorro-del-caribe/http-client-generator
Generate http client for nodejs based on schemas
https://github.com/zorro-del-caribe/http-client-generator
Last synced: 7 months ago
JSON representation
Generate http client for nodejs based on schemas
- Host: GitHub
- URL: https://github.com/zorro-del-caribe/http-client-generator
- Owner: zorro-del-caribe
- License: mit
- Created: 2016-09-21T13:46:57.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-10-12T20:02:34.000Z (almost 9 years ago)
- Last Synced: 2025-01-26T17:35:50.506Z (8 months ago)
- Language: JavaScript
- Size: 11.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zdc-client
[](https://circleci.com/gh/zorro-del-caribe/http-client-generator)
Generate namespaced http client based on schema definition using Bearer token authentication protocol or Basic authentication protocol.
## install
``npm install --save zdc-client``
## usage
```Javascript
const factory = require('zdc-client').bearer;
const schema={
list:{
method:'get', // the HTTP verb
path:'/', //the path
query:['page','size'] // a list of authorized query parameters
},
self:{
method:'get',
path:'/:userId'//a mandatory query parameters,},
create:{
method:'post',
path:'/',
body:['email','username'] // list of allowed parameters
}
};const users = factory({
schema,
namespace:'users',
endpoint:{
protocol:'https',
host:'api.zdc.com'
});users({token:'foobar'})
.list({page:4}) // GET https://api.zdc.com/users/?page=4 Authorization: Bearer foobar
.then(result => {})
.catch(e=>{});users({token:'whatever')
.self({userId:666) // GET https://api.zdc.com/users/666 Authorization: Bearer whatever
.then(user=>{})users({token:'foobar')
.create({email:'foo@bar.com',username:'oufGuedin'}) // POST https://api.zdc.com/users/ Authorization: Bearer foobar, body : {email:'foo@bar.com', username:'oufGuedin'}
.then(user=>{})//OR
const basic = require('zdc-client').basic;
const users = basic({/* .. options */);
users({username:'foo',password:'bar'})
.list()
.then(users=>{});
```Note: factories created are composable using [stampit](https://github.com/stampit-org/stampit)
```Javascript
const bearer = require('zdc-client').bearer;
const users = bearer({schema:{},namespace:'users'});const extendUsers = users.compose(/* some other stamp */);
```