@janiscommerce/api
Advanced tools
Comparing version 1.5.0 to 1.6.0
@@ -7,2 +7,9 @@ # Changelog | ||
## [1.6.0] - 2019-07-04 | ||
### Added | ||
- `Settings` with `@janiscommerce/settings` | ||
### Changed | ||
- improves in `README.md` | ||
## [1.5.0] - 2019-06-28 | ||
@@ -9,0 +16,0 @@ ### Added |
'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; | ||
} | ||
@@ -34,5 +26,2 @@ | ||
if(!this.identifier) | ||
return; | ||
const { clientField, fieldValue } = this.getIdentifierFromApi(api); | ||
@@ -54,3 +43,3 @@ | ||
for(const identifier of this.identifier) { | ||
for(const identifier of this.identifiers) { | ||
@@ -57,0 +46,0 @@ if(!this.isValidIdentifier(identifier)) { |
{ | ||
"name": "@janiscommerce/api", | ||
"version": "1.5.0", | ||
"version": "1.6.0", | ||
"description": "A package for managing API from any origin", | ||
@@ -28,5 +28,5 @@ "main": "index.js", | ||
"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", | ||
@@ -38,4 +38,5 @@ "mock-require": "^3.0.3", | ||
"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", | ||
@@ -42,0 +43,0 @@ "superstruct": "^0.6.1" |
@@ -10,15 +10,51 @@ # API | ||
``` | ||
```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`. | ||
@@ -70,21 +106,31 @@ ## Usage | ||
* **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. | ||
@@ -91,0 +137,0 @@ ### How to validate the API Structure (query string or request body)? |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
19976
220
2
5
444