github.com/outbrain/consul@v1.4.5/ui-v2/app/adapters/node.js (about)

     1  import Adapter, { DATACENTER_QUERY_PARAM as API_DATACENTER_KEY } from './application';
     2  import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc';
     3  import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/node';
     4  import { OK as HTTP_OK } from 'consul-ui/utils/http/status';
     5  // TODO: Looks like ID just isn't used at all
     6  // consider just using .Node for the SLUG_KEY
     7  const fillSlug = function(item) {
     8    if (item[SLUG_KEY] === '') {
     9      item[SLUG_KEY] = item['Node'];
    10    }
    11    return item;
    12  };
    13  export default Adapter.extend({
    14    urlForQuery: function(query, modelName) {
    15      return this.appendURL('internal/ui/nodes', [], this.cleanQuery(query));
    16    },
    17    urlForQueryRecord: function(query, modelName) {
    18      if (typeof query.id === 'undefined') {
    19        throw new Error('You must specify an id');
    20      }
    21      return this.appendURL('internal/ui/node', [query.id], this.cleanQuery(query));
    22    },
    23    handleBatchResponse: function(url, response, primary, slug) {
    24      const dc = url.searchParams.get(API_DATACENTER_KEY) || '';
    25      return response.map((item, i, arr) => {
    26        // this can go in the serializer
    27        item = fillSlug(item);
    28        // this could be replaced by handleSingleResponse
    29        // maybe perf test first although even polyfilled searchParams should be super fast
    30        return {
    31          ...item,
    32          ...{
    33            [DATACENTER_KEY]: dc,
    34            [PRIMARY_KEY]: this.uidForURL(url, item[SLUG_KEY]),
    35          },
    36        };
    37      });
    38    },
    39    handleResponse: function(status, headers, payload, requestData) {
    40      let response = payload;
    41      const method = requestData.method;
    42      if (status === HTTP_OK) {
    43        const url = this.parseURL(requestData.url);
    44        switch (true) {
    45          case this.isQueryRecord(url, method):
    46            response = this.handleSingleResponse(url, fillSlug(response), PRIMARY_KEY, SLUG_KEY);
    47            break;
    48          default:
    49            response = this.handleBatchResponse(url, response, PRIMARY_KEY, SLUG_KEY);
    50        }
    51      }
    52      return this._super(status, headers, response, requestData);
    53    },
    54  });