Skip to content

Commit cef16a7

Browse files
committed
Adding tests
1 parent c3809ec commit cef16a7

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/api-client.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,25 +60,29 @@ const defaultConfig: Config = {
6060

6161
function buildBaseUrl(guid: string) {
6262

63-
let baseUrlSuffixes = {
63+
const baseUrlSuffixes: { [key: string]: string } = {
6464
u: '',
6565
c: '-ca',
6666
e: '-eu',
6767
a: '-aus',
6868
d: '-dev',
69-
'us2': '-us2'
70-
}
69+
us2: '-usa2'
70+
};
7171

72-
let suffix = guid.substr(guid.length - 2, 2);
73-
let env = suffix.substr(1);
74-
// New format of guid
75-
if (suffix.startsWith('-') && baseUrlSuffixes.hasOwnProperty(env)) {
76-
return `https://api${baseUrlSuffixes[env]}.aglty.io/${guid}`
77-
}
78-
else {
79-
//use default url
80-
return `https://${guid}-api.agilitycms.cloud`;
72+
// Match for -us2, -c, -u, -e, -a, -d at the end of the guid
73+
const match = guid.match(/-(us2|[ucead])$/);
74+
75+
if (match) {
76+
const env = match[1];
77+
if (baseUrlSuffixes.hasOwnProperty(env)) {
78+
const url = `https://api${baseUrlSuffixes[env]}.aglty.io/${guid}`;
79+
return url;
80+
}
8181
}
82+
83+
// use default url
84+
const legacyUrl = `https://${guid}-api.agilitycms.cloud`;
85+
return legacyUrl;
8286
}
8387

8488
function validateConfigParams(configParams: Config) {

test/getApi.tests.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ describe('getApi:', function () {
100100
done();
101101
});
102102

103+
it('should return an api client with new stackpath baseUrl based on us2', function (done) {
104+
const baseUrl = 'https://api-usa2.aglty.io/someguid-us2';
105+
const api = agilityFetch.getApi({
106+
guid: 'someguid-us2',
107+
apiKey: 'some-access-token',
108+
baseUrl: null
109+
});
110+
expect(api.config.baseUrl).toBe(baseUrl);
111+
done();
112+
});
113+
103114
it('should return an api client with new stackpath baseUrl based on dev', function (done) {
104115
const baseUrl = 'https://api-dev.aglty.io/some-guid-d';
105116
const api = agilityFetch.getApi({

0 commit comments

Comments
 (0)