Skip to content

Commit

Permalink
Updated README + version 1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
juanhapes committed Jun 12, 2019
1 parent ab6f06e commit 496431d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.3.0] - 2019-06-12
### Added
- Improved `README.md`
- Dispatcher
- `API` inheritance

## [1.2.0] - 2019-05-29
### Added
- Api tests
Expand Down
93 changes: 86 additions & 7 deletions README.md
Expand Up @@ -11,30 +11,32 @@ A package for managing API from any origin.
npm install @janiscommerce/api
```

## API
* **new API( object )**
## APIDispatcher
* **new APIDispatcher( object )**
Construct an API

* async **.dispatch()**
This method dispatch the api instance.
Returns an object with `code` and the `result`.
Returns an object with `code` and the `body`.

## Usage

### How to dispatch an API? (from a server)

```js
const API = require('@janiscommerce/api');
const { APIDispatcher } = require('@janiscommerce/api');

const myApi = new API({
const dispatcher = new APIDispatcher({
endpoint: 'api/pets',
method: 'get', // this is the default verb
data: { status: 'active' },
headers: { 'Content-Type': 'application/json' },
cookies: { 'my-cookie': 123 }
});

const result = await myApi.dispatch();
const response = await dispatcher.dispatch();

console.log(result);
console.log(response);
/**
expected output:
Expand All @@ -55,6 +57,83 @@ console.log(result);
]
}
*/
```

## API
You should extend your apis from this module.

* **pathParameters** (*getter*). Returns the path parameters of the request.

* **headers** (*getter*). Returns the the headers of the request.

* **cookies** (*getter*). Returns the the cookies of the request.

* **setCode(code)**. Set a response httpCode. `code` must be a integer.

* **setHeader(headerName, headerValue)**. Set an individual response header. `headerName` must be a string.

* **setHeaders(headers)**. Set response headers. `headers` must be an object with "key-value" headers.

* **setCookie(cookieName, cookieValue)**. Set an individual response cookie. `cookieName` must be a string.

* **setCookies(cookies)**. Set response cookies. `cookies` must be an object with "key-value" cookies.

* **setBody(body)**. Set the response body.

### How to use the API?

```js
const { API } = require('@janiscommerce/api');

class MyApi extends API {

/**
* Optional method for struct validation (qs or requestBody)
*/
get struct() {
return {
id: 'number',
name: 'string'
};
}

/**
* Optional method for extra validation
*/
async validate() {

if(this.data.id > 10)
throw new Error('Weird validation fail'); // this will response a 400 error

if(!existsInMyDB(this.data.id)) {
this.setCode(404); // set a custom http resposne code
throw new Error('resource not found'); // this will response a 404 error
}
}

/**
* Required method for api process
*/
async process() {

if(!saveInMyDB(this.data))
throw new Error('internal save error'); // this will response a 500 error

if(!saveOtherThingInMyDB(this.data)) {
this.setCode(504); // set a custom http resposne code
throw new Error('internal save error');
}

this
.setHeader('my-header-1', 'foo')
.setHeaders({ 'my-header-2': 'foo', 'my-header-3': 'foo' })
.setCookie('my-cookie-1', 'bar')
.setCookies({ 'my-cookie-2': 'bar', 'my-cookie-3': 'bar' })
.setBody({
'response-body': 123
});
}
}

module.exports = MyApi;
```
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/api",
"version": "1.2.0",
"version": "1.3.0",
"description": "A package for managing API from any origin",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 496431d

Please sign in to comment.