2017-05-08 7 views
1

REST에서 Admin의 새로운 기능입니다. /users에 대한 내 대답은 이것에 같습니다 : 사용자의 목록을 가져 [...] : data :REST에 대한 관리자 목록 요소에 대한 경로를 설정하는 방법

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

가 어떻게 response에 대한 경로를 설정할 수 있습니까? 감사합니다.

답변

2

restClient를 사용자 정의 할 수 있습니다. 예를 들어 나는 내가 app.js이이 jsonServer 작업을 선택합니다

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

customRestClient

실제로 this 파일입니다, 내 소스에 그것을 가지고, 수입을 조정합니다.

이 파일은 귀하의 데이터가 귀하의 앱에서 귀하의 앱으로 전송되고 귀하의 API로 전송되는 지점입니다.

그래서 convertHTTPResponseToREST 기능에 당신은 단순히 resource를 확인 할 수 있으며이 있다면 users 당신은 json.response.data하여 데이터에 액세스하고 많은 @pelak switch

+0

덕분에, 내가 – Alexey

+0

예, 작동 할 것입니다. 나는 당신에게 @pelak 감사합니다. 너는 내 하루를 구한다. 이 기능에 대한 관리자 대 휴식의 개선에 대한 제안에 대해 어떻게 생각하나요, 아니면 그렇게 쉽고 가치가 없습니다. – Alexey

+0

내 하루를 저장합니다. –

0

감사에서 그것을 반환 할 수 있습니다. 답변에 코드 기반을 씁니다.

사용자 지정 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 }; 
    } 
}; 

키워드 : 자식 요소의 목록, 목록 둥지