0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-27 22:49:56 -05:00
ghost/core/server/api/shared/serializers/handle.js
Vikas Potluri 4ac88dce10
Refactored common lib import to use destructuring (#11835)
* refactored `core/frontend/apps` to destructure common imports
* refactored `core/frontend/services/{apps, redirects, routing}` to destructure common imports
* refactored `core/frontend/services/settings` to destructure common imports
* refactored remaining `core/frontend/services` to destructure common imports
* refactored `core/server/adapters` to destructure common imports
* refactored `core/server/data/{db, exporter, schema, validation}` to destructure common imports
* refactored `core/server/data/importer` to destructure common imports
* refactored `core/server/models/{base, plugins, relations}` to destructure common imports
* refactored remaining `core/server/models` to destructure common imports
* refactored `core/server/api/canary/utils/serializers/output` to destructure common imports
* refactored remaining `core/server/api/canary/utils` to destructure common imports
* refactored remaining `core/server/api/canary` to destructure common imports
* refactored `core/server/api/shared` to destructure common imports
* refactored `core/server/api/v2/utils` to destructure common imports
* refactored remaining `core/server/api/v2` to destructure common imports
* refactored `core/frontend/meta` to destructure common imports
* fixed some tests referencing `common.errors` instead of `@tryghost/errors`
   - Not all of them need to be updated; only updating the ones that are
causing failures
* fixed errors import being shadowed by local scope
2020-05-22 19:22:20 +01:00

126 lines
3.7 KiB
JavaScript

const debug = require('ghost-ignition').debug('api:shared:serializers:handle');
const Promise = require('bluebird');
const sequence = require('../../../lib/promise/sequence');
const errors = require('@tryghost/errors');
/**
* @description Shared input serialization handler.
*
* The shared input handler runs the request through all the validation steps.
*
* 1. Shared serialization
* 2. API serialization
*
* @param {Object} apiConfig - Docname + method of the ctrl
* @param {Object} apiSerializers - Target API serializers
* @param {Object} frame
*/
module.exports.input = (apiConfig, apiSerializers, frame) => {
debug('input');
const tasks = [];
const sharedSerializers = require('./input');
if (!apiSerializers) {
return Promise.reject(new errors.IncorrectUsageError());
}
if (!apiConfig) {
return Promise.reject(new errors.IncorrectUsageError());
}
// ##### SHARED ALL SERIALIZATION
tasks.push(function serializeAllShared() {
return sharedSerializers.all.all(apiConfig, frame);
});
if (sharedSerializers.all[apiConfig.method]) {
tasks.push(function serializeAllShared() {
return sharedSerializers.all[apiConfig.method](apiConfig, frame);
});
}
// ##### API VERSION RESOURCE SERIALIZATION
if (apiSerializers.all) {
tasks.push(function serializeOptionsShared() {
return apiSerializers.all(apiConfig, frame);
});
}
if (apiSerializers[apiConfig.docName]) {
if (apiSerializers[apiConfig.docName].all) {
tasks.push(function serializeOptionsShared() {
return apiSerializers[apiConfig.docName].all(apiConfig, frame);
});
}
if (apiSerializers[apiConfig.docName][apiConfig.method]) {
tasks.push(function serializeOptionsShared() {
return apiSerializers[apiConfig.docName][apiConfig.method](apiConfig, frame);
});
}
}
debug(tasks);
return sequence(tasks);
};
/**
* @description Shared output serialization handler.
*
* The shared output handler runs the request through all the validation steps.
*
* 1. Shared serialization
* 2. API serialization
*
* @param {Object} response - API response
* @param {Object} apiConfig - Docname + method of the ctrl
* @param {Object} apiSerializers - Target API serializers
* @param {Object} frame
*/
module.exports.output = (response = {}, apiConfig, apiSerializers, frame) => {
debug('output');
const tasks = [];
if (!apiConfig) {
return Promise.reject(new errors.IncorrectUsageError());
}
if (!apiSerializers) {
return Promise.reject(new errors.IncorrectUsageError());
}
// ##### API VERSION RESOURCE SERIALIZATION
if (apiSerializers.all && apiSerializers.all.before) {
tasks.push(function allSerializeBefore() {
return apiSerializers.all.before(response, apiConfig, frame);
});
}
if (apiSerializers[apiConfig.docName]) {
if (apiSerializers[apiConfig.docName].all) {
tasks.push(function serializeOptionsShared() {
return apiSerializers[apiConfig.docName].all(response, apiConfig, frame);
});
}
if (apiSerializers[apiConfig.docName][apiConfig.method]) {
tasks.push(function serializeOptionsShared() {
return apiSerializers[apiConfig.docName][apiConfig.method](response, apiConfig, frame);
});
}
}
if (apiSerializers.all && apiSerializers.all.after) {
tasks.push(function allSerializeAfter() {
return apiSerializers.all.after(apiConfig, frame);
});
}
debug(tasks);
return sequence(tasks);
};