REST data provider and nested routes

I'm quite heavily using nester routes, such as http://localhost:5173/organization/F16CC4C4-9C83-41E1-8A79-A8108A10DD98/campaigns with a REST API. I've already swizzled the data provider and can see that the API call hooks, such as getList: async ({ resource, pagination, filters, sorters, meta }) return the resource as organization/:organizationId/campaigns and there is organizationId property inside of meta. I’ve written a method that “fixes” the path, looks something like
function fixResoureceParams(resource: string, meta: MetaQuery | undefined): string {
return resource.replace(":organizationId", meta?.organizationId ?? "???");
}

getList: async ({ resource, pagination, filters, sorters, meta }) => {
resource = fixResoureceParams(resource, meta); // my change
const url = `${apiUrl}/${resource}`;
// ....
}
function fixResoureceParams(resource: string, meta: MetaQuery | undefined): string {
return resource.replace(":organizationId", meta?.organizationId ?? "???");
}

getList: async ({ resource, pagination, filters, sorters, meta }) => {
resource = fixResoureceParams(resource, meta); // my change
const url = `${apiUrl}/${resource}`;
// ....
}
so I got it to work. Just wanted to confirm that this is indeed not currently not supported by the REST API provider and that I’m not doing something wrong. Thanks!
2 Replies
kapa.ai
kapa.ai13mo ago
Hi @petr4990. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Yes, you're correct. The REST data provider in Refine doesn't directly support nested routes out of the box. Your approach to swizzle the data provider and modify the resource path based on the meta property is a valid workaround. Refine's data provider concept is designed to be flexible and customizable. The getList method you've modified is part of the data provider API, which is intended to be swizzled or overridden to accommodate different API structures and requirements. In your case, you've effectively created a dynamic resource path by replacing the :organizationId part of the resource string with the actual organizationId from the meta object. This is a smart way to handle nested routes in your API. For more information on swizzling and data providers in Refine, you can refer to the official documentation (https://refine.dev/docs/tutorial/understanding-dataprovider/swizzle/#what-is-swizzle). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai13mo ago
<@356390195681296387> kapa.ai is still learning and improving, please let us know how it did by reacting below

Did you find this page helpful?