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

Commit cce1fef

Browse files
Merge pull request #76 from storyblok/task/int-1121
task(int-1121): Adding Canada and Australia regions
2 parents 4f6c33f + 76391ce commit cce1fef

12 files changed

Lines changed: 98 additions & 76 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ $ storyblok login
4444

4545
**For Both login options you nedd to pass the region**
4646

47-
* `region`: region you would like to work in. Please keep in mind that the region must match the region of your space. You can use `us`, `cn` or `eu`, if left empty, default is `eu`. This region flag will be used for the other cli's commands.
47+
* `region`: region you would like to work in. Please keep in mind that the region must match the region of your space. You can use `us`, `cn`, `eu`, `ca` and `ap`, if left empty, default is `eu`. This region flag will be used for the other cli's commands.
4848

4949
#### Login with token flag
5050
You can also add the token directly from the login’s command, like the example below:

__mocks__/axios.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { LOGIN_URL, SIGNUP_URL } = require('../src/constants')
1+
const { USERS_ROUTES } = require('../src/constants')
22
const { EMAIL_TEST, PASSWORD_TEST, TOKEN_TEST } = require('../tests/constants')
33

44
const isCredCorrects = (email, pass) => {
@@ -9,15 +9,15 @@ const axios = {
99
post: jest.fn((path, data) => {
1010
const { email, password } = data || {}
1111

12-
if (path === LOGIN_URL && isCredCorrects(email, password)) {
12+
if (path === USERS_ROUTES.LOGIN && isCredCorrects(email, password)) {
1313
return Promise.resolve({
1414
data: {
1515
access_token: TOKEN_TEST
1616
}
1717
})
1818
}
1919

20-
if (path === SIGNUP_URL && isCredCorrects(email, password)) {
20+
if (path === USERS_ROUTES.SIGNUP && isCredCorrects(email, password)) {
2121
return Promise.resolve({
2222
data: {
2323
access_token: TOKEN_TEST

src/cli.js

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -492,29 +492,29 @@ program
492492
}
493493
})
494494

495-
// delete-datasources
495+
// delete-datasources
496496
program
497-
.command(COMMANDS.DELETE_DATASOURCES)
498-
.requiredOption('--space-id <SPACE_ID>', 'Space id')
499-
.option('--by-slug <SLUG>', 'Delete datasources by slug')
500-
.option('--by-name <name>', 'Delete datasources by name')
501-
.action(async (options) => {
502-
console.log(`${chalk.blue('-')} Executing ${COMMANDS.DELETE_DATASOURCES} task`)
503-
504-
const { spaceId, bySlug, byName } = options
505-
506-
try {
507-
if (!api.isAuthorized()) {
508-
await api.processLogin()
509-
}
497+
.command(COMMANDS.DELETE_DATASOURCES)
498+
.requiredOption('--space-id <SPACE_ID>', 'Space id')
499+
.option('--by-slug <SLUG>', 'Delete datasources by slug')
500+
.option('--by-name <name>', 'Delete datasources by name')
501+
.action(async (options) => {
502+
console.log(`${chalk.blue('-')} Executing ${COMMANDS.DELETE_DATASOURCES} task`)
510503

511-
api.setSpaceId(spaceId)
504+
const { spaceId, bySlug, byName } = options
512505

513-
await tasks.deleteDatasources(api, { byName, bySlug })
514-
} catch (e) {
515-
errorHandler(e, COMMANDS.DELETE_DATASOURCES)
516-
}
517-
})
506+
try {
507+
if (!api.isAuthorized()) {
508+
await api.processLogin()
509+
}
510+
511+
api.setSpaceId(spaceId)
512+
513+
await tasks.deleteDatasources(api, { byName, bySlug })
514+
} catch (e) {
515+
errorHandler(e, COMMANDS.DELETE_DATASOURCES)
516+
}
517+
})
518518

519519
program.parse(process.argv)
520520

@@ -524,7 +524,7 @@ if (program.rawArgs.length <= 2) {
524524

525525
function errorHandler (e, command) {
526526
if (/404/.test(e.message)) {
527-
console.log(chalk.yellow('/!\\') + ' If your space was created under US or CN region, you must provide the region us or cn upon login.')
527+
console.log(chalk.yellow('/!\\') + ' If your space was created under US, CA, AP or CN region, you must provide the region us, ca, ap or cn upon login.')
528528
} else {
529529
console.log(chalk.red('X') + ' An error occurred when executing the ' + command + ' task: ' + e || e.message)
530530
}

src/constants.js

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
const API_URL = 'https://api.storyblok.com/v1/'
2-
const US_API_URL = 'https://api-us.storyblok.com/v1/'
3-
const CN_API_URL = 'https://app.storyblokchina.cn/v1/'
4-
const LOGIN_URL = `${API_URL}users/login`
5-
const SIGNUP_URL = `${API_URL}users/signup`
6-
71
const SYNC_TYPES = [
82
'folders',
93
'components',
@@ -34,13 +28,43 @@ const DEFAULT_AGENT = {
3428
SB_Agent_Version: process.env.npm_package_version || '3.0.0'
3529
}
3630

31+
const REGIONS = {
32+
cn: {
33+
key: 'cn',
34+
name: 'China',
35+
apiEndpoint: 'https://app.storyblokchina.cn/v1/'
36+
},
37+
eu: {
38+
key: 'eu',
39+
name: 'Europe',
40+
apiEndpoint: 'https://api.storyblok.com/v1/'
41+
},
42+
us: {
43+
key: 'us',
44+
name: 'United States',
45+
apiEndpoint: 'https://api-us.storyblok.com/v1/'
46+
},
47+
ca: {
48+
key: 'ca',
49+
name: 'Canada',
50+
apiEndpoint: 'https://api-ca.storyblok.com/v1/'
51+
},
52+
ap: {
53+
key: 'ap',
54+
name: 'Australia',
55+
apiEndpoint: 'https://api-ap.storyblok.com/v1/'
56+
}
57+
}
58+
59+
const USERS_ROUTES = {
60+
LOGIN: `${REGIONS.eu.apiEndpoint}users/login`,
61+
SIGNUP: `${REGIONS.eu.apiEndpoint}users/signup`
62+
}
63+
3764
module.exports = {
38-
LOGIN_URL,
39-
SIGNUP_URL,
40-
API_URL,
4165
SYNC_TYPES,
42-
US_API_URL,
43-
CN_API_URL,
66+
USERS_ROUTES,
4467
COMMANDS,
45-
DEFAULT_AGENT
68+
DEFAULT_AGENT,
69+
REGIONS
4670
}

src/tasks/list-spaces.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const chalk = require('chalk')
2+
const { REGIONS } = require('../constants')
23
/**
34
* @method listSpaces
45
* @param api - Pass the api instance as a parameter
@@ -7,10 +8,7 @@ const chalk = require('chalk')
78

89
const listSpaces = async (api, currentRegion) => {
910
const isChinaEnv = currentRegion === 'cn'
10-
const regionOptions = {
11-
eu: 'Europe',
12-
us: 'United States'
13-
}
11+
1412
console.log()
1513
console.log(chalk.green('✓') + ' Loading spaces...')
1614

@@ -36,7 +34,8 @@ const listSpaces = async (api, currentRegion) => {
3634
return spaces
3735
} else {
3836
const spacesList = []
39-
for (const key in regionOptions) {
37+
for (const key in REGIONS) {
38+
if (key === 'cn') continue
4039
spacesList.push(await api.getAllSpacesByRegion(key)
4140
.then((res) => {
4241
return {
@@ -51,8 +50,9 @@ const listSpaces = async (api, currentRegion) => {
5150
return []
5251
}
5352
spacesList.forEach(region => {
53+
const regionName = REGIONS[region.key].name
5454
console.log()
55-
console.log(`${chalk.blue(' -')} Spaces From ${regionOptions[region.key]} region:`)
55+
console.log(`${chalk.blue(' -')} Spaces From ${regionName} region:`)
5656
region.res.forEach((space) => {
5757
console.log(` ${space.name} (id: ${space.id})`)
5858
})

src/utils/api.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const inquirer = require('inquirer')
55

66
const creds = require('./creds')
77
const getQuestions = require('./get-questions')
8-
const { SIGNUP_URL, API_URL, US_API_URL, CN_API_URL, DEFAULT_AGENT } = require('../constants')
8+
const { REGIONS, USERS_ROUTES, DEFAULT_AGENT } = require('../constants')
99

1010
module.exports = {
1111
accessToken: '',
@@ -39,7 +39,7 @@ module.exports = {
3939
},
4040

4141
async login (content) {
42-
const { email, password, region } = content
42+
const { email, password, region = 'eu' } = content
4343
try {
4444
const response = await axios.post(`${this.apiSwitcher(region)}users/login`, {
4545
email: email,
@@ -169,7 +169,7 @@ module.exports = {
169169
},
170170

171171
signup (email, password, region = 'eu') {
172-
return axios.post(SIGNUP_URL, {
172+
return axios.post(USERS_ROUTES.SIGNUP, {
173173
email: email,
174174
password: password,
175175
region
@@ -310,12 +310,6 @@ module.exports = {
310310
},
311311

312312
apiSwitcher (region) {
313-
const apiList = {
314-
us: US_API_URL,
315-
cn: CN_API_URL,
316-
eu: API_URL
317-
}
318-
319-
return region ? apiList[region] : apiList[this.region]
313+
return region ? REGIONS[region].apiEndpoint : REGIONS[this.region].apiEndpoint
320314
}
321315
}

src/utils/get-questions.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { REGIONS } = require('../constants')
12
const getOptions = (subCommand, argv = {}, api = {}) => {
23
let email = ''
34
const moreOptions = [
@@ -6,17 +7,17 @@ const getOptions = (subCommand, argv = {}, api = {}) => {
67
'push-components',
78
'scaffold'
89
]
10+
const regionsPrefixList = Object.keys(REGIONS)
911
const regionInput = {
1012
type: 'input',
1113
name: 'region',
12-
message: 'Please enter the region you would like to work in (us, eu or cn) - if not set, default is eu:',
14+
message: `Please enter the region you would like to work in (${regionsPrefixList}) - if not set, default is eu:`,
1315
validate: function (value) {
14-
const flagList = ['us', 'cn', 'eu']
15-
if (flagList.indexOf(value) > -1) {
16+
if (regionsPrefixList.indexOf(value) > -1) {
1617
return true
1718
}
1819

19-
return 'Please enter a valid region: us, eu or cn'
20+
return `Please enter a valid region: ${regionsPrefixList}`
2021
}
2122
}
2223

tests/units/list-spaces.spec.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
const { listSpaces } = require('../../src/tasks/')
22
const { FAKE_SPACES } = require('../constants')
3-
4-
const REGION_FLAGS = {
5-
UNITED_STATES: 'us',
6-
EUROPE: 'eu',
7-
CHINA: 'cn'
8-
}
3+
const { REGIONS } = require('../../src/constants')
94

105
describe('Test spaces method', () => {
116
it('Testing list-spaces funtion without api instance', async () => {
@@ -22,28 +17,36 @@ describe('Test spaces method', () => {
2217
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
2318
}
2419
expect(
25-
await listSpaces(FAKE_API, REGION_FLAGS.CHINA)
20+
await listSpaces(FAKE_API, REGIONS.cn.key)
2621
).toEqual(FAKE_SPACES())
2722
expect(FAKE_API.getAllSpacesByRegion).toHaveBeenCalled()
2823
})
2924

30-
it('Testing list-spaces funtion for Europe and United States regions', async () => {
25+
it('Testing list-spaces funtion for all regions', async () => {
3126
const FAKE_API = {
3227
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
3328
}
3429
const response = [
3530
{
36-
key: REGION_FLAGS.EUROPE,
31+
key: REGIONS.eu.key,
32+
res: [...FAKE_SPACES()]
33+
},
34+
{
35+
key: REGIONS.us.key,
36+
res: [...FAKE_SPACES()]
37+
},
38+
{
39+
key: REGIONS.ca.key,
3740
res: [...FAKE_SPACES()]
3841
},
3942
{
40-
key: REGION_FLAGS.UNITED_STATES,
43+
key: REGIONS.ap.key,
4144
res: [...FAKE_SPACES()]
4245
}
4346
]
4447

4548
expect(
46-
await listSpaces(FAKE_API, REGION_FLAGS.EUROPE)
49+
await listSpaces(FAKE_API, REGIONS.eu.key)
4750
).toEqual(response)
4851
})
4952
})

tests/units/push-components.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const pushComponents = require('../../src/tasks/push-components')
22
const Storyblok = require('storyblok-js-client')
33
const api = require('../../src/utils/api')
4-
const { API_URL } = require('../../src/constants')
4+
const { REGIONS } = require('../../src/constants')
55

66
jest.mock('fs')
77
jest.unmock('axios')
@@ -10,7 +10,7 @@ const deleteDocComponent = async () => {
1010
if (process.env.STORYBLOK_TOKEN) {
1111
const client = new Storyblok({
1212
oauthToken: process.env.STORYBLOK_TOKEN
13-
}, API_URL)
13+
}, REGIONS.eu.apiEndpoint)
1414

1515
try {
1616
const path = `spaces/${process.env.STORYBLOK_SPACE}/components`

tests/units/quickstart.spec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const path = require('path')
44
const quickstart = require('../../src/tasks/quickstart')
55
const Storyblok = require('storyblok-js-client')
66
const api = require('../../src/utils/api')
7-
const { API_URL } = require('../../src/constants')
7+
const { REGIONS } = require('../../src/constants')
88

99
jest.unmock('fs')
1010
jest.unmock('axios')
@@ -60,7 +60,7 @@ describe('testing quickstart()', () => {
6060

6161
const client = new Storyblok({
6262
oauthToken: process.env.STORYBLOK_TOKEN
63-
}, API_URL)
63+
}, REGIONS.eu.apiEndpoint)
6464

6565
const response = await client.get('spaces')
6666
const spaces = response.data.spaces

0 commit comments

Comments
 (0)