mirror of
https://github.com/usebruno/bruno.git
synced 2026-06-25 05:35:41 +00:00
Merge pull request #4918 from lohxt1/bru_send_request_fixes
bru.sendRequest translation fixes
This commit is contained in:
@@ -157,7 +157,7 @@ const transformBody = (j, requestOptions) => {
|
||||
* @returns {Object} - Transformed callback function
|
||||
*/
|
||||
const transformCallback = (j, callback) => {
|
||||
if (!callback || callback.type !== 'FunctionExpression') return null;
|
||||
if (!callback || (callback.type !== 'FunctionExpression' && callback.type !== 'ArrowFunctionExpression')) return null;
|
||||
|
||||
const params = callback.params;
|
||||
const callbackBody = callback.body;
|
||||
@@ -236,6 +236,9 @@ const sendRequestTransformer = (path, j) => {
|
||||
const requestOptions = args[0];
|
||||
const callback = args[1];
|
||||
|
||||
// Check if original call was awaited
|
||||
const wasAwaited = path.parent.parent.value.type === 'AwaitExpression';
|
||||
|
||||
// transform the request config options
|
||||
if (requestOptions.type === 'ObjectExpression') {
|
||||
// Transform headers
|
||||
@@ -249,26 +252,26 @@ const sendRequestTransformer = (path, j) => {
|
||||
const transformedCallback = transformCallback(j, callback);
|
||||
|
||||
// Add async keyword to the callback function
|
||||
if (transformedCallback && transformedCallback.type === 'FunctionExpression') {
|
||||
if (transformedCallback && (transformedCallback.type === 'FunctionExpression' || transformedCallback.type === 'ArrowFunctionExpression')) {
|
||||
transformedCallback.async = true;
|
||||
}
|
||||
|
||||
// Create expression: await bru.sendRequest(requestConfig, callback);
|
||||
return j.awaitExpression(
|
||||
j.callExpression(
|
||||
j.identifier('bru.sendRequest'),
|
||||
transformedCallback ? [requestOptions, transformedCallback] : [requestOptions]
|
||||
)
|
||||
const sendRequestCall = j.callExpression(
|
||||
j.identifier('bru.sendRequest'),
|
||||
transformedCallback ? [requestOptions, transformedCallback] : [requestOptions]
|
||||
);
|
||||
|
||||
return wasAwaited ? sendRequestCall : j.awaitExpression(sendRequestCall);
|
||||
}
|
||||
|
||||
// If there's no callback, just transform to await bru.sendRequest
|
||||
return j.awaitExpression(
|
||||
j.callExpression(
|
||||
j.identifier('bru.sendRequest'),
|
||||
[requestOptions]
|
||||
)
|
||||
const sendRequestCall = j.callExpression(
|
||||
j.identifier('bru.sendRequest'),
|
||||
[requestOptions]
|
||||
);
|
||||
|
||||
return wasAwaited ? sendRequestCall : j.awaitExpression(sendRequestCall);
|
||||
};
|
||||
|
||||
export default sendRequestTransformer;
|
||||
@@ -589,4 +589,100 @@ describe('Send Request Translation', () => {
|
||||
expect(translatedCode).toContain('const text = response.data');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Async/Await', () => {
|
||||
it('Should not add await if already present', () => {
|
||||
const code = `
|
||||
try {
|
||||
const response = await pm.sendRequest({
|
||||
url: "https://echo.usebruno.com",
|
||||
method: "GET"
|
||||
});
|
||||
|
||||
console.log(response.json());
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
`;
|
||||
const translatedCode = translateCode(code);
|
||||
expect(translatedCode).toBe(`
|
||||
try {
|
||||
const response = await bru.sendRequest({
|
||||
url: "https://echo.usebruno.com",
|
||||
method: "GET"
|
||||
});
|
||||
|
||||
console.log(response.json());
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('Should handle arrow function callbacks', () => {
|
||||
const code = `
|
||||
try {
|
||||
pm.sendRequest({
|
||||
url: "https://echo.usebruno.com",
|
||||
method: "GET"
|
||||
}, (error, response) => {
|
||||
console.log(response.json());
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
`;
|
||||
const translatedCode = translateCode(code);
|
||||
expect(translatedCode).toBe(`
|
||||
try {
|
||||
await bru.sendRequest({
|
||||
url: "https://echo.usebruno.com",
|
||||
method: "GET"
|
||||
}, async function(error, response) {
|
||||
console.log(response.data);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
`);
|
||||
});
|
||||
|
||||
it('Should handle async arrow function callbacks', () => {
|
||||
const code = `
|
||||
try {
|
||||
pm.sendRequest({
|
||||
url: "https://echo.usebruno.com",
|
||||
method: "GET"
|
||||
}, async (error, response) => {
|
||||
await new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, 1000)
|
||||
});
|
||||
console.log(response.json());
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
`;
|
||||
const translatedCode = translateCode(code);
|
||||
expect(translatedCode).toBe(`
|
||||
try {
|
||||
await bru.sendRequest({
|
||||
url: "https://echo.usebruno.com",
|
||||
method: "GET"
|
||||
}, async function(error, response) {
|
||||
await new Promise(resolve => {
|
||||
setTimeout(() => {
|
||||
resolve();
|
||||
}, 1000)
|
||||
});
|
||||
console.log(response.data);
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
`);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user