@hyphen/sdk 1.1.0 → 1.2.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 +0 -1
- package/dist/index.cjs +118 -7
- package/dist/index.d.cts +106 -5
- package/dist/index.d.ts +106 -5
- package/dist/index.js +119 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,6 @@
|
|
|
10
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.
|
|
11
11
|
|
|
12
12
|
# Table of Contents
|
|
13
|
-
- [Hyphen Node.js SDK](#hyphen-nodejs-sdk)
|
|
14
13
|
- [Installation](#installation)
|
|
15
14
|
- [Basic Usage](#basic-usage)
|
|
16
15
|
- [Toggle](#toggle)
|
package/dist/index.cjs
CHANGED
|
@@ -57,61 +57,141 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
57
57
|
__name(this, "Toggle");
|
|
58
58
|
}
|
|
59
59
|
_applicationId;
|
|
60
|
-
|
|
60
|
+
_publicApiKey = "";
|
|
61
61
|
_environment;
|
|
62
62
|
_client;
|
|
63
63
|
_context;
|
|
64
64
|
_throwErrors = false;
|
|
65
65
|
_uris;
|
|
66
|
+
/*
|
|
67
|
+
* Create a new Toggle instance. This will create a new client and set the options.
|
|
68
|
+
* @param {ToggleOptions}
|
|
69
|
+
*/
|
|
66
70
|
constructor(options) {
|
|
67
71
|
super();
|
|
68
72
|
this._applicationId = options.applicationId;
|
|
69
|
-
this.
|
|
73
|
+
this.setPublicApiKey(options.publicApiKey);
|
|
70
74
|
this._environment = options.environment ?? import_node_process.default.env.NODE_ENV ?? "development";
|
|
71
75
|
this._context = options.context;
|
|
72
76
|
this._throwErrors = options.throwErrors ?? false;
|
|
73
77
|
this._uris = options.uris;
|
|
74
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Get the application ID
|
|
81
|
+
* @returns {string}
|
|
82
|
+
*/
|
|
75
83
|
get applicationId() {
|
|
76
84
|
return this._applicationId;
|
|
77
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Set the application ID
|
|
88
|
+
* @param {string} value
|
|
89
|
+
*/
|
|
78
90
|
set applicationId(value) {
|
|
79
91
|
this._applicationId = value;
|
|
80
92
|
}
|
|
81
|
-
|
|
82
|
-
|
|
93
|
+
/**
|
|
94
|
+
* Get the public API key
|
|
95
|
+
* @returns {string}
|
|
96
|
+
*/
|
|
97
|
+
get publicApiKey() {
|
|
98
|
+
return this._publicApiKey;
|
|
83
99
|
}
|
|
84
|
-
|
|
85
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Set the public API key
|
|
102
|
+
* @param {string} value
|
|
103
|
+
*/
|
|
104
|
+
set publicApiKey(value) {
|
|
105
|
+
this.setPublicApiKey(value);
|
|
86
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Get the environment
|
|
109
|
+
* @returns {string}
|
|
110
|
+
*/
|
|
87
111
|
get environment() {
|
|
88
112
|
return this._environment;
|
|
89
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Set the environment
|
|
116
|
+
* @param {string} value
|
|
117
|
+
*/
|
|
90
118
|
set environment(value) {
|
|
91
119
|
this._environment = value;
|
|
92
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Get the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
123
|
+
* @returns {boolean}
|
|
124
|
+
*/
|
|
93
125
|
get throwErrors() {
|
|
94
126
|
return this._throwErrors;
|
|
95
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* Set the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
130
|
+
* @param {boolean} value
|
|
131
|
+
*/
|
|
96
132
|
set throwErrors(value) {
|
|
97
133
|
this._throwErrors = value;
|
|
98
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Get the current context. This is the default context used. You can override this at the get function level.
|
|
137
|
+
* @returns {ToggleContext}
|
|
138
|
+
*/
|
|
99
139
|
get context() {
|
|
100
140
|
return this._context;
|
|
101
141
|
}
|
|
142
|
+
/**
|
|
143
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
144
|
+
* @param {ToggleContext} value
|
|
145
|
+
*/
|
|
102
146
|
set context(value) {
|
|
103
147
|
this._context = value;
|
|
104
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
151
|
+
* @returns {Array<string>}
|
|
152
|
+
*/
|
|
105
153
|
get uris() {
|
|
106
154
|
return this._uris;
|
|
107
155
|
}
|
|
156
|
+
/**
|
|
157
|
+
* Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
158
|
+
* @param {Array<string>} value
|
|
159
|
+
*/
|
|
108
160
|
set uris(value) {
|
|
109
161
|
this._uris = value;
|
|
110
162
|
}
|
|
163
|
+
/**
|
|
164
|
+
* 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,
|
|
166
|
+
* 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
|
|
169
|
+
*/
|
|
170
|
+
setPublicApiKey(key) {
|
|
171
|
+
if (!key.startsWith("public_")) {
|
|
172
|
+
this.emit("error", new Error("Public API key should start with public_"));
|
|
173
|
+
if (import_node_process.default.env.NODE_ENV !== "production") {
|
|
174
|
+
console.error("Public API key should start with public_");
|
|
175
|
+
}
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
this._publicApiKey = key;
|
|
179
|
+
this._client = void 0;
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
183
|
+
* @param {ToggleContext} context
|
|
184
|
+
*/
|
|
111
185
|
setContext(context) {
|
|
112
186
|
this._context = context;
|
|
113
187
|
this._client = void 0;
|
|
114
188
|
}
|
|
189
|
+
/**
|
|
190
|
+
* Helper function to get the client. This will create a new client if one does not exist. It will also set the
|
|
191
|
+
* application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
|
|
192
|
+
* This is normally only used internally.
|
|
193
|
+
* @returns {Promise<Client>}
|
|
194
|
+
*/
|
|
115
195
|
async getClient() {
|
|
116
196
|
if (!this._client) {
|
|
117
197
|
const options = {
|
|
@@ -119,11 +199,19 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
119
199
|
environment: this._environment,
|
|
120
200
|
horizonUrls: this._uris
|
|
121
201
|
};
|
|
122
|
-
await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this.
|
|
202
|
+
await import_server_sdk.OpenFeature.setProviderAndWait(new import_openfeature_server_provider.HyphenProvider(this._publicApiKey, options));
|
|
123
203
|
this._client = import_server_sdk.OpenFeature.getClient(this._context);
|
|
124
204
|
}
|
|
125
205
|
return this._client;
|
|
126
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* This is the main function to get a feature flag value. It will check the type of the default value and call the
|
|
209
|
+
* appropriate function. It will also set the context if it is not set.
|
|
210
|
+
* @param {string} key - The key of the feature flag
|
|
211
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
212
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
213
|
+
* @returns {Promise<T>}
|
|
214
|
+
*/
|
|
127
215
|
async get(key, defaultValue, options) {
|
|
128
216
|
switch (typeof defaultValue) {
|
|
129
217
|
case "boolean": {
|
|
@@ -140,6 +228,14 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
140
228
|
}
|
|
141
229
|
}
|
|
142
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* Get a boolean value from the feature flag. This will check the type of the default value and call the
|
|
233
|
+
* appropriate function. It will also set the context if it is not set.
|
|
234
|
+
* @param {string} key - The key of the feature flag
|
|
235
|
+
* @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
236
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
237
|
+
* @returns {Promise<boolean>} - The value of the feature flag
|
|
238
|
+
*/
|
|
143
239
|
async getBoolean(key, defaultValue, options) {
|
|
144
240
|
try {
|
|
145
241
|
const data = {
|
|
@@ -166,6 +262,13 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
166
262
|
}
|
|
167
263
|
return defaultValue;
|
|
168
264
|
}
|
|
265
|
+
/**
|
|
266
|
+
* Get a string value from the feature flag.
|
|
267
|
+
* @param {string} key - The key of the feature flag
|
|
268
|
+
* @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
269
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
270
|
+
* @returns {Promise<string>} - The value of the feature flag
|
|
271
|
+
*/
|
|
169
272
|
async getString(key, defaultValue, options) {
|
|
170
273
|
try {
|
|
171
274
|
const data = {
|
|
@@ -218,6 +321,14 @@ var Toggle = class extends import_hookified.Hookified {
|
|
|
218
321
|
}
|
|
219
322
|
return defaultValue;
|
|
220
323
|
}
|
|
324
|
+
/**
|
|
325
|
+
* Get an object value from the feature flag. This will check the type of the default value and call the
|
|
326
|
+
* appropriate function. It will also set the context if it is not set.
|
|
327
|
+
* @param {string} key - The key of the feature flag
|
|
328
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
329
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
330
|
+
* @returns {Promise<T>} - The value of the feature flag
|
|
331
|
+
*/
|
|
221
332
|
async getObject(key, defaultValue, options) {
|
|
222
333
|
try {
|
|
223
334
|
const data = {
|
package/dist/index.d.cts
CHANGED
|
@@ -19,10 +19,10 @@ type ToggleOptions = {
|
|
|
19
19
|
*/
|
|
20
20
|
applicationId: string;
|
|
21
21
|
/**
|
|
22
|
-
* Your Hyphen API key
|
|
22
|
+
* Your Hyphen Public API key
|
|
23
23
|
* @type {string}
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
publicApiKey: string;
|
|
26
26
|
/**
|
|
27
27
|
* Your environment name such as development, production. Default is what is set at NODE_ENV
|
|
28
28
|
* @type {string}
|
|
@@ -56,35 +56,136 @@ type ToggleOptions = {
|
|
|
56
56
|
uris?: string[];
|
|
57
57
|
};
|
|
58
58
|
type ToggleRequestOptions = {
|
|
59
|
+
/**
|
|
60
|
+
* The context to use for evaluating feature flags
|
|
61
|
+
* @type {ToggleContext}
|
|
62
|
+
*/
|
|
59
63
|
context?: ToggleContext;
|
|
60
64
|
};
|
|
61
65
|
declare class Toggle extends Hookified {
|
|
62
66
|
private _applicationId;
|
|
63
|
-
private
|
|
67
|
+
private _publicApiKey;
|
|
64
68
|
private _environment;
|
|
65
69
|
private _client;
|
|
66
70
|
private _context;
|
|
67
71
|
private _throwErrors;
|
|
68
72
|
private _uris?;
|
|
69
73
|
constructor(options: ToggleOptions);
|
|
74
|
+
/**
|
|
75
|
+
* Get the application ID
|
|
76
|
+
* @returns {string}
|
|
77
|
+
*/
|
|
70
78
|
get applicationId(): string;
|
|
79
|
+
/**
|
|
80
|
+
* Set the application ID
|
|
81
|
+
* @param {string} value
|
|
82
|
+
*/
|
|
71
83
|
set applicationId(value: string);
|
|
72
|
-
|
|
73
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Get the public API key
|
|
86
|
+
* @returns {string}
|
|
87
|
+
*/
|
|
88
|
+
get publicApiKey(): string;
|
|
89
|
+
/**
|
|
90
|
+
* Set the public API key
|
|
91
|
+
* @param {string} value
|
|
92
|
+
*/
|
|
93
|
+
set publicApiKey(value: string);
|
|
94
|
+
/**
|
|
95
|
+
* Get the environment
|
|
96
|
+
* @returns {string}
|
|
97
|
+
*/
|
|
74
98
|
get environment(): string;
|
|
99
|
+
/**
|
|
100
|
+
* Set the environment
|
|
101
|
+
* @param {string} value
|
|
102
|
+
*/
|
|
75
103
|
set environment(value: string);
|
|
104
|
+
/**
|
|
105
|
+
* Get the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
106
|
+
* @returns {boolean}
|
|
107
|
+
*/
|
|
76
108
|
get throwErrors(): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Set the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
111
|
+
* @param {boolean} value
|
|
112
|
+
*/
|
|
77
113
|
set throwErrors(value: boolean);
|
|
114
|
+
/**
|
|
115
|
+
* Get the current context. This is the default context used. You can override this at the get function level.
|
|
116
|
+
* @returns {ToggleContext}
|
|
117
|
+
*/
|
|
78
118
|
get context(): ToggleContext | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
121
|
+
* @param {ToggleContext} value
|
|
122
|
+
*/
|
|
79
123
|
set context(value: ToggleContext | undefined);
|
|
124
|
+
/**
|
|
125
|
+
* Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
126
|
+
* @returns {Array<string>}
|
|
127
|
+
*/
|
|
80
128
|
get uris(): string[] | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
131
|
+
* @param {Array<string>} value
|
|
132
|
+
*/
|
|
81
133
|
set uris(value: string[] | undefined);
|
|
134
|
+
/**
|
|
135
|
+
* 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
|
+
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
137
|
+
* it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
|
|
138
|
+
* @param key
|
|
139
|
+
* @returns
|
|
140
|
+
*/
|
|
141
|
+
setPublicApiKey(key: string): void;
|
|
142
|
+
/**
|
|
143
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
144
|
+
* @param {ToggleContext} context
|
|
145
|
+
*/
|
|
82
146
|
setContext(context: ToggleContext): void;
|
|
147
|
+
/**
|
|
148
|
+
* Helper function to get the client. This will create a new client if one does not exist. It will also set the
|
|
149
|
+
* application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
|
|
150
|
+
* This is normally only used internally.
|
|
151
|
+
* @returns {Promise<Client>}
|
|
152
|
+
*/
|
|
83
153
|
getClient(): Promise<Client>;
|
|
154
|
+
/**
|
|
155
|
+
* This is the main function to get a feature flag value. It will check the type of the default value and call the
|
|
156
|
+
* appropriate function. It will also set the context if it is not set.
|
|
157
|
+
* @param {string} key - The key of the feature flag
|
|
158
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
159
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
160
|
+
* @returns {Promise<T>}
|
|
161
|
+
*/
|
|
84
162
|
get<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
|
|
163
|
+
/**
|
|
164
|
+
* Get a boolean value from the feature flag. This will check the type of the default value and call the
|
|
165
|
+
* appropriate function. It will also set the context if it is not set.
|
|
166
|
+
* @param {string} key - The key of the feature flag
|
|
167
|
+
* @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
168
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
169
|
+
* @returns {Promise<boolean>} - The value of the feature flag
|
|
170
|
+
*/
|
|
85
171
|
getBoolean(key: string, defaultValue: boolean, options?: ToggleRequestOptions): Promise<boolean>;
|
|
172
|
+
/**
|
|
173
|
+
* Get a string value from the feature flag.
|
|
174
|
+
* @param {string} key - The key of the feature flag
|
|
175
|
+
* @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
176
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
177
|
+
* @returns {Promise<string>} - The value of the feature flag
|
|
178
|
+
*/
|
|
86
179
|
getString(key: string, defaultValue: string, options?: ToggleRequestOptions): Promise<string>;
|
|
87
180
|
getNumber(key: string, defaultValue: number, options?: ToggleRequestOptions): Promise<number>;
|
|
181
|
+
/**
|
|
182
|
+
* Get an object value from the feature flag. This will check the type of the default value and call the
|
|
183
|
+
* appropriate function. It will also set the context if it is not set.
|
|
184
|
+
* @param {string} key - The key of the feature flag
|
|
185
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
186
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
187
|
+
* @returns {Promise<T>} - The value of the feature flag
|
|
188
|
+
*/
|
|
88
189
|
getObject<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
|
|
89
190
|
}
|
|
90
191
|
|
package/dist/index.d.ts
CHANGED
|
@@ -19,10 +19,10 @@ type ToggleOptions = {
|
|
|
19
19
|
*/
|
|
20
20
|
applicationId: string;
|
|
21
21
|
/**
|
|
22
|
-
* Your Hyphen API key
|
|
22
|
+
* Your Hyphen Public API key
|
|
23
23
|
* @type {string}
|
|
24
24
|
*/
|
|
25
|
-
|
|
25
|
+
publicApiKey: string;
|
|
26
26
|
/**
|
|
27
27
|
* Your environment name such as development, production. Default is what is set at NODE_ENV
|
|
28
28
|
* @type {string}
|
|
@@ -56,35 +56,136 @@ type ToggleOptions = {
|
|
|
56
56
|
uris?: string[];
|
|
57
57
|
};
|
|
58
58
|
type ToggleRequestOptions = {
|
|
59
|
+
/**
|
|
60
|
+
* The context to use for evaluating feature flags
|
|
61
|
+
* @type {ToggleContext}
|
|
62
|
+
*/
|
|
59
63
|
context?: ToggleContext;
|
|
60
64
|
};
|
|
61
65
|
declare class Toggle extends Hookified {
|
|
62
66
|
private _applicationId;
|
|
63
|
-
private
|
|
67
|
+
private _publicApiKey;
|
|
64
68
|
private _environment;
|
|
65
69
|
private _client;
|
|
66
70
|
private _context;
|
|
67
71
|
private _throwErrors;
|
|
68
72
|
private _uris?;
|
|
69
73
|
constructor(options: ToggleOptions);
|
|
74
|
+
/**
|
|
75
|
+
* Get the application ID
|
|
76
|
+
* @returns {string}
|
|
77
|
+
*/
|
|
70
78
|
get applicationId(): string;
|
|
79
|
+
/**
|
|
80
|
+
* Set the application ID
|
|
81
|
+
* @param {string} value
|
|
82
|
+
*/
|
|
71
83
|
set applicationId(value: string);
|
|
72
|
-
|
|
73
|
-
|
|
84
|
+
/**
|
|
85
|
+
* Get the public API key
|
|
86
|
+
* @returns {string}
|
|
87
|
+
*/
|
|
88
|
+
get publicApiKey(): string;
|
|
89
|
+
/**
|
|
90
|
+
* Set the public API key
|
|
91
|
+
* @param {string} value
|
|
92
|
+
*/
|
|
93
|
+
set publicApiKey(value: string);
|
|
94
|
+
/**
|
|
95
|
+
* Get the environment
|
|
96
|
+
* @returns {string}
|
|
97
|
+
*/
|
|
74
98
|
get environment(): string;
|
|
99
|
+
/**
|
|
100
|
+
* Set the environment
|
|
101
|
+
* @param {string} value
|
|
102
|
+
*/
|
|
75
103
|
set environment(value: string);
|
|
104
|
+
/**
|
|
105
|
+
* Get the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
106
|
+
* @returns {boolean}
|
|
107
|
+
*/
|
|
76
108
|
get throwErrors(): boolean;
|
|
109
|
+
/**
|
|
110
|
+
* Set the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
111
|
+
* @param {boolean} value
|
|
112
|
+
*/
|
|
77
113
|
set throwErrors(value: boolean);
|
|
114
|
+
/**
|
|
115
|
+
* Get the current context. This is the default context used. You can override this at the get function level.
|
|
116
|
+
* @returns {ToggleContext}
|
|
117
|
+
*/
|
|
78
118
|
get context(): ToggleContext | undefined;
|
|
119
|
+
/**
|
|
120
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
121
|
+
* @param {ToggleContext} value
|
|
122
|
+
*/
|
|
79
123
|
set context(value: ToggleContext | undefined);
|
|
124
|
+
/**
|
|
125
|
+
* Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
126
|
+
* @returns {Array<string>}
|
|
127
|
+
*/
|
|
80
128
|
get uris(): string[] | undefined;
|
|
129
|
+
/**
|
|
130
|
+
* Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
131
|
+
* @param {Array<string>} value
|
|
132
|
+
*/
|
|
81
133
|
set uris(value: string[] | undefined);
|
|
134
|
+
/**
|
|
135
|
+
* 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
|
+
* does set it will also set the client to undefined to force a new one to be created. If it does not,
|
|
137
|
+
* it will emit an error and console warning and not set the key. Used by the constructor and publicApiKey setter.
|
|
138
|
+
* @param key
|
|
139
|
+
* @returns
|
|
140
|
+
*/
|
|
141
|
+
setPublicApiKey(key: string): void;
|
|
142
|
+
/**
|
|
143
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
144
|
+
* @param {ToggleContext} context
|
|
145
|
+
*/
|
|
82
146
|
setContext(context: ToggleContext): void;
|
|
147
|
+
/**
|
|
148
|
+
* Helper function to get the client. This will create a new client if one does not exist. It will also set the
|
|
149
|
+
* application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
|
|
150
|
+
* This is normally only used internally.
|
|
151
|
+
* @returns {Promise<Client>}
|
|
152
|
+
*/
|
|
83
153
|
getClient(): Promise<Client>;
|
|
154
|
+
/**
|
|
155
|
+
* This is the main function to get a feature flag value. It will check the type of the default value and call the
|
|
156
|
+
* appropriate function. It will also set the context if it is not set.
|
|
157
|
+
* @param {string} key - The key of the feature flag
|
|
158
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
159
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
160
|
+
* @returns {Promise<T>}
|
|
161
|
+
*/
|
|
84
162
|
get<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
|
|
163
|
+
/**
|
|
164
|
+
* Get a boolean value from the feature flag. This will check the type of the default value and call the
|
|
165
|
+
* appropriate function. It will also set the context if it is not set.
|
|
166
|
+
* @param {string} key - The key of the feature flag
|
|
167
|
+
* @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
168
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
169
|
+
* @returns {Promise<boolean>} - The value of the feature flag
|
|
170
|
+
*/
|
|
85
171
|
getBoolean(key: string, defaultValue: boolean, options?: ToggleRequestOptions): Promise<boolean>;
|
|
172
|
+
/**
|
|
173
|
+
* Get a string value from the feature flag.
|
|
174
|
+
* @param {string} key - The key of the feature flag
|
|
175
|
+
* @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
176
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
177
|
+
* @returns {Promise<string>} - The value of the feature flag
|
|
178
|
+
*/
|
|
86
179
|
getString(key: string, defaultValue: string, options?: ToggleRequestOptions): Promise<string>;
|
|
87
180
|
getNumber(key: string, defaultValue: number, options?: ToggleRequestOptions): Promise<number>;
|
|
181
|
+
/**
|
|
182
|
+
* Get an object value from the feature flag. This will check the type of the default value and call the
|
|
183
|
+
* appropriate function. It will also set the context if it is not set.
|
|
184
|
+
* @param {string} key - The key of the feature flag
|
|
185
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
186
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
187
|
+
* @returns {Promise<T>} - The value of the feature flag
|
|
188
|
+
*/
|
|
88
189
|
getObject<T>(key: string, defaultValue: T, options?: ToggleRequestOptions): Promise<T>;
|
|
89
190
|
}
|
|
90
191
|
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ var __defProp = Object.defineProperty;
|
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
4
|
// src/toggle.ts
|
|
5
|
-
import process from "
|
|
5
|
+
import process from "process";
|
|
6
6
|
import { Hookified } from "hookified";
|
|
7
7
|
import { OpenFeature } from "@openfeature/server-sdk";
|
|
8
8
|
import { HyphenProvider } from "@hyphen/openfeature-server-provider";
|
|
@@ -22,61 +22,141 @@ var Toggle = class extends Hookified {
|
|
|
22
22
|
__name(this, "Toggle");
|
|
23
23
|
}
|
|
24
24
|
_applicationId;
|
|
25
|
-
|
|
25
|
+
_publicApiKey = "";
|
|
26
26
|
_environment;
|
|
27
27
|
_client;
|
|
28
28
|
_context;
|
|
29
29
|
_throwErrors = false;
|
|
30
30
|
_uris;
|
|
31
|
+
/*
|
|
32
|
+
* Create a new Toggle instance. This will create a new client and set the options.
|
|
33
|
+
* @param {ToggleOptions}
|
|
34
|
+
*/
|
|
31
35
|
constructor(options) {
|
|
32
36
|
super();
|
|
33
37
|
this._applicationId = options.applicationId;
|
|
34
|
-
this.
|
|
38
|
+
this.setPublicApiKey(options.publicApiKey);
|
|
35
39
|
this._environment = options.environment ?? process.env.NODE_ENV ?? "development";
|
|
36
40
|
this._context = options.context;
|
|
37
41
|
this._throwErrors = options.throwErrors ?? false;
|
|
38
42
|
this._uris = options.uris;
|
|
39
43
|
}
|
|
44
|
+
/**
|
|
45
|
+
* Get the application ID
|
|
46
|
+
* @returns {string}
|
|
47
|
+
*/
|
|
40
48
|
get applicationId() {
|
|
41
49
|
return this._applicationId;
|
|
42
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Set the application ID
|
|
53
|
+
* @param {string} value
|
|
54
|
+
*/
|
|
43
55
|
set applicationId(value) {
|
|
44
56
|
this._applicationId = value;
|
|
45
57
|
}
|
|
46
|
-
|
|
47
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Get the public API key
|
|
60
|
+
* @returns {string}
|
|
61
|
+
*/
|
|
62
|
+
get publicApiKey() {
|
|
63
|
+
return this._publicApiKey;
|
|
48
64
|
}
|
|
49
|
-
|
|
50
|
-
|
|
65
|
+
/**
|
|
66
|
+
* Set the public API key
|
|
67
|
+
* @param {string} value
|
|
68
|
+
*/
|
|
69
|
+
set publicApiKey(value) {
|
|
70
|
+
this.setPublicApiKey(value);
|
|
51
71
|
}
|
|
72
|
+
/**
|
|
73
|
+
* Get the environment
|
|
74
|
+
* @returns {string}
|
|
75
|
+
*/
|
|
52
76
|
get environment() {
|
|
53
77
|
return this._environment;
|
|
54
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* Set the environment
|
|
81
|
+
* @param {string} value
|
|
82
|
+
*/
|
|
55
83
|
set environment(value) {
|
|
56
84
|
this._environment = value;
|
|
57
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Get the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
88
|
+
* @returns {boolean}
|
|
89
|
+
*/
|
|
58
90
|
get throwErrors() {
|
|
59
91
|
return this._throwErrors;
|
|
60
92
|
}
|
|
93
|
+
/**
|
|
94
|
+
* Set the throwErrors. If true, errors will be thrown in addition to being emitted.
|
|
95
|
+
* @param {boolean} value
|
|
96
|
+
*/
|
|
61
97
|
set throwErrors(value) {
|
|
62
98
|
this._throwErrors = value;
|
|
63
99
|
}
|
|
100
|
+
/**
|
|
101
|
+
* Get the current context. This is the default context used. You can override this at the get function level.
|
|
102
|
+
* @returns {ToggleContext}
|
|
103
|
+
*/
|
|
64
104
|
get context() {
|
|
65
105
|
return this._context;
|
|
66
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
109
|
+
* @param {ToggleContext} value
|
|
110
|
+
*/
|
|
67
111
|
set context(value) {
|
|
68
112
|
this._context = value;
|
|
69
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Get the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
116
|
+
* @returns {Array<string>}
|
|
117
|
+
*/
|
|
70
118
|
get uris() {
|
|
71
119
|
return this._uris;
|
|
72
120
|
}
|
|
121
|
+
/**
|
|
122
|
+
* Set the URIs. This is used to override the default URIs for testing or if you are using a self-hosted version.
|
|
123
|
+
* @param {Array<string>} value
|
|
124
|
+
*/
|
|
73
125
|
set uris(value) {
|
|
74
126
|
this._uris = value;
|
|
75
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* 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,
|
|
131
|
+
* 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
|
|
134
|
+
*/
|
|
135
|
+
setPublicApiKey(key) {
|
|
136
|
+
if (!key.startsWith("public_")) {
|
|
137
|
+
this.emit("error", new Error("Public API key should start with public_"));
|
|
138
|
+
if (process.env.NODE_ENV !== "production") {
|
|
139
|
+
console.error("Public API key should start with public_");
|
|
140
|
+
}
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
this._publicApiKey = key;
|
|
144
|
+
this._client = void 0;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Set the context. This is the default context used. You can override this at the get function level.
|
|
148
|
+
* @param {ToggleContext} context
|
|
149
|
+
*/
|
|
76
150
|
setContext(context) {
|
|
77
151
|
this._context = context;
|
|
78
152
|
this._client = void 0;
|
|
79
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* Helper function to get the client. This will create a new client if one does not exist. It will also set the
|
|
156
|
+
* application ID, environment, and URIs if they are not set. This is used by the get function to get the client.
|
|
157
|
+
* This is normally only used internally.
|
|
158
|
+
* @returns {Promise<Client>}
|
|
159
|
+
*/
|
|
80
160
|
async getClient() {
|
|
81
161
|
if (!this._client) {
|
|
82
162
|
const options = {
|
|
@@ -84,11 +164,19 @@ var Toggle = class extends Hookified {
|
|
|
84
164
|
environment: this._environment,
|
|
85
165
|
horizonUrls: this._uris
|
|
86
166
|
};
|
|
87
|
-
await OpenFeature.setProviderAndWait(new HyphenProvider(this.
|
|
167
|
+
await OpenFeature.setProviderAndWait(new HyphenProvider(this._publicApiKey, options));
|
|
88
168
|
this._client = OpenFeature.getClient(this._context);
|
|
89
169
|
}
|
|
90
170
|
return this._client;
|
|
91
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* This is the main function to get a feature flag value. It will check the type of the default value and call the
|
|
174
|
+
* appropriate function. It will also set the context if it is not set.
|
|
175
|
+
* @param {string} key - The key of the feature flag
|
|
176
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
177
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
178
|
+
* @returns {Promise<T>}
|
|
179
|
+
*/
|
|
92
180
|
async get(key, defaultValue, options) {
|
|
93
181
|
switch (typeof defaultValue) {
|
|
94
182
|
case "boolean": {
|
|
@@ -105,6 +193,14 @@ var Toggle = class extends Hookified {
|
|
|
105
193
|
}
|
|
106
194
|
}
|
|
107
195
|
}
|
|
196
|
+
/**
|
|
197
|
+
* Get a boolean value from the feature flag. This will check the type of the default value and call the
|
|
198
|
+
* appropriate function. It will also set the context if it is not set.
|
|
199
|
+
* @param {string} key - The key of the feature flag
|
|
200
|
+
* @param {boolean} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
201
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
202
|
+
* @returns {Promise<boolean>} - The value of the feature flag
|
|
203
|
+
*/
|
|
108
204
|
async getBoolean(key, defaultValue, options) {
|
|
109
205
|
try {
|
|
110
206
|
const data = {
|
|
@@ -131,6 +227,13 @@ var Toggle = class extends Hookified {
|
|
|
131
227
|
}
|
|
132
228
|
return defaultValue;
|
|
133
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Get a string value from the feature flag.
|
|
232
|
+
* @param {string} key - The key of the feature flag
|
|
233
|
+
* @param {string} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
234
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
235
|
+
* @returns {Promise<string>} - The value of the feature flag
|
|
236
|
+
*/
|
|
134
237
|
async getString(key, defaultValue, options) {
|
|
135
238
|
try {
|
|
136
239
|
const data = {
|
|
@@ -183,6 +286,14 @@ var Toggle = class extends Hookified {
|
|
|
183
286
|
}
|
|
184
287
|
return defaultValue;
|
|
185
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Get an object value from the feature flag. This will check the type of the default value and call the
|
|
291
|
+
* appropriate function. It will also set the context if it is not set.
|
|
292
|
+
* @param {string} key - The key of the feature flag
|
|
293
|
+
* @param {T} defaultValue - The default value to return if the feature flag is not set or does not evaluate.
|
|
294
|
+
* @param {ToggleRequestOptions} options - The options to use for the request. This can be used to override the context.
|
|
295
|
+
* @returns {Promise<T>} - The value of the feature flag
|
|
296
|
+
*/
|
|
186
297
|
async getObject(key, defaultValue, options) {
|
|
187
298
|
try {
|
|
188
299
|
const data = {
|