@@ -8,18 +8,20 @@ import {
88 StandardResolutionReasons ,
99 TypeMismatchError ,
1010} from '@openfeature/js-sdk' ;
11- import axios from 'axios' ;
11+ import axios , { AxiosRequestConfig } from 'axios' ;
1212import { transformContext } from './context-transformer' ;
1313import { ProxyNotReady } from './errors/proxyNotReady' ;
1414import { ProxyTimeout } from './errors/proxyTimeout' ;
1515import { UnknownError } from './errors/unknownError' ;
16+ import { Unauthorized } from './errors/unauthorized' ;
1617import {
1718 GoFeatureFlagProviderOptions ,
1819 GoFeatureFlagProxyRequest ,
1920 GoFeatureFlagProxyResponse ,
2021 GoFeatureFlagUser ,
2122} from './model' ;
2223
24+
2325// GoFeatureFlagProvider is the official Open-feature provider for GO Feature Flag.
2426export class GoFeatureFlagProvider implements Provider {
2527 metadata = {
@@ -30,10 +32,13 @@ export class GoFeatureFlagProvider implements Provider {
3032 private endpoint : string ;
3133 // timeout in millisecond before we consider the request as a failure
3234 private timeout : number ;
35+ // apiKey contains the token to use while calling GO Feature Flag relay proxy
36+ private apiKey ?: string ;
3337
3438 constructor ( options : GoFeatureFlagProviderOptions ) {
3539 this . timeout = options . timeout || 0 ; // default is 0 = no timeout
3640 this . endpoint = options . endpoint ;
41+ this . apiKey = options . apiKey ;
3742 }
3843
3944 /**
@@ -149,7 +154,7 @@ export class GoFeatureFlagProvider implements Provider {
149154 * @throws {ProxyTimeout } When the HTTP call is timing out
150155 * @throws {UnknownError } When an unknown error occurs
151156 * @throws {TypeMismatchError } When the type of the variation is not the one expected
152- * @throws {FlagNotFoundError } When the flag does not exists
157+ * @throws {FlagNotFoundError } When the flag does not exist
153158 */
154159 async resolveEvaluationGoFeatureFlagProxy < T > (
155160 flagKey : string ,
@@ -165,19 +170,24 @@ export class GoFeatureFlagProvider implements Provider {
165170
166171 let apiResponseData : GoFeatureFlagProxyResponse < T > ;
167172 try {
168- const response = await axios . post < GoFeatureFlagProxyResponse < T > > (
169- endpointURL . toString ( ) ,
170- request ,
171- {
172- headers : {
173- 'Content-Type' : 'application/json' ,
174- Accept : 'application/json' ,
175- } ,
176- timeout : this . timeout ,
177- }
178- ) ;
173+ const reqConfig : AxiosRequestConfig = {
174+ headers : {
175+ 'Content-Type' : 'application/json' ,
176+ Accept : 'application/json' ,
177+ } ,
178+ timeout : this . timeout ,
179+ } ;
180+
181+ if ( this . apiKey ) {
182+ reqConfig . headers ?. put ( 'Authorization' , `Bearer ${ this . apiKey } ` ) ;
183+ }
184+
185+ const response = await axios . post < GoFeatureFlagProxyResponse < T > > ( endpointURL . toString ( ) , request , reqConfig ) ;
179186 apiResponseData = response . data ;
180187 } catch ( error ) {
188+ if ( axios . isAxiosError ( error ) && error . response ?. status == 401 ) {
189+ throw new Unauthorized ( 'invalid token used to contact GO Feature Flag relay proxy instance' ) ;
190+ }
181191 // Impossible to contact the relay-proxy
182192 if (
183193 axios . isAxiosError ( error ) &&
0 commit comments