Admin on REST how to set path for list elements

谁说胖子不能爱 提交于 2019-12-11 06:13:17

问题


I am new in Admin on REST. my response for /users is like on this:

{
  "status": 200,
  "response": {
    "data": [
      {
        "id": 298487355,
        "login": "000000000053"
      },
      ...
    ]
  }
  "error": "text error"
}

how can I set path for response: data: [...] to get list of users? Thanks


回答1:


you can customize your restClient. for example I choose to work with jsonServer so I have this in app.js:

import customRestClient from './customRestClient'
const App = () => (
    <Admin restClient={customRestClient('http://path.to.my.api/')}>

customRestClient actually is this file, I bring it to my source, adjust the imports.

this file is the point that your data comes to and goes from your app to your api.

so in the convertHTTPResponseToREST function you simply can check for resource and if it was users you can access the your data by json.response.data and return it in switch




回答2:


Thanks @pelak a lot. I just write code base on your answer.

In your custom restClient point out path to response data.

const convertHTTPResponseToREST = (response, type, resource, params) => {
    const { headers, json } = response;
    switch (type) {
        case GET_LIST:
        case GET_MANY_REFERENCE:
            if (!headers.has('content-range')) {
                throw new Error(
                    'The Content-Range header is missing in the HTTP Response. The simple REST client expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare Content-Range in the Access-Control-Expose-Headers header?'
                );
            }
            // IF you just want use with **users** resource.
            if(resource === 'users') {
                return {
                    data: json.result,
                    total: parseInt(
                        headers
                            .get('content-range')
                            .split('/')
                            .pop(),
                        10
                    ),
                };
            }
            return {
                data: json.result,
                total: parseInt(
                    headers
                        .get('content-range')
                        .split('/')
                        .pop(),
                    10
                ),
            };
        case CREATE:
            return { data: { ...params.data, id: json.id } };
        default:
            return { data: json };
    }
};

Keyword: list in child element, list nest



来源:https://stackoverflow.com/questions/43854617/admin-on-rest-how-to-set-path-for-list-elements

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!