Sundar Rajan Muthuraj
Sundar Rajan Muthuraj

Reputation: 117

How to setup dynamic URLs for Resources in admin-on-rest?

I've a requirement where the below resources are accessed based on the type of user logged in.

R1: /mit/oss/12345/peers

R2: /mit/md/6879/ngrp

R1 should be accessible by an user with id: 12345. And R2 should be accessible by an user with id - 6879.

The question is - how to define Resource URLs with dynamic values (like: userId in the URLs) based on the user who is logged it. I am aware of aor-permissions library to switch the Menus based on user permission, but is it possible to define the resources themselves dynamically with ids in the URLs?

Upvotes: 2

Views: 462

Answers (2)

Jeremy Bunn
Jeremy Bunn

Reputation: 671

Here is a simple example of remapping the resource URL using a map.

import {simpleRestClient} from 'admin-on-rest';

// Update the remap table appropriately to map from a resource name to a different path
const remap = {
    "resource1" : "resource1Remapped",
    "releasepresets" : "productionintents/releasepresets"
}

const simpleRestClientWithResourceUrlRemap = (apiUrl) => {

    var client = simpleRestClient(apiUrl);

    return (type, resource, params) => {

        if (remap[resource]) {
            console.log('remapping resource from ' + resource + ' to ' + remap[resource]);
            resource = remap[resource];
        }
        return client(type, resource, params);
  }

}

export default (simpleRestClientWithResourceUrlRemap);

Instead of a simple remap, a function with logic could be used.

Upvotes: 0

kunal pareek
kunal pareek

Reputation: 1283

You can write a wrapper on your rest client that can intercept the call and dynamically generate the URL.

Basically decorate the rest client like in the docs here --> https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload

You can then check for a case like in below psuedocode

if (type === 'AOR_REST_TYPE' && resource === 'BASE_RESOURCE') {
    if (getUserFromLocalStorage === usr1) {
       url = url1
    } else {
       url = url2
    }
    options.method = 'GET';
    // other options       
    }

Upvotes: 1

Related Questions