Skip to content

Commit

Permalink
Upd package for MeiliSeach v0.11 (#413)
Browse files Browse the repository at this point in the history
* Upd version

* Upd version

* Updated code samples

* Update .code-samples.meilisearch.yaml

Co-authored-by: Clémentine Urquizar <[email protected]>

* added code samples

* Faceting

* Test compatibility with v0.11

* Changes on client tests

* Updated error message

* Improved types for facetFilters

* Faceting typo

* Add attributesForFaceting in settings tests

* Since this library is not node-only main should be UMD

* build(deps-dev): bump eslint-plugin-jsdoc from 27.0.2 to 27.0.4

Bumps [eslint-plugin-jsdoc](https://github.com/gajus/eslint-plugin-jsdoc) from 27.0.2 to 27.0.4.
- [Release notes](https://github.com/gajus/eslint-plugin-jsdoc/releases)
- [Commits](gajus/eslint-plugin-jsdoc@v27.0.2...v27.0.4)

Signed-off-by: dependabot-preview[bot] <[email protected]>

* changes in error handler

* getOrCreateIndex method and Error handler update

* Linting and styling

* Documentation

* Change error names

* Changed errorHandler name to httpErrorHandler

* changed errorhandler name

* Typo in class name

* Remove comments

* Change usage of createIndex

* Update README.md

Co-authored-by: Clémentine Urquizar <[email protected]>

* Make IndexOptions more type safe and create indexRequest type

* Remove unecessary comment

* Updated changelog

* More specific changelog

* Update CHANGELOG.md

Co-authored-by: Clémentine Urquizar <[email protected]>

* Change version in CHANGELOG

* Improve changelog

* Wording in changelog

* Update README.md

* Improve changelog

Co-authored-by: Charlotte Vermandel <[email protected]>
Co-authored-by: cvermand <[email protected]>
Co-authored-by: Charlotte Vermandel <[email protected]>
Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
  • Loading branch information
5 people authored Jun 16, 2020
1 parent 2cbbb39 commit 009fad7
Show file tree
Hide file tree
Showing 29 changed files with 649 additions and 312 deletions.
50 changes: 47 additions & 3 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ get_one_index_1: |-
list_all_indexes_1: |-
client.listIndexes()
create_an_index_1: |-
client.createIndex({ uid: 'movies' })
client.createIndex('movies')
update_an_index_1: |-
client.updateIndex({ uid: 'movies' })
client.updateIndex('movies', { primaryKey: 'movie_review_id' })
delete_an_index_1: |-
client.getIndex('movies').deleteIndex()
get_one_document_1: |-
Expand Down Expand Up @@ -322,7 +322,7 @@ getting_started_create_index_md: |-
var client = new MeiliSearch({ host: 'http://127.0.0.1:7700' })
const index = client
.createIndex({ uid: 'movies' })
.createIndex('movies')
.then((res) => console.log(res))
```
Expand All @@ -340,3 +340,47 @@ getting_started_search_md: |-
```
[About this package](https://github.com/meilisearch/meilisearch-js/)
get_attributes_for_faceting_1: |-
client.getIndex('movies').getAttributesForFaceting()
update_attributes_for_faceting_1: |-
client.getIndex('movies')
.updateAttributesForFaceting([
'genre',
'director'
])
reset_attributes_for_faceting_1: |-
client.getIndex('movies').resetAttributesForFaceting()
faceted_search_update_settings_1: |-
client.getIndex('movies')
.updateAttributesForFaceting([
'genre',
'director'
])
faceted_search_facet_filters_1: |-
client.getIndex('movies')
.search('thriller', {
facetFilters: [['genres:Horror', 'genres:Mystery'], 'director:Jordan Peele']
})
faceted_search_facets_distribution_1: |-
client.getIndex('movies')
.search('Batman', {
facetsDistribution: ['genres']
})
faceted_search_walkthrough_attributes_for_faceting_1: |-
client.getIndex('movies')
.updateAttributesForFaceting([
'director',
'producer',
'genres',
'production_companies'
])
faceted_search_walkthrough_facet_filters_1: |-
client.getIndex('movies')
.search('thriller', {
facetFilters: [['genres:Horror', 'genres:Mystery'], 'director:Jordan Peele']
})
faceted_search_walkthrough_facets_distribution_1: |-
client.getIndex('movies')
.search('Batman', {
facetsDistribution: ['genres']
})
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
## V0.10 (released)
## V0.11.1 (released)

- BREAKING: Usage of createIndex changed `createIndex(uid: string, options: IndexOptions): Index` #436
- BREAKING: Changes in types
- `MeiliSearchApiErrorInterface` changes
- Removed `UpdateIndexRequest` and replaced it with `IndexOptions`
- Error Handler improved by adding a new MeiliSearchCommunicationError #436
- Refactor Error handler #436
- Add getOrCreateIndex method to meilisearch client #436
- Faceting (#421)
- Improve tests with v11 (#422)
- Improve code examples (#434)
- Update dependencies

## V0.10.1 (released)

- Fix bug where you could not import the CJS package from a ES6 node environment. #410

Expand Down
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import MeiliSearch from 'meilisearch'
apiKey: 'masterKey',
})

const index = await client.createIndex({ uid: 'books' }) // If your index does not exists
const index = await client.createIndex('books') // If your index does not exist
// OR
const index = client.getIndex('books') // If your index exists

Expand Down Expand Up @@ -123,7 +123,7 @@ Output:

This package is compatible with the following MeiliSearch versions:

- `v0.10.X`
- `v0.11.X`

## 🎬 Examples

Expand All @@ -140,9 +140,9 @@ In this section, the examples contain the [`await` keyword](https://developer.mo

```javascript
// Create an index
const index = await client.createIndex({ uid: 'books' })
const index = await client.createIndex('books')
// Create an index and give the primary-key
const index = await client.createIndex({ uid: 'books', primaryKey: 'book_id' })
const index = await client.createIndex('books', { primaryKey: 'book_id' })
```

#### List all indexes <!-- omit in toc -->
Expand Down Expand Up @@ -262,7 +262,7 @@ await index.search('prince', { limit: 1, attributesToHighlight: '*' })

## ⚙️ Development Workflow

If you want to contribute, this sections describes the steps to follow.
If you want to contribute, this section describes the steps to follow.

Thank you for your interest in a MeiliSearch tool! ♥️

Expand Down Expand Up @@ -325,19 +325,23 @@ A GitHub Action will be triggered and push the package on [npm](https://www.npmj

- Create new index:

`client.createIndex(data: IndexRequest): Promise<Index>`
`client.createIndex(uid: string, options?: IndexOptions): Promise<Index>`

- Get index object:

`client.getIndex(uid: string): Indexes`

- Get or create index if it does not exist

`client.getOrCreateIndex(uid: string, options?: IndexOptions): Promise<Index>`

- Show Index information:

`index.show(): Promise<IndexResponse>`

- Update Index:

`index.updateIndex(data: UpdateIndexRequest): Promise<IndexResponse>`
`index.updateIndex(data: IndexOptions): Promise<IndexResponse>`

- Delete Index:

Expand Down
2 changes: 1 addition & 1 deletion examples/search_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const addDataset = async () => {
console.log({ indexes, indexFound })

if (!indexFound) {
await meili.createIndex(index)
await meili.createIndex(index.uid)
}
const documents = await meili.getIndex(index.uid).getDocuments()
if (documents.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions examples/small_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const config = {

var meili = new MeiliSearch(config)

const index = {
const newIndex = {
uid: 'movies_test',
}

Expand All @@ -30,7 +30,7 @@ const dataset = [
]

;(async () => {
// This examples creates an index with 7 documents
// This example creates an index with 7 documents
await meili.createIndex(index)
const { updateId } = await meili.getIndex(index.uid).addDocuments(dataset)
const res = await meili.getIndex(index.uid).waitForPendingUpdate(updateId)
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meilisearch",
"version": "0.10.1",
"version": "0.11.0",
"description": "The MeiliSearch JS client for Node.js and the browser.",
"keywords": [
"meilisearch",
Expand All @@ -16,7 +16,7 @@
"qdequele <[email protected]>"
],
"license": "MIT",
"main": "./dist/bundles/meilisearch.cjs.js",
"main": "./dist/bundles/meilisearch.umd.js",
"module": "./dist/bundles/meilisearch.esm.js",
"browser": "./dist/bundles/meilisearch.umd.js",
"typings": "./dist/types/types.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions src/errors/http-error-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { AxiosError } from 'axios'
import MeiliSearchApiError from './meilisearch-api-error'
import MeiliSearchCommunicationError from './meilisearch-communication-error'

function httpErrorHandler(e: AxiosError, cachedStack?: string): void {
if (e.response !== undefined) {
throw new MeiliSearchApiError(e, cachedStack)
} else if (e.isAxiosError) {
throw new MeiliSearchCommunicationError(e.message)
} else {
throw e
}
}

export { httpErrorHandler }
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const MeiliSearchApiError: Types.MeiliSearchApiErrorConstructor = class
extends Error
implements Types.MeiliSearchApiErrorInterface {
response?: Types.MeiliSearchApiErrorResponse
request?: Types.MeiliSearchApiErrorRequest
errorCode?: string
errorType?: string
errorLink?: string
stack?: string
type: string

constructor(error: AxiosError, cachedStack?: string) {
super(error.message)
this.type = this.constructor.name

this.type = 'MeiliSearchApiError'
this.name = 'MeiliSearchApiError'

// Fetch the native error message but add our application name in front of it.
Expand All @@ -21,20 +25,16 @@ const MeiliSearchApiError: Types.MeiliSearchApiErrorConstructor = class
statusText: error.response.statusText,
path: error.response.config.url,
method: error.response.config.method,
body: error.response.data,
}

// If a custom message was sent back by our API
// We change the error message to be more explicit
if (error.response.data?.message !== undefined) {
this.errorCode = error.response.data.errorCode
this.errorType = error.response.data.errorType
this.errorLink = error.response.data.errorLink
this.message = error.response.data.message
}
} else {
// If MeiliSearch did not answered
this.request = {
url: error.request._currentUrl,
path: error.config.url,
method: error.config.method,
}
}

// use cached Stack on error object to keep the call stack
Expand Down
11 changes: 11 additions & 0 deletions src/errors/meilisearch-communication-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class MeiliSearchCommunicationError extends Error {
type: string
constructor(message: string) {
super(message)
this.name = 'MeiliSearchCommunicationError'
this.type = 'MeiliSearchCommunicationError'
Error.captureStackTrace(this, MeiliSearchCommunicationError)
}
}

export default MeiliSearchCommunicationError
File renamed without changes.
50 changes: 47 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict'

import MeiliSearchTimeOutError from './custom-errors/meilisearch-timeout-error'
import MeiliSearchTimeOutError from './errors/meilisearch-timeout-error'
import MeiliAxiosWrapper from './meili-axios-wrapper'
import * as Types from './types'
import { sleep } from './utils'
Expand Down Expand Up @@ -116,10 +116,15 @@ class Index extends MeiliAxiosWrapper implements Types.IndexInterface {
if (options.filters !== undefined) {
params.filters = options.filters
}

if (options.matches !== undefined) {
params.matches = options.matches
}
if (options.facetFilters !== undefined) {
params.facetFilters = JSON.stringify(options.facetFilters)
}
if (options.facetsDistribution !== undefined) {
params.facetsDistribution = JSON.stringify(options.facetsDistribution)
}
}

return await this.get(url, {
Expand Down Expand Up @@ -148,7 +153,7 @@ class Index extends MeiliAxiosWrapper implements Types.IndexInterface {
* @method updateIndex
*/
async updateIndex(
data: Types.UpdateIndexRequest
data: Types.IndexOptions
): Promise<Types.IndexResponse> {
const url = `/indexes/${this.uid}`

Expand Down Expand Up @@ -479,6 +484,45 @@ class Index extends MeiliAxiosWrapper implements Types.IndexInterface {
return await this.delete(url)
}

///
/// ATTRIBUTES FOR FACETING
///

/**
* Get the attributes-for-faceting
* @memberof Index
* @method getAttributesForFaceting
*/
async getAttributesForFaceting(): Promise<string[]> {
const url = `/indexes/${this.uid}/settings/attributes-for-faceting`

return await this.get(url)
}

/**
* Update the attributes-for-faceting.
* @memberof Index
* @method updateAttributesForFaceting
*/
async updateAttributesForFaceting(
attributesForFaceting: string[]
): Promise<Types.EnqueuedUpdate> {
const url = `/indexes/${this.uid}/settings/attributes-for-faceting`

return await this.post(url, attributesForFaceting)
}

/**
* Reset the attributes-for-faceting.
* @memberof Index
* @method resetAttributesForFaceting
*/
async resetAttributesForFaceting(): Promise<Types.EnqueuedUpdate> {
const url = `/indexes/${this.uid}/settings/attributes-for-faceting`

return await this.delete(url)
}

///
/// SEARCHABLE ATTRIBUTE
///
Expand Down
Loading

0 comments on commit 009fad7

Please sign in to comment.