@stolostron/multicluster-sdk
Version:
Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.
52 lines • 2 kB
JavaScript
;
/* Copyright Contributors to the Open Cluster Management project */
// Copyright (c) 2021 Red Hat, Inc.
// Copyright Contributors to the Open Cluster Management project
Object.defineProperty(exports, "__esModule", { value: true });
exports.useSearchClient = void 0;
const client_1 = require("@apollo/client");
const searchUtils_1 = require("./searchUtils");
const react_1 = require("react");
const useBackendURL_1 = require("../useBackendURL");
const csrfHeaderLink = new client_1.ApolloLink((operation, forward) => {
const csrfToken = (0, searchUtils_1.getCookie)('csrf-token');
if (csrfToken) {
operation.setContext(({ headers = {} }) => ({
headers: Object.assign(Object.assign({}, headers), { 'X-CSRFToken': csrfToken }),
}));
}
return forward(operation);
});
const useSearchClient = () => {
const [backendURL] = (0, useBackendURL_1.useBackendURL)();
const [searchClient, setSearchClient] = (0, react_1.useState)();
(0, react_1.useEffect)(() => {
if (!backendURL)
return;
const httpLink = new client_1.HttpLink({
uri: () => `${backendURL}/proxy/search`,
});
setSearchClient(new client_1.ApolloClient({
connectToDevTools: process.env.NODE_ENV === 'development',
link: (0, client_1.from)([csrfHeaderLink, httpLink]),
cache: new client_1.InMemoryCache(),
credentials: 'same-origin',
defaultOptions: {
watchQuery: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
query: {
fetchPolicy: 'network-only',
errorPolicy: 'all',
},
mutate: {
errorPolicy: 'all',
},
},
}));
}, [backendURL]);
return searchClient;
};
exports.useSearchClient = useSearchClient;
//# sourceMappingURL=search-client.js.map