@dotenvx/dotenvx-radar
Version:
Dotenvx Radar
60 lines (52 loc) • 1.65 kB
JavaScript
const { http } = require('../../lib/helpers/http')
const buildApiError = require('../../lib/helpers/buildApiError')
const packageJson = require('../../lib/helpers/packageJson')
class PostObserve {
constructor (hostname, token, encoded, pwd = null, gitUrl = null, gitBranch = null, systemUuid = null, osPlatform = null, osArch = null) {
this.hostname = hostname || 'https://radar.dotenvx.com'
this.token = token
this.encoded = encoded
this.pwd = pwd
this.gitUrl = gitUrl
this.gitBranch = gitBranch
this.systemUuid = systemUuid
this.osPlatform = osPlatform
this.osArch = osArch
}
async run () {
const token = this.token
const url = `${this.hostname}/api/observe`
const encoded = this.encoded
const observedAt = new Date().toISOString()
const pwd = this.pwd
const gitUrl = this.gitUrl
const gitBranch = this.gitBranch
const systemUuid = this.systemUuid
const osPlatform = this.osPlatform
const osArch = this.osArch
const resp = await http(url, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
encoded,
observed_at: observedAt,
pwd,
git_url: gitUrl,
git_branch: gitBranch,
system_uuid: systemUuid,
os_platform: osPlatform,
os_arch: osArch,
cli_version: packageJson.version
})
})
const json = await resp.body.json()
if (resp.statusCode >= 400) {
throw buildApiError(resp.statusCode, json)
}
return json
}
}
module.exports = PostObserve