dataProvider的“ react-admin”代理问题

时间:2018-06-28 00:32:09

标签: proxy dataprovider react-admin

我的构建在本地主机上运行。我让dataProvider使用https(即“显示”,“创建”)在所有标准视图中正常工作。我想从这样的函数中创建数据:

import { CREATE, GET_ONE, UPDATE } from 'react-admin';
import dataProviderFactory from '../dataProvider';

dataProviderFactory(
  process.env.REACT_APP_SERVER_HTTPS_URL
).then(dataProvider => {
  dataProvider(CREATE, 'batches', {
    data: {
      name: "test"
    }
  })
  .then(response => response.data)
  .then(data => {
    console.log('data', data);
  });
});

实体“批次”中的完全相同的POST从标准react-admin“创建”模板开始工作,但是使用此功能,它将失败。它似乎正在尝试代理“ localhost”,而不是维护根API URL。我是否还有其他方法可以从管理控制器中提取dataProvider,或者可以指定dataProvider的代理根网址?

dataProvier.js:

export default type => {
    return import('./rest').then(provider => provider.default);
};

rest.js:

import simpleRestProvider from 'ra-data-simple-rest';
import Constants from "../constants/Constants";
import { fetchUtils } from 'react-admin';
import SimpleRestClient from '../utils/SimpleRestClient';
import AppConfig from '../AppConfig';

const httpClient = (url, options = {}) => {
  if (!options.headers) {
    options.headers = new Headers();
  }
  if(!url.endsWith("/authenticate") && localStorage.getItem('token') !== null) {
    options.user = {
      authenticated: true,
      token: 'Bearer ' + localStorage.getItem('token')
    }
  }
  return fetchUtils.fetchJson(url, options);
}

// Use custom rest client. For fakeserver use react admin rest client
const restDataProvider = !AppConfig.useFakeServer ? SimpleRestClient(Constants.urls.apiBaseUrl, httpClient) :
  simpleRestProvider(Constants.urls.apiBaseUrl, httpClient);

export default (type, resource, params) =>
  new Promise(resolve => {
      if (!AppConfig.useFakeServer) {
        setTimeout(() => resolve(restDataProvider(type, resource, params)), 500);
      } else {
        resolve(restDataProvider(type, resource, params));
      }
    }
  );

0 个答案:

没有答案