bv-ui-core 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc +40 -0
- package/.travis.yml +5 -0
- package/CONTRIBUTING.md +98 -0
- package/LICENSE.md +13 -0
- package/OWNERS +2 -0
- package/README.md +63 -0
- package/catalog-info.yaml +30 -0
- package/karma.conf.js +130 -0
- package/lib/README.md +61 -0
- package/lib/api/.babelrc +3 -0
- package/lib/api/.eslintrc +9 -0
- package/lib/api/README.md +134 -0
- package/lib/api/index.js +15 -0
- package/lib/api/statistics.js +148 -0
- package/lib/application/README.md +96 -0
- package/lib/application/index.js +171 -0
- package/lib/body/README.md +13 -0
- package/lib/body/index.js +19 -0
- package/lib/bvFetch/README.md +101 -0
- package/lib/bvFetch/index.js +383 -0
- package/lib/checkHighContrast/README.md +26 -0
- package/lib/checkHighContrast/index.js +36 -0
- package/lib/cookie/README.md +29 -0
- package/lib/cookie/index.js +111 -0
- package/lib/cookieConsent/README.md +41 -0
- package/lib/cookieConsent/index.js +227 -0
- package/lib/cssLoadChecker/README.md +19 -0
- package/lib/cssLoadChecker/index.js +72 -0
- package/lib/date.now/README.md +11 -0
- package/lib/date.now/index.js +40 -0
- package/lib/domainPolice/README.md +66 -0
- package/lib/domainPolice/index.js +95 -0
- package/lib/evented/README.md +42 -0
- package/lib/evented/index.js +135 -0
- package/lib/getOriginalConstructor/README.md +16 -0
- package/lib/getOriginalConstructor/index.js +52 -0
- package/lib/global/README.md +10 -0
- package/lib/global/index.js +48 -0
- package/lib/ie/README.md +13 -0
- package/lib/ie/index.js +25 -0
- package/lib/loader/README.md +73 -0
- package/lib/loader/index.js +358 -0
- package/lib/logger/README.md +82 -0
- package/lib/logger/index.js +180 -0
- package/lib/messageFormat/README.md +10 -0
- package/lib/messageFormat/index.js +42 -0
- package/lib/namespacer/README.md +57 -0
- package/lib/namespacer/index.js +98 -0
- package/lib/performance/README.md +35 -0
- package/lib/performance/entries.js +5 -0
- package/lib/performance/getEntries.js +42 -0
- package/lib/performance/getEntriesByName.js +61 -0
- package/lib/performance/getEntriesByType.js +55 -0
- package/lib/performance/index.js +11 -0
- package/lib/performance/mark.js +97 -0
- package/lib/performance/measure.js +106 -0
- package/lib/performance/now.js +63 -0
- package/lib/pixelsDisplayed/README.md +19 -0
- package/lib/pixelsDisplayed/index.js +113 -0
- package/lib/polyfills/README.md +39 -0
- package/lib/polyfills/fetch/Headers.js +3 -0
- package/lib/polyfills/fetch/Request.js +3 -0
- package/lib/polyfills/fetch/Response.js +3 -0
- package/lib/polyfills/fetch/fetch.js +3 -0
- package/lib/polyfills/fetch/index.js +57 -0
- package/lib/polyfills/promise.js +265 -0
- package/lib/queryShadowDom/README.md +50 -0
- package/lib/queryShadowDom/index.js +93 -0
- package/lib/region/README.md +64 -0
- package/lib/region/europe/index.js +94 -0
- package/lib/region/europe/territories.js +40 -0
- package/lib/region/index.js +123 -0
- package/lib/staticAssetLoader/README.md +116 -0
- package/lib/staticAssetLoader/index.js +248 -0
- package/lib/waitForBody/README.md +21 -0
- package/lib/waitForBody/index.js +36 -0
- package/mkdocs.yaml +5 -0
- package/package.json +55 -0
- package/test/.eslintrc +5 -0
- package/test/fixtures/lib.loader.document-is-wrong-in-ie8.js +5 -0
- package/test/fixtures/lib.loader.loadscript-attribute.js +3 -0
- package/test/fixtures/lib.loader.loadscript.js +3 -0
- package/test/fixtures/lib.loader.loadstylesheet.css +3 -0
- package/test/unit/api/.eslintrc +6 -0
- package/test/unit/api/index.spec.js +37 -0
- package/test/unit/api/response.json +67 -0
- package/test/unit/api/statistics.spec.js +218 -0
- package/test/unit/application/index.spec.js +313 -0
- package/test/unit/body/index.spec.js +15 -0
- package/test/unit/bvFetch/index.spec.js +246 -0
- package/test/unit/checkHighContrast/index.spec.js +16 -0
- package/test/unit/cookie/index.spec.js +191 -0
- package/test/unit/cookieConsent/index.spec.js +119 -0
- package/test/unit/cssLoadChecker/index.spec.js +75 -0
- package/test/unit/date.now/index.spec.js +21 -0
- package/test/unit/domainPolice/index.spec.js +141 -0
- package/test/unit/evented/index.spec.js +59 -0
- package/test/unit/getOriginalConstructor/index.spec.js +60 -0
- package/test/unit/global/index.spec.js +84 -0
- package/test/unit/ie/index.spec.js +14 -0
- package/test/unit/loader/index.spec.js +644 -0
- package/test/unit/logger/index.spec.js +141 -0
- package/test/unit/messageFormat/index.spec.js +31 -0
- package/test/unit/mochaInit.js +8 -0
- package/test/unit/namespacer/index.spec.js +100 -0
- package/test/unit/performance/getEntries.spec.js +30 -0
- package/test/unit/performance/getEntriesByName.spec.js +88 -0
- package/test/unit/performance/getEntriesByType.spec.js +48 -0
- package/test/unit/performance/index.spec.js +35 -0
- package/test/unit/performance/mark.spec.js +40 -0
- package/test/unit/performance/measure.spec.js +50 -0
- package/test/unit/performance/now.spec.js +21 -0
- package/test/unit/pixelsDisplayed/index.spec.js +271 -0
- package/test/unit/polyfills/fetch/Headers.js +12 -0
- package/test/unit/polyfills/fetch/Request.js +12 -0
- package/test/unit/polyfills/fetch/Response.js +12 -0
- package/test/unit/polyfills/fetch/fetch.js +12 -0
- package/test/unit/polyfills/fetch/index.js +61 -0
- package/test/unit/polyfills/promise.spec.js +48 -0
- package/test/unit/region/europe/index.spec.js +70 -0
- package/test/unit/region/index.spec.js +113 -0
- package/test/unit/staticAssetLoader/index.spec.js +215 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/* global fetch */
|
|
2
|
+
/**
|
|
3
|
+
* @fileOverview Module for interacting with the statistcs.json endpoint.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
const envPrefixMap = {
|
|
7
|
+
qa: 'qa.api',
|
|
8
|
+
staging: 'stg.api',
|
|
9
|
+
production: 'api'
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// Default configuration
|
|
13
|
+
const API_VERSION = 5.4;
|
|
14
|
+
const MAX_REQUESTED_IDS = 100;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Calls the statistics.json API with provided options. Returns a promise that
|
|
18
|
+
* is fulfilled with an array of Results from the API or rejected with the error
|
|
19
|
+
* message.
|
|
20
|
+
*
|
|
21
|
+
* Example:
|
|
22
|
+
*
|
|
23
|
+
* getStatistics({
|
|
24
|
+
* productIds: ['product1', 'product2', 'product3'],
|
|
25
|
+
* environment: 'qa',
|
|
26
|
+
* key: 'clients_api_key',
|
|
27
|
+
* type: 'Reviews',
|
|
28
|
+
* filters: {
|
|
29
|
+
* ContentLocale: 'en_US'
|
|
30
|
+
* }
|
|
31
|
+
* }).then(results => {
|
|
32
|
+
* // Do something with results
|
|
33
|
+
* }, errorMsg => {
|
|
34
|
+
* // Do something with error.
|
|
35
|
+
* })
|
|
36
|
+
*
|
|
37
|
+
* https://developer.bazaarvoice.com/docs/read/conversations/statistics/display/5_4
|
|
38
|
+
*
|
|
39
|
+
* @param {Object} options object that contains options
|
|
40
|
+
* @param {Array} options.productIds - array of product IDs
|
|
41
|
+
* @param {Array} options.environment - the data environment
|
|
42
|
+
* @param {Array} options.key - client's api key
|
|
43
|
+
* @param {string} options.type - Using "Reviews" returns statistics for all
|
|
44
|
+
* content, including syndicated content (if enabled on your API key).
|
|
45
|
+
* If you only want statistics for reviews you own that were written for
|
|
46
|
+
* the products specified, use "NativeReviews" instead.
|
|
47
|
+
* @param {Object} [options.filters] - Filters object. Keyed by filter name.
|
|
48
|
+
* @return {Promise} a promise that will be resolved with the raw results
|
|
49
|
+
* from the statistics call.
|
|
50
|
+
*/
|
|
51
|
+
function getStatistics ({
|
|
52
|
+
productIds,
|
|
53
|
+
environment,
|
|
54
|
+
key,
|
|
55
|
+
type,
|
|
56
|
+
incentivized = false,
|
|
57
|
+
filters = {}
|
|
58
|
+
}) {
|
|
59
|
+
if (!productIds || !Array.isArray(productIds)) {
|
|
60
|
+
throw new TypeError('productIds must be an array');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const envPrefix = envPrefixMap[environment];
|
|
64
|
+
if (!envPrefix) {
|
|
65
|
+
throw new TypeError('environment must be "qa", "staging", or "production"');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (!key) {
|
|
69
|
+
throw new TypeError('key must be provided');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!type || !(type === 'Reviews' || type === 'NativeReviews')) {
|
|
73
|
+
throw new TypeError('type must be "Reviews" or "NativeReviews"');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let uri =
|
|
77
|
+
`https://${envPrefix}.bazaarvoice.com/data/statistics.json` +
|
|
78
|
+
`?apiversion=${API_VERSION}` +
|
|
79
|
+
`&passkey=${key}` +
|
|
80
|
+
`&stats=${type}` +
|
|
81
|
+
Object.keys(filters)
|
|
82
|
+
.map(filter => `&filter=${filter}:${filters[filter]}`)
|
|
83
|
+
.join();
|
|
84
|
+
|
|
85
|
+
// We should add `incentivizedStats` request param in case
|
|
86
|
+
// if `incentivized` flag is enabled that means such reviews
|
|
87
|
+
// have to be loaded in response
|
|
88
|
+
if (incentivized) {
|
|
89
|
+
uri = uri + `&incentivizedStats=${incentivized}`;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Clone the productIds so we can manipulate it.
|
|
93
|
+
productIds = [...productIds];
|
|
94
|
+
const productIdChunks = [];
|
|
95
|
+
|
|
96
|
+
if (productIds.length > 100) {
|
|
97
|
+
console.warn('Requesting more than 100 products is not recommended!');
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
while (productIds.length > 0) {
|
|
101
|
+
productIdChunks.push(productIds.splice(0, MAX_REQUESTED_IDS));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return Promise.all(
|
|
105
|
+
productIdChunks.map(
|
|
106
|
+
products =>
|
|
107
|
+
new Promise((resolve, reject) => {
|
|
108
|
+
const requestUri = `${uri}&filter=ProductId:${products.join(',')}`;
|
|
109
|
+
return fetch(requestUri)
|
|
110
|
+
.then(response => response.json())
|
|
111
|
+
.then(json => {
|
|
112
|
+
// If there are errors in the actual response body (from API)
|
|
113
|
+
// we need to handle them here.
|
|
114
|
+
if (json.HasErrors) {
|
|
115
|
+
let errors = json.Errors;
|
|
116
|
+
// If for some reason errors is empty or doesn't exist,
|
|
117
|
+
if (!errors || !Array.isArray(errors) || errors.length <= 0) {
|
|
118
|
+
reject({
|
|
119
|
+
Message: 'An unknown error occurred.',
|
|
120
|
+
Code: 'ERROR_UNKNOWN',
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
else {
|
|
124
|
+
// We can reasonably assume that if an error occurred for this
|
|
125
|
+
// request it should only have a single error response
|
|
126
|
+
// unlike submission api errors, which have multiples.
|
|
127
|
+
reject(json.Errors[0]);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
resolve(json.Results);
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
})
|
|
135
|
+
)
|
|
136
|
+
).then(results => {
|
|
137
|
+
if (results.length === 0) {
|
|
138
|
+
return results;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
return results.reduce((a, b) => {
|
|
142
|
+
return a.concat(b);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
module.exports = getStatistics;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# application
|
|
2
|
+
|
|
3
|
+
The application module provides an Application class that provides methods to
|
|
4
|
+
queue your users' calls until the actual code to support those calls is
|
|
5
|
+
present.
|
|
6
|
+
|
|
7
|
+
For example, your user may expect to be able to ask your application to render
|
|
8
|
+
immediately after this initial application module is loaded, even though the
|
|
9
|
+
code to perform the rendering has not yet loaded.
|
|
10
|
+
|
|
11
|
+
The application module is typically used in conjunction with the
|
|
12
|
+
[loader][1] module, [namespacer][2] module, and some configuration data, as
|
|
13
|
+
shown here. Once you create an application instance, it is good practice to add
|
|
14
|
+
it as a property to a namespace:
|
|
15
|
+
|
|
16
|
+
```js
|
|
17
|
+
var Application = require('bv-ui-core/lib/application');
|
|
18
|
+
var loader = require('bv-ui-core/lib/loader');
|
|
19
|
+
var namespacer = require('bv-ui-core/lib/namespacer');
|
|
20
|
+
var config = require('json!config.json');
|
|
21
|
+
|
|
22
|
+
// Creates a namespace at window.APP and an application object at
|
|
23
|
+
// window.APP.exampleApp. The config object will be available at
|
|
24
|
+
// window.APP.exampleApp.config.
|
|
25
|
+
namespacer.namespace('APP').registerProperty(
|
|
26
|
+
'exampleApp',
|
|
27
|
+
new Application(config)
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Now load following code.
|
|
31
|
+
// loader.load(url).then( ... do something ... );
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
## ES6 Considerations
|
|
35
|
+
|
|
36
|
+
This module takes advantage of Promises, which you may need to [polyfill][3]:
|
|
37
|
+
|
|
38
|
+
- `Promise`: http://caniuse.com/#feat=promises
|
|
39
|
+
|
|
40
|
+
## Methods to be Called by Users
|
|
41
|
+
|
|
42
|
+
An application instance provides the following methods:
|
|
43
|
+
|
|
44
|
+
- `ready(callback)`: (optional) A method intended for your users to call at
|
|
45
|
+
any time in order to register a callback which will be invoked as soon as
|
|
46
|
+
your application has been loaded and its API is available to be used.
|
|
47
|
+
Callback function should be a standard Node errback signature, where its
|
|
48
|
+
first expected parameter will be an error (if applicable), and its second
|
|
49
|
+
parameter will be the application instance. The return value is a Promise
|
|
50
|
+
that you can chain your callbacks onto. The success callbacks (on your
|
|
51
|
+
`then` Promise chain) will only be given a single input parameter, the
|
|
52
|
+
application instance, and your failure callbacks (on your `catch` Promise
|
|
53
|
+
chain) will only be given a single input parameter,
|
|
54
|
+
the error.
|
|
55
|
+
|
|
56
|
+
Note: If you opt to mix and match the callback and the Promise interfaces,
|
|
57
|
+
be aware that all registered callbacks passed *to* the `ready` function
|
|
58
|
+
will be executed first, in the order in which they were attached, and then
|
|
59
|
+
the Promise chain will be resolved or rejected, which will in turn trigger
|
|
60
|
+
callbacks on the appropriate `then` or `catch` chain.
|
|
61
|
+
|
|
62
|
+
- `render(config)`: A method intended for your users to call any time after
|
|
63
|
+
this application module is loaded, passing in configuration information
|
|
64
|
+
related to a request to render your application.
|
|
65
|
+
|
|
66
|
+
```js
|
|
67
|
+
APP.exampleApp.render({ productId : 1 });
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
- `configure(config)`: A method intended for your users to call any time after
|
|
71
|
+
the application code is loaded, passing in general configuration information
|
|
72
|
+
not necessarily related to rendering.
|
|
73
|
+
|
|
74
|
+
## Methods to Be Called by Core Application Code
|
|
75
|
+
|
|
76
|
+
- `processReady(error)`: A method that your core application code can call in
|
|
77
|
+
order to access calls to `ready` that have occurred. The only parameter it
|
|
78
|
+
accepts is an optional one, a load error to be passed to the callbacks in
|
|
79
|
+
its queue, and to reject its Promise with. It will, from that point on,
|
|
80
|
+
process all callbacks immediately when passed to the `ready` method. Queued
|
|
81
|
+
items are processed synchronously, so if your callback is a long-running
|
|
82
|
+
process, you might want to consider wrapping it in a setTimeout.
|
|
83
|
+
- `processQueue(fn)`: A method that your core application code can call in
|
|
84
|
+
order to access calls to `render` that have occurred; the provided function
|
|
85
|
+
will be used to handle those calls, and will then replace the original
|
|
86
|
+
definition of the `render` method. Queued items are processed on the next
|
|
87
|
+
tick.
|
|
88
|
+
- `processConfig(fn)`: A method that your core application code can call in
|
|
89
|
+
order to access calls to `configure` that have occurred; the provided
|
|
90
|
+
function will be used to handle those calls, and will then replace the
|
|
91
|
+
original definition of the `configure` method. Queued items are processed
|
|
92
|
+
synchronously, as they are not expected to have any DOM implications.
|
|
93
|
+
|
|
94
|
+
[1]: ../loader
|
|
95
|
+
[2]: ../namespacer
|
|
96
|
+
[3]: https://github.com/stefanpenner/es6-promise
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* A module that provides a minimalist application class, useful for managing
|
|
4
|
+
* configuration and rendering.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// --------------------------------------------------------------------------
|
|
8
|
+
// Functions.
|
|
9
|
+
// --------------------------------------------------------------------------
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Process a queue.
|
|
13
|
+
*
|
|
14
|
+
* @param {Mixed[]} queue An array of queued items.
|
|
15
|
+
* @param {Function} fn The function to process queue items.
|
|
16
|
+
* @param {Boolean} sync If true, process synchronously.
|
|
17
|
+
*/
|
|
18
|
+
function processQueue (queue, fn, sync) {
|
|
19
|
+
if (typeof fn !== 'function') {
|
|
20
|
+
throw new Error('A function must be provided to process the queue');
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
var originalLength = queue.length;
|
|
24
|
+
|
|
25
|
+
function dequeue () {
|
|
26
|
+
var queueItem = queue.shift();
|
|
27
|
+
|
|
28
|
+
if (sync) {
|
|
29
|
+
fn(queueItem);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
setTimeout(function () {
|
|
34
|
+
fn(queueItem);
|
|
35
|
+
}, 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
while (queue.length > 0) {
|
|
39
|
+
dequeue();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return originalLength;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// --------------------------------------------------------------------------
|
|
46
|
+
// Application class definition.
|
|
47
|
+
// --------------------------------------------------------------------------
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @class
|
|
51
|
+
* An application framework capable of managing render and configuration queues.
|
|
52
|
+
*
|
|
53
|
+
* @param {Object} config Application configuration.
|
|
54
|
+
*/
|
|
55
|
+
function Application (config) {
|
|
56
|
+
this.config = config;
|
|
57
|
+
this._readyQueue = [];
|
|
58
|
+
this._renderQueue = [];
|
|
59
|
+
this._configQueue = [];
|
|
60
|
+
this._promise = new Promise(function (resolve, reject) {
|
|
61
|
+
this._resolve = resolve;
|
|
62
|
+
this._reject = reject;
|
|
63
|
+
}.bind(this));
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* A method that can be called before the "real" application is
|
|
69
|
+
* loaded; its job is to queue the ready callbacks that are passed,
|
|
70
|
+
* so that they can be called when the real app arrives.
|
|
71
|
+
*/
|
|
72
|
+
Application.prototype.ready = function (callback) {
|
|
73
|
+
if (typeof callback === 'function') {
|
|
74
|
+
this._readyQueue.push(callback);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return this._promise;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A method that can be called before the "real" application is
|
|
82
|
+
* loaded; its job is to queue the render configuration objects that
|
|
83
|
+
* are passed, so that they can be handled when the real app arrives.
|
|
84
|
+
*/
|
|
85
|
+
Application.prototype.render = function (config) {
|
|
86
|
+
return this._renderQueue.push(config);
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* A method that can be called before the "real" application is
|
|
91
|
+
* loaded; its job is to queue the applicaiton configuration objects
|
|
92
|
+
* that are passed, so that they can be handled when the real app
|
|
93
|
+
* arrives.
|
|
94
|
+
*/
|
|
95
|
+
Application.prototype.configure = function (config) {
|
|
96
|
+
return this._configQueue.push(config);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The function to be passed to our processQueue function when processing
|
|
101
|
+
* ready callbacks below.
|
|
102
|
+
*/
|
|
103
|
+
var processReadyFn = function (err, callback) {
|
|
104
|
+
if (typeof callback === 'function') {
|
|
105
|
+
callback(err, this);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* To be called by the real application once it is loaded and its API
|
|
111
|
+
* is fully available to consumers. It processes any passed callbacks as a
|
|
112
|
+
* queue, then redefines the ready method to process incoming callbacks
|
|
113
|
+
* immediately. If the current runtime also supports Promises, this will
|
|
114
|
+
* return a Promise handle that can be used to queue up additional callbacks.
|
|
115
|
+
*
|
|
116
|
+
* @param [Function] callback An optional function to be queued for
|
|
117
|
+
* processing. Any queued callbacks will be processed prior to resolving the
|
|
118
|
+
* Promise, if Promises are supported.
|
|
119
|
+
* @return {Promise} A Promise handle to use for additional callbacks.
|
|
120
|
+
*/
|
|
121
|
+
Application.prototype.processReady = function (err) {
|
|
122
|
+
var processReady = processReadyFn.bind(this, err);
|
|
123
|
+
processQueue.call(this, this._readyQueue, processReady, true);
|
|
124
|
+
|
|
125
|
+
this._readyQueue.push = processReady;
|
|
126
|
+
|
|
127
|
+
this.ready = function (callback) {
|
|
128
|
+
processReady(callback);
|
|
129
|
+
return this._promise;
|
|
130
|
+
};
|
|
131
|
+
if (err) {
|
|
132
|
+
this._reject(err);
|
|
133
|
+
}
|
|
134
|
+
else {
|
|
135
|
+
this._resolve(this);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* To be called by the real application once it is ready to handle
|
|
141
|
+
* queued render calls. It processes the queue using the provided
|
|
142
|
+
* handler, then redefines the render method using the provided handler.
|
|
143
|
+
*
|
|
144
|
+
* @param {Function} fn The function to handle the queued items; this function
|
|
145
|
+
* will also effectively replace the render method.
|
|
146
|
+
* @return {Number} The number of items that were queued.
|
|
147
|
+
*/
|
|
148
|
+
Application.prototype.processQueue = function (fn) {
|
|
149
|
+
processQueue.call(this, this._renderQueue, fn);
|
|
150
|
+
this.render = this._renderQueue.push = fn;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* To be called by the real application once it is ready to handle
|
|
155
|
+
* queued configure calls. It processes the queue using the provided
|
|
156
|
+
* handler, then redefines the configure method using the provided handler.
|
|
157
|
+
*
|
|
158
|
+
* @param {Function} fn The function to handle the queued items; this function
|
|
159
|
+
* will also effectively replace the configure method.
|
|
160
|
+
* @return {Number} The number of items that were queued.
|
|
161
|
+
*/
|
|
162
|
+
Application.prototype.processConfig = function (fn) {
|
|
163
|
+
processQueue.call(this, this._configQueue, fn, true);
|
|
164
|
+
this.configure = this._configQueue.push = fn;
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
// --------------------------------------------------------------------------
|
|
168
|
+
// Exports class definition.
|
|
169
|
+
// --------------------------------------------------------------------------
|
|
170
|
+
|
|
171
|
+
module.exports = Application;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# Body
|
|
2
|
+
|
|
3
|
+
This is a shorthand method for accessing the `<body>` element within the
|
|
4
|
+
document. Not only is it shorter, it returns the current body of the current
|
|
5
|
+
context. Also useful in non-browser environments by overloading the function to
|
|
6
|
+
return an appropriate element.
|
|
7
|
+
|
|
8
|
+
## Usage
|
|
9
|
+
```javascript
|
|
10
|
+
var body = require('body');
|
|
11
|
+
|
|
12
|
+
var body = body();
|
|
13
|
+
```
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileOverview
|
|
3
|
+
* Provides a function to retrieve the body of the current document.
|
|
4
|
+
*
|
|
5
|
+
* https://dojotoolkit.org/reference-guide/1.7/dojo/body.html
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
// Imports
|
|
9
|
+
var global = require('../global');
|
|
10
|
+
|
|
11
|
+
var cachedBody;
|
|
12
|
+
|
|
13
|
+
module.exports = function () {
|
|
14
|
+
// In some browsers (<= IE8) it is possible to have your code execute before document.body has been populated.
|
|
15
|
+
// For that reason, we use a fallback of `getElementsByTagName` -- and because it is possible to have multiple
|
|
16
|
+
// body elements in one page, we specify the first element in the resulting array.
|
|
17
|
+
cachedBody = cachedBody || global.document.body || global.document.getElementsByTagName('body')[0];
|
|
18
|
+
return cachedBody;
|
|
19
|
+
};
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# BvFetch
|
|
2
|
+
|
|
3
|
+
The BvFetch module provides methods to cache duplicate API calls and interact with the cacheStorage
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
## The following methods are provided:
|
|
7
|
+
|
|
8
|
+
## BvFetch Parameters
|
|
9
|
+
`shouldCache (Function):` A function that takes the API response JSON as input and returns a boolean indicating whether to cache the response or not. This allows you to implement custom logic based on the response content. If caching is desired, the function should return true; otherwise, false.
|
|
10
|
+
`cacheName (String):` Optional. Specifies the name of the cache to be used. If not provided, the default cache name 'bvCache' will be used.
|
|
11
|
+
`cacheLimit (Integer)`: Optional. Specifies the cache size limit for the cache storage. Its value should be in MB. Default value is 10 MB.
|
|
12
|
+
`excludeHeaders (Array[String])`: Includes the list of headers that are excluded from the cacheKey
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
## bvFetchFunc Method Parameters
|
|
16
|
+
`url (String):` The URL of the API endpoint to fetch data from.
|
|
17
|
+
`options (Object):` Optional request options such as headers, method, etc., as supported by the Fetch API.
|
|
18
|
+
|
|
19
|
+
## bvFetchFunc Return Value
|
|
20
|
+
`Promise<Response>:` A promise that resolves to the API response. If the response is cached, it returns the cached response. Otherwise, it fetches data from the API endpoint, caches the response according to the caching logic, and returns the fetched response.
|
|
21
|
+
|
|
22
|
+
## generateCacheKey Method
|
|
23
|
+
|
|
24
|
+
Generates a cache key as a Request object for a given URL and options, filtering out any headers specified in `excludeHeaders`.
|
|
25
|
+
|
|
26
|
+
**Parameters:**
|
|
27
|
+
- `url (String)`: The URL of the API endpoint.
|
|
28
|
+
- `options (Object)`: *(Optional)* Request options (e.g., headers, method, etc.).
|
|
29
|
+
|
|
30
|
+
**Returns:**
|
|
31
|
+
`Request`: The generated Request object to be used as a cache key.
|
|
32
|
+
|
|
33
|
+
## retrievecachedRequests Method
|
|
34
|
+
Retrieves cached Requests from the cache storage associated with the provided cache name.
|
|
35
|
+
## retrievecachedRequests Parameters
|
|
36
|
+
This method takes no parameters.
|
|
37
|
+
## retrievecachedRequests Return Value
|
|
38
|
+
`void:` This method does not return anything.
|
|
39
|
+
|
|
40
|
+
## fetchDataAndCache Method
|
|
41
|
+
Fetches data from the specified URL, caches the response, and returns the response.
|
|
42
|
+
## Parameters
|
|
43
|
+
`url (String):` The URL from which to fetch data.
|
|
44
|
+
`options (Object):` Optional request options such as headers, method, etc., as supported by the
|
|
45
|
+
Fetch API.
|
|
46
|
+
`cacheKey (String):`
|
|
47
|
+
The cache key associated with the fetched data.
|
|
48
|
+
## Return Value
|
|
49
|
+
`Promise<Response>:` A promise that resolves to the fetched response.
|
|
50
|
+
|
|
51
|
+
## fetchFromCache Method
|
|
52
|
+
Function to fetch data from cache.
|
|
53
|
+
## Parameters
|
|
54
|
+
`cacheKey (String):` The cache key to fetch data from the cache.
|
|
55
|
+
## Return Value
|
|
56
|
+
Promise<Response|null>: A Promise that resolves with a Response object if the data is found in cache, or null if the data is not cached or expired.
|
|
57
|
+
|
|
58
|
+
## cacheData Method
|
|
59
|
+
Caches the provided response with the specified cache key if it meets the criteria for caching.
|
|
60
|
+
## Parameters
|
|
61
|
+
`response (Response):` The response object to be cached.
|
|
62
|
+
`cacheKey (String):` The cache key associated with the response.
|
|
63
|
+
## Return Value
|
|
64
|
+
`void:` This method does not return anything.
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
## flushCache Method Parameters
|
|
68
|
+
This method takes no parameters.
|
|
69
|
+
## flushCache Return Value
|
|
70
|
+
`Promise<void>:` A promise indicating the completion of cache flush operation.
|
|
71
|
+
|
|
72
|
+
## manageCache Method
|
|
73
|
+
Manages the cache by deleting expired cache entries and maintaining the cache size limit.
|
|
74
|
+
## Parameters
|
|
75
|
+
This method takes no parameters.
|
|
76
|
+
## Return Value
|
|
77
|
+
`void:` This method does not return anything.
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
## Usage with of `BvFetch`:
|
|
81
|
+
|
|
82
|
+
```js
|
|
83
|
+
var BvFetch = require('bv-ui-core/lib/bvFetch')
|
|
84
|
+
|
|
85
|
+
// Initialize BV Fetch instance
|
|
86
|
+
const bvFetch = new BVFetch({
|
|
87
|
+
shouldCache: function(responseJson) => { /* return true or false */ }, // optional, callback function to check if the response is cachable
|
|
88
|
+
cacheName: "bvCache" // optional, default is "bvCache"
|
|
89
|
+
cacheLimit: 10, // optional but default is 10
|
|
90
|
+
excludeHeaders: [], // optional, list of headers to be excluded from cache
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Make API calls using bvFetchFunc method
|
|
94
|
+
bvFetch.bvFetchFunc('https://api.example.com/data')
|
|
95
|
+
.then(response => {
|
|
96
|
+
// Handle response
|
|
97
|
+
})
|
|
98
|
+
.catch(error => {
|
|
99
|
+
// Handle error
|
|
100
|
+
});
|
|
101
|
+
```
|