Skip to content

Commit b6b9768

Browse files
authored
Fetch keys flags individually instead of filtering (#245)
Fetch variable, feature, and env keys flags individually
1 parent 7ec7d5d commit b6b9768

File tree

4 files changed

+49
-17
lines changed

4 files changed

+49
-17
lines changed

src/commands/environments/get.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { Flags } from '@oclif/core'
22
import inquirer from '../../ui/autocomplete'
3-
import { fetchEnvironments } from '../../api/environments'
3+
import { fetchEnvironments,fetchEnvironmentByKey } from '../../api/environments'
44
import { EnvironmentPromptResult, environmentPrompt } from '../../ui/prompts'
55
import Base from '../base'
6+
import { batchRequests } from '../../utils/batchRequests'
67

78
export default class DetailedEnvironments extends Base {
89
static hidden = false
@@ -21,34 +22,35 @@ export default class DetailedEnvironments extends Base {
2122

2223
public async run(): Promise<void> {
2324
const { flags } = await this.parse(DetailedEnvironments)
24-
const keys = flags['keys']?.split(',') || []
25+
const keys = flags['keys']?.split(',')
2526
const { headless, project } = flags
2627
await this.requireProject(project, headless)
2728

28-
let environments = await fetchEnvironments(this.authToken, this.projectKey)
29-
if (keys.length) {
30-
environments = environments.filter((environment) =>
31-
keys.includes(environment.key) || keys.includes(environment._id))
29+
if (keys) {
30+
const environments = await batchRequests(
31+
keys,
32+
(key) => fetchEnvironmentByKey(this.authToken, this.projectKey, key)
33+
)
3234
this.writer.showResults(environments)
3335
return
34-
}
36+
}
3537

3638
// show all environments if no keys flag provided in headless mode
3739
if (flags.headless) {
40+
const environments = await fetchEnvironments(this.authToken, this.projectKey)
3841
this.writer.showResults(environments)
3942
return
4043
}
4144

42-
// prompt for keys in interactive mode
45+
// prompt for key in interactive mode
4346
const responses = await inquirer.prompt<EnvironmentPromptResult>(
4447
[environmentPrompt],
4548
{
4649
token: this.authToken,
4750
projectKey: this.projectKey
4851
}
4952
)
50-
const environment = environments.find((environment) => environment._id === responses.environment._id)
51-
this.writer.showResults(environment)
53+
this.writer.showResults(responses.environment)
5254

5355
}
5456
}

src/commands/features/get.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Flags } from '@oclif/core'
2-
import { fetchFeatures } from '../../api/features'
2+
import { fetchFeatures, fetchFeatureByKey } from '../../api/features'
33
import Base from '../base'
4+
import { batchRequests } from '../../utils/batchRequests'
45

56
export default class DetailedFeatures extends Base {
67
static hidden = false
@@ -22,10 +23,16 @@ export default class DetailedFeatures extends Base {
2223
const keys = flags['keys']?.split(',') || []
2324
const { project, headless } = flags
2425
await this.requireProject(project, headless)
25-
let features = await fetchFeatures(this.authToken, this.projectKey)
26-
if (keys.length) {
27-
features = features.filter((feature) => keys.includes(feature.key) || keys.includes(feature._id))
26+
let features
27+
if (keys) {
28+
features = await batchRequests(
29+
keys,
30+
(key) => fetchFeatureByKey(this.authToken, this.projectKey, key)
31+
)
32+
} else {
33+
features = await fetchFeatures(this.authToken, this.projectKey)
2834
}
35+
2936
this.writer.showResults(features)
3037
}
3138
}

src/commands/variables/get.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Flags } from '@oclif/core'
2-
import { fetchVariables } from '../../api/variables'
2+
import { fetchVariables, fetchVariableByKey } from '../../api/variables'
33
import Base from '../base'
4+
import { batchRequests } from '../../utils/batchRequests'
45

56
export default class DetailedVariables extends Base {
67
static hidden = false
@@ -18,9 +19,15 @@ export default class DetailedVariables extends Base {
1819
const { project, headless } = flags
1920
await this.requireProject(project, headless)
2021

21-
let variables = await fetchVariables(this.authToken, this.projectKey)
22+
let variables
2223
if (keys) {
23-
variables = variables.filter((variable) => keys.includes(variable.key))
24+
variables = await batchRequests(
25+
keys,
26+
(key) => fetchVariableByKey(this.authToken, this.projectKey, key)
27+
)
28+
29+
} else {
30+
variables = await fetchVariables(this.authToken, this.projectKey)
2431
}
2532
this.writer.showResults(variables)
2633
}

src/utils/batchRequests.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { chunk } from 'lodash'
2+
3+
export const batchRequests = async (
4+
values: string[],
5+
mapFunction: (value: string) => Promise<unknown>
6+
) => {
7+
const result = []
8+
const batches = chunk(values, 10)
9+
for (const batch of batches) {
10+
const batchResults = await Promise.all(
11+
batch.map(mapFunction)
12+
)
13+
result.push(...batchResults.filter(Boolean))
14+
}
15+
return result
16+
}

0 commit comments

Comments
 (0)