Comparing version 0.6.2 to 0.7.0
@@ -13,6 +13,5 @@ /** | ||
var REST = require('./util/http.client'); | ||
var deepmerge = require('deepmerge'); | ||
var DEFAULT_GUID = 'g0'; | ||
var DEFAULT_XHR_PATH = '/api'; | ||
var DEFAULT_XHR_TIMEOUT = 3000; | ||
var DEFAULT_PATH = '/api'; | ||
var DEFAULT_TIMEOUT = 3000; | ||
var MAX_URI_LEN = 2048; | ||
@@ -96,4 +95,4 @@ var OP_READ = 'read'; | ||
headers: options.headers, | ||
xhrPath: options.xhrPath || DEFAULT_XHR_PATH, | ||
xhrTimeout: options.xhrTimeout || DEFAULT_XHR_TIMEOUT, | ||
xhrPath: options.xhrPath || DEFAULT_PATH, | ||
xhrTimeout: options.xhrTimeout || DEFAULT_TIMEOUT, | ||
corsPath: options.corsPath, | ||
@@ -300,3 +299,6 @@ context: options.context || {}, | ||
customHeaders, | ||
deepmerge({ xhrTimeout: request.options.xhrTimeout }, clientConfig), | ||
Object.assign( | ||
{ xhrTimeout: request.options.xhrTimeout }, | ||
clientConfig | ||
), | ||
function getDone(err, response) { | ||
@@ -331,3 +333,3 @@ if (err) { | ||
data, | ||
deepmerge( | ||
Object.assign( | ||
{ | ||
@@ -379,4 +381,4 @@ unsafeAllowRetry: allow_retry_post, | ||
* @param {Object} options configuration options for Fetcher | ||
* @param {String} [options.xhrPath="/api"] The path for XHR requests | ||
* @param {Number} [options.xhrTimout=3000] Timeout in milliseconds for all XHR requests | ||
* @param {String} [options.xhrPath="/api"] The path for requests | ||
* @param {Number} [options.xhrTimout=3000] Timeout in milliseconds for all requests | ||
* @param {Boolean} [options.corsPath] Base CORS path in case CORS is enabled | ||
@@ -523,3 +525,31 @@ * @param {Object} [options.context] The context object that is propagated to all outgoing | ||
updateOptions: function (options) { | ||
this.options = deepmerge(this.options, options); | ||
var self = this; | ||
var contextPicker = {}; | ||
if (this.options.contextPicker && options.contextPicker) { | ||
['GET', 'POST'].forEach(function (method) { | ||
var oldPicker = self.options.contextPicker[method]; | ||
var newPicker = options.contextPicker[method]; | ||
if (Array.isArray(oldPicker) && Array.isArray(newPicker)) { | ||
contextPicker[method] = [].concat(oldPicker, newPicker); | ||
} else if (oldPicker || newPicker) { | ||
var picker = newPicker || oldPicker; | ||
contextPicker[method] = Array.isArray(picker) | ||
? [].concat(picker) | ||
: picker; | ||
} | ||
}); | ||
} else { | ||
contextPicker = Object.assign( | ||
{}, | ||
this.options.contextPicker, | ||
options.contextPicker | ||
); | ||
} | ||
this.options = Object.assign({}, this.options, options, { | ||
context: Object.assign({}, this.options.context, options.context), | ||
contextPicker: contextPicker, | ||
headers: Object.assign({}, this.options.headers, options.headers), | ||
}); | ||
}, | ||
@@ -529,3 +559,3 @@ | ||
* get the serviceMeta array. | ||
* The array contains all xhr meta returned in this session | ||
* The array contains all requests meta returned in this session | ||
* with the 0 index being the first call. | ||
@@ -532,0 +562,0 @@ * @method getServiceMeta |
@@ -22,3 +22,3 @@ /** | ||
/** | ||
* Construct xhr GET URI. | ||
* Construct GET URI. | ||
* @method defaultConstructGetUri | ||
@@ -25,0 +25,0 @@ * @param {String} uri base URI |
@@ -10,3 +10,2 @@ /** | ||
var xhr = require('xhr'); | ||
var forEach = require('./forEach'); | ||
@@ -131,3 +130,3 @@ | ||
function doXhr(method, url, headers, data, config, attempt, callback) { | ||
function doRequest(method, url, headers, data, config, attempt, callback) { | ||
headers = normalizeHeaders(headers, method, config.cors); | ||
@@ -140,3 +139,2 @@ config = mergeConfig(config); | ||
headers: headers, | ||
useXDR: config.useXDR, | ||
withCredentials: config.withCredentials, | ||
@@ -148,5 +146,3 @@ on: { | ||
failure: function (err, response) { | ||
if ( | ||
!shouldRetry(method, config, response.statusCode, attempt) | ||
) { | ||
if (!shouldRetry(method, config, response.status, attempt)) { | ||
callback(err); | ||
@@ -160,4 +156,4 @@ } else { | ||
setTimeout(function retryXHR() { | ||
doXhr( | ||
setTimeout(function retryRequest() { | ||
doRequest( | ||
method, | ||
@@ -182,58 +178,95 @@ url, | ||
function io(url, options) { | ||
return xhr( | ||
{ | ||
url: url, | ||
method: options.method || METHOD_GET, | ||
timeout: options.timeout, | ||
headers: options.headers, | ||
body: options.data, | ||
useXDR: options.cors, | ||
withCredentials: options.withCredentials, | ||
}, | ||
function (err, resp, body) { | ||
var status = resp.statusCode; | ||
var errMessage, errBody; | ||
function FetchrError(options, request, response, responseBody, originalError) { | ||
var err = originalError; | ||
var status = response ? response.status : 0; | ||
var errMessage, errBody; | ||
if (!err && (status === 0 || (status >= 400 && status < 600))) { | ||
if (typeof body === 'string') { | ||
try { | ||
errBody = JSON.parse(body); | ||
if (errBody.message) { | ||
errMessage = errBody.message; | ||
} else { | ||
errMessage = body; | ||
} | ||
} catch (e) { | ||
errMessage = body; | ||
} | ||
if (!err && (status === 0 || (status >= 400 && status < 600))) { | ||
if (typeof responseBody === 'string') { | ||
try { | ||
errBody = JSON.parse(responseBody); | ||
if (errBody.message) { | ||
errMessage = errBody.message; | ||
} else { | ||
errMessage = status | ||
? 'Error ' + status | ||
: 'Internal Fetchr XMLHttpRequest Error'; | ||
errMessage = responseBody; | ||
} | ||
err = new Error(errMessage); | ||
err.statusCode = status; | ||
err.body = errBody || body; | ||
if (err.body) { | ||
err.output = err.body.output; | ||
err.meta = err.body.meta; | ||
} | ||
} catch (e) { | ||
errMessage = responseBody; | ||
} | ||
} else { | ||
errMessage = status | ||
? 'Error ' + status | ||
: 'Internal Fetchr XMLHttpRequest Error'; | ||
} | ||
resp.responseText = body; | ||
err = new Error(errMessage); | ||
err.body = errBody || responseBody; | ||
if (err.body) { | ||
err.output = err.body.output; | ||
err.meta = err.body.meta; | ||
} | ||
} | ||
if (err) { | ||
// getting detail info from xhr module | ||
err.rawRequest = resp.rawRequest; | ||
err.url = resp.url; | ||
err.timeout = options.timeout; | ||
err.rawRequest = { | ||
headers: options.headers, | ||
method: request.method, | ||
url: request.url, | ||
}; | ||
err.statusCode = status; | ||
err.timeout = options.timeout; | ||
err.url = request.url; | ||
options.on.failure.call(null, err, resp); | ||
return err; | ||
} | ||
function io(url, options) { | ||
var controller = new AbortController(); | ||
var request = new Request(url, { | ||
method: options.method, | ||
headers: options.headers, | ||
body: options.data, | ||
credentials: options.withCredentials ? 'include' : 'same-origin', | ||
signal: controller.signal, | ||
}); | ||
var timeoutId = setTimeout(function () { | ||
controller.abort(); | ||
}, options.timeout); | ||
fetch(request) | ||
.then(function (response) { | ||
clearTimeout(timeoutId); | ||
if (response.ok) { | ||
response.text().then(function (responseBody) { | ||
options.on.success(null, { | ||
responseText: responseBody, | ||
statusCode: response.status, | ||
}); | ||
}); | ||
} else { | ||
options.on.success.call(null, null, resp); | ||
response.text().then(function (responseBody) { | ||
options.on.failure( | ||
new FetchrError( | ||
options, | ||
request, | ||
response, | ||
responseBody | ||
), | ||
response | ||
); | ||
}); | ||
} | ||
} | ||
); | ||
}) | ||
.catch(function (err) { | ||
clearTimeout(timeoutId); | ||
options.on.failure( | ||
new FetchrError(options, request, null, null, err), | ||
{ status: 0 } | ||
); | ||
}); | ||
return controller; | ||
} | ||
@@ -260,3 +293,3 @@ | ||
get: function (url, headers, config, callback) { | ||
return doXhr( | ||
return doRequest( | ||
METHOD_GET, | ||
@@ -287,3 +320,3 @@ url, | ||
post: function (url, headers, data, config, callback) { | ||
return doXhr( | ||
return doRequest( | ||
METHOD_POST, | ||
@@ -290,0 +323,0 @@ url, |
{ | ||
"name": "fetchr", | ||
"version": "0.6.2", | ||
"version": "0.7.0", | ||
"description": "Fetchr augments Flux applications by allowing Flux stores to be used on server and client to fetch data", | ||
@@ -8,7 +8,9 @@ "main": "index.js", | ||
"scripts": { | ||
"cover": "istanbul cover --dir artifacts -- ./node_modules/mocha/bin/_mocha tests/unit/ --recursive --reporter spec --timeout 20000 --exit", | ||
"format": "prettier --write .", | ||
"format:check": "prettier --check .", | ||
"lint": "eslint . && npm run format:check", | ||
"test": "NODE_ENV=test mocha tests/unit/ --recursive --reporter spec --timeout 20000 --exit" | ||
"test": "npm run test:unit && npm run test:functional", | ||
"test:coverage": "nyc --reporter=lcov npm run test:unit", | ||
"test:unit": "NODE_ENV=test mocha tests/unit/ --recursive --reporter spec --timeout 20000 --exit --require tests/unit/setup.js", | ||
"test:functional": "NODE_ENV=test mocha tests/functional/*.test.js --reporter spec --exit -t 10000" | ||
}, | ||
@@ -27,22 +29,19 @@ "repository": { | ||
"dependencies": { | ||
"deepmerge": "^4.2.2", | ||
"fumble": "^0.1.0", | ||
"xhr": "^2.4.0" | ||
"fumble": "^0.1.0" | ||
}, | ||
"devDependencies": { | ||
"body-parser": "^1.19.0", | ||
"abortcontroller-polyfill": "^1.7.3", | ||
"chai": "^4.2.0", | ||
"coveralls": "^3.0.5", | ||
"eslint": "^7.29.0", | ||
"express": "^4.17.1", | ||
"istanbul": "^0.4.5", | ||
"lodash": "^4.17.15", | ||
"fetch-mock": "^9.11.0", | ||
"mocha": "^9.0.0", | ||
"mockery": "^2.0.0", | ||
"pre-commit": "^1.0.0", | ||
"node-fetch": "^2.6.2", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.3.2", | ||
"qs": "^6.7.0", | ||
"request": "^2.81.0", | ||
"puppeteer": "^10.2.0", | ||
"sinon": "^11.1.1", | ||
"supertest": "^6.0.0" | ||
"supertest": "^6.0.0", | ||
"webpack": "^5.51.1" | ||
}, | ||
@@ -49,0 +48,0 @@ "keywords": [ |
@@ -9,3 +9,3 @@ # Fetchr | ||
Typically on the server, you call your API or database directly to fetch some data. However, on the client, you cannot always call your services in the same way (i.e, cross domain policies). Instead, XHR requests need to be made to the server which get forwarded to your service. | ||
Typically on the server, you call your API or database directly to fetch some data. However, on the client, you cannot always call your services in the same way (i.e, cross domain policies). Instead, XHR/fetch requests need to be made to the server which get forwarded to your service. | ||
@@ -20,2 +20,4 @@ Having to write code differently for both environments is duplicative and error prone. Fetchr provides an abstraction layer over your data service calls so that you can fetch data using the same API on the server and client side. | ||
_Important:_ when on browser, `Fetchr` relies fully on [`Fetch`](https://fetch.spec.whatwg.org/) API. If you need to support old browsers, you will need to install a polyfill as well (eg. https://github.com/github/fetch). | ||
## Setup | ||
@@ -154,4 +156,4 @@ | ||
Service calls on the client transparently become xhr requests. | ||
It is a good idea to set cache headers on common xhr calls. | ||
Service calls on the client transparently become fetch requests. | ||
It is a good idea to set cache headers on common fetch calls. | ||
You can do so by providing a third parameter in your service's callback. | ||
@@ -175,3 +177,3 @@ If you want to look at what headers were set by the service you just called, | ||
}, | ||
statusCode: 200, // You can even provide a custom statusCode for the xhr response | ||
statusCode: 200, // You can even provide a custom statusCode for the fetch response | ||
}; | ||
@@ -248,5 +250,5 @@ callback(null, data, meta); | ||
## XHR Object | ||
## Abort support | ||
The xhr object is returned by the `.end()` method as long as you're _not_ chaining promises. | ||
An object with an `abort` method is returned by the `.end()` method as long as you're _not_ chaining promises. | ||
This is useful if you want to abort a request before it is completed. | ||
@@ -261,7 +263,7 @@ | ||
}); | ||
// req is the xhr object | ||
req.abort(); | ||
``` | ||
However, you can't acces the xhr object if using promise chaining like so: | ||
However, due to the current implementation, you can't access this method if using promise chaining like so: | ||
@@ -277,6 +279,6 @@ ```js | ||
## XHR Timeouts | ||
## Timeouts | ||
`xhrTimeout` is an optional config property that allows you to set timeout (in ms) for all clientside requests, defaults to `3000`. | ||
On the clientside, xhrPath and xhrTimeout will be used for XHR requests. | ||
On the clientside, xhrPath and xhrTimeout will be used for all requests. | ||
On the serverside, xhrPath and xhrTimeout are not needed and are ignored. | ||
@@ -304,5 +306,5 @@ | ||
## XHR Params Processing | ||
## Params Processing | ||
For some applications, there may be a situation where you need to process the service params passed in XHR request before params are sent to the actual service. Typically, you would process these params in the service itself. However, if you want to perform processing across many services (i.e. sanitization for security), then you can use the `paramsProcessor` option. | ||
For some applications, there may be a situation where you need to process the service params passed in the request before they are sent to the actual service. Typically, you would process them in the service itself. However, if you neet to perform processing across many services (i.e. sanitization for security), then you can use the `paramsProcessor` option. | ||
@@ -329,5 +331,5 @@ `paramsProcessor` is a function that is passed into the `Fetcher.middleware` method. It is passed three arguments, the request object, the serviceInfo object, and the service params object. The `paramsProcessor` function can then modify the service params if needed. | ||
## XHR Response Formatting | ||
## Response Formatting | ||
For some applications, there may be a situation where you need to modify an XHR response before it is passed to the client. Typically, you would apply your modifications in the service itself. However, if you want to modify the XHR responses across many services (i.e. add debug information), then you can use the `responseFormatter` option. | ||
For some applications, there may be a situation where you need to modify the response before it is passed to the client. Typically, you would apply your modifications in the service itself. However, if you need to modify the responses across many services (i.e. add debug information), then you can use the `responseFormatter` option. | ||
@@ -354,3 +356,3 @@ `responseFormatter` is a function that is passed into the `Fetcher.middleware` method. It is passed three arguments, the request object, response object and the service response object (i.e. the data returned from your service). The `responseFormatter` function can then modify the service response to add additional information. | ||
Now when an XHR request is performed, your response will contain the `debug` property added above. | ||
Now when an request is performed, your response will contain the `debug` property added above. | ||
@@ -405,3 +407,3 @@ ## CORS Support | ||
You can protect your XHR paths from CSRF attacks by adding a middleware in front of the fetchr middleware: | ||
You can protect your Fetchr middleware paths from CSRF attacks by adding a middleware in front of it: | ||
@@ -412,9 +414,9 @@ `app.use('/myCustomAPIEndpoint', csrf(), Fetcher.middleware());` | ||
Next you need to make sure that the CSRF token is being sent with our XHR requests so that they can be validated. To do this, pass the token in as a key in the `options.context` object on the client: | ||
Next you need to make sure that the CSRF token is being sent with our requests so that they can be validated. To do this, pass the token in as a key in the `options.context` object on the client: | ||
```js | ||
const fetcher = new Fetcher({ | ||
xhrPath: '/myCustomAPIEndpoint', //xhrPath is REQUIRED on the clientside fetcher instantiation | ||
xhrPath: '/myCustomAPIEndpoint', // xhrPath is REQUIRED on the clientside fetcher instantiation | ||
context: { | ||
// These context values are persisted with XHR calls as query params | ||
// These context values are persisted with client calls as query params | ||
_csrf: 'Ax89D94j', | ||
@@ -425,3 +427,3 @@ }, | ||
This `_csrf` will be sent in all XHR requests as a query parameter so that it can be validated on the server. | ||
This `_csrf` will be sent in all client requests as a query parameter so that it can be validated on the server. | ||
@@ -432,3 +434,3 @@ ## Service Call Config | ||
When this call is made from the client, the config object is used to define XHR request options and can be used to override default options: | ||
When this call is made from the client, the config object is used to set some request options and can be used to override default options: | ||
@@ -462,3 +464,3 @@ ```js | ||
With this configuration, Fetchr will retry all requests that fail with 408 status code or with an XHR 0 status code two more times before returning an error. The interval between each request respects | ||
With this configuration, Fetchr will retry all requests that fail with 408 status code or that failed without even reaching the service (status code 0 means, for example, that the client was not able to reach the server) two more times before returning an error. The interval between each request respects | ||
the following formula, based on the exponential backoff and full jitter strategy published in [this AWS architecture blog post](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/): | ||
@@ -505,3 +507,3 @@ | ||
By Default, fetchr appends all context values to the xhr url as query params. `contextPicker` allows you to greater control over which context variables get sent as query params depending on the xhr method (`GET` or `POST`). This is useful when you want to limit the number of variables in a `GET` url in order not to accidentally [cache bust](http://webassets.readthedocs.org/en/latest/expiring.html). | ||
By Default, fetchr appends all context values to the request url as query params. `contextPicker` allows you to have greater control over which context variables get sent as query params depending on the request method (`GET` or `POST`). This is useful when you want to limit the number of variables in a `GET` url in order not to accidentally [cache bust](http://webassets.readthedocs.org/en/latest/expiring.html). | ||
@@ -513,3 +515,3 @@ `contextPicker` follows the same format as the `predicate` parameter in [`lodash/pickBy`](https://lodash.com/docs#pickBy) with two arguments: `(value, key)`. | ||
context: { | ||
// These context values are persisted with XHR calls as query params | ||
// These context values are persisted with client calls as query params | ||
_csrf: 'Ax89D94j', | ||
@@ -532,3 +534,3 @@ device: 'desktop', | ||
context: { | ||
// These context values are persisted with XHR calls as query params | ||
// These context values are persisted with client calls as query params | ||
_csrf: 'Ax89D94j', | ||
@@ -535,0 +537,0 @@ device: 'desktop', |
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
Network access
Supply chain riskThis module accesses the network.
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
81815
1
14
1586
624
1
- Removeddeepmerge@^4.2.2
- Removedxhr@^2.4.0
- Removeddeepmerge@4.3.1(transitive)
- Removeddom-walk@0.1.2(transitive)
- Removedglobal@4.4.0(transitive)
- Removedis-function@1.0.2(transitive)
- Removedmin-document@2.19.0(transitive)
- Removedparse-headers@2.0.5(transitive)
- Removedprocess@0.11.10(transitive)
- Removedxhr@2.6.0(transitive)
- Removedxtend@4.0.2(transitive)