0
Fork 0
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:
Daniel Lockyer 2022-08-12 08:46:32 +02:00
parent 8247242610
commit 8574bcd30a
4 changed files with 8 additions and 12 deletions

View file

@ -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://';

View file

@ -30,7 +30,7 @@ const http = (apiImpl) => {
};
}
if (req.user && req.user.id) {
if (req.user?.id) {
user = req.user.id;
}

View file

@ -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);
});

View file

@ -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) {