React-admin: Unable to include credtials in dataProvider with typescript
Asked Answered
P

1

2

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?

Puff answered 15/6, 2022 at 20:33 Comment(0)
P
0

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);
};
Puff answered 15/6, 2022 at 21:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.