@@ -7,6 +7,7 @@ import { SocketSdkReturnType } from '@socketsecurity/sdk'
77import constants from '../../constants'
88import { handleApiCall , handleUnsuccessfulApiResponse } from '../../utils/api'
99import { AuthError } from '../../utils/errors'
10+ import { mdTable } from '../../utils/markdown'
1011import { getDefaultToken , setupSdk } from '../../utils/sdk'
1112
1213import type { Choice } from '@socketsecurity/registry/lib/prompts'
@@ -111,7 +112,7 @@ async function outputAsMarkdown(
111112 perPage : number
112113) : Promise < void > {
113114 try {
114- const table = mdTable ( auditLogs , [
115+ const table = mdTable < any > ( auditLogs , [
115116 'event_id' ,
116117 'created_at' ,
117118 'type' ,
@@ -144,46 +145,6 @@ ${table}
144145 }
145146}
146147
147- function mdTable <
148- T extends SocketSdkReturnType < 'getAuditLogEvents' > [ 'data' ] [ 'results' ]
149- > (
150- logs : T ,
151- // This is saying "an array of strings and the strings are a valid key of elements of T"
152- // In turn, T is defined above as the audit log event type from our OpenAPI docs.
153- cols : Array < string & keyof T [ number ] >
154- ) : string {
155- // Max col width required to fit all data in that column
156- const cws = cols . map ( col => col . length )
157-
158- for ( const log of logs ) {
159- for ( let i = 0 ; i < cols . length ; ++ i ) {
160- // @ts -ignore
161- const val : unknown = log [ cols [ i ] ?? '' ] ?? ''
162- cws [ i ] = Math . max ( cws [ i ] ?? 0 , String ( val ) . length )
163- }
164- }
165-
166- let div = '|'
167- for ( const cw of cws ) div += ' ' + '-' . repeat ( cw ) + ' |'
168-
169- let header = '|'
170- for ( let i = 0 ; i < cols . length ; ++ i )
171- header += ' ' + String ( cols [ i ] ) . padEnd ( cws [ i ] ?? 0 , ' ' ) + ' |'
172-
173- let body = ''
174- for ( const log of logs ) {
175- body += '|'
176- for ( let i = 0 ; i < cols . length ; ++ i ) {
177- // @ts -ignore
178- const val : unknown = log [ cols [ i ] ?? '' ] ?? ''
179- body += ' ' + String ( val ) . padEnd ( cws [ i ] ?? 0 , ' ' ) + ' |'
180- }
181- body += '\n'
182- }
183-
184- return [ div , header , div , body . trim ( ) , div ] . filter ( s => ! ! s . trim ( ) ) . join ( '\n' )
185- }
186-
187148async function outputAsPrint (
188149 auditLogs : SocketSdkReturnType < 'getAuditLogEvents' > [ 'data' ] [ 'results' ] ,
189150 orgSlug : string ,
0 commit comments