Skip to content

Commit 34e7405

Browse files
authored
Storage AWSS3Provider typescript improvement (#8590)
Added Typescript type improvements for AWSS3Provider Changes are only for internal modules
1 parent 190bd50 commit 34e7405

4 files changed

Lines changed: 273 additions & 187 deletions

File tree

packages/storage/__tests__/providers/AWSS3Provider-unit-test.ts

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
* and limitations under the License.
1212
*/
1313
import StorageProvider from '../../src/providers/AWSS3Provider';
14-
import { Logger, Hub, Credentials } from '@aws-amplify/core';
14+
import { Logger, Hub, Credentials, ICredentials } from '@aws-amplify/core';
1515
import * as formatURL from '@aws-sdk/util-format-url';
1616
import { S3Client, ListObjectsCommand } from '@aws-sdk/client-s3';
1717
import { S3RequestPresigner } from '@aws-sdk/s3-request-presigner';
1818
import * as events from 'events';
1919

20-
import { S3CopySource, S3CopyDestination } from '../../src/types';
20+
import { S3CopySource, S3CopyDestination, StorageOptions, S3ProviderGetConfig } from '../../src/types';
2121
/**
2222
* NOTE - These test cases use Hub.dispatch but they should
2323
* actually be using dispatchStorageEvent from Storage
@@ -46,19 +46,19 @@ S3RequestPresigner.prototype.presign = jest.fn(async (request, expires) => {
4646
return (Promise as any).resolve();
4747
});
4848

49-
const credentials = {
49+
const credentials: ICredentials = {
5050
accessKeyId: 'accessKeyId',
5151
sessionToken: 'sessionToken',
5252
secretAccessKey: 'secretAccessKey',
5353
identityId: 'identityId',
5454
authenticated: true,
5555
};
5656

57-
const options = {
57+
const options: StorageOptions = {
5858
bucket: 'bucket',
5959
region: 'region',
6060
credentials,
61-
level: 'level',
61+
level: 'public',
6262
};
6363

6464
const options_no_cred = {
@@ -139,7 +139,7 @@ describe('StorageProvider test', () => {
139139
jest.spyOn(formatURL, 'formatUrl').mockReturnValueOnce('url');
140140

141141
expect.assertions(3);
142-
expect(await storage.get('key', { downloaded: false })).toBe('url');
142+
expect(await storage.get('key', { download: false })).toBe('url');
143143
expect(spyon.mock.calls[0][0].path).toEqual('/public/key');
144144
expect(spyon.mock.calls[0][0].hostname).toEqual(options.bucket + '.s3.' + options.region + '.amazonaws.com');
145145
});
@@ -241,27 +241,20 @@ describe('StorageProvider test', () => {
241241
on: jest.fn(),
242242
removeAllListeners: mockRemoveAllListeners,
243243
};
244-
jest
245-
.spyOn(events, 'EventEmitter')
246-
.mockImplementationOnce(() => mockEventEmitter);
244+
jest.spyOn(events, 'EventEmitter').mockImplementationOnce(() => mockEventEmitter);
247245
const downloadOptionsWithProgressCallback = Object.assign({}, options, {
248246
download: true,
249247
progressCallback: mockCallback,
250248
});
251249
const storage = new StorageProvider();
252250
storage.configure(downloadOptionsWithProgressCallback);
253-
const spyon = jest
254-
.spyOn(S3Client.prototype, 'send')
255-
.mockImplementationOnce(async params => {
256-
return { Body: [1, 2] };
257-
});
251+
const spyon = jest.spyOn(S3Client.prototype, 'send').mockImplementationOnce(async params => {
252+
return { Body: [1, 2] };
253+
});
258254
expect(await storage.get('key', { download: true })).toEqual({
259255
Body: [1, 2],
260256
});
261-
expect(mockEventEmitter.on).toBeCalledWith(
262-
'sendDownloadProgress',
263-
expect.any(Function)
264-
);
257+
expect(mockEventEmitter.on).toBeCalledWith('sendDownloadProgress', expect.any(Function));
265258
// Get the anonymous function called by the emitter
266259
const emitterOnFn = mockEventEmitter.on.mock.calls[0][1];
267260
// Manully invoke it for testing
@@ -280,25 +273,18 @@ describe('StorageProvider test', () => {
280273
on: jest.fn(),
281274
removeAllListeners: jest.fn(),
282275
};
283-
jest
284-
.spyOn(events, 'EventEmitter')
285-
.mockImplementationOnce(() => mockEventEmitter);
276+
jest.spyOn(events, 'EventEmitter').mockImplementationOnce(() => mockEventEmitter);
286277
const downloadOptionsWithProgressCallback = Object.assign({}, options);
287278
const storage = new StorageProvider();
288279
storage.configure(downloadOptionsWithProgressCallback);
289-
jest
290-
.spyOn(S3Client.prototype, 'send')
291-
.mockImplementationOnce(async params => {
292-
return { Body: [1, 2] };
293-
});
280+
jest.spyOn(S3Client.prototype, 'send').mockImplementationOnce(async params => {
281+
return { Body: [1, 2] };
282+
});
294283
await storage.get('key', {
295284
download: true,
296-
progressCallback: 'this is not a function',
285+
progressCallback: ('this is not a function' as unknown) as S3ProviderGetConfig['progressCallback'], // this is intentional
297286
});
298-
expect(loggerSpy).toHaveBeenCalledWith(
299-
'WARN',
300-
'progressCallback should be a function, not a string'
301-
);
287+
expect(loggerSpy).toHaveBeenCalledWith('WARN', 'progressCallback should be a function, not a string');
302288
});
303289

304290
test('get object with download with failure', async () => {
@@ -472,7 +458,7 @@ describe('StorageProvider test', () => {
472458
});
473459
});
474460

475-
await storage.get('key', { downloaded: false });
461+
await storage.get('key', { download: false });
476462

477463
const curCredSpyOn2 = jest.spyOn(Credentials, 'get').mockImplementationOnce(() => {
478464
return new Promise((res, rej) => {
@@ -482,7 +468,7 @@ describe('StorageProvider test', () => {
482468
});
483469
});
484470

485-
await storage.get('key', { downloaded: false });
471+
await storage.get('key', { download: false });
486472

487473
expect(curCredSpyOn.mock.calls.length).toBe(2);
488474

@@ -667,18 +653,13 @@ describe('StorageProvider test', () => {
667653
emit: jest.fn(),
668654
on: jest.fn(),
669655
};
670-
jest
671-
.spyOn(events, 'EventEmitter')
672-
.mockImplementationOnce(() => mockEventEmitter);
656+
jest.spyOn(events, 'EventEmitter').mockImplementationOnce(() => mockEventEmitter);
673657
const storage = new StorageProvider();
674658
storage.configure(options);
675659
await storage.put('key', 'object', {
676660
progressCallback: mockCallback,
677661
});
678-
expect(mockEventEmitter.on).toBeCalledWith(
679-
'sendUploadProgress',
680-
expect.any(Function)
681-
);
662+
expect(mockEventEmitter.on).toBeCalledWith('sendUploadProgress', expect.any(Function));
682663
const emitterOnFn = mockEventEmitter.on.mock.calls[0][1];
683664
// Manually invoke for testing
684665
emitterOnFn('arg');
@@ -698,18 +679,13 @@ describe('StorageProvider test', () => {
698679
emit: jest.fn(),
699680
on: jest.fn(),
700681
};
701-
jest
702-
.spyOn(events, 'EventEmitter')
703-
.mockImplementationOnce(() => mockEventEmitter);
682+
jest.spyOn(events, 'EventEmitter').mockImplementationOnce(() => mockEventEmitter);
704683
const storage = new StorageProvider();
705684
storage.configure(options);
706685
await storage.put('key', 'object', {
707-
progressCallback: 'hello',
686+
progressCallback: ('hello' as unknown) as S3ProviderGetConfig['progressCallback'], // this is intentional
708687
});
709-
expect(loggerSpy).toHaveBeenCalledWith(
710-
'WARN',
711-
'progressCallback should be a function, not a string'
712-
);
688+
expect(loggerSpy).toHaveBeenCalledWith('WARN', 'progressCallback should be a function, not a string');
713689
});
714690

715691
test('credentials not ok', async () => {
@@ -1012,7 +988,9 @@ describe('StorageProvider test', () => {
1012988
storage.configure(options);
1013989
const spyon = jest.spyOn(S3Client.prototype, 'send');
1014990

1015-
expect(await storage.copy({ key: 'src' }, { key: 'dest' })).toEqual({ key: 'dest' });
991+
expect(await storage.copy({ key: 'src' }, { key: 'dest' })).toEqual({
992+
key: 'dest',
993+
});
1016994
expect(spyon.mock.calls[0][0].input).toStrictEqual({
1017995
Bucket: 'bucket',
1018996
// Should default to public if no level is specified
@@ -1032,7 +1010,10 @@ describe('StorageProvider test', () => {
10321010

10331011
// No src key
10341012
await expect(
1035-
storage.copy({ level: 'public' } as S3CopySource, { key: 'dest', level: 'public' })
1013+
storage.copy({ level: 'public' } as S3CopySource, {
1014+
key: 'dest',
1015+
level: 'public',
1016+
})
10361017
).rejects.toThrowError('source param should be an object with the property "key" with value of type string');
10371018

10381019
// wrong key type
@@ -1053,7 +1034,9 @@ describe('StorageProvider test', () => {
10531034

10541035
// No dest key
10551036
await expect(
1056-
storage.copy({ key: 'src', level: 'public' }, { level: 'public' } as S3CopyDestination)
1037+
storage.copy({ key: 'src', level: 'public' }, {
1038+
level: 'public',
1039+
} as S3CopyDestination)
10571040
).rejects.toThrowError('destination param should be an object with the property "key" with value of type string');
10581041

10591042
// wrong key type

0 commit comments

Comments
 (0)