-
Notifications
You must be signed in to change notification settings - Fork 47
Feature: Add request hooks #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
ed7968c
939c60c
ce37d7e
a6f85ca
94d33c5
6fa48f1
99d0f6b
4b9b26a
b7443d9
3eb39eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
| */ | ||
|
|
@@ -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; | ||
|
|
@@ -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 || {}; | ||
|
|
||
|
|
@@ -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; | ||
|
|
@@ -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); | ||
|
||
| const pipedRequest = pipeRequestInterceptors(requestInterceptors); | ||
| request = pipedRequest(request); | ||
hackermd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| if ("data" in options) { | ||
| request.send(options.data); | ||
| } else { | ||
|
|
@@ -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 | ||
| }); | ||
| } | ||
|
|
||
|
|
@@ -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 | ||
| }); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.