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

Improve HttpRequests #1741

Open
wants to merge 46 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
9f41065
Improve HttpRequests
flevi29 Oct 9, 2024
a32ed2d
Convert props to private, refactor, adapt tests
flevi29 Oct 9, 2024
118534d
Fix type issue
flevi29 Oct 9, 2024
942e889
Improve timeout, refactor
flevi29 Oct 10, 2024
455d8c3
Misc
flevi29 Oct 10, 2024
d1b442b
Fix browser env test
flevi29 Oct 10, 2024
c044319
Fix Node.js 18 fetch signal issue
flevi29 Oct 10, 2024
e6236f6
Add extra RequestInit for search
flevi29 Oct 11, 2024
4a49790
Refactor
flevi29 Oct 11, 2024
0e41c86
Fix type issues
flevi29 Oct 11, 2024
1949134
Fix some types
flevi29 Oct 11, 2024
ace3412
Make extraRequestInit function as it originally did
flevi29 Oct 11, 2024
3854e13
Remove unnecessary transformation
flevi29 Oct 11, 2024
d73d58d
Revert some type changes
flevi29 Oct 11, 2024
2d4849d
Optimize
flevi29 Oct 11, 2024
8825781
Merge branch 'main' into improve-http-request
flevi29 Oct 14, 2024
db9aa94
Merge with main branch
flevi29 Oct 30, 2024
432f338
Merge branch 'main' into improve-http-request
flevi29 Oct 31, 2024
d70f962
Merge branch 'main' into improve-http-request
flevi29 Nov 1, 2024
aa10d8c
Merge with main
flevi29 Dec 2, 2024
f81e7ca
Merge branch 'improve-http-request' of github.com:flevi29/meilisearch…
flevi29 Dec 6, 2024
f231e95
Merge branch 'main' into improve-http-request
flevi29 Dec 6, 2024
8f29bc2
Merge with main
flevi29 Dec 23, 2024
edff9a9
Merge branch 'main' into improve-http-request
flevi29 Dec 27, 2024
36c174f
Fix types
flevi29 Dec 27, 2024
a26cc1d
Merge branch 'main' into improve-http-request
flevi29 Jan 2, 2025
f0998be
Merge branch 'main' into improve-http-request
flevi29 Jan 3, 2025
f7b1e88
Merge with main
flevi29 Jan 8, 2025
a32c773
Fix type and style issues
flevi29 Jan 8, 2025
aaaaf3b
Test extra headers as well
flevi29 Jan 8, 2025
ed15092
Merge branch 'main' into improve-http-request
flevi29 Jan 8, 2025
54e83ba
Avoid saving fetch to a property, cast types in HttpRequests only
flevi29 Jan 10, 2025
905789b
Adjust test
flevi29 Jan 10, 2025
c3b57cd
Merge branch 'main' into improve-http-request
flevi29 Jan 10, 2025
4dfdeb0
Merge branch 'improve-http-request' of github.com:flevi29/meilisearch…
flevi29 Jan 10, 2025
576231a
Add docs, fix custom content type detection, deprecate httpClient
flevi29 Jan 10, 2025
5e0b1c3
Fix custom content type issue
flevi29 Jan 10, 2025
7e9ab0c
Misc
flevi29 Jan 10, 2025
12dbd8f
Fix test with malformed JSON string
flevi29 Jan 10, 2025
3c065d4
Fix handling of Content-Type, misc
flevi29 Jan 11, 2025
3cee506
Merge branch 'main' into improve-http-request
flevi29 Jan 15, 2025
5941fc0
Merge branch 'main' into improve-http-request
flevi29 Jan 17, 2025
71f3e40
Merge branch 'main' into improve-http-request
flevi29 Jan 21, 2025
c8cc95a
Merge branch 'main' into improve-http-request
flevi29 Jan 27, 2025
116305e
Merge branch 'main' into improve-http-request
flevi29 Jan 28, 2025
614aab9
Merge branch 'main' into improve-http-request
flevi29 Jan 29, 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"test": "vitest run --coverage",
"types:watch": "nodemon --config nodemon.json",
"types": "yarn tsc",
"test:env:browser": "yarn build && yarn --cwd tests/env/express && yarn --cwd tests/env/express test",
"test:env:browser": "yarn build && node scripts/copy-umd-file.js --to ./tests/env/express/public && yarn --cwd tests/env/express && yarn --cwd tests/env/express test",
"test:watch": "vitest watch",
"test:coverage": "yarn test",
"test:ci": "yarn test",
Expand Down
16 changes: 16 additions & 0 deletions scripts/copy-umd-file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { parseArgs } = require("node:util");
const { resolve, join, basename } = require("node:path");
const { copyFileSync } = require("node:fs");
const pkg = require("../package.json");

const {
values: { to },
} = parseArgs({ options: { to: { type: "string" } } });

if (to === undefined) {
throw new Error("required argument `to` missing");
}

const umdAbsolutePath = resolve(__dirname, join("..", pkg.jsdelivr));

