Skip to content

Commit 8adca26

Browse files
bors[bot]bidoubiwa
andauthored
Merge #383
383: Tests suites r=bidoubiwa a=bidoubiwa fixes: #384 fixes: #379 fixes: #385 Breaking changes see: #378 Co-authored-by: Charlotte Vermandel <[email protected]> Co-authored-by: cvermand <[email protected]> Co-authored-by: bors[bot] <26634292+bors[bot]@users.noreply.github.com>
2 parents b7ae607 + 7ad63eb commit 8adca26

22 files changed

+550
-280
lines changed

jest.config.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
module.exports = {
22
verbose: true,
3+
watchPlugins: [
4+
'jest-watch-typeahead/filename',
5+
'jest-watch-typeahead/testname',
6+
],
37
projects: [
48
{
59
preset: 'ts-jest',
610
displayName: 'dom',
711
// We are using jest-environment-jsdom 25 until we stop supporting node 10
812
// jest-environment-jsdom 25 uses jsdom 15 which still supports node 10
913
testEnvironment: 'jest-environment-jsdom',
10-
testPathIgnorePatterns: [
11-
'<rootDir>/tests/env/express',
12-
'<rootDir>/playgrounds/angular',
13-
],
14+
testPathIgnorePatterns: ['<rootDir>/tests/env', '<rootDir>/playgrounds'],
15+
testMatch: ['**/*.tests.ts'],
1416
},
1517
{
1618
preset: 'ts-jest',
1719
displayName: 'node',
1820
testEnvironment: 'node',
19-
testPathIgnorePatterns: [
20-
'<rootDir>/tests/env/express',
21-
'<rootDir>/playgrounds/angular',
22-
],
21+
testPathIgnorePatterns: ['<rootDir>/tests/env', '<rootDir>/playgrounds'],
22+
testMatch: ['**/*.tests.ts'],
2323
},
2424
],
2525
}

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"description": "The search client to use MeiliSearch with InstantSearch.",
66
"scripts": {
77
"cleanup": "shx rm -rf dist/",
8-
"test": "yarn build && jest",
8+
"test:watch": "yarn test --watch",
9+
"test": "jest",
910
"test:all": "yarn test && yarn test:env && yarn test:playgrounds && yarn test:e2e",
1011
"test:env": "yarn build && yarn test:env:browser && yarn test:env:nodejs && yarn test:env:esm && yarn test:env:ts",
1112
"test:env:browser": "yarn --cwd tests/env/express && yarn --cwd tests/env/express test",
@@ -92,7 +93,7 @@
9293
"instantsearch.js": "^4.16.1",
9394
"jest": "^26.6.3",
9495
"jest-environment-jsdom": "25.5",
95-
"jest-watch-typeahead": "^0.6.0",
96+
"jest-watch-typeahead": "^0.6.3",
9697
"prettier": "^2.0.0",
9798
"regenerator-runtime": "^0.13.7",
9899
"rollup": "^2.40.0",

rollup.config.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,22 @@ function getOutputFileName(fileName, isProd = false) {
1313

1414
const env = process.env.NODE_ENV || 'development'
1515
const ROOT = resolve(__dirname, '.')
16+
const INPUT = 'src/index.ts'
1617

1718
const PLUGINS = [
1819
typescript({
1920
useTsconfigDeclarationDir: true,
2021
tsconfigOverride: {
2122
includes: ['src'],
22-
exclude: ['tests', 'examples', '*.js', 'scripts'],
23+
exclude: ['tests', '*.js', 'scripts', '**/__tests__/*.ts'],
2324
esModuleInterop: true,
2425
},
2526
}),
2627
]
2728
module.exports = [
2829
// browser-friendly IIFE build
2930
{
30-
input: 'src/index.ts', // directory to transpilation of typescript
31+
input: INPUT, // directory to transpilation of typescript
3132
output: {
3233
name: 'window',
3334
extend: true,
@@ -56,7 +57,7 @@ module.exports = [
5657
],
5758
},
5859
{
59-
input: 'src/index.ts',
60+
input: INPUT,
6061
external: ['meilisearch'],
6162
output: [
6263
{

src/client/index.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { MeiliSearch } from 'meilisearch'
2+
import { InstantMeiliSearchOptions, InstantMeiliSearchInstance } from '../types'
3+
4+
import {
5+
transformToMeiliSearchParams,
6+
transformToISResponse,
7+
} from '../transformers'
8+
9+
export function instantMeiliSearch(
10+
hostUrl: string,
11+
apiKey: string,
12+
options: InstantMeiliSearchOptions = {}
13+
): InstantMeiliSearchInstance {
14+
return {
15+
MeiliSearchClient: new MeiliSearch({ host: hostUrl, apiKey: apiKey }),
16+
search: async function ([isSearchRequest]) {
17+
try {
18+
// Params got from InstantSearch
19+
const {
20+
params: instantSearchParams,
21+
indexName: indexUid,
22+
} = isSearchRequest
23+
24+
const { paginationTotalHits, primaryKey, placeholderSearch } = options
25+
const { page, hitsPerPage } = instantSearchParams
26+
const client = this.MeiliSearchClient
27+
const context = {
28+
client,
29+
paginationTotalHits: paginationTotalHits || 200,
30+
primaryKey: primaryKey || undefined,
31+
placeholderSearch: placeholderSearch !== false, // true by default
32+
hitsPerPage: hitsPerPage === undefined ? 20 : hitsPerPage, // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
33+
page: page || 0, // default page is 0 if none is provided
34+
}
35+
36+
// Transform IS params to MeiliSearch params
37+
const msSearchParams = transformToMeiliSearchParams(
38+
instantSearchParams,
39+
context
40+
)
41+
42+
// Executes the search with MeiliSearch
43+
const searchResponse = await client
44+
.index(indexUid)
45+
.search(msSearchParams.q, msSearchParams)
46+
47+
// Parses the MeiliSearch response and returns it for InstantSearch
48+
return transformToISResponse(
49+
indexUid,
50+
searchResponse,
51+
instantSearchParams,
52+
context
53+
)
54+
} catch (e) {
55+
console.error(e)
56+
throw new Error(e)
57+
}
58+
},
59+
}
60+
}

src/index.ts

Lines changed: 3 additions & 162 deletions
Original file line numberDiff line numberDiff line change
@@ -1,162 +1,3 @@
1-
import { MeiliSearch } from 'meilisearch'
2-
import { createHighlighResult, createSnippetResult } from './format'
3-
import {
4-
InstantMeiliSearchOptions,
5-
InstantMeiliSearchInstance,
6-
InstantSearchTypes,
7-
} from './types'
8-
9-
export function instantMeiliSearch(
10-
hostUrl: string,
11-
apiKey: string,
12-
options: InstantMeiliSearchOptions = {}
13-
): InstantMeiliSearchInstance {
14-
return {
15-
client: new MeiliSearch({ host: hostUrl, apiKey: apiKey }),
16-
paginationTotalHits: options.paginationTotalHits || 200,
17-
primaryKey: options.primaryKey || undefined,
18-
placeholderSearch: options.placeholderSearch !== false, // true by default
19-
hitsPerPage: 20,
20-
page: 0,
21-
22-
/*
23-
REQUEST
24-
*/
25-
26-
transformToMeiliSearchParams: function ({
27-
query,
28-
facets,
29-
facetFilters,
30-
attributesToSnippet: attributesToCrop,
31-
attributesToRetrieve,
32-
attributesToHighlight,
33-
filters = '',
34-
numericFilters = [],
35-
}) {
36-
const limit = this.paginationTotalHits
37-
38-
const filter = [numericFilters.join(' AND '), filters.trim()]
39-
.filter((x) => x)
40-
.join(' AND ')
41-
.trim()
42-
43-
// Creates search params object compliant with MeiliSearch
44-
return {
45-
q: query,
46-
...(facets?.length && { facetsDistribution: facets }),
47-
...(facetFilters && { facetFilters }),
48-
...(attributesToCrop && { attributesToCrop }),
49-
...(attributesToRetrieve && { attributesToRetrieve }),
50-
...(filter && { filters: filter }),
51-
attributesToHighlight: attributesToHighlight || ['*'],
52-
limit: (!this.placeholderSearch && query === '') || !limit ? 0 : limit,
53-
}
54-
},
55-
56-
/*
57-
RESPONSE
58-
*/
59-
60-
getNumberPages: function (hitsLength) {
61-
const adjust = hitsLength % this.hitsPerPage! === 0 ? 0 : 1
62-
return Math.floor(hitsLength / this.hitsPerPage!) + adjust // total number of pages
63-
},
64-
65-
paginateHits: function (hits) {
66-
const start = this.page * this.hitsPerPage!
67-
return hits.splice(start, this.hitsPerPage)
68-
},
69-
70-
transformToISHits: function (meiliSearchHits, instantSearchParams) {
71-
const paginatedHits = this.paginateHits(meiliSearchHits)
72-
73-
return paginatedHits.map((hit: Record<string, any>) => {
74-
const { _formatted: formattedHit, ...restOfHit } = hit
75-
76-
// Creates Hit object compliant with InstantSearch
77-
return {
78-
...restOfHit,
79-
_highlightResult: createHighlighResult({
80-
formattedHit,
81-
...instantSearchParams,
82-
}),
83-
_snippetResult: createSnippetResult({
84-
formattedHit,
85-
...instantSearchParams,
86-
}),
87-
...(this.primaryKey && { objectID: hit[this.primaryKey] }),
88-
}
89-
})
90-
},
91-
92-
transformToISResponse: function (
93-
indexUid,
94-
{
95-
exhaustiveFacetsCount,
96-
exhaustiveNbHits,
97-
facetsDistribution: facets,
98-
nbHits,
99-
processingTimeMs,
100-
query,
101-
hits,
102-
},
103-
instantSearchParams
104-
) {
105-
// Create response object compliant with InstantSearch
106-
const ISResponse = {
107-
index: indexUid,
108-
hitsPerPage: this.hitsPerPage,
109-
...(facets && { facets }),
110-
...(exhaustiveFacetsCount && { exhaustiveFacetsCount }),
111-
page: this.page,
112-
nbPages: this.getNumberPages(hits.length),
113-
exhaustiveNbHits,
114-
nbHits,
115-
processingTimeMS: processingTimeMs,
116-
query,
117-
hits: this.transformToISHits(hits, instantSearchParams),
118-
}
119-
return {
120-
results: [ISResponse],
121-
}
122-
},
123-
124-
/*
125-
SEARCH
126-
*/
127-
search: async function ([
128-
isSearchRequest,
129-
]: InstantSearchTypes.SearchRequest[]) {
130-
try {
131-
// Params got from InstantSearch
132-
const {
133-
params: instantSearchParams,
134-
indexName: indexUid,
135-
} = isSearchRequest
136-
const { page, hitsPerPage } = instantSearchParams
137-
138-
this.page = page || 0 // default page is 0 if none is provided
139-
this.hitsPerPage = hitsPerPage || 20 // 20 is the MeiliSearch's default limit value. `hitsPerPage` can be changed with `InsantSearch.configure`.
140-
141-
// Transform IS params to MeiliSearch params
142-
const msSearchParams = this.transformToMeiliSearchParams(
143-
instantSearchParams
144-
)
145-
// Executes the search with MeiliSearch
146-
const searchResponse = await this.client
147-
.index(indexUid)
148-
.search(msSearchParams.q, msSearchParams)
149-
150-
// Parses the MeiliSearch response and returns it for InstantSearch
151-
return this.transformToISResponse(
152-
indexUid,
153-
searchResponse,
154-
instantSearchParams
155-
)
156-
} catch (e) {
157-
console.error(e)
158-
throw new Error(e)
159-
}
160-
},
161-
}
162-
}
1+
export * from './client'
2+
export * from './transformers'
3+
export * from './types'

0 commit comments

Comments
 (0)