diff --git a/src/responses/search-response-details.ts b/src/responses/search-response-details.ts index edc94fb5..c1ff1b26 100644 --- a/src/responses/search-response-details.ts +++ b/src/responses/search-response-details.ts @@ -11,6 +11,7 @@ import type { SearchHitSchema } from './search-hit-schema'; export interface SearchResponseBody { hits: SearchResponseHits; aggregations?: Record; + collection_titles?: Record; } /** @@ -52,6 +53,12 @@ export class SearchResponseDetails { */ aggregations?: Record; + /** + * A map from collection identifiers (those present on the hits/aggregations) + * to their titles. + */ + collectionTitles?: Record; + /** * The hit schema for this response */ @@ -78,6 +85,10 @@ export class SearchResponseDetails { {} as Record ); } + + if (body?.collection_titles) { + this.collectionTitles = body.collection_titles; + } } /** diff --git a/test/responses/search-response-details.test.ts b/test/responses/search-response-details.test.ts index 4867c1c7..5f7cdcf1 100644 --- a/test/responses/search-response-details.test.ts +++ b/test/responses/search-response-details.test.ts @@ -1,32 +1,39 @@ import { expect } from '@open-wc/testing'; +import { Aggregation } from '../../src/models/aggregation'; import { HitType } from '../../src/models/hit-types/hit'; import { ItemHit } from '../../src/models/hit-types/item-hit'; import { TextHit } from '../../src/models/hit-types/text-hit'; -import { SearchResponseDetails } from '../../src/responses/search-response-details'; +import { + SearchResponseBody, + SearchResponseDetails, +} from '../../src/responses/search-response-details'; -describe('SearchResponseDetails', () => { - it('constructs item hits', () => { - const responseBody = { - hits: { - total: 2, - returned: 2, - hits: [ - { - fields: { - identifier: 'foo', - mediatype: 'texts', - }, - }, - { - fields: { - identifier: 'bar', - collection: ['baz'], - }, - }, - ], +const responseBody: SearchResponseBody = { + hits: { + total: 2, + returned: 2, + hits: [ + { + fields: { + identifier: 'foo', + mediatype: 'texts', + }, }, - }; + { + fields: { + identifier: 'bar', + collection: ['baz'], + }, + }, + ], + }, + collection_titles: { + baz: 'Baz Collection', + }, +}; +describe('SearchResponseDetails', () => { + it('constructs item hits', () => { const responseSchema = { hit_type: 'item' as HitType, field_properties: {}, @@ -42,27 +49,6 @@ describe('SearchResponseDetails', () => { }); it('constructs text hits', () => { - const responseBody = { - hits: { - total: 2, - returned: 2, - hits: [ - { - fields: { - identifier: 'foo', - mediatype: 'texts', - }, - }, - { - fields: { - identifier: 'bar', - collection: ['baz'], - }, - }, - ], - }, - }; - const responseSchema = { hit_type: 'text' as HitType, field_properties: {}, @@ -76,4 +62,73 @@ describe('SearchResponseDetails', () => { expect(details.results[1].identifier).to.equal('bar'); expect(details.results[1].collection?.value).to.equal('baz'); }); + + it('includes aggregations', () => { + const aggsResponseBody: SearchResponseBody = { + hits: { + total: 0, + returned: 0, + hits: [], + }, + aggregations: { + foo: new Aggregation({ + buckets: [ + { + key: 'bar', + doc_count: 10, + }, + ], + }), + }, + }; + + const responseSchema = { + hit_type: 'item' as HitType, + field_properties: {}, + }; + + const details = new SearchResponseDetails(aggsResponseBody, responseSchema); + expect(details.results.length).to.equal(0); + expect(details.aggregations).to.deep.equal({ + foo: new Aggregation({ + buckets: [ + { + key: 'bar', + doc_count: 10, + }, + ], + }), + }); + }); + + it('provides access to collection titles map', () => { + const responseSchema = { + hit_type: 'item' as HitType, + field_properties: {}, + }; + + const details = new SearchResponseDetails(responseBody, responseSchema); + expect(details.results.length).to.equal(2); + expect(details.collectionTitles).to.deep.equal({ baz: 'Baz Collection' }); + }); + + it('collection titles map is optional', () => { + const responseBodyWithoutTitles = Object.assign( + {}, + responseBody + ) as SearchResponseBody; + delete responseBodyWithoutTitles.collection_titles; + + const responseSchema = { + hit_type: 'item' as HitType, + field_properties: {}, + }; + + const details = new SearchResponseDetails( + responseBodyWithoutTitles, + responseSchema + ); + expect(details.results.length).to.equal(2); + expect(details.collectionTitles).to.be.undefined; + }); });