UNPKG

@stolostron/multicluster-sdk

Version:

Provides extensions and APIs that dynamic plugins can use to leverage multicluster capabilities provided by Red Hat Advanced Cluster Management.

70 lines 2.61 kB
"use strict"; /* Copyright Contributors to the Open Cluster Management project */ Object.defineProperty(exports, "__esModule", { value: true }); exports.searchClient = void 0; const client_1 = require("@apollo/client"); const subscriptions_1 = require("@apollo/client/link/subscriptions"); const utilities_1 = require("@apollo/client/utilities"); const graphql_ws_1 = require("graphql-ws"); const constants_1 = require("../constants"); const searchUtils_1 = require("./searchUtils"); const httpLink = new client_1.HttpLink({ uri: () => `${constants_1.BACKEND_URL}/proxy/search`, }); /** WebSocket URL for GraphQL subscriptions (graphql-ws), aligned with {@link httpLink}. */ function getSearchWebSocketUrl() { const httpEndpoint = `${constants_1.BACKEND_URL}/proxy/search`; if (httpEndpoint.startsWith('http://')) { return httpEndpoint.replace(/^http/, 'ws'); } if (httpEndpoint.startsWith('https://')) { return httpEndpoint.replace(/^https/, 'wss'); } if (typeof window === 'undefined') { return 'ws://localhost/proxy/search'; } const url = new URL(httpEndpoint, window.location.origin); url.protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; return url.href; } const wsLink = new subscriptions_1.GraphQLWsLink((0, graphql_ws_1.createClient)({ url: () => getSearchWebSocketUrl(), lazy: true, })); const csrfHeaderLink = new client_1.ApolloLink((operation, forward) => { const csrfToken = (0, searchUtils_1.getCookie)('csrf-token'); if (csrfToken) { operation.setContext(({ headers = {} }) => ({ headers: { ...headers, 'X-CSRFToken': csrfToken, }, })); } return forward(operation); }); const httpChain = (0, client_1.from)([csrfHeaderLink, httpLink]); const link = (0, client_1.split)(({ query }) => { const definition = (0, utilities_1.getMainDefinition)(query); return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'; }, wsLink, httpChain); exports.searchClient = new client_1.ApolloClient({ connectToDevTools: process.env.NODE_ENV === 'development', link, cache: new client_1.InMemoryCache(), credentials: 'same-origin', defaultOptions: { watchQuery: { fetchPolicy: 'network-only', errorPolicy: 'all', }, query: { fetchPolicy: 'network-only', errorPolicy: 'all', }, mutate: { errorPolicy: 'all', }, }, }); //# sourceMappingURL=search-client.js.map