Skip to content

Commit

Permalink
Merge pull request #50 from agility/joel
Browse files Browse the repository at this point in the history
Added URL Redirections
  • Loading branch information
alitahir360 authored May 20, 2020
2 parents e94d760 + 30c3341 commit b9f37cf
Show file tree
Hide file tree
Showing 9 changed files with 96 additions and 57 deletions.
43 changes: 31 additions & 12 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "dist/agility-content-fetch.node.js",
"scripts": {
"test": "nyc --reporter=html --reporter=text mocha --require @babel/register --recursive ./test/",
"test-r": "nyc --reporter=html --reporter=text mocha --require @babel/register --recursive ./test/getUrlRedirections.tests.js",
"generate-docs": "node_modules/.bin/jsdoc --configure .jsdoc.json --verbose",
"build": "webpack --config webpack.config -p"
},
Expand Down
15 changes: 5 additions & 10 deletions src/api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ export default function createClient(userConfig) {
adapter = cache.adapter;
}

const https= require("https")

//create apply the adapter to our axios instance
const api = axios.create({
httpsAgent: new https.Agent({
rejectUnauthorized: false
}),
adapter: adapter,
})

Expand All @@ -107,16 +111,7 @@ export default function createClient(userConfig) {
data['fromCache'] = true;
}

if (! reqConfig.returnHeaders) {
//if they just wanted the data...
return data;
} else {
//if they indicated they wanted the headers as well as the data...
return {
headers: response.headers,
data
}
}
return data;


})
Expand Down
37 changes: 18 additions & 19 deletions src/methods/getUrlRedirections.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ function getUrlRedirections(requestParams) {

let url = "";
if (requestParams.lastAccessDate) {

if (! requestParams.lastAccessDate.toISOString) {
requestParams.lastAccessDate = new Date(requestParams.lastAccessDate);
}

url = `/?lastAccessDate=${requestParams.lastAccessDate.toISOString()}`
}

Expand All @@ -43,35 +48,22 @@ function getUrlRedirections(requestParams) {
method: 'get',
baseURL: buildRequestUrlPath(this.config, 'urlredirection'),
headers: buildAuthHeader(this.config),
params: {},
returnHeaders: true
params: {}
};

const self = this;
let promise = new Promise(function (resolve, reject) {

self.makeRequest(req)
.then(response => {
.then(data => {

if (response == undefined || ! response) {
if (data == undefined || ! data) {
reject(new Error("The URL redirections could not be retrieved."));
} else {

const {headers, data} = response;

//pull the lastAccessDate from the header
let lastAccessDate = null;
if (headers) {
let dateStr = headers["x-lastaccessdate"];
if (dateStr) lastAccessDate = new Date(dateStr);
}

//return an object with items and lastAccessDate
resolve({ lastAccessDate, items: data });
resolve(data);
}
})
.catch(error => {
console.log("error in url redir")
reject(error);
}
);
Expand All @@ -81,8 +73,15 @@ function getUrlRedirections(requestParams) {

function validateRequestParams(requestParams) {

if (requestParams.lastAccessDate && ! requestParams.lastAccessDate.toISOString) {
throw new TypeError('You must include a valid Datetime for the lastAccessDate.');
if (requestParams.lastAccessDate) {

if (! requestParams.lastAccessDate.toISOString) {
let dt = new Date(requestParams.lastAccessDate);

if (isNaN(dt)) {
throw new TypeError('You must include a valid Datetime for the lastAccessDate.');
}
}
} else {
return;
}
Expand Down
11 changes: 11 additions & 0 deletions src/types/UrlRedirection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* Defines **URL Redirection** .
* @typedef URLRedirection
* @memberof AgilityFetch.Types
* @property {Number} id - The id of this URL Redirection
* @property {String} originUrl - The URL to redirect from
* @property {String} destinationUrl - The URL to redirect to
* @property {Number} statusCode - Either a 301 or 302 status code for the redirect
*/


9 changes: 9 additions & 0 deletions src/types/UrlRedirectionResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Defines **URL Redirection** response.
* @typedef URLRedirectionResponse
* @memberof AgilityFetch.Types
* @property {Array} syncToken - The sync token to be used in the next call as a continuation token for syncing content. If this is empty, you are up to date.
* @property {Array.<AgilityFetch.Types.Page>} items - The paginated array of items returned by the request.
*/


1 change: 1 addition & 0 deletions test/apiClients.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const guid = 'c741222b-1080-45f6-9a7f-982381c5a485';
const apiKeyFetch = 'UnitTestsFetch.2ace650991363fbcffa6776d411d1b0d616b8e3424ce842b81cba7af0039197e';
const apiKeyPreview = 'UnitTestsPreview.69e6bca345ced0b7ca5ab358b351ea5c870790a5945c25d749a865332906b124';


function createApiClient() {
var api = agility.getApi({
guid: guid,
Expand Down
21 changes: 10 additions & 11 deletions test/getContentList.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const assert = chai.assert;
const expect = chai.expect;
import { createApiClient, createPreviewApiClient, createCatchedApiClient } from './apiClients.config'

/*
/*
This file contains static references to content from the instance configured in the apiClient.config file.
*/

Expand All @@ -26,7 +26,7 @@ describe('getContentList:', function() {
})
.catch(done);
});

it('should retrieve a content list in preview mode', function(done) {
var api = createPreviewApiClient();
api.getContentList({
Expand All @@ -40,7 +40,7 @@ describe('getContentList:', function() {
})
.catch(done);
});

it('should throw error if referenceName not passed as argument for getContentList', function(done) {
expect(function() {
var api = createApiClient();
Expand All @@ -57,7 +57,7 @@ describe('getContentList:', function() {
}).to.throw( TypeError );
done();
});

it('should throw error if languageCode param is missing in getContentList', function(done) {
expect(function() {
var api = createApiClient();
Expand All @@ -73,7 +73,7 @@ describe('getContentList:', function() {
}).to.throw( TypeError );
done();
});

it('should throw error if take parameter is NOT a number in getContentList', function(done) {
expect(function() {
var api = createApiClient();
Expand All @@ -91,7 +91,7 @@ describe('getContentList:', function() {
}).to.throw( TypeError );
done();
});

it('should throw error if take parameter is a number less than 1 in getContentList', function(done) {
expect(function() {
var api = createApiClient();
Expand All @@ -109,7 +109,7 @@ describe('getContentList:', function() {
}).to.throw( TypeError );
done();
});

it('should throw error if take parameter is a number greater than 50 in getContentList', function(done) {
expect(function() {
var api = createApiClient();
Expand All @@ -127,7 +127,7 @@ describe('getContentList:', function() {
}).to.throw( TypeError );
done();
});

it('should throw error if skip parameter is a number less than 0 in getContentList', function(done) {
expect(function() {
var api = createApiClient();
Expand All @@ -145,7 +145,7 @@ describe('getContentList:', function() {
}).to.throw( TypeError );
done();
});

it('should throw error if skip parameter is NOT a number in getContentList', function(done) {
expect(function() {
var api = createApiClient();
Expand Down Expand Up @@ -192,8 +192,7 @@ describe('getContentList:', function() {
direction: api.types.SortDirections.DESC
})
.then(function(contentList) {
assert.strictEqual(contentList.items[0].contentID, 16);
assert.strictEqual(contentList.items[1].contentID, 15);
assert.isTrue(contentList.items[0].contentID > contentList.items[1].contentID);
done();
})
.catch(done);
Expand Down
Loading

0 comments on commit b9f37cf

Please sign in to comment.