Skip to content

Commit

Permalink
added basic auth support back into rdt package
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Unit committed Jan 28, 2025
1 parent 6779061 commit 70865c5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 3 deletions.
2 changes: 2 additions & 0 deletions apps/supercatalog/src/config/elasticSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ export const elasticConfig: EndpointProps[] = [
{
name: "Super Catalog",
url: import.meta.env.VITE_ELASTICSEARCH_API_ENDPOINT,
user: import.meta.env.VITE_ELASTICSEARCH_API_USER,
pass: import.meta.env.VITE_ELASTICSEARCH_API_PASS,
fullTextFields: fieldConfig.fullTextFields,
fullTextHighlight: fieldConfig.fullTextHighlight,
resultBodyComponent: SingleResult,
Expand Down
6 changes: 6 additions & 0 deletions packages/rdt-search-ui/src/context/props/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,18 @@ interface OptionalSearchProps {

SearchHomeComponent?: React.FC<any>;
fixedFacets?: FixedFacetsProps[];

// For basic auth
user?: string;
pass?: string;
}

// Endpoints for search/endpoint urls
export interface EndpointBaseProps {
name: string;
url: string;
user?: string;
pass?: string;
}

export interface FixedFacetsProps {
Expand Down
6 changes: 6 additions & 0 deletions packages/rdt-search-ui/src/context/state/use-search/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export async function fetchSearchResult(
payload: Payload,
dispatch: any,
fixedFacets?: FixedFacetsProps[],
user?: string,
pass?: string,
) {
let fetchResponse: Response;
let response: any;
Expand All @@ -71,6 +73,10 @@ export async function fetchSearchResult(
method: "post",
headers: {
"Content-Type": "application/json",
// if user and pass are provided, add basic auth header
...(user && pass && {
Authorization: `Basic ${btoa(`${user}:${pass}`)}`,
}),
},
body: body,
});
Expand Down
6 changes: 3 additions & 3 deletions packages/rdt-search-ui/src/context/state/use-search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async function fetchSearchResultOnly(
dispatch: any,
) {
const { payload } = new ESRequest(searchState, searchProps, controllers);
const response = await fetchSearchResult(searchProps.url, payload, dispatch, searchProps.fixedFacets);
const response = await fetchSearchResult(searchProps.url, payload, dispatch, searchProps.fixedFacets, searchProps.user, searchProps.pass);
const result = ESResponseParser(response);
return result;
}
Expand All @@ -138,7 +138,7 @@ async function fetchSearchResultWithFacets(
searchProps,
controllers,
);
const response = await fetchSearchResult(searchProps.url, payload, dispatch, searchProps.fixedFacets);
const response = await fetchSearchResult(searchProps.url, payload, dispatch, searchProps.fixedFacets, searchProps.user, searchProps.pass);
const result = ESResponseWithFacetsParser(response, controllers);
return result;
}
Expand Down Expand Up @@ -172,7 +172,7 @@ async function fetchFacetValuesOnly(
payload.track_total_hits = false;

// Fetch the response
const response = await fetchSearchResult(searchProps.url, payload, dispatch, searchProps.fixedFacets);
const response = await fetchSearchResult(searchProps.url, payload, dispatch, searchProps.fixedFacets, searchProps.user, searchProps.pass);

// Parse only the facet values of the requested facet
let buckets = getBuckets(response, controller.ID);
Expand Down
2 changes: 2 additions & 0 deletions packages/rdt-search-ui/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ export const FacetedWrapper = ({
}
ResultBodyComponent={currentConfig.resultBodyComponent}
url={currentConfig.url}
user={currentConfig.user}
pass={currentConfig.pass}
shareRoutes={{
results: resultRoute,
dashboard: dashRoute,
Expand Down

0 comments on commit 70865c5

Please sign in to comment.