-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathapi.ts
More file actions
104 lines (87 loc) · 3.47 KB
/
api.ts
File metadata and controls
104 lines (87 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import axios, { AxiosInstance, InternalAxiosRequestConfig } from 'axios'
import { name as CIname } from 'ci-info'
import config from '../services/config'
import { assignProxy } from '../services/util'
import Accounts from './accounts'
import Users from './users'
import Projects from './projects'
import Assets from './assets'
import Runtimes from './runtimes'
import PrivateLocations from './private-locations'
import Locations from './locations'
import TestSessions from './test-sessions'
import EnvironmentVariables from './environment-variables'
import HeartbeatChecks from './heartbeat-checks'
import ChecklyStorage from './checkly-storage'
import AlertChannels from './alert-channels';
export function getDefaults () {
const apiKey = config.getApiKey()
const accountId = config.getAccountId()
const baseURL = config.getApiUrl()
const Authorization = `Bearer ${apiKey}`
return { baseURL, accountId, Authorization, apiKey }
}
export async function validateAuthentication (): Promise<void> {
if (!config.hasValidCredentials()) {
throw new Error('Run `npx checkly login` or manually set `CHECKLY_API_KEY` ' +
'& `CHECKLY_ACCOUNT_ID` environment variables to setup authentication.')
}
const accountId = config.getAccountId()
const apiKey = config.getApiKey()
try {
// check if credentials works
await accounts.get(accountId)
} catch (err: any) {
if (err.response?.status === 401) {
throw new Error(`Authentication failed with account id "${accountId}" ` +
`and API key "...${apiKey?.slice(-4)}"`)
} else if (!err.response) {
// The request was made but no response was received. This may be due to an internet connection issue.
throw new Error(`Encountered an error connecting to Checkly. Please check that the internet connection is working. ${err.message}`)
} else {
throw new Error(`Encountered an unexpected error connecting to Checkly: ${err.message}`)
}
}
}
export function requestInterceptor (config: InternalAxiosRequestConfig) {
const { Authorization, accountId } = getDefaults()
if (Authorization && config.headers) {
config.headers.Authorization = Authorization
}
if (accountId && config.headers) {
config.headers['x-checkly-account'] = accountId
}
config.headers['x-checkly-ci-name'] = CIname
return config
}
export function responseErrorInterceptor (error: any) {
if (error.response?.status === 408) {
throw new Error('Encountered an error connecting to Checkly. ' +
'This can be triggered by a slow internet connection or a network with high packet loss.')
}
throw error
}
function init (): AxiosInstance {
const { baseURL } = getDefaults()
const axiosConf = assignProxy(baseURL, { baseURL })
const api = axios.create(axiosConf)
api.interceptors.request.use(requestInterceptor)
api.interceptors.response.use(
response => response,
responseErrorInterceptor,
)
return api
}
export const api = init()
export const accounts = new Accounts(api)
export const user = new Users(api)
export const projects = new Projects(api)
export const assets = new Assets(api)
export const runtimes = new Runtimes(api)
export const locations = new Locations(api)
export const privateLocations = new PrivateLocations(api)
export const testSessions = new TestSessions(api)
export const environmentVariables = new EnvironmentVariables(api)
export const heartbeatCheck = new HeartbeatChecks(api)
export const checklyStorage = new ChecklyStorage(api)
export const alertChannels = new AlertChannels(api)