Skip to content

Commit

Permalink
Merge pull request #76 from storyblok/task/int-1121
Browse files Browse the repository at this point in the history
task(int-1121): Adding Canada and Australia regions
  • Loading branch information
ademarCardoso authored Dec 18, 2023
2 parents 4f6c33f + 76391ce commit cce1fef
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 76 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ $ storyblok login

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

* `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.
* `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.

#### Login with token flag
You can also add the token directly from the login’s command, like the example below:
Expand Down
6 changes: 3 additions & 3 deletions __mocks__/axios.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { LOGIN_URL, SIGNUP_URL } = require('../src/constants')
const { USERS_ROUTES } = require('../src/constants')
const { EMAIL_TEST, PASSWORD_TEST, TOKEN_TEST } = require('../tests/constants')

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

if (path === LOGIN_URL && isCredCorrects(email, password)) {
if (path === USERS_ROUTES.LOGIN && isCredCorrects(email, password)) {
return Promise.resolve({
data: {
access_token: TOKEN_TEST
}
})
}

if (path === SIGNUP_URL && isCredCorrects(email, password)) {
if (path === USERS_ROUTES.SIGNUP && isCredCorrects(email, password)) {
return Promise.resolve({
data: {
access_token: TOKEN_TEST
Expand Down
42 changes: 21 additions & 21 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -492,29 +492,29 @@ program
}
})

// delete-datasources
// delete-datasources
program
.command(COMMANDS.DELETE_DATASOURCES)
.requiredOption('--space-id <SPACE_ID>', 'Space id')
.option('--by-slug <SLUG>', 'Delete datasources by slug')
.option('--by-name <name>', 'Delete datasources by name')
.action(async (options) => {
console.log(`${chalk.blue('-')} Executing ${COMMANDS.DELETE_DATASOURCES} task`)

const { spaceId, bySlug, byName } = options

try {
if (!api.isAuthorized()) {
await api.processLogin()
}
.command(COMMANDS.DELETE_DATASOURCES)
.requiredOption('--space-id <SPACE_ID>', 'Space id')
.option('--by-slug <SLUG>', 'Delete datasources by slug')
.option('--by-name <name>', 'Delete datasources by name')
.action(async (options) => {
console.log(`${chalk.blue('-')} Executing ${COMMANDS.DELETE_DATASOURCES} task`)

api.setSpaceId(spaceId)
const { spaceId, bySlug, byName } = options

await tasks.deleteDatasources(api, { byName, bySlug })
} catch (e) {
errorHandler(e, COMMANDS.DELETE_DATASOURCES)
}
})
try {
if (!api.isAuthorized()) {
await api.processLogin()
}

api.setSpaceId(spaceId)

await tasks.deleteDatasources(api, { byName, bySlug })
} catch (e) {
errorHandler(e, COMMANDS.DELETE_DATASOURCES)
}
})

program.parse(process.argv)

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

function errorHandler (e, command) {
if (/404/.test(e.message)) {
console.log(chalk.yellow('/!\\') + ' If your space was created under US or CN region, you must provide the region us or cn upon login.')
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.')
} else {
console.log(chalk.red('X') + ' An error occurred when executing the ' + command + ' task: ' + e || e.message)
}
Expand Down
48 changes: 36 additions & 12 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
const API_URL = 'https://api.storyblok.com/v1/'
const US_API_URL = 'https://api-us.storyblok.com/v1/'
const CN_API_URL = 'https://app.storyblokchina.cn/v1/'
const LOGIN_URL = `${API_URL}users/login`
const SIGNUP_URL = `${API_URL}users/signup`

const SYNC_TYPES = [
'folders',
'components',
Expand Down Expand Up @@ -34,13 +28,43 @@ const DEFAULT_AGENT = {
SB_Agent_Version: process.env.npm_package_version || '3.0.0'
}

const REGIONS = {
cn: {
key: 'cn',
name: 'China',
apiEndpoint: 'https://app.storyblokchina.cn/v1/'
},
eu: {
key: 'eu',
name: 'Europe',
apiEndpoint: 'https://api.storyblok.com/v1/'
},
us: {
key: 'us',
name: 'United States',
apiEndpoint: 'https://api-us.storyblok.com/v1/'
},
ca: {
key: 'ca',
name: 'Canada',
apiEndpoint: 'https://api-ca.storyblok.com/v1/'
},
ap: {
key: 'ap',
name: 'Australia',
apiEndpoint: 'https://api-ap.storyblok.com/v1/'
}
}

const USERS_ROUTES = {
LOGIN: `${REGIONS.eu.apiEndpoint}users/login`,
SIGNUP: `${REGIONS.eu.apiEndpoint}users/signup`
}

module.exports = {
LOGIN_URL,
SIGNUP_URL,
API_URL,
SYNC_TYPES,
US_API_URL,
CN_API_URL,
USERS_ROUTES,
COMMANDS,
DEFAULT_AGENT
DEFAULT_AGENT,
REGIONS
}
12 changes: 6 additions & 6 deletions src/tasks/list-spaces.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const chalk = require('chalk')
const { REGIONS } = require('../constants')
/**
* @method listSpaces
* @param api - Pass the api instance as a parameter
Expand All @@ -7,10 +8,7 @@ const chalk = require('chalk')

const listSpaces = async (api, currentRegion) => {
const isChinaEnv = currentRegion === 'cn'
const regionOptions = {
eu: 'Europe',
us: 'United States'
}

console.log()
console.log(chalk.green('✓') + ' Loading spaces...')

Expand All @@ -36,7 +34,8 @@ const listSpaces = async (api, currentRegion) => {
return spaces
} else {
const spacesList = []
for (const key in regionOptions) {
for (const key in REGIONS) {
if (key === 'cn') continue
spacesList.push(await api.getAllSpacesByRegion(key)
.then((res) => {
return {
Expand All @@ -51,8 +50,9 @@ const listSpaces = async (api, currentRegion) => {
return []
}
spacesList.forEach(region => {
const regionName = REGIONS[region.key].name
console.log()
console.log(`${chalk.blue(' -')} Spaces From ${regionOptions[region.key]} region:`)
console.log(`${chalk.blue(' -')} Spaces From ${regionName} region:`)
region.res.forEach((space) => {
console.log(` ${space.name} (id: ${space.id})`)
})
Expand Down
14 changes: 4 additions & 10 deletions src/utils/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const inquirer = require('inquirer')

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

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

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

signup (email, password, region = 'eu') {
return axios.post(SIGNUP_URL, {
return axios.post(USERS_ROUTES.SIGNUP, {
email: email,
password: password,
region
Expand Down Expand Up @@ -310,12 +310,6 @@ module.exports = {
},

apiSwitcher (region) {
const apiList = {
us: US_API_URL,
cn: CN_API_URL,
eu: API_URL
}

return region ? apiList[region] : apiList[this.region]
return region ? REGIONS[region].apiEndpoint : REGIONS[this.region].apiEndpoint
}
}
9 changes: 5 additions & 4 deletions src/utils/get-questions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { REGIONS } = require('../constants')
const getOptions = (subCommand, argv = {}, api = {}) => {
let email = ''
const moreOptions = [
Expand All @@ -6,17 +7,17 @@ const getOptions = (subCommand, argv = {}, api = {}) => {
'push-components',
'scaffold'
]
const regionsPrefixList = Object.keys(REGIONS)
const regionInput = {
type: 'input',
name: 'region',
message: 'Please enter the region you would like to work in (us, eu or cn) - if not set, default is eu:',
message: `Please enter the region you would like to work in (${regionsPrefixList}) - if not set, default is eu:`,
validate: function (value) {
const flagList = ['us', 'cn', 'eu']
if (flagList.indexOf(value) > -1) {
if (regionsPrefixList.indexOf(value) > -1) {
return true
}

return 'Please enter a valid region: us, eu or cn'
return `Please enter a valid region: ${regionsPrefixList}`
}
}

Expand Down
25 changes: 14 additions & 11 deletions tests/units/list-spaces.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const { listSpaces } = require('../../src/tasks/')
const { FAKE_SPACES } = require('../constants')

const REGION_FLAGS = {
UNITED_STATES: 'us',
EUROPE: 'eu',
CHINA: 'cn'
}
const { REGIONS } = require('../../src/constants')

describe('Test spaces method', () => {
it('Testing list-spaces funtion without api instance', async () => {
Expand All @@ -22,28 +17,36 @@ describe('Test spaces method', () => {
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
}
expect(
await listSpaces(FAKE_API, REGION_FLAGS.CHINA)
await listSpaces(FAKE_API, REGIONS.cn.key)
).toEqual(FAKE_SPACES())
expect(FAKE_API.getAllSpacesByRegion).toHaveBeenCalled()
})

it('Testing list-spaces funtion for Europe and United States regions', async () => {
it('Testing list-spaces funtion for all regions', async () => {
const FAKE_API = {
getAllSpacesByRegion: jest.fn(() => Promise.resolve(FAKE_SPACES()))
}
const response = [
{
key: REGION_FLAGS.EUROPE,
key: REGIONS.eu.key,
res: [...FAKE_SPACES()]
},
{
key: REGIONS.us.key,
res: [...FAKE_SPACES()]
},
{
key: REGIONS.ca.key,
res: [...FAKE_SPACES()]
},
{
key: REGION_FLAGS.UNITED_STATES,
key: REGIONS.ap.key,
res: [...FAKE_SPACES()]
}
]

expect(
await listSpaces(FAKE_API, REGION_FLAGS.EUROPE)
await listSpaces(FAKE_API, REGIONS.eu.key)
).toEqual(response)
})
})
4 changes: 2 additions & 2 deletions tests/units/push-components.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const pushComponents = require('../../src/tasks/push-components')
const Storyblok = require('storyblok-js-client')
const api = require('../../src/utils/api')
const { API_URL } = require('../../src/constants')
const { REGIONS } = require('../../src/constants')

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

try {
const path = `spaces/${process.env.STORYBLOK_SPACE}/components`
Expand Down
4 changes: 2 additions & 2 deletions tests/units/quickstart.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const path = require('path')
const quickstart = require('../../src/tasks/quickstart')
const Storyblok = require('storyblok-js-client')
const api = require('../../src/utils/api')
const { API_URL } = require('../../src/constants')
const { REGIONS } = require('../../src/constants')

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

const client = new Storyblok({
oauthToken: process.env.STORYBLOK_TOKEN
}, API_URL)
}, REGIONS.eu.apiEndpoint)

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

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

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

jest.unmock('axios')

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

const client = new Storyblok({
oauthToken: process.env.STORYBLOK_TOKEN
}, API_URL)
}, REGIONS.eu.apiEndpoint)

const sourceStories = await getData(
client,
Expand Down

0 comments on commit cce1fef

Please sign in to comment.