@janiscommerce/api-list
Advanced tools
Comparing version 3.2.1 to 3.3.0
@@ -8,2 +8,8 @@ # Changelog | ||
## [Unreleased] | ||
## [3.3.0] - 2020-06-05 | ||
### Added | ||
- multiple search filters | ||
## [3.2.1] - 2020-05-19 | ||
@@ -10,0 +16,0 @@ ### Fixed |
@@ -35,3 +35,3 @@ 'use strict'; | ||
this.filter = new Filter(requestFilters, this.availableFilters); | ||
this.filter = new Filter(requestFilters, this.availableFilters, this.searchFilters); | ||
this.paging = new Paging(this.headers); | ||
@@ -38,0 +38,0 @@ this.sort = new Sort(this.data, this.sortableFields); |
@@ -7,4 +7,5 @@ 'use strict'; | ||
constructor(requestFilters, availableFilters) { | ||
constructor(requestFilters, availableFilters, searchFilters) { | ||
this.requestFilters = requestFilters; | ||
this.searchFilters = searchFilters || []; | ||
this.availableFilters = availableFilters.map(filter => { | ||
@@ -17,3 +18,8 @@ return typeof filter === 'object' ? filter : { name: filter }; | ||
if(!this.availableFilters.length) | ||
const filters = [...this.availableFilters]; | ||
if(this.requestFilters.search && this.searchFilters.length) | ||
filters.push({ name: 'search' }); | ||
if(!filters.length) | ||
return {}; | ||
@@ -23,3 +29,3 @@ | ||
for(const filter of this.availableFilters) | ||
for(const filter of filters) | ||
filterStruct[filter.name] = struct.optional('string | number | array'); | ||
@@ -40,5 +46,20 @@ | ||
return this.availableFilters.filter(filter => { | ||
const filtersParams = [ | ||
...this.getParamsFromAvailableFilters(clientFiltersWithDefaults), | ||
...this.getParamsFromSearchFilters(clientFiltersWithDefaults) | ||
]; | ||
if(!filtersParams.length) | ||
return {}; | ||
return { | ||
filters: filtersParams.length === 1 ? filtersParams[0] : filtersParams | ||
}; | ||
} | ||
getParamsFromAvailableFilters(clientFiltersWithDefaults) { | ||
const availableFiltersParams = this.availableFilters.filter(filter => { | ||
return clientFiltersWithDefaults[filter.name] !== undefined; | ||
}).reduce(({ filters }, filter) => { | ||
}).reduce((filters, filter) => { | ||
@@ -54,12 +75,23 @@ const originalValue = clientFiltersWithDefaults[filter.name]; | ||
return { | ||
filters: { | ||
...filters, | ||
[finalName]: filterValue | ||
} | ||
...filters, | ||
[finalName]: filterValue | ||
}; | ||
}, {}); | ||
return Object.keys(availableFiltersParams).length ? [availableFiltersParams] : []; | ||
} | ||
getParamsFromSearchFilters({ search: searchOriginalValues } = {}) { | ||
if(!searchOriginalValues) | ||
return []; | ||
const searchValues = searchOriginalValues.split(' '); | ||
return this.searchFilters.map(filter => { | ||
return searchValues.map(value => ({ [filter]: { type: 'search', value } })); | ||
}).flat(); | ||
} | ||
} | ||
module.exports = ListFilter; |
{ | ||
"name": "@janiscommerce/api-list", | ||
"version": "3.2.1", | ||
"description": "A package to handle JANIS List APIs", | ||
"version": "3.3.0", | ||
"description": "A package to handle Janis List APIs", | ||
"main": "lib/index.js", | ||
@@ -28,3 +28,3 @@ "scripts": { | ||
"nyc": "^13.1.0", | ||
"sinon": "^7.3.2" | ||
"sinon": "^7.5.0" | ||
}, | ||
@@ -31,0 +31,0 @@ "files": [ |
@@ -6,3 +6,3 @@ # API List | ||
A package to handle JANIS List APIs | ||
A package to handle Janis List APIs | ||
@@ -50,2 +50,9 @@ ## Installation | ||
get searchFilters() { | ||
return [ | ||
'id', | ||
'quantity' | ||
]; | ||
} | ||
async formatRows(rows) { | ||
@@ -152,1 +159,37 @@ return rows.map(row => ({ ...row, oneMoreField: true })); | ||
``` | ||
### get searchFilters() | ||
_Since 3.3.0_ | ||
This is used to indicate which fields will be used to mapped multiple filters (OR Type) for the same value, using only `search` as single filter. | ||
If it don't exist or return an empty array and try to use `search` filter will return 400 status code. | ||
Can be combined with other filters. | ||
For example: | ||
```js | ||
'use strict'; | ||
const { | ||
ApiListData | ||
} = require('@janiscommerce/api-list'); | ||
module.exports = class MyApiListData extends ApiListData { | ||
get searchFilters() { | ||
return ['someField', 'otherField']; | ||
} | ||
}; | ||
``` | ||
* `/api/entity?filters[search]=1` with a single value. | ||
Will filter the list for `someField: 1` or `otherField: 1` | ||
* `/api/entity?filters[search]=fo` with a uncomplete word. | ||
Will filter the list for `someField: fo` or `otherField: fo` and will do a parcial filter (like using `searchMapper`). | ||
* `/api/entity?filters[search]=foo bar` with multiples words divided by white spaces. | ||
Will filter the list for `someField: foo` or `someField: bar` or `otherField: foo` or `otherField: bar`. |
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
19081
348
193