2022-03-21 20:53:46 +00:00
|
|
|
import * as plugins from './smartguard.plugins.js';
|
2024-05-30 14:57:18 +00:00
|
|
|
import { Guard, type TGuardFunction } from './smartguard.classes.guard.js';
|
2019-08-07 14:31:53 +00:00
|
|
|
|
|
|
|
/**
|
2024-05-30 14:57:18 +00:00
|
|
|
* Extended GuardSet that inherits from Guard
|
|
|
|
* and provides additional functionalities.
|
2019-08-07 14:31:53 +00:00
|
|
|
*/
|
2024-05-30 14:57:18 +00:00
|
|
|
export class GuardSet<T> extends Guard<T> {
|
2019-08-07 14:31:53 +00:00
|
|
|
public guards: Array<Guard<T>>;
|
2024-05-30 14:57:18 +00:00
|
|
|
|
|
|
|
constructor(guardArray: Array<Guard<T>> = []) {
|
|
|
|
super(async (dataArg: T) => {
|
|
|
|
return this.allGuardsPass(dataArg);
|
|
|
|
})
|
|
|
|
this.guards = guardArray;
|
2019-08-07 14:31:53 +00:00
|
|
|
}
|
|
|
|
|
2024-05-30 14:57:18 +00:00
|
|
|
/**
|
|
|
|
* executes all guards in all guardSets against a data argument
|
|
|
|
* @param dataArg
|
|
|
|
*/
|
|
|
|
public async executeAllGuardsWithData(dataArg: T) {
|
2019-08-07 14:31:53 +00:00
|
|
|
const resultPromises: Array<Promise<boolean>> = [];
|
2024-05-30 14:57:18 +00:00
|
|
|
|
2019-08-07 14:31:53 +00:00
|
|
|
for (const guard of this.guards) {
|
2024-05-30 14:57:18 +00:00
|
|
|
const guardResultPromise = guard.executeGuardWithData(dataArg);
|
|
|
|
resultPromises.push(guardResultPromise);
|
2019-08-07 14:31:53 +00:00
|
|
|
}
|
2024-05-30 14:57:18 +00:00
|
|
|
|
|
|
|
const results = await Promise.all(resultPromises);
|
2019-08-07 14:31:53 +00:00
|
|
|
return results;
|
|
|
|
}
|
2024-05-30 14:57:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if all guards pass
|
|
|
|
* @param dataArg
|
|
|
|
*/
|
|
|
|
public async allGuardsPass(dataArg: T): Promise<boolean> {
|
|
|
|
const results = await this.executeAllGuardsWithData(dataArg);
|
|
|
|
return results.every(result => result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* checks if any guard passes
|
|
|
|
* @param dataArg
|
|
|
|
*/
|
|
|
|
public async anyGuardsPass(dataArg: T): Promise<boolean> {
|
|
|
|
const results = await this.executeAllGuardsWithData(dataArg);
|
|
|
|
return results.some(result => result);
|
|
|
|
}
|
2019-08-07 14:34:34 +00:00
|
|
|
}
|