statsig-node-vercel 0.4.0 → 0.5.0

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,55 +1,28 @@
1
- import { AdapterResponse, IDataAdapter } from "statsig-node";
2
- import type { EdgeConfigClient } from "@vercel/edge-config";
1
+ import { AdapterResponse, IDataAdapter } from 'statsig-node';
2
+ import { createClient, EdgeConfigClient } from '@vercel/edge-config';
3
3
 
4
4
  export class EdgeConfigDataAdapter implements IDataAdapter {
5
- /**
6
- * The key under which Statisg specs are stored in Edge Config
7
- */
8
- private edgeConfigItemKey: string;
9
- /**
10
- * A fully configured Edge Config client
11
- */
5
+ private configSpecsKey: string;
12
6
  private edgeConfigClient: EdgeConfigClient;
13
7
  private supportConfigSpecPolling: boolean = false;
14
8
 
15
- public constructor(options: {
16
- /**
17
- * The key under which Statsig specs are stored in Edge Config
18
- */
19
- edgeConfigItemKey: string;
20
- /**
21
- * A fully configured Edge Config client.
22
- *
23
- * @example <caption>Creating an Edge Config client</caption>
24
- *
25
- * ```js
26
- * import { createClient } from "@vercel/edge-config";
27
- *
28
- * createClient(process.env.EDGE_CONFIG)
29
- * ```
30
- */
31
- edgeConfigClient: EdgeConfigClient;
32
- }) {
33
- this.edgeConfigItemKey = options.edgeConfigItemKey;
34
- this.edgeConfigClient = options.edgeConfigClient;
9
+ public constructor(key: string, connectionString: string = process.env.EDGE_CONFIG!) {
10
+ this.configSpecsKey = key;
11
+ this.edgeConfigClient = createClient(connectionString);
35
12
  }
36
13
 
37
14
  // eslint-disable-next-line @typescript-eslint/require-await
38
15
  public async get(key: string): Promise<AdapterResponse> {
39
16
  if (key !== "statsig.cache") {
40
- return {
41
- error: new Error(`Edge Config Adapter Only Supports Config Specs`),
42
- };
17
+ return { error: new Error(`Edge Config Adapter Only Supports Config Specs`) };
43
18
  }
44
19
 
45
- const data = await this.edgeConfigClient.get(this.edgeConfigItemKey);
20
+ const data = await this.edgeConfigClient.get(this.configSpecsKey);
46
21
  if (data == null) {
47
22
  return { error: new Error(`key (${key}) does not exist`) };
48
23
  }
49
24
  if (typeof data !== "object") {
50
- return {
51
- error: new Error(`Edge Config value expected to be an object or array`),
52
- };
25
+ return { error: new Error(`Edge Config value expected to be an object or array`) };
53
26
  }
54
27
  return { result: data };
55
28
  }
@@ -58,13 +31,13 @@ export class EdgeConfigDataAdapter implements IDataAdapter {
58
31
  public async set(
59
32
  key: string,
60
33
  value: string,
61
- time?: number | undefined
34
+ time?: number | undefined,
62
35
  ): Promise<void> {
63
36
  // no-op. Statsig's Edge Config integration keeps config specs synced through Statsig's service
64
37
  }
65
38
 
66
39
  public async initialize(): Promise<void> {
67
- const data = await this.edgeConfigClient.get(this.edgeConfigItemKey);
40
+ const data = await this.edgeConfigClient.get(this.configSpecsKey);
68
41
 
69
42
  if (data) {
70
43
  this.supportConfigSpecPolling = true;
@@ -72,7 +45,9 @@ export class EdgeConfigDataAdapter implements IDataAdapter {
72
45
  }
73
46
 
74
47
  // eslint-disable-next-line @typescript-eslint/require-await
75
- public async shutdown(): Promise<void> {}
48
+ public async shutdown(): Promise<void> {
49
+
50
+ }
76
51
 
77
52
  public supportsPollingUpdatesFor(key: string): boolean {
78
53
  if (key === "statsig.cache") {
package/README.md CHANGED
@@ -1,43 +1,30 @@
1
1
  # Statsig Node Server SDK - Edge Config Adapter
2
-
3
- [![npm version](https://badge.fury.io/js/statsig-node-vercel.svg)](https://badge.fury.io/js/statsig-node-vercel)
2
+ [![npm version](https://badge.fury.io/js/statsig-node-vercel.svg)](https://badge.fury.io/js/statsig-node-vercel)
4
3
 
5
4
  A first party Edge Config integration with the [Statsig server-side Node.js SDK](https://github.com/statsig-io/node-js-server-sdk).
6
5
 
7
6
  ## Quick Setup
8
-
9
7
  1. Install the Statsig Node SDK
10
-
11
- ```sh
8
+ ```
12
9
  npm install statsig-node@5.3.0
13
10
  ```
14
-
15
- 2. Install this package and the Edge Config SDK
16
-
17
- ```sh
18
- npm install statsig-node-vercel @vercel/edge-config
11
+ 2. Install this package
12
+ ```
13
+ npm install statsig-node-vercel
19
14
  ```
20
-
21
15
  3. Install the [Statsig Vercel Integration](https://vercel.com/integrations/statsig)
22
- 4. Import the packages
23
-
24
- ```js
25
- import { EdgeConfigDataAdapter } from "statsig-node-vercel";
26
- import { createClient } from "@vercel/edge-config";
16
+ 4. Import the package
17
+ ```
18
+ import { EdgeConfigDataAdapter } from 'statsig-node-vercel'
27
19
  ```
28
-
29
20
  5. Create an instance of the `EdgeConfigDataAdapter`
30
-
31
- ```js
32
- const edgeConfigClient = createClient(process.env.EDGE_CONFIG);
33
- const dataAdapter = new EdgeConfigDataAdapter({
34
- edgeConfigClient: edgeConfigClient,
35
- edgeConfigItemKey: "ITEM_KEY_FROM_INSTALLATION", // something like "statsig-5FSfBpWM9kUPqeKRlZPkod"
36
- })
37
21
  ```
38
-
39
- 6. When initializing the `statsig` sdk, add the adapter to options, along with the initStrategyForIDLists and disableIdListsSync options to avoid a blocking network call for ID Lists. However, if you are using Statsig ID lists for evaluation, you'll need to omit those options and incur a network request to grab ID lists.
40
-
41
- ```js
42
- await statsig.initialize("server-secret-key", { dataAdapter: dataAdapter, initStrategyForIDLists: 'none', disableIdListsSync: true });
22
+ const dataAdapter = new EdgeConfigDataAdapter('KEY_FROM_INSTALLATION');
23
+ ```
24
+ 6. When initializing the `statsig` sdk, add the adapter to options
25
+ ```
26
+ await statsig.initialize(
27
+ 'server-secret-key',
28
+ { dataAdapter: dataAdapter },
29
+ );
43
30
  ```
@@ -1,29 +1,26 @@
1
- import { EdgeConfigDataAdapter } from "../EdgeConfigDataAdapter";
2
- import { createClient } from "@vercel/edge-config";
3
- import fetchMock from "jest-fetch-mock";
1
+ import { EdgeConfigDataAdapter } from '../EdgeConfigDataAdapter';
2
+ import fetchMock from "jest-fetch-mock"
4
3
 
5
- describe("Validate edge config adapter functionality", () => {
6
- const edgeConfigClient = createClient(process.env.EDGE_CONFIG);
7
- const dataAdapter = new EdgeConfigDataAdapter({
8
- edgeConfigItemKey: "statsig-companyid",
9
- edgeConfigClient,
10
- });
4
+ describe('Validate edge config adapter functionality', () => {
5
+ const dataAdapter = new EdgeConfigDataAdapter(
6
+ 'statsig-companyid'
7
+ );
11
8
 
12
9
  beforeEach(async () => {
13
- fetchMock.enableMocks();
10
+ fetchMock.enableMocks()
14
11
  fetchMock.mockResponse('"test123"');
15
12
  await dataAdapter.initialize();
16
- });
13
+ });
17
14
 
18
15
  afterEach(async () => {
19
16
  await dataAdapter.shutdown();
20
17
  });
21
18
 
22
- test("Simple get", async () => {
19
+ test('Simple get', async () => {
23
20
  const { result: gates } = await dataAdapter.get("statsig.cache");
24
21
  if (gates == null) {
25
22
  return;
26
23
  }
27
24
  expect(gates).toEqual('"test123"');
28
25
  });
29
- });
26
+ })
@@ -1,33 +1,9 @@
1
- import { AdapterResponse, IDataAdapter } from "statsig-node";
2
- import type { EdgeConfigClient } from "@vercel/edge-config";
1
+ import { AdapterResponse, IDataAdapter } from 'statsig-node';
3
2
  export declare class EdgeConfigDataAdapter implements IDataAdapter {
4
- /**
5
- * The key under which Statisg specs are stored in Edge Config
6
- */
7
- private edgeConfigItemKey;
8
- /**
9
- * A fully configured Edge Config client
10
- */
3
+ private configSpecsKey;
11
4
  private edgeConfigClient;
12
5
  private supportConfigSpecPolling;
13
- constructor(options: {
14
- /**
15
- * The key under which Statsig specs are stored in Edge Config
16
- */
17
- edgeConfigItemKey: string;
18
- /**
19
- * A fully configured Edge Config client.
20
- *
21
- * @example <caption>Creating an Edge Config client</caption>
22
- *
23
- * ```js
24
- * import { createClient } from "@vercel/edge-config";
25
- *
26
- * createClient(process.env.EDGE_CONFIG)
27
- * ```
28
- */
29
- edgeConfigClient: EdgeConfigClient;
30
- });
6
+ constructor(key: string, connectionString?: string);
31
7
  get(key: string): Promise<AdapterResponse>;
32
8
  set(key: string, value: string, time?: number | undefined): Promise<void>;
33
9
  initialize(): Promise<void>;
@@ -10,28 +10,25 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.EdgeConfigDataAdapter = void 0;
13
+ const edge_config_1 = require("@vercel/edge-config");
13
14
  class EdgeConfigDataAdapter {
14
- constructor(options) {
15
+ constructor(key, connectionString = process.env.EDGE_CONFIG) {
15
16
  this.supportConfigSpecPolling = false;
16
- this.edgeConfigItemKey = options.edgeConfigItemKey;
17
- this.edgeConfigClient = options.edgeConfigClient;
17
+ this.configSpecsKey = key;
18
+ this.edgeConfigClient = (0, edge_config_1.createClient)(connectionString);
18
19
  }
19
20
  // eslint-disable-next-line @typescript-eslint/require-await
20
21
  get(key) {
21
22
  return __awaiter(this, void 0, void 0, function* () {
22
23
  if (key !== "statsig.cache") {
23
- return {
24
- error: new Error(`Edge Config Adapter Only Supports Config Specs`),
25
- };
24
+ return { error: new Error(`Edge Config Adapter Only Supports Config Specs`) };
26
25
  }
27
- const data = yield this.edgeConfigClient.get(this.edgeConfigItemKey);
26
+ const data = yield this.edgeConfigClient.get(this.configSpecsKey);
28
27
  if (data == null) {
29
28
  return { error: new Error(`key (${key}) does not exist`) };
30
29
  }
31
30
  if (typeof data !== "object") {
32
- return {
33
- error: new Error(`Edge Config value expected to be an object or array`),
34
- };
31
+ return { error: new Error(`Edge Config value expected to be an object or array`) };
35
32
  }
36
33
  return { result: data };
37
34
  });
@@ -44,7 +41,7 @@ class EdgeConfigDataAdapter {
44
41
  }
45
42
  initialize() {
46
43
  return __awaiter(this, void 0, void 0, function* () {
47
- const data = yield this.edgeConfigClient.get(this.edgeConfigItemKey);
44
+ const data = yield this.edgeConfigClient.get(this.configSpecsKey);
48
45
  if (data) {
49
46
  this.supportConfigSpecPolling = true;
50
47
  }
@@ -52,7 +49,8 @@ class EdgeConfigDataAdapter {
52
49
  }
53
50
  // eslint-disable-next-line @typescript-eslint/require-await
54
51
  shutdown() {
55
- return __awaiter(this, void 0, void 0, function* () { });
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ });
56
54
  }
57
55
  supportsPollingUpdatesFor(key) {
58
56
  if (key === "statsig.cache") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statsig-node-vercel",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -27,4 +27,4 @@
27
27
  "jest": "^29.0.0",
28
28
  "jest-fetch-mock": "^3.0.3"
29
29
  }
30
- }
30
+ }