Runtime Edge User Agent
The @runtime-edge/user-agent package gives some utilities on top of ua-parser-js (opens in a new tab).
Installation
npm install @runtime-edge/user-agent
This package includes built-in TypeScript support.
Usage
Just call the exported .userAgentFromString
method for getting the parsed data from an User-Agent (opens in a new tab) HTTP header:
import { userAgentFromString } from '@runtime-edge/user-agent'
export default (request: Request) => {
const userAgent = request.headers.get('user-agent')
userAgentFromString(userAgent)
return Response.json(userAgent(request))
// => {
// browser: {
// major: '83',
// name: 'Chrome',
// version: '83.0.4103.116',
// },
// cpu: { architecture: 'amd64' },
// engine: {
// name: 'Blink',
// version: '83.0.4103.116',
// },
// isBot: false,
// os: {
// name: 'Windows',
// version: '10',
// },
// ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)…'
// }
}
or alternatively, you can call .userAgent
and pass the Request
instance:
import { userAgent } from '@runtime-edge/user-agent'
export default (request: Request) => {
return Response.json(userAgent(request))
// => {
// browser: {
// major: '83',
// name: 'Chrome',
// version: '83.0.4103.116',
// },
// cpu: { architecture: 'amd64' },
// engine: {
// name: 'Blink',
// version: '83.0.4103.116',
// },
// isBot: false,
// os: {
// name: 'Windows',
// version: '10',
// },
// ua: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)…'
// }
}