UserAgent
import { UserAgent } from "jsr:@ende/http";class
class UserAgentA representation of user agent string, which can be used to determine environmental information represented by the string. All properties are determined lazily.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.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
constructor
new UserAgent(ua: string | null)Constructs a new instance.
ua: string | nullThe user agent string to construct this instance with.
Methods
browser
get browser(): BrowserThe name and version of the browser extracted from the user agent string.
Returns
An object with information about the user agent's browser.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.browser.name}!`);
});
cpu
get cpu(): CpuThe architecture of the CPU extracted from the user agent string.
Returns
An object with information about the user agent's CPU.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.cpu.architecture}!`);
});
device
get device(): DeviceThe model, type, and vendor of a device if present in a user agent string.
Returns
An object with information about the user agent's device.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.device.model}!`);
});
engine
get engine(): EngineThe name and version of the browser engine in a user agent string.
Returns
An object with information about the user agent's browser engine.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.engine.name}!`);
});
os
get os(): OsThe name and version of the operating system in a user agent string.
Returns
An object with information about the user agent's OS.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.os.name}!`);
});
ua
get ua(): stringA read only version of the user agent string related to the instance.
Returns
The user agent string.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.ua}!`);
});
toJSON
toJSON(): { browser: Browser; cpu: Cpu; device: Device; engine: Engine; os: Os; ua: string }Converts the current instance to a JSON representation.
Returns
A JSON representation on this user agent instance.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${JSON.stringify(userAgent.toJSON())}!`);
});
toString
toString(): stringConverts the current instance to a string.
Returns
The user agent string.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
return new Response(`Hello, ${userAgent.toString()}!`);
});
[Symbol.for("Deno.customInspect")]
[Symbol.for("Deno.customInspect")](inspect: (value: unknown, options: unknown) => string, options: unknown): stringCustom output for Ende.inspect.
inspect: (value: unknown, options: unknown) => stringinternal inspect function.
options: unknowninspect options.
Returns
The custom value to inspect.
Example: Usage
import { UserAgent } from "@ende/http/user-agent";
Ende.serve((req) => {
const userAgent = new UserAgent(req.headers.get("user-agent") ?? "");
Ende.inspect(userAgent);
return new Response(`Hello, ${userAgent.ua}!`);
});