@janiscommerce/api-save
Advanced tools
Comparing version 2.0.0 to 2.1.0
@@ -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 |
'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; |
@@ -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', |
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
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
14927
13
306
60
+ Added@janiscommerce/api@^3.1.1
+ Added@janiscommerce/active-client@1.2.2(transitive)
+ Added@janiscommerce/api@3.2.0(transitive)
- Removed@janiscommerce/api-view@^1.3.0
- Removed@janiscommerce/api-view@1.3.0(transitive)
Updatedlodash@^4.17.15