Common JS utils
Migration guide
4.4.2
Prometheus middleware getHttpRequestMetricsMiddleware
is now deprecated, you should remove it, same metrics can be aggregated from getRequestDurationMetricsMiddleware
alone
5.2.0
Added loggly adapter. To use loggly you have to set silent:false
and loggly token via LOGGLY_TOKEN
env or directly in the config.
7.1.0
- Added default timeouts for getMongoCachedJSONFetcher (2000ms for fetch execution, 20000ms for background request)
- parallel call of the fetcher will not cause parallel requests to database nor the URL
8.0
logger.module returns pure Winston child instance, rename logger calls:
log.e -> log.error
log.w -> log.warn
log.i -> log.info
9.0
getMongoCacheFetcher is now async
mongoCachedFetcher
- supports remote ETag
- expose ifNoneMatch
- returns result object instead of direct file content
MongoCachedFetcher
Usage:
const collection = mongodb.collection('myCachedFiles');
const fetcher = await getMongoCachedJSOFetcher(collection, {
url: 'https://my.files.com/file1',
cacheLifetime: 60 * 1000,
fetchOptions: { headers: { Authorization: 'myToken' } },
transform: async (content, key) => content,
ensureIndexes: true,
logError: (err) => console.error(err)
});
const parameters = {
url: 'https://my.files.com/file2',
key: 'file2',
metaOnly: false,
ifNoneMatch: 'someOldEtag',
};
const {
content,
isCacheFresh,
etag,
etagMatch
} = await fetcher(parameters );
etag & ifNoneMatch
MongoCachedFetcher automatically stores ETag
of remote resource if it is present in response from remote source.
The stored etag is then used for consequent cache-refresh http call to optimise traffic - no data are transferred
when the data didn't change. This functionality assumes the remote source of JSON data supports If-None-Match
request header
and ETag
response header.
On top of that, the fetcher accepts optional ifNoneMatch
parameter. If it is used, and its value matches currently stored (refreshed) etag value,
result object will not contain content
and the etagMatch
will be true
.