@hyphen/sdk 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -14
- package/dist/index.cjs +21 -4
- package/dist/index.d.cts +28 -15
- package/dist/index.d.ts +28 -15
- package/dist/index.js +21 -4
- package/package.json +7 -7
package/README.md
CHANGED
|
@@ -17,6 +17,7 @@ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily
|
|
|
17
17
|
- [Toggle API](#toggle-api)
|
|
18
18
|
- [Toggle Hooks](#toggle-hooks)
|
|
19
19
|
- [Toggle Error Handling](#toggle-error-handling)
|
|
20
|
+
- [Toggle Caching](#toggle-caching)
|
|
20
21
|
- [Toggle Self-Hosted](#toggle-self-hosted)
|
|
21
22
|
- [Contributing](#contributing)
|
|
22
23
|
- [Testing Your Changes](#testing-your-changes)
|
|
@@ -167,32 +168,32 @@ console.log('Boolean toggle value:', result); // true
|
|
|
167
168
|
| *applicationId* | `string` | The application ID for your Hyphen project. You can find this in the Hyphen dashboard. |
|
|
168
169
|
| *environment?* | `string` | The environment for your Hyphen project such as `production`. Default uses `process.env.NODE_ENV` |
|
|
169
170
|
| *context?* | `ToggleContext` | The context object that contains the user and custom attributes. This is optional. |
|
|
170
|
-
| *
|
|
171
|
+
| *caching?* | `{ ttl: number, generateCacheKeyFn: (context?: ToggleContext) => string}` | Whether to use the cache or not and a function to set the key. The `ttl` is in seconds |
|
|
171
172
|
|
|
172
173
|
## Toggle API
|
|
173
174
|
|
|
174
175
|
| Method | Parameters | Description |
|
|
175
176
|
|----------------|----------------|----------------|
|
|
176
177
|
| *setContext* | `context: ToggleContext` | Set the context for the toggle. This is optional. |
|
|
177
|
-
| *get<Type>* | `key: string, defaultValue: T, options?:
|
|
178
|
-
| *getBoolean* | `key: string, defaultValue: boolean, options?:
|
|
179
|
-
| *getNumber* | `key: string, defaultValue: number, options?:
|
|
180
|
-
| *getString* | `key: string, defaultValue: string, options?:
|
|
181
|
-
| *getObject<Type>* | `key: string, defaultValue: any, options?:
|
|
178
|
+
| *get<Type>* | `key: string, defaultValue: T, options?: ToggleGetOptions` | Get the value of a toggle. This is a generic method that can be used to get any type from toggle. |
|
|
179
|
+
| *getBoolean* | `key: string, defaultValue: boolean, options?: ToggleGetOptions` | Get the value of a boolean toggle. |
|
|
180
|
+
| *getNumber* | `key: string, defaultValue: number, options?: ToggleGetOptions` | Get the value of a number toggle. |
|
|
181
|
+
| *getString* | `key: string, defaultValue: string, options?: ToggleGetOptions` | Get the value of a string toggle. |
|
|
182
|
+
| *getObject<Type>* | `key: string, defaultValue: any, options?: ToggleGetOptions` | Get the value of a object toggle. |
|
|
182
183
|
|
|
183
184
|
## Toggle Hooks
|
|
184
185
|
|
|
185
186
|
The following hooks are available for Toggle:
|
|
186
187
|
| Hook | object | Description |
|
|
187
188
|
|----------------|----------------|----------------|
|
|
188
|
-
| *beforeGetBoolean* | `{ key: string, defaultValue:boolean, options?:
|
|
189
|
-
| *afterGetBoolean* | `{ key: string, defaultValue:boolean, options?:
|
|
190
|
-
| *beforeGetNumber* | `{ key: string, defaultValue:number, options?:
|
|
191
|
-
| *afterGetNumber* | `{ key: string, defaultValue:number, options?:
|
|
192
|
-
| *beforeGetString* | `{ key: string, defaultValue:string, options?:
|
|
193
|
-
| *afterGetString* | `{ key: string, defaultValue:string, options?:
|
|
194
|
-
| *beforeGetObject* | `{ key: string, defaultValue:any, options?:
|
|
195
|
-
| *afterGetObject* | `{ key: string, defaultValue:any, options?:
|
|
189
|
+
| *beforeGetBoolean* | `{ key: string, defaultValue:boolean, options?: ToggleGetOptions }` | Called before the boolean toggle is fetched. |
|
|
190
|
+
| *afterGetBoolean* | `{ key: string, defaultValue:boolean, options?: ToggleGetOptions, result: boolean }` | Called after the boolean toggle is fetched. |
|
|
191
|
+
| *beforeGetNumber* | `{ key: string, defaultValue:number, options?: ToggleGetOptions }` | Called before the number toggle is fetched. |
|
|
192
|
+
| *afterGetNumber* | `{ key: string, defaultValue:number, options?: ToggleGetOptions, result: number }` | Called after the number toggle is fetched. |
|
|
193
|
+
| *beforeGetString* | `{ key: string, defaultValue:string, options?: ToggleGetOptions }` | Called before the string toggle is fetched. |
|
|
194
|
+
| *afterGetString* | `{ key: string, defaultValue:string, options?: ToggleGetOptions, result: string }` | Called after the string toggle is fetched. |
|
|
195
|
+
| *beforeGetObject* | `{ key: string, defaultValue:any, options?: ToggleGetOptions }` | Called before the object toggle is fetched. |
|
|
196
|
+
| *afterGetObject* | `{ key: string, defaultValue:any, options?: ToggleGetOptions, result: any }` | Called after the object toggle is fetched. |
|
|
196
197
|
|
|
197
198
|
You can use the hooks to modify the request or the response. For example, you can use the `beforeGetBoolean` hook to log the request before it is sent to the server.
|
|
198
199
|
|
|
@@ -311,6 +312,89 @@ try {
|
|
|
311
312
|
}
|
|
312
313
|
```
|
|
313
314
|
|
|
315
|
+
## Toggle Caching
|
|
316
|
+
The SDK supports caching of toggle values to improve performance and reduce the number of requests made to the server. You can enable caching by providing a `caching` option in the constructor.
|
|
317
|
+
|
|
318
|
+
```javascript
|
|
319
|
+
import { Toggle, ToggleContext } from '@hyphen/sdk';
|
|
320
|
+
|
|
321
|
+
const context: ToggleContext = {
|
|
322
|
+
targetingKey: 'user-123',
|
|
323
|
+
ipAddress: '203.0.113.42',
|
|
324
|
+
customAttributes: {
|
|
325
|
+
subscriptionLevel: 'premium',
|
|
326
|
+
region: 'us-east',
|
|
327
|
+
},
|
|
328
|
+
user: {
|
|
329
|
+
id: 'user-123',
|
|
330
|
+
email: 'john.doe@example.com',
|
|
331
|
+
name: 'John Doe',
|
|
332
|
+
customAttributes: {
|
|
333
|
+
role: 'admin',
|
|
334
|
+
},
|
|
335
|
+
},
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
const toggleOptions = {
|
|
339
|
+
publicApiKey: 'your_public_api_key',
|
|
340
|
+
applicationId: 'your_application_id',
|
|
341
|
+
context: context,
|
|
342
|
+
uris: [
|
|
343
|
+
'https://your-self-hosted-horizon-url',
|
|
344
|
+
],
|
|
345
|
+
caching: {
|
|
346
|
+
ttl: 60, // Cache for 60 seconds
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
const toggle = new Toggle(toggleOptions);
|
|
350
|
+
|
|
351
|
+
const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
|
|
352
|
+
console.log('Boolean toggle value:', result); // true
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
If you want to use a custom cache key generation function, you can provide a `generateCacheKeyFn` function in the `caching` option:
|
|
356
|
+
|
|
357
|
+
```javascript
|
|
358
|
+
import { Toggle, ToggleContext } from '@hyphen/sdk';
|
|
359
|
+
|
|
360
|
+
const context: ToggleContext = {
|
|
361
|
+
targetingKey: 'user-123',
|
|
362
|
+
ipAddress: '203.0.113.42',
|
|
363
|
+
customAttributes: {
|
|
364
|
+
subscriptionLevel: 'premium',
|
|
365
|
+
region: 'us-east',
|
|
366
|
+
},
|
|
367
|
+
user: {
|
|
368
|
+
id: 'user-123',
|
|
369
|
+
email: 'john.doe@example.com',
|
|
370
|
+
name: 'John Doe',
|
|
371
|
+
customAttributes: {
|
|
372
|
+
role: 'admin',
|
|
373
|
+
},
|
|
374
|
+
},
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
const toggleOptions = {
|
|
378
|
+
publicApiKey: 'your_public_api_key',
|
|
379
|
+
applicationId: 'your_application_id',
|
|
380
|
+
context: context,
|
|
381
|
+
uris: [
|
|
382
|
+
'https://your-self-hosted-horizon-url',
|
|
383
|
+
],
|
|
384
|
+
caching: {
|
|
385
|
+
ttl: 60, // Cache for 60 seconds
|
|
386
|
+
generateCacheKeyFn: (context) => {
|
|
387
|
+
return `toggle-${context?.targetingKey || 'default'}-hyphen-sdk-boolean`;
|
|
388
|
+
},
|
|
389
|
+
},
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
const toggle = new Toggle(toggleOptions);
|
|
393
|
+
|
|
394
|
+
const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
|
|
395
|
+
console.log('Boolean toggle value:', result); // true
|
|
396
|
+
```
|
|
397
|
+
|
|
314
398
|
## Toggle Self-Hosted
|
|
315
399
|
|
|
316
400
|
Toggle uses [Horizon](https://hyphen.ai/horizon) to fetch the feature flags. If you are using a self-hosted version of Hyphen you can use the `uris` option in the constructor to set the url of your self-hosted version:
|
package/dist/index.cjs
CHANGED
|
@@ -63,6 +63,7 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
63
63
|
_context;
|
|
64
64
|
_throwErrors = false;
|
|
65
65
|
_uris;
|
|
66
|
+
_caching;
|
|
66
67
|
/*
|
|
67
68
|
* Create a new Toggle instance. This will create a new client and set the options.
|
|
68
69
|
* @param {ToggleOptions}
|
|
@@ -75,6 +76,7 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
75
76
|
this._context = options.context;
|
|
76
77
|
this._throwErrors = options.throwErrors ?? false;
|
|
77
78
|
this._uris = options.uris;
|
|
79
|
+
this._caching = options.caching;
|
|
78
80
|
}
|
|
79
81
|
/**
|
|
80
82
|
* Get the application ID
|
|
@@ -161,11 +163,25 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
161
163
|
this._uris = value;
|
|
162
164
|
}
|
|
163
165
|
/**
|
|
166
|
+
* Get the caching options.
|
|
167
|
+
* @returns {ToggleCachingOptions | undefined}
|
|
168
|
+
*/
|
|
169
|
+
get caching() {
|
|
170
|
+
return this._caching;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Set the caching options.
|
|
174
|
+
* @param {ToggleCachingOptions | undefined} value
|
|
175
|
+
*/
|
|
176
|
+
set caching(value) {
|
|
177
|
+
this._caching = value;
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
164
180
|
* This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
|
|
165
|
-
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
181
|
+
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
166
182
|
* it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
|
|
167
|
-
* @param key
|
|
168
|
-
* @returns
|
|
183
|
+
* @param key
|
|
184
|
+
* @returns
|
|
169
185
|
*/
|
|
170
186
|
setPublicApiKey(key) {
|
|
171
187
|
if (!key.startsWith("public_")) {
|
|
@@ -197,7 +213,8 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
197
213
|
const options = {
|
|
198
214
|
application: this._applicationId,
|
|
199
215
|
environment: this._environment,
|
|
200
|
-
horizonUrls: this._uris
|
|
216
|
+
horizonUrls: this._uris,
|
|
217
|
+
cache: this._caching
|
|
201
218
|
};
|
|
202
219
|
await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this._publicApiKey, options));
|
|
203
220
|
this._client = import_server_sdk.OpenFeature.getClient(this._context);
|
package/dist/index.d.cts
CHANGED
|
@@ -12,6 +12,10 @@ declare enum ToggleHooks {
|
|
|
12
12
|
beforeGetObject = "beforeGetObject",
|
|
13
13
|
afterGetObject = "afterGetObject"
|
|
14
14
|
}
|
|
15
|
+
type ToggleCachingOptions = {
|
|
16
|
+
ttl?: number;
|
|
17
|
+
generateCacheKeyFn?: (context?: ToggleContext) => string;
|
|
18
|
+
};
|
|
15
19
|
type ToggleOptions = {
|
|
16
20
|
/**
|
|
17
21
|
* Your application name
|
|
@@ -34,13 +38,11 @@ type ToggleOptions = {
|
|
|
34
38
|
* @type {ToggleContext}
|
|
35
39
|
*/
|
|
36
40
|
context?: ToggleContext;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
ttl?: number;
|
|
43
|
-
};
|
|
41
|
+
/**
|
|
42
|
+
* Cache options to use for the request
|
|
43
|
+
* @type {ToggleCachingOptions}
|
|
44
|
+
*/
|
|
45
|
+
caching?: ToggleCachingOptions;
|
|
44
46
|
/**
|
|
45
47
|
* Throw errors in addition to emitting them
|
|
46
48
|
* @type {boolean}
|
|
@@ -55,7 +57,7 @@ type ToggleOptions = {
|
|
|
55
57
|
*/
|
|
56
58
|
uris?: string[];
|
|
57
59
|
};
|
|
58
|
-
type
|
|
60
|
+
type ToggleGetOptions = {
|
|
59
61
|
/**
|
|
60
62
|
* The context to use for evaluating feature flags
|
|
61
63
|
* @type {ToggleContext}
|
|
@@ -69,7 +71,8 @@ declare class Toggle extends Hookified {
|
|
|
69
71
|
private _client;
|
|
70
72
|
private _context;
|
|
71
73
|
private _throwErrors;
|
|
72
|
-
private _uris
|
|
74
|
+
private _uris;
|
|
75
|
+
private _caching;
|
|
73
76
|
constructor(options: ToggleOptions);
|
|
74
77
|
/**
|
|
75
78
|
* Get the application ID
|
|
@@ -131,6 +134,16 @@ declare class Toggle extends Hookified {
|
|
|
131
134
|
* @param {Array<string>} value
|
|
132
135
|
*/
|
|
133
136
|
set uris(value: string[] | undefined);
|
|
137
|
+
/**
|
|
138
|
+
* Get the caching options.
|
|
139
|
+
* @returns {ToggleCachingOptions | undefined}
|
|
140
|
+
*/
|
|
141
|
+
get caching(): ToggleCachingOptions | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* Set the caching options.
|
|
144
|
+
* @param {ToggleCachingOptions | undefined} value
|
|
145
|
+
*/
|
|
146
|
+
set caching(value: ToggleCachingOptions | undefined);
|
|
134
147
|
/**
|
|
135
148
|
* This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
|
|
136
149
|
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
@@ -159,7 +172,7 @@ declare class Toggle extends Hookified {
|
|
|
159
172
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
160
173
|
* @returns {Promise<T>}
|
|
161
174
|
*/
|
|
162
|
-
get<T>(key: string, defaultValue: T, options?:
|
|
175
|
+
get<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
|
|
163
176
|
/**
|
|
164
177
|
* Get a boolean value from the feature flag. This will check the type of the default value and call the
|
|
165
178
|
* appropriate function. It will also set the context if it is not set.
|
|
@@ -168,7 +181,7 @@ declare class Toggle extends Hookified {
|
|
|
168
181
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
169
182
|
* @returns {Promise<boolean>} - The value of the feature flag
|
|
170
183
|
*/
|
|
171
|
-
getBoolean(key: string, defaultValue: boolean, options?:
|
|
184
|
+
getBoolean(key: string, defaultValue: boolean, options?: ToggleGetOptions): Promise<boolean>;
|
|
172
185
|
/**
|
|
173
186
|
* Get a string value from the feature flag.
|
|
174
187
|
* @param {string} key - The key of the feature flag
|
|
@@ -176,8 +189,8 @@ declare class Toggle extends Hookified {
|
|
|
176
189
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
177
190
|
* @returns {Promise<string>} - The value of the feature flag
|
|
178
191
|
*/
|
|
179
|
-
getString(key: string, defaultValue: string, options?:
|
|
180
|
-
getNumber(key: string, defaultValue: number, options?:
|
|
192
|
+
getString(key: string, defaultValue: string, options?: ToggleGetOptions): Promise<string>;
|
|
193
|
+
getNumber(key: string, defaultValue: number, options?: ToggleGetOptions): Promise<number>;
|
|
181
194
|
/**
|
|
182
195
|
* Get an object value from the feature flag. This will check the type of the default value and call the
|
|
183
196
|
* appropriate function. It will also set the context if it is not set.
|
|
@@ -186,7 +199,7 @@ declare class Toggle extends Hookified {
|
|
|
186
199
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
187
200
|
* @returns {Promise<T>} - The value of the feature flag
|
|
188
201
|
*/
|
|
189
|
-
getObject<T>(key: string, defaultValue: T, options?:
|
|
202
|
+
getObject<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
|
|
190
203
|
}
|
|
191
204
|
|
|
192
|
-
export { Toggle, type
|
|
205
|
+
export { Toggle, type ToggleCachingOptions, type ToggleContext, type ToggleGetOptions, ToggleHooks, type ToggleOptions };
|
package/dist/index.d.ts
CHANGED
|
@@ -12,6 +12,10 @@ declare enum ToggleHooks {
|
|
|
12
12
|
beforeGetObject = "beforeGetObject",
|
|
13
13
|
afterGetObject = "afterGetObject"
|
|
14
14
|
}
|
|
15
|
+
type ToggleCachingOptions = {
|
|
16
|
+
ttl?: number;
|
|
17
|
+
generateCacheKeyFn?: (context?: ToggleContext) => string;
|
|
18
|
+
};
|
|
15
19
|
type ToggleOptions = {
|
|
16
20
|
/**
|
|
17
21
|
* Your application name
|
|
@@ -34,13 +38,11 @@ type ToggleOptions = {
|
|
|
34
38
|
* @type {ToggleContext}
|
|
35
39
|
*/
|
|
36
40
|
context?: ToggleContext;
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
ttl?: number;
|
|
43
|
-
};
|
|
41
|
+
/**
|
|
42
|
+
* Cache options to use for the request
|
|
43
|
+
* @type {ToggleCachingOptions}
|
|
44
|
+
*/
|
|
45
|
+
caching?: ToggleCachingOptions;
|
|
44
46
|
/**
|
|
45
47
|
* Throw errors in addition to emitting them
|
|
46
48
|
* @type {boolean}
|
|
@@ -55,7 +57,7 @@ type ToggleOptions = {
|
|
|
55
57
|
*/
|
|
56
58
|
uris?: string[];
|
|
57
59
|
};
|
|
58
|
-
type
|
|
60
|
+
type ToggleGetOptions = {
|
|
59
61
|
/**
|
|
60
62
|
* The context to use for evaluating feature flags
|
|
61
63
|
* @type {ToggleContext}
|
|
@@ -69,7 +71,8 @@ declare class Toggle extends Hookified {
|
|
|
69
71
|
private _client;
|
|
70
72
|
private _context;
|
|
71
73
|
private _throwErrors;
|
|
72
|
-
private _uris
|
|
74
|
+
private _uris;
|
|
75
|
+
private _caching;
|
|
73
76
|
constructor(options: ToggleOptions);
|
|
74
77
|
/**
|
|
75
78
|
* Get the application ID
|
|
@@ -131,6 +134,16 @@ declare class Toggle extends Hookified {
|
|
|
131
134
|
* @param {Array<string>} value
|
|
132
135
|
*/
|
|
133
136
|
set uris(value: string[] | undefined);
|
|
137
|
+
/**
|
|
138
|
+
* Get the caching options.
|
|
139
|
+
* @returns {ToggleCachingOptions | undefined}
|
|
140
|
+
*/
|
|
141
|
+
get caching(): ToggleCachingOptions | undefined;
|
|
142
|
+
/**
|
|
143
|
+
* Set the caching options.
|
|
144
|
+
* @param {ToggleCachingOptions | undefined} value
|
|
145
|
+
*/
|
|
146
|
+
set caching(value: ToggleCachingOptions | undefined);
|
|
134
147
|
/**
|
|
135
148
|
* This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
|
|
136
149
|
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
@@ -159,7 +172,7 @@ declare class Toggle extends Hookified {
|
|
|
159
172
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
160
173
|
* @returns {Promise<T>}
|
|
161
174
|
*/
|
|
162
|
-
get<T>(key: string, defaultValue: T, options?:
|
|
175
|
+
get<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
|
|
163
176
|
/**
|
|
164
177
|
* Get a boolean value from the feature flag. This will check the type of the default value and call the
|
|
165
178
|
* appropriate function. It will also set the context if it is not set.
|
|
@@ -168,7 +181,7 @@ declare class Toggle extends Hookified {
|
|
|
168
181
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
169
182
|
* @returns {Promise<boolean>} - The value of the feature flag
|
|
170
183
|
*/
|
|
171
|
-
getBoolean(key: string, defaultValue: boolean, options?:
|
|
184
|
+
getBoolean(key: string, defaultValue: boolean, options?: ToggleGetOptions): Promise<boolean>;
|
|
172
185
|
/**
|
|
173
186
|
* Get a string value from the feature flag.
|
|
174
187
|
* @param {string} key - The key of the feature flag
|
|
@@ -176,8 +189,8 @@ declare class Toggle extends Hookified {
|
|
|
176
189
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
177
190
|
* @returns {Promise<string>} - The value of the feature flag
|
|
178
191
|
*/
|
|
179
|
-
getString(key: string, defaultValue: string, options?:
|
|
180
|
-
getNumber(key: string, defaultValue: number, options?:
|
|
192
|
+
getString(key: string, defaultValue: string, options?: ToggleGetOptions): Promise<string>;
|
|
193
|
+
getNumber(key: string, defaultValue: number, options?: ToggleGetOptions): Promise<number>;
|
|
181
194
|
/**
|
|
182
195
|
* Get an object value from the feature flag. This will check the type of the default value and call the
|
|
183
196
|
* appropriate function. It will also set the context if it is not set.
|
|
@@ -186,7 +199,7 @@ declare class Toggle extends Hookified {
|
|
|
186
199
|
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
187
200
|
* @returns {Promise<T>} - The value of the feature flag
|
|
188
201
|
*/
|
|
189
|
-
getObject<T>(key: string, defaultValue: T, options?:
|
|
202
|
+
getObject<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
|
|
190
203
|
}
|
|
191
204
|
|
|
192
|
-
export { Toggle, type
|
|
205
|
+
export { Toggle, type ToggleCachingOptions, type ToggleContext, type ToggleGetOptions, ToggleHooks, type ToggleOptions };
|
package/dist/index.js
CHANGED
|
@@ -28,6 +28,7 @@ var Toggle = class extends Hookified {
|
|
|
28
28
|
_context;
|
|
29
29
|
_throwErrors = false;
|
|
30
30
|
_uris;
|
|
31
|
+
_caching;
|
|
31
32
|
/*
|
|
32
33
|
* Create a new Toggle instance. This will create a new client and set the options.
|
|
33
34
|
* @param {ToggleOptions}
|
|
@@ -40,6 +41,7 @@ var Toggle = class extends Hookified {
|
|
|
40
41
|
this._context = options.context;
|
|
41
42
|
this._throwErrors = options.throwErrors ?? false;
|
|
42
43
|
this._uris = options.uris;
|
|
44
|
+
this._caching = options.caching;
|
|
43
45
|
}
|
|
44
46
|
/**
|
|
45
47
|
* Get the application ID
|
|
@@ -126,11 +128,25 @@ var Toggle = class extends Hookified {
|
|
|
126
128
|
this._uris = value;
|
|
127
129
|
}
|
|
128
130
|
/**
|
|
131
|
+
* Get the caching options.
|
|
132
|
+
* @returns {ToggleCachingOptions | undefined}
|
|
133
|
+
*/
|
|
134
|
+
get caching() {
|
|
135
|
+
return this._caching;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Set the caching options.
|
|
139
|
+
* @param {ToggleCachingOptions | undefined} value
|
|
140
|
+
*/
|
|
141
|
+
set caching(value) {
|
|
142
|
+
this._caching = value;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
129
145
|
* This is a helper function to set the public API key. It will check if the key starts with public_ and set it. If it
|
|
130
|
-
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
146
|
+
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
131
147
|
* it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
|
|
132
|
-
* @param key
|
|
133
|
-
* @returns
|
|
148
|
+
* @param key
|
|
149
|
+
* @returns
|
|
134
150
|
*/
|
|
135
151
|
setPublicApiKey(key) {
|
|
136
152
|
if (!key.startsWith("public_")) {
|
|
@@ -162,7 +178,8 @@ var Toggle = class extends Hookified {
|
|
|
162
178
|
const options = {
|
|
163
179
|
application: this._applicationId,
|
|
164
180
|
environment: this._environment,
|
|
165
|
-
horizonUrls: this._uris
|
|
181
|
+
horizonUrls: this._uris,
|
|
182
|
+
cache: this._caching
|
|
166
183
|
};
|
|
167
184
|
await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicApiKey, options));
|
|
168
185
|
this._client = OpenFeature.getClient(this._context);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyphen/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Hyphen SDK for Node.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -29,15 +29,15 @@
|
|
|
29
29
|
"author": "Team Hyphen <hello@hyphen.ai>",
|
|
30
30
|
"license": "MIT",
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@swc/core": "^1.11.
|
|
33
|
-
"@types/node": "^22.15.
|
|
34
|
-
"@vitest/coverage-v8": "^3.1.
|
|
32
|
+
"@swc/core": "^1.11.29",
|
|
33
|
+
"@types/node": "^22.15.21",
|
|
34
|
+
"@vitest/coverage-v8": "^3.1.4",
|
|
35
35
|
"rimraf": "^6.0.1",
|
|
36
36
|
"tsd": "^0.32.0",
|
|
37
|
-
"tsup": "^8.
|
|
37
|
+
"tsup": "^8.5.0",
|
|
38
38
|
"typescript": "^5.8.3",
|
|
39
|
-
"vitest": "^3.1.
|
|
40
|
-
"xo": "^0.
|
|
39
|
+
"vitest": "^3.1.4",
|
|
40
|
+
"xo": "^1.0.0"
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"dist",
|