-
Notifications
You must be signed in to change notification settings - Fork 558
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
Added Custom Headers Logic to Query #9458
Changes from 4 commits
f6c40d2
9ca80e0
0883fb4
0f74197
596168e
45a6a50
f897a9f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -2,18 +2,40 @@ import careConfig from "@careConfig"; | |||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
import { QueryError } from "@/Utils/request/queryError"; | ||||||||||||||||||||||||||||||||||||||||||
import { getResponseBody } from "@/Utils/request/request"; | ||||||||||||||||||||||||||||||||||||||||||
import { QueryOptions, Route } from "@/Utils/request/types"; | ||||||||||||||||||||||||||||||||||||||||||
import { Route } from "@/Utils/request/types"; | ||||||||||||||||||||||||||||||||||||||||||
// Remove conflicting QueryOptions import | ||||||||||||||||||||||||||||||||||||||||||
import { makeHeaders, makeUrl } from "@/Utils/request/utils"; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// Define QueryOptions locally, including the 'headers' property | ||||||||||||||||||||||||||||||||||||||||||
export interface QueryOptions<TBody> { | ||||||||||||||||||||||||||||||||||||||||||
body?: TBody; | ||||||||||||||||||||||||||||||||||||||||||
queryParams?: Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||
pathParams?: Record<string, any>; | ||||||||||||||||||||||||||||||||||||||||||
silent?: boolean; | ||||||||||||||||||||||||||||||||||||||||||
signal?: AbortSignal; | ||||||||||||||||||||||||||||||||||||||||||
headers?: Record<string, string>; // Add headers support | ||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. let's not duplicate the type. this is already present in |
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
async function queryRequest<TData, TBody>( | ||||||||||||||||||||||||||||||||||||||||||
{ path, method, noAuth }: Route<TData, TBody>, | ||||||||||||||||||||||||||||||||||||||||||
options?: QueryOptions<TBody>, | ||||||||||||||||||||||||||||||||||||||||||
): Promise<TData> { | ||||||||||||||||||||||||||||||||||||||||||
const url = `${careConfig.apiUrl}${makeUrl(path, options?.queryParams, options?.pathParams)}`; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// Convert Headers object to a plain object | ||||||||||||||||||||||||||||||||||||||||||
const defaultHeaders = Object.fromEntries( | ||||||||||||||||||||||||||||||||||||||||||
makeHeaders(noAuth ?? false).entries(), | ||||||||||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
// Merge default headers with custom headers, with custom headers taking precedence | ||||||||||||||||||||||||||||||||||||||||||
const headers = { | ||||||||||||||||||||||||||||||||||||||||||
...defaultHeaders, | ||||||||||||||||||||||||||||||||||||||||||
...(options?.headers || {}), | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Improve header handling using the Headers API. The current implementation converts Headers to a plain object and then merges them, which:
Consider using the Headers API directly: - // Convert Headers object to a plain object
- const defaultHeaders = Object.fromEntries(
- makeHeaders(noAuth ?? false).entries(),
- );
-
- // Merge default headers with custom headers, with custom headers taking precedence
- const headers = {
- ...defaultHeaders,
- ...(options?.headers || {}),
- };
+ // Use Headers API for better header management
+ const headers = new Headers(makeHeaders(noAuth ?? false));
+
+ // Merge custom headers
+ if (options?.headers) {
+ const customHeaders = new Headers(options.headers);
+ customHeaders.forEach((value, key) => {
+ headers.set(key, value);
+ });
+ } 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any performance overhead introduced by using the Headers API compared to a plain object? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @srijantrpth, using the Headers API introduces minimal performance overhead compared to using a plain object. The Headers API is optimized for managing HTTP headers efficiently, and it provides benefits like built-in normalization and validation. These features enhance the robustness of header handling without significantly impacting performance. In most cases, the slight overhead is negligible and is outweighed by the advantages in code maintainability and correctness. If performance is a critical concern, we can consider benchmarking both implementations to measure any differences. However, it's unlikely to be a bottleneck under normal circumstances. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it used across all our environments? If yes, let me rectify it else it's fine There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @srijantrpth, the Headers API is indeed used in our codebase. Specifically, in const headers = new Headers({
"Content-Type": "application/json",
Accept: "application/json",
...additionalHeaders,
}); Using the Headers API in your implementation would maintain consistency across all our environments. It would be beneficial to adopt it for better header management. 🔗 Analysis chain🏁 Scripts executedThe following scripts were executed for the analysis: Script:
Length of output: 324 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, let me rectify the code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @srijantrpth, glad to hear you're updating the code! Let me know if you need any assistance. If you found this review helpful, would you consider giving us a shout-out on X? |
||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess you may be confused about the objective. This would "overwrite" the headers. But the intended behaviour is to "append" the headers while preserving the previously set headers. Let's stick with using the Headers API. It's there for a purpose :) -- best practices Refer: https://developer.mozilla.org/en-US/docs/Web/API/Headers/append ![]() There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am sorry about that, I'll fix this and let you know |
||||||||||||||||||||||||||||||||||||||||||
const fetchOptions: RequestInit = { | ||||||||||||||||||||||||||||||||||||||||||
method, | ||||||||||||||||||||||||||||||||||||||||||
headers: makeHeaders(noAuth ?? false), | ||||||||||||||||||||||||||||||||||||||||||
headers, | ||||||||||||||||||||||||||||||||||||||||||
signal: options?.signal, | ||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Use
HeadersInit
type for better compatibility with the Fetch API.The
headers
property type should beHeadersInit
instead ofRecord<string, string>
for better compatibility with the Fetch API's Headers interface.Apply this diff:
📝 Committable suggestion