js-data-http icon indicating copy to clipboard operation
js-data-http copied to clipboard

axios complains about adapter field in config object

Open ismanf opened this issue 6 years ago • 1 comments

This code example on js-data-http guid simply does not work for me:

import { Container } from 'js-data';
import { HttpAdapter } from 'js-data-http-node';

const httpAdapter = new HttpAdapter({
	basePath: 'https://mydomain.com'
});
const store = new Container();

store.registerAdapter('http', httpAdapter, { 'default': true });

store.defineMapper('school');
store.defineMapper('student');

// GET /school/1
store.find('school', 1).then((school) => {
  console.log('school');
});

It throws error:

TypeError: adapter is not a function at dispatchRequest (/Users/ismail.niftaliev/Documents/demo/dummy_server/node_modules/axios/lib/core/dispatchRequest.js:59:10) at <anonymous> at process._tickCallback (internal/process/next_tick.js:189:7) at Function.Module.runMain (module.js:696:11) at startup (bootstrap_node.js:204:16) at bootstrap_node.js:625:3

The problem is in config object which you send to axios you set adapter: 'http' and axios first checks if config has an adapter then uses it if not uses default adapter and then calls it as a function, so while you set a string as a value for adapter it makes axios crazy.

Thanks!

ismanf avatar Dec 17 '18 10:12 ismanf

My solution was:

const httpAdapter = new HttpAdapter({
    basePath: '[some url]',
    http: (config) => {
        // axios will use it's default adapter
        config.adapter = null;
        return axios(config);
    }
});

ismanf avatar Dec 17 '18 10:12 ismanf