@flipt-io/flipt-client-js 0.0.2 → 0.0.4
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 +122 -34
- package/dist/browser/flipt_engine_wasm_js_bg.wasm +0 -0
- package/dist/browser/index.cjs +1 -2
- package/dist/browser/index.mjs +1 -2
- package/dist/flipt_engine_wasm_js_bg.wasm +0 -0
- package/dist/node/flipt_engine_wasm_js_bg.wasm +0 -0
- package/dist/node/index.cjs +1 -2
- package/dist/node/index.mjs +1 -2
- package/dist/{browser/flipt_engine_wasm_js.js → slim/index.cjs} +284 -40
- package/dist/{node/flipt_engine_wasm_js.js → slim/index.mjs} +279 -39
- package/dist/types/slim/index.d.ts +31 -0
- package/package.json +17 -8
- package/dist/browser/flipt_engine_wasm_js.d.ts +0 -53
- package/dist/browser/flipt_engine_wasm_js_bg.wasm.d.ts +0 -18
- package/dist/browser/index.cjs.map +0 -1
- package/dist/browser/index.mjs.map +0 -1
- package/dist/node/flipt_engine_wasm_js.d.ts +0 -53
- package/dist/node/flipt_engine_wasm_js_bg.wasm.d.ts +0 -18
- package/dist/node/index.cjs.map +0 -1
- package/dist/node/index.mjs.map +0 -1
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:
|
|
@@ -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
|
|
Binary file
|