Skip to content

Clarification on using CloudEventsContext / Context type #425

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

Closed
jketcham opened this issue Feb 9, 2022 · 5 comments · Fixed by #428
Closed

Clarification on using CloudEventsContext / Context type #425

jketcham opened this issue Feb 9, 2022 · 5 comments · Fixed by #428

Comments

@jketcham
Copy link

jketcham commented Feb 9, 2022

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: cloudevents/sdk-javascript#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
Copy link
Author

jketcham commented Feb 9, 2022

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

grant commented Feb 11, 2022

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

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

grant commented Feb 11, 2022

OK, we will wait for a new version of cloudevents. cloudevents/sdk-javascript#456

grant added a commit that referenced this issue Feb 12, 2022
Signed-off-by: Grant Timmerman <[email protected]>
grant added a commit that referenced this issue Feb 15, 2022
* chore: update cloudevents. Fixes #425

Signed-off-by: Grant Timmerman <[email protected]>

* chore: merge package json

Signed-off-by: Grant Timmerman <[email protected]>
@jketcham
Copy link
Author

Hey @grant, I just tested out the new release for 3.0.0 with the update including [email protected] 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 });
}

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

Successfully merging a pull request may close this issue.

2 participants