-
Notifications
You must be signed in to change notification settings - Fork 48
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
Conversation
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}) { |
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.
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.
9aea0d4
to
b83d0b5
Compare
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.
b83d0b5
to
2aeb62c
Compare
* @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}) { |
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.
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.
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.)