@hyphen/sdk 1.0.3 → 1.1.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 CHANGED
@@ -1,4 +1,4 @@
1
- ![Hyphen AI](https://github.com/Hyphen/nodejs-sdk/raw/main/logo.svg)
1
+ ![Hyphen AI](https://github.com/Hyphen/nodejs-sdk/raw/main/logo.png)
2
2
 
3
3
  [![tests](https://github.com/Hyphen/nodejs-sdk/actions/workflows/tests.yaml/badge.svg)](https://github.com/Hyphen/nodejs-sdk/actions/workflows/tests.yaml)
4
4
  [![npm](https://img.shields.io/npm/v/@hyphen/sdk)](https://www.npmjs.com/package/@hyphen/sdk)
@@ -18,8 +18,9 @@ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily
18
18
  - [Toggle API](#toggle-api)
19
19
  - [Toggle Hooks](#toggle-hooks)
20
20
  - [Toggle Error Handling](#toggle-error-handling)
21
+ - [Toggle Self-Hosted](#toggle-self-hosted)
21
22
  - [Contributing](#contributing)
22
- - [Testing Your Changes](#testing-your-changes)
23
+ - [Testing Your Changes](#testing-your-changes)
23
24
  - [License and Copyright](#license-and-copyright)
24
25
 
25
26
  # Installation
@@ -311,10 +312,100 @@ try {
311
312
  }
312
313
  ```
313
314
 
315
+ ## Toggle Self-Hosted
316
+
317
+ 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:
318
+
319
+ ```javascript
320
+ import { Toggle, ToggleContext } from '@hyphen/sdk';
321
+
322
+ const context: ToggleContext = {
323
+ targetingKey: 'user-123',
324
+ ipAddress: '203.0.113.42',
325
+ customAttributes: {
326
+ subscriptionLevel: 'premium',
327
+ region: 'us-east',
328
+ },
329
+ user: {
330
+ id: 'user-123',
331
+ email: 'john.doe@example.com',
332
+ name: 'John Doe',
333
+ customAttributes: {
334
+ role: 'admin',
335
+ },
336
+ },
337
+ };
338
+
339
+ const toggleOptions = {
340
+ publicApiKey: 'your_public_api_key',
341
+ applicationId: 'your_application_id',
342
+ context: context,
343
+ uris: [
344
+ 'https://your-self-hosted-horizon-url',
345
+ ],
346
+ };
347
+
348
+ const toggle = new Toggle(toggleOptions);
349
+
350
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
351
+ console.log('Boolean toggle value:', result); // true
352
+ ```
353
+
354
+ If you want to use your self-hosted version of [Horizon](https://hyphen.ai/horizon) as a primary and fallback to our hosted version you can do it like this:
355
+
356
+ ```javascript
357
+ import { Toggle, ToggleContext } from '@hyphen/sdk';
358
+
359
+ const context: ToggleContext = {
360
+ targetingKey: 'user-123',
361
+ ipAddress: '203.0.113.42',
362
+ customAttributes: {
363
+ subscriptionLevel: 'premium',
364
+ region: 'us-east',
365
+ },
366
+ user: {
367
+ id: 'user-123',
368
+ email: 'john.doe@example.com',
369
+ name: 'John Doe',
370
+ customAttributes: {
371
+ role: 'admin',
372
+ },
373
+ },
374
+ };
375
+
376
+ const toggleOptions = {
377
+ publicApiKey: 'your_public_api_key',
378
+ applicationId: 'your_application_id',
379
+ context: context,
380
+ uris: [
381
+ 'https://your-self-hosted-horizon-url',
382
+ 'https://toggle.hyphen.cloud',
383
+ ],
384
+ };
385
+
386
+ const toggle = new Toggle(toggleOptions);
387
+
388
+ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
389
+ console.log('Boolean toggle value:', result); // true
390
+ ```
391
+
314
392
  # Contributing
315
393
 
316
394
  We welcome contributions to the Hyphen Node.js SDK! If you have an idea for a new feature, bug fix, or improvement, please follow these steps:
317
395
 
396
+ 1. Fork the repository.
397
+ 2. Create a new branch for your feature or bug fix.
398
+ 3. Install `pnpm` by running `npm install -g pnpm` if you don't have it already.
399
+ 3. Run the installation and tests to ensure everything is working correctly `pnpm i && pnpm test`.
400
+ 4. Make your changes and commit them with a clear message. In the following format:
401
+ - `feat: <describe the feature>`
402
+ - `fix: <describe the bug fix>`
403
+ - `chore: updgrading xxx to version x.x.x`
404
+
405
+ 5. Push your changes to your forked repository.
406
+ 6. Create a pull request to the main repository.
407
+ 7. Describe your changes and why they are necessary.
408
+
318
409
  ## Testing Your Changes
319
410
 
320
411
  To test your changes locally you will need to create an account on [Hyphen](https://hyphen.ai) and do the following:
package/dist/index.cjs CHANGED
@@ -62,6 +62,7 @@ var Toggle = class extends import_hookified.Hookified {
62
62
  _client;
63
63
  _context;
64
64
  _throwErrors = false;
65
+ _uris;
65
66
  constructor(options) {
66
67
  super();
67
68
  this._applicationId = options.applicationId;
@@ -69,6 +70,7 @@ var Toggle = class extends import_hookified.Hookified {
69
70
  this._environment = options.environment ?? import_node_process.default.env.NODE_ENV ?? "development";
70
71
  this._context = options.context;
71
72
  this._throwErrors = options.throwErrors ?? false;
73
+ this._uris = options.uris;
72
74
  }
73
75
  get applicationId() {
74
76
  return this._applicationId;
@@ -100,6 +102,12 @@ var Toggle = class extends import_hookified.Hookified {
100
102
  set context(value) {
101
103
  this._context = value;
102
104
  }
105
+ get uris() {
106
+ return this._uris;
107
+ }
108
+ set uris(value) {
109
+ this._uris = value;
110
+ }
103
111
  setContext(context) {
104
112
  this._context = context;
105
113
  this._client = void 0;
@@ -108,7 +116,8 @@ var Toggle = class extends import_hookified.Hookified {
108
116
  if (!this._client) {
109
117
  const options = {
110
118
  application: this._applicationId,
111
- environment: this._environment
119
+ environment: this._environment,
120
+ horizonUrls: this._uris
112
121
  };
113
122
  await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this._publicKey, options));
114
123
  this._client = import_server_sdk.OpenFeature.getClient(this._context);
package/dist/index.d.cts CHANGED
@@ -47,6 +47,13 @@ type ToggleOptions = {
47
47
  * @default false
48
48
  */
49
49
  throwErrors?: boolean;
50
+ /**
51
+ * Horizon URIs to use for talking to Toggle. You can use this to override
52
+ * the default URIs for testin or if you are using a self-hosted version.
53
+ * @type {Array<string>}
54
+ * @example ['https://toggle.hyphen.ai']
55
+ */
56
+ uris?: string[];
50
57
  };
51
58
  type ToggleRequestOptions = {
52
59
  context?: ToggleContext;
@@ -58,6 +65,7 @@ declare class Toggle extends Hookified {
58
65
  private _client;
59
66
  private _context;
60
67
  private _throwErrors;
68
+ private _uris?;
61
69
  constructor(options: ToggleOptions);
62
70
  get applicationId(): string;
63
71
  set applicationId(value: string);
@@ -69,6 +77,8 @@ declare class Toggle extends Hookified {
69
77
  set throwErrors(value: boolean);
70
78
  get context(): ToggleContext | undefined;
71
79
  set context(value: ToggleContext | undefined);
80
+ get uris(): string[] | undefined;
81
+ set uris(value: string[] | undefined);
72
82
  setContext(context: ToggleContext): void;
73
83
  getClient(): Promise<Client>;
74
84
  get<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
package/dist/index.d.ts CHANGED
@@ -47,6 +47,13 @@ type ToggleOptions = {
47
47
  * @default false
48
48
  */
49
49
  throwErrors?: boolean;
50
+ /**
51
+ * Horizon URIs to use for talking to Toggle. You can use this to override
52
+ * the default URIs for testin or if you are using a self-hosted version.
53
+ * @type {Array<string>}
54
+ * @example ['https://toggle.hyphen.ai']
55
+ */
56
+ uris?: string[];
50
57
  };
51
58
  type ToggleRequestOptions = {
52
59
  context?: ToggleContext;
@@ -58,6 +65,7 @@ declare class Toggle extends Hookified {
58
65
  private _client;
59
66
  private _context;
60
67
  private _throwErrors;
68
+ private _uris?;
61
69
  constructor(options: ToggleOptions);
62
70
  get applicationId(): string;
63
71
  set applicationId(value: string);
@@ -69,6 +77,8 @@ declare class Toggle extends Hookified {
69
77
  set throwErrors(value: boolean);
70
78
  get context(): ToggleContext | undefined;
71
79
  set context(value: ToggleContext | undefined);
80
+ get uris(): string[] | undefined;
81
+ set uris(value: string[] | undefined);
72
82
  setContext(context: ToggleContext): void;
73
83
  getClient(): Promise<Client>;
74
84
  get<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
package/dist/index.js CHANGED
@@ -27,6 +27,7 @@ var Toggle = class extends Hookified {
27
27
  _client;
28
28
  _context;
29
29
  _throwErrors = false;
30
+ _uris;
30
31
  constructor(options) {
31
32
  super();
32
33
  this._applicationId = options.applicationId;
@@ -34,6 +35,7 @@ var Toggle = class extends Hookified {
34
35
  this._environment = options.environment ?? process.env.NODE_ENV ?? "development";
35
36
  this._context = options.context;
36
37
  this._throwErrors = options.throwErrors ?? false;
38
+ this._uris = options.uris;
37
39
  }
38
40
  get applicationId() {
39
41
  return this._applicationId;
@@ -65,6 +67,12 @@ var Toggle = class extends Hookified {
65
67
  set context(value) {
66
68
  this._context = value;
67
69
  }
70
+ get uris() {
71
+ return this._uris;
72
+ }
73
+ set uris(value) {
74
+ this._uris = value;
75
+ }
68
76
  setContext(context) {
69
77
  this._context = context;
70
78
  this._client = void 0;
@@ -73,7 +81,8 @@ var Toggle = class extends Hookified {
73
81
  if (!this._client) {
74
82
  const options = {
75
83
  application: this._applicationId,
76
- environment: this._environment
84
+ environment: this._environment,
85
+ horizonUrls: this._uris
77
86
  };
78
87
  await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicKey, options));
79
88
  this._client = OpenFeature.getClient(this._context);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyphen/sdk",
3
- "version": "1.0.3",
3
+ "version": "1.1.0",
4
4
  "description": "Hyphen SDK for Node.js",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",