Skip to content

Commit 84ad989

Browse files
committed
refactor(utils): refactor utils into api group classes
BREAKING CHANGE: utils in ldUtils object are now grouped below type eg. flags, roles resolves #4
1 parent 9386a7a commit 84ad989

File tree

5 files changed

+168
-134
lines changed

5 files changed

+168
-134
lines changed

api.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dotenv.config();
66
let log = LaunchDarklyLogger.logger();
77

88
let args = process.argv.slice(2);
9-
log.info(`command line args: ${args}`);
9+
log.debug(`command line args: ${args}`);
1010

1111
(async () => {
1212

@@ -23,7 +23,7 @@ log.info(`command line args: ${args}`);
2323
result = 'please supply a projectKey as second parameter'
2424
break;
2525
}
26-
result = await ldUtils.getFeatureFlags(projectKey);
26+
result = await ldUtils.flags.getFeatureFlags(projectKey);
2727
break;
2828

2929
case 'getFeatureFlag':
@@ -42,7 +42,7 @@ log.info(`command line args: ${args}`);
4242
result = 'please supply a environmentKeyQuery as fourth parameter';
4343
break;
4444
}
45-
result = await ldUtils.getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery);
45+
result = await ldUtils.flags.getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery);
4646
break;
4747

4848
case 'getFeatureFlagState':
@@ -61,7 +61,7 @@ log.info(`command line args: ${args}`);
6161
result = 'please supply a environmentKeyQuery as fourth parameter';
6262
break;
6363
}
64-
result = await ldUtils.getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery);
64+
result = await ldUtils.flags.getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery);
6565
break;
6666

6767
case 'toggleFeatureFlag':
@@ -86,11 +86,11 @@ log.info(`command line args: ${args}`);
8686
break;
8787
}
8888
enabled = enabled === 'true';
89-
result = await ldUtils.toggleFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery, enabled);
89+
result = await ldUtils.flags.toggleFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery, enabled);
9090
break;
9191

9292
case 'getCustomRoles':
93-
result = await ldUtils.getCustomRoles();
93+
result = await ldUtils.roles.getCustomRoles();
9494
break;
9595

9696
case 'getCustomRole':
@@ -99,7 +99,7 @@ log.info(`command line args: ${args}`);
9999
result = 'please supply a customRoleKey as second parameter'
100100
break;
101101
}
102-
result = await ldUtils.getCustomRole(customRoleKey);
102+
result = await ldUtils.roles.getCustomRole(customRoleKey);
103103
break;
104104

105105
case 'createCustomRole':
@@ -123,7 +123,7 @@ log.info(`command line args: ${args}`);
123123
break;
124124
}
125125

