diff --git a/packages/bruno-converters/src/utils/send-request-transformer.js b/packages/bruno-converters/src/utils/send-request-transformer.js index 3656c4a2c..1029b1943 100644 --- a/packages/bruno-converters/src/utils/send-request-transformer.js +++ b/packages/bruno-converters/src/utils/send-request-transformer.js @@ -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; \ No newline at end of file diff --git a/packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/transformers/send-request.test.js b/packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/transformers/send-request.test.js index f0c12bf3e..d6e9e62a7 100644 --- a/packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/transformers/send-request.test.js +++ b/packages/bruno-converters/tests/postman/postman-translations/transpiler-tests/transformers/send-request.test.js @@ -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); + } + `); + }); + }); }); \ No newline at end of file