@@ -22,16 +22,47 @@ export const threatFeed: CliSubcommand = {
2222 throw new AuthError ( "User must be authenticated to run this command. To log in, run the command `socket login` and enter your API key." )
2323 }
2424 const spinner = ora ( `Looking up the threat feed \n` ) . start ( )
25- await fetchThreatFeed ( spinner , apiKey )
25+ await fetchThreatFeed ( input , spinner , apiKey )
2626 }
2727 }
2828}
2929
30+ const threatFeedFlags = {
31+ perPage : {
32+ type : 'number' ,
33+ shortFlag : 'pp' ,
34+ default : 30 ,
35+ description : 'Number of items per page'
36+ } ,
37+ page : {
38+ type : 'string' ,
39+ shortFlag : 'p' ,
40+ default : '1' ,
41+ description : 'Page token'
42+ } ,
43+ direction : {
44+ type : 'string' ,
45+ shortFlag : 'd' ,
46+ default : 'desc' ,
47+ description : 'Order asc or desc by the createdAt attribute.'
48+ } ,
49+ filter : {
50+ type : 'string' ,
51+ shortFlag : 'f' ,
52+ default : 'mal' ,
53+ description : 'Filter what type of threats to return'
54+ }
55+ }
56+
3057// Internal functions
3158
3259type CommandContext = {
3360 outputJson : boolean
3461 outputMarkdown : boolean
62+ per_page : number
63+ page : string
64+ direction : string
65+ filter : string
3566}
3667
3768function setupCommand (
@@ -41,6 +72,7 @@ function setupCommand(
4172 importMeta : ImportMeta
4273) : CommandContext | undefined {
4374 const flags : { [ key : string ] : any } = {
75+ ...threatFeedFlags ,
4476 ...outputFlags
4577 }
4678
@@ -66,21 +98,36 @@ function setupCommand(
6698 const {
6799 json : outputJson ,
68100 markdown : outputMarkdown ,
101+ perPage : per_page ,
102+ page,
103+ direction,
104+ filter
69105 } = cli . flags
70106
71107 return < CommandContext > {
72108 outputJson,
73- outputMarkdown
109+ outputMarkdown,
110+ per_page,
111+ page,
112+ direction,
113+ filter
74114 }
75115}
76116
77117async function fetchThreatFeed (
118+ { per_page, page, direction, filter } : CommandContext ,
78119 spinner : Ora ,
79120 apiKey : string
80121) : Promise < void > {
81- const response = await queryAPI ( `threat-feed` , apiKey )
122+ const formattedQueryParams = formatQueryParams ( { per_page, page, direction, filter } ) . join ( '&' )
123+
124+ const response = await queryAPI ( `threat-feed?${ formattedQueryParams } ` , apiKey )
82125 const data = await response . json ( ) ;
83126
84127 spinner . stop ( )
85128 console . log ( data )
86129}
130+
131+ const formatQueryParams = ( params : any ) => {
132+ return Object . entries ( params ) . map ( entry => `${ entry [ 0 ] } =${ entry [ 1 ] } ` )
133+ }
0 commit comments