Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Updated README and little code changes #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ To provide an simple interface to the masthead API within node.
1) Install the node module using the setup instructions below.
2) Import / Require the masthead
3) Call masthead.get() and receive a promise
```
import masthead from 'masthead';
```javascript
import masthead from 'node-masthead-injector';

masthead.get()
.then(function(assets) {
.then((assets) => {
console.log(assets);
})
.catch(function(error) {
.catch((error) => {
// An asset could not be retrieved
});
```
Expand All @@ -39,7 +39,7 @@ masthead.get()
##### Overriding the configuration

You can override these attributes by calling `masthead.setConfig()` before calling `masthead.get()`. Pass in an object to override individual attributes or the entire object.
```
```javascript
{
host: 'https://assets.sky.com/new',
siteArea: 'help-and-support',
Expand Down
30 changes: 10 additions & 20 deletions app/masthead.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,17 @@ const injector = {
},

_log: function(message) {
if (this._config.debug) {
console.log(message);
}
if (this._config.debug) console.log(message);
},

_requestAsset: function(asset) {
this._log('MASTHEAD - Requesting asset - ' + asset.path);

return Request(this._config.host + asset.path)
.then(response => {
asset.data = response;
return Request(this._config.host + asset.path).then(response => {
asset.data = response;

return asset;
});
return asset;
});
},

/**
Expand All @@ -73,11 +70,6 @@ const injector = {
* }
*/
setConfig: function(config) {
if (!config) {
this._config = this._defaultConfig;
return this._config;
}

this._config = Object.assign({},
this._defaultConfig,
config
Expand All @@ -102,20 +94,18 @@ const injector = {
* @return {Promise}
*/
get: function() {
var requests = [];
const requests = [];

this._init();

this._config.assets.forEach(item => {
requests.push(
this._requestAsset(item)
)
requests.push(this._requestAsset(item))
});

return Promise.all(requests)
.then(results => {
let assets = {};
let time = +(new Date());
const assets = {};
const time = +(new Date());
results.forEach(response => {
if (!assets[response.section]) {
assets[response.section] = '';
Expand All @@ -130,7 +120,7 @@ const injector = {
.catch(error => {
this._log('MASTHEAD - ====== Error ======');
this._log(`MASTHEAD - statusCode: ${error.statusCode}`);
this._log(`MASTHEAD - asset: ${error.options.uri}`);
this._log(`MASTHEAD - asset: ${(error.options || {}).uri}`);
this._log('MASTHEAD - ====== Error ======');
});

Expand Down
25 changes: 13 additions & 12 deletions build/masthead.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ var injector = {
}
});

console.log('MASTHEAD - Starting');
this._log('MASTHEAD - Starting');
},

_getConfig: function _getConfig() {
return this._config || this.setConfig();
},

_log: function _log(message) {
if (this._config.debug) {
console.log(message);
}
},

_requestAsset: function _requestAsset(asset) {
console.log('MASTHEAD - Requesting asset - ' + asset.path);
this._log('MASTHEAD - Requesting asset - ' + asset.path);

return (0, _requestPromise2['default'])(this._config.host + asset.path).then(function (response) {
asset.data = response;
Expand All @@ -78,11 +84,6 @@ var injector = {
* }
*/
setConfig: function setConfig(config) {
if (!config) {
this._config = this._defaultConfig;
return this._config;
}

this._config = Object.assign({}, this._defaultConfig, config);

return this._config;
Expand Down Expand Up @@ -125,13 +126,13 @@ var injector = {
assets[response.section] += response.data;
});

console.log('MASTHEAD - Assets received (' + (time - _this2._startTime) / 1000 + 's)');
_this2._log('MASTHEAD - Assets received (' + (time - _this2._startTime) / 1000 + 's)');
return assets;
})['catch'](function (error) {
console.log('MASTHEAD - ====== Error ======');
console.log('MASTHEAD - statusCode: ' + error.statusCode);
console.log('MASTHEAD - asset: ' + error.options.uri);
console.log('MASTHEAD - ====== Error ======');
_this2._log('MASTHEAD - ====== Error ======');
_this2._log('MASTHEAD - statusCode: ' + error.statusCode);
_this2._log('MASTHEAD - asset: ' + error.options.uri);
_this2._log('MASTHEAD - ====== Error ======');
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import masthead from './app/masthead.js';

masthead.get()
.then(function(assets) {
.then((assets) => {
console.log(assets);
});