Skip to content

Commit 0d331cc

Browse files
Merge pull request #17 from drone/async_repository
[MAINT] Async repository
2 parents 16bdd62 + 78b8630 commit 0d331cc

22 files changed

+9076
-583
lines changed

.prettierrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"trailingComma": "all",
44
"overrides": [
55
{
6-
"files": "*.ts",
6+
"files": ["*.ts", "*.js", "*.mjs"],
77
"options": {
88
"parser": "typescript"
99
}

README.md

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ const target = {
5555
```
5656

5757
### Evaluate the flag with default value set to false
58-
```
59-
const value = client.boolVariation('test', target, false);
58+
```typescript
59+
const value = await client.boolVariation('test', target, false);
6060
```
6161

6262
### Shutting down SDK
@@ -65,25 +65,26 @@ client.close();
6565
```
6666

6767
### Avaialable public methods
68-
```
69-
const value = client.boolVariation('test', target, false);
70-
const value = client.stringVariation('test', target, 'BLACK');
71-
const value = client.numberVariation('test', target, 1);
72-
const value = client.jsonVariation('test', target, {});
68+
```typescript
69+
function boolVariation(identifier: string, target: Target, defaultValue = true): Promise<boolean>;
70+
function stringVariation(identifier, target: Target, defaultValue = ''): Promise<string>;
71+
function numberVariation(identifier, target: Target, defaultValue = 1.0): Promise<number>;
72+
function jsonVariation(identifier, target: Target, defaultValue = {}): Promise<Record<string, unknown>>;
73+
function close();
7374
```
7475

7576
### Avaialable options
7677

7778
```
78-
baseUrl: string;
79-
eventsUrl: string;
80-
pollInterval: number;
81-
eventsSyncInterval: number;
82-
enableStream: boolean;
83-
enableAnalytics: boolean;
84-
cache: KeyValueStore;
85-
store: KeyValueStore;
86-
logger: Logger;
79+
baseUrl: string; // baseUrl is where the flag configurations are located
80+
eventsUrl: string; // eventsUrl is where we send summarized target events
81+
pollInterval: number; // pollInterval (default 60s)
82+
eventsSyncInterval: number; // Metrics push event (default 60s)
83+
enableStream: boolean; // enable server sent events
84+
enableAnalytics: boolean; // enable analytics
85+
cache: KeyValueStore; // set custom cache (default lru cache)
86+
store: AsyncKeyValueStore; // set custom persistent store (default file store)
87+
logger: Logger; // set logger (default console)
8788
```
8889

8990
## Singleton example
@@ -101,8 +102,8 @@ const target = {
101102
};
102103
const defaultValue = false;
103104
104-
setInterval(() => {
105-
const value = CfClient.boolVariation(FLAG_KEY, target, defaultValue);
105+
setInterval(async() => {
106+
const value = await CfClient.boolVariation(FLAG_KEY, target, defaultValue);
106107
console.log("Evaluation for flag test and target none: ", value);
107108
}, 10000);
108109
```

__tests__/evaluator.test.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const cache = new SimpleCache();
2020
const repository = new StorageRepository(cache);
2121
const evaluator = new Evaluator(repository);
2222

23+
const results = [];
2324
let files = [];
2425
try {
2526
files = fs.readdirSync(directory);
@@ -29,8 +30,6 @@ try {
2930
}
3031
}
3132

32-
const results = [];
33-
3433
for (const file of files) {
3534
try {
3635
const data = fs.readFileSync(path.join(directory, file), 'utf8');
@@ -56,7 +55,7 @@ for (const file of files) {
5655
describe('evaluation flag', () => {
5756
test.each(results)(
5857
`Usecase %p with target %p and expected value %p`,
59-
(
58+
async (
6059
_file: string,
6160
targetIdentifier: string,
6261
expected: unknown,
@@ -73,24 +72,32 @@ describe('evaluation flag', () => {
7372
let received: unknown;
7473
switch (usecase.flag.kind) {
7574
case FeatureConfigKindEnum.Boolean:
76-
received = evaluator.boolVariation(
75+
received = await evaluator.boolVariation(
7776
usecase.flag.feature,
7877
target,
7978
false,
8079
);
8180
break;
8281
case FeatureConfigKindEnum.String:
83-
received = evaluator.stringVariation(
82+
received = await evaluator.stringVariation(
8483
usecase.flag.feature,
8584
target,
8685
'',
8786
);
8887
break;
8988
case FeatureConfigKindEnum.Int:
90-
received = evaluator.numberVariation(usecase.flag.feature, target, 0);
89+
received = await evaluator.numberVariation(
90+
usecase.flag.feature,
91+
target,
92+
0,
93+
);
9194
break;
9295
case FeatureConfigKindEnum.Json:
93-
received = evaluator.jsonVariation(usecase.flag.feature, target, {});
96+
received = await evaluator.jsonVariation(
97+
usecase.flag.feature,
98+
target,
99+
{},
100+
);
94101
break;
95102
}
96103
expect(received).toBe(expected);

example/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ const client = new Client('1c100d25-4c3f-487b-b198-3b3d01df5794');
44

55
console.log('Starting application');
66

7-
setInterval(() => {
7+
setInterval(async () => {
88
const target = {
99
identifier: 'harness',
1010
};
11-
const value = client.boolVariation('test', target, false);
11+
const value = await client.boolVariation('test', target, false);
1212
console.log('Evaluation for flag test and target: ', value, target);
1313
}, 10000);
1414

example/index.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Client } from 'ff-nodejs-server-sdk';
22

33
const client = new Client('1c100d25-4c3f-487b-b198-3b3d01df5794');
44

5-
setInterval(() => {
6-
const value = client.boolVariation('test', null, false);
7-
console.log("Evaluation for flag test and target none: ", value);
5+
setInterval(async() => {
6+
const value = await client.boolVariation('test', null, false);
7+
console.log('Evaluation for flag test and target none: ', value);
88
}, 10000);

example/index_cf.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import CfClient from 'ff-nodejs-server-sdk';
22

33
CfClient.init('1c100d25-4c3f-487b-b198-3b3d01df5794');
44

5-
setInterval(() => {
6-
const value = CfClient.boolVariation('test', null, false);
5+
setInterval(async() => {
6+
const value = await CfClient.boolVariation('test', null, false);
77
console.log("Evaluation for flag test and target none: ", value);
88
}, 10000);

0 commit comments

Comments
 (0)