Skip to content

Commit b9e57da

Browse files
committed
fix(api): fixes for new swagger json api function signature
1 parent 462b453 commit b9e57da

9 files changed

+222
-46
lines changed

API.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ as api grouping util classes are attached to this class.
7878
* [.getFeatureFlags(projectKey)](#LaunchDarklyUtilsFlags+getFeatureFlags) ⇒ <code>Promise</code>
7979
* [.getFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery)](#LaunchDarklyUtilsFlags+getFeatureFlag) ⇒ <code>Promise</code>
8080
* [.getFeatureFlagState(projectKey, featureFlagKey, environmentKeyQuery)](#LaunchDarklyUtilsFlags+getFeatureFlagState) ⇒ <code>Promise</code>
81-
* [.updateFeatureFlag(projectKey, featureFlagKey, patchComment)](#LaunchDarklyUtilsFlags+updateFeatureFlag) ⇒ <code>Promise</code>
81+
* [.updateFeatureFlag(projectKey, featureFlagKey, patch)](#LaunchDarklyUtilsFlags+updateFeatureFlag) ⇒ <code>Promise</code>
8282
* [.toggleFeatureFlag(projectKey, featureFlagKey, environmentKeyQuery, value)](#LaunchDarklyUtilsFlags+toggleFeatureFlag) ⇒ <code>Promise</code>
8383
* [.migrateFeatureFlag(projectKey, featureFlagKey, fromEnv, toEnv, includeState)](#LaunchDarklyUtilsFlags+migrateFeatureFlag) ⇒ <code>Promise</code>
8484
* [.bulkMigrateFeatureFlags(projectKey, featureFlagKeys, fromEnv, toEnv, includeState)](#LaunchDarklyUtilsFlags+bulkMigrateFeatureFlags) ⇒ <code>Promise</code>
@@ -160,7 +160,7 @@ ldutils getFeatureFlagState my-project my-flag dev
160160
```
161161
<a name="LaunchDarklyUtilsFlags+updateFeatureFlag"></a>
162162

163-
### launchDarklyUtilsFlags.updateFeatureFlag(projectKey, featureFlagKey, patchComment) ⇒ <code>Promise</code>
163+
### launchDarklyUtilsFlags.updateFeatureFlag(projectKey, featureFlagKey, patch) ⇒ <code>Promise</code>
164164
patch a feature flag by key
165165

166166
**Kind**: instance method of [<code>LaunchDarklyUtilsFlags</code>](#LaunchDarklyUtilsFlags)
@@ -171,7 +171,7 @@ patch a feature flag by key
171171
| --- | --- | --- |
172172
| projectKey | <code>string</code> | project identifier |
173173
| featureFlagKey | <code>string</code> | feature flag identifier |
174-
| patchComment | <code>Array.&lt;Object&gt;</code> | array of valid json patch descriptors |
174+
| patch | <code>Array.&lt;Object&gt;</code> | array of valid json patch descriptors |
175175

176176
**Example**
177177
```js

src/LaunchDarklyApiClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Swagger from 'swagger-client';
2-
//import jsYaml from 'js-yaml';
32
import { default as fs } from 'fs';
43
import { default as json } from 'format-json';
54
import { default as HttpsProxyAgent } from 'https-proxy-agent';
@@ -35,6 +34,7 @@ export class LaunchDarklyApiClient {
3534
return Swagger({
3635
spec: JSON.parse(openapiJson),
3736
usePromise: true,
37+
requestContentType: 'application/json',
3838
requestInterceptor: req => {
3939
req.userFetch = fetch;
4040
req.agent = agent;

src/LaunchDarklyUtilsFlags.js

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -104,20 +104,24 @@ export class LaunchDarklyUtilsFlags {
104104
* patch a feature flag by key
105105
* @param {string} projectKey - project identifier
106106
* @param {string} featureFlagKey - feature flag identifier
107-
* @param {Array<Object>} patchComment - array of valid json patch descriptors
107+
* @param {Array<Object>} patch - array of valid json patch descriptors
108108
* @returns {Promise}
109109
* @fulfil {Object} updated feature flag json
110110
* @reject {Error} object with message
111111
* @example ldutils updateFeatureFlag my-project my-flag {jsonPatch}
112112
*/
113-
async updateFeatureFlag(projectKey, featureFlagKey, patchComment) {
113+
async updateFeatureFlag(projectKey, featureFlagKey, patch) {
114114
try {
115115
return this.apiClient.apis[this.API_GROUP]
116-
.patchFeatureFlag({
117-
projectKey: projectKey,
118-
featureFlagKey: featureFlagKey,
119-
patchComment: patchComment
120-
})
116+
.patchFeatureFlag(
117+
{
118+
projectKey: projectKey,
119+
featureFlagKey: featureFlagKey
120+
},
121+
{
122+
requestBody: patch
123+
}
124+
)
121125
.then(response => {
122126
return response.body;
123127
});
@@ -170,15 +174,15 @@ export class LaunchDarklyUtilsFlags {
170174
return patchDelta;
171175
})
172176
.then(patchDelta => {
173-
let patchComment = this.assembleFlagPatch(patchDelta, toEnv, includeState);
177+
let patch = this.assembleFlagPatch(patchDelta, toEnv, includeState);
174178

175-
that.log.debug(`patchComment for '${featureFlagKey}' in ${toEnv} : ${json.plain(patchComment)}`);
176-
return this.updateFeatureFlag(projectKey, featureFlagKey, patchComment);
179+
that.log.debug(`patch for '${featureFlagKey}' in ${toEnv} : ${json.plain(patch)}`);
180+
return this.updateFeatureFlag(projectKey, featureFlagKey, patch);
177181
});
178182
}
179183

180184
assembleFlagPatch(patchDelta, targetEnv, includeState) {
181-
let patchComment = [];
185+
let patches = [];
182186
patchDelta.forEach(patch => {
183187
if (
184188
patch.path.startsWith('/targets') ||
@@ -190,10 +194,10 @@ export class LaunchDarklyUtilsFlags {
190194
) {
191195
// add target env obj path and push
192196
patch.path = `/environments/${targetEnv}${patch.path}`;
193-
patchComment.push(patch);
197+
patches.push(patch);
194198
}
195199
});
196-
return patchComment;
200+
return patches;
197201
}
198202

199203
/**
@@ -256,10 +260,10 @@ export class LaunchDarklyUtilsFlags {
256260
return patchDelta;
257261
})
258262
.then(patchDelta => {
259-
let patchComment = this.assembleFlagPatch(patchDelta, targetEnv, includeState);
263+
let patch = this.assembleFlagPatch(patchDelta, targetEnv, includeState);
260264

261-
that.log.debug(`patchComment for '${key}' in ${targetEnv} : ${json.plain(patchComment)}`);
262-
return this.updateFeatureFlag(projectKey, key, patchComment);
265+
that.log.debug(`patch for '${key}' in ${targetEnv} : ${json.plain(patch)}`);
266+
return this.updateFeatureFlag(projectKey, key, patch);
263267
})
264268
);
265269
});

src/LaunchDarklyUtilsMembers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export class LaunchDarklyUtilsMembers {
145145
!defaultRoles.includes(initialRoleKey) ? (user.customRoles = [initialRoleKey]) : (user.role = initialRoleKey);
146146
members.membersBody.push(user);
147147
try {
148-
return this.apiClient.apis[this.API_GROUP].postMembers(members).then(response => {
148+
return this.apiClient.apis[this.API_GROUP].postMembers({}, { requestBody: members }).then(response => {
149149
return response.body;
150150
});
151151
} catch (e) {

src/LaunchDarklyUtilsProjects.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export class LaunchDarklyUtilsProjects {
132132
defaultClientSideAvailability
133133
};
134134
return this.apiClient.apis[this.API_GROUP]
135-
.postProject({ projectBody: newProject })
135+
.postProject({}, { requestBody: newProject })
136136
.then(response => {
137137
return response.body;
138138
})
@@ -156,7 +156,7 @@ export class LaunchDarklyUtilsProjects {
156156
*/
157157
async updateProject(projectKey, jsonPatch) {
158158
return this.apiClient.apis[this.API_GROUP]
159-
.patchProject({ projectKey: projectKey, patchDelta: jsonPatch })
159+
.patchProject({ projectKey: projectKey }, { requestBody: jsonPatch })
160160
.then(response => {
161161
return response.body;
162162
})

src/LaunchDarklyUtilsRoles.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ export class LaunchDarklyUtilsRoles {
121121
policy: customRolePolicyArray
122122
};
123123
try {
124-
return this.apiClient.apis[this.API_GROUP].postCustomRole({ customRoleBody: customRole }).then(response => {
125-
return response.body;
126-
});
124+
return this.apiClient.apis[this.API_GROUP]
125+
.postCustomRole({}, { requestBody: customRole })
126+
.then(response => {
127+
return response.body;
128+
});
127129
} catch (e) {
128130
throw {
129131
api: 'postCustomRole',
@@ -163,10 +165,14 @@ export class LaunchDarklyUtilsRoles {
163165
.then(patchDelta => {
164166
try {
165167
return this.apiClient.apis[this.API_GROUP]
166-
.patchCustomRole({
167-
customRoleKey: customRoleKey,
168-
patchDelta: patchDelta
169-
})
168+
.patchCustomRole(
169+
{
170+
customRoleKey: customRoleKey
171+
},
172+
{
173+
requestBody: patchDelta
174+
}
175+
)
170176
.then(response => {
171177
return response.body;
172178
});

test/LaunchDarklyUtilsFlags.spec.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ describe('LaunchDarklyUtilsFlags', () => {
7272
});
7373

7474
describe('updateFeatureFlag', () => {
75+
let patchBody;
7576
before(done => {
7677
let scope = nock('https://app.launchdarkly.com')
77-
.patch('/api/v2/flags/sample-project/sort.order')
78+
.patch('/api/v2/flags/sample-project/sort.order', body => {
79+
patchBody = body;
80+
return body;
81+
})
7882
.replyWithFile(200, __dirname + '/fixtures/feature-flags-update.json', {
7983
'Content-Type': 'application/json'
8084
});
@@ -90,14 +94,21 @@ describe('LaunchDarklyUtilsFlags', () => {
9094
])
9195
.then(actual => {
9296
expect(actual).to.deep.equal(expected);
97+
expect(patchBody).to.deep.equal([
98+
{ op: 'replace', path: '/environments/production/on', value: false }
99+
]);
93100
});
94101
});
95102
});
96103

97104
describe('toggleFeatureFlag', () => {
105+
let patchBody;
98106
before(done => {
99107
let scope = nock('https://app.launchdarkly.com')
100-
.patch('/api/v2/flags/sample-project/sort.order')
108+
.patch('/api/v2/flags/sample-project/sort.order', body => {
109+
patchBody = body;
110+
return body;
111+
})
101112
.replyWithFile(200, __dirname + '/fixtures/feature-flags-update.json', {
102113
'Content-Type': 'application/json'
103114
});
@@ -109,6 +120,7 @@ describe('LaunchDarklyUtilsFlags', () => {
109120
let expected = JSON.parse(fs.readFileSync(__dirname + '/fixtures/feature-flags-update.json', 'utf-8'));
110121
return ldutils.flags.toggleFeatureFlag('sample-project', 'sort.order', 'test', true).then(actual => {
111122
expect(actual).to.deep.equal(expected);
123+
expect(patchBody).to.deep.equal([{ op: 'replace', path: `/environments/test/on`, value: true }]);
112124
});
113125
});
114126
});

0 commit comments

Comments
 (0)