Skip to content

Refactor package filename to handle-* fetch-* and output-* #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/commands/package/cmd-package-shallow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import colors from 'yoctocolors-cjs'

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

import { handlePurlsShallowScore } from './handle-purls-shallow-score'
import { parsePackageSpecifiers } from './parse-package-specifiers'
import { showPurlInfo } from './show-purl-info'
import constants from '../../constants'
import { commonFlags, outputFlags } from '../../flags'
import { meowOrExit } from '../../utils/meow-with-subcommands'
Expand Down Expand Up @@ -103,7 +103,7 @@ async function run(
return
}

await showPurlInfo({
await handlePurlsShallowScore({
outputKind: json ? 'json' : markdown ? 'markdown' : 'text',
purls
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
SocketSdkReturnType
} from '@socketsecurity/sdk'

export async function fetchPackageInfo(
export async function fetchPurlsShallowScore(
purls: string[]
): Promise<SocketSdkReturnType<'batchPackageFetch'>> {
const socketSdk = await setupSdk(getPublicToken())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { fetchPackageInfo } from './fetch-package-info'
import { logPackageInfo } from './log-package-info'
import { fetchPurlsShallowScore } from './fetch-purls-shallow-score'
import { outputPurlsShallowScore } from './output-purls-shallow-score'

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

export async function showPurlInfo({
export async function handlePurlsShallowScore({
outputKind,
purls
}: {
outputKind: 'json' | 'markdown' | 'text'
purls: string[]
}) {
const packageData = await fetchPackageInfo(purls)
const packageData = await fetchPurlsShallowScore(purls)
if (packageData) {
logPackageInfo(
outputPurlsShallowScore(
purls,
packageData.data as Array<components['schemas']['SocketArtifact']>,
outputKind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { logger } from '@socketsecurity/registry/lib/logger'

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

export function logPackageInfo(
export function outputPurlsShallowScore(
purls: string[],
packageData: Array<components['schemas']['SocketArtifact']>,
outputKind: 'json' | 'markdown' | 'text'
Expand Down
14 changes: 12 additions & 2 deletions src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import process from 'node:process'
import colors from 'yoctocolors-cjs'

import { logger } from '@socketsecurity/registry/lib/logger'
import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'

import { AuthError } from './errors'
import constants from '../constants'
import { getSetting } from './settings'

import type {
SocketSdkErrorType,
SocketSdkOperations
} from '@socketsecurity/sdk'

const { API_V0_URL } = constants

export function handleUnsuccessfulApiResponse<T extends SocketSdkOperations>(
_name: T,
result: SocketSdkErrorType<T>
Expand Down Expand Up @@ -55,6 +55,8 @@ export async function handleAPIError(code: number) {
return 'One of the options passed might be incorrect.'
} else if (code === 403) {
return 'You might be trying to access an organization that is not linked to the API key you are logged in with.'
} else {
;`Server responded with status code ${code}`
}
}

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

// The API server that should be used for operations.
function getDefaultApiBaseUrl(): string | undefined {
const baseUrl =
process.env['SOCKET_SECURITY_API_BASE_URL'] || getSetting('apiBaseUrl')
return isNonEmptyString(baseUrl) ? baseUrl : undefined
}

export async function queryAPI(path: string, apiToken: string) {
const API_V0_URL = getDefaultApiBaseUrl()
return await fetch(`${API_V0_URL}/${path}`, {
method: 'GET',
headers: {
Expand Down
5 changes: 4 additions & 1 deletion src/utils/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ export function getDefaultToken(): string | undefined {
}

export function getPublicToken(): string {
return getDefaultToken() ?? SOCKET_PUBLIC_API_TOKEN
return (
(process.env['SOCKET_SECURITY_API_TOKEN'] || getDefaultToken()) ??
SOCKET_PUBLIC_API_TOKEN
)
}

export async function setupSdk(
Expand Down
Loading