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 43 commits into
base: main
Choose a base branch
from
Open

Conversation

flevi29
Copy link
Collaborator

@flevi29 flevi29 commented Oct 9, 2024

Pull Request

Sorry for the huge PR, but HttpRequests is core, and is used everywhere.

What does this PR do?

Caution

BREAKING

Rename Config.requestConfig -> Config.requestInit, signal can no longer be passed to it.

Warning

Deprecate Config.httpClient #1824

PR checklist

Please check if your PR fulfills the following requirements:

  • Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)?
  • Have you read the contributing guidelines?
  • Have you made sure that the title is accurate and descriptive of the changes?

Thank you so much for contributing to Meilisearch!

Copy link

codecov bot commented Oct 9, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.61%. Comparing base (def691d) to head (71f3e40).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1741      +/-   ##
==========================================
+ Coverage   98.21%   98.61%   +0.40%     
==========================================
  Files          17       17              
  Lines        1565     1594      +29     
  Branches      333      344      +11     
==========================================
+ Hits         1537     1572      +35     
+ Misses         28       22       -6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@flevi29 flevi29 added maintenance Issue about maintenance (CI, tests, refacto...) breaking-change The related changes are breaking for the users and removed maintenance Issue about maintenance (CI, tests, refacto...) labels Oct 9, 2024
@flevi29 flevi29 linked an issue Oct 10, 2024 that may be closed by this pull request
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

Comment on lines 72 to 95
test(`${permission} key: Create client with custom headers (object)`, async () => {
const key = await getKey(permission);
const client = new MeiliSearch({
...config,
apiKey: key,
requestInit: {
headers: {
"Hello-There!": "General Kenobi",
},
},
},
});

assert.isTrue(await client.isHealthy());

assert.isDefined(fetchSpy.mock.lastCall);
const [, requestInit] = fetchSpy.mock.lastCall!;

assert.isDefined(requestInit?.headers);
assert.instanceOf(requestInit!.headers, Headers);
assert.strictEqual(
(requestInit!.headers! as Headers).get("Hello-There!"),
"General Kenobi",
);
});
Copy link
Collaborator Author

@flevi29 flevi29 Oct 10, 2024

Choose a reason for hiding this comment

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

Because headers are internal implementation details, to check them we have to spy on fetch now.

src/indexes.ts Outdated Show resolved Hide resolved
src/indexes.ts Outdated Show resolved Hide resolved
src/indexes.ts Outdated Show resolved Hide resolved
src/indexes.ts Outdated Show resolved Hide resolved
src/indexes.ts Outdated Show resolved Hide resolved
@flevi29 flevi29 marked this pull request as ready for review October 11, 2024 08:37
@flevi29 flevi29 requested a review from brunoocasali October 11, 2024 11:00
src/http-requests.ts Outdated Show resolved Hide resolved
@flevi29 flevi29 requested a review from Strift January 8, 2025 13:35
Copy link
Collaborator

@Strift Strift left a comment

Choose a reason for hiding this comment

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

Hi @flevi29, thanks for the great work 🙌

I would love to merge this too before the new release!I have a few questions:

Generic methods types

Any reason to switch from method<Type>() to method() as Type?
One example is in indexes.ts: await this.httpRequest.get(...) as IndexObject;, but there are other occurrences of this.

The former approach seems to provide a better DX.

Renaming config to extraRequestInit

I'm all for renaming it in variables and types used internally to make the code base more self-explanatory. But I'm not sure about changing the user-facing field names.

I feel like config / requestConfig are more familiar field names for configuring the underlying HTTP client/request.

Plus, changing them would be a breaking change, right?

src/indexes.ts Outdated Show resolved Hide resolved
tests/search.test.ts Show resolved Hide resolved
expect(e.name).toEqual("MeiliSearchRequestError");
}
});

test(`${permission} key: search should be aborted on already abort signal`, async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't understand the name of this test. Can you explain, or rephrase it, please?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

First it tests whether search gets immediately aborted on an already aborted signal, then it test whether it gets aborted after the request has already been sent. I have renamed it to ${permission} key: search should be aborted on abort signal.

@flevi29
Copy link
Collaborator Author

flevi29 commented Jan 10, 2025

Any reason to switch from method<Type>() to method() as Type?
One example is in indexes.ts: await this.httpRequest.get(...) as IndexObject;, but there are other occurrences of this.

The idea was that when we are calling an external web API, the shape of the response cannot be guaranteed, so we're casting to be explicit about this fact. But you are right, it is an overkill, I'm moving the casting part to HttpRequests.

I feel like config / requestConfig are more familiar field names for configuring the underlying HTTP client/request.

I felt like "RequestInit" is more explicit, because that's literally what it's named and what we're using and allowing via types and it is the standard at this point, while config is more implicit and subjective, even if a lot of people might've gotten used to that one. But I can change it if you insist on it.
https://developer.mozilla.org/en-US/docs/Web/API/RequestInit

It is named "extra", because we're applying 3 layers of RequestInits, once from the method body, then from HttpRequests #requestInit, and then from the method's parameter extraRequestInit. My idea is that I want to be a little more clear or explicit about this fact to reduce confusion, but I do think it can still use some improvements. What do you think?

Also there's an open issue about adding this extra config to all of the methods #1476

@flevi29
Copy link
Collaborator Author

flevi29 commented Jan 10, 2025

For now I've made the requested changes, but I do plan to add better documentation to HttpRequests in this same PR.

@flevi29 flevi29 requested a review from Strift January 10, 2025 12:51
@flevi29
Copy link
Collaborator Author

flevi29 commented Jan 10, 2025

I'm also deprecating Config.httpClient. Updated description.

@flevi29
Copy link
Collaborator Author

flevi29 commented Jan 11, 2025

Plus, changing them would be a breaking change, right?

It's not a breaking change, because it's a positional parameter, not an object property in the search method.
But it is a breaking change in the main config. I've updated the description.

@flevi29
Copy link
Collaborator Author

flevi29 commented Jan 11, 2025

Actually the private properties change is non-breaking, because HttpRequests wasn't exported in the main index file before.

@Strift
Copy link
Collaborator

Strift commented Jan 15, 2025

Hey @flevi29, sorry we couldn't get this in time for #1826.

The next pre-release cycle starts on January 27 and I wanted to have time at least 2 weeks to fix bugs before getting into the new features implementation.

But I think this PR is already in a good state. We'll probably be able to merge it soon after updating the package for Meilisearch 1.13.

@flevi29
Copy link
Collaborator Author

flevi29 commented Jan 15, 2025

Alright, no problem, I just didn't know you guys talked before releasing. But to be fair I'm also excited to get my PRs merged 😅.

@curquiza
Copy link
Member

curquiza commented Jan 15, 2025

Once v1.13 is out, merging your PR will be the priority @flevi29 😉
Thank you very much for your work, as always!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking-change The related changes are breaking for the users bug Something isn't working maintenance Issue about maintenance (CI, tests, refacto...)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Timeout implementation for requests should be re-thought
3 participants