Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to abort search request #636

Merged
merged 2 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,27 @@ await index.search(null, {
}
```

#### Abortable Search

You can abort a pending search request by providing an [AbortSignal](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) to the request.

```js
const controller = new AbortController()

index
.search('prince', {}, 'POST', {
signal: controller.signal,
})
.then((response) => {
/** ... */
})
.catch((e) => {
/** Catch AbortError here. */
})

controller.abort()
```

## ⚙️ Development Workflow and Contributing

Any new contribution is more than welcome in this project!
Expand All @@ -348,7 +369,7 @@ If you want to know more about the development workflow or want to contribute, p

- Make a search request:

`client.getIndex<T>('xxx').search(query: string, options: SearchParams = {}, method: 'POST' | 'GET' = 'POST'): Promise<SearchResponse<T>>`
`client.getIndex<T>('xxx').search(query: string, options: SearchParams = {}, method: 'POST' | 'GET' = 'POST', config?: Partial<Request>): Promise<SearchResponse<T>>`

### Indexes <!-- omit in toc -->

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"@types/prettier": "^2.0.0",
"@typescript-eslint/eslint-plugin": "2.34.0",
"@typescript-eslint/parser": "2.34.0",
"abort-controller": "^3.0.0",
"brotli-size": "^4.0.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
Expand Down
26 changes: 13 additions & 13 deletions src/http-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class HttpRequests {
url: string
params?: { [key: string]: any }
body?: any
config?: Request
config?: Partial<Request>
}) {
try {
const constructURL = new URL(this.url)
Expand Down Expand Up @@ -64,19 +64,19 @@ class HttpRequests {
async get(
url: string,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<void>

async get<T = any>(
url: string,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<T>

async get(
url: string,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<any> {
return await this.request({
method: 'GET',
Expand All @@ -90,21 +90,21 @@ class HttpRequests {
url: string,
data: Types.IndexRequest,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<Types.IndexResponse>

async post<T = any, R = Types.EnqueuedUpdate>(
url: string,
data?: T,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<R>

async post(
url: string,
data?: any,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<any> {
return await this.request({
method: 'POST',
Expand All @@ -119,21 +119,21 @@ class HttpRequests {
url: string,
data: Types.IndexOptions | Types.IndexRequest,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<Types.IndexResponse>

async put<T = any, R = Types.EnqueuedUpdate>(
url: string,
data?: T,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<R>

async put(
url: string,
data?: any,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<any> {
return await this.request({
method: 'PUT',
Expand All @@ -148,19 +148,19 @@ class HttpRequests {
url: string,
data?: any,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<void>
async delete<T>(
url: string,
data?: any,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<T>
async delete(
url: string,
data?: any,
params?: { [key: string]: any },
config?: Request
config?: Partial<Request>
): Promise<any> {
return await this.request({
method: 'DELETE',
Expand Down
13 changes: 10 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class Index<T> implements Types.IndexInterface<T> {
async search<P extends Types.SearchParams<T>>(
query?: string | null,
options?: P,
method: Types.Methods = 'POST'
method: Types.Methods = 'POST',
config?: Partial<Request>
): Promise<Types.SearchResponse<T, P>> {
const url = `/indexes/${this.uid}/search`
const params: Types.SearchRequest = {
Expand All @@ -95,7 +96,12 @@ class Index<T> implements Types.IndexInterface<T> {
attributesToHighlight: options?.attributesToHighlight,
}
if (method.toUpperCase() === 'POST') {
return await this.httpRequest.post(url, removeUndefinedFromObject(params))
return await this.httpRequest.post(
url,
removeUndefinedFromObject(params),
undefined,
config
)
} else if (method.toUpperCase() === 'GET') {
const getParams: Types.GetSearchRequest = {
...params,
Expand All @@ -119,7 +125,8 @@ class Index<T> implements Types.IndexInterface<T> {

return await this.httpRequest.get<Types.SearchResponse<T, P>>(
url,
removeUndefinedFromObject(getParams)
removeUndefinedFromObject(getParams),
config
)
} else {
throw new MeiliSearchError(
Expand Down
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,8 @@ export interface IndexInterface<T = any> {
search: <P extends SearchParams<T>>(
query?: string | null,
options?: P,
method?: Methods
method?: Methods,
config?: Partial<Request>
) => Promise<SearchResponse<T, P>>
show: () => Promise<IndexResponse>
updateIndex: (indexData: IndexOptions) => Promise<IndexResponse>
Expand Down
85 changes: 85 additions & 0 deletions tests/search_tests.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'abort-controller/polyfill'

import * as Types from '../src/types'
import {
clearAllIndexes,
Expand Down Expand Up @@ -506,3 +508,86 @@ describe.each([{ client: anonymousClient, permission: 'Client' }])(
})
}
)

describe.each([
{ client: masterClient, permission: 'Master' },
{ client: privateClient, permission: 'Private' },
{ client: publicClient, permission: 'Public' },
{ client: masterClient, permission: 'Master' },
{ client: privateClient, permission: 'Private' },
{ client: publicClient, permission: 'Public' },
])('Test on abortable search', ({ client, permission }) => {
describe.each([
{ method: 'POST' as Types.Methods, permission, client },
{ method: 'GET' as Types.Methods, permission, client },
])('Test on abortable search', ({ client, permission, method }) => {
beforeAll(async () => {
await clearAllIndexes(config)
await masterClient.createIndex(index.uid)
})

test(`${permission} key: ${method} search on index and abort`, () => {
const controller = new AbortController()

const searchPromise = client
.getIndex(index.uid)
.search('unreachable', {}, method, {
signal: controller.signal,
})

controller.abort()

searchPromise.catch((error) => {
expect(error).toHaveProperty('message', 'The user aborted a request.')
})
})

test(`${permission} key: ${method} search on index multiple times, and abort only one request`, () => {
const controllerA = new AbortController()
const controllerB = new AbortController()
const controllerC = new AbortController()

const searchQuery = 'prince'

const searchAPromise = client
.getIndex(index.uid)
.search(searchQuery, {}, method, {
signal: controllerA.signal,
})

const searchBPromise = client
.getIndex(index.uid)
.search(searchQuery, {}, method, {
signal: controllerB.signal,
})

const searchCPromise = client
.getIndex(index.uid)
.search(searchQuery, {}, method, {
signal: controllerC.signal,
})

const searchDPromise = client
.getIndex(index.uid)
.search(searchQuery, {}, method)

controllerB.abort()

searchDPromise.then((response) => {
expect(response).toHaveProperty('query', searchQuery)
})

searchCPromise.then((response) => {
expect(response).toHaveProperty('query', searchQuery)
})

searchAPromise.then((response) => {
expect(response).toHaveProperty('query', searchQuery)
})

searchBPromise.catch((error) => {
expect(error).toHaveProperty('message', 'The user aborted a request.')
})
})
})
})
12 changes: 12 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1504,6 +1504,13 @@ abab@^2.0.0:
resolved "https://registry.yarnpkg.com/abab/-/abab-2.0.3.tgz#623e2075e02eb2d3f2475e49f99c91846467907a"
integrity sha512-tsFzPpcttalNjFBCFMqsKYQcWxxen1pgJR56by//QwvJc4/OUS3kPOOttx2tSIfjsylB0pYu7f5D3K1RCxUnUg==

abort-controller@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/abort-controller/-/abort-controller-3.0.0.tgz#eaf54d53b62bae4138e809ca225c8439a6efb392"
integrity sha512-h8lQ8tacZYnR3vNQTgibj+tODHI5/+l06Au2Pcriv/Gmet0eaj4TwWH41sO9wnHDiQsEj19q0drzdWdeAHtweg==
dependencies:
event-target-shim "^5.0.0"

acorn-globals@^4.3.2:
version "4.3.4"
resolved "https://registry.yarnpkg.com/acorn-globals/-/acorn-globals-4.3.4.tgz#9fa1926addc11c97308c4e66d7add0d40c3272e7"
Expand Down Expand Up @@ -2626,6 +2633,11 @@ esutils@^2.0.2:
resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64"
integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g==

event-target-shim@^5.0.0:
version "5.0.1"
resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789"
integrity sha512-i/2XbnSz/uxRCU6+NdVJgKWDTM427+MqYbkQzD321DuCQJUqOuJKIA0IM2+W2xtYHdKOmZ4dR6fExsd4SXL+WQ==

exec-sh@^0.3.2:
version "0.3.4"
resolved "https://registry.yarnpkg.com/exec-sh/-/exec-sh-0.3.4.tgz#3a018ceb526cc6f6df2bb504b2bfe8e3a4934ec5"
Expand Down