Skip to content

Commit 6b4d1a7

Browse files
authored
Remove histPerPage and rename limitPerRequest into paginationTotalResults (#37)
* Rename limitPerRequest into totalResults * Fix test:all yarn command * Remove hitsPerPage parameter * Update the playgrounds * Add precision in README
1 parent 462aa00 commit 6b4d1a7

File tree

7 files changed

+36
-21
lines changed

7 files changed

+36
-21
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,19 @@ const searchClient = instantMeiliSearch(
5252
"https://demos.meilisearch.com",
5353
"dc3fedaf922de8937fdea01f0a7d59557f1fd31832cb8440ce94231cfdde7f25",
5454
{
55-
hitsPerPage: 6, // default: 10
56-
limitPerRequest: 30, // default: 50
57-
placeholderSearch: false // default: true. Displays documents even when the query is empty.
55+
paginationTotalHits: 30, // default: 200.
56+
placeholderSearch: false // default: true.
5857
}
5958
);
6059
```
6160

61+
- `paginationTotalHits` (`200` by default): The total (and finite) number of hits you can browse during pagination.<br>
62+
It means, by default, you can browse `paginationTotalHits / hitsPerPage = 200 / 20 = 10` pages during pagination.<br>
63+
By default, `hitsPerPage` is set to `20` but it can be changed with [`InsantSearch.configure`](https://www.algolia.com/doc/api-reference/widgets/configure/js/#examples)<br>
64+
If the pagination widget is not set, this parameter is ignored.
65+
66+
- `placeholderSearch` (`true` by default). Displays documents even when the query is empty.
67+
6268
## Example with InstantSearch
6369

6470
The open-source [InstantSearch](https://www.algolia.com/doc/api-reference/widgets/js/) library powered by Algolia provides all the front-end tools you need to highly customize your search bar environment.

examples/express/tests/client.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ describe('Instant MeiliSearch Browser test', () => {
55

66
it('Should have generated a instant-meiisearch client and displayed', async () => {
77
await expect(page).toMatch(
8-
'{"client":{"cancelTokenSource":{"token":{"promise":{}}},"config":{"host":"http://localhost:7700","apiKey":"masterKey"}},"hitsPerPage":10,"limitPerRequest":50,"attributesToHighlight":["*"],"placeholderSearch":true}'
8+
'{"client":{"cancelTokenSource":{"token":{"promise":{}}},"config":{"host":"http://localhost:7700","apiKey":"masterKey"}},"attributesToHighlight":["*"],"paginationTotalHits":200,"placeholderSearch":true}'
99
)
1010
})
1111
})

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"test:demo:browser": "yarn --cwd examples/express && yarn --cwd examples/express test",
1010
"test:demo:nodejs": "node examples/node/index.js",
1111
"test:demo:esm": "yarn --cwd examples/esm && yarn --cwd examples/esm start",
12-
"test:all": "yarn test yarn test:demo",
12+
"test:all": "yarn test && yarn test:demo",
1313
"lint": "eslint --ext .js,.ts,.tsx .",
1414
"lint:fix": "eslint --ext .js,.ts,.tsx --fix .",
1515
"build": "rollup -c rollup.config.js && rollup --environment NODE_ENV:production -c rollup.config.js ",

playgrounds/react/src/App.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import instantMeiliSearch from "./instant-meilisearch.js";
1515

1616
const searchClient = instantMeiliSearch(
1717
"https://demos.meilisearch.com",
18-
"dc3fedaf922de8937fdea01f0a7d59557f1fd31832cb8440ce94231cfdde7f25"
18+
"dc3fedaf922de8937fdea01f0a7d59557f1fd31832cb8440ce94231cfdde7f25",
19+
{
20+
paginationTotalHits: 60
21+
}
1922
);
2023

2124
class App extends Component {
@@ -52,7 +55,7 @@ class App extends Component {
5255
<div className="right-panel">
5356
<SearchBox />
5457
<Hits hitComponent={Hit} />
55-
<Pagination showLast={true}/>
58+
<Pagination showLast={true} />
5659
</div>
5760
</InstantSearch>
5861
</div>

playgrounds/vue/src/App.vue

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
</ais-hits>
4242

4343
<ais-pagination :padding="4" />
44+
45+
<ais-configure
46+
:hits-per-page.camel="6"
47+
/>
4448
</div>
4549
</ais-instant-search>
4650
</div>
@@ -58,8 +62,7 @@ export default {
5862
"https://demos.meilisearch.com",
5963
"dc3fedaf922de8937fdea01f0a7d59557f1fd31832cb8440ce94231cfdde7f25",
6064
{
61-
hitsPerPage: 6,
62-
limitPerRequest: 100
65+
paginationTotalHits: 60
6366
}
6467
)
6568
};

src/index.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import { isString, removeUndefinedFromObject } from './utils.js'
44
export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
55
return {
66
client: new MeiliSearch({ host: hostUrl, apiKey: apiKey }),
7-
hitsPerPage: options.hitsPerPage || 10,
8-
limitPerRequest: options.limitPerRequest || 50,
97
attributesToHighlight: ['*'],
8+
paginationTotalHits: options.paginationTotalHits || 200,
109
placeholderSearch: options.placeholderSearch !== false, // true by default
1110

1211
transformToMeiliSearchParams: function (params) {
12+
const limit = this.pagination // if pagination widget is set, use paginationTotalHits as limit
13+
? this.paginationTotalHits
14+
: this.hitsPerPage
1315
const searchInput = {
1416
q: this.placeholderSearch && params.query === '' ? null : params.query,
1517
facetsDistribution: params.facets.length ? params.facets : undefined,
1618
facetFilters: params.facetFilters,
1719
attributesToHighlight: this.attributesToHighlight,
18-
limit: this.limitPerRequest,
20+
limit,
1921
}
2022
return removeUndefinedFromObject(searchInput)
2123
},
@@ -43,10 +45,8 @@ export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
4345
},
4446

4547
parseHits: function (meiliSearchHits, params) {
46-
if (params.page !== undefined) {
47-
// If there is a pagination widget set
48-
const hitsPerPage = this.hitsPerPage
49-
const start = params.page * hitsPerPage
48+
if (this.pagination) {
49+
const start = params.page * this.hitsPerPage
5050
meiliSearchHits = meiliSearchHits.splice(start, this.hitsPerPage)
5151
}
5252

@@ -65,7 +65,7 @@ export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
6565
},
6666

6767
paginationParams: function (hitsLength, params) {
68-
if (params.page !== undefined) {
68+
if (this.pagination) {
6969
const adjust = hitsLength % this.hitsPerPage === 0 ? 0 : 1
7070
const nbPages = Math.floor(hitsLength / this.hitsPerPage) + adjust
7171
return {
@@ -76,7 +76,6 @@ export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
7676
},
7777

7878
parseMeiliSearchResponse: function (indexUid, meiliSearchResponse, params) {
79-
this.hitsPerPage = params.hitsPerPage || this.hitsPerPage
8079
const {
8180
exhaustiveFacetsCount,
8281
exhaustiveNbHits,
@@ -106,8 +105,12 @@ export default function instantMeiliSearch(hostUrl, apiKey, options = {}) {
106105
},
107106

108107
search: async function (requests) {
108+
// Params got from InstantSearch
109+
const params = requests[0].params
110+
this.pagination = params.page !== undefined // If the pagination widget has been set
111+
this.hitsPerPage = params.hitsPerPage || 20 // 20 is the MeiliSearch's default limit value. It can be changed with `InsantSearch.configure`.
109112
// Gets information from IS and transforms it for MeiliSearch
110-
const searchInput = this.transformToMeiliSearchParams(requests[0].params)
113+
const searchInput = this.transformToMeiliSearchParams(params)
111114
const indexUid = requests[0].indexName
112115
// Executes the search with MeiliSearch
113116
const searchResponse = await this.client

tests/base.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import instantMeiliSearch from '../src/index'
22

33
test('Should test if instantMeiliSearch Client is created correctly', () => {
44
const client = instantMeiliSearch('http://localhost:7700', 'masterKey')
5-
expect(client.hitsPerPage).toBe(10)
6-
expect(client.limitPerRequest).toBe(50)
5+
expect(client.paginationTotalHits).toBe(200)
76
expect(client.attributesToHighlight[0]).toBe('*')
7+
expect(client.placeholderSearch).toBe(true)
88
})

0 commit comments

Comments
 (0)