@ayonli/jsext
Version:
A JavaScript extension package for building strong and modern applications.
58 lines (57 loc) • 2.02 kB
TypeScript
import type { RuntimeInfo, WellknownPlatform } from "../runtime.ts";
/**
* Represents the user agent information.
*
* Most browsers use the `User-Agent` or the `navigator.userAgent` string to
* indicate what they are compatible instead of what they are made of. This
* interface tries to represent the actual user agent information as accurate
* as possible.
*/
export interface UserAgentInfo {
/**
* The name of the user agent, e.g. `Chrome`, `Firefox`, `Safari`, `Edge`,
* etc.
*/
name: string;
/**
* The version of the user agent, e.g. `127.0.0.0`. This value can be
* `undefined` if the version is not provided.
*/
version?: string | undefined;
/**
* JavaScript runtime information defined by the user agent, only available
* if the user agent is a browser or a JavaScript server runtime.
*/
runtime?: Pick<RuntimeInfo, "identity" | "version"> | undefined;
/**
* The platform on which the user agent is running, e.g. `darwin`, `windows`,
* `linux`, `android`, etc.
*/
platform: WellknownPlatform | "unknown";
/**
* Whether the user agent is running on a mobile device, includes tablets.
*/
mobile: boolean;
/**
* The original user agent string passed into {@link parseUserAgent}.
*/
raw?: string;
}
/**
* Parses the `User-Agent` header or the `navigator.userAgent` property.
*
* @example
* ```ts
* const ua = parseUserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
* console.log(ua);
* // {
* // name: "Chrome",
* // version: "91.0.4472.124",
* // runtime: { identity: "chrome", version: "91.0.4472.124" },
* // platform: "windows",
* // mobile: false,
* // raw: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36",
* // }
* ```
*/
export declare function parseUserAgent(str: string): UserAgentInfo;