11import axios from 'axios' ;
22
33import type { ToolsLogger } from '@sap-ux/logger' ;
4+ import type { Destinations } from '@sap-ux/btp-utils' ;
45
56import { t } from '../i18n' ;
6- import type { Uaa , BtpDestinationConfig } from '../types' ;
7+ import type { Uaa , BtpDestinationConfig , CfDestinationServiceCredentials } from '../types' ;
78
89/**
910 * Obtain an OAuth2 access token using the client credentials grant.
@@ -27,8 +28,8 @@ export async function getToken(uaa: Uaa, logger?: ToolsLogger): Promise<string>
2728 logger ?. debug ( 'OAuth token obtained successfully' ) ;
2829 return response . data [ 'access_token' ] ;
2930 } catch ( e ) {
30- logger ?. error ( `Failed to obtain OAuth token from ${ uri } : ${ e . message } ` ) ;
31- throw new Error ( t ( 'error.failedToGetAuthKey' , { error : e . message } ) ) ;
31+ logger ?. error ( `Failed to obtain OAuth token from ${ uri } : ${ e instanceof Error ? e . message : String ( e ) } ` ) ;
32+ throw new Error ( t ( 'error.failedToGetAuthKey' , { error : e instanceof Error ? e . message : String ( e ) } ) ) ;
3233 }
3334}
3435
@@ -59,7 +60,43 @@ export async function getBtpDestinationConfig(
5960 logger ?. debug ( `Destination "${ destinationName } " config: ProxyType=${ config ?. ProxyType } ` ) ;
6061 return config ;
6162 } catch ( e ) {
62- logger ?. error ( `Failed to fetch destination config for "${ destinationName } ": ${ e . message } ` ) ;
63+ logger ?. error (
64+ `Failed to fetch destination config for "${ destinationName } ": ${ e instanceof Error ? e . message : String ( e ) } `
65+ ) ;
6366 return undefined ;
6467 }
6568}
69+
70+ /**
71+ * Lists all subaccount destinations from the BTP Destination Configuration API.
72+ *
73+ * @param {CfDestinationServiceCredentials } credentials - Destination service credentials.
74+ * @returns {Promise<Destinations> } Map of destination name to Destination object.
75+ */
76+ export async function listBtpDestinations ( credentials : CfDestinationServiceCredentials ) : Promise < Destinations > {
77+ const uaa =
78+ 'uaa' in credentials
79+ ? credentials . uaa
80+ : { clientid : credentials . clientid , clientsecret : credentials . clientsecret , url : credentials . url } ;
81+ const token = await getToken ( uaa ) ;
82+ const url = `${ credentials . uri } /destination-configuration/v1/subaccountDestinations` ;
83+ try {
84+ const response = await axios . get < BtpDestinationConfig [ ] > ( url , {
85+ headers : { Authorization : `Bearer ${ token } ` }
86+ } ) ;
87+ const configs = Array . isArray ( response . data ) ? response . data : [ ] ;
88+ return configs . reduce < Destinations > ( ( acc , config ) => {
89+ acc [ config . Name ] = {
90+ Name : config . Name ,
91+ Host : config . URL ,
92+ Type : config . Type ,
93+ Authentication : config . Authentication ,
94+ ProxyType : config . ProxyType ,
95+ Description : config . Description ?? ''
96+ } ;
97+ return acc ;
98+ } , { } ) ;
99+ } catch ( e ) {
100+ throw new Error ( t ( 'error.failedToListBtpDestinations' , { error : e instanceof Error ? e . message : String ( e ) } ) ) ;
101+ }
102+ }
0 commit comments