Skip to content

Commit

Permalink
[Chore] Refactor Storage Options (#13613)
Browse files Browse the repository at this point in the history
* refactor storage options naming

* update casts

* add bytelength base test
  • Loading branch information
ashika112 authored Sep 24, 2024
1 parent 555defc commit bcc3cbc
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { byteLength } from '../../../../../src/providers/s3/apis/uploadData/byteLength';

describe('byteLength', () => {
it('returns 0 for null or undefined', () => {
expect(byteLength(undefined)).toBe(0);
expect(byteLength(null)).toBe(0);
});

it('calculates byte length correctly for ASCII strings', () => {
expect(byteLength('hello')).toBe(5);
});

it('calculates byte length correctly for multi-byte characters', () => {
expect(byteLength('èちは')).toBe(8);
});

it('handles Uint8Array correctly', () => {
const input = new Uint8Array([1, 2, 3]);
expect(byteLength(input)).toBe(3);
});

it('handles ArrayBuffer correctly', () => {
const buffer = new ArrayBuffer(8);
expect(byteLength(buffer)).toBe(8);
});

it('handles File object correctly', () => {
const file = new Blob(['hello']);
expect(byteLength(file)).toBe(5);
});

it('returns undefined for unsupported types', () => {
const input = { unsupportedType: true };
expect(byteLength(input)).toBeUndefined();
});
});
6 changes: 3 additions & 3 deletions packages/storage/src/providers/s3/apis/internal/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import {
validateStorageOperationInputWithPrefix,
} from '../../utils';
import {
ListAllOptionsWithPath,
ListPaginateOptionsWithPath,
ListAllWithPathOptions,
ListPaginateWithPathOptions,
ResolvedS3Config,
} from '../../types/options';
import {
Expand Down Expand Up @@ -267,7 +267,7 @@ const mapCommonPrefixesToExcludedSubpaths = (
};

const getDelimiter = (
options?: ListAllOptionsWithPath | ListPaginateOptionsWithPath,
options?: ListAllWithPathOptions | ListPaginateWithPathOptions,
): string | undefined => {
if (options?.subpathStrategy?.strategy === 'exclude') {
return options?.subpathStrategy?.delimiter ?? DEFAULT_DELIMITER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from '../../../utils/constants';
import {
ResolvedS3Config,
UploadDataOptionsWithKey,
UploadDataWithKeyOptions,
} from '../../../types/options';
import { StorageError } from '../../../../../errors/StorageError';
import { CanceledError } from '../../../../../errors/CanceledError';
Expand Down Expand Up @@ -99,7 +99,7 @@ export const getMultipartUploadHandlers = (

// Resolve "key" specific options
if (inputType === STORAGE_INPUT_KEY) {
const accessLevel = (uploadDataOptions as UploadDataOptionsWithKey)
const accessLevel = (uploadDataOptions as UploadDataWithKeyOptions)
?.accessLevel;

resolvedKeyPrefix = resolvedS3Options.keyPrefix;
Expand Down
8 changes: 0 additions & 8 deletions packages/storage/src/providers/s3/types/errors.ts

This file was deleted.

31 changes: 14 additions & 17 deletions packages/storage/src/providers/s3/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@
// SPDX-License-Identifier: Apache-2.0

export {
GetUrlOptionsWithKey,
GetUrlOptionsWithPath,
UploadDataOptionsWithPath,
UploadDataOptionsWithKey,
GetPropertiesOptionsWithKey,
GetPropertiesOptionsWithPath,
ListAllOptionsWithPrefix,
ListPaginateOptionsWithPrefix,
ListAllOptionsWithPath,
ListPaginateOptionsWithPath,
GetUrlWithKeyOptions,
GetUrlWithPathOptions,
UploadDataWithPathOptions,
UploadDataWithKeyOptions,
GetPropertiesWithKeyOptions,
GetPropertiesWithPathOptions,
ListAllWithPrefixOptions,
ListPaginateWithPrefixOptions,
ListAllWithPathOptions,
ListPaginateWithPathOptions,
RemoveOptions,
DownloadDataOptionsWithPath,
DownloadDataOptionsWithKey,
CopyDestinationOptionsWithKey,
CopySourceOptionsWithKey,
CopyWithPathSourceOptions,
CopyWithPathDestinationOptions,
DownloadDataWithPathOptions,
DownloadDataWithKeyOptions,
CopyDestinationWithKeyOptions,
CopySourceWithKeyOptions,
} from './options';
export {
UploadDataOutput,
Expand Down Expand Up @@ -58,4 +56,3 @@ export {
ListAllWithPathInput,
ListPaginateWithPathInput,
} from './inputs';
export { S3Exception } from './errors';
56 changes: 28 additions & 28 deletions packages/storage/src/providers/s3/types/inputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ import {
StorageUploadDataInputWithPath,
} from '../../../types';
import {
CopyDestinationOptionsWithKey,
CopySourceOptionsWithKey,
DownloadDataOptionsWithKey,
DownloadDataOptionsWithPath,
GetPropertiesOptionsWithKey,
GetPropertiesOptionsWithPath,
GetUrlOptionsWithKey,
GetUrlOptionsWithPath,
ListAllOptionsWithPath,
ListAllOptionsWithPrefix,
ListPaginateOptionsWithPath,
ListPaginateOptionsWithPrefix,
CopyDestinationWithKeyOptions,
CopySourceWithKeyOptions,
DownloadDataWithKeyOptions,
DownloadDataWithPathOptions,
GetPropertiesWithKeyOptions,
GetPropertiesWithPathOptions,
GetUrlWithKeyOptions,
GetUrlWithPathOptions,
ListAllWithPathOptions,
ListAllWithPrefixOptions,
ListPaginateWithPathOptions,
ListPaginateWithPrefixOptions,
RemoveOptions,
UploadDataOptionsWithKey,
UploadDataOptionsWithPath,
UploadDataWithKeyOptions,
UploadDataWithPathOptions,
} from '../types';

// TODO: support use accelerate endpoint option
Expand All @@ -41,8 +41,8 @@ import {
* Input type for S3 copy API.
*/
export type CopyInput = StorageCopyInputWithKey<
CopySourceOptionsWithKey,
CopyDestinationOptionsWithKey
CopySourceWithKeyOptions,
CopyDestinationWithKeyOptions
>;
/**
* Input type with path for S3 copy API.
Expand All @@ -54,48 +54,48 @@ export type CopyWithPathInput = StorageCopyInputWithPath;
* Input type for S3 getProperties API.
*/
export type GetPropertiesInput =
StorageGetPropertiesInputWithKey<GetPropertiesOptionsWithKey>;
StorageGetPropertiesInputWithKey<GetPropertiesWithKeyOptions>;
/**
* Input type with for S3 getProperties API.
*/
export type GetPropertiesWithPathInput =
StorageGetPropertiesInputWithPath<GetPropertiesOptionsWithPath>;
StorageGetPropertiesInputWithPath<GetPropertiesWithPathOptions>;

/**
* @deprecated Use {@link GetUrlWithPathInput} instead.
* Input type for S3 getUrl API.
*/
export type GetUrlInput = StorageGetUrlInputWithKey<GetUrlOptionsWithKey>;
export type GetUrlInput = StorageGetUrlInputWithKey<GetUrlWithKeyOptions>;
/**
* Input type with path for S3 getUrl API.
*/
export type GetUrlWithPathInput =
StorageGetUrlInputWithPath<GetUrlOptionsWithPath>;
StorageGetUrlInputWithPath<GetUrlWithPathOptions>;

/**
* Input type with path for S3 list API. Lists all bucket objects.
*/
export type ListAllWithPathInput =
StorageListInputWithPath<ListAllOptionsWithPath>;
StorageListInputWithPath<ListAllWithPathOptions>;

/**
* Input type with path for S3 list API. Lists bucket objects with pagination.
*/
export type ListPaginateWithPathInput =
StorageListInputWithPath<ListPaginateOptionsWithPath>;
StorageListInputWithPath<ListPaginateWithPathOptions>;

/**
* @deprecated Use {@link ListAllWithPathInput} instead.
* Input type for S3 list API. Lists all bucket objects.
*/
export type ListAllInput = StorageListInputWithPrefix<ListAllOptionsWithPrefix>;
export type ListAllInput = StorageListInputWithPrefix<ListAllWithPrefixOptions>;

/**
* @deprecated Use {@link ListPaginateWithPathInput} instead.
* Input type for S3 list API. Lists bucket objects with pagination.
*/
export type ListPaginateInput =
StorageListInputWithPrefix<ListPaginateOptionsWithPrefix>;
StorageListInputWithPrefix<ListPaginateWithPrefixOptions>;

/**
* @deprecated Use {@link RemoveWithPathInput} instead.
Expand All @@ -115,23 +115,23 @@ export type RemoveWithPathInput = StorageRemoveInputWithPath<
* Input type for S3 downloadData API.
*/
export type DownloadDataInput =
StorageDownloadDataInputWithKey<DownloadDataOptionsWithKey>;
StorageDownloadDataInputWithKey<DownloadDataWithKeyOptions>;

/**
* Input type with path for S3 downloadData API.
*/
export type DownloadDataWithPathInput =
StorageDownloadDataInputWithPath<DownloadDataOptionsWithPath>;
StorageDownloadDataInputWithPath<DownloadDataWithPathOptions>;

/**
* @deprecated Use {@link UploadDataWithPathInput} instead.
* Input type for S3 uploadData API.
*/
export type UploadDataInput =
StorageUploadDataInputWithKey<UploadDataOptionsWithKey>;
StorageUploadDataInputWithKey<UploadDataWithKeyOptions>;

/**
* Input type with path for S3 uploadData API.
*/
export type UploadDataWithPathInput =
StorageUploadDataInputWithPath<UploadDataOptionsWithPath>;
StorageUploadDataInputWithPath<UploadDataWithPathOptions>;
40 changes: 20 additions & 20 deletions packages/storage/src/providers/s3/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,35 +74,35 @@ interface TransferOptions {
/**
* Input options type for S3 getProperties API.
*/
/** @deprecated Use {@link GetPropertiesOptionsWithPath} instead. */
export type GetPropertiesOptionsWithKey = ReadOptions & CommonOptions;
export type GetPropertiesOptionsWithPath = CommonOptions;
/** @deprecated Use {@link GetPropertiesWithPathOptions} instead. */
export type GetPropertiesWithKeyOptions = ReadOptions & CommonOptions;
export type GetPropertiesWithPathOptions = CommonOptions;

/**
* Input options type for S3 getProperties API.
*/
export type RemoveOptions = WriteOptions & CommonOptions;

/**
* @deprecated Use {@link ListAllOptionsWithPath} instead.
* @deprecated Use {@link ListAllWithPathOptions} instead.
* Input options type with prefix for S3 list all API.
*/
export type ListAllOptionsWithPrefix = StorageListAllOptions &
export type ListAllWithPrefixOptions = StorageListAllOptions &
ReadOptions &
CommonOptions;

/**
* @deprecated Use {@link ListPaginateOptionsWithPath} instead.
* @deprecated Use {@link ListPaginateWithPathOptions} instead.
* Input options type with prefix for S3 list API to paginate items.
*/
export type ListPaginateOptionsWithPrefix = StorageListPaginateOptions &
export type ListPaginateWithPrefixOptions = StorageListPaginateOptions &
ReadOptions &
CommonOptions;

/**
* Input options type with path for S3 list all API.
*/
export type ListAllOptionsWithPath = Omit<
export type ListAllWithPathOptions = Omit<
StorageListAllOptions,
'accessLevel'
> &
Expand All @@ -113,7 +113,7 @@ export type ListAllOptionsWithPath = Omit<
/**
* Input options type with path for S3 list API to paginate items.
*/
export type ListPaginateOptionsWithPath = Omit<
export type ListPaginateWithPathOptions = Omit<
StorageListPaginateOptions,
'accessLevel'
> &
Expand Down Expand Up @@ -150,9 +150,9 @@ export type GetUrlOptions = CommonOptions & {
contentType?: string;
};

/** @deprecated Use {@link GetUrlOptionsWithPath} instead. */
export type GetUrlOptionsWithKey = ReadOptions & GetUrlOptions;
export type GetUrlOptionsWithPath = GetUrlOptions;
/** @deprecated Use {@link GetUrlWithPathOptions} instead. */
export type GetUrlWithKeyOptions = ReadOptions & GetUrlOptions;
export type GetUrlWithPathOptions = GetUrlOptions;

/**
* Input options type for S3 downloadData API.
Expand All @@ -161,9 +161,9 @@ export type DownloadDataOptions = CommonOptions &
TransferOptions &
BytesRangeOptions;

/** @deprecated Use {@link DownloadDataOptionsWithPath} instead. */
export type DownloadDataOptionsWithKey = ReadOptions & DownloadDataOptions;
export type DownloadDataOptionsWithPath = DownloadDataOptions;
/** @deprecated Use {@link DownloadDataWithPathOptions} instead. */
export type DownloadDataWithKeyOptions = ReadOptions & DownloadDataOptions;
export type DownloadDataWithPathOptions = DownloadDataOptions;

export type UploadDataOptions = CommonOptions &
TransferOptions & {
Expand Down Expand Up @@ -192,19 +192,19 @@ export type UploadDataOptions = CommonOptions &
metadata?: Record<string, string>;
};

/** @deprecated Use {@link UploadDataOptionsWithPath} instead. */
export type UploadDataOptionsWithKey = WriteOptions & UploadDataOptions;
export type UploadDataOptionsWithPath = UploadDataOptions;
/** @deprecated Use {@link UploadDataWithPathOptions} instead. */
export type UploadDataWithKeyOptions = WriteOptions & UploadDataOptions;
export type UploadDataWithPathOptions = UploadDataOptions;

/** @deprecated This may be removed in the next major version. */
export type CopySourceOptionsWithKey = ReadOptions & {
export type CopySourceWithKeyOptions = ReadOptions & {
/** @deprecated This may be removed in the next major version. */
key: string;
bucket?: StorageBucket;
};

/** @deprecated This may be removed in the next major version. */
export type CopyDestinationOptionsWithKey = WriteOptions & {
export type CopyDestinationWithKeyOptions = WriteOptions & {
/** @deprecated This may be removed in the next major version. */
key: string;
bucket?: StorageBucket;
Expand Down

0 comments on commit bcc3cbc

Please sign in to comment.