New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@janiscommerce/api

Package Overview
Dependencies
Maintainers
1
Versions
53
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@janiscommerce/api - npm Package Compare versions

Comparing version 1.4.0 to 1.5.0

lib/client.js

13

CHANGELOG.md

@@ -7,2 +7,15 @@ # Changelog

## [1.5.0] - 2019-06-28
### Added
- `ActiveClient`
- `Client` injected in `API`
- `API` getController returns a Controller instance
### Changed
- improved `tests` organization
- improves in `README.md`
### Fixed
- `Fetcher` jsdocs
## [1.4.0] - 2019-06-18

@@ -9,0 +22,0 @@ ### Added

'use strict';
const Controller = require('./controller');
class API {

@@ -50,2 +52,10 @@

set client(client) {
this._client = client;
}
get client() {
return this._client;
}
// Response methods

@@ -86,4 +96,15 @@

}
// Controller helpers
getController(controllerName) {
const controller = Controller.get(controllerName);
if(this.client)
controller.client = this.client;
return controller;
}
}
module.exports = API;

@@ -9,2 +9,3 @@ 'use strict';

const APIError = require('./error');
const Client = require('./client');

@@ -75,2 +76,4 @@ class Dispatcher {

await this.setClient();
await this.validate();

@@ -106,2 +109,10 @@

async setClient() {
if(this.hasError)
return;
await Client.setActive(this.api);
}
async validate() {

@@ -108,0 +119,0 @@

81

lib/fetcher.js

@@ -32,6 +32,44 @@ 'use strict';

/**
* Get a new REST API Controller Instance
* Make and returns the fileName based on endpoint and http method
* @example
* this.filePath() // this.endpoint = '/products/10/skus/'; this.method = 'get'
* products/skus/get.js
*/
get filePath() {
const urlParts = flow(
toLower,
split('/'),
tail
)(this.endpoint);
// if request a list of resources the method is 'list', otherwise is the method
const method = (this.method === 'get' && urlParts.length % 2 === 1) ? 'list' : this.method;
const filePath = flow(
filter((value, index) => index % 2 === 0),
join('/')
)(urlParts);
return path.join(this.constructor.apiPath, filePath, method);
}
/**
* @return {Array} Path parameters
*/
get pathParameters() {
return flow(
toLower,
split('/'),
tail,
filter((value, index) => index % 2 === 1)
)(this.endpoint);
}
/**
* Get a new API REST Controller Instance
*
* @return {Module} The rest controller.
* @return {object} The API Rest controller.
*/

@@ -74,3 +112,3 @@ get apiController() {

if(!(apiController instanceof API))
throw new Error(`API '${apiController.constructor.name}' does not inherit from API`);
throw new Error(`API '${apiController.constructor.name}' does not inherit from 'API'`);

@@ -84,41 +122,4 @@ // validate api process method

/**
* Make and returns the fileName based on endpoint and http method
* @example
* this.filePath() // this.endpoint = '/products/10/skus/'; this.method = 'get'
* products/skus/get.js
*/
get filePath() {
const urlParts = flow(
toLower,
split('/'),
tail
)(this.endpoint);
// if request a list of resources the method is 'list', otherwise is the method
const method = (this.method === 'get' && urlParts.length % 2 === 1) ? 'list' : this.method;
const filePath = flow(
filter((value, index) => index % 2 === 0),
join('/')
)(urlParts);
return path.join(this.constructor.apiPath, filePath, method);
}
/**
* @return {Array} Path parameters
*/
get pathParameters() {
return flow(
toLower,
split('/'),
tail,
filter((value, index) => index % 2 === 1)
)(this.endpoint);
}
}
module.exports = Fetcher;

@@ -7,2 +7,3 @@ 'use strict';

const APIError = require('./error');
const Client = require('./client');

@@ -13,3 +14,4 @@ module.exports = {

Fetcher,
APIError
APIError,
Client
};
{
"name": "@janiscommerce/api",
"version": "1.4.0",
"version": "1.5.0",
"description": "A package for managing API from any origin",

@@ -37,2 +37,3 @@ "main": "index.js",

"dependencies": {
"@janiscommerce/active-client": "^1.0.0",
"@janiscommerce/logger": "^1.1.0",

@@ -39,0 +40,0 @@ "lodash": "^4.17.11",

@@ -14,6 +14,8 @@ # API

## APIDispatcher
* **new APIDispatcher( object )**
Construct an API
## Dispatcher
* **new Dispatcher( object )**
Construct a Dispatcher with the request information
### Public methods
* async **.dispatch()**

@@ -28,5 +30,5 @@ This method dispatch the api instance.

```js
const { APIDispatcher } = require('@janiscommerce/api');
const { Dispatcher } = require('@janiscommerce/api');
const dispatcher = new APIDispatcher({
const dispatcher = new Dispatcher({
endpoint: 'api/pets',

@@ -67,2 +69,4 @@ method: 'get', // this is the default verb

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

@@ -86,4 +90,8 @@

### How to use the API?
* **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:)
If you want to use this validation, you should add a getter method `struct()`.
```js

@@ -103,3 +111,17 @@ const { API } = require('@janiscommerce/api');

}
}
module.exports = MyApi;
```
### How to add custom validation for my API?
The way to add some custom validation is adding a `validate()` method.
This method is called by `Dispatcher` after validate de Struct.
```js
const { API } = require('@janiscommerce/api');
class MyApi extends API {
/**

@@ -118,3 +140,14 @@ * Optional method for extra validation

}
}
module.exports = MyApi;
```
### How to process the API and response correctly?
```js
const { API } = require('@janiscommerce/api');
class MyApi extends API {
/**

@@ -145,2 +178,3 @@ * Required method for api process

module.exports = MyApi;
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc