statsig-node-vercel 0.2.0-beta.1 → 0.2.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,40 +1,65 @@
1
- import { AdapterResponse, IDataAdapter, DataAdapterKey } from 'statsig-node';
2
- import { createClient, EdgeConfigClient } from '@vercel/edge-config';
1
+ import { AdapterResponse, IDataAdapter } from "statsig-node";
2
+ import type { EdgeConfigClient } from "@vercel/edge-config";
3
3
 
4
4
  export class EdgeConfigDataAdapter implements IDataAdapter {
5
- private configSpecsKey: string;
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
+ */
6
12
  private edgeConfigClient: EdgeConfigClient;
7
13
  private supportConfigSpecPolling: boolean = false;
8
14
 
9
- public constructor(key: string, connectionString: string = process.env.EDGE_CONFIG!) {
10
- this.configSpecsKey = key;
11
- this.edgeConfigClient = createClient(connectionString);
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;
12
35
  }
13
36
 
14
37
  // eslint-disable-next-line @typescript-eslint/require-await
15
38
  public async get(key: string): Promise<AdapterResponse> {
16
39
  if (key !== "statsig.cache") {
17
- return { error: new Error(`Edge Config Adapter Only Supports Config Specs`) };
40
+ return {
41
+ error: new Error(`Edge Config Adapter Only Supports Config Specs`),
42
+ };
18
43
  }
19
44
 
20
- const data = await this.edgeConfigClient.get(this.configSpecsKey);
45
+ const data = await this.edgeConfigClient.get(this.edgeConfigItemKey);
21
46
  if (data === undefined) {
22
47
  return { error: new Error(`key (${key}) does not exist`) };
23
48
  }
24
- return { result: JSON.stringify(data), };
49
+ return { result: JSON.stringify(data) };
25
50
  }
26
51
 
27
52
  // eslint-disable-next-line @typescript-eslint/require-await
28
53
  public async set(
29
54
  key: string,
30
55
  value: string,
31
- time?: number | undefined,
56
+ time?: number | undefined
32
57
  ): Promise<void> {
33
58
  // no-op. Statsig's Edge Config integration keeps config specs synced through Statsig's service
34
59
  }
35
60
 
36
61
  public async initialize(): Promise<void> {
37
- const data = await this.edgeConfigClient.get(this.configSpecsKey);
62
+ const data = await this.edgeConfigClient.get(this.edgeConfigItemKey);
38
63
 
39
64
  if (data) {
40
65
  this.supportConfigSpecPolling = true;
@@ -42,11 +67,9 @@ export class EdgeConfigDataAdapter implements IDataAdapter {
42
67
  }
43
68
 
44
69
  // eslint-disable-next-line @typescript-eslint/require-await
45
- public async shutdown(): Promise<void> {
46
-
47
- }
70
+ public async shutdown(): Promise<void> {}
48
71
 
49
- public supportsPollingUpdatesFor(key: DataAdapterKey): boolean {
72
+ public supportsPollingUpdatesFor(key: string): boolean {
50
73
  if (key === "statsig.cache") {
51
74
  return this.supportConfigSpecPolling;
52
75
  }
package/README.md CHANGED
@@ -1,30 +1,40 @@
1
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)
2
+
3
+ [![npm version](https://badge.fury.io/js/statsig-node-vercel.svg)](https://badge.fury.io/js/statsig-node-vercel)
3
4
 
4
5
  A first party Edge Config integration with the [Statsig server-side Node.js SDK](https://github.com/statsig-io/node-js-server-sdk).
5
6
 
6
7
  ## Quick Setup
8
+
7
9
  1. Install the Statsig Node SDK
8
- ```
10
+
11
+ ```sh
9
12
  npm install statsig-node@5.3.0
10
13
  ```
11
- 2. Install this package
12
- ```
13
- npm install statsig-node-vercel
14
+
15
+ 2. Install this package and the Edge Config SDK
16
+
17
+ ```sh
18
+ npm install statsig-node-vercel @vercel/edge-config
14
19
  ```
20
+
15
21
  3. Install the [Statsig Vercel Integration](https://vercel.com/integrations/statsig)
16
- 4. Import the package
17
- ```
18
- import { EdgeConfigDataAdapter } from 'statsig-node-vercel'
22
+ 4. Import the packages
23
+
24
+ ```js
25
+ import { EdgeConfigDataAdapter } from "statsig-node-vercel";
26
+ import { createClient } from "@vercel/edge-config";
19
27
  ```
28
+
20
29
  5. Create an instance of the `EdgeConfigDataAdapter`
30
+
31
+ ```js
32
+ const edgeConfigClient = createClient(process.env.EDGE_CONFIG);
33
+ const dataAdapter = new EdgeConfigDataAdapter("KEY_FROM_INSTALLATION");
21
34
  ```
22
- const dataAdapter = new EdgeConfigDataAdapter('KEY_FROM_INSTALLATION');
23
- ```
35
+
24
36
  6. When initializing the `statsig` sdk, add the adapter to options
25
- ```
26
- await statsig.initialize(
27
- 'server-secret-key',
28
- { dataAdapter: dataAdapter },
29
- );
37
+
38
+ ```js
39
+ await statsig.initialize("server-secret-key", { dataAdapter: dataAdapter });
30
40
  ```
@@ -1,26 +1,29 @@
1
- import { EdgeConfigDataAdapter } from '../EdgeConfigDataAdapter';
2
- import fetchMock from "jest-fetch-mock"
1
+ import { EdgeConfigDataAdapter } from "../EdgeConfigDataAdapter";
2
+ import { createClient } from "@vercel/edge-config";
3
+ import fetchMock from "jest-fetch-mock";
3
4
 
4
- describe('Validate edge config adapter functionality', () => {
5
- const dataAdapter = new EdgeConfigDataAdapter(
6
- 'statsig-companyid'
7
- );
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
+ });
8
11
 
9
12
  beforeEach(async () => {
10
- fetchMock.enableMocks()
13
+ fetchMock.enableMocks();
11
14
  fetchMock.mockResponse('"test123"');
12
15
  await dataAdapter.initialize();
13
- });
16
+ });
14
17
 
15
18
  afterEach(async () => {
16
19
  await dataAdapter.shutdown();
17
20
  });
18
21
 
19
- test('Simple get', async () => {
22
+ test("Simple get", async () => {
20
23
  const { result: gates } = await dataAdapter.get("statsig.cache");
21
24
  if (gates == null) {
22
25
  return;
23
26
  }
24
27
  expect(gates).toEqual('"test123"');
25
28
  });
26
- })
29
+ });
@@ -1,12 +1,36 @@
1
- import { AdapterResponse, IDataAdapter, DataAdapterKey } from 'statsig-node';
1
+ import { AdapterResponse, IDataAdapter } from "statsig-node";
2
+ import type { EdgeConfigClient } from "@vercel/edge-config";
2
3
  export declare class EdgeConfigDataAdapter implements IDataAdapter {
3
- private configSpecsKey;
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
+ */
4
11
  private edgeConfigClient;
5
12
  private supportConfigSpecPolling;
6
- constructor(key: string, connectionString?: string);
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
+ });
7
31
  get(key: string): Promise<AdapterResponse>;
8
32
  set(key: string, value: string, time?: number | undefined): Promise<void>;
9
33
  initialize(): Promise<void>;
10
34
  shutdown(): Promise<void>;
11
- supportsPollingUpdatesFor(key: DataAdapterKey): boolean;
35
+ supportsPollingUpdatesFor(key: string): boolean;
12
36
  }
@@ -10,24 +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");
14
13
  class EdgeConfigDataAdapter {
15
- constructor(key, connectionString = process.env.EDGE_CONFIG) {
14
+ constructor(options) {
16
15
  this.supportConfigSpecPolling = false;
17
- this.configSpecsKey = key;
18
- this.edgeConfigClient = (0, edge_config_1.createClient)(connectionString);
16
+ this.edgeConfigItemKey = options.edgeConfigItemKey;
17
+ this.edgeConfigClient = options.edgeConfigClient;
19
18
  }
20
19
  // eslint-disable-next-line @typescript-eslint/require-await
21
20
  get(key) {
22
21
  return __awaiter(this, void 0, void 0, function* () {
23
22
  if (key !== "statsig.cache") {
24
- return { error: new Error(`Edge Config Adapter Only Supports Config Specs`) };
23
+ return {
24
+ error: new Error(`Edge Config Adapter Only Supports Config Specs`),
25
+ };
25
26
  }
26
- const data = yield this.edgeConfigClient.get(this.configSpecsKey);
27
+ const data = yield this.edgeConfigClient.get(this.edgeConfigItemKey);
27
28
  if (data === undefined) {
28
29
  return { error: new Error(`key (${key}) does not exist`) };
29
30
  }
30
- return { result: JSON.stringify(data), };
31
+ return { result: JSON.stringify(data) };
31
32
  });
32
33
  }
33
34
  // eslint-disable-next-line @typescript-eslint/require-await
@@ -38,7 +39,7 @@ class EdgeConfigDataAdapter {
38
39
  }
39
40
  initialize() {
40
41
  return __awaiter(this, void 0, void 0, function* () {
41
- const data = yield this.edgeConfigClient.get(this.configSpecsKey);
42
+ const data = yield this.edgeConfigClient.get(this.edgeConfigItemKey);
42
43
  if (data) {
43
44
  this.supportConfigSpecPolling = true;
44
45
  }
@@ -46,8 +47,7 @@ class EdgeConfigDataAdapter {
46
47
  }
47
48
  // eslint-disable-next-line @typescript-eslint/require-await
48
49
  shutdown() {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- });
50
+ return __awaiter(this, void 0, void 0, function* () { });
51
51
  }
52
52
  supportsPollingUpdatesFor(key) {
53
53
  if (key === "statsig.cache") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statsig-node-vercel",
3
- "version": "0.2.0-beta.1",
3
+ "version": "0.2.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -12,14 +12,17 @@
12
12
  "author": "",
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
- "@vercel/edge-config": "^0.1.4",
16
- "statsig-node": "5.5.0-beta.0"
15
+ "statsig-node": "5.1.0"
16
+ },
17
+ "peerDependencies": {
18
+ "@vercel/edge-config": "^0.1.4"
17
19
  },
18
20
  "devDependencies": {
19
21
  "@babel/core": "^7.18.13",
20
22
  "@babel/preset-env": "^7.18.10",
21
23
  "@babel/preset-typescript": "^7.18.6",
22
24
  "@types/jest": "^28.1.8",
25
+ "@vercel/edge-config": "^0.2.1",
23
26
  "jest": "^29.0.0",
24
27
  "jest-fetch-mock": "^3.0.3"
25
28
  }