Skip to content
This repository was archived by the owner on Jan 19, 2026. It is now read-only.

Commit d528b4a

Browse files
author
Thiago Saife
authored
Merge pull request #26 from storyblok/task/int-738
task(int-738): List spaces from all regions in spaces command
2 parents 3021eaf + 23a37cd commit d528b4a

4 files changed

Lines changed: 84 additions & 22 deletions

File tree

src/cli.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,9 @@ program
435435
if (!api.isAuthorized()) {
436436
await api.processLogin()
437437
}
438+
const { region } = creds.get()
438439

439-
await tasks.listSpaces(api)
440+
await tasks.listSpaces(api, region)
440441
} catch (e) {
441442
console.log(chalk.red('X') + ' An error ocurred to listing spaces: ' + e.message)
442443
process.exit(1)

src/tasks/list-spaces.js

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,64 @@
11
const chalk = require('chalk')
2-
32
/**
43
* @method listSpaces
54
* @param api - Pass the api instance as a parameter
65
* @return {Promise}
76
*/
87

9-
const listSpaces = async (api) => {
8+
const listSpaces = async (api, currentRegion) => {
9+
const isChinaEnv = currentRegion === 'cn'
10+
const regionOptions = {
11+
eu: 'Europe',
12+
us: 'United States'
13+
}
1014
console.log()
1115
console.log(chalk.green('✓') + ' Loading spaces...')
12-
console.log()
1316

1417
if (!api) {
1518
console.log(chalk.red('X') + 'Api instance is required to make the request')
1619
return []
1720
}
1821

19-
const spaces = await api.getAllSpaces()
20-
.then(res => res)
21-
.catch(err => Promise.reject(err))
22+
if (isChinaEnv) {
23+
const spaces = await api.getAllSpacesByRegion(currentRegion)
24+
.then(res => res)
25+
.catch(err => Promise.reject(err))
2226

23-
if (!spaces) {
24-
console.log(chalk.red('X') + ' No spaces were found for this user ')
25-
return []
26-
}
27-
28-
spaces.map(space => {
29-
console.log(`${space.name} (id: ${space.id})`)
30-
})
27+
if (!spaces) {
28+
console.log(chalk.red('X') + ' No spaces were found for this user ')
29+
return []
30+
}
31+
console.log(chalk.blue(' -') + ' Spaces From China region:')
3132

32-
return spaces
33+
spaces.map(space => {
34+
console.log(` ${space.name} (id: ${space.id})`)
35+
})
36+
return spaces
37+
} else {
38+
const spacesList = []
39+
for (const key in regionOptions) {
40+
spacesList.push(await api.getAllSpacesByRegion(key)
41+
.then((res) => {
42+
return {
43+
key,
44+
res
45+
}
46+
})
47+
.catch(err => Promise.reject(err)))
48+
}
49+
if (!spacesList) {
50+
console.log(chalk.red('X') + ' No spaces were found for this user ')
51+
return []
52+
}
53+
spacesList.forEach(region => {
54+
console.log()
55+
console.log(`${chalk.blue(' -')} Spaces From ${regionOptions[region.key]} region:`)
56+
region.res.forEach((space) => {
57+
console.log(` ${space.name} (id: ${space.id})`)
58+
})
59+
})
60+
return spacesList
61+
}
3362
}
3463

3564
module.exports = listSpaces

src/utils/api.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,13 @@ module.exports = {
240240
return client[method](_path, props)
241241
},
242242

243-
async getAllSpaces () {
244-
return await this.getClient()
243+
async getAllSpacesByRegion (region) {
244+
const customClient = new Storyblok({
245+
accessToken: this.accessToken,
246+
oauthToken: this.oauthToken,
247+
region
248+
}, this.apiSwitcher(region))
249+
return await customClient
245250
.get('spaces/', {})
246251
.then(res => res.data.spaces || [])
247252
.catch(err => Promise.reject(err))

tests/units/list-spaces.spec.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
const { listSpaces } = require('../../src/tasks/')
22
const { FAKE_SPACES } = require('../constants')
33

4+
const REGION_FLAGS = {
5+
UNITED_STATES: 'us',
6+
EUROPE: 'eu',
7+
CHINA: 'cn'
8+
}
9+
410
describe('Test spaces method', () => {
511
it('Testing list-spaces funtion without api instance', async () => {
612
try {
@@ -10,13 +16,34 @@ describe('Test spaces method', () => {
1016
console.error(e)
1117
}
1218
})
13-
it('Testing list-spaces funtion with api instance', async () => {
19+
20+
it('Testing list-spaces function for China region', async () => {
1421
const FAKE_API = {
15-
getAllSpaces: jest.fn(() => Promise.resolve(FAKE_SPACES()))
22+
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
1623
}
1724
expect(
18-
await listSpaces(FAKE_API)
25+
await listSpaces(FAKE_API, REGION_FLAGS.CHINA)
1926
).toEqual(FAKE_SPACES())
20-
expect(FAKE_API.getAllSpaces).toHaveBeenCalled()
27+
expect(FAKE_API.getAllSpacesByRegion).toHaveBeenCalled()
28+
})
29+
30+
it('Testing list-spaces funtion for Europe and United States regions', async () => {
31+
const FAKE_API = {
32+
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
33+
}
34+
const response = [
35+
{
36+
key: REGION_FLAGS.EUROPE,
37+
res: [...FAKE_SPACES()]
38+
},
39+
{
40+
key: REGION_FLAGS.UNITED_STATES,
41+
res: [...FAKE_SPACES()]
42+
}
43+
]
44+
45+
expect(
46+
await listSpaces(FAKE_API, REGION_FLAGS.EUROPE)
47+
).toEqual(response)
2148
})
2249
})

0 commit comments

Comments
 (0)