Skip to content

Commit

Permalink
fix: refactor list API to support strict in TsConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Sridhar committed Aug 16, 2023
1 parent 8a86e4f commit be25267
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/storage/src/AwsClients/S3/listObjectsV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const listObjectsV2Deserializer = async (
): Promise<ListObjectsV2Output> => {
if (response.statusCode >= 300) {
const error = await parseXmlError(response);
// @ts-expect-error error is always set when statusCode >= 300
throw StorageError.fromServiceError(error, response.statusCode);
} else {
const parsed = await parseXmlBody(response);
Expand Down
38 changes: 23 additions & 15 deletions packages/storage/src/providers/s3/apis/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
// SPDX-License-Identifier: Apache-2.0

import { AmplifyV6 } from '@aws-amplify/core';
import { ListObjectsV2Input, listObjectsV2 } from '../../../AwsClients/S3';
import {
ListObjectsV2Input,
ListObjectsV2Output,
listObjectsV2,
} from '../../../AwsClients/S3';
import {
StorageConfig,
StorageListRequest,
Expand Down Expand Up @@ -54,22 +58,23 @@ export const list: S3ListApi = async (
): Promise<S3ListAllResult | S3ListPaginateResult> => {
const { identityId, credentials } = await resolveCredentials();
const { defaultAccessLevel, bucket, region } = resolveStorageConfig();
const {
path,
options: { accessLevel = defaultAccessLevel, listAll },
} = req;
const { path = '', options = {} } = req;
const { accessLevel = defaultAccessLevel, listAll } = options;

const targetIdentityId =
req?.options?.accessLevel === 'protected'
? req.options?.targetIdentityId ?? identityId
: undefined;
const finalPath = getKeyWithPrefix({
accessLevel,
targetIdentityId:
options.accessLevel === 'protected'
? options.targetIdentityId
: identityId,
key: path,
});

const finalPath = getKeyWithPrefix(accessLevel, targetIdentityId, path);
const listConfig: StorageConfig = {
const listConfig = {
region,
credentials,
};
const listParams: ListObjectsV2Input = {
const listParams = {
Bucket: bucket,
Prefix: finalPath,
MaxKeys: req?.options?.listAll === true ? undefined : req.options?.pageSize,
Expand Down Expand Up @@ -117,9 +122,12 @@ const _list = async (
// TODO(ashwinkumar6) V6-logger: defaulting pageSize to ${MAX_PAGE_SIZE}.
}

const response = await listObjectsV2(listConfig, listParamsClone);
const listResult = response.Contents.map(item => ({
key: item.Key.substring(listParamsClone.Prefix.length),
const response: ListObjectsV2Output = await listObjectsV2(
listConfig,
listParamsClone
);
const listResult = response!.Contents!.map(item => ({
key: item.Key!.substring(listParamsClone.Prefix!.length),
eTag: item.ETag,
lastModified: item.LastModified,
size: item.Size,
Expand Down

0 comments on commit be25267

Please sign in to comment.