Skip to content

Commit 6e31411

Browse files
authored
Support Params as inputs in eventTrigger (#168)
### Description The api of the manifest changed in [email protected]. Some inputs have been updated to be T | Expression<T>. This PR addresses those issues ## Code sample * Updated a test to include StringParam * Updated a test to include TernaryExpression
1 parent b090e54 commit 6e31411

File tree

6 files changed

+63
-14
lines changed

6 files changed

+63
-14
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"@types/mocha": "^5.2.7",
4848
"chai": "^4.2.0",
4949
"firebase-admin": "^10.1.0",
50-
"firebase-functions": "^3.22.0",
50+
"firebase-functions": "^3.23.0",
5151
"firebase-tools": "^8.9.2",
5252
"mocha": "^6.2.2",
5353
"prettier": "^1.19.1",
@@ -57,7 +57,7 @@
5757
},
5858
"peerDependencies": {
5959
"firebase-admin": ">=6.0.0",
60-
"firebase-functions": ">=3.22.0",
60+
"firebase-functions": ">=3.23.0",
6161
"jest": ">=28.0.0"
6262
},
6363
"engines": {

spec/v2.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
eventarc,
3434
https,
3535
} from 'firebase-functions/v2';
36+
import { defineString } from 'firebase-functions/v2/params';
3637
import { makeDataSnapshot } from '../src/providers/database';
3738

3839
describe('v2', () => {
@@ -372,6 +373,39 @@ describe('v2', () => {
372373
expect(cloudEvent.ref).equal('foo/bar/baz');
373374
});
374375

376+
it('should resolve default ref given StringParam', () => {
377+
process.env.rtdb_ref = 'foo/StringParam/baz';
378+
const referenceOptions = {
379+
ref: '',
380+
instance: 'instance-1',
381+
};
382+
const cloudFn = database.onValueCreated(referenceOptions, handler);
383+
cloudFn.__endpoint.eventTrigger.eventFilterPathPatterns.ref = defineString(
384+
'rtdb_ref'
385+
);
386+
const cloudFnWrap = wrapV2(cloudFn);
387+
const cloudEvent = cloudFnWrap().cloudEvent;
388+
expect(cloudEvent.ref).equal('foo/StringParam/baz');
389+
});
390+
391+
it.skip('should resolve default ref given TernaryExpression', () => {
392+
const ref1 = defineString('rtdb_ref_1');
393+
process.env.rtdb_ref_1 = 'foo/StringParam/1';
394+
const ref2 = defineString('rtdb_ref_2');
395+
process.env.rtdb_ref_2 = 'foo/StringParam/2';
396+
const referenceOptions = {
397+
ref: '',
398+
instance: 'instance-1',
399+
};
400+
const cloudFn = database.onValueCreated(referenceOptions, handler);
401+
cloudFn.__endpoint.eventTrigger.eventFilterPathPatterns.ref = ref1
402+
.equals('aa')
403+
.then('rtdb_ref_1', 'rtdb_ref_2');
404+
const cloudFnWrap = wrapV2(cloudFn);
405+
const cloudEvent = cloudFnWrap().cloudEvent;
406+
expect(cloudEvent.ref).equal('rtdb_ref_2');
407+
});
408+
375409
it('should resolve using params', () => {
376410
const referenceOptions = {
377411
ref: 'users/{user}',

src/cloudevent/mocks/database/helpers.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { CloudFunction, database } from 'firebase-functions/v2';
2+
import { Expression } from 'firebase-functions/v2/params';
23
import { DeepPartial } from '../../types';
34
import {
45
exampleDataSnapshot,
56
exampleDataSnapshotChange,
67
} from '../../../providers/database';
7-
import { getBaseCloudEvent } from '../helpers';
8+
import { resolveStringExpression, getBaseCloudEvent } from '../helpers';
89
import { Change } from 'firebase-functions';
910
import { makeDataSnapshot } from '../../../providers/database';
1011

@@ -117,18 +118,20 @@ export function getCommonDatabaseFields(
117118
>
118119
>
119120
) {
120-
const instance =
121+
const instanceOrExpression =
121122
(cloudEventPartial?.instance as string) ||
122123
cloudFunction.__endpoint?.eventTrigger?.eventFilterPathPatterns?.instance ||
123124
cloudFunction.__endpoint?.eventTrigger?.eventFilters?.instance ||
124125
'instance-1';
126+
const instance = resolveStringExpression(instanceOrExpression);
125127
const firebaseDatabaseHost =
126128
(cloudEventPartial?.firebaseDatabaseHost as string) ||
127129
'firebaseDatabaseHost';
128-
const rawRef =
130+
const rawRefOrExpression =
129131
(cloudEventPartial?.ref as string) ||
130132
cloudFunction?.__endpoint?.eventTrigger?.eventFilterPathPatterns?.ref ||
131133
'/foo/bar';
134+
const rawRef = resolveStringExpression(rawRefOrExpression);
132135
const location = (cloudEventPartial?.location as string) || 'us-central1';
133136
const params: Record<string, string> = cloudEventPartial?.params || {};
134137
const ref = extractRef(rawRef, params);

src/cloudevent/mocks/helpers.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CloudEvent, CloudFunction } from 'firebase-functions/v2';
2+
import { Expression } from 'firebase-functions/v2/params';
23

34
export const APP_ID = '__APP_ID__';
45
export const PROJECT_ID = '42';
@@ -10,7 +11,7 @@ export function getEventType(cloudFunction: CloudFunction<any>): string {
1011

1112
export function getEventFilters(
1213
cloudFunction: CloudFunction<any>
13-
): Record<string, string> {
14+
): Record<string, string | Expression<string>> {
1415
return cloudFunction?.__endpoint?.eventTrigger?.eventFilters || {};
1516
}
1617

@@ -27,6 +28,15 @@ export function getBaseCloudEvent<EventType extends CloudEvent<unknown>>(
2728
} as EventType;
2829
}
2930

31+
export function resolveStringExpression(
32+
stringOrExpression: string | Expression<string>
33+
) {
34+
if (typeof stringOrExpression === 'string') {
35+
return stringOrExpression;
36+
}
37+
return stringOrExpression?.value();
38+
}
39+
3040
function makeEventId(): string {
3141
return (
3242
Math.random()

src/cloudevent/mocks/storage/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { CloudFunction, CloudEvent } from 'firebase-functions/v2';
33
import { StorageEvent } from 'firebase-functions/v2/storage';
44
import {
55
FILENAME,
6+
resolveStringExpression,
67
getBaseCloudEvent,
78
getEventFilters,
89
getEventType,
@@ -14,10 +15,11 @@ export const storageV1: MockCloudEventAbstractFactory<StorageEvent> = {
1415
cloudFunction: CloudFunction<StorageEvent>,
1516
cloudEventPartial?: DeepPartial<StorageEvent>
1617
): StorageEvent {
17-
const bucket =
18+
const bucketOrExpression =
1819
cloudEventPartial?.bucket ||
1920
getEventFilters(cloudFunction)?.bucket ||
2021
'bucket_name';
22+
const bucket = resolveStringExpression(bucketOrExpression);
2123
const source =
2224
cloudEventPartial?.source ||
2325
`//storage.googleapis.com/projects/_/buckets/${bucket}`;

0 commit comments

Comments
 (0)