GoogleCloudPlatform / functions-framework-nodejs

FaaS (Function as a service) framework for writing portable Node.js functions
Apache License 2.0
1.29k stars 158 forks source link

Missing typings for event functions? #286

Open Wazbat opened 3 years ago

Wazbat commented 3 years ago

Hi there! I'm trying to write a cloud functions project in typescript. The project has typings for http trigger functions available in the functions.ts under the HttpFunction type. These are very useful as they provide the necessary express types for the request and response objects

However when trying to write an event function triggered by a pub/sub message, for example, there is only the EventFunction type, however the data parameter is missing relevant properties for pub/sub triggers, storage triggers, etc

grant commented 3 years ago

Adding types for Pub/Sub, Storage, and other events sounds useful. Thanks for opening the issue.

Wazbat commented 3 years ago

Thanks

I came across this stackoverflow thread which told me where the types were. It might be worth opening making these more readily available

I'd open a PR for this but I'm not sure what the most correct way to do it would be

grant commented 3 years ago

Some more detail as to what you're looking for with some code would be helpful.

For HTTP, we can use req: express.Request, res: express.Response or HttpFunction

For events, we have EventFunction.

The interfaces are here: https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/src/functions.ts

I think we're talking about a more detailed interface for each type of event.

Wazbat commented 3 years ago

Example wise... Well, at the moment I'm just defining declaring my functions as

export const functionName: HttpFunction = async (req, res) => {
    res.send('data')
}

export const functionName: EventFunction = async (message, context) => {
    return 'data'
}

Looking through the documentation it appears that the arguments would be different for each trigger type with little overlap, so there wouldn't be much point doing anything like

 export const functionName: EventFunction<PubSubTrigger> = async (message, context) => {
    return message.data
}
export const functionName: EventFunction<StorageTrigger> = async (file, context) => {
    console.log(context.eventId)
    return file.bucket
}

So while simple, I think just defining some types for the different kinds of triggers would be what I'd be looking for

export const functionName: PubSubEventFunction = async (message, context) => {
    return message.data
}
export const functionName: StorageEventFunction = async (file, context) => {
    console.log(context.eventId)
    return file.bucket
}
Wazbat commented 3 years ago

From what I understand after reading through the docs (I think you can gcloud functions call a pub sub function with an empty json object as data, so no message), I think this simple thing would be the types for it

export interface PubSubEventFunction {
    (data: { message?: string }, context: Context): any
}

I was looking through the storage trigger docs and I understand those typings would be a lot more in depth. I've never had to work with those triggers so I'm not sure what other properties there would be on the file parameter aside from the ones in the example, or if you could call it from the command line without any of them, so an empty object