Skip to content

Commit d71af7d

Browse files
committed
Pass client agent to meilisearch-js
1 parent 6d79386 commit d71af7d

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

src/lib/DocsSearchBar.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import templates from './templates'
33
import utils from './utils'
44
import $ from './zepto'
55
import { MeiliSearch } from 'meilisearch'
6+
import { constructClientAgents } from './agents'
67

78
/**
89
* Adds an autocomplete dropdown to an input field
@@ -56,6 +57,7 @@ class DocsSearchBar {
5657
enhancedSearchInput = false,
5758
layout = 'columns',
5859
enableDarkMode = false,
60+
clientAgents = [],
5961
}) {
6062
DocsSearchBar.checkArguments({
6163
hostUrl,
@@ -72,6 +74,7 @@ class DocsSearchBar {
7274
enhancedSearchInput,
7375
layout,
7476
enableDarkMode,
77+
clientAgents,
7578
})
7679

7780
this.apiKey = apiKey
@@ -115,6 +118,7 @@ class DocsSearchBar {
115118
this.client = new MeiliSearch({
116119
host: hostUrl,
117120
apiKey: this.apiKey,
121+
clientAgents: constructClientAgents(clientAgents),
118122
})
119123

120124
DocsSearchBar.addThemeWrapper(inputSelector, this.enableDarkMode)
@@ -255,6 +259,8 @@ class DocsSearchBar {
255259
false,
256260
)
257261

262+
DocsSearchBar.typeCheck(args, ['clientAgents'], 'array', true)
263+
258264
DocsSearchBar.typeCheck(
259265
args,
260266
['queryDataCallback', 'transformData', 'queryHook', 'handleSelected'],
@@ -268,6 +274,20 @@ class DocsSearchBar {
268274
)
269275
}
270276
}
277+
/**
278+
* Throw a type error.
279+
*
280+
* @param {string} argument - Name of the parameter
281+
* @param {string} type - Type the parameter should have
282+
* @param {string} value - Value the parameter has
283+
*
284+
* @returns {void}
285+
*/
286+
static throwTypeError(argument, type, value) {
287+
throw new Error(
288+
`Error: "${argument}" must be of type: ${type}. Found type: ${typeof value}`,
289+
)
290+
}
271291

272292
/**
273293
* Checks if the arguments defined in the check variable are of the supplied
@@ -283,10 +303,12 @@ class DocsSearchBar {
283303
.filter((argument) => !optional || args[argument])
284304
.forEach((argument) => {
285305
const value = args[argument]
286-
if (typeof args[argument] !== type) {
287-
throw new Error(
288-
`Error: "${argument}" must be of type: ${type}. Found type: ${typeof value}`,
289-
)
306+
if (type === 'array') {
307+
if (!Array.isArray(args[argument])) {
308+
DocsSearchBar.throwTypeError(argument, type, value)
309+
}
310+
} else if (typeof args[argument] !== type) {
311+
DocsSearchBar.throwTypeError(argument, type, value)
290312
}
291313
})
292314
}

src/lib/__tests__/DocsSearchBar-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sinon from 'sinon'
44
import $ from '../zepto'
55
import DocsSearchBar from '../DocsSearchBar'
6+
import PACKAGE_VERSION from '../version'
67
/**
78
* Pitfalls:
89
* Whenever you call new DocsSearchBar(), it will add the a new dropdown markup to
@@ -183,6 +184,7 @@ describe('DocsSearchBar', () => {
183184
MeiliSearch.calledWith({
184185
host: 'https://test.getmeili.com',
185186
apiKey: 'apiKey',
187+
clientAgents: [`Meilisearch docs-searchbar.js (v${PACKAGE_VERSION})`],
186188
}),
187189
).toBe(true)
188190
})
@@ -226,6 +228,27 @@ describe('DocsSearchBar', () => {
226228
expect(autocomplete.on.calledTwice).toBe(true)
227229
expect(autocomplete.on.calledWith('autocomplete:selected')).toBe(true)
228230
})
231+
232+
it('should have spread the user agents', () => {
233+
// Given
234+
const options = { ...defaultOptions, clientAgents: ['test'] }
235+
236+
// When
237+
new DocsSearchBar(options)
238+
239+
// Then
240+
expect(MeiliSearch.calledOnce).toBe(true)
241+
expect(
242+
MeiliSearch.calledWith({
243+
host: 'https://test.getmeili.com',
244+
apiKey: 'apiKey',
245+
clientAgents: [
246+
'test',
247+
`Meilisearch docs-searchbar.js (v${PACKAGE_VERSION})`,
248+
],
249+
}),
250+
).toBe(true)
251+
})
229252
})
230253

231254
describe('checkArguments', () => {
@@ -377,6 +400,15 @@ describe('DocsSearchBar', () => {
377400
// Given
378401
const options = { ...defaultOptions, handleSelected: 'not-a-function' }
379402

403+
// When
404+
expect(() => {
405+
checkArguments(options)
406+
}).toThrow(/^Error:/)
407+
})
408+
it('should throw an error if userAgents is not an array', () => {
409+
// Given
410+
const options = { ...defaultOptions, clientAgents: 'test' }
411+
380412
// When
381413
expect(() => {
382414
checkArguments(options)

src/lib/agents.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import version from './version'
2+
3+
export const constructClientAgents = (clientAgents = []) => {
4+
const instantMeilisearchAgent = `Meilisearch docs-searchbar.js (v${version})`
5+
6+
return clientAgents.concat(instantMeilisearchAgent)
7+
}

0 commit comments

Comments
 (0)