fleet/frontend/kolide/request.js
Mike Stone d747a471af Refactor API client (#1335)
* Isolate each API entity
* Improve code structure in API client and request mocks
* Standardize on a request mock structure
* Use helper for creating request mocks
* Adds Request class to handle API requests
2017-03-02 17:07:01 -05:00

55 lines
1.3 KiB
JavaScript

import fetch from 'isomorphic-fetch';
import { isUndefined, omitBy } from 'lodash';
export default class Request {
constructor (options) {
this.body = options.body;
this.credentials = 'same-origin';
this.endpoint = options.endpoint;
this.headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
...options.headers,
};
this.method = options.method;
}
static REQUEST_METHODS = {
DELETE: 'DELETE',
GET: 'GET',
PATCH: 'PATCH',
POST: 'POST',
};
static handleResponse (response, jsonResponse) {
if (response.ok) {
return jsonResponse;
}
const error = new Error(response.statusText);
error.error = jsonResponse.error;
error.message = jsonResponse;
error.response = jsonResponse;
error.status = response.status;
throw error;
}
get requestAttributes () {
const { body, credentials, headers, method } = this;
return omitBy({ body, credentials, headers, method }, isUndefined);
}
send () {
const { endpoint, requestAttributes } = this;
return fetch(endpoint, requestAttributes)
.then((response) => {
return response.json()
.then((jsonResponse) => {
return Request.handleResponse(response, jsonResponse);
});
});
}
}