@togglely/sdk-core 1.1.6 → 1.1.8
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 +4 -2
- package/dist/__tests__/cors-security.test.d.ts +15 -0
- package/dist/__tests__/cors-security.test.d.ts.map +1 -0
- package/dist/__tests__/cors-security.test.js +297 -0
- package/dist/__tests__/cors-security.test.js.map +1 -0
- package/dist/__tests__/index.test.d.ts +2 -0
- package/dist/__tests__/index.test.d.ts.map +1 -0
- package/dist/__tests__/index.test.js +294 -0
- package/dist/__tests__/index.test.js.map +1 -0
- package/dist/__tests__/integration.test.d.ts +17 -0
- package/dist/__tests__/integration.test.d.ts.map +1 -0
- package/dist/__tests__/integration.test.js +304 -0
- package/dist/__tests__/integration.test.js.map +1 -0
- package/dist/__tests__/setup.d.ts +1 -0
- package/dist/__tests__/setup.d.ts.map +1 -0
- package/dist/__tests__/setup.js +7 -0
- package/dist/__tests__/setup.js.map +1 -0
- package/dist/__tests__/string-flag-value.test.d.ts +8 -0
- package/dist/__tests__/string-flag-value.test.d.ts.map +1 -0
- package/dist/__tests__/string-flag-value.test.js +96 -0
- package/dist/__tests__/string-flag-value.test.js.map +1 -0
- package/dist/index.d.ts +2 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.esm.js +19 -32
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +19 -32
- package/dist/index.js.map +1 -1
- package/package.json +38 -30
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Core SDK for Togglely - Framework agnostic feature toggles.
|
|
4
4
|
|
|
5
|
+
No automatic polling - fetch once on init (configurable) and refresh manually or use WebSockets.
|
|
6
|
+
|
|
5
7
|
## Installation
|
|
6
8
|
|
|
7
9
|
```bash
|
|
@@ -78,10 +80,10 @@ Creates a new Togglely client.
|
|
|
78
80
|
- `apiKey` (string, required): Your API key
|
|
79
81
|
- `environment` (string, required): Environment name
|
|
80
82
|
- `baseUrl` (string, required): Togglely instance URL
|
|
81
|
-
- `refreshInterval` (number, default: 60000): Polling interval in ms
|
|
82
83
|
- `timeout` (number, default: 5000): Request timeout in ms
|
|
83
84
|
- `offlineFallback` (boolean, default: true): Enable offline mode
|
|
84
85
|
- `envPrefix` (string, default: 'TOGGLELY_'): Environment variable prefix
|
|
86
|
+
- `autoFetch` (boolean, default: true): Fetch toggles on init
|
|
85
87
|
|
|
86
88
|
### Methods
|
|
87
89
|
|
|
@@ -92,5 +94,5 @@ Creates a new Togglely client.
|
|
|
92
94
|
- `getAllToggles()`: Get all toggles
|
|
93
95
|
- `setContext(context)`: Set evaluation context (userId, email, etc.)
|
|
94
96
|
- `refresh()`: Manually refresh toggles
|
|
95
|
-
- `destroy()`: Cleanup
|
|
97
|
+
- `destroy()`: Cleanup resources
|
|
96
98
|
- `on(event, handler)`: Listen to events ('ready', 'update', 'error', 'offline', 'online')
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CORS Security Integration Tests
|
|
3
|
+
*
|
|
4
|
+
* These tests verify that the CORS origin validation works correctly
|
|
5
|
+
* between the SDK and the backend.
|
|
6
|
+
*
|
|
7
|
+
* Security scenarios tested:
|
|
8
|
+
* 1. Allowed origin can access flags
|
|
9
|
+
* 2. Disallowed origin is rejected
|
|
10
|
+
* 3. Wildcard allows all origins
|
|
11
|
+
* 4. Subdomain matching works correctly
|
|
12
|
+
* 5. Empty allowedOrigins allows all (backward compatibility)
|
|
13
|
+
*/
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=cors-security.test.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors-security.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/cors-security.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG"}
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CORS Security Integration Tests
|
|
3
|
+
*
|
|
4
|
+
* These tests verify that the CORS origin validation works correctly
|
|
5
|
+
* between the SDK and the backend.
|
|
6
|
+
*
|
|
7
|
+
* Security scenarios tested:
|
|
8
|
+
* 1. Allowed origin can access flags
|
|
9
|
+
* 2. Disallowed origin is rejected
|
|
10
|
+
* 3. Wildcard allows all origins
|
|
11
|
+
* 4. Subdomain matching works correctly
|
|
12
|
+
* 5. Empty allowedOrigins allows all (backward compatibility)
|
|
13
|
+
*/
|
|
14
|
+
import { TogglelyClient } from '../index';
|
|
15
|
+
const TEST_API_URL = 'http://localhost:4000';
|
|
16
|
+
const ADMIN_API_URL = 'http://localhost:4000/api'; // Admin API for setup
|
|
17
|
+
// Demo credentials
|
|
18
|
+
const DEMO_EMAIL = 'demo@togglely.io';
|
|
19
|
+
const DEMO_PASSWORD = 'demo123!';
|
|
20
|
+
// Helper to get admin token
|
|
21
|
+
async function getAdminToken() {
|
|
22
|
+
const response = await fetch(`${ADMIN_API_URL}/auth/login`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: { 'Content-Type': 'application/json' },
|
|
25
|
+
body: JSON.stringify({ email: DEMO_EMAIL, password: DEMO_PASSWORD }),
|
|
26
|
+
});
|
|
27
|
+
if (!response.ok) {
|
|
28
|
+
throw new Error('Failed to get admin token');
|
|
29
|
+
}
|
|
30
|
+
const data = await response.json();
|
|
31
|
+
return data.token;
|
|
32
|
+
}
|
|
33
|
+
// Helper to create a test project with specific CORS settings
|
|
34
|
+
async function createTestProject(token, orgId, name, allowedOrigins) {
|
|
35
|
+
// Create project
|
|
36
|
+
const projectRes = await fetch(`${ADMIN_API_URL}/projects/${orgId}`, {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
'Authorization': `Bearer ${token}`,
|
|
41
|
+
},
|
|
42
|
+
body: JSON.stringify({
|
|
43
|
+
name,
|
|
44
|
+
key: name.toLowerCase().replace(/\s+/g, '-'),
|
|
45
|
+
description: 'Test project for CORS validation',
|
|
46
|
+
type: 'SINGLE',
|
|
47
|
+
}),
|
|
48
|
+
});
|
|
49
|
+
if (!projectRes.ok) {
|
|
50
|
+
throw new Error(`Failed to create project: ${await projectRes.text()}`);
|
|
51
|
+
}
|
|
52
|
+
const project = await projectRes.json();
|
|
53
|
+
// Update project with allowed origins
|
|
54
|
+
await fetch(`${ADMIN_API_URL}/projects/${project.id}`, {
|
|
55
|
+
method: 'PATCH',
|
|
56
|
+
headers: {
|
|
57
|
+
'Content-Type': 'application/json',
|
|
58
|
+
'Authorization': `Bearer ${token}`,
|
|
59
|
+
},
|
|
60
|
+
body: JSON.stringify({ allowedOrigins }),
|
|
61
|
+
});
|
|
62
|
+
// Create API key
|
|
63
|
+
const apiKeyRes = await fetch(`${ADMIN_API_URL}/api-keys/${orgId}`, {
|
|
64
|
+
method: 'POST',
|
|
65
|
+
headers: {
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
'Authorization': `Bearer ${token}`,
|
|
68
|
+
},
|
|
69
|
+
body: JSON.stringify({
|
|
70
|
+
name: `Test Key for ${name}`,
|
|
71
|
+
projectId: project.id,
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
if (!apiKeyRes.ok) {
|
|
75
|
+
throw new Error(`Failed to create API key: ${await apiKeyRes.text()}`);
|
|
76
|
+
}
|
|
77
|
+
const apiKey = await apiKeyRes.json();
|
|
78
|
+
return {
|
|
79
|
+
id: project.id,
|
|
80
|
+
key: project.key,
|
|
81
|
+
apiKey: apiKey.key,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// Helper to delete test project
|
|
85
|
+
async function deleteTestProject(token, projectId) {
|
|
86
|
+
await fetch(`${ADMIN_API_URL}/projects/${projectId}`, {
|
|
87
|
+
method: 'DELETE',
|
|
88
|
+
headers: { 'Authorization': `Bearer ${token}` },
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
// Helper to wait for SDK state
|
|
92
|
+
async function waitForState(client, checkFn, timeout = 5000) {
|
|
93
|
+
const startTime = Date.now();
|
|
94
|
+
while (Date.now() - startTime < timeout) {
|
|
95
|
+
const state = client.getState();
|
|
96
|
+
if (checkFn(state)) {
|
|
97
|
+
return true;
|
|
98
|
+
}
|
|
99
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
100
|
+
}
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
describe('CORS Security Tests', () => {
|
|
104
|
+
let adminToken;
|
|
105
|
+
let orgId;
|
|
106
|
+
let testProjects = [];
|
|
107
|
+
beforeAll(async () => {
|
|
108
|
+
try {
|
|
109
|
+
adminToken = await getAdminToken();
|
|
110
|
+
// Get user's organization
|
|
111
|
+
const orgsRes = await fetch(`${ADMIN_API_URL}/organizations`, {
|
|
112
|
+
headers: { 'Authorization': `Bearer ${adminToken}` },
|
|
113
|
+
});
|
|
114
|
+
if (!orgsRes.ok) {
|
|
115
|
+
throw new Error('Failed to get organizations');
|
|
116
|
+
}
|
|
117
|
+
const orgs = await orgsRes.json();
|
|
118
|
+
if (orgs.length === 0) {
|
|
119
|
+
throw new Error('No organizations found');
|
|
120
|
+
}
|
|
121
|
+
orgId = orgs[0].id;
|
|
122
|
+
console.log('Connected to backend, using org:', orgs[0].name);
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
console.error('Failed to connect to backend:', error);
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}, 30000);
|
|
129
|
+
afterAll(async () => {
|
|
130
|
+
// Cleanup test projects
|
|
131
|
+
for (const project of testProjects) {
|
|
132
|
+
try {
|
|
133
|
+
await deleteTestProject(adminToken, project.id);
|
|
134
|
+
}
|
|
135
|
+
catch (e) {
|
|
136
|
+
console.warn('Failed to cleanup project:', project.key);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}, 30000);
|
|
140
|
+
describe('Origin Allowlist Validation', () => {
|
|
141
|
+
it('should allow access when origin is in allowed list', async () => {
|
|
142
|
+
// Create project with specific allowed origin
|
|
143
|
+
const project = await createTestProject(adminToken, orgId, 'CORS Allowed Test', ['https://trusted-app.com']);
|
|
144
|
+
testProjects.push(project);
|
|
145
|
+
const client = new TogglelyClient({
|
|
146
|
+
apiKey: project.apiKey,
|
|
147
|
+
baseUrl: TEST_API_URL,
|
|
148
|
+
project: project.key,
|
|
149
|
+
environment: 'development',
|
|
150
|
+
autoFetch: true,
|
|
151
|
+
});
|
|
152
|
+
// Wait for fetch to complete (should succeed or go offline)
|
|
153
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
154
|
+
const state = client.getState();
|
|
155
|
+
console.log('SDK state with allowed origin:', state);
|
|
156
|
+
// In Node.js test environment, we can't set the Origin header
|
|
157
|
+
// So the request will either succeed (no origin check) or fail with CORS
|
|
158
|
+
// The important thing is that the SDK handles it gracefully
|
|
159
|
+
expect(state.isReady || state.isOffline).toBe(true);
|
|
160
|
+
client.destroy();
|
|
161
|
+
}, 30000);
|
|
162
|
+
it('should reject access when origin is not in allowed list', async () => {
|
|
163
|
+
// This test demonstrates what happens when a request comes from an unauthorized origin
|
|
164
|
+
// In a real browser, this would be blocked by CORS
|
|
165
|
+
const project = await createTestProject(adminToken, orgId, 'CORS Blocked Test', ['https://trusted-app.com'] // Only trusted-app allowed
|
|
166
|
+
);
|
|
167
|
+
testProjects.push(project);
|
|
168
|
+
// Note: In jsdom/Node environment, we cannot simulate different origins
|
|
169
|
+
// This test documents the expected behavior
|
|
170
|
+
console.log('Created project that only allows https://trusted-app.com');
|
|
171
|
+
console.log(' Requests from other origins would be rejected in a real browser');
|
|
172
|
+
expect(project.apiKey).toBeTruthy();
|
|
173
|
+
}, 30000);
|
|
174
|
+
it('should allow all origins when wildcard is set', async () => {
|
|
175
|
+
const project = await createTestProject(adminToken, orgId, 'CORS Wildcard Test', ['*'] // Allow all
|
|
176
|
+
);
|
|
177
|
+
testProjects.push(project);
|
|
178
|
+
const client = new TogglelyClient({
|
|
179
|
+
apiKey: project.apiKey,
|
|
180
|
+
baseUrl: TEST_API_URL,
|
|
181
|
+
project: project.key,
|
|
182
|
+
environment: 'development',
|
|
183
|
+
autoFetch: true,
|
|
184
|
+
});
|
|
185
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
186
|
+
const state = client.getState();
|
|
187
|
+
console.log('SDK state with wildcard:', state);
|
|
188
|
+
// With wildcard, requests should succeed
|
|
189
|
+
expect(state.isReady || state.isOffline).toBe(true);
|
|
190
|
+
client.destroy();
|
|
191
|
+
}, 30000);
|
|
192
|
+
it('should allow all origins when list is empty (backward compatibility)', async () => {
|
|
193
|
+
const project = await createTestProject(adminToken, orgId, 'CORS Empty Test', [] // Empty = allow all
|
|
194
|
+
);
|
|
195
|
+
testProjects.push(project);
|
|
196
|
+
const client = new TogglelyClient({
|
|
197
|
+
apiKey: project.apiKey,
|
|
198
|
+
baseUrl: TEST_API_URL,
|
|
199
|
+
project: project.key,
|
|
200
|
+
environment: 'development',
|
|
201
|
+
autoFetch: true,
|
|
202
|
+
});
|
|
203
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
204
|
+
const state = client.getState();
|
|
205
|
+
const toggles = client.getAllToggles();
|
|
206
|
+
console.log('SDK state with empty allowedOrigins:', state);
|
|
207
|
+
console.log('Toggles fetched:', Object.keys(toggles).length);
|
|
208
|
+
// Empty allowedOrigins should allow all requests
|
|
209
|
+
expect(state.isReady || state.isOffline).toBe(true);
|
|
210
|
+
client.destroy();
|
|
211
|
+
}, 30000);
|
|
212
|
+
});
|
|
213
|
+
describe('SDK Security Behavior', () => {
|
|
214
|
+
it('should not expose API key in error messages', async () => {
|
|
215
|
+
const client = new TogglelyClient({
|
|
216
|
+
apiKey: 'test-api-key-12345',
|
|
217
|
+
baseUrl: TEST_API_URL,
|
|
218
|
+
project: 'non-existent-project',
|
|
219
|
+
environment: 'development',
|
|
220
|
+
autoFetch: true,
|
|
221
|
+
});
|
|
222
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
223
|
+
// The API key should be stored internally
|
|
224
|
+
// This is more of a documentation test
|
|
225
|
+
expect(client.getState()).toBeDefined();
|
|
226
|
+
client.destroy();
|
|
227
|
+
});
|
|
228
|
+
it('should handle 403 Forbidden gracefully', async () => {
|
|
229
|
+
// Use invalid API key
|
|
230
|
+
const client = new TogglelyClient({
|
|
231
|
+
apiKey: 'invalid-key-format',
|
|
232
|
+
baseUrl: TEST_API_URL,
|
|
233
|
+
project: 'test-project',
|
|
234
|
+
environment: 'development',
|
|
235
|
+
autoFetch: true,
|
|
236
|
+
});
|
|
237
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
238
|
+
const state = client.getState();
|
|
239
|
+
// Should have an error or be offline when receiving 403
|
|
240
|
+
expect(state.lastError !== null || state.isOffline).toBe(true);
|
|
241
|
+
client.destroy();
|
|
242
|
+
});
|
|
243
|
+
it('should use offline fallback when request fails', async () => {
|
|
244
|
+
const client = new TogglelyClient({
|
|
245
|
+
apiKey: 'invalid-key',
|
|
246
|
+
baseUrl: TEST_API_URL,
|
|
247
|
+
project: 'test',
|
|
248
|
+
environment: 'development',
|
|
249
|
+
autoFetch: true,
|
|
250
|
+
offlineFallback: true,
|
|
251
|
+
});
|
|
252
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
253
|
+
// Should have state
|
|
254
|
+
const state = client.getState();
|
|
255
|
+
expect(state).toBeDefined();
|
|
256
|
+
client.destroy();
|
|
257
|
+
});
|
|
258
|
+
});
|
|
259
|
+
describe('API Key Security', () => {
|
|
260
|
+
it('should store API key securely in memory', async () => {
|
|
261
|
+
const project = await createTestProject(adminToken, orgId, 'API Key Security Test', ['*']);
|
|
262
|
+
testProjects.push(project);
|
|
263
|
+
const client = new TogglelyClient({
|
|
264
|
+
apiKey: project.apiKey,
|
|
265
|
+
baseUrl: TEST_API_URL,
|
|
266
|
+
project: project.key,
|
|
267
|
+
environment: 'development',
|
|
268
|
+
autoFetch: false,
|
|
269
|
+
});
|
|
270
|
+
// Verify SDK was created successfully
|
|
271
|
+
expect(client.getState()).toBeDefined();
|
|
272
|
+
client.destroy();
|
|
273
|
+
}, 30000);
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
// Health check
|
|
277
|
+
describe('Backend Security API', () => {
|
|
278
|
+
it('should confirm backend supports CORS configuration', async () => {
|
|
279
|
+
const token = await getAdminToken();
|
|
280
|
+
// Get a project and check it has allowedOrigins field
|
|
281
|
+
const orgsRes = await fetch(`${ADMIN_API_URL}/organizations`, {
|
|
282
|
+
headers: { 'Authorization': `Bearer ${token}` },
|
|
283
|
+
});
|
|
284
|
+
const orgs = await orgsRes.json();
|
|
285
|
+
if (orgs.length > 0) {
|
|
286
|
+
const projectsRes = await fetch(`${ADMIN_API_URL}/projects?orgId=${orgs[0].id}`, { headers: { 'Authorization': `Bearer ${token}` } });
|
|
287
|
+
const projects = await projectsRes.json();
|
|
288
|
+
if (projects.length > 0) {
|
|
289
|
+
const projectRes = await fetch(`${ADMIN_API_URL}/projects/${projects[0].id}`, { headers: { 'Authorization': `Bearer ${token}` } });
|
|
290
|
+
const project = await projectRes.json();
|
|
291
|
+
console.log('Project has allowedOrigins:', Array.isArray(project.allowedOrigins));
|
|
292
|
+
expect(Array.isArray(project.allowedOrigins)).toBe(true);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}, 30000);
|
|
296
|
+
});
|
|
297
|
+
//# sourceMappingURL=cors-security.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cors-security.test.js","sourceRoot":"","sources":["../../src/__tests__/cors-security.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE1C,MAAM,YAAY,GAAG,uBAAuB,CAAC;AAC7C,MAAM,aAAa,GAAG,2BAA2B,CAAC,CAAC,sBAAsB;AAEzE,mBAAmB;AACnB,MAAM,UAAU,GAAG,kBAAkB,CAAC;AACtC,MAAM,aAAa,GAAG,U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KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC;gBAClF,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAC3D,CAAC;QACH,CAAC;IACH,CAAC,EAAE,KAAK,CAAC,CAAC;AACZ,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/index.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import { TogglelyClient, togglesToEnvVars, createOfflineTogglesScript } from '../index';
|
|
2
|
+
describe('TogglelyClient', () => {
|
|
3
|
+
const mockConfig = {
|
|
4
|
+
apiKey: 'test-api-key',
|
|
5
|
+
project: 'test-project',
|
|
6
|
+
environment: 'development',
|
|
7
|
+
baseUrl: 'https://api.togglely.io',
|
|
8
|
+
autoFetch: false // Disable auto-fetch for tests
|
|
9
|
+
};
|
|
10
|
+
beforeEach(() => {
|
|
11
|
+
jest.clearAllMocks();
|
|
12
|
+
global.fetch.mockReset();
|
|
13
|
+
});
|
|
14
|
+
describe('initialization', () => {
|
|
15
|
+
it('should create client with default config', () => {
|
|
16
|
+
const client = new TogglelyClient(mockConfig);
|
|
17
|
+
expect(client).toBeDefined();
|
|
18
|
+
expect(client.isReady()).toBe(false);
|
|
19
|
+
client.destroy();
|
|
20
|
+
});
|
|
21
|
+
it('should load offline toggles from environment variables', () => {
|
|
22
|
+
const client = new TogglelyClient({ ...mockConfig, offlineFallback: true });
|
|
23
|
+
const allToggles = client.getAllToggles();
|
|
24
|
+
expect(allToggles['test-feature']).toBeDefined();
|
|
25
|
+
expect(allToggles['test-feature'].value).toBe(true);
|
|
26
|
+
client.destroy();
|
|
27
|
+
});
|
|
28
|
+
it('should not auto-fetch when autoFetch is false', () => {
|
|
29
|
+
new TogglelyClient(mockConfig);
|
|
30
|
+
expect(global.fetch).not.toHaveBeenCalled();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
describe('context handling', () => {
|
|
34
|
+
it('should set and get context', () => {
|
|
35
|
+
const client = new TogglelyClient(mockConfig);
|
|
36
|
+
client.setContext({ userId: '123', tenantId: 'acme' });
|
|
37
|
+
expect(client.getContext()).toEqual({ userId: '123', tenantId: 'acme' });
|
|
38
|
+
client.destroy();
|
|
39
|
+
});
|
|
40
|
+
it('should merge context', () => {
|
|
41
|
+
const client = new TogglelyClient(mockConfig);
|
|
42
|
+
client.setContext({ userId: '123' });
|
|
43
|
+
client.setContext({ tenantId: 'acme' });
|
|
44
|
+
expect(client.getContext()).toEqual({ userId: '123', tenantId: 'acme' });
|
|
45
|
+
client.destroy();
|
|
46
|
+
});
|
|
47
|
+
it('should clear context', () => {
|
|
48
|
+
const client = new TogglelyClient(mockConfig);
|
|
49
|
+
client.setContext({ userId: '123' });
|
|
50
|
+
client.clearContext();
|
|
51
|
+
expect(client.getContext()).toEqual({});
|
|
52
|
+
client.destroy();
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe('API calls with brandKey and context', () => {
|
|
56
|
+
it('should send both brandKey and context when tenantId is set', async () => {
|
|
57
|
+
const client = new TogglelyClient(mockConfig);
|
|
58
|
+
client.setContext({ userId: '123', tenantId: 'acme-corp' });
|
|
59
|
+
global.fetch.mockResolvedValueOnce({
|
|
60
|
+
ok: true,
|
|
61
|
+
json: async () => ({ value: true, enabled: true })
|
|
62
|
+
});
|
|
63
|
+
await client.getValue('test-flag');
|
|
64
|
+
const fetchCall = global.fetch.mock.calls[0];
|
|
65
|
+
const url = fetchCall[0];
|
|
66
|
+
// Should contain both brandKey and context
|
|
67
|
+
expect(url).toContain('brandKey=acme-corp');
|
|
68
|
+
expect(url).toContain('context=');
|
|
69
|
+
expect(url).toContain('tenantId');
|
|
70
|
+
expect(url).toContain('userId');
|
|
71
|
+
client.destroy();
|
|
72
|
+
});
|
|
73
|
+
it('should send context without brandKey when no tenantId/brandKey', async () => {
|
|
74
|
+
const client = new TogglelyClient(mockConfig);
|
|
75
|
+
client.setContext({ userId: '123', country: 'DE' });
|
|
76
|
+
global.fetch.mockResolvedValueOnce({
|
|
77
|
+
ok: true,
|
|
78
|
+
json: async () => ({ value: true, enabled: true })
|
|
79
|
+
});
|
|
80
|
+
await client.getValue('test-flag');
|
|
81
|
+
const fetchCall = global.fetch.mock.calls[0];
|
|
82
|
+
const url = fetchCall[0];
|
|
83
|
+
// Should contain context but not brandKey
|
|
84
|
+
expect(url).not.toContain('brandKey=');
|
|
85
|
+
expect(url).toContain('context=');
|
|
86
|
+
client.destroy();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
describe('refresh (manual fetch)', () => {
|
|
90
|
+
it('should fetch all flags on refresh', async () => {
|
|
91
|
+
const client = new TogglelyClient(mockConfig);
|
|
92
|
+
global.fetch.mockResolvedValueOnce({
|
|
93
|
+
ok: true,
|
|
94
|
+
json: async () => ({
|
|
95
|
+
'feature-a': { value: true, enabled: true },
|
|
96
|
+
'feature-b': { value: 'test', enabled: true }
|
|
97
|
+
})
|
|
98
|
+
});
|
|
99
|
+
await client.refresh();
|
|
100
|
+
expect(global.fetch).toHaveBeenCalledWith(expect.stringContaining('/sdk/flags/test-project/development'), expect.objectContaining({
|
|
101
|
+
headers: expect.objectContaining({
|
|
102
|
+
'Authorization': 'Bearer test-api-key'
|
|
103
|
+
})
|
|
104
|
+
}));
|
|
105
|
+
expect(client.isReady()).toBe(true);
|
|
106
|
+
client.destroy();
|
|
107
|
+
});
|
|
108
|
+
it('should include brandKey and context in refresh', async () => {
|
|
109
|
+
const client = new TogglelyClient(mockConfig);
|
|
110
|
+
client.setContext({ tenantId: 'acme', userId: '123' });
|
|
111
|
+
global.fetch.mockResolvedValueOnce({
|
|
112
|
+
ok: true,
|
|
113
|
+
json: async () => ({})
|
|
114
|
+
});
|
|
115
|
+
await client.refresh();
|
|
116
|
+
const fetchCall = global.fetch.mock.calls[0];
|
|
117
|
+
const url = fetchCall[0];
|
|
118
|
+
expect(url).toContain('brandKey=acme');
|
|
119
|
+
expect(url).toContain('context=');
|
|
120
|
+
client.destroy();
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
describe('toggle accessors', () => {
|
|
124
|
+
it('should return boolean value from cache', async () => {
|
|
125
|
+
const client = new TogglelyClient(mockConfig);
|
|
126
|
+
// Pre-populate cache via refresh
|
|
127
|
+
global.fetch.mockResolvedValueOnce({
|
|
128
|
+
ok: true,
|
|
129
|
+
json: async () => ({
|
|
130
|
+
'bool-flag': { value: true, enabled: true }
|
|
131
|
+
})
|
|
132
|
+
});
|
|
133
|
+
await client.refresh();
|
|
134
|
+
const value = await client.isEnabled('bool-flag', false);
|
|
135
|
+
expect(value).toBe(true);
|
|
136
|
+
// Should not make another API call
|
|
137
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
138
|
+
client.destroy();
|
|
139
|
+
});
|
|
140
|
+
it('should return default value when toggle not found', async () => {
|
|
141
|
+
const client = new TogglelyClient(mockConfig);
|
|
142
|
+
global.fetch.mockResolvedValueOnce({
|
|
143
|
+
ok: 404,
|
|
144
|
+
status: 404
|
|
145
|
+
});
|
|
146
|
+
const value = await client.isEnabled('missing-flag', false);
|
|
147
|
+
expect(value).toBe(false);
|
|
148
|
+
client.destroy();
|
|
149
|
+
});
|
|
150
|
+
it('should return string value', async () => {
|
|
151
|
+
const client = new TogglelyClient(mockConfig);
|
|
152
|
+
global.fetch.mockResolvedValueOnce({
|
|
153
|
+
ok: true,
|
|
154
|
+
json: async () => ({ value: 'hello', enabled: true })
|
|
155
|
+
});
|
|
156
|
+
const value = await client.getString('msg-flag', 'default');
|
|
157
|
+
expect(value).toBe('hello');
|
|
158
|
+
client.destroy();
|
|
159
|
+
});
|
|
160
|
+
it('should return number value', async () => {
|
|
161
|
+
const client = new TogglelyClient(mockConfig);
|
|
162
|
+
global.fetch.mockResolvedValueOnce({
|
|
163
|
+
ok: true,
|
|
164
|
+
json: async () => ({ value: 42, enabled: true })
|
|
165
|
+
});
|
|
166
|
+
const value = await client.getNumber('limit-flag', 0);
|
|
167
|
+
expect(value).toBe(42);
|
|
168
|
+
client.destroy();
|
|
169
|
+
});
|
|
170
|
+
it('should return JSON value', async () => {
|
|
171
|
+
const client = new TogglelyClient(mockConfig);
|
|
172
|
+
global.fetch.mockResolvedValueOnce({
|
|
173
|
+
ok: true,
|
|
174
|
+
json: async () => ({ value: '{"theme":"dark"}', enabled: true })
|
|
175
|
+
});
|
|
176
|
+
const value = await client.getJSON('config-flag', {});
|
|
177
|
+
expect(value).toEqual({ theme: 'dark' });
|
|
178
|
+
client.destroy();
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
describe('events', () => {
|
|
182
|
+
it('should emit ready event after first successful refresh', async () => {
|
|
183
|
+
const client = new TogglelyClient(mockConfig);
|
|
184
|
+
const readyHandler = jest.fn();
|
|
185
|
+
client.on('ready', readyHandler);
|
|
186
|
+
global.fetch.mockResolvedValueOnce({
|
|
187
|
+
ok: true,
|
|
188
|
+
json: async () => ({})
|
|
189
|
+
});
|
|
190
|
+
await client.refresh();
|
|
191
|
+
expect(readyHandler).toHaveBeenCalled();
|
|
192
|
+
client.destroy();
|
|
193
|
+
});
|
|
194
|
+
it('should emit update event on refresh', async () => {
|
|
195
|
+
const client = new TogglelyClient(mockConfig);
|
|
196
|
+
const updateHandler = jest.fn();
|
|
197
|
+
client.on('update', updateHandler);
|
|
198
|
+
global.fetch.mockResolvedValueOnce({
|
|
199
|
+
ok: true,
|
|
200
|
+
json: async () => ({})
|
|
201
|
+
});
|
|
202
|
+
await client.refresh();
|
|
203
|
+
expect(updateHandler).toHaveBeenCalled();
|
|
204
|
+
client.destroy();
|
|
205
|
+
});
|
|
206
|
+
it('should allow unsubscribing from events', async () => {
|
|
207
|
+
const client = new TogglelyClient(mockConfig);
|
|
208
|
+
const handler = jest.fn();
|
|
209
|
+
const unsubscribe = client.on('ready', handler);
|
|
210
|
+
unsubscribe();
|
|
211
|
+
global.fetch.mockResolvedValueOnce({
|
|
212
|
+
ok: true,
|
|
213
|
+
json: async () => ({})
|
|
214
|
+
});
|
|
215
|
+
await client.refresh();
|
|
216
|
+
expect(handler).not.toHaveBeenCalled();
|
|
217
|
+
client.destroy();
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
describe('no polling behavior', () => {
|
|
221
|
+
it('should not poll automatically', async () => {
|
|
222
|
+
jest.useFakeTimers();
|
|
223
|
+
const client = new TogglelyClient({ ...mockConfig, autoFetch: false });
|
|
224
|
+
global.fetch.mockResolvedValue({
|
|
225
|
+
ok: true,
|
|
226
|
+
json: async () => ({})
|
|
227
|
+
});
|
|
228
|
+
// Fast-forward 10 minutes
|
|
229
|
+
jest.advanceTimersByTime(10 * 60 * 1000);
|
|
230
|
+
// Should not have made any calls
|
|
231
|
+
expect(global.fetch).not.toHaveBeenCalled();
|
|
232
|
+
client.destroy();
|
|
233
|
+
jest.useRealTimers();
|
|
234
|
+
});
|
|
235
|
+
it('should allow manual refresh', async () => {
|
|
236
|
+
const client = new TogglelyClient(mockConfig);
|
|
237
|
+
global.fetch.mockResolvedValue({
|
|
238
|
+
ok: true,
|
|
239
|
+
json: async () => ({})
|
|
240
|
+
});
|
|
241
|
+
await client.refresh();
|
|
242
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
243
|
+
await client.refresh();
|
|
244
|
+
expect(global.fetch).toHaveBeenCalledTimes(2);
|
|
245
|
+
client.destroy();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
describe('destroy', () => {
|
|
249
|
+
it('should clean up resources', async () => {
|
|
250
|
+
const client = new TogglelyClient(mockConfig);
|
|
251
|
+
global.fetch.mockResolvedValueOnce({
|
|
252
|
+
ok: true,
|
|
253
|
+
json: async () => ({ 'test': { value: true, enabled: true } })
|
|
254
|
+
});
|
|
255
|
+
await client.refresh();
|
|
256
|
+
expect(client.getAllToggles()).toHaveProperty('test');
|
|
257
|
+
client.destroy();
|
|
258
|
+
expect(client.getAllToggles()).toEqual({});
|
|
259
|
+
});
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
describe('Utility functions', () => {
|
|
263
|
+
describe('togglesToEnvVars', () => {
|
|
264
|
+
it('should convert toggles to env vars', () => {
|
|
265
|
+
const toggles = {
|
|
266
|
+
'dark-mode': true,
|
|
267
|
+
'api-url': 'https://api.com'
|
|
268
|
+
};
|
|
269
|
+
const envVars = togglesToEnvVars(toggles);
|
|
270
|
+
expect(envVars).toEqual({
|
|
271
|
+
'TOGGLELY_DARK_MODE': 'true',
|
|
272
|
+
'TOGGLELY_API_URL': 'https://api.com'
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
it('should use custom prefix', () => {
|
|
276
|
+
const toggles = { feature: true };
|
|
277
|
+
const envVars = togglesToEnvVars(toggles, 'MYAPP_');
|
|
278
|
+
expect(envVars).toEqual({
|
|
279
|
+
'MYAPP_FEATURE': 'true'
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
describe('createOfflineTogglesScript', () => {
|
|
284
|
+
it('should create script tag with toggles', () => {
|
|
285
|
+
const toggles = { 'new-feature': true };
|
|
286
|
+
const script = createOfflineTogglesScript(toggles);
|
|
287
|
+
expect(script).toContain('<script>');
|
|
288
|
+
expect(script).toContain('window.__TOGGLELY_TOGGLES');
|
|
289
|
+
expect(script).toContain('"new-feature":true');
|
|
290
|
+
expect(script).toContain('</script>');
|
|
291
|
+
});
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
//# sourceMappingURL=index.test.js.map
|