From 18c3b317f7c47dd740faf491c3ad6f9c93411d37 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Mon, 19 Jul 2021 12:43:14 -0400 Subject: [PATCH 1/3] Call response handling code when `httpClient` is called directly. --- CHANGELOG.md | 9 +++++++++ main.js | 43 +++++++++++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 629d579..802e57f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # @digitalbazaar/http-client ChangeLog +## 1.2.0 - 2021-07-xx + +### Added +- Ensure that body parsing will occur for JSON content types + when individual method functions (e.g., `get`, `post`) are + not used (e.g., `httpClient(url, {method: 'get'}`). Body + parsing can be disabled by passing the `parseBody` option + set to `false`. + ## 1.1.0 - 2021-04-06 ### Changed diff --git a/main.js b/main.js index c4976e2..ed1b612 100644 --- a/main.js +++ b/main.js @@ -14,6 +14,17 @@ const proxyMethods = new Set([ ]); export const httpClient = new Proxy(ky, { + async apply(target, thisArg, args) { + let method = 'get'; + if(args[1] && typeof args[1] === 'object') { + method = args[1].method; + } + // only intercept particular methods + if(!(method && proxyMethods.has(method))) { + return target.apply(thisArg, args); + } + return _handleResponse(target, thisArg, args); + }, get(target, propKey) { const propValue = target[propKey]; @@ -22,22 +33,30 @@ export const httpClient = new Proxy(ky, { return propValue; } return async function() { - let response; - try { - response = await propValue.apply(this, arguments); - } catch(e) { - return _handleError(e); - } - // a 204 will not include a content-type header - const contentType = response.headers.get('content-type'); - if(contentType && contentType.includes('json')) { - response.data = await response.json(); - } - return response; + return _handleResponse(propValue, this, arguments); }; } }); +async function _handleResponse(target, thisArg, args) { + let response; + try { + response = await target.apply(thisArg, args); + } catch(e) { + return _handleError(e); + } + const {parseBody = true} = args[1] || {}; + if(parseBody) { + // a 204 will not include a content-type header + const contentType = response.headers.get('content-type'); + if(contentType && contentType.includes('json')) { + console.log('body parsed for', args[0]); + response.data = await response.json(); + } + } + return response; +} + async function _handleError(e) { // handle network errors that do not have a response if(!e.response) { From a8a43fd9afab406efdc7665f35cd64e4cce358c9 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Mon, 19 Jul 2021 12:44:14 -0400 Subject: [PATCH 2/3] Remove logging. --- main.js | 1 - 1 file changed, 1 deletion(-) diff --git a/main.js b/main.js index ed1b612..41b0040 100644 --- a/main.js +++ b/main.js @@ -50,7 +50,6 @@ async function _handleResponse(target, thisArg, args) { // a 204 will not include a content-type header const contentType = response.headers.get('content-type'); if(contentType && contentType.includes('json')) { - console.log('body parsed for', args[0]); response.data = await response.json(); } } From 1c49c2387a344fab53aa86ae4f2a9bd76db464d4 Mon Sep 17 00:00:00 2001 From: Dave Longley Date: Mon, 19 Jul 2021 12:51:44 -0400 Subject: [PATCH 3/3] Simplify `apply` implementation. - `httpClient` is only called as a function to perform a request, so the proxy apply handler implementation can be simplified. --- main.js | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/main.js b/main.js index 41b0040..64209b4 100644 --- a/main.js +++ b/main.js @@ -14,17 +14,7 @@ const proxyMethods = new Set([ ]); export const httpClient = new Proxy(ky, { - async apply(target, thisArg, args) { - let method = 'get'; - if(args[1] && typeof args[1] === 'object') { - method = args[1].method; - } - // only intercept particular methods - if(!(method && proxyMethods.has(method))) { - return target.apply(thisArg, args); - } - return _handleResponse(target, thisArg, args); - }, + apply: _handleResponse, get(target, propKey) { const propValue = target[propKey];