@hyphen/sdk 1.4.0 → 1.6.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 +46 -1
- package/dist/index.cjs +49 -3
- package/dist/index.d.cts +17 -1
- package/dist/index.d.ts +17 -1
- package/dist/index.js +47 -2
- package/package.json +2 -2
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
|
|
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,51 @@ 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
|
+
The following override path is:
|
|
495
|
+
```
|
|
496
|
+
.env -> .env.local -> .env.<environment> -> .env.<environment>.local
|
|
497
|
+
```
|
|
498
|
+
|
|
499
|
+
Here is an example of how to use the `loadEnv` function:
|
|
500
|
+
|
|
501
|
+
```javascript
|
|
502
|
+
import { loadEnv } from '@hyphen/sdk';
|
|
503
|
+
|
|
504
|
+
//load your default environment variables and envrionment variables
|
|
505
|
+
loadEnv();
|
|
506
|
+
```
|
|
507
|
+
|
|
508
|
+
If your environment variables are not stored in the root of your project you can specify the path to your `.env` file:
|
|
509
|
+
|
|
510
|
+
```javascript
|
|
511
|
+
import { loadEnv } from '@hyphen/sdk';
|
|
512
|
+
//load your default environment variables and envrionment variables
|
|
513
|
+
loadEnv({ path: '/path/to/your/env/files/' });
|
|
514
|
+
```
|
|
515
|
+
|
|
516
|
+
You can also specify the environment variables to load by passing an array of variable names:
|
|
517
|
+
|
|
518
|
+
```javascript
|
|
519
|
+
import { loadEnv } from '@hyphen/sdk';
|
|
520
|
+
//load your default environment variables and envrionment variables
|
|
521
|
+
loadEnv({ environment: 'development' });
|
|
522
|
+
```
|
|
523
|
+
|
|
524
|
+
if you want to turn off the local environment variables you can do it like this:
|
|
525
|
+
|
|
526
|
+
```javascript
|
|
527
|
+
import { loadEnv } from '@hyphen/sdk';
|
|
528
|
+
//load your default environment variables and envrionment variables
|
|
529
|
+
loadEnv({ local: false });
|
|
530
|
+
```
|
|
531
|
+
|
|
487
532
|
# Contributing
|
|
488
533
|
|
|
489
534
|
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,54 @@ 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 local = options?.local ?? true;
|
|
408
|
+
const currentWorkingDirectory = options?.path ?? import_node_process2.default.cwd();
|
|
409
|
+
const envPath = import_node_path.default.resolve(currentWorkingDirectory, ".env");
|
|
410
|
+
if (import_node_fs.default.existsSync(envPath)) {
|
|
411
|
+
(0, import_dotenv2.config)({
|
|
412
|
+
path: envPath
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
if (local) {
|
|
416
|
+
const localEnvPath = import_node_path.default.resolve(currentWorkingDirectory, ".env.local");
|
|
417
|
+
if (import_node_fs.default.existsSync(localEnvPath)) {
|
|
418
|
+
(0, import_dotenv2.config)({
|
|
419
|
+
path: localEnvPath,
|
|
420
|
+
override: true
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const environment = options?.environment ?? import_node_process2.default.env.NODE_ENV;
|
|
425
|
+
if (environment) {
|
|
426
|
+
const envSpecificPath = import_node_path.default.resolve(currentWorkingDirectory, `.env.${environment}`);
|
|
427
|
+
if (import_node_fs.default.existsSync(envSpecificPath)) {
|
|
428
|
+
(0, import_dotenv2.config)({
|
|
429
|
+
path: envSpecificPath,
|
|
430
|
+
override: true
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
if (local) {
|
|
434
|
+
const envLocalPath = import_node_path.default.resolve(currentWorkingDirectory, `.env.${environment}.local`);
|
|
435
|
+
if (import_node_fs.default.existsSync(envLocalPath)) {
|
|
436
|
+
(0, import_dotenv2.config)({
|
|
437
|
+
path: envLocalPath,
|
|
438
|
+
override: true
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
__name(loadEnv, "loadEnv");
|
|
400
445
|
// Annotate the CommonJS export names for ESM import in node:
|
|
401
446
|
0 && (module.exports = {
|
|
402
447
|
Toggle,
|
|
403
|
-
ToggleHooks
|
|
448
|
+
ToggleHooks,
|
|
449
|
+
loadEnv
|
|
404
450
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -202,4 +202,20 @@ declare class Toggle extends Hookified {
|
|
|
202
202
|
getObject<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
|
|
205
|
+
type LoadEnvOptions = {
|
|
206
|
+
path?: string;
|
|
207
|
+
environment?: string;
|
|
208
|
+
local?: boolean;
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* @description Helper function to load your environment variables based on your default .env file
|
|
212
|
+
* and the current environment.
|
|
213
|
+
* @param {LoadEnvOptions} [options] - Options to customize the loading of environment variables.
|
|
214
|
+
* @returns {void}
|
|
215
|
+
* @example
|
|
216
|
+
* import { loadEnv } from '@hyphen/sdk';
|
|
217
|
+
* loadEnv();
|
|
218
|
+
*/
|
|
219
|
+
declare function loadEnv(options?: LoadEnvOptions): void;
|
|
220
|
+
|
|
221
|
+
export { Toggle, type ToggleCachingOptions, type ToggleContext, type ToggleGetOptions, ToggleHooks, type ToggleOptions, loadEnv };
|
package/dist/index.d.ts
CHANGED
|
@@ -202,4 +202,20 @@ declare class Toggle extends Hookified {
|
|
|
202
202
|
getObject<T>(key: string, defaultValue: T, options?: ToggleGetOptions): Promise<T>;
|
|
203
203
|
}
|
|
204
204
|
|
|
205
|
-
|
|
205
|
+
type LoadEnvOptions = {
|
|
206
|
+
path?: string;
|
|
207
|
+
environment?: string;
|
|
208
|
+
local?: boolean;
|
|
209
|
+
};
|
|
210
|
+
/**
|
|
211
|
+
* @description Helper function to load your environment variables based on your default .env file
|
|
212
|
+
* and the current environment.
|
|
213
|
+
* @param {LoadEnvOptions} [options] - Options to customize the loading of environment variables.
|
|
214
|
+
* @returns {void}
|
|
215
|
+
* @example
|
|
216
|
+
* import { loadEnv } from '@hyphen/sdk';
|
|
217
|
+
* loadEnv();
|
|
218
|
+
*/
|
|
219
|
+
declare function loadEnv(options?: LoadEnvOptions): void;
|
|
220
|
+
|
|
221
|
+
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,53 @@ 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 local = options?.local ?? true;
|
|
372
|
+
const currentWorkingDirectory = options?.path ?? process2.cwd();
|
|
373
|
+
const envPath = path.resolve(currentWorkingDirectory, ".env");
|
|
374
|
+
if (fs.existsSync(envPath)) {
|
|
375
|
+
config({
|
|
376
|
+
path: envPath
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
if (local) {
|
|
380
|
+
const localEnvPath = path.resolve(currentWorkingDirectory, ".env.local");
|
|
381
|
+
if (fs.existsSync(localEnvPath)) {
|
|
382
|
+
config({
|
|
383
|
+
path: localEnvPath,
|
|
384
|
+
override: true
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
const environment = options?.environment ?? process2.env.NODE_ENV;
|
|
389
|
+
if (environment) {
|
|
390
|
+
const envSpecificPath = path.resolve(currentWorkingDirectory, `.env.${environment}`);
|
|
391
|
+
if (fs.existsSync(envSpecificPath)) {
|
|
392
|
+
config({
|
|
393
|
+
path: envSpecificPath,
|
|
394
|
+
override: true
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
if (local) {
|
|
398
|
+
const envLocalPath = path.resolve(currentWorkingDirectory, `.env.${environment}.local`);
|
|
399
|
+
if (fs.existsSync(envLocalPath)) {
|
|
400
|
+
config({
|
|
401
|
+
path: envLocalPath,
|
|
402
|
+
override: true
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
__name(loadEnv, "loadEnv");
|
|
365
409
|
export {
|
|
366
410
|
Toggle,
|
|
367
|
-
ToggleHooks
|
|
411
|
+
ToggleHooks,
|
|
412
|
+
loadEnv
|
|
368
413
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hyphen/sdk",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.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.
|
|
40
|
+
"xo": "^1.1.0"
|
|
41
41
|
},
|
|
42
42
|
"files": [
|
|
43
43
|
"dist",
|