Is there a way to add a Response Header for `X-Total-Count` in the client in React Admin ? Without sending the header from the API

Viewed 1227

I'm building a RactAdmin app and I'm using the method getList. Even that my devtools are receiving the response list with a 200 status code, ReactAdmin is throwing an error that expects the X-Total-Count header in the response.

Anyone knows if there's a way to add that header in the client side before it gets to ReactAdmin?

1 Answers

You're using ra-data-json-server, right? You'll need to copy it to a local file and edit the area around line 50 that looks for that header

   return httpClient(url).then(({ headers, json }) => {
       if (!headers.has('x-total-count')) {
           throw new Error(
               'The X-Total-Count header is missing in the HTTP Response...'
           );
       }
       return {
           data: json,
           total: parseInt(
               headers.get('x-total-count').split('/').pop(),
               10
           ),
       }

at the very least you'll need to remove the if statement, and you'll need to do something about the total key in the return object

   return httpClient(url).then(({ headers, json }) => {
       // IF STATEMENT REMOVED
       return {
           data: json,
           total: ??, //either put a static number here or maybe remove it?
       }

you'll either need to put a number in there, or perhaps remove it entirely - i don't know what RA will do if the total key is not there at all. Either way this isn't really viable for a fully working app, because pagination will be messed up without an accurate total.

Related