Skip to content

Commit

Permalink
fix(hub-search): searchContent() should return results as IHubContent[]
Browse files Browse the repository at this point in the history
affects: @esri/hub-search
  • Loading branch information
tomwayson committed Sep 24, 2021
1 parent 129ad95 commit 907e84f
Show file tree
Hide file tree
Showing 6 changed files with 296 additions and 272 deletions.
20 changes: 2 additions & 18 deletions packages/search/src/content/helpers/convert-hub-response.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UserSession } from "@esri/arcgis-rest-auth";
import { getProp } from "@esri/hub-common";
import { datasetToContent, getProp } from "@esri/hub-common";
import { ISearchParams } from "../../ago/params";
import {
IContentAggregations,
Expand All @@ -25,9 +25,7 @@ export function convertHubResponse(
response: any = { data: [], meta: {} },
defaultAuthentication?: UserSession
): IContentSearchResponse {
const results: any[] = response.data.map((d: Record<string, any>) =>
getAttributes(d)
);
const results = response.data.map(datasetToContent);
const { count, total, hasNext, query, aggregations } =
getResponseMetadata(response);
const next: (
Expand Down Expand Up @@ -133,17 +131,3 @@ function getResponseMetadata(response: any): {
aggregations,
};
}

function getAttributes(data: Record<string, any>) {
const attributes = getProp(data, "attributes");
/* istanbul ignore else */
if (attributes) {
Object.keys(PROP_MAP).map((key: string) => {
/* istanbul ignore else */
if (PROP_MAP[key]) {
attributes[key] = attributes[PROP_MAP[key]];
}
});
}
return attributes;
}
16 changes: 8 additions & 8 deletions packages/search/src/content/helpers/convert-portal-response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import {
IItem,
ISearchOptions,
ISearchResult,
searchItems
searchItems,
} from "@esri/arcgis-rest-portal";
import { UserSession } from "@esri/arcgis-rest-auth";
import {
IContentSearchResponse,
IContentAggregations
IContentAggregations,
} from "../../types/content";
import {
IAggregation,
IAggregationResult
IAggregationResult,
} from "../../util/aggregations/merge-aggregations";
import { cloneObject } from "@esri/hub-common";
import { cloneObject, itemToContent } from "@esri/hub-common";

/**
* Converts the response format returned by the Portal API to a common format
Expand All @@ -24,7 +24,7 @@ export function convertPortalResponse(
request: ISearchOptions,
response: ISearchResult<IItem>
): IContentSearchResponse {
const results: IItem[] = response.results;
const results = response.results.map(itemToContent);
const count: number = response.num;
const total: number = response.total;
const hasNext: boolean = response.nextStart > -1;
Expand All @@ -47,7 +47,7 @@ export function convertPortalResponse(
hasNext,
query,
aggregations,
next
next,
};
}

Expand All @@ -74,13 +74,13 @@ function mapCountAggregations(
const mappedAggs: IAggregation[] = agg.fieldValues.map(
(aggValue: Record<string, any>) => ({
label: aggValue.value,
value: aggValue.count
value: aggValue.count,
})
);

return {
fieldName: agg.fieldName,
aggregations: mappedAggs
aggregations: mappedAggs,
};
});
}
Expand Down
3 changes: 2 additions & 1 deletion packages/search/src/types/content.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { UserSession } from "@esri/arcgis-rest-auth";
import { IHubContent } from "@esri/hub-common";
import { IAggregationResult } from "../util/aggregations/merge-aggregations";
import {
IDateRange,
Expand Down Expand Up @@ -93,7 +94,7 @@ export interface IContentAggregations {
* a parameterized results list type of any. Includes query, paginated result count,
* and aggregations
*/
export interface IContentSearchResponse extends ISearchResponse<any> {
export interface IContentSearchResponse extends ISearchResponse<IHubContent> {
query: string;
count: number;
aggregations?: IContentAggregations;
Expand Down
Loading

0 comments on commit 907e84f

Please sign in to comment.