Skip to content

Commit 314de35

Browse files
author
craigparra
committed
1.0.6 / 2021-07-27
================== * Remove node-fetch dependency, must inject/boot - @craigparra * Tidy tests and lint - @craigparra
1 parent 74d0d27 commit 314de35

14 files changed

+200
-90
lines changed

ConfigFactory.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,37 @@ const PrefixSelector = require('./PrefixSelector');
88
const URLResolver = require('./URLResolver');
99

1010
module.exports = class ConfigFactory {
11-
static getConfig(config, resolver) {
11+
static detectBrowser() {
12+
const browser = !(typeof window === 'undefined');
13+
return browser;
14+
}
15+
16+
static detectFetch(fetchArg) {
17+
let $fetch = null;
18+
if (!(typeof fetch === 'undefined')) {
19+
// eslint-disable-next-line no-undef
20+
$fetch = fetch;
21+
}
22+
if (global?.boot?.contexts?.root?.fetch) {
23+
$fetch = global.boot.contexts.root.fetch;
24+
}
25+
if (ConfigFactory.detectBrowser() && window?.fetch) {
26+
$fetch = window.fetch;
27+
}
28+
if (ConfigFactory.detectBrowser() && window?.boot?.contexts?.root?.fetch) {
29+
$fetch = window.boot.contexts.root.fetch;
30+
}
31+
$fetch = fetchArg || $fetch;
32+
return $fetch;
33+
}
34+
35+
static getConfig(config, resolver, fetchArg) {
1236
const placeHolderResolver = new PlaceHolderResolver(new PlaceHolderSelector());
1337
const jasyptDecryptor = new JasyptDecryptor(new PrefixSelector('enc.'));
14-
const urlResolver = new URLResolver(new PrefixSelector('url.'))
15-
const delegatingResolver = new DelegatingResolver([placeHolderResolver, jasyptDecryptor,urlResolver]);
38+
const urlResolver = new URLResolver(new PrefixSelector('url.'), ConfigFactory.detectFetch(fetchArg));
39+
const delegatingResolver = new DelegatingResolver(
40+
[placeHolderResolver, jasyptDecryptor, urlResolver],
41+
);
1642
const valueResolvingConfig = new ValueResolvingConfig(config || npmconfig,
1743
resolver || delegatingResolver);
1844

History.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
1.0.6 / 2021-07-27
2+
==================
3+
4+
* Remove node-fetch dependency, must inject/boot - @craigparra
5+
* Tidy tests and lint - @craigparra
6+
17
1.0.5 / 2021-07-21
28
==================
39

PrefixSelector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ module.exports = class PrefixSelector extends Selector {
1616
}
1717

1818
async asyncResolveValue(value) {
19-
return resolveValue(value);
19+
return this.resolveValue(value);
2020
}
2121
};

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ An extensible wrapper of the popular config package, supporting:
1313
- placeholder resolution (or variable expansion),
1414
- encrypted values (via jasypt)
1515
- default (or fallback) values,
16-
- and asynchronous url fetching.
16+
- and optional asynchronous url fetching.
1717

1818
<a name="usage">Usage</a>
1919
-------------------------
@@ -37,11 +37,16 @@ Config values that start with the prefix `enc.` will be decrypted with the
3737
[jasypt](https://www.npmjs.com/package/jasypt) package port, with the passphrase being
3838
sourced from the `process.env.NODE_CONFIG_PASSPHRASE` environment variable.
3939

40-
Config values that start with the prefix `url.` can be fetched and resolved asynchronously with the `fetch` function,
41-
and HTTP options can be specified as in the example config file.
40+
Optionally, config values that start with the prefix `url.` can be fetched and resolved asynchronously with the `fetch`
41+
function, and HTTP options can be specified as in the example config file. To avoid bundling `node-fetch`, you need to
42+
provide it by using `@alt-javascript/boot` to boot it into the global root context, where the package will detect it.
4243

4344
```javascript
45+
const {boot} = require('@alt-javascript/boot');
4446
const {config} = require('@alt-javascript/config');
47+
const fetch = require('node-fetch');
48+
49+
boot({config,fetch})
4550
const webdata = await config.fetch('pathToUrlPrefixedValue');
4651
```
4752
> :warning: While we have implemented asynchronous fetch from "the network", we discourage it.

Resolver.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,4 @@ module.exports = class Resolver {
1414
}
1515
return callback(values);
1616
}
17-
1817
};

URLResolver.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const _ = require('lodash');
2-
const fetch = require('node-fetch');
32
const Resolver = require('./Resolver');
43
const SelectiveResolver = require('./SelectiveResolver');
54
const PrefixSelector = require('./PrefixSelector');
65

76
module.exports = class URLResolver extends SelectiveResolver {
8-
constructor(selector) {
7+
constructor(selector, fetchArg) {
98
super(selector || (new PrefixSelector('url.')));
9+
this.$fetch = fetchArg;
1010
}
1111

1212
resolve(config) {
@@ -30,12 +30,14 @@ module.exports = class URLResolver extends SelectiveResolver {
3030
if (self.selector.matches(v)) {
3131
try {
3232
const selectedValue = self.selector.resolveValue(v);
33-
let urlPath = path.substring(0,path.lastIndexOf('.'));
34-
let method = parentConfig.has(`${urlPath}.method`) ? parentConfig.get(`${urlPath}.method`) : null;
35-
let authorization = parentConfig.has(`${urlPath}.authorization`) ? parentConfig.get(`${urlPath}.authorization`) : null;
36-
let body = parentConfig.has(`${urlPath}.body`) ? parentConfig.get(`${urlPath}.body`) : null;
37-
let headers = parentConfig.has(`${urlPath}.headers`) ? parentConfig.get(`${urlPath}.headers`) : null;
38-
const fetchedValue = await this.fetch(selectedValue,authorization,method,body,headers);
33+
const urlPath = path.substring(0, path.lastIndexOf('.'));
34+
const method = parentConfig.has(`${urlPath}.method`) ? parentConfig.get(`${urlPath}.method`) : null;
35+
const authorization = parentConfig.has(`${urlPath}.authorization`) ? parentConfig.get(`${urlPath}.authorization`) : null;
36+
const body = parentConfig.has(`${urlPath}.body`) ? parentConfig.get(`${urlPath}.body`) : null;
37+
const headers = parentConfig.has(`${urlPath}.headers`) ? parentConfig.get(`${urlPath}.headers`) : null;
38+
const fetchedValue = await this.fetch(
39+
selectedValue, authorization, method, body, headers,
40+
);
3941
return fetchedValue;
4042
} catch (e) {
4143
return v;
@@ -46,14 +48,16 @@ module.exports = class URLResolver extends SelectiveResolver {
4648
return resolvedConfig;
4749
}
4850

49-
async fetch (url,authorization,method,body,headers){
50-
let _headers = authorization ? {'authorization':authorization} : {};
51-
_.assignIn(_headers,headers)
52-
let opts = {method: method || 'get',headers: _headers}
53-
if (method && method?.toLowerCase()!='get' && method?.toLowerCase() != 'head'){
54-
_.assignIn(opts,JSON.stringify(body||{}))
51+
async fetch(url, authorization, method, body, headers) {
52+
if (!this.$fetch) {
53+
throw new Error('fetch is required');
5554
}
56-
return await fetch(url, opts).then(res => res.json());
55+
const $headers = authorization ? { authorization } : {};
56+
_.assignIn($headers, headers);
57+
const opts = { method: method || 'get', headers: $headers };
58+
if (method && method?.toLowerCase() !== 'get' && method?.toLowerCase() !== 'head') {
59+
_.assignIn(opts, JSON.stringify(body || {}));
60+
}
61+
return this.$fetch(url, opts).then((res) => res.json());
5762
}
58-
5963
};

ValueResolvingConfig.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ module.exports = class ValueResolvingConfig extends DelegatingConfig {
2121
return new ValueResolvingConfig(this.config, this.resolver, path).resolved_config;
2222
}
2323

24-
async fetch (path, defaultValue){
25-
let self = this;
24+
async fetch(path, defaultValue) {
25+
const self = this;
2626
if (defaultValue && this.has(path) === false) {
2727
return defaultValue;
2828
}
29-
let asyncConfig = new ValueResolvingConfig(this.config, this.resolver, path, true);
30-
return await asyncConfig.resolver.asyncResolve(asyncConfig.path == null ? asyncConfig : asyncConfig.config.get(asyncConfig.path),self,path);
29+
const asyncConfig = new ValueResolvingConfig(this.config, this.resolver, path, true);
30+
return asyncConfig.resolver.asyncResolve(
31+
asyncConfig.path == null ? asyncConfig : asyncConfig.config.get(asyncConfig.path),
32+
self, path,
33+
);
3134
}
3235
};

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ module.exports = {
2626
Selector,
2727
URLResolver,
2828
ValueResolvingConfig,
29-
config : ConfigFactory.getConfig()
29+
config: ConfigFactory.getConfig(),
3030
};

package-lock.json

Lines changed: 44 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@alt-javascript/config",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "An extensible wrapper of the popular config package, supporting placeholder resolution, encrypted values and url fetch.",
55
"author": "Craig Parravicini",
66
"keywords": [
@@ -42,10 +42,11 @@
4242
"dependencies": {
4343
"config": "^3.3.6",
4444
"jasypt": "^1.0.5",
45-
"lodash": "^4.17.21",
46-
"node-fetch": "^2.6.1"
45+
"lodash": "^4.17.21"
4746
},
4847
"devDependencies": {
48+
"@alt-javascript/boot": "^1.0.3",
49+
"@alt-javascript/logger": "^1.1.5",
4950
"@cucumber/cucumber": "^7.3.0",
5051
"babel-eslint": "^10.1.0",
5152
"chai": "^4.3.4",
@@ -56,6 +57,7 @@
5657
"eslint-plugin-cucumber": "^1.4.0",
5758
"eslint-plugin-import": "^2.23.4",
5859
"mocha": "^9.0.2",
60+
"node-fetch": "^2.6.1",
5961
"nyc": "^15.1.0",
6062
"run-script-os": "^1.1.6"
6163
}

0 commit comments

Comments
 (0)