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
kirrg001 0338ba56c0 Added removal of null values in v2
refs #9866

- also moved id mismatch to global validator
- this is not specific to posts
2018-10-12 21:46:16 +02:00

95 lines
2.8 KiB
JavaScript

const debug = require('ghost-ignition').debug('api:shared:serializers:handle');
const Promise = require('bluebird');
const sequence = require('../../../lib/promise/sequence');
const common = require('../../../lib/common');
/**
* The shared serialization handler runs the request through all the serialization steps.
*
* 1. shared serialization
* 2. api serialization
*/
module.exports.input = (apiConfig, apiSerializers, frame) => {
debug('input');
const tasks = [];
const sharedSerializers = require('./input');
if (!apiSerializers) {
return Promise.reject(new common.errors.IncorrectUsageError());
}
if (!apiConfig) {
return Promise.reject(new common.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);
};
module.exports.output = (response = {}, apiConfig, apiSerializers, options) => {
debug('output');
const tasks = [];
if (!apiConfig) {
return Promise.reject(new common.errors.IncorrectUsageError());
}
if (!apiSerializers) {
return Promise.reject(new common.errors.IncorrectUsageError());
}
// ##### API VERSION RESOURCE SERIALIZATION
if (apiSerializers[apiConfig.docName]) {
if (apiSerializers[apiConfig.docName].all) {
tasks.push(function serializeOptionsShared() {
return apiSerializers[apiConfig.docName].all(response, apiConfig, options);
});
}
if (apiSerializers[apiConfig.docName][apiConfig.method]) {
tasks.push(function serializeOptionsShared() {
return apiSerializers[apiConfig.docName][apiConfig.method](response, apiConfig, options);
});
}
}
debug(tasks);
return sequence(tasks);
};