From b308c59254bc6a9066e650e31724b082939cf953 Mon Sep 17 00:00:00 2001 From: aadelina <48461605+aadelina@users.noreply.github.com> Date: Sat, 8 May 2021 02:28:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(credentials):=20=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E6=97=B6=E6=B7=BB=E5=8A=A0withCredentials=E5=B1=9E=E6=80=A7=20?= =?UTF-8?q?(#33)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 请求时添加withCredentials属性 * 添加分号,优化代码 --- src/api.js | 240 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 173 insertions(+), 67 deletions(-) diff --git a/src/api.js b/src/api.js index 2ac38fe..e69a936 100644 --- a/src/api.js +++ b/src/api.js @@ -171,6 +171,14 @@ class DICOMwebClient { } } + // Add withCredentials to request if needed + if ("withCredentials" in options) { + if (options.withCredentials) { + request.withCredentials = true; + } + } + + if ("data" in options) { request.send(options.data); } else { @@ -189,10 +197,11 @@ class DICOMwebClient { * @return {*} * @private */ - _httpGet(url, headers, responseType, progressCallback) { + _httpGet(url, headers, responseType, progressCallback, withCredentials) { return this._httpRequest(url, "get", headers, { responseType, - progressCallback + progressCallback, + withCredentials }); } @@ -206,7 +215,7 @@ class DICOMwebClient { * @return {*} * @private */ - _httpGetApplicationJson(url, params = {}, progressCallback) { + _httpGetApplicationJson(url, params = {}, progressCallback, withCredentials) { let urlWithQueryParams = url; if (typeof params === "object") { @@ -220,7 +229,8 @@ class DICOMwebClient { urlWithQueryParams, headers, responseType, - progressCallback + progressCallback, + withCredentials ); } @@ -234,7 +244,7 @@ class DICOMwebClient { * @return {*} * @private */ - _httpGetApplicationPdf(url, params = {}, progressCallback) { + _httpGetApplicationPdf(url, params = {}, progressCallback, withCredentials) { let urlWithQueryParams = url; if (typeof params === "object") { @@ -248,7 +258,8 @@ class DICOMwebClient { urlWithQueryParams, headers, responseType, - progressCallback + progressCallback, + withCredentials ); } @@ -263,7 +274,7 @@ class DICOMwebClient { * @return {*} * @private */ - _httpGetImage(url, mediaTypes, params = {}, progressCallback) { + _httpGetImage(url, mediaTypes, params = {}, progressCallback, withCredentials) { let urlWithQueryParams = url; if (typeof params === "object") { @@ -291,7 +302,8 @@ class DICOMwebClient { urlWithQueryParams, headers, responseType, - progressCallback + progressCallback, + withCredentials ); } @@ -306,7 +318,7 @@ class DICOMwebClient { * @return {*} * @private */ - _httpGetText(url, mediaTypes, params = {}, progressCallback) { + _httpGetText(url, mediaTypes, params = {}, progressCallback, withCredentials) { let urlWithQueryParams = url; if (typeof params === "object") { @@ -334,7 +346,8 @@ class DICOMwebClient { urlWithQueryParams, headers, responseType, - progressCallback + progressCallback, + withCredentials ); } @@ -349,7 +362,7 @@ class DICOMwebClient { * @return {*} * @private */ - _httpGetVideo(url, mediaTypes, params = {}, progressCallback) { + _httpGetVideo(url, mediaTypes, params = {}, progressCallback, withCredentials) { let urlWithQueryParams = url; if (typeof params === "object") { @@ -376,7 +389,8 @@ class DICOMwebClient { urlWithQueryParams, headers, responseType, - progressCallback + progressCallback, + withCredentials ); } @@ -425,7 +439,8 @@ class DICOMwebClient { byteRange, params, rendered = false, - progressCallback + progressCallback, + withCredentials ) { const headers = {}; let supportedMediaTypes; @@ -461,7 +476,7 @@ class DICOMwebClient { supportedMediaTypes ); - return this._httpGet(url, headers, "arraybuffer", progressCallback).then( + return this._httpGet(url, headers, "arraybuffer", progressCallback, withCredentials).then( multipartDecode ); } @@ -485,7 +500,8 @@ class DICOMwebClient { byteRange, params, rendered = false, - progressCallback + progressCallback, + withCredentials ) { const headers = {}; let supportedMediaTypes; @@ -518,7 +534,7 @@ class DICOMwebClient { supportedMediaTypes ); - return this._httpGet(url, headers, "arraybuffer", progressCallback).then( + return this._httpGet(url, headers, "arraybuffer", progressCallback, withCredentials).then( multipartDecode ); } @@ -534,7 +550,7 @@ class DICOMwebClient { * @private * @returns {Promise} Content of HTTP message body parts */ - _httpGetMultipartApplicationDicom(url, mediaTypes, params, progressCallback) { + _httpGetMultipartApplicationDicom(url, mediaTypes, params, progressCallback, withCredentials) { const headers = {}; const defaultMediaType = "application/dicom"; const supportedMediaTypes = { @@ -569,7 +585,7 @@ class DICOMwebClient { supportedMediaTypes ); - return this._httpGet(url, headers, "arraybuffer", progressCallback).then( + return this._httpGet(url, headers, "arraybuffer", progressCallback, withCredentials).then( multipartDecode ); } @@ -591,7 +607,8 @@ class DICOMwebClient { mediaTypes, byteRange, params, - progressCallback + progressCallback, + withCredentials ) { const headers = {}; const defaultMediaType = "application/octet-stream"; @@ -613,7 +630,7 @@ class DICOMwebClient { supportedMediaTypes ); - return this._httpGet(url, headers, "arraybuffer", progressCallback).then( + return this._httpGet(url, headers, "arraybuffer", progressCallback, withCredentials).then( multipartDecode ); } @@ -628,10 +645,11 @@ class DICOMwebClient { * @private * @returns {Promise} Response */ - _httpPost(url, headers, data, progressCallback) { + _httpPost(url, headers, data, progressCallback, withCredentials) { return this._httpRequest(url, "post", headers, { data, - progressCallback + progressCallback, + withCredentials }); } @@ -645,9 +663,9 @@ class DICOMwebClient { * @private * @returns {Promise} Response */ - _httpPostApplicationJson(url, data, progressCallback) { + _httpPostApplicationJson(url, data, progressCallback, withCredentials) { const headers = { "Content-Type": MEDIATYPES.DICOM_JSON }; - return this._httpPost(url, headers, data, progressCallback); + return this._httpPost(url, headers, data, progressCallback, withCredentials); } /** @@ -846,11 +864,17 @@ class DICOMwebClient { */ searchForStudies(options = {}) { console.log("search for studies"); + let withCredentials = false; let url = `${this.qidoURL}/studies`; if ("queryParams" in options) { url += DICOMwebClient._parseQueryParameters(options.queryParams); } - return this._httpGetApplicationJson(url); + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + return this._httpGetApplicationJson(url, {}, false, withCredentials); } /** @@ -869,7 +893,13 @@ class DICOMwebClient { } console.log(`retrieve metadata of study ${options.studyInstanceUID}`); const url = `${this.wadoURL}/studies/${options.studyInstanceUID}/metadata`; - return this._httpGetApplicationJson(url); + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + return this._httpGetApplicationJson(url, {}, false, withCredentials); } /** @@ -890,7 +920,13 @@ class DICOMwebClient { if ("queryParams" in options) { url += DICOMwebClient._parseQueryParameters(options.queryParams); } - return this._httpGetApplicationJson(url); + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + return this._httpGetApplicationJson(url, {}, false, withCredentials); } /** @@ -918,7 +954,13 @@ class DICOMwebClient { const url = `${this.wadoURL}/studies/${options.studyInstanceUID}/series/${ options.seriesInstanceUID }/metadata`; - return this._httpGetApplicationJson(url); + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + return this._httpGetApplicationJson(url, {}, false, withCredentials); } /** @@ -932,6 +974,7 @@ class DICOMwebClient { */ searchForInstances(options = {}) { let url = this.qidoURL; + let withCredentials = false; if ("studyInstanceUID" in options) { url += `/studies/${options.studyInstanceUID}`; if ("seriesInstanceUID" in options) { @@ -951,7 +994,12 @@ class DICOMwebClient { if ("queryParams" in options) { url += DICOMwebClient._parseQueryParameters(options.queryParams); } - return this._httpGetApplicationJson(url); + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + return this._httpGetApplicationJson(url, {}, false, withCredentials); } /** Returns a WADO-URI URL for an instance @@ -1015,8 +1063,13 @@ class DICOMwebClient { const url = `${this.wadoURL}/studies/${options.studyInstanceUID}/series/${ options.seriesInstanceUID }/instances/${options.sopInstanceUID}/metadata`; - - return this._httpGetApplicationJson(url); + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + return this._httpGetApplicationJson(url, {}, false, withCredentials); } /** @@ -1057,19 +1110,25 @@ class DICOMwebClient { }/frames/${options.frameNumbers.toString()}`; const { mediaTypes } = options; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + if (!mediaTypes) { - return this._httpGetMultipartApplicationOctetStream(url); + return this._httpGetMultipartApplicationOctetStream(url, false, false, false, false, withCredentials); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType === MEDIATYPES.OCTET_STREAM) { - return this._httpGetMultipartApplicationOctetStream(url, mediaTypes); + return this._httpGetMultipartApplicationOctetStream(url, mediaTypes, false, false, false, withCredentials); } else if (commonMediaType.startsWith("image")) { - return this._httpGetMultipartImage(url, mediaTypes); + return this._httpGetMultipartImage(url, mediaTypes, false, false, false, false, withCredentials); } else if (commonMediaType.startsWith("video")) { - return this._httpGetMultipartVideo(url, mediaTypes); + return this._httpGetMultipartVideo(url, mediaTypes, false, false, false, false, withCredentials); } throw new Error( @@ -1111,24 +1170,29 @@ class DICOMwebClient { const { mediaTypes, queryParams } = options; const headers = {}; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { const responseType = "arraybuffer"; if (queryParams) { url += DICOMwebClient._parseQueryParameters(queryParams); } - return this._httpGet(url, headers, responseType); + return this._httpGet(url, headers, responseType, false, withCredentials); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType.startsWith("image")) { - return this._httpGetImage(url, mediaTypes, queryParams); + return this._httpGetImage(url, mediaTypes, queryParams, false, withCredentials); } else if (commonMediaType.startsWith("video")) { - return this._httpGetVideo(url, mediaTypes, queryParams); + return this._httpGetVideo(url, mediaTypes, queryParams, false, withCredentials); } else if (commonMediaType.startsWith("text")) { - return this._httpGetText(url, mediaTypes, queryParams); + return this._httpGetText(url, mediaTypes, queryParams, false, withCredentials); } else if (commonMediaType === MEDIATYPES.PDF) { - return this._httpGetApplicationPdf(url, queryParams); + return this._httpGetApplicationPdf(url, queryParams, false, withCredentials); } throw new Error( @@ -1170,18 +1234,23 @@ class DICOMwebClient { const { mediaTypes, queryParams } = options; const headers = {}; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { const responseType = "arraybuffer"; if (queryParams) { url += DICOMwebClient._parseQueryParameters(queryParams); } - return this._httpGet(url, headers, responseType); + return this._httpGet(url, headers, responseType, false, withCredentials); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType.startsWith("image")) { - return this._httpGetImage(url, mediaTypes, queryParams); + return this._httpGetImage(url, mediaTypes, queryParams, false, withCredentials); } throw new Error( @@ -1235,20 +1304,25 @@ class DICOMwebClient { const { mediaTypes, queryParams } = options; const headers = {}; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { const responseType = "arraybuffer"; if (queryParams) { url += DICOMwebClient._parseQueryParameters(queryParams); } - return this._httpGet(url, headers, responseType); + return this._httpGet(url, headers, responseType, false, withCredentials); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType.startsWith("image")) { - return this._httpGetImage(url, mediaTypes, queryParams); + return this._httpGetImage(url, mediaTypes, queryParams, false, withCredentials); } else if (commonMediaType.startsWith("video")) { - return this._httpGetVideo(url, mediaTypes, queryParams); + return this._httpGetVideo(url, mediaTypes, queryParams, false, withCredentials); } throw new Error( @@ -1302,18 +1376,23 @@ class DICOMwebClient { const { mediaTypes, queryParams } = options; const headers = {}; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { const responseType = "arraybuffer"; if (queryParams) { url += DICOMwebClient._parseQueryParameters(queryParams); } - return this._httpGet(url, headers, responseType); + return this._httpGet(url, headers, responseType, false, withCredentials); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType.startsWith("image")) { - return this._httpGetImage(url, mediaTypes, queryParams); + return this._httpGetImage(url, mediaTypes, queryParams, false, withCredentials); } throw new Error( @@ -1345,14 +1424,19 @@ class DICOMwebClient { }/instances/${options.sopInstanceUID}`; const { mediaTypes } = options; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { - return this._httpGetMultipartApplicationDicom(url).then(getFirstResult); + return this._httpGetMultipartApplicationDicom(url, false, false, false, withCredentials).then(getFirstResult); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType === MEDIATYPES.DICOM) { - return this._httpGetMultipartApplicationDicom(url, mediaTypes).then( + return this._httpGetMultipartApplicationDicom(url, mediaTypes, false, false, withCredentials).then( getFirstResult ); } @@ -1383,14 +1467,19 @@ class DICOMwebClient { }`; const { mediaTypes } = options; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { - return this._httpGetMultipartApplicationDicom(url); + return this._httpGetMultipartApplicationDicom(url, false, false, false, withCredentials); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType === MEDIATYPES.DICOM) { - return this._httpGetMultipartApplicationDicom(url, mediaTypes); + return this._httpGetMultipartApplicationDicom(url, mediaTypes, false, false, withCredentials); } throw new Error( @@ -1413,14 +1502,19 @@ class DICOMwebClient { const url = `${this.wadoURL}/studies/${options.studyInstanceUID}`; const { mediaTypes } = options; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { - return this._httpGetMultipartApplicationDicom(url); + return this._httpGetMultipartApplicationDicom(url, false, false, false, withCredentials); } const commonMediaType = DICOMwebClient._getCommonMediaType(mediaTypes); if (commonMediaType === MEDIATYPES.DICOM) { - return this._httpGetMultipartApplicationDicom(url, mediaTypes); + return this._httpGetMultipartApplicationDicom(url, mediaTypes, false, false, withCredentials); } throw new Error( @@ -1446,12 +1540,18 @@ class DICOMwebClient { const url = options.BulkDataURI; const { mediaTypes, byteRange } = options; - + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } if (!mediaTypes) { return this._httpGetMultipartApplicationOctetStream( url, mediaTypes, - byteRange + byteRange, + false, false, withCredentials ); } @@ -1461,10 +1561,11 @@ class DICOMwebClient { return this._httpGetMultipartApplicationOctetStream( url, mediaTypes, - byteRange + byteRange, + false, false, withCredentials ); } else if (commonMediaType.startsWith("image")) { - return this._httpGetMultipartImage(url, mediaTypes, byteRange); + return this._httpGetMultipartImage(url, mediaTypes, byteRange, false, false, false, withCredentials); } throw new Error( @@ -1494,8 +1595,13 @@ class DICOMwebClient { const headers = { "Content-Type": `multipart/related; type="application/dicom"; boundary="${boundary}"` }; - - return this._httpPost(url, headers, data, options.progressCallback); + let withCredentials = false; + if ("withCredentials" in options) { + if(options.withCredentials) { + withCredentials = options.withCredentials; + } + } + return this._httpPost(url, headers, data, options.progressCallback, withCredentials); } }