github.com/outbrain/consul@v1.4.5/ui-v2/app/adapters/intention.js (about) 1 import Adapter, { 2 REQUEST_CREATE, 3 REQUEST_UPDATE, 4 DATACENTER_QUERY_PARAM as API_DATACENTER_KEY, 5 } from './application'; 6 import { FOREIGN_KEY as DATACENTER_KEY } from 'consul-ui/models/dc'; 7 import { PRIMARY_KEY, SLUG_KEY } from 'consul-ui/models/intention'; 8 import { OK as HTTP_OK } from 'consul-ui/utils/http/status'; 9 import { POST as HTTP_POST } from 'consul-ui/utils/http/method'; 10 import makeAttrable from 'consul-ui/utils/makeAttrable'; 11 export default Adapter.extend({ 12 urlForQuery: function(query, modelName) { 13 return this.appendURL('connect/intentions', [], this.cleanQuery(query)); 14 }, 15 urlForQueryRecord: function(query, modelName) { 16 if (typeof query.id === 'undefined') { 17 throw new Error('You must specify an id'); 18 } 19 return this.appendURL('connect/intentions', [query.id], this.cleanQuery(query)); 20 }, 21 urlForCreateRecord: function(modelName, snapshot) { 22 return this.appendURL('connect/intentions', [], { 23 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 24 }); 25 }, 26 urlForUpdateRecord: function(id, modelName, snapshot) { 27 return this.appendURL('connect/intentions', [snapshot.attr(SLUG_KEY)], { 28 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 29 }); 30 }, 31 urlForDeleteRecord: function(id, modelName, snapshot) { 32 return this.appendURL('connect/intentions', [snapshot.attr(SLUG_KEY)], { 33 [API_DATACENTER_KEY]: snapshot.attr(DATACENTER_KEY), 34 }); 35 }, 36 isUpdateRecord: function(url, method) { 37 return ( 38 url.pathname === 39 this.parseURL( 40 this.urlForUpdateRecord(null, 'intention', makeAttrable({ [DATACENTER_KEY]: '' })) 41 ).pathname 42 ); 43 }, 44 isCreateRecord: function(url, method) { 45 return ( 46 method.toUpperCase() === HTTP_POST && 47 url.pathname === 48 this.parseURL(this.urlForCreateRecord('intention', makeAttrable({ [DATACENTER_KEY]: '' }))) 49 .pathname 50 ); 51 }, 52 handleResponse: function(status, headers, payload, requestData) { 53 let response = payload; 54 const method = requestData.method; 55 if (status === HTTP_OK) { 56 const url = this.parseURL(requestData.url); 57 switch (true) { 58 case this.isQueryRecord(url, method): 59 case this.isUpdateRecord(url, method): 60 case this.isCreateRecord(url, method): 61 // TODO: We just need to upgrade this (^^ sorry linter) entire API to 62 // use a full request-like object 63 response = this.handleSingleResponse(url, response, PRIMARY_KEY, SLUG_KEY); 64 break; 65 default: 66 response = this.handleBatchResponse(url, response, PRIMARY_KEY, SLUG_KEY); 67 } 68 } 69 return this._super(status, headers, response, requestData); 70 }, 71 dataForRequest: function(params) { 72 const data = this._super(...arguments); 73 switch (params.requestType) { 74 case REQUEST_UPDATE: 75 case REQUEST_CREATE: 76 return data.intention; 77 } 78 return data; 79 }, 80 });