Skip to content

Commit

Permalink
Client identifiers settings with Settings package + readme + v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
juanhapes committed Jul 4, 2019
1 parent 0bc525d commit 5590226
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 108 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,13 @@ 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.6.0] - 2019-07-04
### Added
- `Settings` with `@janiscommerce/settings`

### Changed
- improves in `README.md`

## [1.5.0] - 2019-06-28
### Added
- `ActiveClient`
Expand Down
80 changes: 63 additions & 17 deletions README.md
Expand Up @@ -7,19 +7,55 @@ A package for managing API from any origin.

## Installation

```
```bash
npm install @janiscommerce/api
```

## Dispatcher
* **new Dispatcher( object )**
Construct a Dispatcher with the request information
## Client injection
The module can detect and inject a client. This works with configurable identifiers.
The api can receive the identifier and an internal model will get an inject the client for you.
The identifiers can be configurated with the package [Settings](https://www.npmjs.com/package/@janiscommerce/settings) in the key `api.identifiers`.

### Active Client
For the client injection functionality si required to install the package `active-client`.
The package `active-client` will get in DB by the field configurated in the identifiers and received in the api.
For more information see [Active Client](https://www.npmjs.com/package/@janiscommerce/active-client)

### Examples of configuration in **.janiscommercerc.json**

1. In this case `api` will use the *header* 'client' getting in DB using the field **name**

```json
{
"api": {
"identifiers": {
"header": "client",
"clientField": "name"
}
}
}
```

2. In this case `api` will search the client using `client-id` or `client-code` (sent in qs or requestBody), the field in DB is `id` and `code` respectively.
```json
{
"api": {
"identifiers": [{
"data": "client-id",
"clientField": "id"
}, {
"data": "client-code",
"clientField": "code"
}]
}
}
```


### Public methods

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

## Usage

Expand Down Expand Up @@ -66,25 +102,35 @@ You should extend your apis from this module.

### Public methods

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

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

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

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

* **setHeader(headerName, headerValue)**. Set an individual response header. `headerName` must be a string.
* **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.
* **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.
* **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.
* **setCookies(cookies)**.
Set response cookies. `cookies` must be an object with "key-value" cookies.

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

* **getController(ControllerName)**. Get a Controller instance with client injected.
* **getController(ControllerName)**.
Get a Controller instance with client injected.

### How to validate the API Structure (query string or request body)?
The API Struct is easily validated using [superstruct](https://www.npmjs.com/package/superstruct) (Thank's superstruct :pray:)
Expand Down
31 changes: 10 additions & 21 deletions lib/client.js
@@ -1,39 +1,28 @@
'use strict';

const path = require('path');

const ActiveClient = require('@janiscommerce/active-client');
const Settings = require('@janiscommerce/settings');

class Client {

static get identifierFilePath() {
return path.join(process.cwd(), 'config', 'api.json');
}
static get identifiers() {

static get identifier() {
if(typeof this._identifiers === 'undefined') {

if(typeof this._identifier === 'undefined') {
try {
/* eslint-disable global-require, import/no-dynamic-require */
this._identifier = require(this.identifierFilePath);
/* eslint-enable */
const apiSettings = Settings.get('api') || {};
let clientIdentifiers = apiSettings.clientIdentifiers || [];

if(!Array.isArray(this._identifier))
this._identifier = [this._identifier];
if(!Array.isArray(clientIdentifiers))
clientIdentifiers = [clientIdentifiers];

} catch(error) {
this._identifier = false;
}
this._identifiers = clientIdentifiers;
}

return this._identifier;
return this._identifiers;
}

static async setActive(api) {

if(!this.identifier)
return;

const { clientField, fieldValue } = this.getIdentifierFromApi(api);

if(!clientField || !fieldValue)
Expand All @@ -50,7 +39,7 @@ class Client {
let clientField;
let fieldValue;

for(const identifier of this.identifier) {
for(const identifier of this.identifiers) {

if(!this.isValidIdentifier(identifier)) {
// ERROR bad identifier
Expand Down
64 changes: 35 additions & 29 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/api",
"version": "1.5.0",
"version": "1.6.0",
"description": "A package for managing API from any origin",
"main": "index.js",
"scripts": {
Expand All @@ -25,17 +25,18 @@
"homepage": "https://github.com/janis-commerce/api#readme",
"devDependencies": {
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.3",
"husky": "^2.4.1",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.18.0",
"husky": "^2.7.0",
"mocha": "^5.2.0",
"mock-require": "^3.0.3",
"nyc": "^14.1.1",
"sinon": "^7.3.2"
},
"dependencies": {
"@janiscommerce/active-client": "^1.0.0",
"@janiscommerce/active-client": "^1.1.0",
"@janiscommerce/logger": "^1.1.0",
"@janiscommerce/settings": "^1.0.0",
"lodash": "^4.17.11",
"superstruct": "^0.6.1"
}
Expand Down

0 comments on commit 5590226

Please sign in to comment.