1
-
2
- const got = require ( 'got' )
3
- const config = require ( './args' )
4
- const auth = require ( './auth' )
5
- const { logger, getSymbol } = require ( './logger' )
6
- const { serializeError } = require ( 'serialize-error' )
1
+ import got from 'got'
2
+ import { options } from './args.js'
3
+ import { getToken , tokens } from './auth.js'
4
+ import { logger , getSymbol } from './logger.js'
7
5
8
6
const cache = {
9
7
collection : null ,
@@ -13,21 +11,21 @@ const cache = {
13
11
scapBenchmarkMap : null ,
14
12
stigs : null
15
13
}
16
-
17
- module . exports . cache = cache
14
+ const _cache = cache
15
+ export { _cache as cache }
18
16
19
17
async function apiGet ( endpoint , authenticate = true ) {
20
18
try {
21
- const options = {
19
+ const requestOptions = {
22
20
responseType : 'json'
23
21
}
24
22
if ( authenticate ) {
25
- await auth . getToken ( )
26
- options . headers = {
27
- Authorization : `Bearer ${ auth . tokens . access_token } `
23
+ await getToken ( )
24
+ requestOptions . headers = {
25
+ Authorization : `Bearer ${ tokens . access_token } `
28
26
}
29
27
}
30
- const response = await got . get ( `${ config . api } ${ endpoint } ` , options )
28
+ const response = await got . get ( `${ options . api } ${ endpoint } ` , requestOptions )
31
29
logResponse ( response )
32
30
return response . body
33
31
}
@@ -38,38 +36,38 @@ async function apiGet(endpoint, authenticate = true) {
38
36
}
39
37
}
40
38
41
- module . exports . getScapBenchmarkMap = async function ( ) {
39
+ export async function getScapBenchmarkMap ( ) {
42
40
const response = await apiGet ( '/stigs/scap-maps' )
43
41
cache . scapBenchmarkMap = new Map ( response . map ( apiScapMap => [ apiScapMap . scapBenchmarkId , apiScapMap . benchmarkId ] ) )
44
42
return cache . scapBenchmarkMap
45
43
}
46
44
47
- module . exports . getDefinition = async function ( jsonPath ) {
45
+ export async function getDefinition ( jsonPath ) {
48
46
cache . definition = await apiGet ( `/op/definition${ jsonPath ? '?jsonpath=' + encodeURIComponent ( jsonPath ) : '' } ` , false )
49
47
return cache . definition
50
48
}
51
49
52
- module . exports . getCollection = async function ( collectionId ) {
50
+ export async function getCollection ( collectionId ) {
53
51
cache . collection = await apiGet ( `/collections/${ collectionId } ` )
54
52
return cache . collection
55
53
}
56
54
57
- module . exports . getCollectionAssets = async function ( collectionId ) {
55
+ export async function getCollectionAssets ( collectionId ) {
58
56
cache . assets = await apiGet ( `/assets?collectionId=${ collectionId } &projection=stigs` )
59
57
return cache . assets
60
58
}
61
59
62
- module . exports . getInstalledStigs = async function ( ) {
60
+ export async function getInstalledStigs ( ) {
63
61
cache . stigs = await apiGet ( '/stigs' )
64
62
return cache . stigs
65
63
}
66
64
67
- module . exports . createOrGetAsset = async function ( asset ) {
65
+ export async function createOrGetAsset ( asset ) {
68
66
try {
69
- await auth . getToken ( )
70
- const response = await got . post ( `${ config . api } /assets?projection=stigs` , {
67
+ await getToken ( )
68
+ const response = await got . post ( `${ options . api } /assets?projection=stigs` , {
71
69
headers : {
72
- Authorization : `Bearer ${ auth . tokens . access_token } `
70
+ Authorization : `Bearer ${ tokens . access_token } `
73
71
} ,
74
72
json : asset ,
75
73
responseType : 'json'
@@ -87,12 +85,12 @@ module.exports.createOrGetAsset = async function (asset) {
87
85
}
88
86
}
89
87
90
- module . exports . patchAsset = async function ( assetId , body ) {
88
+ export async function patchAsset ( assetId , body ) {
91
89
try {
92
- await auth . getToken ( )
93
- const response = await got . patch ( `${ config . api } /assets/${ assetId } ?projection=stigs` , {
90
+ await getToken ( )
91
+ const response = await got . patch ( `${ options . api } /assets/${ assetId } ?projection=stigs` , {
94
92
headers : {
95
- Authorization : `Bearer ${ auth . tokens . access_token } `
93
+ Authorization : `Bearer ${ tokens . access_token } `
96
94
} ,
97
95
json : body ,
98
96
responseType : 'json'
@@ -106,12 +104,12 @@ module.exports.patchAsset = async function (assetId, body) {
106
104
}
107
105
}
108
106
109
- module . exports . postReviews = async function ( collectionId , assetId , reviews ) {
107
+ export async function postReviews ( collectionId , assetId , reviews ) {
110
108
try {
111
- await auth . getToken ( )
112
- const response = await got . post ( `${ config . api } /collections/${ collectionId } /reviews/${ assetId } ` , {
109
+ await getToken ( )
110
+ const response = await got . post ( `${ options . api } /collections/${ collectionId } /reviews/${ assetId } ` , {
113
111
headers : {
114
- Authorization : `Bearer ${ auth . tokens . access_token } `
112
+ Authorization : `Bearer ${ tokens . access_token } `
115
113
} ,
116
114
json : reviews ,
117
115
responseType : 'json'
@@ -125,12 +123,12 @@ module.exports.postReviews = async function (collectionId, assetId, reviews) {
125
123
}
126
124
}
127
125
128
- module . exports . getUser = async function ( ) {
126
+ export async function getUser ( ) {
129
127
cache . user = await apiGet ( '/user' )
130
128
return cache . user
131
129
}
132
130
133
- module . exports . canUserAccept = function ( ) {
131
+ export function canUserAccept ( ) {
134
132
const curUser = cache . user
135
133
const apiCollection = cache . collection
136
134
const userGrant = curUser . collectionGrants . find ( i => i . collection . collectionId === apiCollection . collectionId ) ?. accessLevel
0 commit comments