Skip to content

[FEAT] useDataProvider hook should return the providers with their assigned types #7445

@Bugrabugra

Description

@Bugrabugra

Is your feature request related to a problem? Please describe.

useDataProvider hook doesn't return the custom dataProvider assigned as default with custom types

Describe alternatives you've considered

useDataProvider should return the default provider (or others) with their respected types. No casting should be necessary.

Additional context

  1. npm create refine-app@latest
  2. choose rest provider and include example pages
  3. open src/providers/data.ts and use this code instead
import type { BaseKey, DataProvider, GetOneParams } from "@refinedev/core";

const API_URL = "https://api.fake-rest.refine.dev";

export const dataProvider = {
  getOne: async ({ resource, id, meta }: {resource: string, id: BaseKey, meta?: {test?: string} & GetOneParams['meta']}) => {
    console.log(meta?.test)
    const response = await fetch(`${API_URL}/${resource}/${id}`);

    if (response.status < 200 || response.status > 299) throw response;

    const data = await response.json();

    return { data };
  },
  getApiUrl: () => API_URL,
  update: () => { throw new Error("Not implemented"); },
  getList: () => { throw new Error("Not implemented"); },
  create: () => { throw new Error("Not implemented"); },
  deleteOne: () => { throw new Error("Not implemented"); },
  /* ... */
} satisfies DataProvider;
  1. open src/pages/blog-posts/show.tsx
  2. under BlogPostShow, add this hook after useShow row (row 13)
  const dataProvider = useDataProvider();
  const provider = dataProvider()

  const fetch = async () => {
    const res = await provider.getOne({resource: "blog_posts", id: id ?? "", meta: {}})

    console.log(res)
  }
  1. inside meta, you should see "test" key but you can't cause useDataProvider() hook brings you the default provider but without its type (it returns provider with default DataProvider type). In addition to that, you can't use generic type to state the type of the custom provider. You can cast the type though but I think passing a generic type or if it is possible, automatically getting the type of the corresponding dataProvider passed to would be better.

I have tried directly assigning CustomDataProvider type instead of "satisfies" keyword, but it didn't helped either.

Describe the thing to improve

Add feature to use generic dataProvider type with useDataProvider hook or directly get the type from

<Refine dataProvider={dataProvider}>

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions