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?

Enfield Li
  • 1,952
  • 1
  • 8
  • 23

1 Answers1

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);
};
Enfield Li
  • 1,952
  • 1
  • 8
  • 23