@contentstack/management
Version:
The Content Management API is used to manage the content of your Contentstack account
89 lines • 2.56 kB
JavaScript
import { platform, release } from 'os';
var HOST_REGEX = /^(?!\w+:\/\/)([\w-:]+\.)+([\w-:]+)(?::(\d+))?(?!:)$/;
export function isHost(host) {
return HOST_REGEX.test(host);
}
export function isNode() {
return typeof process !== 'undefined' && !process.browser;
}
export function getNodeVersion() {
return process.versions.node ? "v".concat(process.versions.node) : process.version;
}
function isReactNative() {
return typeof window !== 'undefined' && 'navigator' in window && 'product' in window.navigator && window.navigator.product === 'ReactNative';
}
function getBrowserOS() {
if (!window) {
return null;
}
var userAgent = window.navigator.userAgent;
var platform = window.navigator.platform;
var macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'];
var windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'];
var iosPlatforms = ['iPhone', 'iPad', 'iPod'];
var os = null;
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'macOS';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows';
} else if (/Android/.test(userAgent)) {
os = 'Android';
} else if (/Linux/.test(platform)) {
os = 'Linux';
}
return os;
}
function getNodeOS() {
var os = platform() || 'linux';
var version = release() || '0.0.0';
var osMap = {
android: 'Android',
aix: 'Linux',
darwin: 'macOS',
freebsd: 'Linux',
linux: 'Linux',
openbsd: 'Linux',
sunos: 'Linux',
win32: 'Windows'
};
if (os in osMap) {
return "".concat(osMap[os] || 'Linux', "/").concat(version);
}
return null;
}
export default function getUserAgent(sdk, application, integration, feature) {
var headerParts = [];
if (application) {
headerParts.push("app ".concat(application));
}
if (integration) {
headerParts.push("integration ".concat(integration));
}
if (feature) {
headerParts.push('feature ' + feature);
}
headerParts.push("sdk ".concat(sdk));
var os = null;
try {
if (isReactNative()) {
os = getBrowserOS();
headerParts.push('platform ReactNative');
} else if (isNode()) {
os = getNodeOS();
headerParts.push("platform node.js/".concat(getNodeVersion()));
} else {
os = getBrowserOS();
headerParts.push("platform browser");
}
} catch (e) {
os = null;
}
if (os) {
headerParts.push("os ".concat(os));
}
return "".concat(headerParts.filter(function (item) {
return item !== '';
}).join('; '), ";");
}