@internetarchive/search-service
Advanced tools
Comparing version 0.0.1-alpha.24 to 0.0.1-alpha.25
@@ -13,8 +13,6 @@ import { SearchResponseHeader } from './search-response-header'; | ||
* | ||
* @type {*} | ||
* @type {Record<string, any>} | ||
* @memberof SearchResponse | ||
*/ | ||
rawResponse: { | ||
[key: string]: any; | ||
}; | ||
rawResponse: Record<string, any>; | ||
/** | ||
@@ -21,0 +19,0 @@ * The resonse header |
@@ -6,5 +6,28 @@ export interface AggregateSearchParam { | ||
export declare class AggregateSearchParams { | ||
searchParams: [AggregateSearchParam]; | ||
constructor(searchParams: [AggregateSearchParam]); | ||
asSearchParams(): Record<string, AggregateSearchParam>[]; | ||
searchParams: AggregateSearchParam[]; | ||
constructor(searchParams: AggregateSearchParam[]); | ||
/** | ||
* Generates a query parameter from the given aggregate search params | ||
* | ||
* Example: | ||
* | ||
* [ | ||
* { | ||
* "terms": { | ||
* "field": "collection", | ||
* "size":10 | ||
* } | ||
* }, | ||
* { | ||
* "terms": { | ||
* "field": "subjectSorter", | ||
* "size": 10 | ||
* } | ||
* } | ||
* ] | ||
* | ||
* @returns {Record<string, AggregateSearchParam>[]} | ||
* @memberof AggregateSearchParams | ||
*/ | ||
get asSearchParams(): Record<string, AggregateSearchParam>[]; | ||
} | ||
@@ -35,2 +58,3 @@ export declare enum SortDirection { | ||
fields?: string[]; | ||
aggregations?: AggregateSearchParams; | ||
constructor(options: { | ||
@@ -42,2 +66,3 @@ query: string; | ||
fields?: string[]; | ||
aggregations?: AggregateSearchParams; | ||
}); | ||
@@ -44,0 +69,0 @@ /** |
@@ -5,3 +5,26 @@ export class AggregateSearchParams { | ||
} | ||
asSearchParams() { | ||
/** | ||
* Generates a query parameter from the given aggregate search params | ||
* | ||
* Example: | ||
* | ||
* [ | ||
* { | ||
* "terms": { | ||
* "field": "collection", | ||
* "size":10 | ||
* } | ||
* }, | ||
* { | ||
* "terms": { | ||
* "field": "subjectSorter", | ||
* "size": 10 | ||
* } | ||
* } | ||
* ] | ||
* | ||
* @returns {Record<string, AggregateSearchParam>[]} | ||
* @memberof AggregateSearchParams | ||
*/ | ||
get asSearchParams() { | ||
return this.searchParams.map(param => { | ||
@@ -43,2 +66,3 @@ return { | ||
this.fields = options.fields; | ||
this.aggregations = options.aggregations; | ||
} | ||
@@ -69,2 +93,7 @@ /** | ||
} | ||
if (this.aggregations) { | ||
const searchParams = this.aggregations.asSearchParams; | ||
const stringified = JSON.stringify(searchParams); | ||
params.append('user_aggs', stringified); | ||
} | ||
return params; | ||
@@ -71,0 +100,0 @@ } |
import { expect } from '@open-wc/testing'; | ||
import { SearchParams, SortDirection, SortParam } from '../src/search-params'; | ||
import { AggregateSearchParams, SearchParams, SortDirection, SortParam, } from '../src/search-params'; | ||
describe('SearchParams', () => { | ||
@@ -76,3 +76,24 @@ it('can be instantiated with just a query', async () => { | ||
}); | ||
it('properly generates a URLSearchParam with aggregations', async () => { | ||
const query = 'title:foo AND collection:bar'; | ||
const aggregations = new AggregateSearchParams([ | ||
{ | ||
field: 'foo', | ||
size: 10, | ||
}, | ||
{ | ||
field: 'bar', | ||
size: 7, | ||
}, | ||
]); | ||
const params = new SearchParams({ | ||
query, | ||
aggregations, | ||
}); | ||
const urlSearchParam = params.asUrlSearchParams; | ||
const queryAsString = urlSearchParam.toString(); | ||
const expected = 'q=title%3Afoo+AND+collection%3Abar&output=json&user_aggs=%5B%7B%22terms%22%3A%7B%22field%22%3A%22foo%22%2C%22size%22%3A10%7D%7D%2C%7B%22terms%22%3A%7B%22field%22%3A%22bar%22%2C%22size%22%3A7%7D%7D%5D'; | ||
expect(queryAsString).to.equal(expected); | ||
}); | ||
}); | ||
//# sourceMappingURL=search-params.test.js.map |
{ | ||
"name": "@internetarchive/search-service", | ||
"version": "0.0.1-alpha.24", | ||
"version": "0.0.1-alpha.25", | ||
"description": "A search service for the Internet Archive", | ||
@@ -5,0 +5,0 @@ "license": "AGPL-3.0-only", |
@@ -15,6 +15,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ | ||
* | ||
* @type {*} | ||
* @type {Record<string, any>} | ||
* @memberof SearchResponse | ||
*/ | ||
rawResponse: { [key: string]: any }; | ||
rawResponse: Record<string, any>; | ||
@@ -21,0 +21,0 @@ /** |
@@ -7,9 +7,32 @@ export interface AggregateSearchParam { | ||
export class AggregateSearchParams { | ||
searchParams: [AggregateSearchParam]; | ||
searchParams: AggregateSearchParam[]; | ||
constructor(searchParams: [AggregateSearchParam]) { | ||
constructor(searchParams: AggregateSearchParam[]) { | ||
this.searchParams = searchParams; | ||
} | ||
asSearchParams(): Record<string, AggregateSearchParam>[] { | ||
/** | ||
* Generates a query parameter from the given aggregate search params | ||
* | ||
* Example: | ||
* | ||
* [ | ||
* { | ||
* "terms": { | ||
* "field": "collection", | ||
* "size":10 | ||
* } | ||
* }, | ||
* { | ||
* "terms": { | ||
* "field": "subjectSorter", | ||
* "size": 10 | ||
* } | ||
* } | ||
* ] | ||
* | ||
* @returns {Record<string, AggregateSearchParam>[]} | ||
* @memberof AggregateSearchParams | ||
*/ | ||
get asSearchParams(): Record<string, AggregateSearchParam>[] { | ||
return this.searchParams.map(param => { | ||
@@ -61,2 +84,4 @@ return { | ||
aggregations?: AggregateSearchParams; | ||
constructor(options: { | ||
@@ -68,2 +93,3 @@ query: string; | ||
fields?: string[]; | ||
aggregations?: AggregateSearchParams; | ||
}) { | ||
@@ -75,2 +101,3 @@ this.query = options.query; | ||
this.fields = options.fields; | ||
this.aggregations = options.aggregations; | ||
} | ||
@@ -107,4 +134,10 @@ | ||
if (this.aggregations) { | ||
const searchParams = this.aggregations.asSearchParams; | ||
const stringified = JSON.stringify(searchParams); | ||
params.append('user_aggs', stringified); | ||
} | ||
return params; | ||
} | ||
} |
import { expect } from '@open-wc/testing'; | ||
import { SearchParams, SortDirection, SortParam } from '../src/search-params'; | ||
import { | ||
AggregateSearchParams, | ||
SearchParams, | ||
SortDirection, | ||
SortParam, | ||
} from '../src/search-params'; | ||
@@ -87,2 +92,25 @@ describe('SearchParams', () => { | ||
}); | ||
it('properly generates a URLSearchParam with aggregations', async () => { | ||
const query = 'title:foo AND collection:bar'; | ||
const aggregations = new AggregateSearchParams([ | ||
{ | ||
field: 'foo', | ||
size: 10, | ||
}, | ||
{ | ||
field: 'bar', | ||
size: 7, | ||
}, | ||
]); | ||
const params = new SearchParams({ | ||
query, | ||
aggregations, | ||
}); | ||
const urlSearchParam = params.asUrlSearchParams; | ||
const queryAsString = urlSearchParam.toString(); | ||
const expected = | ||
'q=title%3Afoo+AND+collection%3Abar&output=json&user_aggs=%5B%7B%22terms%22%3A%7B%22field%22%3A%22foo%22%2C%22size%22%3A10%7D%7D%2C%7B%22terms%22%3A%7B%22field%22%3A%22bar%22%2C%22size%22%3A7%7D%7D%5D'; | ||
expect(queryAsString).to.equal(expected); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
347238
4641