import * as Action from './generated/Actions' ; export enum Effect { ALLOW = 'Allow' , DENY = 'Deny' } export interface PolicyFactoryProps { effect ? : Effect | string ; resources ? : string [ ] ; actions ? : string [ ] ; } export class PolicyFactory { protected effect : string ; protected resources : string [ ] ; protected actions : string [ ] ; constructor ( props ? ) { this . effect = Effect . ALLOW ; this . resources = [ ] ; this . actions = [ ] ; if ( props ) { const { effect , resources , actions } = props ; this . effect = effect ? effect : this . effect ; this . resources = resources ? resources : this . resources ; if ( actions ) { this . addActions ( actions ) ; } } } setEffect ( effect ) { this . effect = effect ; return this ; } addResource ( arn ) { this . resources . push ( arn ) ; return this ; } addResources ( resources ) { resources . forEach ( resource => { this . addResource ( resource ) ; } ) ; return this ; } private isValidAction ( action ) { return Object . values ( Action ) . find ( service => { return Object . values ( service ) . find ( a => a === action ) ; } ) ; } addAction ( action ) { if ( this . isValidAction ( action ) ) { return this . addActionRaw ( action ) ; } throw new Error ( `Invalid action: "${ action }".` + 'If you need to include a wildcard, please use factory.addActionRaw(action).' ) ; } addActionRaw ( action ) { this . actions . push ( action ) ; return this ; } addActions ( actions ) { actions . forEach ( action => this . addAction ( action ) ) ; return this ; } buildPolicy ( ) { const { effect , resources , actions } = this ; return JSON . stringify ( { effect , resources , actions } ) ; } }