-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
index.ts
54 lines (44 loc) · 1.37 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { CustomUserAgentDetails, Framework } from './types';
import { version } from './version';
import { detectFramework, observeFrameworkChanges } from './detectFramework';
import { UserAgent as AWSUserAgent } from '@aws-sdk/types';
const BASE_USER_AGENT = `aws-amplify`;
class PlatformBuilder {
userAgent = `${BASE_USER_AGENT}/${version}`;
get framework() {
return detectFramework();
}
get isReactNative() {
return (
this.framework === Framework.ReactNative ||
this.framework === Framework.Expo
);
}
observeFrameworkChanges(fcn: () => void) {
observeFrameworkChanges(fcn);
}
}
export const Platform = new PlatformBuilder();
export const getAmplifyUserAgentObject = ({
category,
action,
framework,
}: CustomUserAgentDetails = {}): AWSUserAgent => {
const userAgent: AWSUserAgent = [[BASE_USER_AGENT, version]];
if (category) {
userAgent.push([category, action]);
}
userAgent.push(['framework', detectFramework()]);
return userAgent;
};
export const getAmplifyUserAgent = (
customUserAgentDetails?: CustomUserAgentDetails
): string => {
const userAgent = getAmplifyUserAgentObject(customUserAgentDetails);
const userAgentString = userAgent
.map(([agentKey, agentValue]) => `${agentKey}/${agentValue}`)
.join(' ');
return userAgentString;
};