@janiscommerce/api-get
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -8,2 +8,8 @@ # Changelog | ||
## [Unreleased] | ||
## [4.1.0] - 2022-05-31 | ||
### Added | ||
- Struct validation for path id | ||
## [4.0.1] - 2021-12-13 | ||
@@ -10,0 +16,0 @@ ### Added |
@@ -24,4 +24,7 @@ 'use strict'; | ||
this._parseEndpoint(); | ||
this._validateModel(); | ||
await this.validateId(); | ||
return this.postValidate(); | ||
@@ -31,2 +34,22 @@ } | ||
/** | ||
* Validates if path ID type is valid. | ||
* Validation will only be performed if database driver has `idStruct` getter implemented. | ||
* @returns {void} | ||
*/ | ||
async validateId() { | ||
let idStruct; | ||
try { | ||
idStruct = await this.model.getIdStruct(); | ||
} catch(error) { | ||
this.setCode(500); | ||
throw error; | ||
} | ||
if(idStruct) | ||
idStruct(this.recordId); | ||
} | ||
/** | ||
* It is to perform extra validations | ||
@@ -33,0 +56,0 @@ * @returns {*} |
{ | ||
"name": "@janiscommerce/api-get", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "A package to handle Janis Get APIs", | ||
@@ -31,3 +31,4 @@ "main": "lib/index.js", | ||
"sinon": "^12.0.1", | ||
"typescript": "^4.5.2" | ||
"typescript": "^4.5.2", | ||
"@janiscommerce/superstruct": "^1.2.0" | ||
}, | ||
@@ -34,0 +35,0 @@ "files": [ |
@@ -71,3 +71,3 @@ # API Get | ||
# Get APIs with parents | ||
## Get APIs with parents | ||
@@ -77,1 +77,34 @@ If you have for example, a get API for a sub-entity of one specific record, the parent will be automatically be added as a filter. | ||
For example, the following endpoint: `/api/parent-entity/1/sub-entity/2`, will be a get of the sub-entity, and `parentEntity: '1'` will be set as a filter. | ||
## ✔️ Path ID validation | ||
The `ID` in the `pathParameters` can be validated if the database needs it in order to avoid problems. If this feature is active, the statusCode for this kind of error will be `400`. This validation has a default behavior (Model version 6.3.0 or higher is needed), and can also be customized for specific needs. | ||
### Default behavior | ||
1. The ID will not be validated unless the database driver has `idStruct` method implemented. | ||
2. Validation applies only to main record ID (eg: For `/api/parent-entity/1/sub-entity/2` the ID validation will be applied only to `2`). | ||
### Customization | ||
❗In case you want to set a different behavior or validation, you can do it by overriding the `validateId` method. | ||
**eg: Adding validation for parent `ids`** | ||
```javascript | ||
validateId() { | ||
Object.values(this.parents).forEach(parentId => { | ||
struct('string&!empty')(parentId) | ||
}); | ||
struct('objectId')(this.recordId) | ||
} | ||
``` | ||
#### How to disable validation | ||
In case database driver has an `idStruct` defined and you want to disable validation, you can do it by overriding the `validateId` method. | ||
**eg:** | ||
```javascript | ||
validateId() { | ||
// Do nothing | ||
} | ||
``` |
@@ -19,2 +19,3 @@ export = ApiGetError; | ||
constructor(err: Error, code: number); | ||
message: string | Error; | ||
code: number; | ||
@@ -21,0 +22,0 @@ previousError: Error; |
export = ApiGet; | ||
declare 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} | ||
*/ | ||
validate(): void; | ||
/** | ||
* Validates if path ID type is valid. | ||
* Validation will only be performed if database driver has `idStruct` getter implemented. | ||
* @returns {void} | ||
*/ | ||
validateId(): void; | ||
/** | ||
* It is to perform extra validations | ||
@@ -9,2 +22,8 @@ * @returns {*} | ||
/** | ||
* 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} | ||
*/ | ||
process(): void; | ||
/** | ||
* Validates the record getted from DB before format. | ||
@@ -11,0 +30,0 @@ * @param {Object} record The record in DB |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
16069
374
109
0
10