[ES6 - axios] - Copying static ES6 class

MayoMayn

BestDev
Oct 18, 2016
1,423
683
I'm trying to create a new instance with default params assigned.
The baseURL is appended, but not the params:

I have a static HTTP class that I clone with options for an axios instance:
Code:
static create(options: Object = {}): Object {
    // See comment for class cloning <https://stackoverflow.com/a/41474987>
    const clone = Object.assign(Object.create(this), this)

    return Object.defineProperty(clone, 'axios', {
      enumerable: false,
      configurable: false,
      writable: false,
      value: axios.create(options)
    })
  }

And then I have a TMDbProvider class that uses the cloned class from HTTP.
Code:
constructor(state) {
    this.http = HTTP.create({
      baseURL: `${config.TMDB.API}/3`,
      params: {
        api_key: config.TMDB.KEY,
        language: state.saved.prefs.iso4,
        append_to_response: 'external_ids,videos'
      }
    })
  }

Example of a get function in the HTTP class:
Code:
  static get(url: String, params: Object): Promise {
    return this.axios.get(url, { params })
      .then(res => res.data)
  }

As stated above in the summary, the default params doesn't get attached as shown below.
Code:
/home/sentinel/git/venobo-redux/node_modules/axios/lib/adapters/xhr.js:178 GET https://api.themoviedb.org/3/movie/76341 401 (Unauthorized)

But if I console.log the clone.axios.defaults object then the params are assigned.

Is this happening because it's cloning a static class, instead of just cloning a new instance of it?
 

Users who are viewing this thread

Top