REST 管理员如何设置列表元素的路径

Admin on REST how to set path for list elements

我是 REST 管理员的新手。 我对 /users 的回复是这样的:

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

如何为 response: data: [...] 设置路径以获取用户列表? 谢谢

您可以自定义您的 restClient。例如我选择使用 jsonServer 所以我在 app.js:

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

customRestClient实际上是this文件,我把它带到我的源中,调整导入。

此文件是您的数据进出您的应用程序和 api 的点。

所以在 convertHTTPResponseToREST 函数中,您只需检查 resource,如果是 users,您可以通过以下方式访问您的数据 json.response.data 和 return 它在 switch

非常感谢@pelak。 我只是根据您的回答编写代码。

在您的自定义中 restClient 指出响应数据的路径。

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 };
    }
};

关键字:list in child element,list nest