forked from statsig-io/vercel-data-adapter-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEdgeConfigDataAdapter.ts
83 lines (74 loc) · 2.34 KB
/
EdgeConfigDataAdapter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { AdapterResponse, IDataAdapter } from "statsig-node";
import type { EdgeConfigClient } from "@vercel/edge-config";
export class EdgeConfigDataAdapter implements IDataAdapter {
/**
* The key under which Statisg specs are stored in Edge Config
*/
private edgeConfigItemKey: string;
/**
* A fully configured Edge Config client
*/
private edgeConfigClient: EdgeConfigClient;
private supportConfigSpecPolling: boolean = false;
public constructor(options: {
/**
* The key under which Statsig specs are stored in Edge Config
*/
edgeConfigItemKey: string;
/**
* A fully configured Edge Config client.
*
* @example <caption>Creating an Edge Config client</caption>
*
* ```js
* import { createClient } from "@vercel/edge-config";
*
* createClient(process.env.EDGE_CONFIG)
* ```
*/
edgeConfigClient: EdgeConfigClient;
}) {
this.edgeConfigItemKey = options.edgeConfigItemKey;
this.edgeConfigClient = options.edgeConfigClient;
}
// eslint-disable-next-line @typescript-eslint/require-await
public async get(key: string): Promise<AdapterResponse> {
if (key !== "statsig.cache") {
return {
error: new Error(`Edge Config Adapter Only Supports Config Specs`),
};
}
const data = await this.edgeConfigClient.get(this.edgeConfigItemKey);
if (data == null) {
return { error: new Error(`key (${key}) does not exist`) };
}
if (typeof data !== "object") {
return {
error: new Error(`Edge Config value expected to be an object or array`),
};
}
return { result: data };
}
// eslint-disable-next-line @typescript-eslint/require-await
public async set(
key: string,
value: string,
time?: number | undefined
): Promise<void> {
// no-op. Statsig's Edge Config integration keeps config specs synced through Statsig's service
}
public async initialize(): Promise<void> {
const data = await this.edgeConfigClient.get(this.edgeConfigItemKey);
if (data) {
this.supportConfigSpecPolling = true;
}
}
// eslint-disable-next-line @typescript-eslint/require-await
public async shutdown(): Promise<void> {}
public supportsPollingUpdatesFor(key: string): boolean {
if (key === "statsig.cache") {
return this.supportConfigSpecPolling;
}
return false;
}
}