Admin on REST how to set path for list elements

2019-07-25 10:46发布

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

2条回答
孤傲高冷的网名
2楼-- · 2019-07-25 10:50

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

查看更多
爷、活的狠高调
3楼-- · 2019-07-25 10:51

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

查看更多
登录 后发表回答