mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-25 11:55:03 +03:00
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:
parent
3d40952632
commit
cf8be34c43
@ -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,69 +60,67 @@ 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}`);
|
|
||||||
return shared.headers.get(result, apiImpl.headers, frame)
|
debug(`External API request to ${frame.docName}.${frame.method}`);
|
||||||
.then(headers => ({result, headers}));
|
const headers = await shared.headers.get(result, apiImpl.headers, frame);
|
||||||
})
|
|
||||||
.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');
|
return result(req, res, next);
|
||||||
return result(req, res, next);
|
}
|
||||||
|
|
||||||
|
let statusCode = 200;
|
||||||
|
if (typeof apiImpl.statusCode === 'function') {
|
||||||
|
statusCode = apiImpl.statusCode(result);
|
||||||
|
} else if (apiImpl.statusCode) {
|
||||||
|
statusCode = apiImpl.statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.status(statusCode);
|
||||||
|
|
||||||
|
// CASE: generate headers based on the api ctrl configuration
|
||||||
|
res.set(headers);
|
||||||
|
|
||||||
|
const send = (format) => {
|
||||||
|
if (format === 'plain') {
|
||||||
|
debug('plain text response');
|
||||||
|
return res.send(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
let statusCode = 200;
|
debug('json response');
|
||||||
if (typeof apiImpl.statusCode === 'function') {
|
res.json(result || {});
|
||||||
statusCode = apiImpl.statusCode(result);
|
};
|
||||||
} else if (apiImpl.statusCode) {
|
|
||||||
statusCode = apiImpl.statusCode;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.status(statusCode);
|
let responseFormat;
|
||||||
|
|
||||||
// CASE: generate headers based on the api ctrl configuration
|
if (apiImpl.response){
|
||||||
res.set(headers);
|
if (typeof apiImpl.response.format === 'function') {
|
||||||
|
const apiResponseFormat = apiImpl.response.format();
|
||||||
|
|
||||||
const send = (format) => {
|
if (apiResponseFormat.then) { // is promise
|
||||||
if (format === 'plain') {
|
return apiResponseFormat.then((formatName) => {
|
||||||
debug('plain text response');
|
send(formatName);
|
||||||
return res.send(result);
|
});
|
||||||
}
|
|
||||||
|
|
||||||
debug('json response');
|
|
||||||
res.json(result || {});
|
|
||||||
};
|
|
||||||
|
|
||||||
let responseFormat;
|
|
||||||
|
|
||||||
if (apiImpl.response){
|
|
||||||
if (typeof apiImpl.response.format === 'function') {
|
|
||||||
const apiResponseFormat = apiImpl.response.format();
|
|
||||||
|
|
||||||
if (apiResponseFormat.then) { // is promise
|
|
||||||
return apiResponseFormat.then((formatName) => {
|
|
||||||
send(formatName);
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
responseFormat = apiResponseFormat;
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
responseFormat = apiImpl.response.format;
|
responseFormat = apiResponseFormat;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
responseFormat = apiImpl.response.format;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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);
|
||||||
});
|
}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user