statsig-node-vercel 0.2.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,65 +1,43 @@
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);
46
- if (data === undefined) {
20
+ const data = await this.edgeConfigClient.get(this.configSpecsKey);
21
+ if (data == null) {
47
22
  return { error: new Error(`key (${key}) does not exist`) };
48
23
  }
49
- return { result: JSON.stringify(data) };
24
+ if (typeof data !== "object") {
25
+ return { error: new Error(`Edge Config value expected to be an object or array`) };
26
+ }
27
+ return { result: data };
50
28
  }
51
29
 
52
30
  // eslint-disable-next-line @typescript-eslint/require-await
53
31
  public async set(
54
32
  key: string,
55
33
  value: string,
56
- time?: number | undefined
34
+ time?: number | undefined,
57
35
  ): Promise<void> {
58
36
  // no-op. Statsig's Edge Config integration keeps config specs synced through Statsig's service
59
37
  }
60
38
 
61
39
  public async initialize(): Promise<void> {
62
- const data = await this.edgeConfigClient.get(this.edgeConfigItemKey);
40
+ const data = await this.edgeConfigClient.get(this.configSpecsKey);
63
41
 
64
42
  if (data) {
65
43
  this.supportConfigSpecPolling = true;
@@ -67,7 +45,9 @@ export class EdgeConfigDataAdapter implements IDataAdapter {
67
45
  }
68
46
 
69
47
  // eslint-disable-next-line @typescript-eslint/require-await
70
- public async shutdown(): Promise<void> {}
48
+ public async shutdown(): Promise<void> {
49
+
50
+ }
71
51
 
72
52
  public supportsPollingUpdatesFor(key: string): boolean {
73
53
  if (key === "statsig.cache") {
package/README.md CHANGED
@@ -1,40 +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("KEY_FROM_INSTALLATION");
34
21
  ```
35
-
22
+ const dataAdapter = new EdgeConfigDataAdapter('KEY_FROM_INSTALLATION');
23
+ ```
36
24
  6. When initializing the `statsig` sdk, add the adapter to options
37
-
38
- ```js
39
- await statsig.initialize("server-secret-key", { dataAdapter: dataAdapter });
25
+ ```
26
+ await statsig.initialize(
27
+ 'server-secret-key',
28
+ { dataAdapter: dataAdapter },
29
+ );
40
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,25 +10,27 @@ 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);
28
- if (data === undefined) {
26
+ const data = yield this.edgeConfigClient.get(this.configSpecsKey);
27
+ if (data == null) {
29
28
  return { error: new Error(`key (${key}) does not exist`) };
30
29
  }
31
- return { result: JSON.stringify(data) };
30
+ if (typeof data !== "object") {
31
+ return { error: new Error(`Edge Config value expected to be an object or array`) };
32
+ }
33
+ return { result: data };
32
34
  });
33
35
  }
34
36
  // eslint-disable-next-line @typescript-eslint/require-await
@@ -39,7 +41,7 @@ class EdgeConfigDataAdapter {
39
41
  }
40
42
  initialize() {
41
43
  return __awaiter(this, void 0, void 0, function* () {
42
- const data = yield this.edgeConfigClient.get(this.edgeConfigItemKey);
44
+ const data = yield this.edgeConfigClient.get(this.configSpecsKey);
43
45
  if (data) {
44
46
  this.supportConfigSpecPolling = true;
45
47
  }
@@ -47,7 +49,8 @@ class EdgeConfigDataAdapter {
47
49
  }
48
50
  // eslint-disable-next-line @typescript-eslint/require-await
49
51
  shutdown() {
50
- return __awaiter(this, void 0, void 0, function* () { });
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ });
51
54
  }
52
55
  supportsPollingUpdatesFor(key) {
53
56
  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",
3
+ "version": "0.5.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -12,7 +12,7 @@
12
12
  "author": "",
13
13
  "license": "ISC",
14
14
  "dependencies": {
15
- "statsig-node": "5.1.0"
15
+ "statsig-node": "5.22.0"
16
16
  },
17
17
  "peerDependencies": {
18
18
  "@vercel/edge-config": "^0.1.4"
@@ -22,6 +22,7 @@
22
22
  "@babel/preset-env": "^7.18.10",
23
23
  "@babel/preset-typescript": "^7.18.6",
24
24
  "@types/jest": "^28.1.8",
25
+ "@types/node": "^20.14.10",
25
26
  "@vercel/edge-config": "^0.2.1",
26
27
  "jest": "^29.0.0",
27
28
  "jest-fetch-mock": "^3.0.3"