Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use region-helper #784

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,8 @@
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": true
},
ademarCardoso marked this conversation as resolved.
Show resolved Hide resolved
"dependencies": {
"@storyblok/region-helper": "^0.2.0"
}
}
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import RichTextResolver from './richTextResolver'
import { SbHelpers } from './sbHelpers'
import SbFetch from './sbFetch'
import { STORYBLOK_AGENT, STORYBLOK_JS_CLIENT_AGENT } from './constants'
import { getRegionUrl, type Region } from '@storyblok/region-helper'

import Method from './constants'
import {
Expand Down Expand Up @@ -87,13 +88,12 @@ class Storyblok {
let endpoint = config.endpoint || pEndpoint

if (!endpoint) {
const getRegion = new SbHelpers().getRegionURL
const protocol = config.https === false ? 'http' : 'https'
ademarCardoso marked this conversation as resolved.
Show resolved Hide resolved

if (!config.oauthToken) {
endpoint = `${protocol}://${getRegion(config.region)}/${'v2' as Version}`
endpoint = `${protocol}://${getRegionUrl(config.region as Region)}/${'v2' as Version}`
} else {
endpoint = `${protocol}://${getRegion(config.region)}/${'v1' as Version}`
endpoint = `${protocol}://${getRegionUrl(config.region as Region)}/${'v1' as Version}`
}
}

Expand Down
26 changes: 0 additions & 26 deletions src/sbHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,6 @@ export class SbHelpers {
return pairs.join('&')
}

/**
* @method getRegionURL
* @param {String} regionCode region code, could be eu, us, cn, ap or ca
* @return {String} The base URL of the region
*/
public getRegionURL(regionCode?: string): string {
const EU_API_URL = 'api.storyblok.com'
const US_API_URL = 'api-us.storyblok.com'
const CN_API_URL = 'app.storyblokchina.cn'
const AP_API_URL = 'api-ap.storyblok.com'
const CA_API_URL = 'api-ca.storyblok.com'

switch (regionCode) {
case 'us':
return US_API_URL
case 'cn':
return CN_API_URL
case 'ap':
return AP_API_URL
case 'ca':
return CA_API_URL
default:
return EU_API_URL
}
}

/**
* @method escapeHTML
* @param {String} string text to be parsed
Expand Down
36 changes: 0 additions & 36 deletions tests/units/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,39 +91,3 @@ describe('flatMap function', () => {
expect(helpers.flatMap([0, 2], (v) => [v, v + 1])).toEqual([0, 1, 2, 3])
})
})

describe('getRegionURL function', () => {
const EU_API_URL = 'api.storyblok.com'
const US_API_URL = 'api-us.storyblok.com'
const CN_API_URL = 'app.storyblokchina.cn'
const AP_API_URL = 'api-ap.storyblok.com'
const CA_API_URL = 'api-ca.storyblok.com'

test('should return the europe url when pass a empty string', () => {
expect(helpers.getRegionURL('')).toEqual(EU_API_URL)
})

test('should return the europe url when pass non supported region code', () => {
expect(helpers.getRegionURL('nn')).toEqual(EU_API_URL)
})

test('should return the europe url when pass eu string', () => {
expect(helpers.getRegionURL('eu')).toEqual(EU_API_URL)
})

test('should return the united states url when pass us string', () => {
expect(helpers.getRegionURL('us')).toEqual(US_API_URL)
})

test('should return the china url when pass cn string', () => {
expect(helpers.getRegionURL('cn')).toEqual(CN_API_URL)
})

test('should return the australia url when pass ap string', () => {
expect(helpers.getRegionURL('ap')).toEqual(AP_API_URL)
})

test('should return the canada url when pass ca string', () => {
expect(helpers.getRegionURL('ca')).toEqual(CA_API_URL)
})
})
Loading