0

I'm using the Loopback REST Client for admin-on-rest successfully with no issues. It's easy to use and works very well for the standard CRUD operations, however I quickly found myself in the need of using non-conventional REST calls like the following:

http://www.remoteurl.com/api/getUsersWithRolesInfo

I also want in some situations to make use of Loopback's in-URL filters, like such:

http://www.remoteurl.com/api/Users?filter=[include][profile]&filter=[include][posts]

How can I achieve this with the <Resource /> component?

Initially I thought of modifying the REST client in order to map the above end points. However the REST client maps its calls to types like GET_ONE, GET_MANY, etc.. while what I want to map is an URL (like ../getUserWithRolesInfo).

Thanks for your help.

1 Answers1

0

You can pass a permanent filter with your List component as well as a one time filter with the ReferenceInput component. For more specific modifications to the call to your API you can use a RestWrapper that will intercept some of the calls to your API and pass the rest to the Loopback Rest Client.

https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload

The Loopback REST client does not account for the include filter. You will have to manually construct the URL query. Something like below. Most of this is from the AOR-Loopback Code as is the queryParameters function used below. You can find it all there and modify it to your needs.

function getListQueryConstructor(params, apiResource) {
  const page =  params.pagination.page
  const perPage = params.pagination.perPage
  const {field, order} = params.sort
  const query = {}
  if (params.filter.include) {
    query['include'] = params.filter.include
    delete params.filter.include
  }
  query['where'] = {...params.filter}
  if (field) {query['order'] = [field + ' ' + order]}
  if (perPage > 0) {
      query['limit'] = perPage;
      if (page >= 0) {
          query['skip'] = (page - 1) * perPage
      }
  }
  return (config.host + '/' + apiResource + '?' +  queryParameters({filter: JSON.stringify(query)}))
}
kunal pareek
  • 1,285
  • 10
  • 21
  • Thanks, I will try something like this and let you know. By the way, I know about being able to set the filter property of the component, but they are used in the where clause. The filter I'm talking about is a relational one that makes Loopback to lazy load the content of the related models (no where clause). – Chakir Mrabet Jul 07 '17 at 15:53
  • That's the include filter in the query right? The AOR loopback plugin does not account for include filters. Which is why you will have to bypass it completely and make the entire query from the Rest Wrapper. ^^ Code just adds the include filter. You can then set include in your resource request. – kunal pareek Jul 08 '17 at 07:53
  • But where do you I call this function that creates the required URL to the API? I understand what you are saying and doing above, but I don't see any property in the component where you can pass the generated URL directly? the Component does have a filter property, so the function above seems to be manipulating its query property, so if that is the place where I should put it, I would need to create a custom component, right? – Chakir Mrabet Jul 09 '17 at 16:04
  • OK got it. I've just forked the loopback rest client and did all the modifications. Working now. Thanks. – Chakir Mrabet Jul 09 '17 at 17:34
  • Thats one way to do it. You can also write a Rest Wrapper. Link in the docs above if you only have 1 or 2 special cases not covered by AOR Loopback – kunal pareek Jul 10 '17 at 06:22