forked from open-telemetry/opentelemetry-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeatureFlag.gateway.ts
75 lines (63 loc) · 2.77 KB
/
FeatureFlag.gateway.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
import {ChannelCredentials, status} from '@grpc/grpc-js';
import {
CreateFlagRequest,
CreateFlagResponse,
DeleteFlagResponse, EvaluateProbabilityFeatureFlagResponse,
FeatureFlagServiceClient, GetFeatureFlagValueResponse,
ListFlagsResponse, UpdateFlagValueRequest, UpdateFlagValueResponse,
} from '../../protos/demo';
const {FEATURE_FLAG_GRPC_SERVICE_ADDR = ''} = process.env;
const client = new FeatureFlagServiceClient(FEATURE_FLAG_GRPC_SERVICE_ADDR, ChannelCredentials.createInsecure());
const FeatureFlagGateway = () => ({
getFeatureFlagValue(name: string) {
return new Promise<GetFeatureFlagValueResponse>((resolve, reject) =>
client.getFeatureFlagValue({name}, (error, response) => (error ? reject(error) : resolve(response)))
);
},
evaluateProbabilityFeatureFlag(name: string) {
return new Promise<EvaluateProbabilityFeatureFlagResponse>((resolve, reject) =>
client.evaluateProbabilityFeatureFlag({name}, (error, response) => (error ? reject(error) : resolve(response)))
);
},
getRangeFeatureFlag(name: string) {
return new Promise<EvaluateProbabilityFeatureFlagResponse>((resolve, reject) =>
client.getRangeFeatureFlag({
name,
nameLowerBound: `${name}LowerBound`,
nameUpperBound: `${name}UpperBound`
}, (error, response) => (error ? reject(error) : resolve(response)))
);
},
createFlag(flag: CreateFlagRequest) {
return new Promise<CreateFlagResponse>((resolve, reject) =>
client.createFlag(flag, (error, response) => (error ? reject(error) : resolve(response)))
);
},
updateFlagValue(name: string, flag: UpdateFlagValueRequest) {
return new Promise<UpdateFlagValueResponse>((resolve, reject) => client.updateFlagValue(flag, (error, response) => (error ? reject(error) : resolve(response)))
);
},
listFlags() {
return new Promise<ListFlagsResponse>((resolve, reject) =>
client.listFlags({}, (error, response) => (error ? reject(error) : resolve(response)))
);
},
deleteFlag(name: string) {
return new Promise<DeleteFlagResponse>((resolve, reject) => client.deleteFlag({name},
(error, response) => {
if (error) {
if (error.code === status.NOT_FOUND) {
return resolve({} as DeleteFlagResponse)
} else {
return reject(error)
}
}
resolve(response)
}
)
);
},
});
export default FeatureFlagGateway();