Skip to content
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

Merged
merged 2 commits into from
Nov 4, 2021

Conversation

rhodgkins
Copy link
Contributor

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 with users/FOO would result in params: { 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 provided params.

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

Comment on lines +233 to +264
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);
Copy link
Contributor Author

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!

@rhodgkins
Copy link
Contributor Author

Can anyone review and, if happy, merge this in please?

@rhodgkins
Copy link
Contributor Author

Bump @inlined & @taeold

Copy link
Member

@inlined inlined left a 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.

expect(context.auth).to.equal(null);
expect(context.authType).to.equal('UNAUTHENTICATED');
});
context('database functions', () => {
Copy link
Member

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.

Copy link
Contributor Author

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).

@rhodgkins rhodgkins force-pushed the params branch 2 times, most recently from a01ce37 to 78a2bc8 Compare November 2, 2021 11:00
@rhodgkins
Copy link
Contributor Author

@inlined done and rebased from master and also ran format:fix as I think it was missed from #123

@inlined inlined merged commit c1dd82b into firebase:master Nov 4, 2021
@rhodgkins rhodgkins deleted the params branch March 10, 2022 09:11
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