@uppy/companion-client
Advanced tools
Comparing version 0.28.3 to 0.28.4
@@ -5,4 +5,2 @@ 'use strict'; | ||
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -15,2 +13,3 @@ | ||
var RequestClient = require('./RequestClient'); | ||
var tokenStorage = require('./tokenStorage'); | ||
@@ -40,6 +39,26 @@ var _getName = function _getName(id) { | ||
Provider.prototype.headers = function headers() { | ||
var _this2 = this; | ||
return new Promise(function (resolve, reject) { | ||
_RequestClient.prototype.headers.call(_this2).then(function (headers) { | ||
_this2.getAuthToken().then(function (token) { | ||
resolve(_extends({}, headers, { 'uppy-auth-token': token })); | ||
}); | ||
}).catch(reject); | ||
}); | ||
}; | ||
Provider.prototype.onReceiveResponse = function onReceiveResponse(response) { | ||
response = _RequestClient.prototype.onReceiveResponse.call(this, response); | ||
var authenticated = response.status !== 401; | ||
this.uppy.getPlugin(this.pluginId).setPluginState({ authenticated: authenticated }); | ||
return response; | ||
}; | ||
// @todo(i.olarewaju) consider whether or not this method should be exposed | ||
Provider.prototype.setAuthToken = function setAuthToken(token) { | ||
// @todo(i.olarewaju) add fallback for OOM storage | ||
this.uppy.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token); | ||
return this.uppy.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token); | ||
}; | ||
@@ -51,8 +70,2 @@ | ||
Provider.prototype.checkAuth = function checkAuth() { | ||
return this.get(this.id + '/authorized').then(function (payload) { | ||
return payload.authenticated; | ||
}); | ||
}; | ||
Provider.prototype.authUrl = function authUrl() { | ||
@@ -71,9 +84,12 @@ return this.hostname + '/' + this.id + '/connect'; | ||
Provider.prototype.logout = function logout() { | ||
var _this2 = this; | ||
var _this3 = this; | ||
var redirect = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : location.href; | ||
return this.get(this.id + '/logout?redirect=' + redirect).then(function (res) { | ||
_this2.uppy.getPlugin(_this2.pluginId).storage.removeItem(_this2.tokenKey); | ||
return res; | ||
return new Promise(function (resolve, reject) { | ||
_this3.get(_this3.id + '/logout?redirect=' + redirect).then(function (res) { | ||
_this3.uppy.getPlugin(_this3.pluginId).storage.removeItem(_this3.tokenKey).then(function () { | ||
return resolve(res); | ||
}).catch(reject); | ||
}).catch(reject); | ||
}); | ||
@@ -98,4 +114,4 @@ }; | ||
// does not start with https:// | ||
if (/^(?!https?:\/\/).*$/.test(opts.serverUrl)) { | ||
plugin.opts.serverPattern = location.protocol + '//' + opts.serverUrl.replace(/^\/\//, ''); | ||
if (/^(?!https?:\/\/).*$/i.test(opts.serverUrl)) { | ||
plugin.opts.serverPattern = 'https://' + opts.serverUrl.replace(/^\/\//, ''); | ||
} else { | ||
@@ -106,13 +122,6 @@ plugin.opts.serverPattern = opts.serverUrl; | ||
plugin.storage = plugin.opts.storage || localStorage; | ||
plugin.storage = plugin.opts.storage || tokenStorage; | ||
}; | ||
_createClass(Provider, [{ | ||
key: 'defaultHeaders', | ||
get: function get() { | ||
return _extends({}, _RequestClient.prototype.defaultHeaders, { 'uppy-auth-token': this.getAuthToken() }); | ||
} | ||
}]); | ||
return Provider; | ||
}(RequestClient); |
'use strict'; | ||
// Remove the trailing slash so we can always safely append /xyz. | ||
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
@@ -11,2 +9,5 @@ | ||
var AuthError = require('./AuthError'); | ||
// Remove the trailing slash so we can always safely append /xyz. | ||
function stripSlash(url) { | ||
@@ -25,2 +26,18 @@ return url.replace(/\/$/, ''); | ||
RequestClient.prototype.headers = function headers() { | ||
return Promise.resolve(_extends({}, this.defaultHeaders, this.opts.serverHeaders || {})); | ||
}; | ||
RequestClient.prototype._getPostResponseFunc = function _getPostResponseFunc(skip) { | ||
var _this = this; | ||
return function (response) { | ||
if (!skip) { | ||
return _this.onReceiveResponse(response); | ||
} | ||
return response; | ||
}; | ||
}; | ||
RequestClient.prototype.onReceiveResponse = function onReceiveResponse(response) { | ||
@@ -49,53 +66,72 @@ var state = this.uppy.getState(); | ||
RequestClient.prototype.get = function get(path) { | ||
var _this = this; | ||
RequestClient.prototype._json = function _json(res) { | ||
if (res.status === 401) { | ||
throw new AuthError(); | ||
} | ||
return fetch(this._getUrl(path), { | ||
method: 'get', | ||
headers: this.headers, | ||
credentials: 'same-origin' | ||
}) | ||
// @todo validate response status before calling json | ||
.then(this.onReceiveResponse).then(function (res) { | ||
return res.json(); | ||
}).catch(function (err) { | ||
throw new Error('Could not get ' + _this._getUrl(path) + '. ' + err); | ||
}); | ||
if (res.status < 200 || res.status > 300) { | ||
throw new Error('Failed request to ' + res.url + '. ' + res.statusText); | ||
} | ||
return res.json(); | ||
}; | ||
RequestClient.prototype.post = function post(path, data) { | ||
RequestClient.prototype.get = function get(path, skipPostResponse) { | ||
var _this2 = this; | ||
return fetch(this._getUrl(path), { | ||
method: 'post', | ||
headers: this.headers, | ||
credentials: 'same-origin', | ||
body: JSON.stringify(data) | ||
}).then(this.onReceiveResponse).then(function (res) { | ||
if (res.status < 200 || res.status > 300) { | ||
throw new Error('Could not post ' + _this2._getUrl(path) + '. ' + res.statusText); | ||
} | ||
return res.json(); | ||
}).catch(function (err) { | ||
throw new Error('Could not post ' + _this2._getUrl(path) + '. ' + err); | ||
return new Promise(function (resolve, reject) { | ||
_this2.headers().then(function (headers) { | ||
fetch(_this2._getUrl(path), { | ||
method: 'get', | ||
headers: headers, | ||
credentials: 'same-origin' | ||
}).then(_this2._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this2._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error('Could not get ' + _this2._getUrl(path) + '. ' + err); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
}; | ||
RequestClient.prototype.delete = function _delete(path, data) { | ||
RequestClient.prototype.post = function post(path, data, skipPostResponse) { | ||
var _this3 = this; | ||
return fetch(this.hostname + '/' + path, { | ||
method: 'delete', | ||
headers: this.headers, | ||
credentials: 'same-origin', | ||
body: data ? JSON.stringify(data) : null | ||
}).then(this.onReceiveResponse) | ||
// @todo validate response status before calling json | ||
.then(function (res) { | ||
return res.json(); | ||
}).catch(function (err) { | ||
throw new Error('Could not delete ' + _this3._getUrl(path) + '. ' + err); | ||
return new Promise(function (resolve, reject) { | ||
_this3.headers().then(function (headers) { | ||
fetch(_this3._getUrl(path), { | ||
method: 'post', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: JSON.stringify(data) | ||
}).then(_this3._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this3._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error('Could not post ' + _this3._getUrl(path) + '. ' + err); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
}; | ||
RequestClient.prototype.delete = function _delete(path, data, skipPostResponse) { | ||
var _this4 = this; | ||
return new Promise(function (resolve, reject) { | ||
_this4.headers().then(function (headers) { | ||
fetch(_this4.hostname + '/' + path, { | ||
method: 'delete', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: data ? JSON.stringify(data) : null | ||
}).then(_this4._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
return _this4._json(res).then(resolve); | ||
}).catch(function (err) { | ||
err = err.isAuthError ? err : new Error('Could not delete ' + _this4._getUrl(path) + '. ' + err); | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
}; | ||
_createClass(RequestClient, [{ | ||
@@ -118,7 +154,2 @@ key: 'hostname', | ||
} | ||
}, { | ||
key: 'headers', | ||
get: function get() { | ||
return _extends({}, this.defaultHeaders, this.opts.serverHeaders || {}); | ||
} | ||
}]); | ||
@@ -125,0 +156,0 @@ |
{ | ||
"name": "@uppy/companion-client", | ||
"description": "Client library for communication with Companion. Intended for use in Uppy plugins.", | ||
"version": "0.28.3", | ||
"version": "0.28.4", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "27899d944f0ffb551d0473955236734c24eb4fff" | ||
"gitHead": "7ae8af44abdd42eef71c46b831b2e3dadb00237f" | ||
} |
'use strict' | ||
const RequestClient = require('./RequestClient') | ||
const tokenStorage = require('./tokenStorage') | ||
@@ -20,10 +21,22 @@ const _getName = (id) => { | ||
get defaultHeaders () { | ||
return Object.assign({}, super.defaultHeaders, {'uppy-auth-token': this.getAuthToken()}) | ||
headers () { | ||
return new Promise((resolve, reject) => { | ||
super.headers().then((headers) => { | ||
this.getAuthToken().then((token) => { | ||
resolve(Object.assign({}, headers, { 'uppy-auth-token': token })) | ||
}) | ||
}).catch(reject) | ||
}) | ||
} | ||
onReceiveResponse (response) { | ||
response = super.onReceiveResponse(response) | ||
const authenticated = response.status !== 401 | ||
this.uppy.getPlugin(this.pluginId).setPluginState({ authenticated }) | ||
return response | ||
} | ||
// @todo(i.olarewaju) consider whether or not this method should be exposed | ||
setAuthToken (token) { | ||
// @todo(i.olarewaju) add fallback for OOM storage | ||
this.uppy.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token) | ||
return this.uppy.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token) | ||
} | ||
@@ -35,9 +48,2 @@ | ||
checkAuth () { | ||
return this.get(`${this.id}/authorized`) | ||
.then((payload) => { | ||
return payload.authenticated | ||
}) | ||
} | ||
authUrl () { | ||
@@ -56,7 +62,10 @@ return `${this.hostname}/${this.id}/connect` | ||
logout (redirect = location.href) { | ||
return this.get(`${this.id}/logout?redirect=${redirect}`) | ||
.then((res) => { | ||
this.uppy.getPlugin(this.pluginId).storage.removeItem(this.tokenKey) | ||
return res | ||
}) | ||
return new Promise((resolve, reject) => { | ||
this.get(`${this.id}/logout?redirect=${redirect}`) | ||
.then((res) => { | ||
this.uppy.getPlugin(this.pluginId).storage.removeItem(this.tokenKey) | ||
.then(() => resolve(res)) | ||
.catch(reject) | ||
}).catch(reject) | ||
}) | ||
} | ||
@@ -80,4 +89,4 @@ | ||
// does not start with https:// | ||
if (/^(?!https?:\/\/).*$/.test(opts.serverUrl)) { | ||
plugin.opts.serverPattern = `${location.protocol}//${opts.serverUrl.replace(/^\/\//, '')}` | ||
if (/^(?!https?:\/\/).*$/i.test(opts.serverUrl)) { | ||
plugin.opts.serverPattern = `https://${opts.serverUrl.replace(/^\/\//, '')}` | ||
} else { | ||
@@ -88,4 +97,4 @@ plugin.opts.serverPattern = opts.serverUrl | ||
plugin.storage = plugin.opts.storage || localStorage | ||
plugin.storage = plugin.opts.storage || tokenStorage | ||
} | ||
} |
'use strict' | ||
const AuthError = require('./AuthError') | ||
// Remove the trailing slash so we can always safely append /xyz. | ||
@@ -28,6 +30,16 @@ function stripSlash (url) { | ||
get headers () { | ||
return Object.assign({}, this.defaultHeaders, this.opts.serverHeaders || {}) | ||
headers () { | ||
return Promise.resolve(Object.assign({}, this.defaultHeaders, this.opts.serverHeaders || {})) | ||
} | ||
_getPostResponseFunc (skip) { | ||
return (response) => { | ||
if (!skip) { | ||
return this.onReceiveResponse(response) | ||
} | ||
return response | ||
} | ||
} | ||
onReceiveResponse (response) { | ||
@@ -56,49 +68,68 @@ const state = this.uppy.getState() | ||
get (path) { | ||
return fetch(this._getUrl(path), { | ||
method: 'get', | ||
headers: this.headers, | ||
credentials: 'same-origin' | ||
_json (res) { | ||
if (res.status === 401) { | ||
throw new AuthError() | ||
} | ||
if (res.status < 200 || res.status > 300) { | ||
throw new Error(`Failed request to ${res.url}. ${res.statusText}`) | ||
} | ||
return res.json() | ||
} | ||
get (path, skipPostResponse) { | ||
return new Promise((resolve, reject) => { | ||
this.headers().then((headers) => { | ||
fetch(this._getUrl(path), { | ||
method: 'get', | ||
headers: headers, | ||
credentials: 'same-origin' | ||
}) | ||
.then(this._getPostResponseFunc(skipPostResponse)) | ||
.then((res) => this._json(res).then(resolve)) | ||
.catch((err) => { | ||
err = err.isAuthError ? err : new Error(`Could not get ${this._getUrl(path)}. ${err}`) | ||
reject(err) | ||
}) | ||
}) | ||
}) | ||
// @todo validate response status before calling json | ||
.then(this.onReceiveResponse) | ||
.then((res) => res.json()) | ||
.catch((err) => { | ||
throw new Error(`Could not get ${this._getUrl(path)}. ${err}`) | ||
}) | ||
} | ||
post (path, data) { | ||
return fetch(this._getUrl(path), { | ||
method: 'post', | ||
headers: this.headers, | ||
credentials: 'same-origin', | ||
body: JSON.stringify(data) | ||
post (path, data, skipPostResponse) { | ||
return new Promise((resolve, reject) => { | ||
this.headers().then((headers) => { | ||
fetch(this._getUrl(path), { | ||
method: 'post', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: JSON.stringify(data) | ||
}) | ||
.then(this._getPostResponseFunc(skipPostResponse)) | ||
.then((res) => this._json(res).then(resolve)) | ||
.catch((err) => { | ||
err = err.isAuthError ? err : new Error(`Could not post ${this._getUrl(path)}. ${err}`) | ||
reject(err) | ||
}) | ||
}) | ||
}) | ||
.then(this.onReceiveResponse) | ||
.then((res) => { | ||
if (res.status < 200 || res.status > 300) { | ||
throw new Error(`Could not post ${this._getUrl(path)}. ${res.statusText}`) | ||
} | ||
return res.json() | ||
}) | ||
.catch((err) => { | ||
throw new Error(`Could not post ${this._getUrl(path)}. ${err}`) | ||
}) | ||
} | ||
delete (path, data) { | ||
return fetch(`${this.hostname}/${path}`, { | ||
method: 'delete', | ||
headers: this.headers, | ||
credentials: 'same-origin', | ||
body: data ? JSON.stringify(data) : null | ||
delete (path, data, skipPostResponse) { | ||
return new Promise((resolve, reject) => { | ||
this.headers().then((headers) => { | ||
fetch(`${this.hostname}/${path}`, { | ||
method: 'delete', | ||
headers: headers, | ||
credentials: 'same-origin', | ||
body: data ? JSON.stringify(data) : null | ||
}) | ||
.then(this._getPostResponseFunc(skipPostResponse)) | ||
.then((res) => this._json(res).then(resolve)) | ||
.catch((err) => { | ||
err = err.isAuthError ? err : new Error(`Could not delete ${this._getUrl(path)}. ${err}`) | ||
reject(err) | ||
}) | ||
}) | ||
}) | ||
.then(this.onReceiveResponse) | ||
// @todo validate response status before calling json | ||
.then((res) => res.json()) | ||
.catch((err) => { | ||
throw new Error(`Could not delete ${this._getUrl(path)}. ${err}`) | ||
}) | ||
} | ||
} |
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
33528
24
812