Skip to content

Commit 963617f

Browse files
Merge pull request #46 from recursivefunk/jra/get-ip
feat: getIp()
2 parents 2f54b9c + cd3220f commit 963617f

File tree

6 files changed

+84
-2
lines changed

6 files changed

+84
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,15 @@ const {
159159
} = env.getAWS({ region: 'region' });
160160
```
161161

162+
Fetch an IP Address
163+
164+
```javascript
165+
const validIP = env.getIp('MY_IP', '127.0.0.1');
166+
console.log(validIp); // 192.168.1.60
167+
const invalidIP = env.getIp('INVALID_IP');
168+
console.log(invalidIp); // null
169+
```
170+
162171
Fetch `URL` objects from url strings
163172

164173
```javascript
@@ -216,6 +225,7 @@ Why would one use `env.getUrl()` if one just wishes to grab the url string value
216225

217226
As of now, `http`, `redis` and `postgresql` are the only supported protocols. Other protocols will return `null`. I'm not against adding new protocol support, but these are the ones that seemed most obvious to me. If you want other protocols supported, I'd recommend making a PR. You may create an issue, but I can't guarantee when I'll get around to implementation.
218227

228+
219229
## Shortcut Methods
220230

221231
```javascript

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "good-env",
3-
"version": "7.1.4",
3+
"version": "7.2.0",
44
"description": "Better environment variable handling for Twelve-Factor node apps",
55
"main": "src/index.js",
66
"scripts": {

src/index.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
declare module "good-env" {
2+
/**
3+
* @description Fetches an IP address from the environment. If the value found under the specified key is not a valid IPv4
4+
* or IPv6 IP and there's no default value, null is returned. If a default value is provided and it is a valid IPv4 or IPv6
5+
* IP, the default value is retruned. If the default value is not valid, null is returned. This function won't return a value
6+
* that is not a valid IP address.
7+
* @param {string} key - A unique key that points to the IP address
8+
* @param {string} defaultVal - The default IP address to be used if the key isn't found
9+
* @returns
10+
*/
11+
export const getIP: (key: string, defaultVal?: string) => any;
212
/**
313
* @description Fetches three commonly used AWS environment variables - access key id, secret access key and region.
414
* Note: You can only pass in a default region. No defaults for access key id or access key will be honored. This also

src/index.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
2-
2+
const { isIP } = require('node:net');
33
const { URL } = require('node:url');
44
const { makeGoodUrl } = require('./lib/url-types');
55
const ok = x => !!x;
@@ -16,6 +16,30 @@ const validType = item => ['number', 'boolean', 'string'].includes(item);
1616

1717
module.exports = Object
1818
.create({
19+
/**
20+
* @description Fetches an IP address from the environment. If the value found under the specified key is not a valid IPv4
21+
* or IPv6 IP and there's no default value, null is returned. If a default value is provided and it is a valid IPv4 or IPv6
22+
* IP, the default value is retruned. If the default value is not valid, null is returned. This function won't return a value
23+
* that is not a valid IP address.
24+
* @param {string} key - A unique key that points to the IP address
25+
* @param {string} defaultVal - The default IP address to be used if the key isn't found
26+
* @returns
27+
*/
28+
getIP (key, defaultVal) {
29+
const strIP = this.get(key);
30+
31+
if (!strIP) {
32+
if (!isIP(defaultVal)) {
33+
return null;
34+
} else {
35+
return defaultVal;
36+
}
37+
}
38+
39+
if (isIP(strIP)) return strIP;
40+
if (isIP(defaultVal)) return defaultVal;
41+
return null;
42+
},
1943
/**
2044
* @description Fetches three commonly used AWS environment variables - access key id, secret access key and region.
2145
* Note: You can only pass in a default region. No defaults for access key id or access key will be honored. This also

test/test.env

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ UNSUPPORTED_URL=beep://baz.bop
1616
AWS_ACCESS_KEY_ID=exampleaccesskeyid
1717
AWS_SECRET_ACCESS_KEY=examplesecretaccesskey
1818
AWS_REGION=us-east-1
19+
VALID_IP=192.168.1.60
20+
INVALID_IP=nope

test/test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,42 @@ require('dotenv').config({ path: 'test/test.env' });
55
const test = require('tape');
66
const env = require('../src/index');
77

8+
test('it gets an IP address', (t) => {
9+
const ip = env.getIP('VALID_IP');
10+
t.equals(ip, '192.168.1.60');
11+
t.end();
12+
});
13+
14+
test('it gets a default IP address', (t) => {
15+
const ip = env.getIP('NO_IP_HERE', '127.0.0.1');
16+
t.equals(ip, '127.0.0.1');
17+
t.end();
18+
});
19+
20+
test('it does not return an invalid IP', (t) => {
21+
const ip = env.getIP('INVALID_IP');
22+
t.notOk(ip);
23+
t.end();
24+
});
25+
26+
test('it returns a valid default with an invalid key', (t) => {
27+
const ip = env.getIP('INVALID_IP', '127.0.0.1');
28+
t.equals(ip, '127.0.0.1');
29+
t.end();
30+
});
31+
32+
test('it does not return a non-existing IP value', (t) => {
33+
const ip = env.getIP('NO_IP_HERE');
34+
t.notOk(ip);
35+
t.end();
36+
});
37+
38+
test('it does not return an invalid default IP', (t) => {
39+
const ip = env.getIP('NO_IP_HERE', 'bleep');
40+
t.notOk(ip);
41+
t.end();
42+
});
43+
844
test('it gets AWS creds', (t) => {
945
const {
1046
awsKeyId,

0 commit comments

Comments
 (0)