import { DGuard , Client , DDiscord , DIService } from "../.." ; import { Decorator } from "./Decorator" ; export abstract class Method extends Decorator { protected _discord : DDiscord ; protected _guards : DGuard [ ] = [ ] ; get discord ( ) { return this . _discord ; } set discord ( value ) { this . _discord = value ; } returns get execute ( ) { return async ( ... params ) => { return await this . getGuardFunction ( ) ( ... params ) ; } ; } /**
     * Returns all the guards of the application
     * The guards that are defined globaly with Client
     * The guards that decorate @Discord
     * The guards that decorate the method (this)
     */ get guards ( ) { const clientGuards = Client . guards . map ( ( guard ) => DGuard . create ( guard . bind ( undefined ) ) ) ; return [ ... clientGuards , ... this . discord . guards , ... this . _guards , DGuard . create ( this . _method . bind ( this . _discord . instance ) ) ] ; } set guards ( value ) { this . _guards = value ; } param params abstract parseParams ( ... params ) ; /**
     * Execute a guard with params
     */ getGuardFunction ( ) { const next = async ( params , index , paramsToNext ) => { const nextFn = ( ) => next ( params , index + 1 , paramsToNext ) ; const guardToExecute = this . guards [ index ] ; let res ; if ( index >= this . guards . length - 1 ) { res = await ( guardToExecute . fn as any ) ( ... this . parseParams ( ... params as any ) , ... params , paramsToNext ) ; } else { res = await ( guardToExecute . fn as any ) ( ... params , nextFn , paramsToNext ) ; } if ( res ) { return res ; } return paramsToNext ; } ; return ( ... params ) => next ( params , 0 , { } ) ; } }