storyblok 3.16.1 → 3.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/cli.js +2 -1
- package/src/tasks/list-spaces.js +44 -15
- package/src/utils/api.js +7 -2
- package/tests/units/list-spaces.spec.js +31 -4
package/package.json
CHANGED
package/src/cli.js
CHANGED
|
@@ -435,8 +435,9 @@ program
|
|
|
435
435
|
if (!api.isAuthorized()) {
|
|
436
436
|
await api.processLogin()
|
|
437
437
|
}
|
|
438
|
+
const { region } = creds.get()
|
|
438
439
|
|
|
439
|
-
await tasks.listSpaces(api)
|
|
440
|
+
await tasks.listSpaces(api, region)
|
|
440
441
|
} catch (e) {
|
|
441
442
|
console.log(chalk.red('X') + ' An error ocurred to listing spaces: ' + e.message)
|
|
442
443
|
process.exit(1)
|
package/src/tasks/list-spaces.js
CHANGED
|
@@ -1,35 +1,64 @@
|
|
|
1
1
|
const chalk = require('chalk')
|
|
2
|
-
|
|
3
2
|
/**
|
|
4
3
|
* @method listSpaces
|
|
5
4
|
* @param api - Pass the api instance as a parameter
|
|
6
5
|
* @return {Promise}
|
|
7
6
|
*/
|
|
8
7
|
|
|
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
|
+
}
|
|
10
14
|
console.log()
|
|
11
15
|
console.log(chalk.green('✓') + ' Loading spaces...')
|
|
12
|
-
console.log()
|
|
13
16
|
|
|
14
17
|
if (!api) {
|
|
15
18
|
console.log(chalk.red('X') + 'Api instance is required to make the request')
|
|
16
19
|
return []
|
|
17
20
|
}
|
|
18
21
|
|
|
19
|
-
|
|
20
|
-
.
|
|
21
|
-
|
|
22
|
+
if (isChinaEnv) {
|
|
23
|
+
const spaces = await api.getAllSpacesByRegion(currentRegion)
|
|
24
|
+
.then(res => res)
|
|
25
|
+
.catch(err => Promise.reject(err))
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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:')
|
|
31
32
|
|
|
32
|
-
|
|
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
|
+
}
|
|
33
62
|
}
|
|
34
63
|
|
|
35
64
|
module.exports = listSpaces
|
package/src/utils/api.js
CHANGED
|
@@ -240,8 +240,13 @@ module.exports = {
|
|
|
240
240
|
return client[method](_path, props)
|
|
241
241
|
},
|
|
242
242
|
|
|
243
|
-
async
|
|
244
|
-
|
|
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
|
|
245
250
|
.get('spaces/', {})
|
|
246
251
|
.then(res => res.data.spaces || [])
|
|
247
252
|
.catch(err => Promise.reject(err))
|
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
const { listSpaces } = require('../../src/tasks/')
|
|
2
2
|
const { FAKE_SPACES } = require('../constants')
|
|
3
3
|
|
|
4
|
+
const REGION_FLAGS = {
|
|
5
|
+
UNITED_STATES: 'us',
|
|
6
|
+
EUROPE: 'eu',
|
|
7
|
+
CHINA: 'cn'
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
describe('Test spaces method', () => {
|
|
5
11
|
it('Testing list-spaces funtion without api instance', async () => {
|
|
6
12
|
try {
|
|
@@ -10,13 +16,34 @@ describe('Test spaces method', () => {
|
|
|
10
16
|
console.error(e)
|
|
11
17
|
}
|
|
12
18
|
})
|
|
13
|
-
|
|
19
|
+
|
|
20
|
+
it('Testing list-spaces function for China region', async () => {
|
|
14
21
|
const FAKE_API = {
|
|
15
|
-
|
|
22
|
+
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
|
|
16
23
|
}
|
|
17
24
|
expect(
|
|
18
|
-
await listSpaces(FAKE_API)
|
|
25
|
+
await listSpaces(FAKE_API, REGION_FLAGS.CHINA)
|
|
19
26
|
).toEqual(FAKE_SPACES())
|
|
20
|
-
expect(FAKE_API.
|
|
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)
|
|
21
48
|
})
|
|
22
49
|
})
|