Skip to content

Commit

Permalink
feat: update region-helper, replace strings with constant, use new ge…
Browse files Browse the repository at this point in the history
…tRegionName function
  • Loading branch information
BibiSebi committed Feb 28, 2024
1 parent 897c5c2 commit 9074893
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 53 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"author": "Dominik Angerer <[email protected]>, Alexander Feiglstorfer <[email protected]>",
"license": "MIT",
"dependencies": {
"@storyblok/region-helper": "0.2.0",
"@storyblok/region-helper": "^1.0.0",
"axios": "^0.27.2",
"chalk": "^4.1.0",
"clear": "0.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const chalk = require('chalk')
const clear = require('clear')
const figlet = require('figlet')
const inquirer = require('inquirer')
const { ALL_REGIONS } = require('@storyblok/region-helper')
const { ALL_REGIONS, EU_CODE } = require('@storyblok/region-helper')

const updateNotifier = require('update-notifier')
const pkg = require('../package.json')
Expand Down Expand Up @@ -42,7 +42,7 @@ program
.command(COMMANDS.LOGIN)
.description('Login to the Storyblok cli')
.option('-t, --token <token>', 'Token to login directly without questions, like for CI environments')
.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')
.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)
.action(async (options) => { // TODO: add region validation
const { token, region } = options

Expand Down
27 changes: 4 additions & 23 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { getRegionUrl } = require('@storyblok/region-helper')
const { getRegionBaseUrl, EU_CODE } = require('@storyblok/region-helper')
const SYNC_TYPES = ['folders', 'components', 'roles', 'stories', 'datasources']

const COMMANDS = {
Expand All @@ -23,37 +23,18 @@ const DEFAULT_AGENT = {
SB_Agent_Version: process.env.npm_package_version || '3.0.0'
}

const REGIONS = {
cn: {
name: 'China'
},
eu: {
name: 'Europe'
},
us: {
name: 'United States'
},
ca: {
name: 'Canada'
},
ap: {
name: 'Australia'
}
}

const getRegionApiEndpoint = (region) => `${getRegionUrl(region)}/v1/`
const getRegionApiEndpoint = (region) => `${getRegionBaseUrl(region)}/v1/`

// todo: FIND OUT IF THIS WORKS WITH us
const USERS_ROUTES = {
LOGIN: `${getRegionApiEndpoint('usa')}users/login`,
SIGNUP: `${getRegionApiEndpoint('eu')}users/signup`
LOGIN: `${getRegionApiEndpoint(EU_CODE)}users/login`,
SIGNUP: `${getRegionApiEndpoint(EU_CODE)}users/signup`
}

module.exports = {
SYNC_TYPES,
USERS_ROUTES,
COMMANDS,
DEFAULT_AGENT,
REGIONS,
getRegionApiEndpoint
}
11 changes: 4 additions & 7 deletions src/tasks/list-spaces.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const chalk = require('chalk')
const { ALL_REGIONS } = require('@storyblok/region-helper')
const { REGIONS } = require('../constants')
const { ALL_REGIONS, getRegionName, CN_CODE } = require('@storyblok/region-helper')
/**
* @method listSpaces
* @param api - Pass the api instance as a parameter
Expand All @@ -9,7 +8,7 @@ const { REGIONS } = require('../constants')

// TODO: test
const listSpaces = async (api, currentRegion) => {
const isChinaEnv = currentRegion === 'cn'
const isChinaEnv = currentRegion === CN_CODE

console.log()
console.log(chalk.green('✓') + ' Loading spaces...')
Expand Down Expand Up @@ -37,7 +36,7 @@ const listSpaces = async (api, currentRegion) => {
} else {
const spacesList = []
for (const key of ALL_REGIONS) {
if (key === 'cn') continue
if (key === CN_CODE) continue
spacesList.push(await api.getAllSpacesByRegion(key)
.then((res) => {
return {
Expand All @@ -52,10 +51,8 @@ const listSpaces = async (api, currentRegion) => {
return []
}
spacesList.forEach(region => {
// todo: ADAPT once adding name
const regionName = REGIONS[region.key].name
console.log()
console.log(`${chalk.blue(' -')} Spaces From ${regionName} region:`)
console.log(`${chalk.blue(' -')} Spaces From ${getRegionName(region.key)} region:`)
region.res.forEach((space) => {
console.log(` ${space.name} (id: ${space.id})`)
})
Expand Down
7 changes: 4 additions & 3 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const inquirer = require('inquirer')
const creds = require('./creds')
const getQuestions = require('./get-questions')
const { getRegionApiEndpoint, USERS_ROUTES, DEFAULT_AGENT } = require('../constants')
const { EU_CODE } = require('@storyblok/region-helper')

module.exports = {
accessToken: '',
Expand Down Expand Up @@ -39,7 +40,7 @@ module.exports = {
},

async login (content) {
const { email, password, region = 'eu' } = content
const { email, password, region = EU_CODE } = content
try {
const response = await axios.post(`${this.apiSwitcher(region)}users/login`, {
email: email,
Expand Down Expand Up @@ -96,7 +97,7 @@ module.exports = {
}
},

persistCredentials (email, token = null, region = 'eu') {
persistCredentials (email, token = null, region = EU_CODE) {
if (token) {
this.oauthToken = token
creds.set(email, token, region)
Expand Down Expand Up @@ -168,7 +169,7 @@ module.exports = {
creds.set(null)
},

signup (email, password, region = 'eu') {
signup (email, password, region = EU_CODE) {
return axios.post(USERS_ROUTES.SIGNUP, {
email: email,
password: password,
Expand Down
4 changes: 2 additions & 2 deletions src/utils/region.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getRegionUrl } = require('@storyblok/region-helper')
const { getRegionBaseUrl } = require('@storyblok/region-helper')

const getManagementBaseURLByRegion = (region) => `${getRegionUrl(region)}/v1/`
const getManagementBaseURLByRegion = (region) => `${getRegionBaseUrl(region)}/v1/`

module.exports = {
getManagementBaseURLByRegion
Expand Down
3 changes: 2 additions & 1 deletion tests/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { EU_CODE } = require('@storyblok/region-helper')
const EMAIL_TEST = '[email protected]'
const PASSWORD_TEST = 'test'
const TOKEN_TEST = 'storyblok1234'
const REGION_TEST = 'eu'
const REGION_TEST = EU_CODE

// use functions to always returns 'new' data
const FAKE_COMPONENTS = () => [
Expand Down
13 changes: 7 additions & 6 deletions tests/units/list-spaces.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { listSpaces } = require('../../src/tasks/')
const { FAKE_SPACES } = require('../constants')
const { EU_CODE, US_CODE, AP_CODE, CA_CODE, CN_CODE } = require('@storyblok/region-helper')

describe('Test spaces method', () => {
it('Testing list-spaces funtion without api instance', async () => {
Expand All @@ -16,7 +17,7 @@ describe('Test spaces method', () => {
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
}
expect(
await listSpaces(FAKE_API, 'cn')
await listSpaces(FAKE_API, CN_CODE)
).toEqual(FAKE_SPACES())
expect(FAKE_API.getAllSpacesByRegion).toHaveBeenCalled()
})
Expand All @@ -27,25 +28,25 @@ describe('Test spaces method', () => {
}
const response = [
{
key: 'eu',
key: EU_CODE,
res: [...FAKE_SPACES()]
},
{
key: 'us',
key: US_CODE,
res: [...FAKE_SPACES()]
},
{
key: 'ap',
key: AP_CODE,
res: [...FAKE_SPACES()]
},
{
key: 'ca',
key: CA_CODE,
res: [...FAKE_SPACES()]
}
]

expect(
await listSpaces(FAKE_API, 'eu')
await listSpaces(FAKE_API, EU_CODE)
).toEqual(response)
})
})
3 changes: 2 additions & 1 deletion tests/units/push-components.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const pushComponents = require('../../src/tasks/push-components')
const Storyblok = require('storyblok-js-client')
const api = require('../../src/utils/api')
const { getRegionApiEndpoint } = require('../../src/constants')
const { EU_CODE } = require('@storyblok/region-helper')

jest.mock('fs')
jest.unmock('axios')
Expand All @@ -10,7 +11,7 @@ const deleteDocComponent = async () => {
if (process.env.STORYBLOK_TOKEN) {
const client = new Storyblok({
oauthToken: process.env.STORYBLOK_TOKEN
}, getRegionApiEndpoint('eu'))
}, getRegionApiEndpoint(EU_CODE))

try {
const path = `spaces/${process.env.STORYBLOK_SPACE}/components`
Expand Down
3 changes: 2 additions & 1 deletion tests/units/quickstart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const quickstart = require('../../src/tasks/quickstart')
const Storyblok = require('storyblok-js-client')
const api = require('../../src/utils/api')
const { getRegionApiEndpoint } = require('../../src/constants')
const { EU_CODE } = require('@storyblok/region-helper')

jest.unmock('fs')
jest.unmock('axios')
Expand Down Expand Up @@ -60,7 +61,7 @@ describe('testing quickstart()', () => {

const client = new Storyblok({
oauthToken: process.env.STORYBLOK_TOKEN
}, getRegionApiEndpoint('eu'))
}, getRegionApiEndpoint(EU_CODE))

const response = await client.get('spaces')
const spaces = response.data.spaces
Expand Down
3 changes: 2 additions & 1 deletion tests/units/scaffold.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const scaffold = require('../../src/tasks/scaffold')
const Storyblok = require('storyblok-js-client')
const api = require('../../src/utils/api')
const { getRegionApiEndpoint } = require('../../src/constants')
const { EU_CODE } = require('@storyblok/region-helper')

jest.mock('fs')
jest.unmock('axios')
Expand All @@ -12,7 +13,7 @@ const deleteTestComponent = async () => {
if (process.env.STORYBLOK_TOKEN) {
const client = new Storyblok({
oauthToken: process.env.STORYBLOK_TOKEN
}, getRegionApiEndpoint('eu'))
}, getRegionApiEndpoint(EU_CODE))

try {
const path = `spaces/${process.env.STORYBLOK_SPACE}/components`
Expand Down
3 changes: 2 additions & 1 deletion tests/units/sync.spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const sync = require('../../src/tasks/sync')
const Storyblok = require('storyblok-js-client')
const { getRegionApiEndpoint } = require('../../src/constants')
const { EU_CODE } = require('@storyblok/region-helper')

jest.unmock('axios')

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

const client = new Storyblok({
oauthToken: process.env.STORYBLOK_TOKEN
}, getRegionApiEndpoint('eu'))
}, getRegionApiEndpoint(EU_CODE))

const sourceStories = await getData(
client,
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,10 @@
dependencies:
"@sinonjs/commons" "^1.7.0"

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

"@szmarczak/http-timer@^1.1.2":
version "1.1.2"
Expand Down

0 comments on commit 9074893

Please sign in to comment.