Skip to content

Commit 3c36cb9

Browse files
jsprilaurenzlong
authored andcommitted
Fall back to event.params if event.resource is undefined. (#92)
1 parent 89a12b5 commit 3c36cb9

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

spec/cloud-functions.spec.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ describe('makeParams', () => {
7979
handler: () => null,
8080
};
8181

82-
const testEvent: Event<string> = {
83-
resource: 'projects/_/instances/pid/ref/a/nested/b',
84-
data: 'data',
85-
};
86-
8782
it('should construct params from the event resource', () => {
8883
let args: any = _.assign({}, cloudFunctionArgs, {handler: (e) => e});
8984
let cf = makeCloudFunction(args);
9085

86+
const testEvent: Event<string> = {
87+
resource: 'projects/_/instances/pid/ref/a/nested/b',
88+
data: 'data',
89+
};
90+
9191
return expect(cf(testEvent)).to.eventually.deep.equal({
9292
resource: 'projects/_/instances/pid/ref/a/nested/b',
9393
data: 'data',
@@ -97,4 +97,25 @@ describe('makeParams', () => {
9797
},
9898
});
9999
});
100+
101+
it('should construct params from the event params', () => {
102+
let args: any = _.assign({}, cloudFunctionArgs, {handler: (e) => e});
103+
let cf = makeCloudFunction(args);
104+
105+
const testEvent: Event<string> = {
106+
data: 'data',
107+
params: {
108+
foo: 'a',
109+
bar: 'b',
110+
},
111+
};
112+
113+
return expect(cf(testEvent)).to.eventually.deep.equal({
114+
data: 'data',
115+
params: {
116+
foo: 'a',
117+
bar: 'b',
118+
},
119+
});
120+
});
100121
});

src/cloud-functions.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ export interface MakeCloudFunctionArgs<EventData> {
7373
after?: (raw: Event<any>) => void;
7474
}
7575

76-
function _makeParams (event: Event<any>, triggerResource: string): { [option: string]: any } {
76+
function _makeParams(event: Event<any>, triggerResource: string): { [option: string]: any } {
77+
if (!event.resource) { // In unit testing, "resource" may not be populated for a test event.
78+
return event.params || {};
79+
}
80+
7781
let wildcards = triggerResource.match(WILDCARD_REGEX);
7882
let params = {};
7983
if (wildcards) {
@@ -86,6 +90,7 @@ function _makeParams (event: Event<any>, triggerResource: string): { [option: st
8690
params[wildcardNoBraces] = eventResourceParts[position];
8791
});
8892
}
93+
8994
return params;
9095
};
9196

0 commit comments

Comments
 (0)