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

feat!: Request Revamp #1938

Merged
merged 11 commits into from
Feb 15, 2025
Merged

feat!: Request Revamp #1938

merged 11 commits into from
Feb 15, 2025

Conversation

d-goog
Copy link
Collaborator

@d-goog d-goog commented Feb 12, 2025

Description

This is a large refactor of the auth library's internals with sweeping improvements in UX, correctness, performance, security, and scalability.

Examples:

  • UX: Adopts fetch API conventions
  • Correctness: Allows users to add charset to content-types and removes responseType parameter from GaxiosOptions (which was problematic in error cases)
  • Performance: There is far less object-copying when things can be safely reused, especially for Headers

Testing

All existing tests pass and enhanced numerous throughout the codebase. Notably, the sample tests required minimal changes.

Impact

This revamp brings number of improvements to library:

  • It returns users predictable, consistent Headers, while allowing the open, flexible HeadersInit for requests.
  • Bumps our dependencies to the Node 18+ versions
  • Simplifies requests:
    • With native support for the RequestInit body types, users can omit certain headers, such as content-type
  • Improves cross-platform compatibility through JS standards adoption (Request, Headers, URLSearchParams, etc.)/

Additional Information

While a large PR, most of the changes are internal and doesn't affect external API consumption much outside from returning Headers.

Note: a large portion of this PR are non-functional changes related to Node 18's Headers - which are not case-insensitive when compared in tests (which required a lot of lower-casing header names).

A follow-up PR will add fetch support.

Fixes: #1829

🦕

@d-goog d-goog requested review from a team as code owners February 12, 2025 10:22
@product-auto-label product-auto-label bot added the size: l Pull request size is large. label Feb 12, 2025
@d-goog d-goog mentioned this pull request Feb 12, 2025
4 tasks
Copy link

Warning: This pull request is touching the following templated files:

@product-auto-label product-auto-label bot added size: xl Pull request size is extra large. and removed size: l Pull request size is large. labels Feb 12, 2025
@product-auto-label product-auto-label bot added size: l Pull request size is large. and removed size: xl Pull request size is extra large. labels Feb 12, 2025
@d-goog d-goog changed the title refactor!: Request Revamp feat!: Request Revamp Feb 12, 2025
@@ -60,15 +49,15 @@ interface GenerateAuthHeaderMapOptions {
// The AWS service URL query string.
canonicalQuerystring: string;
// The HTTP method used to call this API.
method: HttpMethod;
method: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this type changing? seems more flexible? also not clear if this change has to do with the gaxios upgrade.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The HttpMethod was a handwritten version for what the spec provides. The spec accepts any method as a string.

if (typeof requestPayload !== 'string' && requestPayload !== undefined) {
throw new TypeError(
`'requestPayload' is expected to be a string if provided. Got: ${requestPayload}`
);
Copy link
Contributor

Choose a reason for hiding this comment

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

do we know what requestPayload type could be? This could be an ugly error Got [object Object]

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We want it to be a string, however we marshal objects to strings a few lines above it. This is preserving the existing functionality and makes it clearer, we used to throw later in the call stack.

}
opts.headers = Gaxios.mergeHeaders(opts.headers);

this.addUserProjectAndAuthHeaders(opts.headers, r.headers);
Copy link
Contributor

Choose a reason for hiding this comment

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

nice DRY!

}
} else {

if (!METHODS_SUPPORTING_REQUEST_BODY.includes(method)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I see why you made this change - definitely more readable! - but ideally could be another PR (given this is a big PR already, and this is more of a refactor, and we're already doing so many other gaxios changes). NB.

// eslint-disable-next-line @typescript-eslint/no-explicit-any
delete (values as {[index: string]: any})[key];

// Keep defined fields.
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, another small refactor that in general seems harmless but in the context of a feature, might be better as a F/U.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Understood, however this required the refactor now given the more precise type requirements of
URLSearchParams.

@d-goog d-goog merged commit f23e807 into main Feb 15, 2025
17 checks passed
@d-goog d-goog deleted the request-revamp branch February 15, 2025 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DeprecationWarning: The punycode module is deprecated. Please use a userland alternative instead
2 participants