Skip to content

Commit 9074893

Browse files
committed
feat: update region-helper, replace strings with constant, use new getRegionName function
1 parent 897c5c2 commit 9074893

File tree

13 files changed

+38
-53
lines changed

13 files changed

+38
-53
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"author": "Dominik Angerer <dominikangerer1@gmail.com>, Alexander Feiglstorfer <delooks@gmail.com>",
2626
"license": "MIT",
2727
"dependencies": {
28-
"@storyblok/region-helper": "0.2.0",
28+
"@storyblok/region-helper": "^1.0.0",
2929
"axios": "^0.27.2",
3030
"chalk": "^4.1.0",
3131
"clear": "0.1.0",

src/cli.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const chalk = require('chalk')
77
const clear = require('clear')
88
const figlet = require('figlet')
99
const inquirer = require('inquirer')
10-
const { ALL_REGIONS } = require('@storyblok/region-helper')
10+
const { ALL_REGIONS, EU_CODE } = require('@storyblok/region-helper')
1111

1212
const updateNotifier = require('update-notifier')
1313
const pkg = require('../package.json')
@@ -42,7 +42,7 @@ program
4242
.command(COMMANDS.LOGIN)
4343
.description('Login to the Storyblok cli')
4444
.option('-t, --token <token>', 'Token to login directly without questions, like for CI environments')
45-
.option('-r, --region <region>', `The region you would like to work in. Please keep in mind that the region must match the region of your space. This region flag will be used for the other cli's commands. You can use the values: ${allRegionsText}.`, 'eu')
45+
.option('-r, --region <region>', `The region you would like to work in. Please keep in mind that the region must match the region of your space. This region flag will be used for the other cli's commands. You can use the values: ${allRegionsText}.`, EU_CODE)
4646
.action(async (options) => { // TODO: add region validation
4747
const { token, region } = options
4848

src/constants.js

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { getRegionUrl } = require('@storyblok/region-helper')
1+
const { getRegionBaseUrl, EU_CODE } = require('@storyblok/region-helper')
22
const SYNC_TYPES = ['folders', 'components', 'roles', 'stories', 'datasources']
33

44
const COMMANDS = {
@@ -23,37 +23,18 @@ const DEFAULT_AGENT = {
2323
SB_Agent_Version: process.env.npm_package_version || '3.0.0'
2424
}
2525

26-
const REGIONS = {
27-
cn: {
28-
name: 'China'
29-
},
30-
eu: {
31-
name: 'Europe'
32-
},
33-
us: {
34-
name: 'United States'
35-
},
36-
ca: {
37-
name: 'Canada'
38-
},
39-
ap: {
40-
name: 'Australia'
41-
}
42-
}
43-
44-
const getRegionApiEndpoint = (region) => `${getRegionUrl(region)}/v1/`
26+
const getRegionApiEndpoint = (region) => `${getRegionBaseUrl(region)}/v1/`
4527

4628
// todo: FIND OUT IF THIS WORKS WITH us
4729
const USERS_ROUTES = {
48-
LOGIN: `${getRegionApiEndpoint('usa')}users/login`,
49-
SIGNUP: `${getRegionApiEndpoint('eu')}users/signup`
30+
LOGIN: `${getRegionApiEndpoint(EU_CODE)}users/login`,
31+
SIGNUP: `${getRegionApiEndpoint(EU_CODE)}users/signup`
5032
}
5133

5234
module.exports = {
5335
SYNC_TYPES,
5436
USERS_ROUTES,
5537
COMMANDS,
5638
DEFAULT_AGENT,
57-
REGIONS,
5839
getRegionApiEndpoint
5940
}

src/tasks/list-spaces.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const chalk = require('chalk')
2-
const { ALL_REGIONS } = require('@storyblok/region-helper')
3-
const { REGIONS } = require('../constants')
2+
const { ALL_REGIONS, getRegionName, CN_CODE } = require('@storyblok/region-helper')
43
/**
54
* @method listSpaces
65
* @param api - Pass the api instance as a parameter
@@ -9,7 +8,7 @@ const { REGIONS } = require('../constants')
98

109
// TODO: test
1110
const listSpaces = async (api, currentRegion) => {
12-
const isChinaEnv = currentRegion === 'cn'
11+
const isChinaEnv = currentRegion === CN_CODE
1312

1413
console.log()
1514
console.log(chalk.green('✓') + ' Loading spaces...')
@@ -37,7 +36,7 @@ const listSpaces = async (api, currentRegion) => {
3736
} else {
3837
const spacesList = []
3938
for (const key of ALL_REGIONS) {
40-
if (key === 'cn') continue
39+
if (key === CN_CODE) continue
4140
spacesList.push(await api.getAllSpacesByRegion(key)
4241
.then((res) => {
4342
return {
@@ -52,10 +51,8 @@ const listSpaces = async (api, currentRegion) => {
5251
return []
5352
}
5453
spacesList.forEach(region => {
55-
// todo: ADAPT once adding name
56-
const regionName = REGIONS[region.key].name
5754
console.log()
58-
console.log(`${chalk.blue(' -')} Spaces From ${regionName} region:`)
55+
console.log(`${chalk.blue(' -')} Spaces From ${getRegionName(region.key)} region:`)
5956
region.res.forEach((space) => {
6057
console.log(` ${space.name} (id: ${space.id})`)
6158
})

src/utils/api.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const inquirer = require('inquirer')
66
const creds = require('./creds')
77
const getQuestions = require('./get-questions')
88
const { getRegionApiEndpoint, USERS_ROUTES, DEFAULT_AGENT } = require('../constants')
9+
const { EU_CODE } = require('@storyblok/region-helper')
910

1011
module.exports = {
1112
accessToken: '',
@@ -39,7 +40,7 @@ module.exports = {
3940
},
4041

4142
async login (content) {
42-
const { email, password, region = 'eu' } = content
43+
const { email, password, region = EU_CODE } = content
4344
try {
4445
const response = await axios.post(`${this.apiSwitcher(region)}users/login`, {
4546
email: email,
@@ -96,7 +97,7 @@ module.exports = {
9697
}
9798
},
9899

99-
persistCredentials (email, token = null, region = 'eu') {
100+
persistCredentials (email, token = null, region = EU_CODE) {
100101
if (token) {
101102
this.oauthToken = token
102103
creds.set(email, token, region)
@@ -168,7 +169,7 @@ module.exports = {
168169
creds.set(null)
169170
},
170171

171-
signup (email, password, region = 'eu') {
172+
signup (email, password, region = EU_CODE) {
172173
return axios.post(USERS_ROUTES.SIGNUP, {
173174
email: email,
174175
password: password,

src/utils/region.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const { getRegionUrl } = require('@storyblok/region-helper')
1+
const { getRegionBaseUrl } = require('@storyblok/region-helper')
22

3-
const getManagementBaseURLByRegion = (region) => `${getRegionUrl(region)}/v1/`
3+
const getManagementBaseURLByRegion = (region) => `${getRegionBaseUrl(region)}/v1/`
44

55
module.exports = {
66
getManagementBaseURLByRegion

tests/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
const { EU_CODE } = require('@storyblok/region-helper')
12
const EMAIL_TEST = 'test@storyblok.com'
23
const PASSWORD_TEST = 'test'
34
const TOKEN_TEST = 'storyblok1234'
4-
const REGION_TEST = 'eu'
5+
const REGION_TEST = EU_CODE
56

67
// use functions to always returns 'new' data
78
const FAKE_COMPONENTS = () => [

tests/units/list-spaces.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const { listSpaces } = require('../../src/tasks/')
22
const { FAKE_SPACES } = require('../constants')
3+
const { EU_CODE, US_CODE, AP_CODE, CA_CODE, CN_CODE } = require('@storyblok/region-helper')
34

45
describe('Test spaces method', () => {
56
it('Testing list-spaces funtion without api instance', async () => {
@@ -16,7 +17,7 @@ describe('Test spaces method', () => {
1617
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
1718
}
1819
expect(
19-
await listSpaces(FAKE_API, 'cn')
20+
await listSpaces(FAKE_API, CN_CODE)
2021
).toEqual(FAKE_SPACES())
2122
expect(FAKE_API.getAllSpacesByRegion).toHaveBeenCalled()
2223
})
@@ -27,25 +28,25 @@ describe('Test spaces method', () => {
2728
}
2829
const response = [
2930
{
30-
key: 'eu',
31+
key: EU_CODE,
3132
res: [...FAKE_SPACES()]
3233
},
3334
{
34-
key: 'us',
35+
key: US_CODE,
3536
res: [...FAKE_SPACES()]
3637
},
3738
{
38-
key: 'ap',
39+
key: AP_CODE,
3940
res: [...FAKE_SPACES()]
4041
},
4142
{
42-
key: 'ca',
43+
key: CA_CODE,
4344
res: [...FAKE_SPACES()]
4445
}
4546
]
4647

4748
expect(
48-
await listSpaces(FAKE_API, 'eu')
49+
await listSpaces(FAKE_API, EU_CODE)
4950
).toEqual(response)
5051
})
5152
})

tests/units/push-components.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const pushComponents = require('../../src/tasks/push-components')
22
const Storyblok = require('storyblok-js-client')
33
const api = require('../../src/utils/api')
44
const { getRegionApiEndpoint } = require('../../src/constants')
5+
const { EU_CODE } = require('@storyblok/region-helper')
56

67
jest.mock('fs')
78
jest.unmock('axios')
@@ -10,7 +11,7 @@ const deleteDocComponent = async () => {
1011
if (process.env.STORYBLOK_TOKEN) {
1112
const client = new Storyblok({
1213
oauthToken: process.env.STORYBLOK_TOKEN
13-
}, getRegionApiEndpoint('eu'))
14+
}, getRegionApiEndpoint(EU_CODE))
1415

1516
try {
1617
const path = `spaces/${process.env.STORYBLOK_SPACE}/components`

tests/units/quickstart.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const quickstart = require('../../src/tasks/quickstart')
55
const Storyblok = require('storyblok-js-client')
66
const api = require('../../src/utils/api')
77
const { getRegionApiEndpoint } = require('../../src/constants')
8+
const { EU_CODE } = require('@storyblok/region-helper')
89

910
jest.unmock('fs')
1011
jest.unmock('axios')
@@ -60,7 +61,7 @@ describe('testing quickstart()', () => {
6061

6162
const client = new Storyblok({
6263
oauthToken: process.env.STORYBLOK_TOKEN
63-
}, getRegionApiEndpoint('eu'))
64+
}, getRegionApiEndpoint(EU_CODE))
6465

6566
const response = await client.get('spaces')
6667
const spaces = response.data.spaces

tests/units/scaffold.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const scaffold = require('../../src/tasks/scaffold')
44
const Storyblok = require('storyblok-js-client')
55
const api = require('../../src/utils/api')
66
const { getRegionApiEndpoint } = require('../../src/constants')
7+
const { EU_CODE } = require('@storyblok/region-helper')
78

89
jest.mock('fs')
910
jest.unmock('axios')
@@ -12,7 +13,7 @@ const deleteTestComponent = async () => {
1213
if (process.env.STORYBLOK_TOKEN) {
1314
const client = new Storyblok({
1415
oauthToken: process.env.STORYBLOK_TOKEN
15-
}, getRegionApiEndpoint('eu'))
16+
}, getRegionApiEndpoint(EU_CODE))
1617

1718
try {
1819
const path = `spaces/${process.env.STORYBLOK_SPACE}/components`

tests/units/sync.spec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const sync = require('../../src/tasks/sync')
22
const Storyblok = require('storyblok-js-client')
33
const { getRegionApiEndpoint } = require('../../src/constants')
4+
const { EU_CODE } = require('@storyblok/region-helper')
45

56
jest.unmock('axios')
67

@@ -32,7 +33,7 @@ describe('testing sync function', () => {
3233

3334
const client = new Storyblok({
3435
oauthToken: process.env.STORYBLOK_TOKEN
35-
}, getRegionApiEndpoint('eu'))
36+
}, getRegionApiEndpoint(EU_CODE))
3637

3738
const sourceStories = await getData(
3839
client,

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,10 @@
559559
dependencies:
560560
"@sinonjs/commons" "^1.7.0"
561561

562-
"@storyblok/region-helper@0.2.0":
563-
version "0.2.0"
564-
resolved "https://registry.yarnpkg.com/@storyblok/region-helper/-/region-helper-0.2.0.tgz#ea3b244938850910ed1d7c706b2f730985067155"
565-
integrity sha512-LOSFgAiHTmxuBsrjkpTwCk5ZGKmMVmxfmxv8Mi305RtT/o7JOZrjZXnP7kLbLu7m0F18wVVGFmS6XJqbFhBfcA==
562+
"@storyblok/region-helper@^1.0.0":
563+
version "1.0.0"
564+
resolved "https://registry.yarnpkg.com/@storyblok/region-helper/-/region-helper-1.0.0.tgz#5a75506a264b728da640b3c2e85680bec6cd6a1a"
565+
integrity sha512-02B4J3XzD6CLK8DAQbK63fSar8oGYqBJxdx+7Ya0C3uJwMU5DzOMix6ShtUo1iDSd9rOl8aA5wDoCC0wh0YHMw==
566566

567567
"@szmarczak/http-timer@^1.1.2":
568568
version "1.1.2"

0 commit comments

Comments
 (0)