Comparing version 1.3.2 to 1.4.0
@@ -133,2 +133,3 @@ # APIClient | ||
- `.get(url, options = {})` | ||
- `.head(url, options = {})` | ||
- `.post(url, body, options = {})` | ||
@@ -135,0 +136,0 @@ - `.put(url, body, options = {})` |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://homer0.github.io/wootils/", | ||
"version": "1.3.2", | ||
"version": "1.4.0", | ||
"repository": "homer0/wootils", | ||
@@ -8,0 +8,0 @@ "author": "Leonardo Apiwan (@homer0) <me@homer0.com>", |
@@ -9,5 +9,6 @@ const statuses = require('statuses'); | ||
* @typedef {Object} FetchOptions | ||
* @property {String} method The request method. | ||
* @property {Object} headers The request headers. | ||
* @property {String} body The request body. | ||
* @property {string} method The request method. | ||
* @property {Object} headers The request headers. | ||
* @property {string} body The request body. | ||
* @property {boolean} json Whether or not the response should _"JSON decoded"_. | ||
*/ | ||
@@ -186,2 +187,11 @@ | ||
/** | ||
* Makes a `HEAD` request. | ||
* @param {String} url The request URL. | ||
* @param {FetchOptions} [options={}] The request options. | ||
* @return {Promise<Object,Error>} | ||
*/ | ||
head(url, options = {}) { | ||
return this.get(url, Object.assign({}, options, { method: 'head' })); | ||
} | ||
/** | ||
* Makes a `POST` request. | ||
@@ -313,7 +323,9 @@ * @param {String} url The request URL. | ||
* Makes a request. | ||
* @param {Object} options The request options. | ||
* @param {String} options.url The request URL. | ||
* @param {String} options.method The request method. `GET` by default. | ||
* @param {Object} options.body A request body to send. | ||
* @param {Object} options.headers The request headers. | ||
* @param {Object} options The request options. | ||
* @param {string} options.url The request URL. | ||
* @param {string} options.method The request method. `GET` by default. | ||
* @param {Object} options.body A request body to send. | ||
* @param {Object} options.headers The request headers. | ||
* @param {boolean} options.json Whether or not the response should _"JSON decoded"_. `true` | ||
* by default. | ||
* @return {Promise<Object,Error>} | ||
@@ -333,6 +345,8 @@ * @todo Add support for a `string` `body`. | ||
} | ||
// Get the request URL. | ||
// Format the flag the method will use to decided whether to decode the response or not. | ||
const handleAsJSON = typeof opts.json === 'boolean' ? opts.json : true; | ||
const { url } = opts; | ||
// Remove the URL from the options in order to make it a valid FetchOptions object. | ||
// Remove the necessary options in order to make it a valid `FetchOptions` object. | ||
delete opts.url; | ||
delete opts.json; | ||
// If the options include a body... | ||
@@ -365,4 +379,9 @@ if (opts.body) { | ||
responseStatus = response.status; | ||
// If the response supports `json()`, decode it, otherwise return the same response. | ||
return response.json ? response.json() : response; | ||
/** | ||
* If the response should be handled as JSON and it has a `json()` method, return the | ||
* promise of the decoded content, otherwise just return the same object. | ||
*/ | ||
return handleAsJSON && response.json ? | ||
response.json() : | ||
response; | ||
}) | ||
@@ -369,0 +388,0 @@ .then((response) => ( |
@@ -275,3 +275,3 @@ jest.unmock('/shared/apiClient'); | ||
it('should make a successfully GET request with a client that doesn\'t support json()', () => { | ||
it('should make a successfully GET request with a response that doesn\'t support json()', () => { | ||
// Given | ||
@@ -300,2 +300,28 @@ const requestURL = 'http://example.com'; | ||
it('should make a successfully GET request without decoding the response', () => { | ||
// Given | ||
const requestURL = 'http://example.com'; | ||
const requestResponse = { | ||
status: 200, | ||
json: jest.fn(), | ||
}; | ||
const fetchClient = jest.fn(() => Promise.resolve(requestResponse)); | ||
let sut = null; | ||
// When | ||
sut = new APIClient('', '', fetchClient); | ||
return sut.fetch({ url: requestURL, json: false }) | ||
.then((response) => { | ||
// Then | ||
expect(response).toEqual(requestResponse); | ||
expect(requestResponse.json).toHaveBeenCalledTimes(0); | ||
expect(fetchClient).toHaveBeenCalledTimes(1); | ||
expect(fetchClient).toHaveBeenCalledWith(requestURL, { | ||
method: 'GET', | ||
}); | ||
}) | ||
.catch((error) => { | ||
throw error; | ||
}); | ||
}); | ||
it('should make a successfully POST request', () => { | ||
@@ -660,2 +686,26 @@ // Given | ||
}); | ||
it('should make a successfully HEAD request using the shortcut method', () => { | ||
// Given | ||
const requestURL = 'http://example.com'; | ||
const requestMethod = 'head'; | ||
const requestResponse = { | ||
status: 200, | ||
}; | ||
const fetchClient = jest.fn(() => Promise.resolve(requestResponse)); | ||
let sut = null; | ||
// When | ||
sut = new APIClient('', '', fetchClient); | ||
return sut.head(requestURL) | ||
.then(() => { | ||
// Then | ||
expect(fetchClient).toHaveBeenCalledTimes(1); | ||
expect(fetchClient).toHaveBeenCalledWith(requestURL, { | ||
method: requestMethod.toUpperCase(), | ||
}); | ||
}) | ||
.catch((error) => { | ||
throw error; | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
367038
4123
56