@hyphen/sdk 1.3.0 → 1.4.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
@@ -18,6 +18,7 @@ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily
18
18
  - [Toggle Hooks](#toggle-hooks)
19
19
  - [Toggle Error Handling](#toggle-error-handling)
20
20
  - [Toggle Caching](#toggle-caching)
21
+ - [Toggle Environment Variables](#toggle-environment-variables)
21
22
  - [Toggle Self-Hosted](#toggle-self-hosted)
22
23
  - [Contributing](#contributing)
23
24
  - [Testing Your Changes](#testing-your-changes)
@@ -395,6 +396,17 @@ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
395
396
  console.log('Boolean toggle value:', result); // true
396
397
  ```
397
398
 
399
+ # Toggle Environment Variables
400
+
401
+ You can also use environment variables to set the `publicApiKey` and `applicationId`. This is useful for keeping your API keys secure and not hardcoding them in your code. To do this just set your `.env` file with the following variables:
402
+
403
+ ```bash
404
+ HYPHEN_PUBLIC_API_KEY=your_public_api_key
405
+ HYPHEN_APPLICATION_ID=your_application_id
406
+ ```
407
+
408
+ On initialization of the `Toggle` class, the SDK will automatically check for these environment variables and use them if they are set. If they are not set, you will need to provide them in the constructor.
409
+
398
410
  ## Toggle Self-Hosted
399
411
 
400
412
  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
@@ -39,8 +39,10 @@ module.exports = __toCommonJS(index_exports);
39
39
  // src/toggle.ts
40
40
  var import_node_process = __toESM(require("process"), 1);
41
41
  var import_hookified = require("hookified");
42
+ var import_dotenv = __toESM(require("dotenv"), 1);
42
43
  var import_server_sdk = require("@openfeature/server-sdk");
43
44
  var import_openfeature_server_provider = require("@hyphen/openfeature-server-provider");
45
+ import_dotenv.default.config();
44
46
  var ToggleHooks = /* @__PURE__ */ function(ToggleHooks2) {
45
47
  ToggleHooks2["beforeGetBoolean"] = "beforeGetBoolean";
46
48
  ToggleHooks2["afterGetBoolean"] = "afterGetBoolean";
@@ -56,8 +58,8 @@ var Toggle = class extends import_hookified.Hookified {
56
58
  static {
57
59
  __name(this, "Toggle");
58
60
  }
59
- _applicationId;
60
- _publicApiKey = "";
61
+ _applicationId = import_node_process.default.env.HYPHEN_APPLICATION_ID;
62
+ _publicApiKey = import_node_process.default.env.HYPHEN_PUBLIC_API_KEY;
61
63
  _environment;
62
64
  _client;
63
65
  _context;
@@ -70,24 +72,26 @@ var Toggle = class extends import_hookified.Hookified {
70
72
  */
71
73
  constructor(options) {
72
74
  super();
73
- this._applicationId = options.applicationId;
74
- this.setPublicApiKey(options.publicApiKey);
75
- this._environment = options.environment ?? import_node_process.default.env.NODE_ENV ?? "development";
76
- this._context = options.context;
77
- this._throwErrors = options.throwErrors ?? false;
78
- this._uris = options.uris;
79
- this._caching = options.caching;
75
+ this._throwErrors = options?.throwErrors ?? false;
76
+ this._applicationId = options?.applicationId;
77
+ if (options?.publicApiKey) {
78
+ this.setPublicApiKey(options.publicApiKey);
79
+ }
80
+ this._environment = options?.environment ?? import_node_process.default.env.NODE_ENV ?? "development";
81
+ this._context = options?.context;
82
+ this._uris = options?.uris;
83
+ this._caching = options?.caching;
80
84
  }
81
85
  /**
82
86
  * Get the application ID
83
- * @returns {string}
87
+ * @returns {string | undefined}
84
88
  */
85
89
  get applicationId() {
86
90
  return this._applicationId;
87
91
  }
88
92
  /**
89
93
  * Set the application ID
90
- * @param {string} value
94
+ * @param {string | undefined} value
91
95
  */
92
96
  set applicationId(value) {
93
97
  this._applicationId = value;
@@ -104,6 +108,11 @@ var Toggle = class extends import_hookified.Hookified {
104
108
  * @param {string} value
105
109
  */
106
110
  set publicApiKey(value) {
111
+ if (!value) {
112
+ this._publicApiKey = void 0;
113
+ this._client = void 0;
114
+ return;
115
+ }
107
116
  this.setPublicApiKey(value);
108
117
  }
109
118
  /**
@@ -210,13 +219,28 @@ var Toggle = class extends import_hookified.Hookified {
210
219
  */
211
220
  async getClient() {
212
221
  if (!this._client) {
222
+ console.log("Application ID:", this._applicationId);
223
+ if (this._applicationId === void 0 || this._applicationId.length === 0) {
224
+ const errorMessage = "Application ID is not set. You must set it before using the client or have the HYPHEN_APPLICATION_ID environment variable set.";
225
+ this.emit("error", new Error(errorMessage));
226
+ if (this._throwErrors) {
227
+ throw new Error(errorMessage);
228
+ }
229
+ }
213
230
  const options = {
214
231
  application: this._applicationId,
215
232
  environment: this._environment,
216
233
  horizonUrls: this._uris,
217
234
  cache: this._caching
218
235
  };
219
- await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this._publicApiKey, options));
236
+ if (this._publicApiKey && this._publicApiKey.length > 0) {
237
+ await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this._publicApiKey, options));
238
+ } else {
239
+ this.emit("error", new Error("Public API key is not set. You must set it before using the client or have the HYPHEN_PUBLIC_API_KEY environment variable set."));
240
+ if (this._throwErrors) {
241
+ throw new Error("Public API key is not set");
242
+ }
243
+ }
220
244
  this._client = import_server_sdk.OpenFeature.getClient(this._context);
221
245
  }
222
246
  return this._client;
package/dist/index.d.cts CHANGED
@@ -18,15 +18,15 @@ type ToggleCachingOptions = {
18
18
  };
19
19
  type ToggleOptions = {
20
20
  /**
21
- * Your application name
21
+ * Your application name. If this is not set it will look for the HYPHEN_APPLICATION_ID environment variable.
22
22
  * @type {string}
23
23
  */
24
- applicationId: string;
24
+ applicationId?: string;
25
25
  /**
26
- * Your Hyphen Public API key
26
+ * Your Hyphen Public API key. If this is not set it will look for the HYPHEN_PUBLIC_API_KEY environment variable.
27
27
  * @type {string}
28
28
  */
29
- publicApiKey: string;
29
+ publicApiKey?: string;
30
30
  /**
31
31
  * Your environment name such as development, production. Default is what is set at NODE_ENV
32
32
  * @type {string}
@@ -73,27 +73,27 @@ declare class Toggle extends Hookified {
73
73
  private _throwErrors;
74
74
  private _uris;
75
75
  private _caching;
76
- constructor(options: ToggleOptions);
76
+ constructor(options?: ToggleOptions);
77
77
  /**
78
78
  * Get the application ID
79
- * @returns {string}
79
+ * @returns {string | undefined}
80
80
  */
81
- get applicationId(): string;
81
+ get applicationId(): string | undefined;
82
82
  /**
83
83
  * Set the application ID
84
- * @param {string} value
84
+ * @param {string | undefined} value
85
85
  */
86
- set applicationId(value: string);
86
+ set applicationId(value: string | undefined);
87
87
  /**
88
88
  * Get the public API key
89
89
  * @returns {string}
90
90
  */
91
- get publicApiKey(): string;
91
+ get publicApiKey(): string | undefined;
92
92
  /**
93
93
  * Set the public API key
94
94
  * @param {string} value
95
95
  */
96
- set publicApiKey(value: string);
96
+ set publicApiKey(value: string | undefined);
97
97
  /**
98
98
  * Get the environment
99
99
  * @returns {string}
package/dist/index.d.ts CHANGED
@@ -18,15 +18,15 @@ type ToggleCachingOptions = {
18
18
  };
19
19
  type ToggleOptions = {
20
20
  /**
21
- * Your application name
21
+ * Your application name. If this is not set it will look for the HYPHEN_APPLICATION_ID environment variable.
22
22
  * @type {string}
23
23
  */
24
- applicationId: string;
24
+ applicationId?: string;
25
25
  /**
26
- * Your Hyphen Public API key
26
+ * Your Hyphen Public API key. If this is not set it will look for the HYPHEN_PUBLIC_API_KEY environment variable.
27
27
  * @type {string}
28
28
  */
29
- publicApiKey: string;
29
+ publicApiKey?: string;
30
30
  /**
31
31
  * Your environment name such as development, production. Default is what is set at NODE_ENV
32
32
  * @type {string}
@@ -73,27 +73,27 @@ declare class Toggle extends Hookified {
73
73
  private _throwErrors;
74
74
  private _uris;
75
75
  private _caching;
76
- constructor(options: ToggleOptions);
76
+ constructor(options?: ToggleOptions);
77
77
  /**
78
78
  * Get the application ID
79
- * @returns {string}
79
+ * @returns {string | undefined}
80
80
  */
81
- get applicationId(): string;
81
+ get applicationId(): string | undefined;
82
82
  /**
83
83
  * Set the application ID
84
- * @param {string} value
84
+ * @param {string | undefined} value
85
85
  */
86
- set applicationId(value: string);
86
+ set applicationId(value: string | undefined);
87
87
  /**
88
88
  * Get the public API key
89
89
  * @returns {string}
90
90
  */
91
- get publicApiKey(): string;
91
+ get publicApiKey(): string | undefined;
92
92
  /**
93
93
  * Set the public API key
94
94
  * @param {string} value
95
95
  */
96
- set publicApiKey(value: string);
96
+ set publicApiKey(value: string | undefined);
97
97
  /**
98
98
  * Get the environment
99
99
  * @returns {string}
package/dist/index.js CHANGED
@@ -4,8 +4,10 @@ var __name = (target, value) => __defProp(target, "name", { value, configurable:
4
4
  // src/toggle.ts
5
5
  import process from "process";
6
6
  import { Hookified } from "hookified";
7
+ import dotenv from "dotenv";
7
8
  import { OpenFeature } from "@openfeature/server-sdk";
8
9
  import { HyphenProvider } from "@hyphen/openfeature-server-provider";
10
+ dotenv.config();
9
11
  var ToggleHooks = /* @__PURE__ */ function(ToggleHooks2) {
10
12
  ToggleHooks2["beforeGetBoolean"] = "beforeGetBoolean";
11
13
  ToggleHooks2["afterGetBoolean"] = "afterGetBoolean";
@@ -21,8 +23,8 @@ var Toggle = class extends Hookified {
21
23
  static {
22
24
  __name(this, "Toggle");
23
25
  }
24
- _applicationId;
25
- _publicApiKey = "";
26
+ _applicationId = process.env.HYPHEN_APPLICATION_ID;
27
+ _publicApiKey = process.env.HYPHEN_PUBLIC_API_KEY;
26
28
  _environment;
27
29
  _client;
28
30
  _context;
@@ -35,24 +37,26 @@ var Toggle = class extends Hookified {
35
37
  */
36
38
  constructor(options) {
37
39
  super();
38
- this._applicationId = options.applicationId;
39
- this.setPublicApiKey(options.publicApiKey);
40
- this._environment = options.environment ?? process.env.NODE_ENV ?? "development";
41
- this._context = options.context;
42
- this._throwErrors = options.throwErrors ?? false;
43
- this._uris = options.uris;
44
- this._caching = options.caching;
40
+ this._throwErrors = options?.throwErrors ?? false;
41
+ this._applicationId = options?.applicationId;
42
+ if (options?.publicApiKey) {
43
+ this.setPublicApiKey(options.publicApiKey);
44
+ }
45
+ this._environment = options?.environment ?? process.env.NODE_ENV ?? "development";
46
+ this._context = options?.context;
47
+ this._uris = options?.uris;
48
+ this._caching = options?.caching;
45
49
  }
46
50
  /**
47
51
  * Get the application ID
48
- * @returns {string}
52
+ * @returns {string | undefined}
49
53
  */
50
54
  get applicationId() {
51
55
  return this._applicationId;
52
56
  }
53
57
  /**
54
58
  * Set the application ID
55
- * @param {string} value
59
+ * @param {string | undefined} value
56
60
  */
57
61
  set applicationId(value) {
58
62
  this._applicationId = value;
@@ -69,6 +73,11 @@ var Toggle = class extends Hookified {
69
73
  * @param {string} value
70
74
  */
71
75
  set publicApiKey(value) {
76
+ if (!value) {
77
+ this._publicApiKey = void 0;
78
+ this._client = void 0;
79
+ return;
80
+ }
72
81
  this.setPublicApiKey(value);
73
82
  }
74
83
  /**
@@ -175,13 +184,28 @@ var Toggle = class extends Hookified {
175
184
  */
176
185
  async getClient() {
177
186
  if (!this._client) {
187
+ console.log("Application ID:", this._applicationId);
188
+ if (this._applicationId === void 0 || this._applicationId.length === 0) {
189
+ const errorMessage = "Application ID is not set. You must set it before using the client or have the HYPHEN_APPLICATION_ID environment variable set.";
190
+ this.emit("error", new Error(errorMessage));
191
+ if (this._throwErrors) {
192
+ throw new Error(errorMessage);
193
+ }
194
+ }
178
195
  const options = {
179
196
  application: this._applicationId,
180
197
  environment: this._environment,
181
198
  horizonUrls: this._uris,
182
199
  cache: this._caching
183
200
  };
184
- await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicApiKey, options));
201
+ if (this._publicApiKey && this._publicApiKey.length > 0) {
202
+ await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicApiKey, options));
203
+ } else {
204
+ this.emit("error", new Error("Public API key is not set. You must set it before using the client or have the HYPHEN_PUBLIC_API_KEY environment variable set."));
205
+ if (this._throwErrors) {
206
+ throw new Error("Public API key is not set");
207
+ }
208
+ }
185
209
  this._client = OpenFeature.getClient(this._context);
186
210
  }
187
211
  return this._client;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyphen/sdk",
3
- "version": "1.3.0",
3
+ "version": "1.4.0",
4
4
  "description": "Hyphen SDK for Node.js",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -11,12 +11,12 @@
11
11
  "require": "./dist/index.cjs"
12
12
  }
13
13
  },
14
- "types": "dist/node/index.d.ts",
14
+ "types": "dist/index.d.ts",
15
15
  "scripts": {
16
16
  "test": "xo --fix && vitest run --coverage",
17
17
  "test:ci": "xo && vitest run --coverage",
18
18
  "build": "rimraf ./dist && tsup src/index.ts --format esm,cjs --dts --clean",
19
- "clean": "rimraf ./dist",
19
+ "clean": "rimraf ./dist pnpm-lock.yaml node_modules coverage",
20
20
  "prepublishOnly": "rimraf ./dist && tsup src/index.ts --format esm,cjs --dts --clean"
21
21
  },
22
22
  "keywords": [