Skip to content

Commit

Permalink
Merge pull request #48 from oat-sa/release-1.1.0
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
krampstudio authored Oct 22, 2019
2 parents 7d766aa + 183d767 commit bbfab10
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oat-sa/tao-core-sdk",
"version": "1.0.1",
"version": "1.1.0",
"displayName": "TAO Core SDK",
"description": "Core libraries of TAO",
"homepage": "https://github.com/oat-sa/tao-core-sdk-fe#readme",
Expand Down
18 changes: 13 additions & 5 deletions src/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const createError = (response, fallbackMessage, httpCode, httpSent) => {
* @param {Boolean} [options.sequential] - if true, the request must join a queue to be run sequentially
* @param {Number} [options.timeout] - timeout in seconds for the AJAX request
* @param {Object} [options.jwtTokenHandler] - JWT token handler instance
* @param {string} [options.logLevel] - Minimum log level for request
* @returns {Promise} resolves with response, or reject if something went wrong
*/
export default function request(options) {
Expand All @@ -99,6 +100,13 @@ export default function request(options) {
throw new TypeError('At least give a URL...');
}

// Request logger
const requestLogger = logger.child({ url: options.url });
const { logLevel } = options;
if (logLevel) {
requestLogger.level(logLevel);
}

/**
* Function wrapper which allows the contents to be run now, or added to a queue
* @returns {Promise} resolves with response, or rejects if something went wrong
Expand Down Expand Up @@ -151,7 +159,7 @@ export default function request(options) {
*/
const reEnqueueTempToken = () => {
if (tempToken) {
logger.debug('re-enqueueing %s token %s', tokenHeaderName, tempToken);
requestLogger.debug('re-enqueueing %s token %s', tokenHeaderName, tempToken);
return tokenHandler.setToken(tempToken).then(() => {
tempToken = null;
});
Expand All @@ -167,7 +175,7 @@ export default function request(options) {
const setTokenFromXhr = xhr => {
if (_.isFunction(xhr.getResponseHeader)) {
const token = xhr.getResponseHeader(tokenHeaderName);
logger.debug('received %s header %s', tokenHeaderName, token);
requestLogger.debug('received %s header %s', tokenHeaderName, token);

if (token) {
return tokenHandler.setToken(token);
Expand Down Expand Up @@ -195,7 +203,7 @@ export default function request(options) {
timeout: options.timeout * 1000 || context.timeout * 1000 || 0,
beforeSend() {
if (!_.isEmpty(customHeaders)) {
logger.debug(
requestLogger.debug(
'sending %s header %s',
tokenHeaderName,
customHeaders && customHeaders[tokenHeaderName]
Expand Down Expand Up @@ -245,7 +253,7 @@ export default function request(options) {
);
})
.catch(error => {
logger.error(error);
requestLogger.error(error);
reject(createError(response, error, xhr.status, xhr.readyState > 0));
});
};
Expand Down Expand Up @@ -319,7 +327,7 @@ export default function request(options) {
);
})
.catch(error => {
logger.error(error);
requestLogger.error(error);
reject(createError(enhancedResponse, error, xhr.status, xhr.readyState > 0));
});
};
Expand Down
18 changes: 18 additions & 0 deletions test/core/request/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,24 @@
<script type="text/javascript">
require(['/environment/config.js'], function() {
require(['qunitEnv'], function() {
define('core/logger', () => {
const logger = {
child() {
return logger;
},
level(level) {
logger.loggerLevel = level;
},
trace() {},
debug() {},
info() {},
error() {},
fatal() {},
warn() {}
};
return () => logger;
});

require(['test/core/request/test'], function() {
QUnit.start();
});
Expand Down
29 changes: 27 additions & 2 deletions test/core/request/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
*
* @author Martin Nicholson <[email protected]>
*/
define(['jquery', 'lodash', 'core/request', 'core/tokenHandler', 'core/jwt/jwtTokenHandler', 'jquery.mockjax'], function(
define(['jquery', 'lodash', 'core/request', 'core/tokenHandler', 'core/jwt/jwtTokenHandler', 'core/logger', 'jquery.mockjax'], function(
$,
_,
request,
tokenHandlerFactory,
jwtTokenHandlerFactory
jwtTokenHandlerFactory,
loggerFactory
) {
'use strict';

Expand Down Expand Up @@ -848,4 +849,28 @@ define(['jquery', 'lodash', 'core/request', 'core/tokenHandler', 'core/jwt/jwtTo
'request fails if token handler cannot provide access token'
);
});

QUnit.module('request logger', {
afterEach: function() {
$.mockjax.clear();
}
});

QUnit.cases.init(['warn', 'error', 'fatal']).test('request logger min level is set based on config', (logLevel, assert) => {
const done = assert.async();
assert.expect(1);

$.mockjax([
{
url: '//endpoint',
status: 200,
responseText: JSON.stringify({})
}
]);

request({ url: '//endpoint', noToken: true, logLevel }).then(() => {
assert.equal(loggerFactory().loggerLevel, logLevel, `loggel level should be ${logLevel} based on config`);
done();
});
});
});

0 comments on commit bbfab10

Please sign in to comment.