-
Notifications
You must be signed in to change notification settings - Fork 58
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
Try to extract context.params from triggered data #114
Conversation
if (eventType.startsWith('google.firebase.database.ref.')) { | ||
let data: database.DataSnapshot; | ||
if (eventType.endsWith('.write')) { | ||
// Triggered with change | ||
if (!(triggerData instanceof Change)) { | ||
throw new Error('Must be triggered by database change'); | ||
} | ||
data = triggerData.before; | ||
} else { | ||
data = triggerData as any; | ||
} | ||
triggerParams = _extractDatabaseParams(eventResource, data); | ||
} else if (eventType.startsWith('google.firestore.document.')) { | ||
let data: firestore.DocumentSnapshot; | ||
if (eventType.endsWith('.write')) { | ||
// Triggered with change | ||
if (!(triggerData instanceof Change)) { | ||
throw new Error('Must be triggered by firestore document change'); | ||
} | ||
data = triggerData.before; | ||
} else { | ||
data = triggerData as any; | ||
} | ||
triggerParams = _extractFirestoreDocumentParams(eventResource, data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like these bits but not really sure there's a better way?
I was thinking to check data instanceof database.DataSnapshot
(or firestore.DocumentSnapshot
) and throwing an Error
but then if you call the wrapped function with an actual fetched database.DataSnapshot
(using the admin SDK) it would fail that check which would be annoying!
Can anyone review and, if happy, merge this in please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me. Fix the nits/rebase and I'll merge.
spec/main.spec.ts
Outdated
expect(context.auth).to.equal(null); | ||
expect(context.authType).to.equal('UNAUTHENTICATED'); | ||
}); | ||
context('database functions', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be a refactor without a benefit, unless I"m missing something. We also just use describe
in our codebases instead of context
. We also generally use beforeEach
over before
because the nanosecond we save by not calling a setup function repeatedly is less important than hairpulling if a test changes some of the state between runs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool, will update context
/ before
usage 👍 (and context
usage below)
The refactor is so change
can be defined for the RTDB function tests, the current tests just used some junk data ('data'
) so wasn't actually a Change
object with a before
and after
. Because we're now using the incoming paths to populate the params
the input in to the RTDB functions is now validated and has to be a Change
or a DataSnapshot
(depending on the function type).
a01ce37
to
78a2bc8
Compare
@inlined done and rebased from |
Description
When running the function, the wrapper tries to extract the
EventContext
params
from the triggered data based on the wildcard path.E.g. A wildcard path of
users/{userId}
run against withusers/FOO
would result inparams: { userId: 'FOO' }
.Provided
params
to the wrapped function are preferred over the extracted ones - I've only done this to prevent breaking usage for anyone whose not matching the values in the path up to the providedparams
.Also added tests (and updated the
auth and authType
tests as they were just being run with junk data).Code sample
See the tests:
Firebase RTDB - https://github.com/bookcreator/firebase-functions-test/blob/params/spec/main.spec.ts#L150-L165
Firestore - https://github.com/bookcreator/firebase-functions-test/blob/params/spec/main.spec.ts#L167-L185