Comparing version 6.1.0 to 7.0.0
@@ -239,3 +239,3 @@ 'use strict'; | ||
return this.delete(`/auth/keys/${credentials.id}`) | ||
return this.delete(`/auth/keys/${credentials.id}`, null, true) | ||
.then(removeAuth, removeAuth); | ||
@@ -402,6 +402,9 @@ } | ||
* @param {string} endpoint API route's endpoint | ||
* @param {boolean} [authenticate=false] Send authentication headers with the | ||
* request | ||
* @returns {Promise} | ||
*/ | ||
get(endpoint) { | ||
get(endpoint, authenticate = false) { | ||
return this.doRequest({ | ||
authenticate, | ||
endpoint, | ||
@@ -438,6 +441,9 @@ method: 'GET', | ||
* @param {(string|Object)} [data] Payload to send with the request | ||
* @param {boolean} [authenticate=false] Send authentication headers with the | ||
* request | ||
* @returns {Promise} | ||
*/ | ||
post(endpoint, data) { | ||
post(endpoint, data, authenticate = false) { | ||
return this.doRequest({ | ||
authenticate, | ||
data, | ||
@@ -472,6 +478,9 @@ endpoint, | ||
* @param {(string|Object)} [data] Payload to send with the request | ||
* @param {boolean} [authenticate=false] Send authentication headers with the | ||
* request | ||
* @returns {Promise} | ||
*/ | ||
put(endpoint, data) { | ||
put(endpoint, data, authenticate = false) { | ||
return this.doRequest({ | ||
authenticate, | ||
data, | ||
@@ -501,6 +510,9 @@ endpoint, | ||
* @param {(string|Object)} [data] Payload to send with the request | ||
* @param {boolean} [authenticate=false] Send authentication headers with the | ||
* request | ||
* @returns {Promise} | ||
*/ | ||
delete(endpoint, data) { | ||
delete(endpoint, data, authenticate = false) { | ||
return this.doRequest({ | ||
authenticate, | ||
data, | ||
@@ -522,14 +534,13 @@ endpoint, | ||
* @param {string} options.method HTTP method | ||
* @param {boolean} [options.authenticate=false] Send authentication headers | ||
* with the request | ||
* @param {(string|Object)} [options.data] | ||
* @returns {Promise<Object,Object>} | ||
*/ | ||
doRequest(options) { | ||
if (typeof options !== 'object') { | ||
return Promise.reject(new Error('Expected options')); | ||
} | ||
const data = options.data; | ||
const endpoint = options.endpoint; | ||
const method = options.method; | ||
doRequest({ | ||
authenticate = false, | ||
data, | ||
endpoint, | ||
method, | ||
}) { | ||
if (!endpoint || typeof endpoint !== 'string') { | ||
@@ -543,3 +554,2 @@ return Promise.reject(new Error('Expected URL')); | ||
const requestOptions = { | ||
headers: this.getAuthenticationHeader(url, method), | ||
method: method.toLowerCase(), | ||
@@ -550,2 +560,6 @@ url, | ||
if (authenticate) { | ||
requestOptions.headers = this.getAuthenticationHeader(url, method); | ||
} | ||
if (data) { | ||
@@ -577,5 +591,8 @@ if (isPlainObject(data)) { | ||
return this.requestEngine(this.mapRequestOptions(requestOptions)) | ||
.catch(error => Promise.reject( | ||
error.response ? error.response : error | ||
)); | ||
.then( | ||
response => response, | ||
error => Promise.reject( | ||
error.response ? error.response : error | ||
) | ||
); | ||
} | ||
@@ -582,0 +599,0 @@ |
@@ -57,3 +57,3 @@ { | ||
}, | ||
"version": "6.1.0" | ||
"version": "7.0.0" | ||
} |
@@ -61,5 +61,6 @@ # Halfpenny | ||
### `Halfpenny#get(endpoint)` | ||
### `Halfpenny#get(endpoint[, authenticate])` | ||
* **`endpoint`** `<String>`: API route’s endpoint | ||
* **`authenticate`** `<Boolean>`: Send authentication headers with the request. Default = `false`. | ||
@@ -79,6 +80,7 @@ Make a HTTP `GET` request with the specified `endpoint` to the API. Returns a `Promise`. | ||
### `Halfpenny#post(endpoint[, data])` | ||
### `Halfpenny#post(endpoint[, data][, authenticate])` | ||
* **`endpoint`** `<String>`: API route’s endpoint | ||
* **`data`** `<String> | <Object>`: Payload to send with the request | ||
* **`authenticate`** `<Boolean>`: Send authentication headers with the request. Default = `false`. | ||
@@ -106,6 +108,7 @@ Make a HTTP `POST` request with the specified `endpoint` to the API. Optionally, send `data` in the request body. Returns a `Promise`. | ||
### `Halfpenny#put(endpoint[, data])` | ||
### `Halfpenny#put(endpoint[, data][, authenticate])` | ||
* **`endpoint`** `<String>`: API route’s endpoint | ||
* **`data`** `<String> | <Object>`: Payload to send with the request | ||
* **`authenticate`** `<Boolean>`: Send authentication headers with the request. Default = `false`. | ||
@@ -131,6 +134,7 @@ Make a HTTP `PUT` request with the specified `endpoint` to the API. Optionally, send `data` in the request body. Returns a `Promise`. | ||
### `Halfpenny#delete(endpoint[, data])` | ||
### `Halfpenny#delete(endpoint[, data][, authenticate])` | ||
* **`endpoint`** `<String>`: API route’s endpoint | ||
* **`data`** `<String> | <Object>`: Payload to send with the request | ||
* **`authenticate`** `<Boolean>`: Send authentication headers with the request. Default = `false`. | ||
@@ -171,13 +175,39 @@ Make a HTTP `DELETE` request with the specified `endpoint` to the API. Optionally, send `data` in the request body. Returns a `Promise`. | ||
```js | ||
const cloneDeep = require('lodash/cloneDeep'); | ||
/** | ||
* Map request options. | ||
* | ||
* @param {Object} requestOptions | ||
* @param {Object} requestOptions.headers | ||
* @param {string} requestOptions.method | ||
* @param {string} requestOptions.url | ||
* @param {booleam} requestOptions.withCredentials | ||
* @param {(string|Object)} [requestOptions.data] | ||
* @returns {Object} | ||
*/ | ||
hp.mapRequestOptions = (requestOptions) => { | ||
const opts = cloneDeep(requestOptions); | ||
delete ops.withCredentials; | ||
opts.xhrFields = { | ||
withCredentials: true, | ||
const data = requestOptions.data; | ||
const mappedOptions = { | ||
method: requestOptions.method.toUpperCase(), | ||
url: requestOptions.url, | ||
}; | ||
return opts; | ||
if (requestOptions.headers) { | ||
mappedOptions.headers = requestOptions.headers; | ||
} | ||
if (requestOptions.withCredentials) { | ||
mappedOptions.xhrFields = { | ||
withCredentials: true, | ||
}; | ||
} | ||
if (data) { | ||
if (typeof data === 'object') { | ||
mappedOptions.dataType = 'json'; | ||
} | ||
mappedOptions.data = data; | ||
} | ||
return mappedOptions; | ||
}; | ||
@@ -184,0 +214,0 @@ ``` |
@@ -142,10 +142,19 @@ 'use strict'; | ||
tape('get API URL', (t) => { | ||
const config = getValidConfig(); | ||
const hp = new Halfpenny(config); | ||
const { baseUrl } = getValidConfig(); | ||
const testEndpoint = 'my-test-endpoint'; | ||
const apiUrl = hp.getApiUrl(testEndpoint); | ||
const expected = `${baseUrl}/${testEndpoint}`; | ||
t.ok(apiUrl, 'returns a value'); | ||
t.ok(apiUrl.indexOf(config.baseUrl) === 0, 'includes baseUrl'); | ||
t.ok(apiUrl.indexOf(testEndpoint) > -1, 'includes endpoint'); | ||
t.equal( | ||
Halfpenny.prototype.getApiUrl.call({ baseUrl }, testEndpoint), | ||
expected, | ||
'returns formatted URL' | ||
); | ||
t.equal( | ||
Halfpenny.prototype.getApiUrl.call( | ||
{ baseUrl: `${baseUrl}/` }, | ||
testEndpoint | ||
), | ||
expected, | ||
'handles extra slashes' | ||
); | ||
t.end(); | ||
@@ -740,3 +749,4 @@ }); | ||
const hp = new Halfpenny(getValidConfig()); | ||
const stub = sinon.stub(hp, 'requestEngine'); | ||
const headerStub = sinon.stub(hp, 'getAuthenticationHeader'); | ||
const requestEngineStub = sinon.stub(hp, 'requestEngine'); | ||
const testData = { | ||
@@ -749,9 +759,17 @@ email: 'oh-you@pretty-thin.gs', | ||
}; | ||
const testHeaders = { | ||
Authentication: 'and-papas-insane', | ||
}; | ||
const testResponse = {}; | ||
const encode = string => btoa(escape(encodeURIComponent(string))); | ||
const teardown = () => { | ||
headerStub.restore(); | ||
requestEngineStub.restore(); | ||
}; | ||
t.plan(4); | ||
t.plan(5); | ||
stub.returns(Promise.resolve(testResponse)); | ||
headerStub.returns(testHeaders); | ||
requestEngineStub.returns(Promise.resolve(testResponse)); | ||
@@ -764,3 +782,3 @@ hp.doRequest({ | ||
.then((response) => { | ||
const args = stub.firstCall.args[0]; | ||
const args = requestEngineStub.firstCall.args[0]; | ||
@@ -781,5 +799,16 @@ t.equal(response, testResponse, 'passes response object'); | ||
t.ok(args.withCredentials, 'sets CORS'); | ||
return hp.doRequest({ | ||
authenticate: true, | ||
endpoint: '/its-terminal', | ||
method: 'GET', | ||
}); | ||
}) | ||
.then(() => { | ||
const args = requestEngineStub.secondCall.args[0]; | ||
t.equal(args.headers, testHeaders, 'sets authenticate header'); | ||
}) | ||
.catch(t.end) | ||
.then(stub.restore, stub.restore); | ||
.then(teardown, teardown); | ||
}); | ||
@@ -798,11 +827,6 @@ | ||
hp.doRequest() | ||
t.throws(() => hp.doRequest(), 'throws with no args'); | ||
hp.doRequest({}) | ||
.then( | ||
() => t.fail('Resolves with no args'), | ||
() => { | ||
t.pass('Rejects with no args'); | ||
return hp.doRequest({}); | ||
} | ||
) | ||
.then( | ||
() => t.fail('Resolves with no endpoint'), | ||
@@ -881,3 +905,2 @@ () => { | ||
data, | ||
headers: {}, | ||
url: hp.getApiUrl('/silly/route'), | ||
@@ -884,0 +907,0 @@ method: 'post', |
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
51946
1458
223