GoogleCloudPlatform / functions-framework-nodejs

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

Clarification on using CloudEventsContext / Context type #425

Closed jketcham closed 2 years ago

jketcham commented 2 years ago

I have a cloud function (triggered by PubSub) I've written in Typescript and transpiled with babel which was using the CloudEventsContext type exported in version 1.9.0. When I tried to upgrade to version 2.1.1, I now get the following errors:

node_modules/cloudevents/dist/index.d.ts(7,60): error TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided.
src/index.ts(1,15): error TS2305: Module '"@google-cloud/functions-framework"' has no exported member 'CloudEventsContext'.

It looks like I should now be using the Context type, but them I'm running into the same Cannot access ambient const enums error with the --isolatedModules flag, which looks like it's related to this issue with cloudevents itself: https://github.com/cloudevents/sdk-javascript/issues/456.

Can anyone provide any guidance on how I should type my cloud functions with CloudEvent context going forward? Is this something that needs to be addressed in the cloudevents repo?

Thank you!

jketcham commented 2 years ago

Locally I was able to remove the const declaration from this enum in cloudevents:

https://github.com/cloudevents/sdk-javascript/blob/2ac731eb884965e91a19bb3529100a6aee7069dd/src/event/cloudevent.ts#L17

and was able to get the type working correctly.

Added some more findings on the issue in this comment.

grant commented 2 years ago

Hey @jketcham, can you provide an exact reproduction of the issue with some code?

Have you tried importing CloudEvent instead? We decided to use the official CESDK for JS types. If there's a problem using them, we should fix that.

In terms of guidance, we provide a set of types in this file:

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/src/functions.ts

Which should be exported by default with the FF, from here:

https://github.com/GoogleCloudPlatform/functions-framework-nodejs/blob/master/src/index.ts


A minimal sample of the issue would help.

jketcham commented 2 years ago

Hi @grant, I think the issue I ran into should be resolved when the next version of cloudevents/sdk-javascript is released and can be used in this library.

For reference, the code I had previously that was working fine was importing CloudEventsContext like:

import { CloudEventsContext } from '@google-cloud/functions-framework';

I updated my code to instead import the Context type and the only problem there was the Cannot access ambient const enums error coming from the cloudevents library (which should be resolved in the next release).

grant commented 2 years ago

OK, we will wait for a new version of cloudevents. https://github.com/cloudevents/sdk-javascript/issues/456

jketcham commented 2 years ago

Hey @grant, I just tested out the new release for 3.0.0 with the update including cloudevents@5.3.2 and unfortunately am still getting the same error it seems 🤔

I created a repo that reproduces the issue here: https://github.com/jketcham/cloudevents-ambient-const-enums

This code is still giving that ambient const enums error when running tsc --noEmit with the latest versions:

import type { CloudEventV1 } from 'cloudevents';
import type { Context } from '@google-cloud/functions-framework';

export function handleWithCloudEvent(context: CloudEventV1<unknown>) {
  console.log({ context });
}

export function handleWithContext(context: Context) {
  console.log({ context });
}