Skip to content

Commit 2fb34ff

Browse files
author
James Vidler
authored
Merge pull request #56 from agility/ali
stackpath work
2 parents 28e3f14 + 68df48c commit 2fb34ff

File tree

6 files changed

+115
-6
lines changed

6 files changed

+115
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@agility/content-fetch",
3-
"version": "0.9.0",
3+
"version": "0.9.1",
44
"description": "JavaScript library for the Agility Fetch API (node and browser)",
55
"main": "dist/agility-content-fetch.node.js",
66
"scripts": {

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/apiClients.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ function createApiClient() {
1414
return api;
1515
}
1616

17+
function createApiClientWithNewCdn() {
18+
var api = agility.getApi({
19+
guid: '2b64a4d8-d',
20+
apiKey: 'JSSDK.e27e61f56d4c9b58ab98961aaf86a0d3c544dfe7d0eb385ece42123dad5d1af7'
21+
});
22+
return api;
23+
}
1724

1825
function createCachedApiClient() {
1926
var api = agility.getApi({
@@ -38,5 +45,6 @@ function createPreviewApiClient() {
3845
export {
3946
createApiClient,
4047
createCachedApiClient,
41-
createPreviewApiClient
48+
createPreviewApiClient,
49+
createApiClientWithNewCdn
4250
}

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) {

test/getContentItem.tests.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import chai from 'chai'
22
const assert = chai.assert;
33
const expect = chai.expect;
44

5-
import { createApiClient, createPreviewApiClient, createCachedApiClient } from './apiClients.config'
5+
import {
6+
createApiClient,
7+
createPreviewApiClient,
8+
createCachedApiClient,
9+
createApiClientWithNewCdn
10+
} from './apiClients.config'
611

712
/*
813
This file contains static references to content from the instance configured in the apiClient.config file.
@@ -178,5 +183,18 @@ describe('getContentItem:', function() {
178183
.catch(done);
179184
})
180185

186+
it('should be able to fetch an item using global cdn site', function(done) {
187+
var api = createApiClientWithNewCdn();
188+
api.getContentItem({
189+
contentID: 4, //item within jssdklist
190+
languageCode: 'en-us',
191+
})
192+
.then(function(contentItem) {
193+
assert.strictEqual(contentItem.fields.title, 'JS SDK Item - DO NOT DELETE');
194+
done();
195+
})
196+
.catch(done);
197+
})
198+
181199
});
182200

test/getContentList.tests.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import chai from 'chai'
22
const assert = chai.assert;
33
const expect = chai.expect;
4-
import { createApiClient, createPreviewApiClient, createCatchedApiClient } from './apiClients.config'
4+
import {
5+
createApiClient,
6+
createPreviewApiClient,
7+
createCachedApiClient,
8+
createApiClientWithNewCdn
9+
} from './apiClients.config'
510

611
/*
712
This file contains static references to content from the instance configured in the apiClient.config file.
@@ -350,4 +355,18 @@ describe('getContentList:', function() {
350355
})
351356
.catch(done);
352357
})
358+
359+
it('should be able to fetch a list using global cdn site', function(done) {
360+
var api = createApiClientWithNewCdn();
361+
let referenceName = 'jssdklist';
362+
api.getContentList({
363+
referenceName: referenceName,
364+
languageCode: 'en-us'
365+
})
366+
.then(function(contentList) {
367+
assert.strictEqual(contentList.items[0].properties.referenceName, referenceName);
368+
done();
369+
})
370+
.catch(done);
371+
})
353372
});

0 commit comments

Comments
 (0)