From 84c4560c4fa9862a9560bbc7c9431875086b8d84 Mon Sep 17 00:00:00 2001 From: Karl Haworth Date: Thu, 4 Apr 2024 10:23:32 -0400 Subject: [PATCH] use authcallback method instead Signed-off-by: Karl Haworth --- .../ApolloExplorerBrowser.tsx | 45 ++++++++++++++++++- .../ApolloExplorerPage/ApolloExplorerPage.tsx | 27 +++++------ 2 files changed, 54 insertions(+), 18 deletions(-) diff --git a/plugins/apollo-explorer/src/components/ApolloExplorerBrowser/ApolloExplorerBrowser.tsx b/plugins/apollo-explorer/src/components/ApolloExplorerBrowser/ApolloExplorerBrowser.tsx index 57f9eb7f8a..9f09ba9967 100644 --- a/plugins/apollo-explorer/src/components/ApolloExplorerBrowser/ApolloExplorerBrowser.tsx +++ b/plugins/apollo-explorer/src/components/ApolloExplorerBrowser/ApolloExplorerBrowser.tsx @@ -19,6 +19,8 @@ import { Divider, makeStyles, Tab, Tabs } from '@material-ui/core'; import { JSONObject } from '@apollo/explorer/src/helpers/types'; import { ApolloExplorer } from '@apollo/explorer/react'; import { Content } from '@backstage/core-components'; +import { ApiHolder } from '@backstage/core-plugin-api'; +import { HandleRequest } from '@apollo/explorer/src/helpers/postMessageRelayHelpers'; const useStyles = makeStyles(theme => ({ tabs: { @@ -53,9 +55,42 @@ export type ApolloEndpointProps = { type Props = { endpoints: ApolloEndpointProps[]; + authCallback?: () => Promise; + apiHolder?: ApiHolder; }; -export const ApolloExplorerBrowser = ({ endpoints }: Props) => { +export const handleAuthRequest = ({ + legacyIncludeCookies, + authCallback, +}: { + legacyIncludeCookies?: boolean; + authCallback: any; +}): HandleRequest => { + let cookies = {}; + if (legacyIncludeCookies) { + cookies = { credentials: 'include' }; + } else if (legacyIncludeCookies !== undefined) { + cookies = { credentials: 'omit' }; + } else { + cookies = {}; + } + + const handleRequestWithCookiePref: HandleRequest = async ( + endpointUrl, + options, + ) => + fetch(endpointUrl, { + ...options, + headers: { + ...options.headers, + Authorization: `Bearer ${await authCallback({})}`, + }, + ...cookies, + }); + return handleRequestWithCookiePref; +}; + +export const ApolloExplorerBrowser = ({ endpoints, authCallback }: Props) => { const classes = useStyles(); const [tabIndex, setTabIndex] = useState(0); @@ -76,6 +111,14 @@ export const ApolloExplorerBrowser = ({ endpoints }: Props) => { diff --git a/plugins/apollo-explorer/src/components/ApolloExplorerPage/ApolloExplorerPage.tsx b/plugins/apollo-explorer/src/components/ApolloExplorerPage/ApolloExplorerPage.tsx index 0a0ea0ccdc..250b304fac 100644 --- a/plugins/apollo-explorer/src/components/ApolloExplorerPage/ApolloExplorerPage.tsx +++ b/plugins/apollo-explorer/src/components/ApolloExplorerPage/ApolloExplorerPage.tsx @@ -19,9 +19,6 @@ import { Content, Header, Page } from '@backstage/core-components'; import { ApolloExplorerBrowser } from '../ApolloExplorerBrowser'; import { JSONObject } from '@apollo/explorer/src/helpers/types'; import { ApiHolder, useApiHolder } from '@backstage/core-plugin-api'; -import useAsync from 'react-use/lib/useAsync'; -import { CircularProgress } from '@material-ui/core'; -import { Alert } from '@material-ui/lab'; /** * Export types to be used with {@link @backstage/apollo-explorer#ApolloExplorerPage}. @@ -44,34 +41,30 @@ export type EndpointProps = { }; }; -type EndpointPropsCallback = (options: { +export type authCallback = (options: { apiHolder: ApiHolder; -}) => Promise; +}) => Promise; type Props = { title?: string | undefined; subtitle?: string | undefined; - endpoints: EndpointProps[] | EndpointPropsCallback; + endpoints: EndpointProps[]; + authCallback: authCallback; }; export const ApolloExplorerPage = (props: Props) => { - const { title, subtitle, endpoints } = props; - const apiHolder = useApiHolder(); + const { title, subtitle, endpoints, authCallback } = props; - const { value, loading, error } = useAsync(async () => { - if (typeof endpoints === 'function') { - return await endpoints({ apiHolder }); - } - return endpoints; - }, []); + const apiHolder = useApiHolder(); return (
- {loading && } - {error && {error?.message}} - {value && } + authCallback({ apiHolder })} + /> );