6
6
Flag ,
7
7
FormatEnum ,
8
8
PrecomputedFlag ,
9
+ PrecomputedFlagsPayload ,
9
10
} from './interfaces' ;
11
+ import { Attributes } from './types' ;
10
12
11
13
export interface IQueryParams {
12
14
apiKey : string ;
@@ -16,7 +18,7 @@ export interface IQueryParams {
16
18
17
19
export interface IQueryParamsWithSubject extends IQueryParams {
18
20
subjectKey : string ;
19
- subjectAttributes : Record < string , any > ;
21
+ subjectAttributes : Attributes ;
20
22
}
21
23
22
24
export class HttpRequestError extends Error {
@@ -50,8 +52,11 @@ export interface IPrecomputedFlagsResponse {
50
52
export interface IHttpClient {
51
53
getUniversalFlagConfiguration ( ) : Promise < IUniversalFlagConfigResponse | undefined > ;
52
54
getBanditParameters ( ) : Promise < IBanditParametersResponse | undefined > ;
53
- getPrecomputedFlags ( ) : Promise < IPrecomputedFlagsResponse | undefined > ;
55
+ getPrecomputedFlags (
56
+ payload : PrecomputedFlagsPayload ,
57
+ ) : Promise < IPrecomputedFlagsResponse | undefined > ;
54
58
rawGet < T > ( url : URL ) : Promise < T | undefined > ;
59
+ rawPost < T , P > ( url : URL , payload : P ) : Promise < T | undefined > ;
55
60
}
56
61
57
62
export default class FetchHttpClient implements IHttpClient {
@@ -67,9 +72,11 @@ export default class FetchHttpClient implements IHttpClient {
67
72
return await this . rawGet < IBanditParametersResponse > ( url ) ;
68
73
}
69
74
70
- async getPrecomputedFlags ( ) : Promise < IPrecomputedFlagsResponse | undefined > {
75
+ async getPrecomputedFlags (
76
+ payload : PrecomputedFlagsPayload ,
77
+ ) : Promise < IPrecomputedFlagsResponse | undefined > {
71
78
const url = this . apiEndpoints . precomputedFlagsEndpoint ( ) ;
72
- return await this . rawGet < IPrecomputedFlagsResponse > ( url ) ;
79
+ return await this . rawPost < IPrecomputedFlagsResponse , PrecomputedFlagsPayload > ( url , payload ) ;
73
80
}
74
81
75
82
async rawGet < T > ( url : URL ) : Promise < T | undefined > {
@@ -97,4 +104,37 @@ export default class FetchHttpClient implements IHttpClient {
97
104
throw new HttpRequestError ( 'Network error' , 0 , error ) ;
98
105
}
99
106
}
107
+
108
+ async rawPost < T , P > ( url : URL , payload : P ) : Promise < T | undefined > {
109
+ try {
110
+ const controller = new AbortController ( ) ;
111
+ const signal = controller . signal ;
112
+ const timeoutId = setTimeout ( ( ) => controller . abort ( ) , this . timeout ) ;
113
+
114
+ const response = await fetch ( url . toString ( ) , {
115
+ method : 'POST' ,
116
+ headers : {
117
+ 'Content-Type' : 'application/json' ,
118
+ } ,
119
+ body : JSON . stringify ( payload ) ,
120
+ signal,
121
+ } ) ;
122
+
123
+ clearTimeout ( timeoutId ) ;
124
+
125
+ if ( ! response ?. ok ) {
126
+ const errorBody = await response . text ( ) ;
127
+ throw new HttpRequestError ( errorBody || 'Failed to post data' , response ?. status ) ;
128
+ }
129
+ return await response . json ( ) ;
130
+ } catch ( error : any ) {
131
+ if ( error . name === 'AbortError' ) {
132
+ throw new HttpRequestError ( 'Request timed out' , 408 , error ) ;
133
+ } else if ( error instanceof HttpRequestError ) {
134
+ throw error ;
135
+ }
136
+
137
+ throw new HttpRequestError ( 'Network error' , 0 , error ) ;
138
+ }
139
+ }
100
140
}
0 commit comments