@newskit-render/feature-flags 1.9.1 → 1.10.0-alpha.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
@@ -11,31 +11,20 @@ There are two ways that feature flags can be utilized.
11
11
 
12
12
  ### getFeatureFlags() in getServerSideProps
13
13
 
14
- The most performant way to use feature flags is to call `getFeatureFlags()` in `getServersideProps` and then use them throughout the page. This way the implementation will only be available for the current page and and wouldn't effect the rest of the site.
15
- To do so you will need to initialize the package by calling `createFeatureFlagsInstance` with the sdkKey from optimizely.
14
+ The most performant way to use feature flags is to call `getFeatureFlags()` or `getFeatureFlagsByKeys()` in `getServerSideProps` and then use them throughout the page. This way the implementation will only be available for the current page and and wouldn't effect the rest of the site.
16
15
  Best practice is to store your sdk key in environment variables, the examples below utilize that method.
17
16
  For local use, you cadd the Optimizely SDK key from your project in your `.env.local` file like so
18
17
  `OPTIMIZELY_SDK_KEY="123"`.
19
18
  In case you don't have project or access to the Optimizely platform please contact the Experimentation team.
20
- Optionally the config can be switched to suit your need. More on this can be found [here](#createFeatureFlagsInstance).<br /> <br />
21
19
  An example implementation would be as follows:
22
20
 
23
21
  `pages/index.ts`
24
22
 
25
23
  ```
26
- import {getFeatureFlags, createFeatureFlagsInstance} from '@newskit-render/feature-flags';
24
+ import { getFeatureFlags } from '@newskit-render/feature-flags';
27
25
 
28
26
  export async function getServerSideProps(context) {
29
- // code
30
- createFeatureFlagsInstance({ optimizelyConfig: { sdkKey: process.env.OPTIMIZELY_SDK_KEY } })
31
- // code
32
-
33
- const [resultFromOtherQueries, featureFlagsResult] = await Promise.allSettled([
34
- // other queries,
35
- getFeatureFlags()
36
- ])
37
-
38
- const featureFlags = featureFlagsResult.value;
27
+ const featureFlags = await getAllFeatureFlags(config)
39
28
 
40
29
  return {
41
30
  props: {
@@ -46,16 +35,13 @@ export async function getServerSideProps(context) {
46
35
  }
47
36
  ```
48
37
 
49
- The package also provides a helper function that conbines getFeatureFlags, createFeatureFlagsInstance in one.
38
+ The package also provides a function that is going to return only selected flags
50
39
 
51
40
  ```
52
- import {initAndGetFeatureFlag} from '@newskit-render/feature-flags';
41
+ import { getFeatureFlagsByKeys } from '@newskit-render/feature-flags';
53
42
 
54
43
  export async function getServerSideProps(context) {
55
- const [resultFromOtherQueries, featureFlags] = await Promise.allSettled([
56
- // other queries,
57
- initAndGetFeatureFlag(process.env.OPTIMIZELY_SDK_KEY)
58
- ])
44
+ const featureFlags = getFeatureFlagsByKeys(['flag_1', 'flag_2'], config)
59
45
 
60
46
  return {
61
47
  props: {
@@ -66,32 +52,6 @@ export async function getServerSideProps(context) {
66
52
  }
67
53
  ```
68
54
 
69
- `samplePage/index.tsx`
70
-
71
- ```
72
- import React from 'react'
73
- import { FeatureFlag } from '@newskit-render/feature-flags'
74
-
75
- const SectionPage: React.FC<{
76
- page: Page
77
- // pageprops...
78
- featureFlags?: FeatureFlag[]
79
- }> = ({ page, pageprops..., featureFlags }) => {
80
-
81
- return(
82
- <Layout>
83
- {featureFlags && featureFlags['test_flag'] && <p>FEATURE FLAG IS HEREEE</p>}
84
- <Cell>
85
- // ...content
86
- </Cell>
87
- </Layout>
88
- )}
89
-
90
- export default SamplePage
91
-
92
- ```
93
-
94
- ### hooks in getInitialProps
95
55
 
96
56
  Alternatively, feature flags can be applied on the whole app. To do so, you'll need to instantiate the package in `getInitialProps` of the main app, then call `getFeatureFlags` and pass the result to the whole app.
97
57
  By calling the `useFeatureFlagsContext` hook, the list of featureFlags can be accessed from any point of the site.
@@ -147,9 +107,39 @@ const Header: React.FC<{ user: UserData }> = ({ user }) => {
147
107
  export default Header
148
108
  ```
149
109
 
150
- ### createFeatureFlagsInstance
110
+ ### config argument
111
+
112
+ The `config` object is optional. It can be added as an argument to both `getFeatureFlags()` and `getFeatureFlagsByKeys()` functions.
113
+ By default the package is going to get the `OPTIMIZELY_SDK_KEY` directly from the environment variables but the key can be overwritten with a key
114
+ added in the config.
115
+
116
+ The configurations that can be passed to Optimizely in the `config` object is:
117
+
118
+ ```
119
+ sdkConfig?: {
120
+ datafileOptions?: DatafileOptions;
121
+ eventBatchSize?: number;
122
+ eventFlushInterval?: number;
123
+ eventMaxQueueSize?: number;
124
+ sdkKey?: string;
125
+ odpOptions?: OdpOptions;
126
+ persistentCacheProvider?: PersistentCacheProvider;
127
+ }
128
+ logLevel?: 'critical' | 'error' | 'warning' | 'debug' | 'info'
129
+ userData?: {
130
+ userId?: string
131
+ attributes?: {
132
+ [name: string]: UserAttributeValue
133
+ }
134
+ }
135
+ defaultFeatureFlags?: Flags
136
+ includeFlagVariables?: boolean
137
+ decideOptions?: OptimizelyDecideOption[]
138
+ ```
151
139
 
152
- `createFeatureFlagsInstance` takes config object as parameter. The config object consists of [`optimizelyConfig`](https://docs.developers.optimizely.com/full-stack/v4.0/docs/initialize-sdk-javascript-node#section-parameters), `userId`, `defaultFeatureFlags`, `logLevel`. The only requirement for the feature flags to be instantiated is passing optimizely sdk key to `optimizelyConfig`. Further, the whole config can be changed to suit your needs.
153
140
  `userId` serves as optimizely user identity.
141
+ `attributes` is a map of custom key-value pairs specifying attributes for the user that are used for audience targeting. Detailed information can be found in the [`Optimizely docs`](https://docs.developers.optimizely.com/feature-experimentation/docs/optimizelyusercontext-javascript-node)
154
142
  `defaultFeatureFlags` are used in the event that optimizely doesn't load up and initial values are required.
155
143
  `logLevel` serves to configure the optimizely logger if you wish to use it. It accepts `critical`, `error`, `warning`, `debug` or `info`
144
+ `decideOptions` is an array of OptimizelyDecideOption enums. Detailed information can be found in the [`Optimizely docs`](https://docs.developers.optimizely.com/feature-experimentation/docs/decide-methods-javascript-node)
145
+ `includeFlagVariables` with default of false can be used when you need additional flag information like `variationKey`, `enabled` and `variables`.