Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing typings for event functions? #286

Open
Wazbat opened this issue May 13, 2021 · 5 comments
Open

Missing typings for event functions? #286

Wazbat opened this issue May 13, 2021 · 5 comments

Comments

@Wazbat
Copy link

Wazbat commented May 13, 2021

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
Copy link
Contributor

grant commented May 18, 2021

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

@Wazbat
Copy link
Author

Wazbat commented May 18, 2021

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
Copy link
Contributor

grant commented May 18, 2021

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
Copy link
Author

Wazbat commented May 18, 2021

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
Copy link
Author

Wazbat commented May 18, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants