Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function(config) {

// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['mocha', 'chai'],
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
Expand Down
137 changes: 11 additions & 126 deletions package-lock.json

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

7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,17 @@
"devDependencies": {
"@babel/core": "^7.1.0",
"@babel/preset-env": "^7.1.0",
"chai": "^4.1.2",
"eslint": "^5.16.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.16.0",
"karma": "^4.2.0",
"karma-chai": "^0.1.0",
"karma-chrome-launcher": "^2.2.0",
"karma-mocha": "^1.3.0",
"mocha": "^5.2.0",
"karma-jasmine": "^4.0.1",
"prettier": "^1.16.4",
"puppeteer": "^1.18.1",
"rollup": "^0.63.2",
"rollup-plugin-babel": "^4.0.3"
}
},
"dependencies": {}
}
39 changes: 31 additions & 8 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ const MEDIATYPES = {
PNG: "image/png"
};

/**
* A callback with the request instance and metadata information
* of the currently request being executed that should necessarily
* return the given request optionally modified.
* @typedef {function} RequestInterceptor
* @param {XMLHttpRequest} request - The original XMLHttpRequest instance.
* @param {object} metadata - The metadata used by the request.
*/

/**
* Class for interacting with DICOMweb RESTful services.
*/
Expand All @@ -37,6 +46,7 @@ class DICOMwebClient {
* @param {String} options.username - Username
* @param {String} options.password - Password
* @param {Object} options.headers - HTTP headers
* @param {Array.<RequestInterceptor>} options.requestInterceptors - Request interceptors.
*/
constructor(options) {
this.baseURL = options.url;
Expand Down Expand Up @@ -75,6 +85,10 @@ class DICOMwebClient {
this.stowURL = this.baseURL;
}

if ("requestInterceptors" in options) {
this.requestInterceptors = options.requestInterceptors;
}

// Headers to pass to requests.
this.headers = options.headers || {};

Expand All @@ -100,15 +114,16 @@ class DICOMwebClient {
* @param {String} method
* @param {Object} headers
* @param {Object} options
* @param {Array.<RequestInterceptor>} options.requestInterceptors - Request interceptors.
* @return {*}
* @private
*/
_httpRequest(url, method, headers, options = {}) {

const {errorInterceptor} = this;
const { errorInterceptor, requestInterceptors } = this;

return new Promise((resolve, reject) => {
const request = new XMLHttpRequest();
let request = new XMLHttpRequest();
request.open(method, url, true);
if ("responseType" in options) {
request.responseType = options.responseType;
Expand Down Expand Up @@ -171,6 +186,14 @@ class DICOMwebClient {
}
}

if (requestInterceptors) {
console.debug('yes')
const metadata = { method, url };
const pipeRequestInterceptors = functions => (args) => functions.reduce((args, fn) => fn(args, metadata), args);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest adding a couple of runtime checks here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. Let me know if I should add more. Thanks.

const pipedRequest = pipeRequestInterceptors(requestInterceptors);
request = pipedRequest(request);
}

if ("data" in options) {
request.send(options.data);
} else {
Expand All @@ -190,9 +213,9 @@ class DICOMwebClient {
* @private
*/
_httpGet(url, headers, responseType, progressCallback) {
return this._httpRequest(url, "get", headers, {
responseType,
progressCallback
return this._httpRequest(url, "get", headers, {
responseType,
progressCallback
});
}

Expand Down Expand Up @@ -629,9 +652,9 @@ class DICOMwebClient {
* @returns {Promise} Response
*/
_httpPost(url, headers, data, progressCallback) {
return this._httpRequest(url, "post", headers, {
data,
progressCallback
return this._httpRequest(url, "post", headers, {
data,
progressCallback
});
}

Expand Down
Loading