question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot enable destructuring assignment to axios methods when apply this library

See original GitHub issue

Hi,

I cannot use tough cookie jar support v 0.5.0 with axios 0.19.0. on Noe.js v10.13.0

I have tried the example you have in the README.md but it is not working for me:

const axios = require('axios').default;

const axiosCookieJarSupport = require('axios-cookiejar-support').default;
const tough = require('tough-cookie');

const instance = axios.create({
  // WARNING: This value will be ignored.
  jar: new tough.CookieJar(),
});

// Set directly after wrapping instance.
axiosCookieJarSupport(instance);
instance.defaults.jar = new tough.CookieJar();


const { get, post } = instance;

The code fails here:

https://github.com/3846masa/axios-cookiejar-support/blob/master/workspaces/axios-cookiejar-support/lib/index.mjs#L59

In the packaged lib its L73 in /lib/index.js:

Screen Shot 2019-07-01 at 11 28 48

EDIT:

fixed it by using instance instead of this in the anonymous function:

PR: https://github.com/3846masa/axios-cookiejar-support/pull/190

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
3846masacommented, Jul 3, 2019

First, this two codes have same behavior, so my code hasn’t to modify for using normally. (See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain for more details)

function Test {
}
Test.prototype.get = function () {
  console.log(this);
}
const test = new Test();
test.get();
function Test {
}
const test = new Test();
test.get = function () {
  console.log(this);
}
test.get();

However, axios will bind this to own prototype when creating instance. https://github.com/axios/axios/blob/v0.19.0/lib/axios.js#L15-L26

Because of that, you can apply destructuring assignment to axios methods in current version.

I know it, but this behavior isn’t basic prototype behavior, so I decide I won’t support destructuring assignment.


FYI: If you want to use destructuring assignment, you can write wrapper code to bind this as below.

import _axiosCookieJarSupport from 'axios-cookiejar-support';

function bind(instance) {
  Object.getOwnPropertyNames(instance).forEach((key) => {
    if (typeof instance[key] === 'function') {
      instance[key] = instance[key].bind(instance);
    }
  });
}

export function axiosCookieJarSupport(instance) {
  _axiosCookieJarSupport(instance);
  bind(instance);

  if (instance.create) {
    const create = instance.create;
    instance.create = function() {
      return bind(create.apply(this, arguments));
    };
  }

  return instance;
}
import axios from 'axios';
import { axiosCookieJarSupport } from './axios-cookiejar-support';

axiosCookieJarSupport(axios);

const { get } = axios;

get('/').then(console.log);
0reactions
akirilyukcommented, Jul 3, 2019

Thank you very much for this detailed answer!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't destructure data from axios post request response
this worked, and I was able to destructure the response. However my store seems to be adding undefined objects to the array even...
Read more >
Understanding Axios GET requests - LogRocket Blog
Learn how to make GET requests in Axios with query parameters and API keys, plus concurrent and HEAD requests and error handling.
Read more >
Cancel all axios requests in React's componentWillUnmount ...
Inside the constructor method, we assigned the cancel token source property to the source variable. source = axios.CancelToken.source();; Inside the fetchUser ...
Read more >
Boot files - Quasar Framework
Notice we are using the ES6 destructuring assignment . ... Configure aspects of libraries - An example would be to create an instance...
Read more >
Prevent Error with Default {} when Destructuring
When you use destructuring, make sure to set a default value of empty `{}` to prevent it from throwing an error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found