Skip to content

Commit 8b6d13a

Browse files
committed
Bring back the tests utils
1 parent d6f5080 commit 8b6d13a

File tree

22 files changed

+208
-42
lines changed

22 files changed

+208
-42
lines changed

packages/autocomplete-client/README.md

Lines changed: 161 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525

2626
This library is the search client that you should use to make [Meilisearch](https://github.com/meilisearch/meilisearch) work with [autocomplete](https://github.com/algolia/autocomplete). Autocomplete, an open-source project developed by Algolia, is a JavaScript library that lets you quickly build an autocomplete experiences.
2727

28-
<div align="center">
29-
<!-- TODO: provide our own code-sandbox -->
30-
<a href="https://codesandbox.io/s/github/algolia/autocomplete/tree/next/examples/playground?file=/app.tsx">
31-
<img src="./media/autocomplete-example.png" alt="Screenshot" width="600px">
32-
</a>
33-
</div>
34-
3528
Since `autocomplete.js` provides the possibility to use a custom `data source`, we are able to plug into it. Nonetheless, it has been created by Algolia and thus some of its components only works with Algolia.
3629

3730

@@ -40,10 +33,9 @@ Since `autocomplete.js` provides the possibility to use a custom `data source`,
4033
- [📖 Documentation](#-documentation)
4134
- [🔧 Installation](#-installation)
4235
- [🎬 Usage](#-usage)
36+
- [🎬 Getting started](#-getting-started)
4337
- [💅 Customization](#-customization)
44-
- [🪡 Example with InstantSearch](#-example-with-instantsearch)
45-
- [🤖 Compatibility with Meilisearch and InstantSearch](#-compatibility-with-meilisearch-and-instantsearch)
46-
- [📜 API Resources](#-api-resources)
38+
- [🤖 Compatibility with Meilisearch and Autocomplete](#-compatibility-with-meilisearch-and-autocomplete)
4739
- [⚙️ Development Workflow and Contributing](#️-development-workflow-and-contributing)
4840

4941

@@ -67,13 +59,19 @@ To be able to use both, you need to [install `autocomplete`](https://www.algolia
6759
## 🎬 Usage
6860

6961
The Meilisearch Autocomplete client provides 2 methods:
70-
- `meilisearchAutocompleteClient`: the search client.
71-
- `getMeilisearchResults`: The data source handler.
62+
- `meilisearchAutocompleteClient({ host, url, options? })`: The search client.
63+
- `url`: The URL to your Meilisearch instance.
64+
- `apiKey`: A valid API key with enough rights to search. ⚠️ Avoid using the admin key or master key
65+
- `options`: Additional options. See [this section](#-customization)
66+
- `getMeilisearchResults(searchClient, queries)`: The data source handler.
67+
- `searchClient`: The client created with `meilisearchAutocompleteClient`
68+
- `queries`: An array of queries. See [this documentation](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/getAlgoliaResults/#param-queries-2) on what `queries` accepts.
7269

73-
## Getting started
7470

75-
In the following
71+
## 🎬 Getting started
7672

73+
To make `autocomplete` work with meilisearch, create the `autocompleteSearchClient` and provide it to the `getMeilisearchResults` method as the searchClient.
74+
The following code provides a basic working code example.
7775

7876
```js
7977
import { autocomplete } from '@algolia/autocomplete-js'
@@ -120,3 +118,152 @@ autocomplete({
120118
})
121119

122120
```
121+
122+
## 💅 Customization
123+
124+
The `options` field in the `meilisearchAutocompleteClient` function provides the possibility to alter the default behavior of the search.
125+
126+
- [`placeholderSearch`](#placeholder-search): Enable or disable placeholder search (default: `true`).
127+
- [`primaryKey`](#primary-key): Specify the primary key of your documents (default `undefined`).
128+
- [`keepZeroFacets`](#keep-zero-facets): Show the facets value even when they have 0 matches (default `false`).
129+
- [`matchingStrategy`](#matching-strategy): Determine the search strategy on words matching (default `last`).
130+
- [`requestConfig`](#request-config): Use custom request configurations.
131+
- ['httpClient'](#custom-http-client): Use a custom HTTP client.
132+
133+
```js
134+
const client = meilisearchAutocompleteClient({
135+
url: 'http://localhost:7700',
136+
apiKey: 'searchKey',
137+
options: {
138+
placeholderSearch: false,
139+
},
140+
})
141+
142+
```
143+
144+
### Placeholder Search
145+
146+
Placeholders search means showing results even when the search query is empty. By default it is `true`.
147+
When placeholder search is set to `false`, no results appears when the search box is empty.
148+
149+
```js
150+
{ placeholderSearch : true } // default true
151+
```
152+
153+
### Primary key
154+
155+
Specify the field in your documents containing the [unique identifier](https://docs.meilisearch.com/learn/core_concepts/documents.html#primary-field) (`undefined` by default). By adding this option, we avoid errors that are thrown in some environments. For example, In `React` particularly, this option removes the `Each child in a list should have a unique "key" prop` error.
156+
157+
```js
158+
{ primaryKey : 'id' } // default: undefined
159+
```
160+
161+
### Keep zero facets
162+
163+
`keepZeroFacets` set to `true` keeps the facets even when they have 0 matching documents (default `false`).
164+
165+
If in your autocomplete implementation you are showing the facet values distribution, same values may completely disapear when they have no matching documents in the current filtering state.
166+
167+
by setting this option to `true`, the facet values do not disapear and instead are given the distribution `0`.
168+
169+
With `keepZeroFacets` set to `true`:
170+
171+
genres:
172+
- [x] horror (2000)
173+
- [x] thriller (214)
174+
- [ ] comedy (0)
175+
176+
With `keepZeroFacets` set to `false`, `comedy` disapears:
177+
178+
genres:
179+
- [x] horror (2000)
180+
- [x] thriller (214)
181+
182+
```js
183+
{ keepZeroFacets : true } // default: false
184+
```
185+
186+
### Matching strategy
187+
188+
`matchingStrategy` gives you the possibility to choose how Meilisearch should handle the presence of multiple query words, see [documentation](https://docs.meilisearch.com/reference/api/search.html#matching-strategy).
189+
190+
For example, if your query is `hello world` by default Meilisearch returns documents containing either both `hello` and `world` or documents that only contain `hello`. This is the `last` strategy, where words are stripped from the right.
191+
The other strategy is `all`, where both `hello` and `world` **must** be present in a document for it to be returned.
192+
193+
194+
```js
195+
{
196+
matchingStrategy: 'all' // default last
197+
}
198+
```
199+
200+
### Request Config
201+
202+
You can provide a custom request configuration. Available field can be [found here](https://fetch.spec.whatwg.org/#requestinit).
203+
204+
for example, with custom headers.
205+
206+
```ts
207+
{
208+
requestConfig: {
209+
headers: {
210+
Authorization: AUTH_TOKEN
211+
},
212+
credentials: 'include'
213+
}
214+
}
215+
```
216+
217+
218+
### Custom HTTP client
219+
220+
You can use your own HTTP client, for example, with [`axios`](https://github.com/axios/axios).
221+
222+
```ts
223+
{
224+
httpClient: async (url, opts) => {
225+
const response = await $axios.request({
226+
url,
227+
data: opts?.body,
228+
headers: opts?.headers,
229+
method: (opts?.method?.toLocaleUpperCase() as Method) ?? 'GET'
230+
})
231+
return response.data
232+
}
233+
}
234+
```
235+
236+
## More Documentation
237+
238+
- The open-source `autocomplete` library is widely used and well documented in the [Algolia documentation](https://www.algolia.com/doc/ui-libraries/autocomplete/introduction/what-is-autocomplete/). It provides all the necessery information to set up your autocomplete experience.
239+
- The [Meilisearch documentation](https://docs.meilisearch.com/).
240+
241+
## 🤖 Compatibility with Meilisearch and Autocomplete
242+
243+
**Supported autocomplete versions**:
244+
245+
This package only guarantees the compatibility with the [version v1.x.x of Autocomplete](https://github.com/algolia/autocomplete). It may work with older or newer versions, but these are not tested nor officially supported at this time.
246+
247+
**API compatibility with `autocomplete`**
248+
Some `autocomplete` parameters are not working using the meilisearch autocomplete client.
249+
250+
- The autocomplete [insights] parameters (https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-js/autocomplete/#param-insights)
251+
- The [autocomplete-plugin-algolia-insights](https://www.algolia.com/doc/ui-libraries/autocomplete/api-reference/autocomplete-plugin-algolia-insights/) plugin
252+
253+
**Supported Meilisearch versions**:
254+
255+
This package only guarantees the compatibility with the [version v1.0.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v1.0.0).
256+
257+
**Node / NPM versions**:
258+
259+
- NodeJS >= 14 <= 18
260+
261+
## ⚙️ Development Workflow and Contributing
262+
263+
Any new contribution is more than welcome in this project!
264+
265+
If you want to know more about the development workflow or want to contribute, please visit our [contributing guidelines](../../CONTRIBUTING.md) for detailed instructions!
266+
267+
<hr>
268+
269+
**Meilisearch** provides and maintains many **SDKs and Integration tools** like this one. We want to provide everyone with an **amazing search experience for any kind of project**. If you want to contribute, make suggestions, or just know what's going on right now, visit us in the [integration-guides](https://github.com/meilisearch/integration-guides) repository.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { meilisearchAutocompleteClient } from '../src'
2+
3+
const dataset = [
4+
{ id: 1, label: 'Hit 1' },
5+
{ id: 2, label: 'Hit 2' },
6+
]
7+
const HOST = 'http://localhost:7700'
8+
const API_KEY = 'masterKey'
9+
const searchClient = meilisearchAutocompleteClient({
10+
url: HOST,
11+
apiKey: API_KEY,
12+
})
13+
export { HOST, API_KEY, searchClient, dataset }

packages/autocomplete-client/src/client/createAutocompleteClient.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import { instantMeiliSearch } from '@meilisearch/instant-meilisearch'
2-
import { AutocompleteSearchClient } from '../types/AutocompleteSearchClient'
3-
import { AutocompleteOptions } from '../types/AutoCompleteOptions'
4-
import { createUserAgent } from './createUserAgent'
2+
import { SearchClient } from '../types/SearchClient'
3+
import { ClientConfig } from '../types/ClientConfig'
54

6-
type InstantMeilisearch = typeof instantMeiliSearch
7-
type MeiliURL = Parameters<InstantMeilisearch>[0]
8-
type MeiliApiKey = Parameters<InstantMeilisearch>[1]
5+
import { createUserAgent } from './createUserAgent'
96

107
export function createSearchClient() {
11-
return (
12-
url: MeiliURL,
13-
apiKey: MeiliApiKey,
14-
autocompleteOptions: AutocompleteOptions = {}
15-
): AutocompleteSearchClient => {
8+
return ({ url, apiKey, options = {} }: ClientConfig): SearchClient => {
169
const searchClient = instantMeiliSearch(url, apiKey, {
17-
...autocompleteOptions,
18-
clientAgents: createUserAgent(autocompleteOptions.clientAgents),
10+
...options,
11+
clientAgents: createUserAgent(options.clientAgents),
1912
})
2013

2114
return {

packages/autocomplete-client/src/requesters/createRequester.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
AlgoliaSearchResponse,
55
AlgoliaSearchForFacetValuesResponse,
66
} from '@meilisearch/instant-meilisearch'
7-
import { AutocompleteSearchClient } from '../types/AutocompleteSearchClient'
7+
import { SearchClient as MeilisearchClient } from '../types/SearchClient'
88

99
// All types copied from: autocomplete/packages/autocomplete-preset-algolia/src/requester/createRequester.ts
1010
// As most of the types are not exported and we need to be able to provide our own Fetcher
@@ -56,7 +56,7 @@ type FetcherParamsQuery<THit> = {
5656
}
5757

5858
type ExecuteParams<THit> = {
59-
searchClient: AutocompleteSearchClient
59+
searchClient: MeilisearchClient
6060
requests: Array<FetcherParamsQuery<THit>>
6161
}
6262

packages/autocomplete-client/src/requesters/getMeilisearchResults.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createMeilisearchRequester } from './createMeilisearchRequester'
22

33
/**
4-
* Retrieves Meilisearch results from multiple indices.
4+
* Retrieves Meilisearch results from multiple indexes.
55
*/
66
export const getMeilisearchResults = createMeilisearchRequester({
77
transformResponse: (response) => response.hits,

packages/autocomplete-client/src/search/fetchMeilisearchResults.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import {
33
AlgoliaSearchResponse,
44
} from '@meilisearch/instant-meilisearch'
55
import { HIGHLIGHT_PRE_TAG, HIGHLIGHT_POST_TAG } from '../constants'
6-
import { AutocompleteSearchClient } from '../types/AutocompleteSearchClient'
6+
import { SearchClient as MeilisearchSearchClient } from '../types/SearchClient'
77

88
interface SearchParams {
99
/**
1010
* The initialized Meilisearch search client.
1111
*/
12-
searchClient: AutocompleteSearchClient
12+
searchClient: MeilisearchSearchClient
1313
/**
1414
* A list of queries to execute.
1515
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { instantMeiliSearch } from '@meilisearch/instant-meilisearch'
2+
import { MeilisearchOptions } from './MeilisearchOptions'
3+
4+
export type InstantMeilisearch = typeof instantMeiliSearch
5+
export type MeilisearchURL = Parameters<InstantMeilisearch>[0]
6+
export type MeilisearchApiKey = Parameters<InstantMeilisearch>[1]
7+
export type ClientConfig = {
8+
url: MeilisearchURL
9+
apiKey: MeilisearchApiKey
10+
options?: MeilisearchOptions
11+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { InstantMeiliSearchOptions } from '@meilisearch/instant-meilisearch'
22

3-
export type AutocompleteOptions = InstantMeiliSearchOptions
3+
export type MeilisearchOptions = InstantMeiliSearchOptions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { InstantMeiliSearchInstance } from '@meilisearch/instant-meilisearch'
22

3-
export type AutocompleteSearchClient = InstantMeiliSearchInstance
3+
export type SearchClient = InstantMeiliSearchInstance
File renamed without changes.

0 commit comments

Comments
 (0)