-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathapi.ts
55 lines (47 loc) · 1.5 KB
/
api.ts
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
import type {AxiosPromise, AxiosRequestConfig, AxiosResponse} from 'axios'
import {CONTENT_TYPE_HEADER, CONTENT_TYPE_VALUE_JSON, METHOD_POST} from '../../constants'
import {getRequestBuilder} from '../../helpers/utils'
import {getBaseUrl} from '../junit/utils'
import {API_ENDPOINT} from './constants'
import {ScaRequest} from './types'
const maxBodyLength = Infinity
/**
* Get the function to upload our results to the intake.
* @param apiKey
*/
export const getApiHelper = (
apiKey: string,
appKey: string
): ((scaRequest: ScaRequest) => AxiosPromise<AxiosResponse>) => {
/**
* function used to marshall and send the data
* @param request - the AXIOS element used to send the request
*/
const uploadSBomPayload = (request: (args: AxiosRequestConfig) => AxiosPromise<AxiosResponse>) => async (
scaPayload: ScaRequest
) => {
// Make sure we follow the API signature
const payload = {
data: {
type: 'scarequests',
attributes: scaPayload,
},
}
return request({
data: JSON.stringify(payload),
headers: {
[CONTENT_TYPE_HEADER]: CONTENT_TYPE_VALUE_JSON,
'DD-EVP-ORIGIN': 'datadog-ci',
'DD-EVP-ORIGIN-VERSION': '0.0.1',
},
maxBodyLength,
method: METHOD_POST,
url: API_ENDPOINT,
})
}
// Get the intake name
const url = getBaseUrl()
// Get the AXIOS request/response function
const requestIntake = getRequestBuilder({baseUrl: url, apiKey, appKey})
return uploadSBomPayload(requestIntake)
}