Skip to content

Commit cbbd8c2

Browse files
authored
Refactor package filename to handle-* fetch-* and output-* (#371)
* Use handle- fetch- and output- for command file steps * Read and prioritize env var for url
1 parent 6d02a16 commit cbbd8c2

File tree

6 files changed

+25
-12
lines changed

6 files changed

+25
-12
lines changed

src/commands/package/cmd-package-shallow.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import colors from 'yoctocolors-cjs'
22

33
import { logger } from '@socketsecurity/registry/lib/logger'
44

5+
import { handlePurlsShallowScore } from './handle-purls-shallow-score'
56
import { parsePackageSpecifiers } from './parse-package-specifiers'
6-
import { showPurlInfo } from './show-purl-info'
77
import constants from '../../constants'
88
import { commonFlags, outputFlags } from '../../flags'
99
import { meowOrExit } from '../../utils/meow-with-subcommands'
@@ -103,7 +103,7 @@ async function run(
103103
return
104104
}
105105

106-
await showPurlInfo({
106+
await handlePurlsShallowScore({
107107
outputKind: json ? 'json' : markdown ? 'markdown' : 'text',
108108
purls
109109
})

src/commands/package/fetch-package-info.ts renamed to src/commands/package/fetch-purls-shallow-score.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type {
99
SocketSdkReturnType
1010
} from '@socketsecurity/sdk'
1111

12-
export async function fetchPackageInfo(
12+
export async function fetchPurlsShallowScore(
1313
purls: string[]
1414
): Promise<SocketSdkReturnType<'batchPackageFetch'>> {
1515
const socketSdk = await setupSdk(getPublicToken())

src/commands/package/show-purl-info.ts renamed to src/commands/package/handle-purls-shallow-score.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { fetchPackageInfo } from './fetch-package-info'
2-
import { logPackageInfo } from './log-package-info'
1+
import { fetchPurlsShallowScore } from './fetch-purls-shallow-score'
2+
import { outputPurlsShallowScore } from './output-purls-shallow-score'
33

44
import type { components } from '@socketsecurity/sdk/types/api'
55

6-
export async function showPurlInfo({
6+
export async function handlePurlsShallowScore({
77
outputKind,
88
purls
99
}: {
1010
outputKind: 'json' | 'markdown' | 'text'
1111
purls: string[]
1212
}) {
13-
const packageData = await fetchPackageInfo(purls)
13+
const packageData = await fetchPurlsShallowScore(purls)
1414
if (packageData) {
15-
logPackageInfo(
15+
outputPurlsShallowScore(
1616
purls,
1717
packageData.data as Array<components['schemas']['SocketArtifact']>,
1818
outputKind

src/commands/package/log-package-info.ts renamed to src/commands/package/output-purls-shallow-score.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { logger } from '@socketsecurity/registry/lib/logger'
55

66
import type { components } from '@socketsecurity/sdk/types/api'
77

8-
export function logPackageInfo(
8+
export function outputPurlsShallowScore(
99
purls: string[],
1010
packageData: Array<components['schemas']['SocketArtifact']>,
1111
outputKind: 'json' | 'markdown' | 'text'

src/utils/api.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import process from 'node:process'
33
import colors from 'yoctocolors-cjs'
44

55
import { logger } from '@socketsecurity/registry/lib/logger'
6+
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
67

78
import { AuthError } from './errors'
89
import constants from '../constants'
10+
import { getSetting } from './settings'
911

1012
import type {
1113
SocketSdkErrorType,
1214
SocketSdkOperations
1315
} from '@socketsecurity/sdk'
1416

15-
const { API_V0_URL } = constants
16-
1717
export function handleUnsuccessfulApiResponse<T extends SocketSdkOperations>(
1818
_name: T,
1919
result: SocketSdkErrorType<T>
@@ -55,6 +55,8 @@ export async function handleAPIError(code: number) {
5555
return 'One of the options passed might be incorrect.'
5656
} else if (code === 403) {
5757
return 'You might be trying to access an organization that is not linked to the API key you are logged in with.'
58+
} else {
59+
;`Server responded with status code ${code}`
5860
}
5961
}
6062

@@ -63,7 +65,15 @@ export function getLastFiveOfApiToken(token: string): string {
6365
return token.slice(-9, -4)
6466
}
6567

68+
// The API server that should be used for operations.
69+
function getDefaultApiBaseUrl(): string | undefined {
70+
const baseUrl =
71+
process.env['SOCKET_SECURITY_API_BASE_URL'] || getSetting('apiBaseUrl')
72+
return isNonEmptyString(baseUrl) ? baseUrl : undefined
73+
}
74+
6675
export async function queryAPI(path: string, apiToken: string) {
76+
const API_V0_URL = getDefaultApiBaseUrl()
6777
return await fetch(`${API_V0_URL}/${path}`, {
6878
method: 'GET',
6979
headers: {

src/utils/sdk.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ export function getDefaultToken(): string | undefined {
4848
}
4949

5050
export function getPublicToken(): string {
51-
return getDefaultToken() ?? SOCKET_PUBLIC_API_TOKEN
51+
return (
52+
(process.env['SOCKET_SECURITY_API_TOKEN'] || getDefaultToken()) ??
53+
SOCKET_PUBLIC_API_TOKEN
54+
)
5255
}
5356

5457
export async function setupSdk(

0 commit comments

Comments
 (0)