import { request , IRequestOptions , appendCustomParams , } from "@esri/arcgis-rest-request" ; import { IItem , IGroup , IUser } from "@esri/arcgis-rest-types" ; import { SearchQueryBuilder } from "./SearchQueryBuilder" ; import { getPortalUrl } from "../util/get-portal-url" ; import { ISearchOptions , ISearchGroupContentOptions , ISearchResult , } from "../util/search" ; export function genericSearch < T extends IItem | IGroup | IUser > ( search , searchType ) { let options ; if ( typeof search === "string" || search instanceof SearchQueryBuilder ) { options = { httpMethod : "GET" , params : { q : search , } , } ; } else { options = appendCustomParams < ISearchOptions > ( search , [ "q" , "num" , "start" , "sortField" , "sortOrder" , "searchUserAccess" , "searchUserName" , "filter" , "countFields" , "countSize" , "categories" , "categoryFilters" , ] , { httpMethod : "GET" , } ) ; } let path ; switch ( searchType ) { case "item" : path = "/search" ; break ; case "group" : path = "/community/groups" ; break ; case "groupContent" : if ( typeof search !== "string" && ! ( search instanceof SearchQueryBuilder ) && search . groupId ) { path = `/content/groups/${ search . groupId }/search` ; } else { return Promise . reject ( new Error ( "you must pass a `groupId` option to `searchGroupContent`" ) ) ; } break ; default : path = "/portals/self/users/search" ; break ; } const url = getPortalUrl ( options ) + path ; return request ( url , options ) . then ( ( r ) => { if ( r . nextStart && r . nextStart !== - 1 ) { r . nextPage = function ( ) { let newOptions ; if ( typeof search === "string" || search instanceof SearchQueryBuilder ) { newOptions = { q : search , start : r . nextStart , } ; } else { newOptions = search ; newOptions . start = r . nextStart ; } return genericSearch < T > ( newOptions , searchType ) ; } ; } return r ; } ) ; }