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

[bugfix] default params for signedUrl() #749

Merged
merged 1 commit into from
Jan 1, 2024
Merged

Conversation

jameshadfield
Copy link
Member

The default parameter values for this function would only be used in the case where no argument was provided, which makes no sense as a bucket and key will always be needed. These changes preserve the intended defaults of headers={} and method='GET'. (Reading the previous code it seems the intention was to allow a default method of 'GET', despite the docstring indicating it was required.)

  • Checks pass

@jameshadfield jameshadfield marked this pull request as ready for review November 7, 2023 03:05
@nextstrain-bot nextstrain-bot temporarily deployed to nextstrain-s-james-fix--6sn87p November 7, 2023 03:05 Inactive
jameshadfield added a commit that referenced this pull request Nov 7, 2023
This is #749 here for testing purposes only
src/s3.js Outdated
export async function signedUrl({method, bucket, key, headers, expiresIn, issuedAt} = {method: "GET", headers: {}}) {
const normalizedHeaders = normalizeHeaders(headers);
export async function signedUrl({method, bucket, key, headers, expiresIn, issuedAt}) {
Copy link
Member

Choose a reason for hiding this comment

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

Ahh, thanks for the catch! I get this backwards sometimes. I meant to write:

-export async function signedUrl({method, bucket, key, headers, expiresIn, issuedAt} = {method: "GET", headers: {}}) {
+export async function signedUrl({method = "GET", bucket, key, headers = {}, expiresIn, issuedAt} = {}) {

which makes the headers || {} and method || 'GET' in the function body unnecessary. This is my preferred way to support optional args in a destructuring, as it naturally documents them in the function signature.

@jameshadfield
Copy link
Member Author

jameshadfield commented Jan 1, 2024

Updated to use the requested syntax, rebased onto master and tested locally. Will merge once CI passes.

The default parameter values for this function would only be used in the
case where no argument was provided, which makes no sense as a bucket
and key will always be needed.
@jameshadfield jameshadfield merged commit 0913b45 into master Jan 1, 2024
7 checks passed
@jameshadfield jameshadfield deleted the james/fix-s3-params branch January 1, 2024 07:09
* @param {object} [params.headers] - Content-Type and/or Content-Encoding headers for PUT
* @param {number} [params.expiresIn] - seconds until the URL expires
* @param {number} [params.issuedAt] - absolute time in seconds since the Unix epoch at which the signed URL should be considered issued at, i.e. when the countdown for expiresIn starts
* @returns {string} signed URL
*/
export async function signedUrl({method, bucket, key, headers, expiresIn, issuedAt} = {method: "GET", headers: {}}) {
export async function signedUrl({method="GET", bucket, key, headers={}, expiresIn, issuedAt}) {
Copy link
Member

@tsibley tsibley Jan 10, 2024

Choose a reason for hiding this comment

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

FWIW, this dropped the trailing = {} that I'd intentionally suggested in {method="GET", …} = {}. Without it, a call to signedUrl() generates an error like:

Uncaught TypeError: Cannot read properties of undefined (reading 'method')

While calling signedUrl() is always an error, omitting the default = {} seems a weirder way to throw the error compared to signedUrl({}):

Uncaught Error: No value provided for input HTTP label: Bucket.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants