statsig-node-vercel 0.0.1-beta.0 → 0.0.1-beta.2
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/EdgeConfigDataAdapter.ts +18 -19
- package/README.md +12 -32
- package/__tests__/EdgeConfigDataAdapter.test.ts +8 -19
- package/jest.setup.js +12 -0
- package/package.json +5 -4
package/EdgeConfigDataAdapter.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AdapterResponse, IDataAdapter
|
|
1
|
+
import { AdapterResponse, IDataAdapter } from 'statsig-node';
|
|
2
2
|
import { get } from '@vercel/edge-config';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
type Data = {
|
|
5
5
|
dynamic_configs: Record<string, unknown>[];
|
|
6
6
|
feature_gates: Record<string, unknown>[];
|
|
7
7
|
has_updates: boolean;
|
|
@@ -12,24 +12,24 @@ interface Data {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export class EdgeConfigDataAdapter implements IDataAdapter {
|
|
15
|
-
private
|
|
16
|
-
private
|
|
17
|
-
private timeCache: Map<string, number>;
|
|
15
|
+
private configSpecsKey: string;
|
|
16
|
+
private supportConfigSpecPolling: boolean = false;
|
|
18
17
|
|
|
19
18
|
public constructor(key: string) {
|
|
20
|
-
this.
|
|
21
|
-
this.cache = new Map<string, string>();
|
|
22
|
-
this.timeCache = new Map<string, number>();
|
|
19
|
+
this.configSpecsKey = key;
|
|
23
20
|
}
|
|
24
21
|
|
|
25
22
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
26
23
|
public async get(key: string): Promise<AdapterResponse> {
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
if (key !== "statsig.cache") {
|
|
25
|
+
return { error: new Error(`Edge Config Adapter Only Supports Config Specs`) };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const data = await get<Data>(this.configSpecsKey);
|
|
29
|
+
if (data === undefined) {
|
|
29
30
|
return { error: new Error(`key (${key}) does not exist`) };
|
|
30
31
|
}
|
|
31
|
-
|
|
32
|
-
return { result: value, time };
|
|
32
|
+
return { result: JSON.stringify(data), };
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
@@ -42,22 +42,21 @@ export class EdgeConfigDataAdapter implements IDataAdapter {
|
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
public async initialize(): Promise<void> {
|
|
45
|
-
const data = await get<Data>(this.
|
|
45
|
+
const data = await get<Data>(this.configSpecsKey);
|
|
46
46
|
|
|
47
47
|
if (data) {
|
|
48
|
-
this.
|
|
49
|
-
this.timeCache.set('statsig.cache', data.time);
|
|
48
|
+
this.supportConfigSpecPolling = true;
|
|
50
49
|
}
|
|
51
50
|
}
|
|
52
51
|
|
|
53
52
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
54
53
|
public async shutdown(): Promise<void> {
|
|
55
|
-
|
|
54
|
+
|
|
56
55
|
}
|
|
57
56
|
|
|
58
|
-
public supportsPollingUpdatesFor(key:
|
|
59
|
-
if (key ===
|
|
60
|
-
return
|
|
57
|
+
public supportsPollingUpdatesFor(key: string): boolean {
|
|
58
|
+
if (key === "statsig.cache") {
|
|
59
|
+
return this.supportConfigSpecPolling;
|
|
61
60
|
}
|
|
62
61
|
return false;
|
|
63
62
|
}
|
package/README.md
CHANGED
|
@@ -1,50 +1,30 @@
|
|
|
1
|
-
# Statsig Node Server SDK -
|
|
2
|
-
[](https://badge.fury.io/js/statsig-node-vercel)
|
|
3
3
|
|
|
4
|
-
A first party
|
|
4
|
+
A first party Edge Config integration with the [Statsig server-side Node.js SDK](https://github.com/statsig-io/node-js-server-sdk).
|
|
5
5
|
|
|
6
6
|
## Quick Setup
|
|
7
|
-
1. Install the Statsig Node SDK *NOTE: (For now, you will need the latest beta version
|
|
7
|
+
1. Install the Statsig Node SDK *NOTE: (For now, you will need the latest beta version v5.1.0-beta.1)*
|
|
8
8
|
```
|
|
9
|
-
npm install statsig-node@
|
|
9
|
+
npm install statsig-node@5.1.0-beta.1
|
|
10
10
|
```
|
|
11
11
|
2. Install this package
|
|
12
12
|
```
|
|
13
|
-
npm install statsig-node-
|
|
13
|
+
npm install statsig-node-vercel
|
|
14
14
|
```
|
|
15
|
-
3.
|
|
15
|
+
3. Install the [Statsig Vercel App](https://vercel.com/integrations/statsig)
|
|
16
|
+
4. Import the package
|
|
16
17
|
```
|
|
17
|
-
import {
|
|
18
|
+
import { EdgeConfigDataAdapter } from 'statsig-node-vercel'
|
|
18
19
|
```
|
|
19
|
-
|
|
20
|
+
5. Create an instance of the `EdgeConfigDataAdapter`
|
|
20
21
|
```
|
|
21
|
-
const dataAdapter = new
|
|
22
|
+
const dataAdapter = new EdgeConfigDataAdapter('KEY_FROM_INSTALLATION');
|
|
22
23
|
```
|
|
23
|
-
|
|
24
|
+
6. When initializing the `statsig` sdk, add the adapter to options
|
|
24
25
|
```
|
|
25
26
|
await statsig.initialize(
|
|
26
27
|
'server-secret-key',
|
|
27
28
|
{ dataAdapter: dataAdapter },
|
|
28
29
|
);
|
|
29
30
|
```
|
|
30
|
-
|
|
31
|
-
## Customizing the adapter
|
|
32
|
-
When initializing `RedisDataAdapter`, you can specify the following options:
|
|
33
|
-
```
|
|
34
|
-
const dataAdapter = new RedisDataAdapter(
|
|
35
|
-
hostname,
|
|
36
|
-
port,
|
|
37
|
-
password,
|
|
38
|
-
db,
|
|
39
|
-
);
|
|
40
|
-
```
|
|
41
|
-
|
|
42
|
-
| param | default | description |
|
|
43
|
-
| --- | --- | --- |
|
|
44
|
-
| hostname | 'localhost' | Redis server hostname |
|
|
45
|
-
| port | 6379 | Redis server port |
|
|
46
|
-
| password | | ACL password or the old "--requirepass" password |
|
|
47
|
-
| db | 0 | Redis database number (supports 16 databases) |
|
|
48
|
-
|
|
49
|
-
## Links
|
|
50
|
-
[Node Redis](https://github.com/redis/node-redis)
|
|
@@ -1,37 +1,26 @@
|
|
|
1
1
|
import { EdgeConfigDataAdapter } from '../EdgeConfigDataAdapter';
|
|
2
|
-
import
|
|
3
|
-
// @ts-ignore
|
|
4
|
-
const statsig = statsigsdk.default;
|
|
2
|
+
import fetchMock from "jest-fetch-mock"
|
|
5
3
|
|
|
6
4
|
describe('Validate edge config adapter functionality', () => {
|
|
7
5
|
const dataAdapter = new EdgeConfigDataAdapter(
|
|
8
|
-
'statsig-
|
|
6
|
+
'statsig-companyid'
|
|
9
7
|
);
|
|
10
|
-
const statsigOptions = {
|
|
11
|
-
dataAdapter: dataAdapter,
|
|
12
|
-
environment: { tier: 'staging' },
|
|
13
|
-
};
|
|
14
|
-
const user = {
|
|
15
|
-
userID: '12345',
|
|
16
|
-
email: 'kenny@nfl.com',
|
|
17
|
-
custom: { level: 9 },
|
|
18
|
-
};
|
|
19
8
|
|
|
20
9
|
beforeEach(async () => {
|
|
21
|
-
|
|
10
|
+
fetchMock.enableMocks()
|
|
11
|
+
fetchMock.mockResponse('"test123"');
|
|
22
12
|
await dataAdapter.initialize();
|
|
23
|
-
})
|
|
13
|
+
});
|
|
24
14
|
|
|
25
15
|
afterEach(async () => {
|
|
26
16
|
await dataAdapter.shutdown();
|
|
27
17
|
});
|
|
28
18
|
|
|
29
|
-
test('Simple get
|
|
30
|
-
dataAdapter.
|
|
31
|
-
const { result: gates } = await dataAdapter.get('gates');
|
|
19
|
+
test('Simple get', async () => {
|
|
20
|
+
const { result: gates } = await dataAdapter.get("statsig.cache");
|
|
32
21
|
if (gates == null) {
|
|
33
22
|
return;
|
|
34
23
|
}
|
|
35
|
-
expect(gates).toEqual('test123');
|
|
24
|
+
expect(gates).toEqual('"test123"');
|
|
36
25
|
});
|
|
37
26
|
})
|
package/jest.setup.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// @ts-nocheck
|
|
2
|
+
global.console = {
|
|
3
|
+
log: console.log, // console.log are kept in tests for debugging
|
|
4
|
+
|
|
5
|
+
// Mock other console functions so they don't pollute the console when running test
|
|
6
|
+
error: jest.fn(),
|
|
7
|
+
warn: jest.fn(),
|
|
8
|
+
info: jest.fn(),
|
|
9
|
+
debug: jest.fn(),
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
jest.setTimeout(20000);
|
package/package.json
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "statsig-node-vercel",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
3
|
+
"version": "0.0.1-beta.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"prepare": "rm -rf dist/ && tsc",
|
|
8
|
-
"test": "npm run prepare && pnpm jest"
|
|
8
|
+
"test": "npm run prepare && EDGE_CONFIG=\"https://edge-config.vercel.com/ecfg_123?token=123\" pnpm jest"
|
|
9
9
|
},
|
|
10
10
|
"types": "./index.d.ts",
|
|
11
11
|
"keywords": [],
|
|
12
12
|
"author": "",
|
|
13
13
|
"license": "ISC",
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@vercel/edge-config": "^0.1.0-canary.15"
|
|
15
|
+
"@vercel/edge-config": "^0.1.0-canary.15",
|
|
16
|
+
"statsig-node": "5.1.0-beta.5"
|
|
16
17
|
},
|
|
17
18
|
"devDependencies": {
|
|
18
19
|
"@babel/core": "^7.18.13",
|
|
@@ -20,6 +21,6 @@
|
|
|
20
21
|
"@babel/preset-typescript": "^7.18.6",
|
|
21
22
|
"@types/jest": "^28.1.8",
|
|
22
23
|
"jest": "^29.0.0",
|
|
23
|
-
"
|
|
24
|
+
"jest-fetch-mock": "^3.0.3"
|
|
24
25
|
}
|
|
25
26
|
}
|