diff --git a/spec/v2.spec.ts b/spec/v2.spec.ts index 1101aa7..6a4b77b 100644 --- a/spec/v2.spec.ts +++ b/spec/v2.spec.ts @@ -39,12 +39,28 @@ import { import { defineString } from 'firebase-functions/params'; import { makeDataSnapshot } from '../src/providers/database'; import { makeDocumentSnapshot } from '../src/providers/firestore'; +import { makeChange } from '../src/v1'; import { inspect } from 'util'; describe('v2', () => { describe('#wrapV2', () => { const handler = (cloudEvent) => ({ cloudEvent }); + it('should restrict V2 wrapper arguments to CloudEvent partials', () => { + const cloudFn = firestore.onDocumentWritten('foo/bar/baz', handler); + const cloudFnWrap = wrapV2(cloudFn); + const before = makeDocumentSnapshot( + { snapshot: 'before' }, + 'foo/bar/baz' + ); + const after = makeDocumentSnapshot({ snapshot: 'after' }, 'foo/bar/baz'); + const change = makeChange(before, after); + + cloudFnWrap({ data: change }); + // @ts-expect-error V2 wrappers accept CloudEvent partials, not raw event data. + cloudFnWrap(change); + }); + describe('alerts', () => { describe('alerts.onAlertPublished()', () => { it('should update CloudEvent appropriately', () => { diff --git a/src/v2.ts b/src/v2.ts index e3fef18..9fdc883 100644 --- a/src/v2.ts +++ b/src/v2.ts @@ -27,11 +27,17 @@ import { generateCombinedCloudEvent } from './cloudevent/generate'; import { DeepPartial } from './cloudevent/types'; import * as express from 'express'; +type WrappedV2CloudEventPartial> = DeepPartial< + Omit +> & { + data?: T['data'] | object; +}; + /** A function that can be called with test data and optional override values for {@link CloudEvent} * It will subsequently invoke the cloud function it wraps with the provided {@link CloudEvent} */ export type WrappedV2Function> = ( - cloudEventPartial?: DeepPartial + cloudEventPartial?: WrappedV2CloudEventPartial ) => any | Promise; export type WrappedV2CallableFunction = (