mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-24 19:33:02 +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}
|
||||
*/
|
||||
const http = (apiImpl) => {
|
||||
return (req, res, next) => {
|
||||
return async (req, res, next) => {
|
||||
debug(`External API request to ${req.url}`);
|
||||
let apiKey = null;
|
||||
let integration = null;
|
||||
@ -60,69 +60,67 @@ const http = (apiImpl) => {
|
||||
data: apiImpl.data
|
||||
});
|
||||
|
||||
apiImpl(frame)
|
||||
.then((result) => {
|
||||
debug(`External API request to ${frame.docName}.${frame.method}`);
|
||||
return 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)
|
||||
if (typeof result === 'function') {
|
||||
debug('ctrl function call');
|
||||
return result(req, res, next);
|
||||
try {
|
||||
const result = await apiImpl(frame);
|
||||
|
||||
debug(`External API request to ${frame.docName}.${frame.method}`);
|
||||
const headers = await shared.headers.get(result, apiImpl.headers, frame);
|
||||
|
||||
// CASE: api ctrl wants to handle the express response (e.g. streams)
|
||||
if (typeof result === 'function') {
|
||||
debug('ctrl function call');
|
||||
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;
|
||||
if (typeof apiImpl.statusCode === 'function') {
|
||||
statusCode = apiImpl.statusCode(result);
|
||||
} else if (apiImpl.statusCode) {
|
||||
statusCode = apiImpl.statusCode;
|
||||
}
|
||||
debug('json response');
|
||||
res.json(result || {});
|
||||
};
|
||||
|
||||
res.status(statusCode);
|
||||
let responseFormat;
|
||||
|
||||
// CASE: generate headers based on the api ctrl configuration
|
||||
res.set(headers);
|
||||
if (apiImpl.response){
|
||||
if (typeof apiImpl.response.format === 'function') {
|
||||
const apiResponseFormat = apiImpl.response.format();
|
||||
|
||||
const send = (format) => {
|
||||
if (format === 'plain') {
|
||||
debug('plain text response');
|
||||
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;
|
||||
}
|
||||
if (apiResponseFormat.then) { // is promise
|
||||
return apiResponseFormat.then((formatName) => {
|
||||
send(formatName);
|
||||
});
|
||||
} else {
|
||||
responseFormat = apiImpl.response.format;
|
||||
responseFormat = apiResponseFormat;
|
||||
}
|
||||
} else {
|
||||
responseFormat = apiImpl.response.format;
|
||||
}
|
||||
}
|
||||
|
||||
send(responseFormat);
|
||||
})
|
||||
.catch((err) => {
|
||||
req.frameOptions = {
|
||||
docName: frame.docName,
|
||||
method: frame.method
|
||||
};
|
||||
send(responseFormat);
|
||||
} catch (err) {
|
||||
req.frameOptions = {
|
||||
docName: frame.docName,
|
||||
method: frame.method
|
||||
};
|
||||
|
||||
next(err);
|
||||
});
|
||||
next(err);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user