@uppy/companion-client
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -25,2 +25,4 @@ 'use strict'; | ||
this.onReceiveResponse = this.onReceiveResponse.bind(this); | ||
this.allowedHeaders = ['accept', 'content-type', 'uppy-auth-token']; | ||
this.preflightDone = false; | ||
} | ||
@@ -77,3 +79,10 @@ | ||
if (res.status < 200 || res.status > 300) { | ||
throw new Error("Failed request to " + res.url + ". " + res.statusText); | ||
var errMsg = "Failed request with status: " + res.status + ". " + res.statusText; | ||
return res.json().then(function (errData) { | ||
errMsg = errData.message ? errMsg + " message: " + errData.message : errMsg; | ||
errMsg = errData.requestId ? errMsg + " request-Id: " + errData.requestId : errMsg; | ||
throw new Error(errMsg); | ||
}).catch(function () { | ||
throw new Error(errMsg); | ||
}); | ||
} | ||
@@ -84,18 +93,64 @@ | ||
_proto.get = function get(path, skipPostResponse) { | ||
_proto.preflight = function preflight(path) { | ||
var _this2 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this2.headers().then(function (headers) { | ||
fetch(_this2._getUrl(path), { | ||
if (_this2.preflightDone) { | ||
return resolve(_this2.allowedHeaders.slice()); | ||
} | ||
fetch(_this2._getUrl(path), { | ||
method: 'OPTIONS' | ||
}).then(function (response) { | ||
if (response.headers.has('access-control-allow-headers')) { | ||
_this2.allowedHeaders = response.headers.get('access-control-allow-headers').split(',').map(function (headerName) { | ||
return headerName.trim().toLowerCase(); | ||
}); | ||
} | ||
_this2.preflightDone = true; | ||
resolve(_this2.allowedHeaders.slice()); | ||
}).catch(function (err) { | ||
_this2.uppy.log("[CompanionClient] unable to make preflight request " + err, 'warning'); | ||
_this2.preflightDone = true; | ||
resolve(_this2.allowedHeaders.slice()); | ||
}); | ||
}); | ||
}; | ||
_proto.preflightAndHeaders = function preflightAndHeaders(path) { | ||
var _this3 = this; | ||
return Promise.all([this.preflight(path), this.headers()]).then(function (_ref) { | ||
var allowedHeaders = _ref[0], | ||
headers = _ref[1]; | ||
// filter to keep only allowed Headers | ||
Object.keys(headers).forEach(function (header) { | ||
if (allowedHeaders.indexOf(header.toLowerCase()) === -1) { | ||
_this3.uppy.log("[CompanionClient] excluding unallowed header " + header); | ||
delete headers[header]; | ||
} | ||
}); | ||
return headers; | ||
}); | ||
}; | ||
_proto.get = function get(path, skipPostResponse) { | ||
var _this4 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this4.preflightAndHeaders(path).then(function (headers) { | ||
fetch(_this4._getUrl(path), { | ||
method: 'get', | ||
headers: headers, | ||
credentials: 'same-origin' | ||
}).then(_this2._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this2._json(res).then(resolve); | ||
}).then(_this4._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this4._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not get " + _this2._getUrl(path) + ". " + err); | ||
err = err.isAuthError ? err : new Error("Could not get " + _this4._getUrl(path) + ". " + err); | ||
reject(err); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
@@ -105,7 +160,7 @@ }; | ||
_proto.post = function post(path, data, skipPostResponse) { | ||
var _this3 = this; | ||
var _this5 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this3.headers().then(function (headers) { | ||
fetch(_this3._getUrl(path), { | ||
_this5.preflightAndHeaders(path).then(function (headers) { | ||
fetch(_this5._getUrl(path), { | ||
method: 'post', | ||
@@ -115,9 +170,9 @@ headers: headers, | ||
body: JSON.stringify(data) | ||
}).then(_this3._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this3._json(res).then(resolve); | ||
}).then(_this5._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this5._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not post " + _this3._getUrl(path) + ". " + err); | ||
err = err.isAuthError ? err : new Error("Could not post " + _this5._getUrl(path) + ". " + err); | ||
reject(err); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
@@ -127,7 +182,7 @@ }; | ||
_proto.delete = function _delete(path, data, skipPostResponse) { | ||
var _this4 = this; | ||
var _this6 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this4.headers().then(function (headers) { | ||
fetch(_this4.hostname + "/" + path, { | ||
_this6.preflightAndHeaders(path).then(function (headers) { | ||
fetch(_this6.hostname + "/" + path, { | ||
method: 'delete', | ||
@@ -137,9 +192,9 @@ headers: headers, | ||
body: data ? JSON.stringify(data) : null | ||
}).then(_this4._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this4._json(res).then(resolve); | ||
}).then(_this6._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this6._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error("Could not delete " + _this4._getUrl(path) + ". " + err); | ||
err = err.isAuthError ? err : new Error("Could not delete " + _this6._getUrl(path) + ". " + err); | ||
reject(err); | ||
}); | ||
}); | ||
}).catch(reject); | ||
}); | ||
@@ -162,3 +217,4 @@ }; | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
'Uppy-Versions': "@uppy/companion-client=" + RequestClient.VERSION | ||
}; | ||
@@ -169,2 +225,2 @@ } | ||
return RequestClient; | ||
}(), _class.VERSION = "1.1.0", _temp); | ||
}(), _class.VERSION = "1.2.0", _temp); |
{ | ||
"name": "@uppy/companion-client", | ||
"description": "Client library for communication with Companion. Intended for use in Uppy plugins.", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "28d235fe2fb57d87a399c20883fd6590aa49f4f4" | ||
"gitHead": "bd2beedcffbaa840de7069860e341f02268ddbb1" | ||
} |
@@ -17,2 +17,4 @@ 'use strict' | ||
this.onReceiveResponse = this.onReceiveResponse.bind(this) | ||
this.allowedHeaders = ['accept', 'content-type', 'uppy-auth-token'] | ||
this.preflightDone = false | ||
} | ||
@@ -29,3 +31,4 @@ | ||
'Accept': 'application/json', | ||
'Content-Type': 'application/json' | ||
'Content-Type': 'application/json', | ||
'Uppy-Versions': `@uppy/companion-client=${RequestClient.VERSION}` | ||
} | ||
@@ -35,3 +38,5 @@ } | ||
headers () { | ||
return Promise.resolve(Object.assign({}, this.defaultHeaders, this.opts.serverHeaders || {})) | ||
return Promise.resolve( | ||
Object.assign({}, this.defaultHeaders, this.opts.serverHeaders || {}) | ||
) | ||
} | ||
@@ -78,3 +83,9 @@ | ||
if (res.status < 200 || res.status > 300) { | ||
throw new Error(`Failed request to ${res.url}. ${res.statusText}`) | ||
let errMsg = `Failed request with status: ${res.status}. ${res.statusText}` | ||
return res.json() | ||
.then((errData) => { | ||
errMsg = errData.message ? `${errMsg} message: ${errData.message}` : errMsg | ||
errMsg = errData.requestId ? `${errMsg} request-Id: ${errData.requestId}` : errMsg | ||
throw new Error(errMsg) | ||
}).catch(() => { throw new Error(errMsg) }) | ||
} | ||
@@ -84,5 +95,45 @@ return res.json() | ||
preflight (path) { | ||
return new Promise((resolve, reject) => { | ||
if (this.preflightDone) { | ||
return resolve(this.allowedHeaders.slice()) | ||
} | ||
fetch(this._getUrl(path), { | ||
method: 'OPTIONS' | ||
}) | ||
.then((response) => { | ||
if (response.headers.has('access-control-allow-headers')) { | ||
this.allowedHeaders = response.headers.get('access-control-allow-headers') | ||
.split(',').map((headerName) => headerName.trim().toLowerCase()) | ||
} | ||
this.preflightDone = true | ||
resolve(this.allowedHeaders.slice()) | ||
}) | ||
.catch((err) => { | ||
this.uppy.log(`[CompanionClient] unable to make preflight request ${err}`, 'warning') | ||
this.preflightDone = true | ||
resolve(this.allowedHeaders.slice()) | ||
}) | ||
}) | ||
} | ||
preflightAndHeaders (path) { | ||
return Promise.all([this.preflight(path), this.headers()]) | ||
.then(([allowedHeaders, headers]) => { | ||
// filter to keep only allowed Headers | ||
Object.keys(headers).forEach((header) => { | ||
if (allowedHeaders.indexOf(header.toLowerCase()) === -1) { | ||
this.uppy.log(`[CompanionClient] excluding unallowed header ${header}`) | ||
delete headers[header] | ||
} | ||
}) | ||
return headers | ||
}) | ||
} | ||
get (path, skipPostResponse) { | ||
return new Promise((resolve, reject) => { | ||
this.headers().then((headers) => { | ||
this.preflightAndHeaders(path).then((headers) => { | ||
fetch(this._getUrl(path), { | ||
@@ -99,3 +150,3 @@ method: 'get', | ||
}) | ||
}) | ||
}).catch(reject) | ||
}) | ||
@@ -106,3 +157,3 @@ } | ||
return new Promise((resolve, reject) => { | ||
this.headers().then((headers) => { | ||
this.preflightAndHeaders(path).then((headers) => { | ||
fetch(this._getUrl(path), { | ||
@@ -120,3 +171,3 @@ method: 'post', | ||
}) | ||
}) | ||
}).catch(reject) | ||
}) | ||
@@ -127,3 +178,3 @@ } | ||
return new Promise((resolve, reject) => { | ||
this.headers().then((headers) => { | ||
this.preflightAndHeaders(path).then((headers) => { | ||
fetch(`${this.hostname}/${path}`, { | ||
@@ -141,5 +192,5 @@ method: 'delete', | ||
}) | ||
}) | ||
}).catch(reject) | ||
}) | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38550
934
8