@digicms/cms
Version:
An open source headless CMS solution to create and manage your own API. It provides a powerful dashboard and features to make your life easier. Databases supported: MySQL, MariaDB, PostgreSQL, SQLite
52 lines (35 loc) • 1.31 kB
JavaScript
;
const { getOr } = require('lodash/fp');
const { contentTypes, sanitize } = require('@strapi/utils');
const { transformResponse } = require('./transform');
const createSingleTypeController = require('./single-type');
const createCollectionTypeController = require('./collection-type');
const getAuthFromKoaContext = getOr({}, 'state.auth');
const createController = ({ contentType }) => {
const ctx = { contentType };
const proto = {
transformResponse(data, meta) {
return transformResponse(data, meta, { contentType });
},
async sanitizeOutput(data, ctx) {
const auth = getAuthFromKoaContext(ctx);
return sanitize.contentAPI.output(data, contentType, { auth });
},
sanitizeInput(data, ctx) {
const auth = getAuthFromKoaContext(ctx);
return sanitize.contentAPI.input(data, contentType, { auth });
},
sanitizeQuery(ctx) {
const auth = getAuthFromKoaContext(ctx);
return sanitize.contentAPI.query(ctx.query, contentType, { auth });
},
};
let ctrl;
if (contentTypes.isSingleType(contentType)) {
ctrl = createSingleTypeController(ctx);
} else {
ctrl = createCollectionTypeController(ctx);
}
return Object.assign(Object.create(proto), ctrl);
};
module.exports = { createController };