@@ -8,18 +8,20 @@ import {
8
8
StandardResolutionReasons ,
9
9
TypeMismatchError ,
10
10
} from '@openfeature/js-sdk' ;
11
- import axios from 'axios' ;
11
+ import axios , { AxiosRequestConfig } from 'axios' ;
12
12
import { transformContext } from './context-transformer' ;
13
13
import { ProxyNotReady } from './errors/proxyNotReady' ;
14
14
import { ProxyTimeout } from './errors/proxyTimeout' ;
15
15
import { UnknownError } from './errors/unknownError' ;
16
+ import { Unauthorized } from './errors/unauthorized' ;
16
17
import {
17
18
GoFeatureFlagProviderOptions ,
18
19
GoFeatureFlagProxyRequest ,
19
20
GoFeatureFlagProxyResponse ,
20
21
GoFeatureFlagUser ,
21
22
} from './model' ;
22
23
24
+
23
25
// GoFeatureFlagProvider is the official Open-feature provider for GO Feature Flag.
24
26
export class GoFeatureFlagProvider implements Provider {
25
27
metadata = {
@@ -30,10 +32,13 @@ export class GoFeatureFlagProvider implements Provider {
30
32
private endpoint : string ;
31
33
// timeout in millisecond before we consider the request as a failure
32
34
private timeout : number ;
35
+ // apiKey contains the token to use while calling GO Feature Flag relay proxy
36
+ private apiKey ?: string ;
33
37
34
38
constructor ( options : GoFeatureFlagProviderOptions ) {
35
39
this . timeout = options . timeout || 0 ; // default is 0 = no timeout
36
40
this . endpoint = options . endpoint ;
41
+ this . apiKey = options . apiKey ;
37
42
}
38
43
39
44
/**
@@ -149,7 +154,7 @@ export class GoFeatureFlagProvider implements Provider {
149
154
* @throws {ProxyTimeout } When the HTTP call is timing out
150
155
* @throws {UnknownError } When an unknown error occurs
151
156
* @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
153
158
*/
154
159
async resolveEvaluationGoFeatureFlagProxy < T > (
155
160
flagKey : string ,
@@ -165,19 +170,24 @@ export class GoFeatureFlagProvider implements Provider {
165
170
166
171
let apiResponseData : GoFeatureFlagProxyResponse < T > ;
167
172
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 ) ;
179
186
apiResponseData = response . data ;
180
187
} 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
+ }
181
191
// Impossible to contact the relay-proxy
182
192
if (
183
193
axios . isAxiosError ( error ) &&
0 commit comments