copyFileSync(umdAbsolutePath, join(to, basename(pkg.jsdelivr)));
139 changes: 70 additions & 69 deletions src/clients/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,20 @@
ErrorStatusCode,
TokenSearchRules,
TokenOptions,
TasksQuery,
WaitOptions,
KeyUpdate,
IndexesQuery,
IndexesResults,
KeysQuery,
KeysResults,
TasksResults,
EnqueuedTaskObject,
SwapIndexesParams,
CancelTasksQuery,
DeleteTasksQuery,
MultiSearchParams,
MultiSearchResponse,
SearchResponse,
FederatedMultiSearchParams,
} from "../types";
import { HttpRequests } from "../http-requests";
import { TaskClient, Task } from "../task";
import { TaskClient } from "../task";
import { EnqueuedTask } from "../enqueued-task";

class Client {
Expand Down Expand Up @@ -98,7 +93,7 @@
* @returns Promise returning array of raw index information
*/
async getIndexes(
parameters: IndexesQuery = {},
parameters?: IndexesQuery,
): Promise<IndexesResults<Index[]>> {
const rawIndexes = await this.getRawIndexes(parameters);
const indexes: Index[] = rawIndexes.results.map(
Expand All @@ -114,13 +109,12 @@
* @returns Promise returning array of raw index information
*/
async getRawIndexes(
parameters: IndexesQuery = {},
parameters?: IndexesQuery,
): Promise<IndexesResults<IndexObject[]>> {
const url = `indexes`;
return await this.httpRequest.get<IndexesResults<IndexObject[]>>(
url,
parameters,
);
return (await this.httpRequest.get({
relativeURL: "indexes",
params: parameters,
})) as IndexesResults<IndexObject[]>;
}

/**
Expand All @@ -132,7 +126,7 @@
*/
async createIndex(
uid: string,
options: IndexOptions = {},
options?: IndexOptions,
): Promise<EnqueuedTask> {
return await Index.create(uid, options, this.config);
}
Expand All @@ -146,7 +140,7 @@
*/
async updateIndex(
uid: string,
options: IndexOptions = {},
options?: IndexOptions,
): Promise<EnqueuedTask> {
return await new Index(this.config, uid).update(options);
}
Expand Down Expand Up @@ -188,7 +182,12 @@
*/
async swapIndexes(params: SwapIndexesParams): Promise<EnqueuedTask> {
const url = "/swap-indexes";
return await this.httpRequest.post(url, params);
const taks = (await this.httpRequest.post({
relativeURL: url,
body: params,
})) as EnqueuedTaskObject;

return new EnqueuedTask(taks);
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug: previously this did not return EnqueuedTask, but rather EnqueuedTaskObject


///
Expand Down Expand Up @@ -226,11 +225,12 @@
): Promise<SearchResponse<T>>;
async multiSearch<T extends Record<string, unknown> = Record<string, any>>(
queries: MultiSearchParams | FederatedMultiSearchParams,
config?: Partial<Request>,

Check failure on line 228 in src/clients/client.ts

View workflow job for this annotation

GitHub Actions / style-check

'config' is defined but never used. Allowed unused args must match /^_/u
): Promise<MultiSearchResponse<T> | SearchResponse<T>> {
const url = `multi-search`;

return await this.httpRequest.post(url, queries, undefined, config);
return (await this.httpRequest.post({
relativeURL: "multi-search",
body: queries,
})) as MultiSearchResponse<T> | SearchResponse<T>;
}

///
Expand All @@ -243,8 +243,10 @@
* @param parameters - Parameters to browse the tasks
* @returns Promise returning all tasks
*/
async getTasks(parameters: TasksQuery = {}): Promise<TasksResults> {
return await this.tasks.getTasks(parameters);
async getTasks(
...params: Parameters<TaskClient["getTasks"]>
): ReturnType<TaskClient["getTasks"]> {
return await this.tasks.getTasks(...params);
}

/**
Expand All @@ -253,8 +255,10 @@
* @param taskUid - Task identifier
* @returns Promise returning a task
*/
async getTask(taskUid: number): Promise<Task> {
return await this.tasks.getTask(taskUid);
async getTask(
...params: Parameters<TaskClient["getTask"]>
): ReturnType<TaskClient["getTask"]> {
return await this.tasks.getTask(...params);
}

/**
Expand All @@ -265,13 +269,9 @@
* @returns Promise returning an array of tasks
*/
async waitForTasks(
taskUids: number[],
{ timeOutMs = 5000, intervalMs = 50 }: WaitOptions = {},
): Promise<Task[]> {
return await this.tasks.waitForTasks(taskUids, {
timeOutMs,
intervalMs,
});
...params: Parameters<TaskClient["waitForTasks"]>
): ReturnType<TaskClient["waitForTasks"]> {
return await this.tasks.waitForTasks(...params);
}

/**
Expand All @@ -282,13 +282,9 @@
* @returns Promise returning an array of tasks
*/
async waitForTask(
taskUid: number,
{ timeOutMs = 5000, intervalMs = 50 }: WaitOptions = {},
): Promise<Task> {
return await this.tasks.waitForTask(taskUid, {
timeOutMs,
intervalMs,
});
...params: Parameters<TaskClient["waitForTask"]>
): ReturnType<TaskClient["waitForTask"]> {
return await this.tasks.waitForTask(...params);
}

/**
Expand All @@ -297,8 +293,10 @@
* @param parameters - Parameters to filter the tasks.
* @returns Promise containing an EnqueuedTask
*/
async cancelTasks(parameters: CancelTasksQuery): Promise<EnqueuedTask> {
return await this.tasks.cancelTasks(parameters);
async cancelTasks(
...params: Parameters<TaskClient["cancelTasks"]>
): ReturnType<TaskClient["cancelTasks"]> {
return await this.tasks.cancelTasks(...params);
}

/**
Expand All @@ -307,8 +305,10 @@
* @param parameters - Parameters to filter the tasks.
* @returns Promise containing an EnqueuedTask
*/
async deleteTasks(parameters: DeleteTasksQuery = {}): Promise<EnqueuedTask> {
return await this.tasks.deleteTasks(parameters);
async deleteTasks(
...params: Parameters<TaskClient["deleteTasks"]>
): ReturnType<TaskClient["deleteTasks"]> {
return await this.tasks.deleteTasks(...params);
}

///
Expand All @@ -321,9 +321,11 @@
* @param parameters - Parameters to browse the indexes
* @returns Promise returning an object with keys
*/
async getKeys(parameters: KeysQuery = {}): Promise<KeysResults> {
const url = `keys`;
const keys = await this.httpRequest.get<KeysResults>(url, parameters);
async getKeys(parameters?: KeysQuery): Promise<KeysResults> {
const keys = (await this.httpRequest.get({
relativeURL: "keys",
params: parameters,
})) as KeysResults;

keys.results = keys.results.map((key) => ({
...key,
Expand All @@ -341,8 +343,9 @@
* @returns Promise returning a key
*/
async getKey(keyOrUid: string): Promise<Key> {
const url = `keys/${keyOrUid}`;
return await this.httpRequest.get<Key>(url);
return (await this.httpRequest.get({
relativeURL: `keys/${keyOrUid}`,
})) as Key;
}

/**
Expand All @@ -352,8 +355,10 @@
* @returns Promise returning a key
*/
async createKey(options: KeyCreation): Promise<Key> {
const url = `keys`;
return await this.httpRequest.post(url, options);
return (await this.httpRequest.post({
relativeURL: "keys",
body: options,
})) as Key;
}

/**
Expand All @@ -364,8 +369,10 @@
* @returns Promise returning a key
*/
async updateKey(keyOrUid: string, options: KeyUpdate): Promise<Key> {
const url = `keys/${keyOrUid}`;
return await this.httpRequest.patch(url, options);
return (await this.httpRequest.patch({
relativeURL: `keys/${keyOrUid}`,
body: options,
})) as Key;
}

/**
Expand All @@ -375,8 +382,7 @@
* @returns
*/
async deleteKey(keyOrUid: string): Promise<void> {
const url = `keys/${keyOrUid}`;
return await this.httpRequest.delete<any>(url);
await this.httpRequest.delete({ relativeURL: `keys/${keyOrUid}` });
}

///
Expand All @@ -389,8 +395,7 @@
* @returns Promise returning an object with health details
*/
async health(): Promise<Health> {
const url = `health`;
return await this.httpRequest.get<Health>(url);
return (await this.httpRequest.get({ relativeURL: "health" })) as Health;
}

/**
Expand All @@ -400,9 +405,8 @@
*/
async isHealthy(): Promise<boolean> {
try {
const url = `health`;
await this.httpRequest.get(url);
return true;
const { status } = await this.health();
return status === "available";
} catch {
return false;
}
Expand All @@ -418,8 +422,7 @@
* @returns Promise returning object of all the stats
*/
async getStats(): Promise<Stats> {
const url = `stats`;
return await this.httpRequest.get<Stats>(url);
return (await this.httpRequest.get({ relativeURL: "stats" })) as Stats;
}

///
Expand All @@ -432,8 +435,7 @@
* @returns Promise returning object with version details
*/
async getVersion(): Promise<Version> {
const url = `version`;
return await this.httpRequest.get<Version>(url);
return (await this.httpRequest.get({ relativeURL: "version" })) as Version;
}

///
Expand All @@ -446,10 +448,10 @@
* @returns Promise returning object of the enqueued task
*/
async createDump(): Promise<EnqueuedTask> {
const url = `dumps`;
const task = await this.httpRequest.post<undefined, EnqueuedTaskObject>(
url,
);
const task = (await this.httpRequest.post({
relativeURL: "dumps",
})) as EnqueuedTaskObject;

return new EnqueuedTask(task);
}

Expand All @@ -463,10 +465,9 @@
* @returns Promise returning object of the enqueued task
*/
async createSnapshot(): Promise<EnqueuedTask> {
const url = `snapshots`;
const task = await this.httpRequest.post<undefined, EnqueuedTaskObject>(
url,
);
const task = (await this.httpRequest.post({
relativeURL: "snapshots",
})) as EnqueuedTaskObject;

return new EnqueuedTask(task);
}
Expand Down
Loading
Loading