Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
[ES6 - axios] - Copying static ES6 class
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="MayoMayn" data-source="post: 414057" data-attributes="member: 71840"><p>I'm trying to create a new <strong><a href="https://npmjs.com/package/axios" target="_blank">axios</a></strong> instance with default params assigned.</p><p>The baseURL is appended, but not the params:</p><p></p><p>I have a <strong>static</strong> HTTP class that I clone with options for an axios instance:</p><p>[Code]</p><p>static create(options: Object = {}): Object {</p><p> // See comment for class cloning <https://stackoverflow.com/a/41474987></p><p> const clone = Object.assign(Object.create(this), this)</p><p></p><p> return Object.defineProperty(clone, 'axios', {</p><p> enumerable: false,</p><p> configurable: false,</p><p> writable: false,</p><p> value: axios.create(options)</p><p> })</p><p> }</p><p>[/CODE]</p><p></p><p>And then I have a TMDbProvider class that uses the cloned class from HTTP.</p><p>[CODE]</p><p>constructor(state) {</p><p> this.http = HTTP.create({</p><p> baseURL: `${config.TMDB.API}/3`,</p><p> params: {</p><p> api_key: config.TMDB.KEY,</p><p> language: state.saved.prefs.iso4,</p><p> append_to_response: 'external_ids,videos'</p><p> }</p><p> })</p><p> }</p><p>[/CODE]</p><p></p><p>Example of a get function in the HTTP class:</p><p>[CODE]</p><p> static get(url: String, params: Object): Promise {</p><p> return this.axios.get(url, { params })</p><p> .then(res => res.data)</p><p> }</p><p>[/CODE]</p><p></p><p>As stated above in the summary, the default params doesn't get attached as shown below.</p><p>[code]/home/sentinel/git/venobo-redux/node_modules/axios/lib/adapters/xhr.js:178 GET https://api.themoviedb.org/3/movie/76341 401 (Unauthorized)[/code]</p><p></p><p>But if I <strong>console.log</strong> the <strong>clone.axios.defaults</strong> object then the params are assigned.</p><p></p><p>Is this happening because it's cloning a static class, instead of just cloning a new instance of it?</p></blockquote><p></p>
[QUOTE="MayoMayn, post: 414057, member: 71840"] I'm trying to create a new [B][URL='https://npmjs.com/package/axios']axios[/URL][/B] instance with default params assigned. The baseURL is appended, but not the params: I have a [B]static[/B] 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) }) } [/CODE] 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' } }) } [/CODE] 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) } [/CODE] 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)[/code] But if I [B]console.log[/B] the [B]clone.axios.defaults[/B] 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? [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
[ES6 - axios] - Copying static ES6 class
Top