Skip to content

Function: Before() ​

Before<ClassPrototype, Method>(beforeFunc): MethodDecorator<ClassPrototype, Method>

Run a function before each call of the decorated method, with its arguments. The function is called with the instance as this; an array it returns replaces the arguments passed to the method, undefined keeps them. Several @Before on a method run in turn, each one receiving the arguments left by the previous one; an error thrown by the function is thrown by the call, and the method does not run.

Type Parameters ​

ClassPrototype ​

ClassPrototype extends Object

Method ​

Method extends (...args) => unknown

Parameters ​

beforeFunc ​

BeforeFunction<ClassPrototype, Method>

The function receiving the method's arguments. It may be async only when the method is async.

Returns ​

MethodDecorator<ClassPrototype, Method>

The method decorator.

Throws ​

When the class is defined, if beforeFunc is async and the method is not, or if the decorated member is not a method.

Example ​

typescript
import {Component} from 'lakutata'
import {Before} from 'lakutata/decorator/asst'

class Users extends Component {
    //The names are looked up trimmed and in lower case
    @Before(function (this: Users, name: string): [string] {
        return [name.trim().toLowerCase()]
    })
    public async find(name: string): Promise<string | undefined> {
        return name === 'ada' ? 'Ada Lovelace' : undefined
    }
}

Released under the MIT License.