@flipt-io/flipt-client-js 0.0.1 → 0.0.3

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,40 +39,6 @@ const variant = client.evaluateVariant({
39
39
  console.log(variant);
40
40
  ```
41
41
 
42
- ### Edge Function Usage (e.g., Vercel Edge)
43
-
44
- ```typescript
45
- import { FliptClient } from '@flipt-io/flipt-client-js';
46
-
47
- export const config = {
48
- runtime: 'edge'
49
- };
50
-
51
- export default async function middleware(req) {
52
- const client = await FliptClient.init({
53
- namespace: 'default',
54
- url: process.env.FLIPT_URL,
55
- authentication: {
56
- clientToken: process.env.FLIPT_AUTH_TOKEN
57
- }
58
- });
59
-
60
- const result = client.evaluateBoolean({
61
- flagKey: 'my-flag',
62
- entityId: 'user-123',
63
- context: {
64
- country: 'US'
65
- }
66
- });
67
-
68
- if (result.enabled) {
69
- return new Response('Feature enabled!');
70
- }
71
-
72
- return new Response('Feature disabled');
73
- }
74
- ```
75
-
76
42
  ### Initialization Arguments
77
43
 
78
44
  The `FliptClient` constructor accepts the following optional arguments:
@@ -83,7 +49,7 @@ The `FliptClient` constructor accepts the following optional arguments:
83
49
  - `updateInterval`: **Node.js Only** The interval (in seconds) in which to fetch new flag state. If not provided, the client will default to 120 seconds.
84
50
  - `authentication`: The authentication strategy to use when communicating with the upstream Flipt instance. If not provided, the client will default to no authentication. See the [Authentication](#authentication) section for more information.
85
51
  - `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.
86
- - `fetcher`: An implementation of a [fetcher](https://github.com/flipt-io/flipt-client-sdks/blob/4821cb227c6c8b10419b96674d44ad1d6668a647/flipt-client-browser/src/models.ts#L5) 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.
52
+ - `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.
87
53
  - `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.
88
54
 
89
55
  ### Authentication
@@ -118,7 +84,7 @@ const client = await FliptClient.init({
118
84
  });
119
85
  ```
120
86
 
121
- The fetcher is a function that takes an optional [`IFetcherOpts`](https://github.com/flipt-io/flipt-client-sdks/blob/4821cb227c6c8b10419b96674d44ad1d6668a647/flipt-client-browser/src/models.ts#L1) argument and returns a `Promise` that resolves to a `Response` object.
87
+ 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.
122
88
 
123
89
  ## State Management
124
90
 
@@ -167,6 +133,128 @@ The default fetcher uses [ETag HTTP headers](https://developer.mozilla.org/en-US
167
133
 
168
134
  To disable ETag support, you can implement a [custom fetcher](#custom-fetcher) that does not use ETags.
169
135
 
136
+ ## Slim Module
137
+
138
+ For environments like Cloudflare Workers where WebAssembly.compile is not supported, we provide a "slim" version of the client that allows you to explicitly provide the WASM module.
139
+
140
+ ### Using the Slim Client
141
+
142
+ The slim client requires you to explicitly provide the WASM module when initializing the client. This gives you more control over how the WASM is loaded, which is essential for edge environments with specific limitations.
143
+
144
+ #### Direct Import (Recommended)
145
+
146
+ The simplest approach is to directly import the WASM file using the package export path:
147
+
148
+ ```js
149
+ // Works in all environments (Node.js, browsers, edge functions)
150
+ import { FliptClient } from '@flipt-io/flipt-client-js/slim';
151
+ import wasm from '@flipt-io/flipt-client-js/engine.wasm';
152
+
153
+ const client = await FliptClient.init(
154
+ {
155
+ namespace: 'default',
156
+ url: 'https://flipt.example.com'
157
+ },
158
+ { wasm }
159
+ );
160
+
161
+ // Use the client as normal
162
+ const result = client.evaluateBoolean({
163
+ flagKey: 'my-flag',
164
+ entityId: 'user-123',
165
+ context: { country: 'US' }
166
+ });
167
+ ```
168
+
169
+ #### Cloudflare Workers Example
170
+
171
+ When using Cloudflare Workers with Webpack or a similar bundler:
172
+
173
+ ```js
174
+ import { FliptClient } from '@flipt-io/flipt-client-js/slim';
175
+ import wasm from '@flipt-io/flipt-client-js/engine.wasm';
176
+
177
+ export default {
178
+ async fetch(request, env, ctx) {
179
+ // Initialize the client with the WASM module
180
+ const client = await FliptClient.init(
181
+ {
182
+ namespace: 'default',
183
+ url: 'https://flipt.example.com'
184
+ },
185
+ { wasm }
186
+ );
187
+
188
+ // Use the client as normal
189
+ const result = client.evaluateBoolean({
190
+ flagKey: 'my-flag',
191
+ entityId: 'user-123',
192
+ context: { country: 'US' }
193
+ });
194
+
195
+ return new Response(result.enabled ? 'Flag enabled' : 'Flag disabled');
196
+ }
197
+ };
198
+ ```
199
+
200
+ #### Next.js Edge Runtime Example
201
+
202
+ Using the slim client with Next.js Edge Runtime (middleware or edge API routes):
203
+
204
+ ```js
205
+ // middleware.js or an Edge API Route
206
+ import { FliptClient } from '@flipt-io/flipt-client-js/slim';
207
+ import wasm from '@flipt-io/flipt-client-js/engine.wasm';
208
+ import { NextResponse } from 'next/server';
209
+
210
+ export const config = {
211
+ runtime: 'edge'
212
+ };
213
+
214
+ export default async function middleware(req) {
215
+ // Initialize the client with the WASM module
216
+ const client = await FliptClient.init(
217
+ {
218
+ namespace: 'default',
219
+ url: process.env.FLIPT_URL || 'https://flipt.example.com',
220
+ authentication: {
221
+ clientToken: process.env.FLIPT_TOKEN
222
+ }
223
+ },
224
+ { wasm }
225
+ );
226
+
227
+ // Use the client to evaluate a flag
228
+ const result = client.evaluateBoolean({
229
+ flagKey: 'my-edge-feature',
230
+ entityId: 'user-123',
231
+ context: {
232
+ path: req.nextUrl.pathname,
233
+ country: req.geo?.country || 'unknown'
234
+ }
235
+ });
236
+
237
+ // Use the flag result to make decisions
238
+ if (result.enabled) {
239
+ if (req.nextUrl.pathname.startsWith('/api/')) {
240
+ // For API routes, modify the response
241
+ return new Response(JSON.stringify({ featureEnabled: true }), {
242
+ status: 200,
243
+ headers: { 'Content-Type': 'application/json' }
244
+ });
245
+ } else {
246
+ // For page routes, redirect or rewrite
247
+ const url = req.nextUrl.clone();
248
+ url.pathname = '/new-experience';
249
+ return NextResponse.rewrite(url);
250
+ }
251
+ }
252
+
253
+ // Continue normal request handling
254
+ return NextResponse.next();
255
+ }
256
+ ```
257
+
170
258
  ## Development
171
259
 
172
260
  ### WASM