@flipt-io/flipt-client-js 0.2.0 → 0.4.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
@@ -39,6 +39,20 @@ const variant = client.evaluateVariant({
39
39
  console.log(variant);
40
40
  ```
41
41
 
42
+ ### Importing Types
43
+
44
+ All types are available from the main package export:
45
+
46
+ ```typescript
47
+ import type { Flag, ClientOptions, EvaluationRequest } from '@flipt-io/flipt-client-js';
48
+ ```
49
+
50
+ Alternatively, you can import types from the dedicated `/types` subpath:
51
+
52
+ ```typescript
53
+ import type { Flag, ClientOptions, EvaluationRequest } from '@flipt-io/flipt-client-js/types';
54
+ ```
55
+
42
56
  ### Initialization Arguments
43
57
 
44
58
  The `FliptClient` constructor accepts the following optional arguments:
@@ -52,6 +66,7 @@ The `FliptClient` constructor accepts the following optional arguments:
52
66
  - `reference`: The [reference](https://docs.flipt.io/guides/user/using-references) to use when fetching flag state. If not provided, reference will not be used.
53
67
  - `fetcher`: An implementation of a [fetcher](https://github.com/flipt-io/flipt-client-sdks/blob/d0869dc9f6b3a65052712926b88fa0f9b18defaa/flipt-client-js/src/core/types.ts#L60-L62) interface to use when requesting flag state. If not provided, a default fetcher using the browser's [`fetch` API](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API) will be used.
54
68
  - `errorStrategy`: The error strategy to use when fetching flag state. If not provided, the client will default to fail. See the [Error Strategies](#error-strategies) section for more information.
69
+ - `hook`: A hook implementation for before and after evaluation callbacks. See the [Hooks](#hooks) section for more information.
55
70
 
56
71
  ### Authentication
57
72
 
@@ -87,6 +102,60 @@ const client = await FliptClient.init({
87
102
 
88
103
  The fetcher is a function that takes an optional [`IFetcherOpts`](https://github.com/flipt-io/flipt-client-sdks/blob/d0869dc9f6b3a65052712926b88fa0f9b18defaa/flipt-client-js/src/core/types.ts#L50-L55) argument and returns a `Promise` that resolves to a `Response` object.
89
104
 
105
+ ### Hooks
106
+
107
+ The `FliptClient` supports hooks that allow you to execute custom code before and after flag evaluation. This is useful for logging, metrics collection, or other observability patterns.
108
+
109
+ #### Hook Interface
110
+
111
+ To use hooks, implement the `Hook` interface.
112
+
113
+ #### Example: Logging Hook
114
+
115
+ ```typescript
116
+ import { FliptClient } from '@flipt-io/flipt-client-js';
117
+
118
+ class LoggingHook {
119
+ before(data: { flagKey: string }) {
120
+ console.log(`Evaluating flag: ${data.flagKey}`);
121
+ }
122
+
123
+ after(data: {
124
+ flagKey: string;
125
+ flagType: string;
126
+ value: string;
127
+ reason: string;
128
+ segmentKeys: string[];
129
+ }) {
130
+ console.log(
131
+ `Flag ${data.flagKey} evaluated: type=${data.flagType}, value=${data.value}, reason=${data.reason}`
132
+ );
133
+ }
134
+ }
135
+
136
+ const client = await FliptClient.init({
137
+ namespace: 'default',
138
+ url: 'http://localhost:8080',
139
+ hook: new LoggingHook()
140
+ });
141
+
142
+ const result = client.evaluateVariant({
143
+ flagKey: 'my-flag',
144
+ entityId: 'user-123',
145
+ context: { environment: 'production' }
146
+ });
147
+
148
+ console.log(result);
149
+ ```
150
+
151
+ The hooks are called for all evaluation methods:
152
+
153
+ - `evaluateVariant`: Called once per evaluation
154
+ - `evaluateBoolean`: Called once per evaluation
155
+ - `evaluateBatch`: Called once before for each request, and once after for each successful response
156
+
157
+ **Note:** Hooks are optional. If no hook is provided, evaluations proceed normally without any callbacks.
158
+
90
159
  ## State Management
91
160
 
92
161
  The `FliptClient` class pulls flag state from the Flipt instance at the `url` provided in the `options` object on instantiation.