consul/ui-v2/app/serializers/application.js
John Cowen 9daf8f53d9 ui: Adds blocking query support to the service detail page (#5479)
This commit includes several pieces of functionality to enable services
to be removed and the page to present information that this has happened
but also keep the deleted information on the page. Along with the more
usual blocking query based listing.

To enable this:

1. Implements `meta` on the model (only available on collections in
ember)
2. Adds new `catchable` ComputedProperty alongside a `listen` helper for
working with specific errors that can be thrown from EventSources in an
ember-like way. Briefly, normal computed properties update when a
property changes, EventSources can additionally throw errors so we can
catch them and show different visuals based on that.
2019-05-01 18:22:22 +00:00

63 lines
2.0 KiB
JavaScript

import Serializer from 'ember-data/serializers/rest';
import { get } from '@ember/object';
import {
HEADERS_SYMBOL as HTTP_HEADERS_SYMBOL,
HEADERS_INDEX as HTTP_HEADERS_INDEX,
} from 'consul-ui/utils/http/consul';
export default Serializer.extend({
// this could get confusing if you tried to override
// say `normalizeQueryResponse`
// TODO: consider creating a method for each one of the `normalize...Response` family
normalizeResponse: function(store, primaryModelClass, payload, id, requestType) {
// Pick the meta/headers back off the payload and cleanup
// before we go through serializing
const headers = payload[HTTP_HEADERS_SYMBOL] || {};
delete payload[HTTP_HEADERS_SYMBOL];
const normalizedPayload = this.normalizePayload(payload, id, requestType);
// put the meta onto the response, here this is ok
// as JSON-API allows this and our specific data is now in
// response[primaryModelClass.modelName]
// so we aren't in danger of overwriting anything
// (which was the reason for the Symbol-like property earlier)
// use a method modelled on ember-data methods so we have the opportunity to
// do this on a per-model level
const meta = this.normalizeMeta(
store,
primaryModelClass,
headers,
normalizedPayload,
id,
requestType
);
if (requestType === 'queryRecord') {
normalizedPayload.meta = meta;
}
return this._super(
store,
primaryModelClass,
{
meta: meta,
[primaryModelClass.modelName]: normalizedPayload,
},
id,
requestType
);
},
normalizeMeta: function(store, primaryModelClass, headers, payload, id, requestType) {
const meta = {
cursor: headers[HTTP_HEADERS_INDEX],
date: headers['date'],
};
if (requestType === 'query') {
meta.ids = payload.map(item => {
return get(item, this.primaryKey);
});
}
return meta;
},
normalizePayload: function(payload, id, requestType) {
return payload;
},
});