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

@janiscommerce/api-save

Package Overview
Dependencies
Maintainers
1
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@janiscommerce/api-save - npm Package Compare versions

Comparing version 2.0.0 to 2.1.0

lib/helpers/endpoint-parser.js

10

CHANGELOG.md

@@ -10,2 +10,12 @@ # Changelog

## [2.1.0] - 2019-07-26
### Added
- Support for client specific models instanciation
### Changed
- Now save APIs rely on API package, so they can be handled as common REST APIs
### Fixed
- README updated
## [2.0.0] - 2019-07-18

@@ -12,0 +22,0 @@ ### Changed

18

lib/api-save-data.js
'use strict';
const { APIView } = require('@janiscommerce/api-view');
const { API } = require('@janiscommerce/api');

@@ -9,4 +9,5 @@ const ApiSaveValidator = require('./api-save-validator');

const ApiSaveError = require('./api-save-error');
const EndpointParser = require('./helpers/endpoint-parser');
class ApiSaveData extends APIView {
class ApiSaveData extends API {

@@ -17,4 +18,6 @@ static get relationshipsParameters() {

validate() {
async validate() {
this._parseEndpoint();
const apiSaveValidator = new ApiSaveValidator(this);

@@ -56,4 +59,13 @@

}
_parseEndpoint() {
const { modelName, recordId, parents } = EndpointParser.parse(this.endpoint);
this.modelName = modelName;
this.recordId = recordId;
this.parents = parents;
}
}
module.exports = ApiSaveData;

4

lib/api-save-relationships.js

@@ -39,3 +39,3 @@ 'use strict';

const model = new ModelClass();
const model = this.apiInstance.client ? this.apiInstance.client.getInstance(ModelClass) : new ModelClass();
return model.multiInsert(relationshipsToSave);

@@ -48,3 +48,3 @@ }

const model = new ModelClass();
const model = this.apiInstance.client ? this.apiInstance.client.getInstance(ModelClass) : new ModelClass();
const currentRelationships = model.get({

@@ -51,0 +51,0 @@ [mainIdentifierField]: this.mainId

@@ -26,11 +26,16 @@ 'use strict';

const dataToValidate = {
...this.apiInstance.data,
...this.apiInstance.parents
};
try {
return validationStruct({
id: this.apiInstance.pathParameters[0],
main: this.apiInstance.data,
relationships: this.apiInstance.data
id: this.apiInstance.recordId,
main: dataToValidate,
relationships: dataToValidate
});
} catch(e) {
const errorParh = e.path.join('.');
throw new ApiSaveError(`${e.message} in ${errorParh}`, ApiSaveError.codes.INVALID_REQUEST_DATA);
const errorPath = e.path.join('.');
throw new ApiSaveError(`${e.message} in ${errorPath}`, ApiSaveError.codes.INVALID_REQUEST_DATA);
}

@@ -41,3 +46,3 @@ }

try {
return this._getModelInstance();
return this._getModelInstance(path.join(process.cwd(), process.env.MS_PATH || '', 'models', this.apiInstance.modelName));
} catch(e) {

@@ -49,6 +54,6 @@ throw new ApiSaveError(e.message, ApiSaveError.codes.INVALID_ENTITY);

/* istanbul ignore next */
_getModelInstance() {
_getModelInstance(modelPath) {
// eslint-disable-next-line global-require, import/no-dynamic-require
const Model = require(path.join(process.cwd(), process.env.MS_PATH, 'models', this.apiInstance.entity));
return new Model();
const Model = require(modelPath);
return this.client ? this.client.getInstance(Model) : new Model();
}

@@ -55,0 +60,0 @@

{
"name": "@janiscommerce/api-save",
"version": "2.0.0",
"version": "2.1.0",
"description": "A package to handle JANIS Views Save APIs",

@@ -23,3 +23,3 @@ "main": "index.js",

"eslint-config-airbnb-base": "^13.2.0",
"eslint-plugin-import": "^2.17.3",
"eslint-plugin-import": "^2.18.2",
"husky": "^2.7.0",

@@ -37,7 +37,7 @@ "mocha": "^5.2.0",

"dependencies": {
"@janiscommerce/api-view": "^1.3.0",
"@janiscommerce/api": "^3.1.1",
"@janiscommerce/model": "^1.1.1",
"lodash": "^4.17.14",
"lodash": "^4.17.15",
"superstruct": "^0.6.1"
}
}

@@ -18,5 +18,6 @@ # API Save

const { ApiSaveData } = require('@janiscommerce/api-save');
const { Controller } = require('@janiscommerce/model-controller');
const { struct } = require('superstruct');
const MyRelatedModel = require('../../models/my-related-model');
class MyApiSaveData extends ApiSaveData {

@@ -27,3 +28,3 @@

children: {
controller: Controller.getInstance('some-relationship-entity'),
ModelClass: MyRelatedModel,
mainIdentifierField: 'dbFieldForMainEntity',

@@ -30,0 +31,0 @@ secondaryIdentifierField: 'dbFieldForRelatedEntity',

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