@@ -31,6 +31,15 @@ export interface SchematicOptions {
3131 logger ?: Logger ;
3232 /** Enable offline mode to prevent network activity */
3333 offline ?: boolean ;
34+ /** The default maximum time to wait for a response in milliseconds */
35+ timeoutMs ?: number ;
36+ }
37+
38+ export interface CheckFlagOptions {
39+ /** Default value to return on error. Can be a boolean or a function returning a boolean. If not provided, uses configured flag defaults */
40+ defaultValue ?: boolean | ( ( ) => boolean ) ;
41+ /** The maximum time to wait for a response in milliseconds */
42+ timeoutMs ?: number ;
3443}
3544
3645export class SchematicClient extends BaseClient {
@@ -51,6 +60,7 @@ export class SchematicClient extends BaseClient {
5160 eventBufferInterval,
5261 flagDefaults = { } ,
5362 logger = new ConsoleLogger ( ) ,
63+ timeoutMs,
5464 } = opts ?? { } ;
5565 let { offline = false } = opts ?? { } ;
5666
@@ -78,6 +88,7 @@ export class SchematicClient extends BaseClient {
7888 apiKey,
7989 environment : basePath ,
8090 fetcher : offline ? offlineFetcher : provideFetcher ( headers ) ,
91+ timeoutInSeconds : timeoutMs !== undefined ? timeoutMs / 1000 : undefined ,
8192 } ) ;
8293
8394 this . logger = logger ;
@@ -95,13 +106,21 @@ export class SchematicClient extends BaseClient {
95106 * Checks the value of a feature flag for the given evaluation context
96107 * @param evalCtx - The context (company and/or user) for evaluating the feature flag
97108 * @param key - The unique identifier of the feature flag
109+ * @param options - Optional configuration for the flag check
98110 * @returns Promise resolving to the flag's boolean value, falling back to default if unavailable
99111 * @throws Will log error and return flag default if check fails
100112 */
101- async checkFlag ( evalCtx : api . CheckFlagRequestBody , key : string ) : Promise < boolean > {
113+ async checkFlag ( evalCtx : api . CheckFlagRequestBody , key : string , options ?: CheckFlagOptions ) : Promise < boolean > {
114+ const getDefault = ( ) : boolean => {
115+ if ( options ?. defaultValue === undefined ) {
116+ return this . getFlagDefault ( key ) ;
117+ }
118+ return typeof options . defaultValue === "function" ? options . defaultValue ( ) : options . defaultValue ;
119+ } ;
120+
102121 if ( this . offline ) {
103122 this . logger . debug ( `Offline mode enabled, returning default flag value for flag ${ key } ` ) ;
104- return this . getFlagDefault ( key ) ;
123+ return getDefault ( ) ;
105124 }
106125
107126 try {
@@ -114,10 +133,12 @@ export class SchematicClient extends BaseClient {
114133 }
115134 }
116135
117- const response = await this . features . checkFlag ( key , evalCtx ) ;
136+ const response = await this . features . checkFlag ( key , evalCtx , {
137+ timeoutInSeconds : options ?. timeoutMs !== undefined ? options . timeoutMs / 1000 : undefined ,
138+ } ) ;
118139 if ( response . data . value === undefined ) {
119140 this . logger . debug ( `No value returned from feature flag API for flag ${ key } , falling back to default` ) ;
120- return this . getFlagDefault ( key ) ;
141+ return getDefault ( ) ;
121142 }
122143
123144 for ( const provider of this . flagCheckCacheProviders ) {
@@ -129,7 +150,7 @@ export class SchematicClient extends BaseClient {
129150 return response . data . value ;
130151 } catch ( err ) {
131152 this . logger . error ( `Error checking flag ${ key } : ${ err } ` ) ;
132- return this . getFlagDefault ( key ) ;
153+ return getDefault ( ) ;
133154 }
134155 }
135156
0 commit comments