gcf-decorators
v0.0.3
Published
Create a decorator-based framework for google cloud functions
Downloads
2
Readme
Decorators for Google Cloud Functions
Google Cloud Functions is an amazing product that lowers the threshold to develop and deploy code. However, it's still a little raw and lacks many basic validations. Very quickly, developers find themselves repeating the same set of validations across functions, and to that end, this library is meant to aid in making code DRY and clean, allowing developers to focus on core business logic.
Method Decorators
The library currently only supports using typescript method decorators, which means functions should be in classes instead of in a single file or declared as functions.
NOTE: Incorrect
exports function helloHttp = (req, res) => {
res.send(`Hello World!`);
};
Instead, use classes.
class HelloApi {
static async helloHttp(req, res) {
res.send(`Hello World!`);
}
}
Cors
Google's recommendation for handling CORS can be a bit gnarly, especially if the majority of cloud functions need to be cors enabled. With this library, we simply apply the @cors
decorator to the methods that are API's:
class HelloWorldApi {
@cors() // substitute you own domains here, default is '*'
static async helloWorld(req, res) {
return res.status(200).send('Hello world')
}
}
// In index.ts, do the following:
export const helloWorldApi = {
helloWorld: HelloWorldApi.helloWorld,
}
Http Methods
Similarly for HTTP methods, Google's official recommendation is extremely verbose. Instead, with this library, we can automatically enforce the correct HTTP method:
class HelloWorldApi {
@post() // anything other than a GET will automatically return a 404, with empty json
static async helloWorld(req, res) {
// Do cool POST things
return res.status(200).send('Hello world')
}
}
// In index.ts, do the following:
export const helloWorldApi = {
helloWorld: HelloWorldApi.helloWorld,
}
More powerfully, by coupling this library with tsc-parsers, we can enforce POST
and GET
parameters of even complex types.
class HelloWorldApi {
@post({
message: StringValidator, // Any non-string auto rejected with 400
count: IntValidator, // Any non-integer auto rejected with 400
})
static async helloWorld(req, res) {
// Do cool POST things
return res.status(200).send('Hello world')
}
}
// In index.ts, do the following:
export const helloWorldApi = {
helloWorld: HelloWorldApi.helloWorld,
}
Authorization
Currently, this library only supports Basic Authentication.
class HelloWorldApi {
@basicauth(process.env.SUPER_SECRET_PASSWORD)
@post() // anything other than a GET will automatically return a 404, with empty json
static async helloWorld(req, res) {
// Do cool POST things
return res.status(200).send('Hello world')
}
}
// In index.ts, do the following:
export const helloWorldApi = {
helloWorld: HelloWorldApi.helloWorld,
}