node-imgur icon indicating copy to clipboard operation
node-imgur copied to clipboard

[bug] `imgur.saveClient` not working as per docs

Open swarajpure opened this issue 5 years ago • 6 comments

As per the documentation:

// Setting
imgur.setClientId('aCs53GSs4tga0ikp');

// Getting
imgur.getClientId();

// Saving to disk. Returns a promise.
// NOTE: path is optional. Defaults to ~/.imgur
imgur
  .saveClientId(path)
  .then(() => {
    console.log('Saved.');
  })
  .catch((err) => {
    console.log(err.message);
  });

If we continue just like this, we get the error:

Error: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received undefined

Reason:

  • imgur.saveClientId function expects two arguments: clientId and path and we're passing only one that too wrong one. Why wrong? clientId is supposed to be the first argument and then path

What's the fix?

Short:

  • Edit the function call: saveClientId('aCs53GSs4tga0ikp', null)

Long:

  • Edit the function arguments of imgur.saveClientId to be an object and pass path as null Something like:
imgur.saveClientId = async ({ clientId, path = null }) => {
......
  • Then edit the function call to be saveClientId({ clientId: 'aCs53GSs4tga0ikp' })

I was struggling for some time with this issue and then finally after looking the library functions could I find the problem. Would be better if we can fix this. I know people can use the v2 but there will be people who would be using v1 as it's stable.

Thank you! 😄

swarajpure avatar Apr 05 '21 20:04 swarajpure

Once I get your approval @kaimallea I can work on this issue and send a PR :) Also, please let me know which way should we follow, the short/long fix?

swarajpure avatar Apr 06 '21 05:04 swarajpure

@swarajpure hey, thanks for the thorough issue! All PRs are welcome, would love for you to send in a PR fixing this. saveClientId would definitely work better if it saved a client id that was previously set with setClientId()

Out of curiousity, would it be easier for you if you passed in the client id from an environment variable, rather than saving it to disk? e.g., imgur.setClientId(process.env.IMGUR_CLIENTID);`. Also feel free to ignore my suggestion and send a PR. :)

kaimallea avatar Apr 07 '21 05:04 kaimallea

@swarajpure hey, thanks for the thorough issue! All PRs are welcome, would love for you to send in a PR fixing this. saveClientId would definitely work better if it saved a client id that was previously set with setClientId()

Thanks! Please let me know whether I should implement the long fix or the short fix. Modifying the library function might cause tests to break (if any). Just wanted to confirm once before I send in a PR 😉

Out of curiousity, would it be easier for you if you passed in the client id from an environment variable, rather than saving it to disk? e.g., imgur.setClientId(process.env.IMGUR_CLIENTID);`. Also feel free to ignore my suggestion and send a PR. :)

Yes, when I was trying it, I was loading the clientId from the environment variables only 🙂 I just copied the clientId from the README.md, just to maintain the way function calls are there in the README.md

swarajpure avatar Apr 07 '21 10:04 swarajpure

Oh, I see there is no test for saveClientId function.

I guess then modifying the library function saveClientId to take an object as parameters would be better (check the long fix in description). Let me know what you think 😄

swarajpure avatar Apr 07 '21 10:04 swarajpure

@swarajpure since v1.x is in maintenance/support mode, I'd prefer to limit the change to a fix/patch rather than a new feature or breaking change that would require a minor or major version bump

kaimallea avatar Apr 10 '21 19:04 kaimallea

:tada: This issue has been resolved in version 1.0.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

kaimallea avatar Apr 20 '21 15:04 kaimallea