Changed frames http module to use async/await

no issue

- It was hard to plug in with additional code into current `.then` based chain. Refactoring to use a more modern syntax helps with readability and allows for easier edits
This commit is contained in:
Naz 2022-04-06 14:48:30 +08:00
parent 3d40952632
commit cf8be34c43

View File

@ -13,7 +13,7 @@ const models = require('../../models');
* @return {Function} * @return {Function}
*/ */
const http = (apiImpl) => { const http = (apiImpl) => {
return (req, res, next) => { return async (req, res, next) => {
debug(`External API request to ${req.url}`); debug(`External API request to ${req.url}`);
let apiKey = null; let apiKey = null;
let integration = null; let integration = null;
@ -60,13 +60,12 @@ const http = (apiImpl) => {
data: apiImpl.data data: apiImpl.data
}); });
apiImpl(frame) try {
.then((result) => { const result = await apiImpl(frame);
debug(`External API request to ${frame.docName}.${frame.method}`); debug(`External API request to ${frame.docName}.${frame.method}`);
return shared.headers.get(result, apiImpl.headers, frame) const headers = await shared.headers.get(result, apiImpl.headers, frame);
.then(headers => ({result, headers}));
})
.then(({result, headers}) => {
// CASE: api ctrl wants to handle the express response (e.g. streams) // CASE: api ctrl wants to handle the express response (e.g. streams)
if (typeof result === 'function') { if (typeof result === 'function') {
debug('ctrl function call'); debug('ctrl function call');
@ -114,15 +113,14 @@ const http = (apiImpl) => {
} }
send(responseFormat); send(responseFormat);
}) } catch (err) {
.catch((err) => {
req.frameOptions = { req.frameOptions = {
docName: frame.docName, docName: frame.docName,
method: frame.method method: frame.method
}; };
next(err); next(err);
}); }
}; };
}; };