2019-08-09 17:11:24 +03:00
|
|
|
const models = require('../../models');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('../../../shared/i18n');
|
2020-05-22 21:22:20 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2019-08-09 17:11:24 +03:00
|
|
|
|
|
|
|
const allowedTypes = {
|
|
|
|
post: models.Post,
|
|
|
|
tag: models.Tag,
|
2020-03-19 18:23:10 +03:00
|
|
|
user: models.User
|
2019-08-09 17:11:24 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
docName: 'slugs',
|
|
|
|
generate: {
|
2021-07-07 19:12:51 +03:00
|
|
|
headers: {
|
|
|
|
cacheInvalidate: true
|
|
|
|
},
|
2019-08-09 17:11:24 +03:00
|
|
|
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) {
|
2020-05-22 21:22:20 +03:00
|
|
|
return Promise.reject(new errors.GhostError({
|
|
|
|
message: i18n.t('errors.api.slugs.couldNotGenerateSlug')
|
2019-08-09 17:11:24 +03:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
return slug;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|