John Cowen b5b9c8d953
ui: Remove jQuery from the production build (#8088)
* ui: Split up client/http and replace $.ajax

This splits the client/http service more in the following ways:

1. Connections are now split out into its own service
2. The transport is now split out into its own service that returns a
listener based http transport
3. Various string parsing/stringifying functions are now split out into
utils

* Remove jQuery from our production build

* Move the coverage serving to the server.js file

* Self review amends

* Add X-Requested-With header

* Move some files around, externalize some functions

* Move connection tracking to use native Set

* Ensure HTTP parsing doesn't encode headers

In the future this will change to deal with all HTTP parsing in one
place, hence the commented out METHOD_PARSING etc

* Start to fix up integration tests to use requestParams
2020-07-07 19:58:46 +01:00

58 lines
1.5 KiB
JavaScript

import Service from '@ember/service';
import createHeaders from 'consul-ui/utils/http/create-headers';
import createXHR from 'consul-ui/utils/http/xhr';
import Request from 'consul-ui/utils/http/request';
import HTTPError from 'consul-ui/utils/http/error';
const xhr = createXHR(createHeaders());
export default Service.extend({
xhr: function(options) {
return xhr(options);
},
request: function(params) {
const request = new Request(params.method, params.url, { body: params.data || {} });
const options = {
...params,
beforeSend: function(xhr) {
request.open(xhr);
},
converters: {
'text json': function(response) {
try {
return JSON.parse(response);
} catch (e) {
return response;
}
},
},
success: function(headers, response, status, statusText) {
// Response-ish
request.respond({
headers: headers,
response: response,
status: status,
statusText: statusText,
});
},
error: function(headers, response, status, statusText, err) {
let error;
if (err instanceof Error) {
error = err;
} else {
error = new HTTPError(status, response);
}
request.error(error);
},
complete: function(status) {
request.close();
},
};
request.fetch = () => {
this.xhr(options);
};
return request;
},
});