2022-08-11 17:42:21 +03:00
|
|
|
const debug = require('@tryghost/debug')('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');
|
|
|
|
|
2022-03-09 17:22:59 +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
|
|
|
}
|
|
|
|
|
2022-03-09 17:22:59 +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
|
|
|
}
|
|
|
|
|
|
|
|
// ##### 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);
|
|
|
|
};
|
|
|
|
|
2022-03-27 14:44:55 +03:00
|
|
|
const getBestMatchSerializer = function (apiSerializers, docName, method) {
|
|
|
|
if (apiSerializers[docName] && apiSerializers[docName][method]) {
|
|
|
|
debug(`Calling ${docName}.${method}`);
|
|
|
|
return apiSerializers[docName][method].bind(apiSerializers[docName]);
|
|
|
|
} else if (apiSerializers[docName] && apiSerializers[docName].all) {
|
|
|
|
debug(`Calling ${docName}.all`);
|
|
|
|
return apiSerializers[docName].all.bind(apiSerializers[docName]);
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(`Returning as-is`);
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-27 14:44:55 +03:00
|
|
|
const customSerializer = getBestMatchSerializer(apiSerializers, apiConfig.docName, apiConfig.method);
|
|
|
|
const defaultSerializer = getBestMatchSerializer(apiSerializers, 'default', apiConfig.method);
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2022-03-27 14:44:55 +03:00
|
|
|
if (customSerializer) {
|
|
|
|
// CASE: custom serializer exists
|
|
|
|
tasks.push(function doCustomSerializer() {
|
|
|
|
return customSerializer(response, apiConfig, frame);
|
|
|
|
});
|
|
|
|
} else if (defaultSerializer) {
|
|
|
|
// CASE: Fall back to default serializer
|
|
|
|
tasks.push(function doDefaultSerializer() {
|
|
|
|
return defaultSerializer(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);
|
|
|
|
};
|