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

feat: make BrunoResponse callable to access body data using expressions fixes (#481) #3710

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Pragadesh-45
Copy link
Contributor

fixes: #481

Description

This PR introduces the ability to use the res object as a callable function to access body data directly. For example:

const page = res('page');

This functionality simplifies post-response scripting. Currently, developers has a workaround:

const { get } = require('lodash');
const page = get(res.getBody(), 'page');
console.log('page', page);

With this PR, the res object can now be used both as an object and as a function, providing a more convenient way to scripts.

Contribution Checklist:

  • The pull request only addresses one issue or adds one feature.
  • The pull request does not introduce any breaking changes
  • I have added screenshots or gifs to help explain the change if applicable.
  • I have read the contribution guidelines.
  • Create an issue and link to the pull request.

@@ -36,6 +44,10 @@ class BrunoResponse {
this.body = data;
this.res.data = data;
}

call(expr, ...fns) {
return get(this.res.data, expr, ...fns);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't satisfy the full feature. lodash.get does not support array filtering.

@Kenttleton
Copy link

Check out the examples given in the docs. The solution should satisfy the example inputs and outputs as written.

@Pragadesh-45
Copy link
Contributor Author

@Kenttleton Thanks for your valuable input. I will update this PR

@Kenttleton
Copy link

I recommend using the JSONPath library instead of lodash as it gets you most of the way there. This lib is also used in Bruno to filter responses and has a similar syntax as the docs. Don't forget to add an optional callback to the signature for further refinements and please provide test cases for your implementation.

@Pragadesh-45 Pragadesh-45 marked this pull request as draft January 2, 2025 06:50
@pull-request-size pull-request-size bot added size/XS and removed size/S labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

script PostResponse: res is not a function
3 participants