Skip to content

Commit a52fb6f

Browse files
authored
fix optional params typing in few APIs (#1378)
1 parent a8e1abb commit a52fb6f

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/internal/client.ts

+33-29
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ export class TypedClient {
904904
* Creates the bucket `bucketName`.
905905
*
906906
*/
907-
async makeBucket(bucketName: string, region: Region = '', makeOpts: MakeBucketOpt = {}): Promise<void> {
907+
async makeBucket(bucketName: string, region: Region = '', makeOpts?: MakeBucketOpt): Promise<void> {
908908
if (!isValidBucketName(bucketName)) {
909909
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
910910
}
@@ -917,7 +917,7 @@ export class TypedClient {
917917
if (!isString(region)) {
918918
throw new TypeError('region should be of type "string"')
919919
}
920-
if (!isObject(makeOpts)) {
920+
if (makeOpts && !isObject(makeOpts)) {
921921
throw new TypeError('makeOpts should be of type "object"')
922922
}
923923

@@ -943,7 +943,7 @@ export class TypedClient {
943943
const method = 'PUT'
944944
const headers: RequestHeaders = {}
945945

946-
if (makeOpts.ObjectLocking) {
946+
if (makeOpts && makeOpts.ObjectLocking) {
947947
headers['x-amz-bucket-object-lock-enabled'] = true
948948
}
949949

@@ -1009,7 +1009,7 @@ export class TypedClient {
10091009
/**
10101010
* Callback is called with readable stream of the object content.
10111011
*/
1012-
async getObject(bucketName: string, objectName: string, getOpts: GetObjectOpts = {}): Promise<stream.Readable> {
1012+
async getObject(bucketName: string, objectName: string, getOpts?: GetObjectOpts): Promise<stream.Readable> {
10131013
if (!isValidBucketName(bucketName)) {
10141014
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
10151015
}
@@ -1032,7 +1032,7 @@ export class TypedClient {
10321032
objectName: string,
10331033
offset: number,
10341034
length = 0,
1035-
getOpts: GetObjectOpts = {},
1035+
getOpts?: GetObjectOpts,
10361036
): Promise<stream.Readable> {
10371037
if (!isValidBucketName(bucketName)) {
10381038
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
@@ -1060,17 +1060,26 @@ export class TypedClient {
10601060
}
10611061
}
10621062

1063-
const sseHeaders: Record<string, string> = {
1064-
...(getOpts.SSECustomerAlgorithm && {
1065-
'X-Amz-Server-Side-Encryption-Customer-Algorithm': getOpts.SSECustomerAlgorithm,
1066-
}),
1067-
...(getOpts.SSECustomerKey && { 'X-Amz-Server-Side-Encryption-Customer-Key': getOpts.SSECustomerKey }),
1068-
...(getOpts.SSECustomerKeyMD5 && { 'X-Amz-Server-Side-Encryption-Customer-Key-MD5': getOpts.SSECustomerKeyMD5 }),
1063+
let query = ''
1064+
let headers: RequestHeaders = {
1065+
...(range !== '' && { range }),
10691066
}
10701067

1071-
const headers: RequestHeaders = {
1072-
...prependXAMZMeta(sseHeaders),
1073-
...(range !== '' && { range }),
1068+
if (getOpts) {
1069+
const sseHeaders: Record<string, string> = {
1070+
...(getOpts.SSECustomerAlgorithm && {
1071+
'X-Amz-Server-Side-Encryption-Customer-Algorithm': getOpts.SSECustomerAlgorithm,
1072+
}),
1073+
...(getOpts.SSECustomerKey && { 'X-Amz-Server-Side-Encryption-Customer-Key': getOpts.SSECustomerKey }),
1074+
...(getOpts.SSECustomerKeyMD5 && {
1075+
'X-Amz-Server-Side-Encryption-Customer-Key-MD5': getOpts.SSECustomerKeyMD5,
1076+
}),
1077+
}
1078+
query = qs.stringify(getOpts)
1079+
headers = {
1080+
...prependXAMZMeta(sseHeaders),
1081+
...headers,
1082+
}
10741083
}
10751084

10761085
const expectedStatusCodes = [200]
@@ -1079,7 +1088,6 @@ export class TypedClient {
10791088
}
10801089
const method = 'GET'
10811090

1082-
const query = qs.stringify(getOpts)
10831091
return await this.makeRequestAsync({ method, bucketName, objectName, headers, query }, '', expectedStatusCodes)
10841092
}
10851093

@@ -1092,12 +1100,7 @@ export class TypedClient {
10921100
* @param filePath - path to which the object data will be written to
10931101
* @param getOpts - Optional object get option
10941102
*/
1095-
async fGetObject(
1096-
bucketName: string,
1097-
objectName: string,
1098-
filePath: string,
1099-
getOpts: GetObjectOpts = {},
1100-
): Promise<void> {
1103+
async fGetObject(bucketName: string, objectName: string, filePath: string, getOpts?: GetObjectOpts): Promise<void> {
11011104
// Input validation.
11021105
if (!isValidBucketName(bucketName)) {
11031106
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
@@ -1153,19 +1156,20 @@ export class TypedClient {
11531156
/**
11541157
* Stat information of the object.
11551158
*/
1156-
async statObject(bucketName: string, objectName: string, statOpts: StatObjectOpts = {}): Promise<BucketItemStat> {
1159+
async statObject(bucketName: string, objectName: string, statOpts?: StatObjectOpts): Promise<BucketItemStat> {
1160+
const statOptDef = statOpts || {}
11571161
if (!isValidBucketName(bucketName)) {
11581162
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
11591163
}
11601164
if (!isValidObjectName(objectName)) {
11611165
throw new errors.InvalidObjectNameError(`Invalid object name: ${objectName}`)
11621166
}
11631167

1164-
if (!isObject(statOpts)) {
1168+
if (!isObject(statOptDef)) {
11651169
throw new errors.InvalidArgumentError('statOpts should be of type "object"')
11661170
}
11671171

1168-
const query = qs.stringify(statOpts)
1172+
const query = qs.stringify(statOptDef)
11691173
const method = 'HEAD'
11701174
const res = await this.makeRequestAsyncOmit({ method, bucketName, objectName, query })
11711175

@@ -1574,7 +1578,7 @@ export class TypedClient {
15741578
/**
15751579
* Uploads the object using contents from a file
15761580
*/
1577-
async fPutObject(bucketName: string, objectName: string, filePath: string, metaData: ObjectMetaData = {}) {
1581+
async fPutObject(bucketName: string, objectName: string, filePath: string, metaData?: ObjectMetaData) {
15781582
if (!isValidBucketName(bucketName)) {
15791583
throw new errors.InvalidBucketNameError('Invalid bucket name: ' + bucketName)
15801584
}
@@ -1585,12 +1589,12 @@ export class TypedClient {
15851589
if (!isString(filePath)) {
15861590
throw new TypeError('filePath should be of type "string"')
15871591
}
1588-
if (!isObject(metaData)) {
1592+
if (metaData && !isObject(metaData)) {
15891593
throw new TypeError('metaData should be of type "object"')
15901594
}
15911595

15921596
// Inserts correct `content-type` attribute based on metaData and filePath
1593-
metaData = insertContentType(metaData, filePath)
1597+
metaData = insertContentType(metaData || {}, filePath)
15941598
const stat = await fsp.lstat(filePath)
15951599
return await this.putObject(bucketName, objectName, fs.createReadStream(filePath), stat.size, metaData)
15961600
}
@@ -1945,7 +1949,7 @@ export class TypedClient {
19451949
/**
19461950
* Get the tags associated with a bucket OR an object
19471951
*/
1948-
async getObjectTagging(bucketName: string, objectName: string, getOpts: GetObjectOpts = {}): Promise<Tag[]> {
1952+
async getObjectTagging(bucketName: string, objectName: string, getOpts?: GetObjectOpts): Promise<Tag[]> {
19491953
const method = 'GET'
19501954
let query = 'tagging'
19511955

@@ -1955,7 +1959,7 @@ export class TypedClient {
19551959
if (!isValidObjectName(objectName)) {
19561960
throw new errors.InvalidBucketNameError('Invalid object name: ' + objectName)
19571961
}
1958-
if (!isObject(getOpts)) {
1962+
if (getOpts && !isObject(getOpts)) {
19591963
throw new errors.InvalidArgumentError('getOpts should be of type "object"')
19601964
}
19611965

0 commit comments

Comments
 (0)