Skip to content
This repository was archived by the owner on May 20, 2025. It is now read-only.

Create new opts object - #42

Closed
nordsimon wants to merge 1 commit into
silas:masterfrom
nordsimon:master
Closed

Create new opts object#42
nordsimon wants to merge 1 commit into
silas:masterfrom
nordsimon:master

Conversation

@nordsimon

Copy link
Copy Markdown

Hello

2 days ago i started to use this lib for consul and very quickly i ran into a weird error where papi was yelling at the baseUrl. After some digging i found out that your library is actually mutating the options object that one pass in. So when using the same options object twice in your codebase you get weird errors

example:

File 1

var config = require('./config')

var consul = require('consul')(config.consul);

File 2

var config = require('./config')

var consul = require('consul')(config.consul);

Here file 2 breaks on initialization since the the first constructor have mutated my config.consul object. This is easy to fix by just creating new object but it took me a while to figure out why papi was breaking. I also think it's good practice to handle options params as "immutable" to avoid side effects like this.

I added test to make sure the object is not referenced and updated the opts to use _.extend from user options params.

Not a major thing but this would have saved me some trouble so I hope you think it's a good idea

@silas silas closed this in db420b2 Sep 25, 2016
@silas

silas commented Sep 25, 2016

Copy link
Copy Markdown
Owner

Thanks for the PR and test!

I don't currently include lodash in the non-test dependencies, so I reworked it to use another function.

The fix has been pushed to npm in 0.26.0.

@nordsimon

Copy link
Copy Markdown
Author

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants