Skip to content

Commit d644d5d

Browse files
author
Ali Tahir
committed
stackpath work
1 parent 28e3f14 commit d644d5d

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

src/api-client.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,27 @@ function buildEnvConfig() {
5252
return envConfig;
5353
}
5454

55+
function buildBaseUrl(guid) {
56+
57+
let baseUrlSuffixes = {
58+
u: '',
59+
c: '-ca',
60+
e: '-eu',
61+
d: '-dev'
62+
}
63+
64+
let suffix = guid.substr(guid.length - 2, 2);
65+
let env = suffix.substr(1);
66+
// New format of guid
67+
if (suffix.startsWith('-') && baseUrlSuffixes.hasOwnProperty(env)) {
68+
return `https://api${baseUrlSuffixes[env]}.aglty.io/${guid}`
69+
}
70+
else {
71+
//use default url
72+
return `https://${guid}-api.agilitycms.cloud`;
73+
}
74+
}
75+
5576
export default function createClient(userConfig) {
5677

5778
let envConfig = buildEnvConfig();
@@ -65,8 +86,7 @@ export default function createClient(userConfig) {
6586

6687
//compute the base Url
6788
if (!config.baseUrl) {
68-
//use default url
69-
config.baseUrl = `https://${config.guid}-api.agilitycms.cloud`;
89+
config.baseUrl = buildBaseUrl(config.guid);
7090
} else {
7191
//we are using a custom url, make sure we include the guid in the headers
7292
config.requiresGuidInHeaders = true;

test/getApi.tests.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,50 @@ describe('getApi:', function() {
8585
done();
8686
});
8787

88+
it('should return an api client with new stackpath baseUrl based on canada', function(done) {
89+
const baseUrl = 'https://api-ca.aglty.io/some-guid-c';
90+
const api = agility.getApi({
91+
guid: 'some-guid-c',
92+
apiKey: 'some-access-token',
93+
baseUrl: null
94+
});
95+
assert.strictEqual(api.config.baseUrl, baseUrl);
96+
done();
97+
});
98+
99+
it('should return an api client with new stackpath baseUrl based on usa', function(done) {
100+
const baseUrl = 'https://api.aglty.io/some-guid-u';
101+
const api = agility.getApi({
102+
guid: 'some-guid-u',
103+
apiKey: 'some-access-token',
104+
baseUrl: null
105+
});
106+
assert.strictEqual(api.config.baseUrl, baseUrl);
107+
done();
108+
});
109+
110+
it('should return an api client with new stackpath baseUrl based on dev', function(done) {
111+
const baseUrl = 'https://api-dev.aglty.io/some-guid-d';
112+
const api = agility.getApi({
113+
guid: 'some-guid-d',
114+
apiKey: 'some-access-token',
115+
baseUrl: null
116+
});
117+
assert.strictEqual(api.config.baseUrl, baseUrl);
118+
done();
119+
});
120+
121+
it('should return an api client with legacy stackpath baseUrl', function(done) {
122+
const baseUrl = 'https://some-guid-api.agilitycms.cloud';
123+
const api = agility.getApi({
124+
guid: 'some-guid',
125+
apiKey: 'some-access-token',
126+
baseUrl: null
127+
});
128+
assert.strictEqual(api.config.baseUrl, baseUrl);
129+
done();
130+
});
131+
88132
// TESTING EXCEPTIONS ----------------------------------------------------
89133

90134
it('should throw an error if guid is not passed-in', function(done) {

0 commit comments

Comments
 (0)