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: support for sendRequest functionality #3845

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
59 changes: 59 additions & 0 deletions packages/bruno-js/src/bru.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { cloneDeep } = require('lodash');
const { interpolate } = require('@usebruno/common');
const axios = require('axios');

const variableNameRegex = /^[\w-.]*$/;

Expand All @@ -24,6 +25,7 @@ class Bru {
this.nextRequest = nextRequest;
}
};
this.axios = axios.create();
}

_interpolate = (str) => {
Expand Down Expand Up @@ -153,6 +155,63 @@ class Bru {
sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}

sendRequest(requestConfig, callback) {
Copy link

@thiagomini thiagomini Jan 20, 2025

Choose a reason for hiding this comment

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

Would it be possible to send the request and pass a specific variable to be used in either the pre or post scripts?

For instance, I'd like to call bru.sendRequest('./items/delete-item', { itemId: myCustomItemId }). This would allow developers to customize further their scripts by replacing "request-scoped" variables

Copy link
Contributor

@helloanoop helloanoop Jan 21, 2025

Choose a reason for hiding this comment

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

@thiagomini Bruno provides an API, bruno.runRequest('items/delete-item'), to trigger another request already defined in Bruno.

The sendRequest() API, on the other hand, is used for making direct HTTP requests.

It seems logical to support passing variables when using sendRequest(). Could you please create a new GitHub issue outlining this requirement?

Choose a reason for hiding this comment

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

Oh, I see @helloanoop , thanks for the info! So, my idea is actually related to passing variables using the bruno.runRequest, do we have that? If not, I'll create an issue asking for that feature

Copy link
Contributor

Choose a reason for hiding this comment

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

@thiagomini Currently bruno.runRequest() does not support passing variables. Please create an issue for the same

if (typeof callback === 'function') {
this._sendRequestWithCallback(requestConfig, callback);
return;
}

return this._sendRequestWithPromise(requestConfig);
}

async _sendRequestWithPromise(requestConfig) {
try {
const config = typeof requestConfig === 'string'
? { url: requestConfig, method: 'GET' }
: { ...requestConfig };

if (config.body) {
config.data = config.body;
delete config.body;
}

config.url = this._interpolate(config.url);

if (config.data) {
if (typeof config.data === 'string') {
config.data = this._interpolate(config.data);
} else if (typeof config.data === 'object') {
config.data = JSON.parse(this._interpolate(JSON.stringify(config.data)));
}
}

const response = await this.axios(config);

return {
code: response.status,
status: response.statusText,
headers: response.headers,
body: response.data
};
} catch (error) {
if (error.response) {
return {
code: error.response.status,
status: error.response.statusText,
headers: error.response.headers,
body: error.response.data
};
}
throw error;
}
}

_sendRequestWithCallback(requestConfig, callback) {
this._sendRequestWithPromise(requestConfig)
.then(response => callback(null, response))
.catch(error => callback(error, null));
}
}

module.exports = Bru;
Loading