Skip to content
This repository was archived by the owner on Oct 10, 2022. It is now read-only.

Commit 92b33fd

Browse files
author
Daniel Climent
committed
More details about the system in the userAgent
1 parent 2585b65 commit 92b33fd

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

src/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import pWaitFor from 'p-wait-for'
33
import { getMethods } from './methods/index.js'
44
import { openApiSpec } from './open_api.js'
55
import { getOperations } from './operations.js'
6+
import { getUserAgent } from './user_agent.js'
67

78
export class NetlifyAPI {
89
constructor(firstArg, secondArg) {
@@ -11,7 +12,7 @@ export class NetlifyAPI {
1112

1213
// default opts
1314
const {
14-
userAgent = 'netlify/js-client',
15+
userAgent = getUserAgent(),
1516
scheme = openApiSpec.schemes[0],
1617
host = openApiSpec.host,
1718
pathPrefix = openApiSpec.basePath,

src/user_agent.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { process } from 'process'
2+
3+
import { version } from '../package.json'
4+
5+
const isNode = function () {
6+
return typeof process === 'object'
7+
}
8+
9+
const getBrowserName = function () {
10+
const { userAgent } = navigator
11+
let browserName = 'Unkown Browser'
12+
13+
if (/chrome|chromium|crios/i.test(userAgent)) browserName = 'chrome'
14+
if (/firefox|fxios/i.test(userAgent)) browserName = 'firefox'
15+
if (/safari/i.test(userAgent)) browserName = 'safari'
16+
if (/opr/i.test(userAgent)) browserName = 'opera'
17+
if (/edg/i.test(userAgent)) browserName = 'edge'
18+
19+
return browserName
20+
}
21+
22+
const getBrowserPlatormData = function () {
23+
const browserName = getBrowserName()
24+
const { appVersion } = navigator
25+
26+
let OSName = 'Unknown OS'
27+
28+
if (appVersion.includes('Win')) OSName = 'Windows'
29+
if (appVersion.includes('Mac')) OSName = 'MacOS'
30+
if (appVersion.includes('X11')) OSName = 'UNIX'
31+
if (appVersion.includes('Linux')) OSName = 'Linux'
32+
33+
return { os: OSName, runtime: browserName, runtimeVersion: appVersion }
34+
}
35+
36+
const getNodePlatformData = function () {
37+
const { platform } = process
38+
const {
39+
versions: { node: runtimeVersion },
40+
} = process
41+
42+
let OSName = platform
43+
44+
if (platform === 'darwin') OSName = 'MacOS'
45+
if (platform.includes('win')) OSName = 'Windows'
46+
47+
return { os: OSName, runtime: 'nodeJS', runtimeVersion }
48+
}
49+
50+
export const getUserAgent = function () {
51+
const packageVersion = version
52+
const platformData = isNode() ? getNodePlatformData() : getBrowserPlatormData()
53+
const { os, runtime, runtimeVersion } = platformData
54+
55+
return `netlify/js-client ${packageVersion}; ${runtime} version ${runtimeVersion}; OS: ${os}`
56+
}

0 commit comments

Comments
 (0)