Skip to content

Improve TaskClient, BatchClient and associated types #1825

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

Merged
merged 63 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
797d9ee
Remove unnecessary classes
flevi29 Jan 8, 2025
23218d2
Save progress
flevi29 Jan 10, 2025
e5f1840
Progress
flevi29 Jan 13, 2025
4dceb10
Progress
flevi29 Jan 13, 2025
a18fad3
Merge branch 'main' into improve-task-batch
flevi29 Jan 13, 2025
9553b75
Fix formatting
flevi29 Jan 13, 2025
baf08c5
Fix test
flevi29 Jan 13, 2025
b5bfb5d
Fix test
flevi29 Jan 13, 2025
b8c7afc
Fix rest of the tests, adjust readme
flevi29 Jan 13, 2025
207da36
Add documentation, adjust types, functions, misc
flevi29 Jan 14, 2025
5651f6c
Merge branch 'main' into improve-task-batch
flevi29 Jan 14, 2025
a10699a
Fix formatting
flevi29 Jan 14, 2025
1ea7670
Revert some changes, misc
flevi29 Jan 15, 2025
8062d9b
Adjust types
flevi29 Jan 15, 2025
b77291b
Fix test
flevi29 Jan 15, 2025
b525e8d
Misc
flevi29 Jan 15, 2025
ccd5c20
Adjust docs
flevi29 Jan 16, 2025
bcbac12
Adjust doc
flevi29 Jan 18, 2025
03be679
Merge branch 'main' into improve-task-batch
flevi29 Jan 28, 2025
49dedd0
Merge with main
flevi29 Jan 30, 2025
890102a
Merge branch 'improve-task-batch' of github.com:flevi29/meilisearch-j…
flevi29 Jan 30, 2025
6e00229
Revert settings types change
flevi29 Jan 30, 2025
1dc418a
Fix formatting
flevi29 Jan 30, 2025
d141b1b
Add task wait applier to tasks as well
flevi29 Jan 30, 2025
6ff3d52
Revert bad test
flevi29 Jan 30, 2025
72c1456
Remove focus from test, experiment with old interval
flevi29 Jan 30, 2025
048a479
Fix not clearing timers when task is successfully awaited
flevi29 Jan 30, 2025
53a9049
Try and fix waitTask being slow
flevi29 Jan 30, 2025
adc946b
Fix unhandled rejections
flevi29 Jan 30, 2025
d2b3234
Refactor waitTask
flevi29 Jan 30, 2025
b509d60
Try fix slow waitTask
flevi29 Jan 31, 2025
9f4aacc
Try fix slow waitTask
flevi29 Jan 31, 2025
4db49a6
Add ability to specify default wait options for tasks through exporte…
flevi29 Jan 31, 2025
bc08957
Shorten code
flevi29 Jan 31, 2025
b5a6a7d
Rename variables
flevi29 Jan 31, 2025
a906055
Remove hint about bad Meilisearch version
flevi29 Jan 31, 2025
8c500a3
Add test for failing waitTask
flevi29 Jan 31, 2025
b2acf84
Fix formatting, don't add timeout if interval is invalid
flevi29 Jan 31, 2025
88f210e
Rename functions, adjust description
flevi29 Jan 31, 2025
15984a2
Do not export TaskClient in index
flevi29 Jan 31, 2025
1c74be0
Merge branch 'main' into improve-task-batch
flevi29 Feb 1, 2025
3156692
Revert deprecations
flevi29 Feb 5, 2025
9be9626
Merge branch 'main' into improve-task-batch
flevi29 Feb 6, 2025
a278fe6
Merge with main
flevi29 Feb 17, 2025
824f284
Merge branch 'improve-task-batch' of github.com:flevi29/meilisearch-j…
flevi29 Feb 17, 2025
61cf291
Fix formatting
flevi29 Feb 17, 2025
f9d412a
Merge branch 'main' into improve-task-batch
flevi29 Mar 2, 2025
7db2295
Merge with main
flevi29 Mar 12, 2025
cb7ff86
Added missing batchUids prop, misc
flevi29 Mar 13, 2025
d1e7b60
Make waitTask abort last getTask on timeout
flevi29 Mar 13, 2025
0b65095
Improve timeout errors, fix tests
flevi29 Mar 13, 2025
97c3019
Misc
flevi29 Mar 15, 2025
cdbd556
Fix error
flevi29 Mar 15, 2025
12fe057
Fix formatting
flevi29 Mar 15, 2025
fc4e9f4
Remove unnecessary tests
flevi29 Mar 15, 2025
324ae8c
Fix waitTask, try Symbol for timeout ID
flevi29 Mar 15, 2025
43f79b2
Fix http requests not catching abort error on response.text()
flevi29 Mar 15, 2025
6248a59
Fix Node.js 18 compat
flevi29 Mar 15, 2025
4f2b292
Add documentation to changed errors
flevi29 Mar 15, 2025
13cec4b
Merge with main
flevi29 Mar 21, 2025
517e119
Merge with main
flevi29 Mar 26, 2025
1cd5aae
Merge branch 'main' into improve-task-batch
Strift Apr 1, 2025
68c4afd
Update type definitions header
flevi29 Apr 1, 2025
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
22 changes: 2 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,34 +564,16 @@ client.index('myIndex').getTasks(parameters: TasksQuery): Promise<TasksResults>
client.index('myIndex').getTask(uid: number): Promise<Task>
```


#### Wait for one task


##### Using the client

```ts
client.waitForTask(uid: number, { timeOutMs?: number, intervalMs?: number }): Promise<Task>
```

##### Using the index

```ts
client.index('myIndex').waitForTask(uid: number, { timeOutMs?: number, intervalMs?: number }): Promise<Task>
client.tasks.waitForTask(uid: number, { timeout?: number, interval?: number }): Promise<Task>
```

#### Wait for multiple tasks

##### Using the client

```ts
client.waitForTasks(uids: number[], { timeOutMs?: number, intervalMs?: number }): Promise<Task[]>
```

##### Using the index

```ts
client.index('myIndex').waitForTasks(uids: number[], { timeOutMs?: number, intervalMs?: number }): Promise<Task[]>
client.tasks.waitForTasks(uids: number[], { timeout?: number, interval?: number }): Promise<Task[]>
```

### Batches <!-- omit in toc -->
Expand Down
33 changes: 16 additions & 17 deletions playgrounds/javascript/src/meilisearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,22 @@ const index = client.index<{ id: number; title: string; genres: string[] }>(
export async function addDocuments(): Promise<void> {
await client.deleteIndexIfExists(indexUid);

const task1 = await client.createIndex(indexUid);
await client.waitForTask(task1.taskUid);

const task2 = await index.addDocuments([
{ id: 1, title: "Carol", genres: ["Romance", "Drama"] },
{ id: 2, title: "Wonder Woman", genres: ["Action", "Adventure"] },
{ id: 3, title: "Life of Pi", genres: ["Adventure", "Drama"] },
{
id: 4,
title: "Mad Max: Fury Road",
genres: ["Adventure", "Science Fiction"],
},
{ id: 5, title: "Moana", genres: ["Fantasy", "Action"] },
{ id: 6, title: "Philadelphia", genres: ["Drama"] },
]);

await client.index(indexUid).waitForTask(task2.taskUid);
await client.createIndex(indexUid).waitTask();

await index
.addDocuments([
{ id: 1, title: "Carol", genres: ["Romance", "Drama"] },
{ id: 2, title: "Wonder Woman", genres: ["Action", "Adventure"] },
{ id: 3, title: "Life of Pi", genres: ["Adventure", "Drama"] },
{
id: 4,
title: "Mad Max: Fury Road",
genres: ["Adventure", "Science Fiction"],
},
{ id: 5, title: "Moana", genres: ["Fantasy", "Action"] },
{ id: 6, title: "Philadelphia", genres: ["Drama"] },
])
.waitTask();
}

export async function getAllHits(element: HTMLDivElement): Promise<void> {
Expand Down
79 changes: 24 additions & 55 deletions src/batch.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,38 @@
import type {
Config,
BatchObject,
BatchesQuery,
Batch,
BatchesResults,
BatchesResultsObject,
} from "./types.js";
import { HttpRequests } from "./http-requests.js";

class Batch {
uid: BatchObject["uid"];
details: BatchObject["details"];
stats: BatchObject["stats"];
startedAt: BatchObject["startedAt"];
finishedAt: BatchObject["finishedAt"];
duration: BatchObject["duration"];
progress: BatchObject["progress"];

constructor(batch: BatchObject) {
this.uid = batch.uid;
this.details = batch.details;
this.stats = batch.stats;
this.startedAt = batch.startedAt;
this.finishedAt = batch.finishedAt;
this.duration = batch.duration;
this.progress = batch.progress;
}
}

class BatchClient {
httpRequest: HttpRequests;

constructor(config: Config) {
this.httpRequest = new HttpRequests(config);
TasksOrBatchesQuery,
} from "./types/index.js";
import type { HttpRequests } from "./http-requests.js";

/**
* Class for handling batches.
*
* @see {@link https://www.meilisearch.com/docs/reference/api/batches}
*/
export class BatchClient {
readonly #httpRequest: HttpRequests;

constructor(httpRequests: HttpRequests) {
this.#httpRequest = httpRequests;
}

/**
* Get one batch
*
* @param uid - Unique identifier of the batch
* @returns
*/
/** {@link https://www.meilisearch.com/docs/reference/api/batches#get-one-batch} */
async getBatch(uid: number): Promise<Batch> {
const batch = await this.httpRequest.get<BatchObject>({
const batch = await this.#httpRequest.get<Batch>({
path: `batches/${uid}`,
});
return new Batch(batch);
return batch;
}

/**
* Get batches
*
* @param parameters - Parameters to browse the batches
* @returns Promise containing all batches
*/
async getBatches(batchesQuery?: BatchesQuery): Promise<BatchesResults> {
const batches = await this.httpRequest.get<BatchesResultsObject>({
/** {@link https://www.meilisearch.com/docs/reference/api/batches#get-batches} */
async getBatches(
batchesQuery?: TasksOrBatchesQuery,
): Promise<BatchesResults> {
const batches = await this.#httpRequest.get<BatchesResults>({
path: "batches",
params: batchesQuery,
});

return {
...batches,
results: batches.results.map((batch) => new Batch(batch)),
};
return batches;
}
}

export { BatchClient, Batch };
19 changes: 0 additions & 19 deletions src/enqueued-task.ts

This file was deleted.

3 changes: 2 additions & 1 deletion src/errors/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./meilisearch-api-error.js";
export * from "./meilisearch-request-error.js";
export * from "./meilisearch-error.js";
export * from "./meilisearch-timeout-error.js";
export * from "./meilisearch-request-timeout-error.js";
export * from "./meilisearch-task-timeout-error.js";
2 changes: 1 addition & 1 deletion src/errors/meilisearch-api-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { MeiliSearchErrorResponse } from "../types.js";
import type { MeiliSearchErrorResponse } from "../types/index.js";
import { MeiliSearchError } from "./meilisearch-error.js";

export class MeiliSearchApiError extends MeiliSearchError {
Expand Down
4 changes: 0 additions & 4 deletions src/errors/meilisearch-error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export class MeiliSearchError extends Error {
override name = "MeiliSearchError";

constructor(...params: ConstructorParameters<typeof Error>) {
super(...params);
}
}
13 changes: 13 additions & 0 deletions src/errors/meilisearch-request-timeout-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { MeiliSearchError } from "./meilisearch-error.js";

/** Error thrown when a HTTP request times out. */
export class MeiliSearchRequestTimeOutError extends MeiliSearchError {
override name = "MeiliSearchRequestTimeOutError";
override cause: { timeout: number; requestInit: RequestInit };

constructor(timeout: number, requestInit: RequestInit) {
super(`request timed out after ${timeout}ms`);

this.cause = { timeout, requestInit };
}
}
15 changes: 15 additions & 0 deletions src/errors/meilisearch-task-timeout-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MeiliSearchError } from "./meilisearch-error.js";

/** Error thrown when a waiting for a task times out. */
export class MeiliSearchTaskTimeOutError extends MeiliSearchError {
override name = "MeiliSearchTaskTimeOutError";
override cause: { taskUid: number; timeout: number };

constructor(taskUid: number, timeout: number) {
super(
`timeout of ${timeout}ms has exceeded on task ${taskUid} when waiting for it to be resolved.`,
);

this.cause = { taskUid, timeout };
}
}
9 changes: 0 additions & 9 deletions src/errors/meilisearch-timeout-error.ts

This file was deleted.

9 changes: 4 additions & 5 deletions src/http-requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import type {
MainRequestOptions,
URLSearchParamsRecord,
MeiliSearchErrorResponse,
} from "./types.js";
} from "./types/index.js";
import { PACKAGE_VERSION } from "./package-version.js";
import {
MeiliSearchError,
MeiliSearchApiError,
MeiliSearchRequestError,
MeiliSearchRequestTimeOutError,
} from "./errors/index.js";
import { addProtocolIfNotPresent, addTrailingSlash } from "./utils.js";

Expand Down Expand Up @@ -79,7 +80,7 @@ const TIMEOUT_ID = {};
* @remarks
* This could be a short few straight forward lines using {@link AbortSignal.any}
* and {@link AbortSignal.timeout}, but these aren't yet widely supported enough,
* nor polyfillable, at the time of writing.
* nor polyfill -able, at the time of writing.
* @returns A new function which starts the timeout, which then returns another
* function that clears the timeout
*/
Expand Down Expand Up @@ -240,9 +241,7 @@ export class HttpRequests {
throw new MeiliSearchRequestError(
url.toString(),
Object.is(error, TIMEOUT_ID)
? new Error(`request timed out after ${this.#requestTimeout}ms`, {
cause: init,
})
? new MeiliSearchRequestTimeOutError(this.#requestTimeout!, init)
: error,
);
} finally {
Expand Down
4 changes: 1 addition & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
export * from "./types.js";
export * from "./types/index.js";
export * from "./errors/index.js";
export * from "./indexes.js";
export * from "./enqueued-task.js";
export * from "./task.js";
import { MeiliSearch } from "./meilisearch.js";

/**
Expand Down
Loading