mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-03-11 02:12:21 -05:00
Refactored api-framework to use optional chaining
- this makes the code more readable and succinct
This commit is contained in:
parent
8247242610
commit
8574bcd30a
4 changed files with 8 additions and 12 deletions
|
@ -122,12 +122,8 @@ module.exports = {
|
|||
}
|
||||
}
|
||||
|
||||
const locationHeaderDisabled = apiConfigHeaders && apiConfigHeaders.location === false;
|
||||
const hasFrameData = frame
|
||||
&& (frame.method === 'add')
|
||||
&& result[frame.docName]
|
||||
&& result[frame.docName][0]
|
||||
&& result[frame.docName][0].id;
|
||||
const locationHeaderDisabled = apiConfigHeaders?.location === false;
|
||||
const hasFrameData = frame?.method === 'add' && result[frame.docName]?.[0]?.id;
|
||||
|
||||
if (!locationHeaderDisabled && hasFrameData) {
|
||||
const protocol = (frame.original.url.secure === false) ? 'http://' : 'https://';
|
||||
|
|
|
@ -30,7 +30,7 @@ const http = (apiImpl) => {
|
|||
};
|
||||
}
|
||||
|
||||
if (req.user && req.user.id) {
|
||||
if (req.user?.id) {
|
||||
user = req.user.id;
|
||||
}
|
||||
|
||||
|
|
|
@ -68,10 +68,10 @@ module.exports.input = (apiConfig, apiSerializers, frame) => {
|
|||
};
|
||||
|
||||
const getBestMatchSerializer = function (apiSerializers, docName, method) {
|
||||
if (apiSerializers[docName] && apiSerializers[docName][method]) {
|
||||
if (apiSerializers[docName]?.[method]) {
|
||||
debug(`Calling ${docName}.${method}`);
|
||||
return apiSerializers[docName][method].bind(apiSerializers[docName]);
|
||||
} else if (apiSerializers[docName] && apiSerializers[docName].all) {
|
||||
} else if (apiSerializers[docName]?.all) {
|
||||
debug(`Calling ${docName}.all`);
|
||||
return apiSerializers[docName].all.bind(apiSerializers[docName]);
|
||||
}
|
||||
|
@ -108,7 +108,7 @@ module.exports.output = (response = {}, apiConfig, apiSerializers, frame) => {
|
|||
|
||||
// ##### API VERSION RESOURCE SERIALIZATION
|
||||
|
||||
if (apiSerializers.all && apiSerializers.all.before) {
|
||||
if (apiSerializers.all?.before) {
|
||||
tasks.push(function allSerializeBefore() {
|
||||
return apiSerializers.all.before(response, apiConfig, frame);
|
||||
});
|
||||
|
@ -129,7 +129,7 @@ module.exports.output = (response = {}, apiConfig, apiSerializers, frame) => {
|
|||
});
|
||||
}
|
||||
|
||||
if (apiSerializers.all && apiSerializers.all.after) {
|
||||
if (apiSerializers.all?.after) {
|
||||
tasks.push(function allSerializeAfter() {
|
||||
return apiSerializers.all.after(apiConfig, frame);
|
||||
});
|
||||
|
|
|
@ -53,7 +53,7 @@ const validate = (config, attrs) => {
|
|||
errors = errors.concat(validator.validate(value, key, GLOBAL_VALIDATORS[key]));
|
||||
}
|
||||
|
||||
if (config && config[key]) {
|
||||
if (config?.[key]) {
|
||||
const allowedValues = Array.isArray(config[key]) ? config[key] : config[key].values;
|
||||
|
||||
if (allowedValues) {
|
||||
|
|
Loading…
Add table
Reference in a new issue