2021-01-21 11:57:52 +03:00
|
|
|
const models = require('../../models');
|
2021-04-27 16:18:04 +03:00
|
|
|
const i18n = require('../../lib/common/i18n');
|
2021-01-21 11:57:52 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
|
|
|
|
|
|
|
const allowedTypes = {
|
|
|
|
post: models.Post,
|
|
|
|
tag: models.Tag,
|
|
|
|
user: models.User
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'slugs',
|
|
|
|
generate: {
|
|
|
|
options: [
|
|
|
|
'include',
|
|
|
|
'type'
|
|
|
|
],
|
|
|
|
data: [
|
|
|
|
'name'
|
|
|
|
],
|
|
|
|
permissions: true,
|
|
|
|
validation: {
|
|
|
|
options: {
|
|
|
|
type: {
|
|
|
|
required: true,
|
|
|
|
values: Object.keys(allowedTypes)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
name: {
|
|
|
|
required: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
query(frame) {
|
|
|
|
return models.Base.Model.generateSlug(allowedTypes[frame.options.type], frame.data.name, {status: 'all'})
|
|
|
|
.then((slug) => {
|
|
|
|
if (!slug) {
|
|
|
|
return Promise.reject(new errors.GhostError({
|
|
|
|
message: i18n.t('errors.api.slugs.couldNotGenerateSlug')
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
return slug;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|