Class UserAgent

A representation of user agent string, which can be used to determine environmental information represented by the string. All properties are determined lazily.

import { UserAgent } from "@std/http/user-agent";

Deno.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.browser.name}
on ${userAgent.os.name} ${userAgent.os.version}!`);
});

Constructors

Accessors

Methods

Constructors

Accessors

  • get browser(): Browser
  • The name and version of the browser extracted from the user agent string.

    Returns Browser

    An object with information about the user agent's browser.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${userAgent.browser.name}!`);
    });
  • get cpu(): Cpu
  • The architecture of the CPU extracted from the user agent string.

    Returns Cpu

    An object with information about the user agent's CPU.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${userAgent.cpu.architecture}!`);
    });
  • get device(): Device
  • The model, type, and vendor of a device if present in a user agent string.

    Returns Device

    An object with information about the user agent's device.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${userAgent.device.model}!`);
    });
  • get engine(): Engine
  • The name and version of the browser engine in a user agent string.

    Returns Engine

    An object with information about the user agent's browser engine.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${userAgent.engine.name}!`);
    });
  • get os(): Os
  • The name and version of the operating system in a user agent string.

    Returns Os

    An object with information about the user agent's OS.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${userAgent.os.name}!`);
    });
  • get ua(): string
  • A read only version of the user agent string related to the instance.

    Returns string

    The user agent string.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${userAgent.ua}!`);
    });

Methods

  • Converts the current instance to a JSON representation.

    Returns {
        browser: Browser;
        cpu: Cpu;
        device: Device;
        engine: Engine;
        os: Os;
        ua: string;
    }

    A JSON representation on this user agent instance.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${JSON.stringify(userAgent.toJSON())}!`);
    });
  • Converts the current instance to a string.

    Returns string

    The user agent string.

    import { UserAgent } from "@std/http/user-agent";

    Deno.serve((req) => {
    const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
    return new Response(`Hello, ${userAgent.toString()}!`);
    });