Created Operators type and added type guard + fixed filter value to be string or number
Signed-off-by: shmaram <shaharshmaram@gmail.com>
This commit is contained in:
@@ -16,6 +16,20 @@
|
||||
|
||||
import { createApiRef } from '@backstage/core-plugin-api';
|
||||
|
||||
/**
|
||||
* @public
|
||||
* The operators that can be used in filter.
|
||||
*/
|
||||
export type Operators = '<' | '<=' | '==' | '!=' | '>' | '>=' | 'contains';
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Type guard for operators.
|
||||
*/
|
||||
export const isOperator = (s: string): s is Operators => {
|
||||
return ['<', '<=', '==', '!=', '>', '>=', 'contains'].includes(s);
|
||||
};
|
||||
|
||||
/**
|
||||
* @public
|
||||
* Model for a visit entity.
|
||||
@@ -84,7 +98,7 @@ export type VisitsApiQueryParams = {
|
||||
*/
|
||||
filterBy?: Array<{
|
||||
field: keyof Visit;
|
||||
operator: '<' | '<=' | '==' | '!=' | '>' | '>=' | 'contains';
|
||||
operator: Operators;
|
||||
value: string | number;
|
||||
}>;
|
||||
};
|
||||
|
||||
@@ -14,7 +14,12 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Visit, VisitsApiQueryParams } from './VisitsApi';
|
||||
import {
|
||||
Visit,
|
||||
VisitsApiQueryParams,
|
||||
Operators,
|
||||
isOperator,
|
||||
} from './VisitsApi';
|
||||
import { Config } from '@backstage/config';
|
||||
|
||||
/**
|
||||
@@ -24,25 +29,23 @@ import { Config } from '@backstage/config';
|
||||
*
|
||||
* @public
|
||||
*/
|
||||
|
||||
export function readFilterConfig(config: Config):
|
||||
| {
|
||||
field: keyof Visit;
|
||||
operator: '<' | '<=' | '==' | '!=' | '>' | '>=' | 'contains';
|
||||
operator: Operators;
|
||||
value: string | number;
|
||||
}
|
||||
| undefined {
|
||||
try {
|
||||
const field = config.getString('field') as keyof Visit;
|
||||
const operator = config.getString('operator') as
|
||||
| '<'
|
||||
| '<='
|
||||
| '=='
|
||||
| '!='
|
||||
| '>'
|
||||
| '>='
|
||||
| 'contains';
|
||||
const value = config.getString('value');
|
||||
return { field, operator, value };
|
||||
const operator = config.getString('operator');
|
||||
const value =
|
||||
config.getOptionalNumber('value') ?? config.getString('value');
|
||||
if (isOperator(operator)) {
|
||||
return { field, operator, value };
|
||||
}
|
||||
return undefined;
|
||||
} catch (error) {
|
||||
// invalid filter config - ignore filter
|
||||
return undefined;
|
||||
|
||||
Reference in New Issue
Block a user