Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@janiscommerce/api-get

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@janiscommerce/api-get - npm Package Compare versions

Comparing version 4.0.0 to 4.0.1

types/api-get-error.d.ts

8

CHANGELOG.md

@@ -8,5 +8,13 @@ # Changelog

## [4.0.1] - 2021-12-13
### Added
- Typings build from JSDoc
### Changed
- Updated dependencies
## [4.0.0] - 2020-08-27
### Added
- GitHub Actions for build, coverage and publish
- Added Type-definitions

@@ -13,0 +21,0 @@ ### Changed

'use strict';
/**
* @typedef CodesError
* @property {Number} INVALID_REQUEST_DATA
* @property {Number} INVALID_ENTITY
* @property {Number} INTERNAL_ERROR
*/
class ApiGetError extends Error {
/**
* Get the error codes
* @returns {CodesError}
*/
static get codes() {

@@ -15,2 +26,6 @@

/**
* @param {Error} err The details of the error
* @param {Number} code The error code
*/
constructor(err, code) {

@@ -17,0 +32,0 @@

85

lib/api-get.js

@@ -9,11 +9,35 @@ 'use strict';

class ApiGet extends API {
/**
* @typedef {Object} ApiGetError A instance of APIGetError class
*/
module.exports = class ApiGet extends API {
/**
* Perform validations before processing
* Set the Model to use and parse the Endpoint
* Important, it is not advisable to overwrite it
* @returns {void}
*/
async validate() {
this._parseEndpoint();
this._validateModel();
this._validateModel();
return this.postValidate();
}
/**
* It is to perform extra validations
* @returns {*}
*/
async postValidate() {
return true;
}
/**
* It makes the query to the DB with the filters and params obtained from the endpoint
* Important, it is not advisable to overwrite it
* @returns {void}
*/
async process() {

@@ -27,3 +51,3 @@

const getParams = {
filters: this._parseFilters ? this._parseFilters(filters) : filters,
filters: this._parseFilters(filters),
page: 1,

@@ -48,6 +72,5 @@ limit: 1

if(this.postGetValidate)
await this.postGetValidate(this.record);
await this.postGetValidate(this.record);
const response = this.format ? await this.format(this.record) : this.record;
const response = await this.format(this.record);

@@ -57,2 +80,33 @@ this.setBody(response);

/**
* Validates the record getted from DB before format.
* @param {Object} record The record in DB
* @returns {*}
*/
async postGetValidate(record) {
return record;
}
/**
* For format your record before they are returned.
* @param {Object} record The record in DB
* @returns {Object}
*/
async format(record) {
return record;
}
/**
* To parse any field of the record
* @param {Object} record The record in DB
* @returns {Object} The record parsed
*/
_parseFilters(record) {
return record;
}
/**
* Set the modelName, recordId and parents of API after parsing the endpoint
* @returns {void}
*/
_parseEndpoint() {

@@ -62,3 +116,5 @@

this.modelName = modelName;
if(!this.modelName)
this.modelName = modelName;
this.recordId = recordId;

@@ -68,4 +124,10 @@ this.parents = parents;

/**
* Set the model of the API getting the model instance from its name
* @throws {ApiGetError} if the model not exists
* @returns {void}
*/
_validateModel() {
try {
/* istanbul ignore next */
this.model = this._getModelInstance(path.join(process.cwd(), process.env.MS_PATH || '', 'models', this.modelName));

@@ -77,2 +139,7 @@ } catch(e) {

/**
* Get the instance of the Model indicated by parameter
* @param {String} modelPath The model path
* @returns {Object} An instance injected with the session
*/
_getModelInstance(modelPath) {

@@ -89,4 +156,2 @@

}
module.exports = ApiGet;
};

@@ -6,4 +6,15 @@ 'use strict';

/**
* @typedef {Object} ParseEndpoint
* @property {String} [modelName] The model name
* @property {String} [recordId] The ID of the record
* @property {String} [parents] The rest of the text string that does not have a specific function
*/
class EndpointParser {
/**
* Parse the endpoint passed by parameter in different parts for use
* @param {String} endpoint The endpoint to parse
* @returns {ParseEndpoint} The parsed endpoint
*/
static parse(endpoint) {

@@ -10,0 +21,0 @@

'use strict';
/**
* Transform the string entered in camelCase format
* @param {String} string
* @returns {String}
*/
const camelize = string => string.replace(/-+([^-])/g, (_, firstLetter) => firstLetter.toUpperCase());
module.exports = camelize;

23

package.json
{
"name": "@janiscommerce/api-get",
"version": "4.0.0",
"version": "4.0.1",
"description": "A package to handle Janis Get APIs",

@@ -10,4 +10,6 @@ "main": "lib/index.js",

"test-ci": "nyc --reporter=lcov --reporter=text-summary mocha --exit --recursive tests/",
"build-types": "tsc lib/index.js --declaration --allowJs --emitDeclarationOnly --outDir types",
"coverage": "nyc npm test",
"lint": "eslint lib/ tests/"
"lint": "eslint lib/ tests/",
"prepare": "husky install"
},

@@ -22,14 +24,17 @@ "repository": {

"devDependencies": {
"eslint": "^7.7.0",
"eslint": "^8.4.1",
"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.20.2",
"husky": "^4.2.5",
"mocha": "^8.1.2",
"eslint-plugin-import": "^2.25.3",
"husky": "^7.0.4",
"mocha": "^9.1.3",
"mock-require": "^3.0.3",
"nyc": "^15.1.0",
"sinon": "^9.0.3"
"sinon": "^12.0.1",
"typescript": "^4.5.2"
},
"files": [
"lib/"
"lib/",
"types/"
],
"types": "types/index.d.ts",
"directories": {

@@ -39,4 +44,4 @@ "test": "tests"

"dependencies": {
"@janiscommerce/api": "^6.0.0"
"@janiscommerce/api": "^6.4.2"
}
}

@@ -56,2 +56,10 @@ # API Get

/**
* It is to perform extra validations
* @returns {void}
*/
async postValidate() {
if(!this.session.hasAccessToAllLocations)
throw new Error("No access");
}
}

@@ -58,0 +66,0 @@

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