2021-06-15 19:01:22 +03:00
|
|
|
const debug = require('@tryghost/debug')('api:shared:serializers:handle');
|
2018-10-05 01:50:45 +03:00
|
|
|
const Promise = require('bluebird');
|
2020-08-11 20:44:21 +03:00
|
|
|
const {sequence} = require('@tryghost/promise');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2018-10-05 01:50:45 +03:00
|
|
|
|
|
|
|
/**
|
2019-05-06 15:24:12 +03:00
|
|
|
* @description Shared input serialization handler.
|
2018-10-05 01:50:45 +03:00
|
|
|
*
|
2019-05-06 15:24:12 +03:00
|
|
|
* 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
|
2018-10-05 01:50:45 +03:00
|
|
|
*/
|
|
|
|
module.exports.input = (apiConfig, apiSerializers, frame) => {
|
|
|
|
debug('input');
|
|
|
|
|
|
|
|
const tasks = [];
|
|
|
|
const sharedSerializers = require('./input');
|
|
|
|
|
|
|
|
if (!apiSerializers) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError());
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiConfig) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError());
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ##### SHARED ALL SERIALIZATION
|
|
|
|
|
|
|
|
tasks.push(function serializeAllShared() {
|
2018-10-12 22:46:16 +03:00
|
|
|
return sharedSerializers.all.all(apiConfig, frame);
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
|
2018-10-12 22:46:16 +03:00
|
|
|
if (sharedSerializers.all[apiConfig.method]) {
|
|
|
|
tasks.push(function serializeAllShared() {
|
|
|
|
return sharedSerializers.all[apiConfig.method](apiConfig, frame);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
// ##### 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);
|
|
|
|
};
|
|
|
|
|
2019-05-06 15:24:12 +03:00
|
|
|
/**
|
|
|
|
* @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) => {
|
2018-10-05 01:50:45 +03:00
|
|
|
debug('output');
|
|
|
|
|
|
|
|
const tasks = [];
|
|
|
|
|
|
|
|
if (!apiConfig) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError());
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!apiSerializers) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.IncorrectUsageError());
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// ##### API VERSION RESOURCE SERIALIZATION
|
|
|
|
|
2018-12-17 19:45:07 +03:00
|
|
|
if (apiSerializers.all && apiSerializers.all.before) {
|
|
|
|
tasks.push(function allSerializeBefore() {
|
2019-05-06 15:24:12 +03:00
|
|
|
return apiSerializers.all.before(response, apiConfig, frame);
|
2018-12-17 19:45:07 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
if (apiSerializers[apiConfig.docName]) {
|
|
|
|
if (apiSerializers[apiConfig.docName].all) {
|
|
|
|
tasks.push(function serializeOptionsShared() {
|
2019-05-06 15:24:12 +03:00
|
|
|
return apiSerializers[apiConfig.docName].all(response, apiConfig, frame);
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiSerializers[apiConfig.docName][apiConfig.method]) {
|
|
|
|
tasks.push(function serializeOptionsShared() {
|
2019-05-06 15:24:12 +03:00
|
|
|
return apiSerializers[apiConfig.docName][apiConfig.method](response, apiConfig, frame);
|
2018-10-05 01:50:45 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-17 19:45:07 +03:00
|
|
|
if (apiSerializers.all && apiSerializers.all.after) {
|
|
|
|
tasks.push(function allSerializeAfter() {
|
2019-05-06 15:24:12 +03:00
|
|
|
return apiSerializers.all.after(apiConfig, frame);
|
2018-12-17 19:45:07 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
debug(tasks);
|
|
|
|
return sequence(tasks);
|
|
|
|
};
|