Skip to content

Commit d33a286

Browse files
committed
wip
1 parent a882f84 commit d33a286

File tree

3 files changed

+42
-28
lines changed

3 files changed

+42
-28
lines changed

.dep-stats.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,12 @@
2929
"chalk": "^5.3.0",
3030
"cli-cursor": "^4.0.0",
3131
"configstore": "^7.0.0",
32-
"data-uri-to-buffer": "^4.0.0",
3332
"default-browser": "^5.2.1",
3433
"default-browser-id": "^5.0.0",
3534
"define-lazy-prop": "^3.0.0",
3635
"dot-prop": "^9.0.0",
3736
"escape-goat": "^4.0.0",
3837
"execa": "^9.3.0",
39-
"fetch-blob": "^3.1.2",
4038
"figures": "^6.1.0",
4139
"get-east-asian-width": "^1.0.0",
4240
"get-stream": "^9.0.0",

src/commands/diff-scan/get.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
// @ts-nocheck
2-
/* eslint-disable */
31
import chalk from 'chalk'
42
import meow from 'meow'
53
import ora from 'ora'
6-
import fetch from 'node-fetch'
74
import util from 'util'
85

96
import { outputFlags } from '../../flags'
10-
// import {
11-
// handleApiCall,
12-
// handleUnsuccessfulApiResponse
13-
// } from '../../utils/api-helpers'
147
import { printFlagList } from '../../utils/formatting'
158
import { getDefaultKey } from '../../utils/sdk'
169

1710
import type { CliSubcommand } from '../../utils/meow-with-subcommands'
1811
import type { Ora } from 'ora'
1912
import { AuthError } from '../../utils/errors'
13+
import { handleAPIError, queryAPI } from '../../utils/api-helpers'
2014

2115
export const get: CliSubcommand = {
2216
description: 'Get a diff scan for an organization',
@@ -30,7 +24,7 @@ export const get: CliSubcommand = {
3024
}
3125
const spinnerText = 'Getting diff scan... \n'
3226
const spinner = ora(spinnerText).start()
33-
await getDiffScan(input.before, input.after, spinner, apiKey)
27+
await getDiffScan(input.before, input.after, spinner, apiKey, input.orgSlug)
3428
}
3529
}
3630
}
@@ -64,6 +58,7 @@ type CommandContext = {
6458
before: string
6559
after: string
6660
preview: boolean
61+
orgSlug: string
6762
}
6863

6964
function setupCommand(
@@ -112,39 +107,43 @@ function setupCommand(
112107
return
113108
}
114109

110+
if(cli.input.length < 1){
111+
console.error(
112+
`${chalk.bgRed.white('Input error')}: Please provide an organization slug \n`
113+
)
114+
cli.showHelp()
115+
return
116+
}
117+
118+
const [orgSlug = ''] = cli.input
119+
115120
return <CommandContext>{
116121
outputJson,
117122
outputMarkdown,
118123
before,
119124
after,
120-
preview
125+
preview,
126+
orgSlug
121127
}
122128
}
123129

124130
async function getDiffScan(
125131
before: string,
126132
after: string,
127133
spinner: Ora,
128-
apiKey: string
134+
apiKey: string,
135+
orgSlug: string
129136
): Promise<void> {
130-
// const socketSdk = await setupSdk(apiKey)
131-
// const result = await handleApiCall(
132-
// socketSdk.getOrgFullScanList(orgSlug, input),
133-
// 'Listing scans'
134-
// )
135-
136-
const response = await fetch(`https://api.socket.dev/v0/orgs/SocketDev/full-scans/diff?before=${before}&after=${after}&preview`, {
137-
method: 'GET',
138-
headers: {
139-
'Authorization': 'Basic ' + btoa(`${apiKey}:${apiKey}`)
140-
}
141-
});
137+
const response = await queryAPI(`${orgSlug}/full-scans/diff?before=${before}&after=${after}&preview`, apiKey)
142138
const data = await response.json();
143139

144-
// if (!result.success) {
145-
// handleUnsuccessfulApiResponse('getOrgFullScanList', result, spinner)
146-
// return
147-
// }
140+
if(response.status !== 200){
141+
spinner.stop()
142+
const err = await handleAPIError(response.status)
143+
console.error(err)
144+
return
145+
}
146+
148147
spinner.stop()
149148

150149
// before: dfc4cf0c-aefd-4081-9e4e-7385257f26e2

src/utils/api-helpers.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,20 @@ export async function handleApiCall<T>(
4545

4646
return result
4747
}
48+
49+
export async function handleAPIError(code: number) {
50+
if(code === 400){
51+
return `Bad request`
52+
}
53+
}
54+
55+
const API_V0_URL = 'https://api.socket.dev/v0'
56+
57+
export async function queryAPI(path: string, apiKey: string) {
58+
return await fetch(`${API_V0_URL}/orgs/${path}`, {
59+
method: 'GET',
60+
headers: {
61+
'Authorization': 'Basic ' + btoa(`${apiKey}:${apiKey}`)
62+
}
63+
});
64+
}

0 commit comments

Comments
 (0)