Admin in REST how to set path for list items

I'm new to Admin on REST. my answer for /users

looks like this:

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

      

how to set the path to response

: data

: [...]

to get a list of users? Thanks to

+1


source to share


2 answers


you can customize your restClient. for example i prefer 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 this file , I will bring it to my source, adjust the import.



This file is what your data is coming from your application to your api.

so in the function convertHTTPResponseToREST

you can just check resource

, and if there was one users

, you can access your data through json.response.data

and return it toswitch

+2


source


Thanks @pelak a lot. I am just writing code for your answer.

In your custom restClient provide the 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, list of nests

0


source







All Articles