@hyphen/sdk 1.4.0 → 1.5.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
@@ -7,7 +7,7 @@
7
7
 
8
8
  # Hyphen Node.js SDK
9
9
 
10
- The Hyphen Node.js SDK is a JavaScript library that allows developers to easily integrate Hyphen's feature flagging and experimentation capabilities into their Node.js applications. With this SDK, you can manage feature flags more effectively, enabling you to control the rollout of new features and conduct A/B testing with ease.
10
+ The Hyphen Node.js SDK is a JavaScript library that allows developers to easily integrate Hyphen's feature flag service [Toggle](https://hyphen.ai/toggle) into their Node.js applications. In addition to Toggle, the SDK also provides ENV management capabilities such as loading environment variables from `.env` files, managing environment variables.
11
11
 
12
12
  # Table of Contents
13
13
  - [Installation](#installation)
@@ -484,6 +484,36 @@ const result = await toggle.getBoolean('hyphen-sdk-boolean', false);
484
484
  console.log('Boolean toggle value:', result); // true
485
485
  ```
486
486
 
487
+ # ENV
488
+
489
+ Hyphens secret management service known as [ENV](https://hyphen.ai/env) allows you to manage your environment variables in a secure way. The Hyphen Node.js SDK provides a simple way to access your environment variables.
490
+
491
+ ## Loading Environment Variables
492
+ To load your environment variables, you can use the `loadEnv` function from the SDK. This function will automatically load your environment variables from the `.env` file and then override them with the environment based environment file if it exists (ex: `.env.development`). This is useful for managing different environments such as development, staging, and production.
493
+
494
+ ```javascript
495
+ import { loadEnv } from '@hyphen/sdk';
496
+
497
+ //load your default environment variables and envrionment variables
498
+ loadEnv();
499
+ ```
500
+
501
+ If your environment variables are not stored in the root of your project you can specify the path to your `.env` file:
502
+
503
+ ```javascript
504
+ import { loadEnv } from '@hyphen/sdk';
505
+ //load your default environment variables and envrionment variables
506
+ loadEnv({ path: '/path/to/your/env/files/' });
507
+ ```
508
+
509
+ You can also specify the environment variables to load by passing an array of variable names:
510
+
511
+ ```javascript
512
+ import { loadEnv } from '@hyphen/sdk';
513
+ //load your default environment variables and envrionment variables
514
+ loadEnv({ environment: 'development' });
515
+ ```
516
+
487
517
  # Contributing
488
518
 
489
519
  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:
package/dist/index.cjs CHANGED
@@ -32,7 +32,8 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
32
32
  var index_exports = {};
33
33
  __export(index_exports, {
34
34
  Toggle: () => Toggle,
35
- ToggleHooks: () => ToggleHooks
35
+ ToggleHooks: () => ToggleHooks,
36
+ loadEnv: () => loadEnv
36
37
  });
37
38
  module.exports = __toCommonJS(index_exports);
38
39
 
@@ -219,7 +220,6 @@ var Toggle = class extends import_hookified.Hookified {
219
220
  */
220
221
  async getClient() {
221
222
  if (!this._client) {
222
- console.log("Application ID:", this._applicationId);
223
223
  if (this._applicationId === void 0 || this._applicationId.length === 0) {
224
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
225
  this.emit("error", new Error(errorMessage));
@@ -397,8 +397,35 @@ var Toggle = class extends import_hookified.Hookified {
397
397
  return defaultValue;
398
398
  }
399
399
  };
400
+
401
+ // src/env.ts
402
+ var import_node_process2 = __toESM(require("process"), 1);
403
+ var import_node_fs = __toESM(require("fs"), 1);
404
+ var import_node_path = __toESM(require("path"), 1);
405
+ var import_dotenv2 = require("dotenv");
406
+ function loadEnv(options) {
407
+ const currentWorkingDirectory = options?.path ?? import_node_process2.default.cwd();
408
+ const envPath = import_node_path.default.resolve(currentWorkingDirectory, ".env");
409
+ if (import_node_fs.default.existsSync(envPath)) {
410
+ (0, import_dotenv2.config)({
411
+ path: envPath
412
+ });
413
+ }
414
+ const environment = options?.environment ?? import_node_process2.default.env.NODE_ENV;
415
+ if (environment) {
416
+ const envSpecificPath = import_node_path.default.resolve(currentWorkingDirectory, `.env.${environment}`);
417
+ if (import_node_fs.default.existsSync(envSpecificPath)) {
418
+ (0, import_dotenv2.config)({
419
+ path: envSpecificPath,
420
+ override: true
421
+ });
422
+ }
423
+ }
424
+ }
425
+ __name(loadEnv, "loadEnv");
400
426
  // Annotate the CommonJS export names for ESM import in node:
401
427
  0 && (module.exports = {
402
428
  Toggle,
403
- ToggleHooks
429
+ ToggleHooks,
430
+ loadEnv
404
431
  });
package/dist/index.d.cts CHANGED
@@ -202,4 +202,19 @@ declare class Toggle extends Hookified {
202
202
  getObject<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
203
203
  }
204
204
 
205
- export { Toggle, type ToggleCachingOptions, type ToggleContext, type ToggleGetOptions, ToggleHooks, type ToggleOptions };
205
+ type LoadEnvOptions = {
206
+ path?: string;
207
+ environment?: string;
208
+ };
209
+ /**
210
+ * @description Helper function to load your environment variables based on your default .env file
211
+ * and the current environment.
212
+ * @param {LoadEnvOptions} [options] - Options to customize the loading of environment variables.
213
+ * @returns {void}
214
+ * @example
215
+ * import { loadEnv } from '@hyphen/sdk';
216
+ * loadEnv();
217
+ */
218
+ declare function loadEnv(options?: LoadEnvOptions): void;
219
+
220
+ export { Toggle, type ToggleCachingOptions, type ToggleContext, type ToggleGetOptions, ToggleHooks, type ToggleOptions, loadEnv };
package/dist/index.d.ts CHANGED
@@ -202,4 +202,19 @@ declare class Toggle extends Hookified {
202
202
  getObject<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
203
203
  }
204
204
 
205
- export { Toggle, type ToggleCachingOptions, type ToggleContext, type ToggleGetOptions, ToggleHooks, type ToggleOptions };
205
+ type LoadEnvOptions = {
206
+ path?: string;
207
+ environment?: string;
208
+ };
209
+ /**
210
+ * @description Helper function to load your environment variables based on your default .env file
211
+ * and the current environment.
212
+ * @param {LoadEnvOptions} [options] - Options to customize the loading of environment variables.
213
+ * @returns {void}
214
+ * @example
215
+ * import { loadEnv } from '@hyphen/sdk';
216
+ * loadEnv();
217
+ */
218
+ declare function loadEnv(options?: LoadEnvOptions): void;
219
+
220
+ export { Toggle, type ToggleCachingOptions, type ToggleContext, type ToggleGetOptions, ToggleHooks, type ToggleOptions, loadEnv };
package/dist/index.js CHANGED
@@ -184,7 +184,6 @@ var Toggle = class extends Hookified {
184
184
  */
185
185
  async getClient() {
186
186
  if (!this._client) {
187
- console.log("Application ID:", this._applicationId);
188
187
  if (this._applicationId === void 0 || this._applicationId.length === 0) {
189
188
  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
189
  this.emit("error", new Error(errorMessage));
@@ -362,7 +361,34 @@ var Toggle = class extends Hookified {
362
361
  return defaultValue;
363
362
  }
364
363
  };
364
+
365
+ // src/env.ts
366
+ import process2 from "process";
367
+ import fs from "fs";
368
+ import path from "path";
369
+ import { config } from "dotenv";
370
+ function loadEnv(options) {
371
+ const currentWorkingDirectory = options?.path ?? process2.cwd();
372
+ const envPath = path.resolve(currentWorkingDirectory, ".env");
373
+ if (fs.existsSync(envPath)) {
374
+ config({
375
+ path: envPath
376
+ });
377
+ }
378
+ const environment = options?.environment ?? process2.env.NODE_ENV;
379
+ if (environment) {
380
+ const envSpecificPath = path.resolve(currentWorkingDirectory, `.env.${environment}`);
381
+ if (fs.existsSync(envSpecificPath)) {
382
+ config({
383
+ path: envSpecificPath,
384
+ override: true
385
+ });
386
+ }
387
+ }
388
+ }
389
+ __name(loadEnv, "loadEnv");
365
390
  export {
366
391
  Toggle,
367
- ToggleHooks
392
+ ToggleHooks,
393
+ loadEnv
368
394
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyphen/sdk",
3
- "version": "1.4.0",
3
+ "version": "1.5.0",
4
4
  "description": "Hyphen SDK for Node.js",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",
@@ -37,7 +37,7 @@
37
37
  "tsup": "^8.5.0",
38
38
  "typescript": "^5.8.3",
39
39
  "vitest": "^3.1.4",
40
- "xo": "^1.0.0"
40
+ "xo": "^1.1.0"
41
41
  },
42
42
  "files": [
43
43
  "dist",