React-Admin:无法在DateProvider中包含Credtials与Typescript

发布于 2025-02-07 22:34:51 字数 1133 浏览 2 评论 0原文

我正在尝试配置dataProvider以包含凭据/cookie,然后遵循官方回购指南将凭据发送到API 其他问题”

import * as ra_core from "ra-core";
import { fetchUtils } from "react-admin";
import simpleRestProvider from "ra-data-json-server";

const httpClient = (url: any, options?: ra_core.Options) => {
  if (options) { // Have to do a null check
    console.log("run options"); // It doesn't run from here
    options.credentials = "include";
  }

  console.log("run httpClient"); // It only runs here
  return fetchUtils.fetchJson(url, options);
};

const dataProvider= simpleRestProvider("http://localhost:3000", httpClient);

return <Admin dataProvider={dataProvider}>...</Admin>

-based-based-based-authentication-via-rest-api-in-react-admin/ 54727456 httpclient采用可选的选项,并且执行NULL检查完全跳过。有什么解决方案吗?

I'm trying to configure dataProvider to include credentials/cookies, and following official repo's guide on sending Credentials To The API and other question, I managed to produce a typescript version of it:

import * as ra_core from "ra-core";
import { fetchUtils } from "react-admin";
import simpleRestProvider from "ra-data-json-server";

const httpClient = (url: any, options?: ra_core.Options) => {
  if (options) { // Have to do a null check
    console.log("run options"); // It doesn't run from here
    options.credentials = "include";
  }

  console.log("run httpClient"); // It only runs here
  return fetchUtils.fetchJson(url, options);
};

const dataProvider= simpleRestProvider("http://localhost:3000", httpClient);

return <Admin dataProvider={dataProvider}>...</Admin>

Above code doesn't send cookies to server, as the httpClient takes an optional options, and performing null check skips it entirely. Is there any fix for this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆蝶 2025-02-14 22:34:51

解决方案:

需要将选项定义为空对象{},然后施放到ra_core.options

const httpClient = (url: any, options = {} as ra_core.Options) => { // <- here
  options.credentials = "include"; // typescript is happy now :)
  return fetchUtils.fetchJson(url, options);
};

Solution:

Need to define options as empty object { }, and cast to ra_core.Options:

const httpClient = (url: any, options = {} as ra_core.Options) => { // <- here
  options.credentials = "include"; // typescript is happy now :)
  return fetchUtils.fetchJson(url, options);
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文