@@ -36,7 +36,38 @@ export const getSearchResultsWithFacets = async (
3636 if ( Config . useResourceTemplateFixtures && hasFixtureResource ( query ) )
3737 return Promise . resolve ( resourceSearchResults ( query ) )
3838
39- const body = new URLSearchParams ( { q : query } )
39+ // Check if query is a full URL (for pagination links)
40+ let url
41+ const extractedOptions = { ...options }
42+
43+ if (
44+ query &&
45+ typeof query === "string" &&
46+ ( query . startsWith ( "http://" ) || query . startsWith ( "https://" ) )
47+ ) {
48+ url = query
49+ // Extract offset from URL query parameters
50+ try {
51+ const urlObj = new URL ( query )
52+ const offset = urlObj . searchParams . get ( "offset" )
53+ const limit = urlObj . searchParams . get ( "limit" )
54+ if ( offset !== null ) {
55+ extractedOptions . startOfRange = parseInt ( offset , 10 )
56+ }
57+ if ( limit !== null ) {
58+ extractedOptions . resultsPerPage = parseInt ( limit , 10 )
59+ }
60+ } catch ( e ) {
61+ // If URL parsing fails, continue with original options
62+ }
63+ } else if ( query ) {
64+ const body = new URLSearchParams ( { q : query } )
65+ url = `${ Config . searchHost } ${ Config . searchPath } ?${ body } `
66+ } else {
67+ // If query is null/undefined, construct URL without query parameter
68+ url = `${ Config . searchHost } ${ Config . searchPath } `
69+ }
70+
4071 // const termsFilters = []
4172 // if (options.typeFilter) {
4273 // termsFilters.push({
@@ -73,7 +104,7 @@ export const getSearchResultsWithFacets = async (
73104 // },
74105 // }
75106 // }
76- return fetchSearchResults ( body , keycloak )
107+ return fetchSearchResultsFromUrl ( url , keycloak , extractedOptions )
77108}
78109
79110export const getSearchResultsByUris = ( resourceUris ) => {
@@ -84,6 +115,8 @@ export const getSearchResultsByUris = (resourceUris) => {
84115 )
85116 return Promise . resolve ( resourceSearchResults ( resourceUris [ 0 ] ) [ 0 ] )
86117
118+ // This function appears to be for a different use case
119+ // For now, keeping it as-is since it uses a different body format
87120 const body = {
88121 query : {
89122 terms : {
@@ -92,45 +125,47 @@ export const getSearchResultsByUris = (resourceUris) => {
92125 } ,
93126 size : resourceUris . length ,
94127 }
95- return fetchSearchResults ( body ) . then ( ( results ) => results [ 0 ] )
128+ // TODO: This needs to be updated to work with the new API
129+ const url = `${ Config . searchHost } ${ Config . searchPath } `
130+ return fetchSearchResultsFromUrl ( url , null ) . then ( ( results ) => results [ 0 ] )
96131}
97132
98- const fetchSearchResults = ( body , keycloak ) => {
99- const url = `${ Config . searchHost } ${ Config . searchPath } ?${ body } `
100- return fetch ( url , {
133+ const fetchSearchResultsFromUrl = ( url , keycloak , extractedOptions ) => fetch ( url , {
101134 method : "GET" ,
102135 } )
103- . then ( ( resp ) => {
104- return resp . json ( )
105- } )
136+ . then ( ( resp ) => resp . json ( ) )
106137 . then ( ( json ) => {
107138 if ( json . error ) {
108139 return [
109140 {
110141 totalHits : 0 ,
111142 results : [ ] ,
112143 error : json . error . reason || json . error ,
144+ options : extractedOptions ,
113145 } ,
114146 undefined ,
115147 ]
116148 }
117- return [ hitsToResult ( json ) , aggregationsToResult ( json ) ]
149+ const result = hitsToResult ( json )
150+ // Add extracted options to result
151+ result . options = extractedOptions
152+ return [ result , aggregationsToResult ( json ) ]
118153 } )
119154 . catch ( ( err ) => [
120155 {
121156 totalHits : 0 ,
122157 results : [ ] ,
123158 error : err . toString ( ) ,
159+ options : extractedOptions ,
124160 } ,
125161 undefined ,
126162 ] )
127- }
128163
129164const hitsToResult = ( payload ) => {
130165 const results = [ ]
131166 payload . results . forEach ( ( hit ) => {
132167 const types = hit . data [ "@type" ]
133- let rdfTypes = [ ]
168+ const rdfTypes = [ ]
134169 if ( Array . isArray ( types ) ) {
135170 types . forEach ( ( type ) =>
136171 rdfTypes . push ( `http://id.loc.gov/ontologies/bibframe/${ type } ` )
@@ -145,21 +180,25 @@ const hitsToResult = (payload) => {
145180 // Handle array of titles
146181 if ( Array . isArray ( mainTitle ) ) {
147182 // Map each title to extract @value if it's an object, otherwise use as-is
148- const titles = mainTitle . map ( title => {
149- if ( title && typeof title === ' object' && ' @value' in title ) {
150- return title [ ' @value' ]
183+ const titles = mainTitle . map ( ( title ) => {
184+ if ( title && typeof title === " object" && " @value" in title ) {
185+ return title [ " @value" ]
151186 }
152187 return title
153188 } )
154189 // Join multiple titles with a separator
155- label = titles . join ( ' / ' )
156- } else if ( mainTitle && typeof mainTitle === 'object' && '@value' in mainTitle ) {
190+ label = titles . join ( " / " )
191+ } else if (
192+ mainTitle &&
193+ typeof mainTitle === "object" &&
194+ "@value" in mainTitle
195+ ) {
157196 // Handle single JSON-LD object
158- label = mainTitle [ ' @value' ]
197+ label = mainTitle [ " @value" ]
159198 }
160199 results . push ( {
161200 uri : hit . uri ,
162- label : label ,
201+ label,
163202 created : hit . created_at ,
164203 modified : hit . updated_at ,
165204 type : rdfTypes ,
@@ -168,8 +207,9 @@ const hitsToResult = (payload) => {
168207 } )
169208 } )
170209 return {
171- totalHits : results . length ,
172- results : results ,
210+ totalHits : payload . total ,
211+ results,
212+ links : payload . links ,
173213 }
174214}
175215
@@ -308,8 +348,8 @@ const templateModFromBlueCore = (hit) => {
308348 id : resourceId ,
309349 originalURI : hit . uri ,
310350 remark : resourceRemark ,
311- resourceLabel : resourceLabel ,
312- resourceURI : resourceURI ,
351+ resourceLabel,
352+ resourceURI,
313353 uri : bcURI ,
314354 }
315355}
0 commit comments