126-
result = await ldUtils.createCustomRole(
126+
result = await ldUtils.roles.createCustomRole(
127127
customRoleKey,
128128
customRoleName,
129129
customRolePolicyArray,
@@ -152,7 +152,7 @@ log.info(`command line args: ${args}`);
152152
result = 'please supply a customRolePolicyArray as fourth parameter';
153153
break;
154154
}
155-
result = await ldUtils.updateCustomRole(
155+
result = await ldUtils.roles.updateCustomRole(
156156
customRoleKey,
157157
customRoleName,
158158
customRolePolicyArray,
@@ -180,7 +180,7 @@ log.info(`command line args: ${args}`);
180180
result = 'please supply a customRolePolicyArray as fourth parameter';
181181
break;
182182
}
183-
result = await ldUtils.upsertCustomRole(
183+
result = await ldUtils.roles.upsertCustomRole(
184184
customRoleKey,
185185
customRoleName,
186186
customRolePolicyArray,
@@ -190,7 +190,7 @@ log.info(`command line args: ${args}`);
190190

191191
case 'bulkUpsertCustomRoles':
192192
let roleBulkLoadFile = args[1];
193-
result = await ldUtils.bulkUpsertCustomRoles(roleBulkLoadFile);
193+
result = await ldUtils.roles.bulkUpsertCustomRoles(roleBulkLoadFile);
194194

195195
break;
196196

src/LaunchDarklyApiClient.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { default as fs } from 'fs';
44

55
export class LaunchDarklyApiClient {
66
static async create(API_TOKEN, log) {
7-
log.info(`creating client with api token: ${API_TOKEN}`);
7+
log.debug(`creating api client with token: ${API_TOKEN}`);
88

99
// swagger.yaml from https://launchdarkly.github.io/ld-openapi/swagger.yaml
1010
const yaml = fs.readFileSync(__dirname + `/../swagger.yaml`, 'utf-8').toString();
@@ -16,9 +16,6 @@ export class LaunchDarklyApiClient {
1616
requestInterceptor: req => {
1717
req.headers.Authorization = API_TOKEN;
1818

19-
// hack - fix incorrect qs mapping..
20-
req.url = req.url.replace('environmentKeyQuery', 'env');
21-
2219
log.debug(req);
2320

2421
return req;

src/LaunchDarklyUtils.js

Lines changed: 15 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,28 @@
1+
import { LaunchDarklyUtilsFlags } from './LaunchDarklyUtilsFlags';
2+
import { LaunchDarklyUtilsRoles } from './LaunchDarklyUtilsRoles';
13
import { LaunchDarklyApiClient } from './LaunchDarklyApiClient';
24
import { LaunchDarklyLogger } from './LaunchDarklyLogger';
3-
import { default as jsonPatch } from 'fast-json-patch';
45
import { default as dotenv } from 'dotenv';
5-
import { default as fs } from 'fs';
66
dotenv.config();
77

8-
let log = LaunchDarklyLogger.logger();
9-
108
export class LaunchDarklyUtils {
119
async create(API_TOKEN, customLogger) {
12-
if (customLogger) log = customLogger;
13-
this.apiClient = await LaunchDarklyApiClient.create(API_TOKEN, log);
14-
log.debug(this.apiClient.apis);
15-
return this;
16-
}
10+
// setup logger
11+
this.log = customLogger ? customLogger : LaunchDarklyLogger.logger();
12+
this.log.debug('logger attached..');
1713

18-
async getFeatureFlags(projectKey) {
19-
return this.apiClient.apis['Feature flags'].getFeatureFlags({ projectKey: projectKey });
20-
}
14+
// create LaunchDarkly apiClient
15+
this.apiClient = await LaunchDarklyApiClient.create(API_TOKEN, this.log);
16+
this.log.debug('api client instantiated..');
2117

22-
async getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery) {
23-
return this.apiClient.apis['Feature flags'].getFeatureFlag({
24-
projectKey: projectKey,
25-
featureFlagKey: featureFlagKey,
26-
environmentKeyQuery: environmentKeyQuery
27-
});
28-
}
18+
// attach flag utils
19+
this.flags = new LaunchDarklyUtilsFlags(this.apiClient, this.log);
20+
this.log.debug(`flag functions: ${this.flags}`);
2921

30-
async getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery) {
31-
return this.getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery).then(result => {
32-
return result.obj.environments[environmentKeyQuery].on;
33-
});
34-
}
22+
// attach role utils
23+
this.roles = new LaunchDarklyUtilsRoles(this.apiClient, this.log);
24+
this.log.debug(`role functions: ${this.roles}`);
3525

36-
async toggleFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery, value) {
37-
return this.apiClient.apis['Feature flags'].patchFeatureFlag({
38-
projectKey: projectKey,
39-
featureFlagKey: featureFlagKey,
40-
patchDelta: [{ op: 'replace', path: `/environments/${environmentKeyQuery}/on`, value: value }]
41-
});
42-
}
43-
44-
async getCustomRoles() {
45-
return this.apiClient.apis['Custom roles'].getCustomRoles();
46-
}
47-
48-
async getCustomRole(customRoleKey) {
49-
return this.apiClient.apis['Custom roles']
50-
.getCustomRole({
51-
customRoleKey: customRoleKey
52-
})
53-
.catch(e => {
54-
log.error(e);
55-
throw e;
56-
});
57-
}
58-
59-
async createCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {
60-
let customRole = {
61-
name: customRoleName,
62-
key: customRoleKey,
63-
description: customRoleDescription,
64-
policy: customRolePolicyArray
65-
};
66-
return this.apiClient.apis['Custom roles'].postCustomRole({ customRoleBody: customRole });
67-
}
68-
69-
async updateCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {
70-
let updatedCustomRole = {
71-
name: customRoleName,
72-
key: customRoleKey,
73-
description: customRoleDescription,
74-
policy: customRolePolicyArray
75-
};
76-
77-
return this.getCustomRole(customRoleKey)
78-
79-
.then(customRoleResponse => {
80-
let patchDelta = jsonPatch.compare(customRoleResponse.obj, updatedCustomRole);
81-
log.debug(`customRoleDiff for '${customRoleKey}' ${JSON.stringify(patchDelta)}`);
82-
return patchDelta;
83-
})
84-
.then(patchDelta => {
85-
return this.apiClient.apis['Custom roles'].patchCustomRole({
86-
customRoleKey: customRoleKey,
87-
patchDelta: patchDelta
88-
});
89-
});
90-
}
91-
92-
async upsertCustomRole(customRoleKey, customRoleName, customRolePolicyArray, customRoleDescription) {
93-
return this.getCustomRole(customRoleKey)
94-
95-
.then(() => {
96-
log.info(`role '${customRoleKey}' found, updating..`);
97-
return this.updateCustomRole(
98-
customRoleKey,
99-
customRoleName,
100-
customRolePolicyArray,
101-
customRoleDescription
102-
);
103-
})
104-
105-
.catch(() => {
106-
log.info(`role '${customRoleKey}' not found, creating..`);
107-
return this.createCustomRole(
108-
customRoleKey,
109-
customRoleName,
110-
customRolePolicyArray,
111-
customRoleDescription
112-
);
113-
});
114-
}
115-
116-
async bulkUpsertCustomRoles(roleBulkLoadFile) {
117-
let filePath = `${process.cwd()}/${roleBulkLoadFile}`;
118-
let roles = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
119-
let that = this;
120-
121-
log.info(`bulk upserting roles from file: ${filePath}`);
122-
123-
return roles.reduce(function(acc, role) {
124-
return acc.then(function(results) {
125-
return that.upsertCustomRole(role.key, role.name, role.policy, role.description).then(function(data) {
126-
results.push(data);
127-
return results;
128-
});
129-
});
130-
}, Promise.resolve([]));
26+
return this;
13127
}
13228
}

src/LaunchDarklyUtilsFlags.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
export class LaunchDarklyUtilsFlags {
2+
constructor(apiClient, log) {
3+
this.log = log;
4+
this.apiClient = apiClient;
5+
}
6+
7+
get API_GROUP() {
8+
return 'Feature flags';
9+
}
10+
11+
async getFeatureFlags(projectKey) {
12+
return this.apiClient.apis[this.API_GROUP].getFeatureFlags({ projectKey: projectKey });
13+
}
14+
15+
async getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery) {
16+
return this.apiClient.apis[this.API_GROUP].getFeatureFlag({
17+
projectKey: projectKey,
18+
featureFlagKey: featureFlagKey,
19+
environmentKeyQuery: environmentKeyQuery
20+
});
21+
}
22+
23+
async getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery) {
24+
return this.getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery).then(result => {
25+
return result.obj.environments[environmentKeyQuery].on;
26+
});
27+
}
28+
29+
async toggleFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery, value) {
30+
return this.apiClient.apis[this.API_GROUP].patchFeatureFlag({
31+
projectKey: projectKey,
32+
featureFlagKey: featureFlagKey,
33+
patchDelta: [{ op: 'replace', path: `/environments/${environmentKeyQuery}/on`, value: value }]
34+
});
35+
}
36+
}

0 commit comments

Comments
 (0)