@@ -3,11 +3,7 @@ import { stripIndents } from 'common-tags'
33import { logger } from '@socketsecurity/registry/lib/logger'
44import { Separator , select } from '@socketsecurity/registry/lib/prompts'
55
6- import constants from '../../constants'
7- import { handleApiCall , handleUnsuccessfulApiResponse } from '../../utils/api'
8- import { AuthError } from '../../utils/errors'
96import { mdTable } from '../../utils/markdown'
10- import { getDefaultToken , setupSdk } from '../../utils/sdk'
117
128import type { Choice } from '@socketsecurity/registry/lib/prompts'
139import type { SocketSdkReturnType } from '@socketsecurity/sdk'
@@ -16,41 +12,29 @@ type AuditChoice = Choice<string>
1612
1713type AuditChoices = Array < Separator | AuditChoice >
1814
19- export async function getAuditLog ( {
20- logType,
21- orgSlug,
22- outputKind,
23- page,
24- perPage
25- } : {
26- outputKind : 'json' | 'markdown' | 'print'
27- orgSlug : string
28- page : number
29- perPage : number
30- logType : string
31- } ) : Promise < void > {
32- const apiToken = getDefaultToken ( )
33- if ( ! apiToken ) {
34- throw new AuthError (
35- 'User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key.'
36- )
37- }
38-
39- const auditLogs = await getAuditLogWithToken ( {
40- apiToken,
15+ export async function outputAuditLog (
16+ auditLogs : SocketSdkReturnType < 'getAuditLogEvents' > [ 'data' ] ,
17+ {
18+ logType,
4119 orgSlug,
4220 outputKind,
4321 page,
44- perPage,
45- logType
46- } )
47- if ( ! auditLogs ) return
48-
49- if ( outputKind === 'json' )
22+ perPage
23+ } : {
24+ outputKind : 'json' | 'markdown' | 'print'
25+ orgSlug : string
26+ page : number
27+ perPage : number
28+ logType : string
29+ }
30+ ) : Promise < void > {
31+ if ( outputKind === 'json' ) {
5032 await outputAsJson ( auditLogs . results , orgSlug , logType , page , perPage )
51- else if ( outputKind === 'markdown' )
33+ } else if ( outputKind === 'markdown' ) {
5234 await outputAsMarkdown ( auditLogs . results , orgSlug , logType , page , perPage )
53- else await outputAsPrint ( auditLogs . results , orgSlug , logType )
35+ } else {
36+ await outputAsPrint ( auditLogs . results , orgSlug , logType )
37+ }
5438}
5539
5640async function outputAsJson (
@@ -174,48 +158,3 @@ async function outputAsPrint(
174158 ]
175159 )
176160}
177-
178- async function getAuditLogWithToken ( {
179- apiToken,
180- logType,
181- orgSlug,
182- outputKind,
183- page,
184- perPage
185- } : {
186- apiToken : string
187- outputKind : 'json' | 'markdown' | 'print'
188- orgSlug : string
189- page : number
190- perPage : number
191- logType : string
192- } ) : Promise < SocketSdkReturnType < 'getAuditLogEvents' > [ 'data' ] | void > {
193- // Lazily access constants.spinner.
194- const { spinner } = constants
195-
196- spinner . start ( `Looking up audit log for ${ orgSlug } ` )
197-
198- const socketSdk = await setupSdk ( apiToken )
199- const result = await handleApiCall (
200- socketSdk . getAuditLogEvents ( orgSlug , {
201- // I'm not sure this is used at all.
202- outputJson : String ( outputKind === 'json' ) ,
203- // I'm not sure this is used at all.
204- outputMarkdown : String ( outputKind === 'markdown' ) ,
205- orgSlug,
206- type : logType ,
207- page : String ( page ) ,
208- per_page : String ( perPage )
209- } ) ,
210- `Looking up audit log for ${ orgSlug } \n`
211- )
212-
213- if ( ! result . success ) {
214- handleUnsuccessfulApiResponse ( 'getAuditLogEvents' , result )
215- return
216- }
217-
218- spinner . stop ( )
219-
220- return result . data
221- }
0 commit comments