Search code examples
typescripttypescript-decorator

Typescript decorators for @OnCall and @OnRequest firebase functions?


I'm trying to write some method decorators to use with firebase functions in typescript but i'm a bit confused on the syntax and what I've tried so far isn't working.

I'd like to have methods in my class, for example:

@OnCall('europe-west1')
@Wrapper([errorHandler, validateSomething])
doSomething(data, context) {
// doing stuff in the function
}

and the final result to be:

const doSomething = functions.region(<REGION PARAM>).https.onCall((data, context) => {

errorHandler() {

  validateSomething() {

    DECORATEDFUNCTION()
}

});

Now the wrapper part isn't as critical at first although desirable. So far I've tried various variations of this:

export function OnCall(region: string, name: string, target: any): MethodDecorator {
    return target => {
        //
    };
}

Solution

  • For anyone stumbling upon this, I ended up writing a few decorators and a function that consumes them to get the indended result, I have, in the end, abandoned the idea as it seems to go agains the purpose of serverless functions and essentially just trying to mimmick REST endpoints with standalone functions.

    The github project can be found here. Where there's also an example project using the decorators.