UNPKG

client-parser

Version:

A utility to detect the device type (Android, iOS, Windows Phone, PC, unknown) based on the User Agent string.

145 lines (141 loc) 4.82 kB
/** * Defines standardized string values for different device types. * These values are used to categorize devices detected via the User Agent string. * @readonly * @enum {string} */ declare const DEVICE_TYPES: Readonly<{ /** Represents an unknown or uncategorized device type. */ readonly UNKNOWN: "unknown"; /** Represents a Windows Phone device. */ readonly WINDOWS_PHONE: "windows_phone"; /** Represents an iOS device (iPhone, iPad, iPod Touch). */ readonly IOS: "ios"; /** Represents an Android device. */ readonly ANDROID: "android"; /** Represents a personal computer (desktop or laptop). */ readonly PC: "pc"; }>; /** * Defines standardized string values for common operating system names. * These values are used to identify the OS detected via the User Agent string. * @readonly * @enum {string} */ declare const OS_NAMES: Readonly<{ /** Represents an unknown or uncategorized operating system. */ readonly UNKNOWN: "unknown"; /** Represents the Windows Phone operating system. */ readonly WINDOWS_PHONE: "Windows Phone"; /** Represents the iOS operating system. */ readonly IOS: "iOS"; /** Represents the Android operating system. */ readonly ANDROID: "Android"; /** Represents the Windows desktop operating system. */ readonly WINDOWS: "Windows"; /** Represents the macOS operating system. */ readonly MACOS: "macOS"; /** Represents the Linux operating system. */ readonly LINUX: "Linux"; }>; /** * Defines standardized string values for common browser names. * These values are used to identify the browser detected via the User Agent string. * @readonly * @enum {string} */ declare const BROWSER_NAMES: Readonly<{ /** Represents an unknown or uncategorized browser. */ readonly UNKNOWN: "unknown"; /** Represents the Microsoft Edge browser. */ readonly EDGE: "Edge"; /** Represents the Opera browser. */ readonly OPERA: "Opera"; /** Represents the Mozilla Firefox browser. */ readonly FIREFOX: "Firefox"; /** Represents the Google Chrome browser. */ readonly CHROME: "Chrome"; /** Represents the Apple Safari browser. */ readonly SAFARI: "Safari"; /** Represents the Internet Explorer browser. */ readonly INTERNET_EXPLORER: "Internet Explorer"; }>; /** * Interface for the 'device' property within IDeviceInfo. */ interface IDeviceDetails { type: (typeof DEVICE_TYPES)[keyof typeof DEVICE_TYPES]; name: string; model: string; manufacturer?: string; } /** * Interface for the 'engine' property within IDeviceInfo. */ interface IEngineInfo { name: string; version: string; } /** * Interface for the 'os' property within IDeviceInfo. */ interface IOSInfo { name: (typeof OS_NAMES)[keyof typeof OS_NAMES] | string; version?: string; architecture?: string; } /** * Interface for the 'browser' property within IDeviceInfo. */ interface IBrowserInfo { name: (typeof BROWSER_NAMES)[keyof typeof BROWSER_NAMES] | string; version: string; } /** * Main interface for detailed device information. */ interface IDeviceInfo { /** * The raw User Agent string. */ userAgentString: string; /** * Detailed information about the device itself. */ device: IDeviceDetails; /** * Information about the rendering engine. */ engine: IEngineInfo; /** * Information about the operating system. */ os: IOSInfo; /** * Information about the browser. */ browser: IBrowserInfo; /** * The platform detected (e.g., 'Win32', 'MacIntel', 'Linux x86_64'). */ platform: string; /** * Indicates if the user agent is a bot. */ isBot: boolean; } /** * Detects detailed information about the device based on the browser's User Agent string. * This function uses private helper functions `_detectOSAndDevice` and `_detectBrowser` * to populate an `IDeviceInfo` object. * * @returns {IDeviceInfo} An object containing detailed device information, * including operating system, device type (mobile, tablet, PC), browser name, and browser version. * The `type` property can be 'unknown', 'windows_phone', 'ios', 'android', 'pc'. * The `os` property will indicate the operating system (e.g., 'Windows Phone', 'iOS', 'Android', 'Windows', 'macOS', 'Linux'). * The `osVersion` property will provide the version of the operating system if detectable. * The `browser` property will contain the browser name (e.g., 'Edge', 'Opera', 'Firefox', 'Chrome', 'Safari', 'Internet Explorer'). * The `browserVersion` property will provide the version of the detected browser. */ declare const getDeviceType: (userAgent: string, platform?: string) => IDeviceInfo; export { getDeviceType as default };