3

admin-on-rest allows consuming of any JSON response by writing custom rest clients. The examples in the documentation are for consuming JSON from json-server project which is straightforward.

I was wondering how easy is it to consume this api in admin-on-rest with minor changes to restClient.

François Zaninotto
  • 7,068
  • 2
  • 35
  • 56
Chida
  • 421
  • 3
  • 11

3 Answers3

0

Okay - let us look into sources of admin on rest (file admin-on-rest/src/util/fetch.js) we are intrested in fetchJson method.

That method return fetch promise, in which it try to parse json in that code:

try {
  json = JSON.parse(body);
} catch (e) {
  // not json, no big deal
}

and then it returns that: return { status, headers, body, json };
But we have body in result and may reuse it or we may use parsed object in json

For your example we may do so (some code missed):

const httpClient = (url, options = {}) => {
    if (!options.headers) {
        options.headers = new Headers({ Accept: 'application/json' });
    }
    options.withCredentials = true;
        return fetchUtils.fetchJson(url, options).then(({status, headers, body, json}) => {
        json = json['results'] ? json['results'] : json;
        return {status, headers, body, json};
    });
}

So we just overrwrited json object by collection from 'results' in your schema in that line:
json = json['results'] ? json['results'] : json;
Now you may use that client in Admin

<Admin restClient={restClient}>
...
</Admin>

WARNING!!! That will affect to ALL requests of Admin. But you may add additional parameter. If you do not want to use json = json['results'] ? json['results'] : json; you may add additional parameter or check in method fetch

Muritiku
  • 222
  • 1
  • 7
0

I recommend you to take a look at https://github.com/dev-family/admiral It's just recently launched, but it's already proven to be a good thing. I think it should help you to solve you problems.

user1946758
  • 143
  • 2
  • 10
-1

if i am not mistaken, you want to have a rewrite of the url from (as example): https://marmelab.com/admin-on-rest-demo/#/customers/684 TO: https://marmelab.com/admin-on-rest-demo/?customers=684

you can rewrite the GET_ONE in restClient.js: case GET_ONE: url = ${apiUrl}/${resource}/${params.id}; TO: case GET_ONE: url = ${apiUrl}/?${resource}=${params.id};

THis will retreive the record via a paramter instead of the url part.