@@ -107,19 +107,9 @@ export class YepCodeApi {
107107 this . teamId = finalConfig . teamId ;
108108 this . accessToken = finalConfig . accessToken ;
109109 this . timeout = finalConfig . timeout ;
110- if ( ! this . clientId && this . accessToken ) {
111- this . clientId = this . clientIdFromAccessToken ( ) ;
112- }
113- if ( ! this . teamId && this . clientId ) {
114- this . teamId = this . teamIdFromClientId ( ) ;
115- }
116- }
117-
118- getClientId ( ) : string {
119- if ( ! this . clientId ) {
120- throw new Error ( "Client ID is not set" ) ;
110+ if ( ! this . teamId ) {
111+ this . initTeamId ( ) ;
121112 }
122- return this . clientId ;
123113 }
124114
125115 getTeamId ( ) : string {
@@ -129,28 +119,25 @@ export class YepCodeApi {
129119 return this . teamId ;
130120 }
131121
132- private clientIdFromAccessToken ( ) : string {
133- if ( ! this . accessToken ) {
134- throw new Error ( "Access token is not set" ) ;
135- }
136- const [ , payload ] = this . accessToken . split ( "." ) ;
137- const decodedPayload = JSON . parse (
138- Buffer . from ( payload , "base64" ) . toString ( )
139- ) ;
140- return decodedPayload . client_id ;
141- }
142-
143- private teamIdFromClientId ( ) : string {
144- if ( ! this . clientId ) {
145- throw new Error ( "Client ID is not set" ) ;
122+ private initTeamId ( ) : void {
123+ if ( this . clientId ) {
124+ const match = this . clientId . match ( / ^ s a - ( .* ) - [ a - z 0 - 9 ] { 8 } $ / ) ;
125+ if ( match ) {
126+ this . teamId = match [ 1 ] ;
127+ }
146128 }
147- const match = this . clientId . match ( / ^ s a - ( . * ) - [ a - z 0 - 9 ] { 8 } $ / ) ;
148- if ( ! match ) {
149- throw new Error (
150- "Client ID is not valid. It must be in the format sa-<teamId>-<8randomCharsOrDigits>"
129+ if ( ! this . teamId && this . accessToken ) {
130+ const [ , payload ] = this . accessToken . split ( "." ) ;
131+ const decodedPayload = JSON . parse (
132+ Buffer . from ( payload , "base64" ) . toString ( )
151133 ) ;
134+ this . teamId =
135+ decodedPayload . groups &&
136+ decodedPayload . groups . filter ( ( group : string ) => group !== "sandbox" ) [ 0 ] ;
137+ }
138+ if ( ! this . teamId ) {
139+ throw new Error ( "Team ID is not set" ) ;
152140 }
153- return match [ 1 ] ;
154141 }
155142
156143 private getBaseURL ( ) : string {
0 commit comments