Skip to content

Make WrappedFunction curry type info #138

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

Merged
merged 2 commits into from
May 6, 2022
Merged

Make WrappedFunction curry type info #138

merged 2 commits into from
May 6, 2022

Conversation

inlined
Copy link
Member

@inlined inlined commented May 5, 2022

Sorry that the formatter made a bunch of changes. The majority of the impactful changes here are that the wrapped function types keep the type info of the functions that they wrap. This also required us a fix to the CloudFunction type because it is now generic based on an EventType rather than event data so that we can keep type info for subtypes of CloudEvent.

I'd like to eventually look at refactoring this to have fewer places where we have any. I think we might want to have three pieces:

function baseEvent<T>(): Omit<CloudEvent<any>, 'data'> that returns what you have today. Then in each partial you do the combination of baseEvent with per-event type overrides (e.g. bucket for Storage) and the partial data. By putting the event construction lower in the stack we can make sure data is referentially correct (e.g. the StorageEvent.bucket matches the StorageEvent.data.bucket)

@inlined inlined requested a review from TheIronDev May 5, 2022 16:58
function generateBaseCloudEvent<EventType extends CloudEvent<unknown>>(
cloudFunction: CloudFunction<EventType>
): EventType {
// TODO: Consider refactoring so that we don't use this utility function. This
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

import { alertsBillingOnPlanUpdatePublished } from './alerts/billing-on-plan-update-published';
import { eventarcOnCustomEventPublished } from './eventarc/eventarc-on-custom-event-published';
import { pubsubOnMessagePublished } from './pubsub/pubsub-on-message-published';
import { storageV1 } from './storage';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't there be a new file here?

Copy link
Contributor

@TheIronDev TheIronDev May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

src/cloudevent/partials/partials.ts:14:27 - error TS2307: Cannot find module './storage' or its corresponding type declarations.

14 import { storageV1 } from './storage';

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Added missing file back.

cloudFunction: CloudFunctionV2<T>
): WrappedV2Function<T>;

export function wrap<T, V extends CloudEvent<unknown>>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this function overloading much better than what I did earlier 🐢

@inlined inlined merged commit 9f401ca into master May 6, 2022
@inlined inlined deleted the inlined.type-fixes branch May 6, 2022 16:02
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 this pull request may close these issues.

None yet

2 participants