@featbit/js-client-sdk 2.0.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/LICENSE +201 -0
- package/README.md +289 -0
- package/esm/constants.d.ts +7 -0
- package/esm/constants.js +8 -0
- package/esm/constants.js.map +1 -0
- package/esm/events.d.ts +14 -0
- package/esm/events.js +27 -0
- package/esm/events.js.map +1 -0
- package/esm/featbit.d.ts +34 -0
- package/esm/featbit.js +382 -0
- package/esm/featbit.js.map +1 -0
- package/esm/index.d.ts +4 -0
- package/esm/index.js +5 -0
- package/esm/index.js.map +1 -0
- package/esm/logger.d.ts +4 -0
- package/esm/logger.js +24 -0
- package/esm/logger.js.map +1 -0
- package/esm/network.service.d.ts +27 -0
- package/esm/network.service.js +288 -0
- package/esm/network.service.js.map +1 -0
- package/esm/optionMessages.d.ts +5 -0
- package/esm/optionMessages.js +16 -0
- package/esm/optionMessages.js.map +1 -0
- package/esm/queue.d.ts +8 -0
- package/esm/queue.js +35 -0
- package/esm/queue.js.map +1 -0
- package/esm/store.d.ts +20 -0
- package/esm/store.js +143 -0
- package/esm/store.js.map +1 -0
- package/esm/throttleutil.d.ts +9 -0
- package/esm/throttleutil.js +133 -0
- package/esm/throttleutil.js.map +1 -0
- package/esm/types.d.ts +94 -0
- package/esm/types.js +24 -0
- package/esm/types.js.map +1 -0
- package/esm/umd.d.ts +2 -0
- package/esm/utils.d.ts +11 -0
- package/esm/utils.js +142 -0
- package/esm/utils.js.map +1 -0
- package/package.json +48 -0
- package/src/constants.ts +7 -0
- package/src/events.ts +29 -0
- package/src/featbit.ts +343 -0
- package/src/index.ts +6 -0
- package/src/logger.ts +18 -0
- package/src/network.service.ts +223 -0
- package/src/optionMessages.ts +13 -0
- package/src/queue.ts +23 -0
- package/src/store.ts +169 -0
- package/src/throttleutil.ts +72 -0
- package/src/types.ts +113 -0
- package/src/umd.ts +15 -0
- package/src/utils.ts +173 -0
- package/umd/featbit-js-client-sdk-2.0.0.js +2 -0
- package/umd/featbit-js-client-sdk-2.0.0.js.map +1 -0
- package/umd/featbit-js-client-sdk.js +2 -0
- package/umd/featbit-js-client-sdk.js.map +1 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# FeatBit Client-Side SDK for JavaScript
|
|
2
|
+
|
|
3
|
+
## Introduction
|
|
4
|
+
|
|
5
|
+
This is the client side SDK for the feature management platform [FeatBit](https://github.com/featbit/featbit).
|
|
6
|
+
|
|
7
|
+
Be aware, this is a client side SDK, it is intended for use in a single-user context, which can be mobile, desktop or embedded applications. This SDK can only be ran in a browser environment, it is not suitable for NodeJs applications.
|
|
8
|
+
|
|
9
|
+
## Get Started
|
|
10
|
+
|
|
11
|
+
### Installation
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @featbit/js-client-sdk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Prerequisite
|
|
18
|
+
|
|
19
|
+
Before using the SDK, you need to obtain the environment secret and SDK URLs.
|
|
20
|
+
|
|
21
|
+
Follow the documentation below to retrieve these values
|
|
22
|
+
|
|
23
|
+
- [How to get the environment secret](https://docs.featbit.co/sdk/faq#how-to-get-the-environment-secret)
|
|
24
|
+
- [How to get the SDK URLs](https://docs.featbit.co/sdk/faq#how-to-get-the-sdk-urls)
|
|
25
|
+
|
|
26
|
+
### Quick Start
|
|
27
|
+
|
|
28
|
+
The following code demonstrates:
|
|
29
|
+
1. Initialize the SDK
|
|
30
|
+
2. Evaluate flag
|
|
31
|
+
3. Subscribe to flag change
|
|
32
|
+
|
|
33
|
+
```javascript
|
|
34
|
+
import fbClient from '@featbit/js-client-sdk';
|
|
35
|
+
|
|
36
|
+
const option = {
|
|
37
|
+
secret: "your env secret",
|
|
38
|
+
api:"http://localhost:5100", // the Streaming URL
|
|
39
|
+
user: {
|
|
40
|
+
name: "Bot",
|
|
41
|
+
keyId: "bot-id",
|
|
42
|
+
customizedProperties: [
|
|
43
|
+
{
|
|
44
|
+
"name": "level",
|
|
45
|
+
"value": "high"
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// initialization
|
|
52
|
+
fbClient.init(option);
|
|
53
|
+
|
|
54
|
+
// evaluation
|
|
55
|
+
const flagValue = fbClient.variation("YOUR_FEATURE_KEY", defaultValue);
|
|
56
|
+
|
|
57
|
+
// subscribe to flag change
|
|
58
|
+
fbClient.on('ff_update:YOUR_FEATURE_KEY', (change) => {
|
|
59
|
+
// change has this structure {id: 'the feature_flag_key', oldValue: theOldValue, newValue: theNewValue }
|
|
60
|
+
// the type of theOldValue and theNewValue is defined on FeatBit
|
|
61
|
+
|
|
62
|
+
// defaultValue should have the same type as theOldValue and theNewValue
|
|
63
|
+
const myFeature = fbClient.variation('YOUR_FEATURE_KEY', defaultValue);
|
|
64
|
+
});
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Examples
|
|
68
|
+
|
|
69
|
+
- [Vue](https://github.com/featbit/featbit-samples/tree/main/samples/dino-game/interactive-demo-vue)
|
|
70
|
+
- For how to use FeatBit with React, please refer to the [react-client-sdk](https://github.com/featbit/featbit-react-client-sdk)
|
|
71
|
+
|
|
72
|
+
## SDK
|
|
73
|
+
|
|
74
|
+
### Initialization
|
|
75
|
+
|
|
76
|
+
Before initializing the SDK, you need to get the client-side env secret of your environment from FeatBit.
|
|
77
|
+
|
|
78
|
+
```javascript
|
|
79
|
+
const option = {
|
|
80
|
+
secret: "your env secret",
|
|
81
|
+
streamingUri:"ws://localhost:5100",
|
|
82
|
+
eventsUri:"http://localhost:5100",
|
|
83
|
+
user: {
|
|
84
|
+
name: "Bot",
|
|
85
|
+
keyId: "bot-id",
|
|
86
|
+
customizedProperties: [
|
|
87
|
+
{
|
|
88
|
+
"name": "level",
|
|
89
|
+
"value": "high"
|
|
90
|
+
}
|
|
91
|
+
]
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
fbClient.init(option);
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
The user has three properties:
|
|
99
|
+
- name(**requried**): The user's name, useful when viewing users in the portal.
|
|
100
|
+
- keyId(**requried**): The unique user identifier.
|
|
101
|
+
- streamingUri: The streaming URL.
|
|
102
|
+
- eventsUri: The events URL.
|
|
103
|
+
- customizedProperties(**optional**): Any other customized properties. Users can be targeted by these customized properties. Here is the format definition:
|
|
104
|
+
```json
|
|
105
|
+
[
|
|
106
|
+
{
|
|
107
|
+
"name": "the name of the property",
|
|
108
|
+
"value": "the value of the property"
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
This table lists all available options
|
|
114
|
+
|
|
115
|
+
| Options | Defaults | Description |
|
|
116
|
+
|-----------------------|--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
117
|
+
| secret (**required**) | - | The client side secret of your environment. |
|
|
118
|
+
| user (**required**) | - | The user connected to your APP, can be ignored if anonymous equals to true. |
|
|
119
|
+
| anonymous | `false` | Set to true if you want to use a anonymous user, which is the case before user login to your APP. If that is your case, the user can be set later with the identify method after the user has logged in. |
|
|
120
|
+
| enableDataSync | `true` | Set to false if you do not want to sync data with remote server, in this case feature flags must be set to bootstrap option or be passed to the method bootstrap. |
|
|
121
|
+
| bootstrap | `[ ]` | Init the SDK with feature flags, this will trigger the ready event immediately instead of requesting from the remote. |
|
|
122
|
+
| streamingUri | `` | The evaluation server streaming URL. |
|
|
123
|
+
| eventsUri | `` | The evaluation server events URL. |
|
|
124
|
+
| appType | `javascript` | The app type. |
|
|
125
|
+
|
|
126
|
+
### Bootstrap
|
|
127
|
+
If you already have the feature flags available, two ways to pass them to the SDK instead of requesting from the remote.
|
|
128
|
+
- By the **init** method
|
|
129
|
+
|
|
130
|
+
```javascript
|
|
131
|
+
// define the option with the bootstrap parameter
|
|
132
|
+
const option = {
|
|
133
|
+
...
|
|
134
|
+
// the array should contain all your feature flags
|
|
135
|
+
bootstrap = [{
|
|
136
|
+
// feature flag key name
|
|
137
|
+
id: string,
|
|
138
|
+
// variation value
|
|
139
|
+
variation: string,
|
|
140
|
+
// variation data type, string will be used if not specified
|
|
141
|
+
variationType: VariationDataType
|
|
142
|
+
}],
|
|
143
|
+
...
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
fbClient.init(option);
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
- By the **bootstrap** method
|
|
150
|
+
|
|
151
|
+
```javascript
|
|
152
|
+
// this array should contain all your feature flags
|
|
153
|
+
const featureflags = [{
|
|
154
|
+
// feature flag key name
|
|
155
|
+
id: string,
|
|
156
|
+
// variation value
|
|
157
|
+
variation: string,
|
|
158
|
+
// variation data type, string will used if not specified
|
|
159
|
+
variationType: VariationDataType
|
|
160
|
+
}]
|
|
161
|
+
|
|
162
|
+
fbClient.bootstrap(featureflags);
|
|
163
|
+
```
|
|
164
|
+
|
|
165
|
+
**If you want to disable the synchronization with remote server, set enableDataSync to false in option**. In this case, bootstrap option must be set or bootstrap method must be called with feature flags.
|
|
166
|
+
|
|
167
|
+
### Evaluation
|
|
168
|
+
|
|
169
|
+
After initialization, the SDK has all the feature flags locally, and it does not need to request the remote server for any feature flag evaluation. All evaluation is done locally and synchronously, the average evaluation time is about **1** ms.
|
|
170
|
+
|
|
171
|
+
```javascript
|
|
172
|
+
// Use this method for all cases
|
|
173
|
+
// This method supports type inspection, it returns the value with the type defined on FeatBit,
|
|
174
|
+
// so defaultValue should have the same type as defined on FeatBit
|
|
175
|
+
const flagValue = fbClient.variation("YOUR_FEATURE_KEY", defaultValue);
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Events
|
|
179
|
+
|
|
180
|
+
#### Wait for ready
|
|
181
|
+
|
|
182
|
+
To find out when the client is ready, you can use one of two mechanisms: events or promises.
|
|
183
|
+
|
|
184
|
+
The client object can emit JavaScript events. It emits a ready event when it receives initial flag values from the server. You can listen for this event to determine when the client is ready to evaluate flags.
|
|
185
|
+
|
|
186
|
+
```javascript
|
|
187
|
+
fbClient.on('ready', (data) => {
|
|
188
|
+
// data has the following structure [ {id: 'featureFlagKey', variation: variationValue} ]
|
|
189
|
+
// variationValue has the type as defined on remote
|
|
190
|
+
var flagValue = fbClient.variation("YOUR_FEATURE_KEY", defaultValue);
|
|
191
|
+
});
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
Or, you can use a promise instead of an event. The SDK has a method that return a promise for initialization: waitUntilReady(). The behavior of waitUntilReady() is equivalent to the ready event. The promise resolves when the client receives its initial flag data. As with all promises, you can either use .then() to provide a callback, or use await if you are writing asynchronous code.
|
|
195
|
+
|
|
196
|
+
```javascript
|
|
197
|
+
fbClient.waitUntilReady().then((data) => {
|
|
198
|
+
// data has the following structure [ {id: 'featureFlagKey', variation: variationValue } ]
|
|
199
|
+
// variationValue has the type as defined on remote
|
|
200
|
+
// initialization succeeded, flag values are now available
|
|
201
|
+
});
|
|
202
|
+
// or, with await:
|
|
203
|
+
const featureFlags = await fbClient.waitUntilReady();
|
|
204
|
+
// initialization succeeded, flag values are now available
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
The SDK only decides initialization has failed if it receives an error response indicating that the environment ID is invalid. If it has trouble connecting to feature-flags.co, it will keep retrying until it succeeds.
|
|
208
|
+
|
|
209
|
+
#### Subscribe to flag(s) changes
|
|
210
|
+
|
|
211
|
+
To get notified when a feature flag is changed, we offer two methods
|
|
212
|
+
- subscribe to the changes of any feature flag(s)
|
|
213
|
+
|
|
214
|
+
```javascript
|
|
215
|
+
fbClient.on('ff_update', (changes) => {
|
|
216
|
+
// changes has this structure [{id: 'the feature_flag_key', oldValue: theOldValue, newValue: theNewValue }]
|
|
217
|
+
// the type of theOldValue and theNewValue is defined on FeatBit
|
|
218
|
+
|
|
219
|
+
// do something when any feature flag changes
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
```
|
|
223
|
+
- subscribe to the changes of a specific feature flag
|
|
224
|
+
|
|
225
|
+
```javascript
|
|
226
|
+
// replace feature_flag_key with your feature flag key
|
|
227
|
+
fbClient.on('ff_update:feature_flag_key', (change) => {
|
|
228
|
+
// change has this structure {id: 'the feature_flag_key', oldValue: theOldValue, newValue: theNewValue }
|
|
229
|
+
// the type of theOldValue and theNewValue is defined on FeatBit
|
|
230
|
+
|
|
231
|
+
// defaultValue should have the same type as theOldValue and theNewValue
|
|
232
|
+
// this is the prefered way than calling change.newValue as each time you call fbClient.variation,
|
|
233
|
+
// the insight data is sent to server automatically
|
|
234
|
+
const myFeature = fbClient.variation('feature_flag_key', defaultValue);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
### Switch user after initialization
|
|
240
|
+
If the user parameter cannot be passed by the init method, the following method can be used to set the user after initialization.
|
|
241
|
+
|
|
242
|
+
```javascript
|
|
243
|
+
await fbClient.identify(user);
|
|
244
|
+
```
|
|
245
|
+
|
|
246
|
+
We can manually call the method logout, which will switch the current user back to anonymous user if exists already or create a new anonymous user.
|
|
247
|
+
|
|
248
|
+
```javascript
|
|
249
|
+
fbClient.logout(user);
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Data synchronization
|
|
253
|
+
|
|
254
|
+
We use websocket to make the local data synchronized with the server, and then store them in memory by default. Whenever there is any change to a feature flag or its related data, this change will be pushed to the SDK, the average synchronization time is less than 100ms. Be aware the websocket connection may be interrupted due to internet outage, but it will be resumed automatically once the problem is gone.
|
|
255
|
+
|
|
256
|
+
### Network failure handling
|
|
257
|
+
|
|
258
|
+
As all data is stored locally in the localStorage, in the following situations, the SDK would still work when there is temporarily no internet connection:
|
|
259
|
+
- it has already received the data from previous connections
|
|
260
|
+
- the fbClient.bootstrap(featureFlags) method is called with all necessary feature flags
|
|
261
|
+
|
|
262
|
+
In the meantime, the SDK would try to reconnect to the server by an incremental interval, this makes sure that the websocket would be restored when the internet connection is back.
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
### Experiments (A/B/n Testing)
|
|
266
|
+
|
|
267
|
+
We support automatic experiments for page views and clicks, you just need to set your experiment on FeatBit portal, then you should be able to see the result in near real time after the experiment is started.
|
|
268
|
+
|
|
269
|
+
In case you need more control over the experiment data sent to our server, we offer a method to send custom event.
|
|
270
|
+
|
|
271
|
+
```javascript
|
|
272
|
+
fbClient.sendCustomEvent([{
|
|
273
|
+
eventName: 'your event name',
|
|
274
|
+
numericValue: 1
|
|
275
|
+
}])
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
**numericValue** is not mandatory, the default value is **1**.
|
|
279
|
+
|
|
280
|
+
Make sure sendCustomEvent is called after the related feature flag is called by simply calling **fbClient.variation('featureFlagKeyName', 'default value')**, otherwise, the custom event won't be included into the experiment result.
|
|
281
|
+
|
|
282
|
+
## Getting support
|
|
283
|
+
|
|
284
|
+
- If you have a specific question about using this SDK, we encourage you to [ask it in Slack](https://join.slack.com/t/featbit/shared_invite/zt-1ew5e2vbb-x6Apan1xZOaYMnFzqZkGNQ).
|
|
285
|
+
- If you encounter a bug or would like to request a feature, [submit an issue](https://github.com/featbit/featbit-js-client-sdk/issues).
|
|
286
|
+
|
|
287
|
+
## See Also
|
|
288
|
+
|
|
289
|
+
- [Connect To JavaScript Sdk](https://docs.featbit.co/getting-started/connect-an-sdk#javascript)
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const insightsFlushTopic = "insights.flush";
|
|
2
|
+
export declare const featureFlagEvaluatedTopic = "featureflag.evaluated.topic";
|
|
3
|
+
export declare const featureFlagEvaluatedBufferTopic = "featureflag.evaluated.buffer.topic";
|
|
4
|
+
export declare const websocketReconnectTopic = "network.websocket.reconnect";
|
|
5
|
+
export declare const debugModeQueryStr = "debugmode";
|
|
6
|
+
export declare const insightsTopic = "insights.topic";
|
|
7
|
+
export declare const currentUserStorageKey = "fb-user";
|
package/esm/constants.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export var insightsFlushTopic = 'insights.flush';
|
|
2
|
+
export var featureFlagEvaluatedTopic = 'featureflag.evaluated.topic';
|
|
3
|
+
export var featureFlagEvaluatedBufferTopic = 'featureflag.evaluated.buffer.topic';
|
|
4
|
+
export var websocketReconnectTopic = 'network.websocket.reconnect';
|
|
5
|
+
export var debugModeQueryStr = 'debugmode'; // will print debug logs if true
|
|
6
|
+
export var insightsTopic = 'insights.topic';
|
|
7
|
+
export var currentUserStorageKey = 'fb-user';
|
|
8
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,IAAM,kBAAkB,GAAG,gBAAgB,CAAC;AACnD,MAAM,CAAC,IAAM,yBAAyB,GAAG,6BAA6B,CAAC;AACvE,MAAM,CAAC,IAAM,+BAA+B,GAAG,oCAAoC,CAAC;AACpF,MAAM,CAAC,IAAM,uBAAuB,GAAG,6BAA6B,CAAC;AACrE,MAAM,CAAC,IAAM,iBAAiB,GAAG,WAAW,CAAC,CAAC,gCAAgC;AAC9E,MAAM,CAAC,IAAM,aAAa,GAAG,gBAAgB,CAAC;AAC9C,MAAM,CAAC,IAAM,qBAAqB,GAAG,SAAS,CAAC"}
|
package/esm/events.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
interface Events {
|
|
2
|
+
[key: string]: Function[];
|
|
3
|
+
}
|
|
4
|
+
declare class EventEmitter {
|
|
5
|
+
events: Events;
|
|
6
|
+
constructor(events?: Events);
|
|
7
|
+
subscribe(name: string, cb: Function): {
|
|
8
|
+
unsubscribe: () => Function[];
|
|
9
|
+
};
|
|
10
|
+
unsubscribe(name: string, cb: Function): void;
|
|
11
|
+
emit(name: string, ...args: any[]): void;
|
|
12
|
+
}
|
|
13
|
+
export declare const eventHub: EventEmitter;
|
|
14
|
+
export {};
|
package/esm/events.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
var EventEmitter = /** @class */ (function () {
|
|
2
|
+
function EventEmitter(events) {
|
|
3
|
+
this.events = events || {};
|
|
4
|
+
}
|
|
5
|
+
EventEmitter.prototype.subscribe = function (name, cb) {
|
|
6
|
+
var _this = this;
|
|
7
|
+
(this.events[name] || (this.events[name] = [])).push(cb);
|
|
8
|
+
return {
|
|
9
|
+
unsubscribe: function () {
|
|
10
|
+
return _this.events[name] && _this.events[name].splice(_this.events[name].indexOf(cb) >>> 0, 1);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
EventEmitter.prototype.unsubscribe = function (name, cb) {
|
|
15
|
+
this.events[name] && this.events[name].splice(this.events[name].indexOf(cb) >>> 0, 1);
|
|
16
|
+
};
|
|
17
|
+
EventEmitter.prototype.emit = function (name) {
|
|
18
|
+
var args = [];
|
|
19
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
20
|
+
args[_i - 1] = arguments[_i];
|
|
21
|
+
}
|
|
22
|
+
(this.events[name] || []).forEach(function (fn) { return fn.apply(void 0, args); });
|
|
23
|
+
};
|
|
24
|
+
return EventEmitter;
|
|
25
|
+
}());
|
|
26
|
+
export var eventHub = new EventEmitter();
|
|
27
|
+
//# sourceMappingURL=events.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.js","sourceRoot":"","sources":["../src/events.ts"],"names":[],"mappings":"AAIA;IAEE,sBAAY,MAAe;QACzB,IAAI,CAAC,MAAM,GAAG,MAAM,IAAI,EAAE,CAAC;IAC7B,CAAC;IAEM,gCAAS,GAAhB,UAAiB,IAAY,EAAE,EAAY;QAA3C,iBAOC;QANC,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;QAEzD,OAAO;YACL,WAAW,EAAE;gBACX,OAAA,KAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,KAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,KAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC;YAArF,CAAqF;SACxF,CAAC;IACJ,CAAC;IAEM,kCAAW,GAAlB,UAAmB,IAAY,EAAE,EAAY;QAC3C,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,EAAE,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;IACxF,CAAC;IAEM,2BAAI,GAAX,UAAY,IAAY;QAAE,cAAc;aAAd,UAAc,EAAd,qBAAc,EAAd,IAAc;YAAd,6BAAc;;QACtC,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC,OAAO,CAAC,UAAA,EAAE,IAAI,OAAA,EAAE,eAAI,IAAI,GAAV,CAAW,CAAC,CAAC;IACvD,CAAC;IACH,mBAAC;AAAD,CAAC,AAtBD,IAsBC;AAED,MAAM,CAAC,IAAM,QAAQ,GAAG,IAAI,YAAY,EAAE,CAAC"}
|
package/esm/featbit.d.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { FeatureFlagValue, ICustomEvent, IFeatureFlag, IFeatureFlagBase, IFeatureFlagSet, IOption, IUser } from "./types";
|
|
2
|
+
export declare class FB {
|
|
3
|
+
private _readyEventEmitted;
|
|
4
|
+
private _readyPromise;
|
|
5
|
+
private _insightsQueue;
|
|
6
|
+
private _featureFlagEvaluationBuffer;
|
|
7
|
+
private _option;
|
|
8
|
+
constructor();
|
|
9
|
+
on(name: string, cb: Function): void;
|
|
10
|
+
off(name: string, cb: Function): void;
|
|
11
|
+
waitUntilReady(): Promise<IFeatureFlagBase[]>;
|
|
12
|
+
init(option: IOption): Promise<void>;
|
|
13
|
+
identify(user: IUser): Promise<void>;
|
|
14
|
+
logout(): Promise<IUser>;
|
|
15
|
+
/**
|
|
16
|
+
* bootstrap with predefined feature flags.
|
|
17
|
+
* @param {array} featureFlags the predefined feature flags.
|
|
18
|
+
* @param {boolean} forceFullFetch if a forced full fetch should be made.
|
|
19
|
+
* @return {Promise<void>} nothing.
|
|
20
|
+
*/
|
|
21
|
+
bootstrap(featureFlags?: IFeatureFlag[], forceFullFetch?: boolean): Promise<void>;
|
|
22
|
+
private dataSync;
|
|
23
|
+
variation(key: string, defaultResult: FeatureFlagValue): FeatureFlagValue;
|
|
24
|
+
/**
|
|
25
|
+
* deprecated, you should use variation method directly
|
|
26
|
+
*/
|
|
27
|
+
boolVariation(key: string, defaultResult: boolean): boolean;
|
|
28
|
+
getUser(): IUser;
|
|
29
|
+
sendCustomEvent(data: ICustomEvent[]): void;
|
|
30
|
+
sendFeatureFlagInsight(key: string, variation: string): void;
|
|
31
|
+
getAllFeatureFlags(): IFeatureFlagSet;
|
|
32
|
+
}
|
|
33
|
+
declare const _default: FB;
|
|
34
|
+
export default _default;
|