Skip to content

Commit e267527

Browse files
authored
5.0.0 (#58)
* BREAKING CHANGE: URL verification logic now uses Node.js core APIs
1 parent 89df0a4 commit e267527

File tree

6 files changed

+50
-38
lines changed

6 files changed

+50
-38
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
## 4.1.0 (11/06/19)
1+
## 5.0.0 (14/06/19)
2+
* Return values from `asArray()` are now more intuitive & consitent
3+
* `asUrlString()` and `asUrlObject`now use the built-in `URL` class in Node.js
4+
to perform validation
5+
* README updated in accordance with changes listed above
6+
7+
## 4.1.0 (14/06/19)
28
* Add `asPortNumber()` function
39
* Update documentation structure
410

README.md

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ some functions to verify it satisfies our program's needs.
3535
```js
3636
const env = require('env-var');
3737

38+
// Or using import syntax:
39+
// import * as env from 'env-var'
40+
3841
const PASSWORD = env.get('DB_PASSWORD')
3942
// Throws an error if the DB_PASSWORD variable is not set (optional)
4043
.required()
@@ -176,15 +179,15 @@ app.listen(PORT)
176179

177180
#### convertFromBase64()
178181
Sometimes environment variables need to be encoded as base64. You can use this
179-
function to convert them before reading their value.
182+
function to convert them to UTF-8 strings before parsing them.
180183

181184
For example if we run the script script below, using the command `DB_PASSWORD=
182185
$(echo -n 'secret_password' | base64) node`, we'd get the following results:
183186

184187
```js
185188
console.log(process.env.DB_PASSWORD) // prints "c2VjcmV0X3Bhc3N3b3Jk"
186189

187-
// dbpass will contain the value "secret_password"
190+
// dbpass will contain the converted value of "secret_password"
188191
const dbpass = env.get('DB_PASSWORD').convertFromBase64().asString()
189192
```
190193

@@ -249,16 +252,22 @@ The same as _asJson_ but checks that the data is a JSON Object, e.g {a: 1}.
249252
#### asArray([delimiter: string])
250253
Reads an environment variable as a string, then splits it on each occurence of
251254
the specified _delimiter_. By default a comma is used as the delimiter. For
252-
example a var set to "1,2,3" would become ['1', '2', '3'].
255+
example a var set to "1,2,3" would become ['1', '2', '3']. Example outputs for
256+
specific values are:
257+
258+
* Reading `MY_ARRAY=''` results in `[]`
259+
* Reading `MY_ARRAY='1'` results in `['1']`
260+
* Reading `MY_ARRAY='1,2,3'` results in `['1', '2', '3']`
253261

254262
#### asUrlString()
255-
Verifies that the variable is a valid URL string and returns that string. Uses
256-
`is-url` to perform validation, so check that module for validation rules.
263+
Verifies that the variable is a valid URL string and returns the validated
264+
string. The validation is performed by passing the URL string to the
265+
[Node.js URL Constructor](https://nodejs.org/docs/latest/api/url.html).
257266

258267
#### asUrlObject()
259-
Verifies that the variable is a valid URL string, then parses it using
260-
`url.parse` from the Node.js core `url` module and returns the parsed Object.
261-
See the [Node.js docs](https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost) for more info
268+
Verifies that the variable is a valid URL string using the same method as
269+
`asUrlString()`, but instead returns the resulting URL instance. For details
270+
see the [Node.js URL docs](https://nodejs.org/docs/latest/api/url.html).
262271

263272
## Examples
264273

@@ -301,15 +310,6 @@ const commaArray = env.get('DASH_ARRAY').asArray('-');
301310
const enumVal = env.get('ENVIRONMENT').asEnum(['dev', 'test', 'live'])
302311
```
303312

304-
## Contributors
305-
* @caccialdo
306-
* @evanshortiss
307-
* @hhravn
308-
* @itavy
309-
* @MikeyBurkman
310-
* @pepakriz
311-
* @rmblstrp
312-
313313
## Contributing
314314
Contributions are welcomed. If you'd like to discuss an idea open an issue, or a
315315
PR with an initial implementation.
@@ -353,3 +353,13 @@ Once you've done that, add some unit tests and use it like so:
353353
// Uses your new function to ensure the SOME_NUMBER is the integer 0
354354
env.get('SOME_NUMBER').asNumberZero()
355355
```
356+
357+
## Contributors
358+
* @caccialdo
359+
* @evanshortiss
360+
* @gabrieloczkowski
361+
* @hhravn
362+
* @itavy
363+
* @MikeyBurkman
364+
* @pepakriz
365+
* @rmblstrp

lib/accessors/url-object.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
'use strict'
22

3-
const parseUrl = require('url').parse
4-
const asUrlString = require('./url-string')
3+
const URL = require('url').URL
4+
const asString = require('./string')
55

66
module.exports = function asUrlObject (raiseError, value) {
7-
return parseUrl(asUrlString(raiseError, value))
7+
const ret = asString(raiseError, value)
8+
9+
try {
10+
return new URL(ret)
11+
} catch (e) {
12+
raiseError('should be a valid URL')
13+
}
814
}

lib/accessors/url-string.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
11
'use strict'
22

3-
const isUrl = require('is-url')
4-
const asString = require('./string')
3+
const urlObject = require('./url-object')
54

65
module.exports = function (raiseError, value) {
7-
var ret = asString(raiseError, value)
8-
9-
if (!isUrl(ret)) {
10-
raiseError('should be a valid URL')
11-
}
12-
13-
return ret
6+
return urlObject(raiseError, value).toString()
147
}

package.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "env-var",
3-
"version": "4.1.0",
3+
"version": "5.0.0",
44
"description": "Verification, sanatization, and type coercion for environment variables in Node.js",
55
"main": "env-var.js",
66
"typings": "env-var.d.ts",
@@ -58,9 +58,6 @@
5858
"standard": "~12.0.1",
5959
"typescript": "~3.1.3"
6060
},
61-
"dependencies": {
62-
"is-url": "~1.2.2"
63-
},
6461
"engines": {
6562
"node": ">=6.0"
6663
}

test/index.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/* eslint-env mocha */
44

5-
var expect = require('chai').expect
5+
const expect = require('chai').expect
6+
const URL = require('url').URL
67

78
describe('env-var', function () {
89
// Some default env vars for tests
@@ -22,7 +23,7 @@ describe('env-var', function () {
2223
ARRAY_WITH_DELIMITER: 'value,',
2324
ARRAY_WITH_DELIMITER_PREFIX: ',value',
2425
DASH_ARRAY: '1-2-3',
25-
URL: 'http://google.com',
26+
URL: 'http://google.com/',
2627
ENUM: 'VALID'
2728
}
2829

@@ -107,8 +108,7 @@ describe('env-var', function () {
107108

108109
describe('#asUrlObject', function () {
109110
it('should return a url object', function () {
110-
expect(mod.get('URL').asUrlObject()).to.be.a('object')
111-
expect(mod.get('URL').asUrlObject()).to.deep.equal(require('url').parse(process.env.URL))
111+
expect(mod.get('URL').asUrlObject()).to.be.an.instanceOf(URL)
112112
})
113113

114114
it('should throw due to bad url', function () {

0 commit comments

Comments
 (0)