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.
@@ -1,7 +1,7 @@
1
- import { AdapterResponse, IDataAdapter, DataAdapterKey } from 'statsig-node';
1
+ import { AdapterResponse, IDataAdapter } from 'statsig-node';
2
2
  import { get } from '@vercel/edge-config';
3
3
 
4
- interface Data {
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 key: string;
16
- private cache: Map<string, string>;
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.key = key;
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
- const value = this.cache.get(key);
28
- if (value === undefined) {
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
- const time = this.timeCache.get(key);
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.key);
45
+ const data = await get<Data>(this.configSpecsKey);
46
46
 
47
47
  if (data) {
48
- this.cache.set('statsig.cache', JSON.stringify(data));
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
- this.cache.clear();
54
+
56
55
  }
57
56
 
58
- public supportsPollingUpdatesFor(key: DataAdapterKey): boolean {
59
- if (key === DataAdapterKey.Rulesets) {
60
- return true;
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 - Redis Integration
2
- [![npm version](https://badge.fury.io/js/statsig-node-redis.svg)](https://badge.fury.io/js/statsig-node-redis)
1
+ # Statsig Node Server SDK - Edge Config Adapter
2
+ [![npm version](https://badge.fury.io/js/statsig-node-vercel.svg)](https://badge.fury.io/js/statsig-node-vercel)
3
3
 
4
- A first party Redis integration with the [Statsig server-side Node.js SDK](https://github.com/statsig-io/node-js-server-sdk).
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 4.18.0-beta.7)*
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@4.18.0-beta.7
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-redis
13
+ npm install statsig-node-vercel
14
14
  ```
15
- 3. Import the package
15
+ 3. Install the [Statsig Vercel App](https://vercel.com/integrations/statsig)
16
+ 4. Import the package
16
17
  ```
17
- import { RedisDataAdapter } from 'statsig-node-redis'
18
+ import { EdgeConfigDataAdapter } from 'statsig-node-vercel'
18
19
  ```
19
- 4. Create an instance of the `RedisDataAdapter`
20
+ 5. Create an instance of the `EdgeConfigDataAdapter`
20
21
  ```
21
- const dataAdapter = new RedisDataAdapter();
22
+ const dataAdapter = new EdgeConfigDataAdapter('KEY_FROM_INSTALLATION');
22
23
  ```
23
- 5. When initializing the `statsig` sdk, add the adapter to options
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 * as statsigsdk from 'statsig-node';
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-3Xl7zBzgVt7La7nzjE0m9V'
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
- statsig._instance = null;
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/set', async () => {
30
- dataAdapter.set('gates', 'test123');
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.0",
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
- "statsig-node": "5.1.0-beta.1"
24
+ "jest-fetch-mock": "^3.0.3"
24
25
  }
25
26
  }