mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
323074f106
fixes https://github.com/TryGhost/Team/issues/809 - Bookshelf won't throw a `NotFoundError` unless `require=true` in the options - this is present in most other API endpoints, so it's just simply missing from the snippet one - without this, Ghost will crash with a 500 saying `Cannot read property 'destroy' of null` - this commit adds `require=true` to the destroy options for both the canary + v3 endpoints
112 lines
2.9 KiB
JavaScript
112 lines
2.9 KiB
JavaScript
const Promise = require('bluebird');
|
|
const i18n = require('../../../shared/i18n');
|
|
const errors = require('@tryghost/errors');
|
|
const models = require('../../models');
|
|
|
|
module.exports = {
|
|
docName: 'snippets',
|
|
|
|
browse: {
|
|
options: [
|
|
'limit',
|
|
'order',
|
|
'page'
|
|
],
|
|
permissions: true,
|
|
query(frame) {
|
|
return models.Snippet.findPage(frame.options);
|
|
}
|
|
},
|
|
|
|
read: {
|
|
headers: {},
|
|
data: [
|
|
'id'
|
|
],
|
|
permissions: true,
|
|
query(frame) {
|
|
return models.Snippet.findOne(frame.data, frame.options)
|
|
.then((model) => {
|
|
if (!model) {
|
|
return Promise.reject(new errors.NotFoundError({
|
|
message: i18n.t('errors.api.snippets.snippetNotFound')
|
|
}));
|
|
}
|
|
|
|
return model;
|
|
});
|
|
}
|
|
},
|
|
|
|
add: {
|
|
statusCode: 201,
|
|
headers: {},
|
|
permissions: true,
|
|
async query(frame) {
|
|
try {
|
|
return await models.Snippet.add(frame.data.snippets[0], frame.options);
|
|
} catch (error) {
|
|
if (error.code && error.message.toLowerCase().indexOf('unique') !== -1) {
|
|
throw new errors.ValidationError({message: i18n.t('errors.api.snippets.snippetAlreadyExists')});
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
}
|
|
},
|
|
|
|
edit: {
|
|
headers: {},
|
|
options: [
|
|
'id'
|
|
],
|
|
validation: {
|
|
options: {
|
|
id: {
|
|
required: true
|
|
}
|
|
}
|
|
},
|
|
permissions: true,
|
|
query(frame) {
|
|
return models.Snippet.edit(frame.data.snippets[0], frame.options)
|
|
.then((model) => {
|
|
if (!model) {
|
|
return Promise.reject(new errors.NotFoundError({
|
|
message: i18n.t('errors.api.snippets.snippetNotFound')
|
|
}));
|
|
}
|
|
|
|
return model;
|
|
});
|
|
}
|
|
},
|
|
|
|
destroy: {
|
|
statusCode: 204,
|
|
headers: {},
|
|
options: [
|
|
'id'
|
|
],
|
|
validation: {
|
|
options: {
|
|
id: {
|
|
required: true
|
|
}
|
|
}
|
|
},
|
|
permissions: true,
|
|
query(frame) {
|
|
frame.options.require = true;
|
|
|
|
return models.Snippet.destroy(frame.options)
|
|
.then(() => null)
|
|
.catch(models.Snippet.NotFoundError, () => {
|
|
return Promise.reject(new errors.NotFoundError({
|
|
message: i18n.t('errors.api.snippets.snippetNotFound')
|
|
}));
|
|
});
|
|
}
|
|
}
|
|
};
|