Skip to content

Commit

Permalink
chore: add user agent string to createStorageBrowser (#6187)
Browse files Browse the repository at this point in the history
Co-authored-by: AllanZhengYP <[email protected]>
  • Loading branch information
dindjarinjs and AllanZhengYP authored Dec 13, 2024
1 parent 85fda63 commit 89f3ec2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/react-storage/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
"name": "createStorageBrowser",
"path": "dist/esm/browser.mjs",
"import": "{ createStorageBrowser }",
"limit": "60 kB",
"limit": "60.5 kB",
"ignore": [
"@aws-amplify/storage"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';

import * as ProvidersModule from '../providers';
import * as UIModule from '@aws-amplify/ui';

import { createStorageBrowser } from '../createStorageBrowser';
import { StorageBrowserDisplayText } from '../displayText/types';
Expand All @@ -11,6 +12,8 @@ const createConfigurationProviderSpy = jest.spyOn(
'createConfigurationProvider'
);

const setCustomUserAgentSpy = jest.spyOn(UIModule, 'setUserAgent');

const accountId = '012345678901';
const customEndpoint = 'mock-endpoint';
const getLocationCredentials = jest.fn();
Expand Down Expand Up @@ -71,4 +74,18 @@ describe('createStorageBrowser', () => {
const Title = screen.getByText('Hello');
expect(Title).toBeInTheDocument();
});

it('sets a custom user agent', async () => {
const { StorageBrowser } = createStorageBrowser(input);

await waitFor(() => {
render(<StorageBrowser />);
});

expect(setCustomUserAgentSpy).toHaveBeenCalledWith({
componentName: 'StorageBrowser',
packageName: 'react-storage',
version: '3.5.0',
});
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React from 'react';

import { setUserAgent } from '@aws-amplify/ui';

import { VERSION } from '../../version';

import {
defaultActionConfigs,
getActionConfigs,
Expand Down Expand Up @@ -61,6 +65,12 @@ export function createStorageBrowser<
region,
} = input.config;

setUserAgent({
componentName: 'StorageBrowser',
packageName: 'react-storage',
version: VERSION,
});

const actions = {
default: {
...defaultActionConfigs,
Expand Down
17 changes: 17 additions & 0 deletions packages/ui/src/utils/setUserAgent/__tests__/setUserAgent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IN_APP_MESSAGING_INPUT_BASE,
LOCATION_SEARCH_INPUT_BASE,
MAP_VIEW_INPUT_BASE,
STORAGE_BROWSER_INPUT_BASE,
STORAGE_MANAGER_INPUT_BASE,
} from '../constants';
import { setUserAgent } from '..';
Expand Down Expand Up @@ -135,4 +136,20 @@ describe('setUserAgent', () => {
additionalDetails: [['StorageManager'], ['ui-react-storage', '1.0.0']],
});
});

it('passes the expected input for StorageBrowser', () => {
const details: SetUserAgentOptions = {
componentName: 'StorageBrowser',
packageName: 'react-storage',
version: '1.0.0',
};

setUserAgent(details);

expect(setCustomUserAgentSpy).toHaveBeenCalledTimes(1);
expect(setCustomUserAgentSpy).toHaveBeenCalledWith({
...STORAGE_BROWSER_INPUT_BASE,
additionalDetails: [['StorageBrowser'], ['ui-react-storage', '1.0.0']],
});
});
});
16 changes: 16 additions & 0 deletions packages/ui/src/utils/setUserAgent/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,19 @@ export const STORAGE_MANAGER_INPUT_BASE: Omit<
apis: [StorageAction.UploadData],
category: Category.Storage,
};

export const STORAGE_BROWSER_INPUT_BASE: Omit<
StorageUserAgentInput,
'additionalDetails'
> = {
apis: [
StorageAction.UploadData,
StorageAction.Copy,
StorageAction.GetUrl,
StorageAction.List,
StorageAction.Remove,
StorageAction.GetDataAccess,
StorageAction.ListCallerAccessGrants,
],
category: Category.Storage,
};
9 changes: 9 additions & 0 deletions packages/ui/src/utils/setUserAgent/setUserAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IN_APP_MESSAGING_INPUT_BASE,
LOCATION_SEARCH_INPUT_BASE,
MAP_VIEW_INPUT_BASE,
STORAGE_BROWSER_INPUT_BASE,
STORAGE_MANAGER_INPUT_BASE,
} from './constants';
import { noop } from '../utils';
Expand Down Expand Up @@ -36,6 +37,7 @@ export type ComponentName =
| 'InAppMessaging'
| 'LocationSearch'
| 'MapView'
| 'StorageBrowser'
| 'StorageManager'
| 'StorageImage';

Expand Down Expand Up @@ -123,6 +125,13 @@ export const setUserAgent = ({
});
break;
}
case 'StorageBrowser': {
setCustomUserAgent({
...STORAGE_BROWSER_INPUT_BASE,
additionalDetails: [[componentName], packageData],
});
break;
}
}

return noop as () => void;
Expand Down

0 comments on commit 89f3ec2

Please sign in to comment.