storyblok 3.16.1 → 3.17.1

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "storyblok",
3
- "version": "3.16.1",
3
+ "version": "3.17.1",
4
4
  "description": "A simple CLI to start Storyblok from your command line.",
5
5
  "repository": {
6
6
  "type": "git",
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)
@@ -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
- 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))
22
26
 
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:')
31
32
 
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
+ }
33
62
  }
34
63
 
35
64
  module.exports = listSpaces
package/src/utils/api.js CHANGED
@@ -16,11 +16,15 @@ module.exports = {
16
16
  getClient () {
17
17
  const { region } = creds.get()
18
18
 
19
- return new Storyblok({
20
- accessToken: this.accessToken,
21
- oauthToken: this.oauthToken,
22
- region: this.region
23
- }, this.apiSwitcher(region))
19
+ try {
20
+ return new Storyblok({
21
+ accessToken: this.accessToken,
22
+ oauthToken: this.oauthToken,
23
+ region: this.region
24
+ }, this.apiSwitcher(region))
25
+ } catch (error) {
26
+ throw new Error(error)
27
+ }
24
28
  },
25
29
 
26
30
  getPath (path) {
@@ -240,8 +244,13 @@ module.exports = {
240
244
  return client[method](_path, props)
241
245
  },
242
246
 
243
- async getAllSpaces () {
244
- return await this.getClient()
247
+ async getAllSpacesByRegion (region) {
248
+ const customClient = new Storyblok({
249
+ accessToken: this.accessToken,
250
+ oauthToken: this.oauthToken,
251
+ region
252
+ }, this.apiSwitcher(region))
253
+ return await customClient
245
254
  .get('spaces/', {})
246
255
  .then(res => res.data.spaces || [])
247
256
  .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
- it('Testing list-spaces funtion with api instance', async () => {
19
+
20
+ it('Testing list-spaces function for China region', async () => {
14
21
  const FAKE_API = {
15
- getAllSpaces: jest.fn(() => Promise.resolve(FAKE_SPACES()))
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.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)
21
48
  })
22
49
  })