@@ -3,17 +3,17 @@ import process from 'node:process'
33import colors from 'yoctocolors-cjs'
44
55import { logger } from '@socketsecurity/registry/lib/logger'
6+ import { isNonEmptyString } from '@socketsecurity/registry/lib/strings'
67
78import { AuthError } from './errors'
89import constants from '../constants'
10+ import { getSetting } from './settings'
911
1012import type {
1113 SocketSdkErrorType ,
1214 SocketSdkOperations
1315} from '@socketsecurity/sdk'
1416
15- const { API_V0_URL } = constants
16-
1717export 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+
6675export 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 : {
0 commit comments