Ghost/ghost/admin/mirage/config/webhooks.js
Kevin Ansfield d0a834b477 🐛 Fixed missing error notification when attempting to activate an invalid theme
no issue
- updates error handling to work with new v2 error response formats
  - `.errorType` -> `.error`
  - `.errorDetails` -> `.details`
2019-03-25 11:32:29 +00:00

63 lines
1.8 KiB
JavaScript

import {Response} from 'ember-cli-mirage';
import {isEmpty} from '@ember/utils';
import {paginatedResponse} from '../utils';
export default function mockWebhooks(server) {
server.get('/webhooks/', paginatedResponse('webhooks'));
server.post('/webhooks/', function ({webhooks}) {
let attrs = this.normalizedRequestAttrs();
let errors = [];
if (!attrs.name) {
errors.push({
type: 'ValidationError',
message: 'Name is required',
property: 'name'
});
}
if (!attrs.event) {
errors.push({
type: 'ValidationError',
message: 'Event is required',
property: 'event'
});
}
if (!attrs.targetUrl) {
errors.push({
type: 'ValidationError',
message: 'Target URL is required',
property: 'target_url'
});
}
if (attrs.name && (webhooks.findBy({name: attrs.name, integrationId: attrs.integrationId}) || attrs.name.match(/Duplicate/i))) {
errors.push({
type: 'ValidationError',
message: 'Name has already been used',
property: 'name'
});
}
// TODO: check server-side validation
if (webhooks.findBy({targetUrl: attrs.targetUrl, event: attrs.event})) {
errors.push({
type: 'ValidationError',
message: 'Target URL has already been used for this event',
property: 'target_url'
});
}
if (!isEmpty(errors)) {
return new Response(422, {}, {errors});
}
return webhooks.create(attrs);
});
server.put('/webhooks/:id/');
server.del('/webhooks/:id/');
}