mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-03 23:00:14 -05:00
refs: https://github.com/TryGhost/Toolbox/issues/245 - Added a serializer called default to the canary API - Ideally, this would be part of the shared framework, but this would change v2/v3 and we're about to get rid of them - Therefore, we change just canary for now, and we can refactor again later. - Added wiring to handler that uses the default serializer, if there is a default, and isn't an explicit serializer for the endpoint - Removed the invites serializer, so that one endpoint now uses the default Note: previous commits have added explicit serializers to every endpoint, this is the first step towards paring that back so that we have less serializers overall, not more!
141 lines
4.2 KiB
JavaScript
141 lines
4.2 KiB
JavaScript
const debug = require('@tryghost/debug')('api:shared:serializers:handle');
|
|
const Promise = require('bluebird');
|
|
const {sequence} = require('@tryghost/promise');
|
|
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 (!apiConfig) {
|
|
return Promise.reject(new errors.IncorrectUsageError());
|
|
}
|
|
|
|
if (!apiSerializers) {
|
|
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);
|
|
});
|
|
}
|
|
|
|
// CASE: custom serializer exists
|
|
if (apiSerializers[apiConfig.docName]) {
|
|
if (apiSerializers[apiConfig.docName].all) {
|
|
tasks.push(function serialiseCustomAll() {
|
|
return apiSerializers[apiConfig.docName].all(response, apiConfig, frame);
|
|
});
|
|
}
|
|
|
|
if (apiSerializers[apiConfig.docName][apiConfig.method]) {
|
|
tasks.push(function serialiseCustomMethod() {
|
|
return apiSerializers[apiConfig.docName][apiConfig.method](response, apiConfig, frame);
|
|
});
|
|
}
|
|
|
|
// CASE: Fall back to default serializer
|
|
} else if (apiSerializers.default) {
|
|
if (apiSerializers.default.all) {
|
|
tasks.push(function serializeDefaultAll() {
|
|
return apiSerializers.default.all(response, apiConfig, frame);
|
|
});
|
|
}
|
|
|
|
if (apiSerializers.default[apiConfig.method]) {
|
|
tasks.push(function serializeDefaultMethod() {
|
|
return apiSerializers.default[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);
|
|
};
|