@uppy/companion-client
Advanced tools
Comparing version 1.7.0 to 1.8.0
@@ -7,2 +7,4 @@ 'use strict'; | ||
var qsStringify = require('qs-stringify'); | ||
var RequestClient = require('./RequestClient'); | ||
@@ -30,2 +32,4 @@ | ||
_this.tokenKey = "companion-" + _this.pluginId + "-auth-token"; | ||
_this.companionKeysParams = _this.opts.companionKeysParams; | ||
_this.preAuthToken = null; | ||
return _this; | ||
@@ -37,8 +41,20 @@ } | ||
_proto.headers = function headers() { | ||
var _this2 = this; | ||
return Promise.all([_RequestClient.prototype.headers.call(this), this.getAuthToken()]).then(function (_ref) { | ||
var headers = _ref[0], | ||
token = _ref[1]; | ||
return _extends({}, headers, { | ||
'uppy-auth-token': token | ||
}); | ||
var authHeaders = {}; | ||
if (token) { | ||
authHeaders['uppy-auth-token'] = token; | ||
} | ||
if (_this2.companionKeysParams) { | ||
authHeaders['uppy-credentials-params'] = btoa(JSON.stringify({ | ||
params: _this2.companionKeysParams | ||
})); | ||
} | ||
return _extends({}, headers, authHeaders); | ||
}); | ||
@@ -67,4 +83,14 @@ }; | ||
_proto.authUrl = function authUrl() { | ||
return this.hostname + "/" + this.id + "/connect"; | ||
_proto.authUrl = function authUrl(queries) { | ||
if (queries === void 0) { | ||
queries = {}; | ||
} | ||
if (this.preAuthToken) { | ||
queries.uppyPreAuthToken = this.preAuthToken; | ||
} | ||
var strigifiedQueries = qsStringify(queries); | ||
strigifiedQueries = strigifiedQueries ? "?" + strigifiedQueries : strigifiedQueries; | ||
return this.hostname + "/" + this.id + "/connect" + strigifiedQueries; | ||
}; | ||
@@ -76,2 +102,18 @@ | ||
_proto.fetchPreAuthToken = function fetchPreAuthToken() { | ||
var _this3 = this; | ||
if (!this.companionKeysParams) { | ||
return Promise.resolve(); | ||
} | ||
return this.post(this.id + "/preauth/", { | ||
params: this.companionKeysParams | ||
}).then(function (res) { | ||
_this3.preAuthToken = res.token; | ||
}).catch(function (err) { | ||
_this3.uppy.log("[CompanionClient] unable to fetch preAuthToken " + err, 'warning'); | ||
}); | ||
}; | ||
_proto.list = function list(directory) { | ||
@@ -82,6 +124,6 @@ return this.get(this.id + "/list/" + (directory || '')); | ||
_proto.logout = function logout() { | ||
var _this2 = this; | ||
var _this4 = this; | ||
return this.get(this.id + "/logout").then(function (response) { | ||
return Promise.all([response, _this2.uppy.getPlugin(_this2.pluginId).storage.removeItem(_this2.tokenKey)]); | ||
return Promise.all([response, _this4.uppy.getPlugin(_this4.pluginId).storage.removeItem(_this4.tokenKey)]); | ||
}).then(function (_ref2) { | ||
@@ -88,0 +130,0 @@ var response = _ref2[0]; |
@@ -143,3 +143,3 @@ 'use strict'; | ||
headers: headers, | ||
credentials: 'same-origin' | ||
credentials: _this4.opts.companionCookiesRule || 'same-origin' | ||
}); | ||
@@ -161,3 +161,3 @@ }).then(this._getPostResponseFunc(skipPostResponse)).then(function (res) { | ||
headers: headers, | ||
credentials: 'same-origin', | ||
credentials: _this5.opts.companionCookiesRule || 'same-origin', | ||
body: JSON.stringify(data) | ||
@@ -180,3 +180,3 @@ }); | ||
headers: headers, | ||
credentials: 'same-origin', | ||
credentials: _this6.opts.companionCookiesRule || 'same-origin', | ||
body: data ? JSON.stringify(data) : null | ||
@@ -213,2 +213,2 @@ }); | ||
return RequestClient; | ||
}(), _class.VERSION = "1.7.0", _temp); | ||
}(), _class.VERSION = "1.8.0", _temp); |
{ | ||
"name": "@uppy/companion-client", | ||
"description": "Client library for communication with Companion. Intended for use in Uppy plugins.", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"license": "MIT", | ||
@@ -24,6 +24,7 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@uppy/utils": "^3.3.0", | ||
"namespace-emitter": "^2.0.1" | ||
"@uppy/utils": "^3.3.1", | ||
"namespace-emitter": "^2.0.1", | ||
"qs-stringify": "^1.1.0" | ||
}, | ||
"gitHead": "1493a807fe83f7b465a8f7562bfb5631b1945e5b" | ||
"gitHead": "e1efe07927e7c47b94bba5540d9c0f6fd1c4c514" | ||
} |
@@ -6,3 +6,3 @@ # @uppy/companion-client | ||
<a href="https://www.npmjs.com/package/@uppy/companion-client"><img src="https://img.shields.io/npm/v/@uppy/companion-client.svg?style=flat-square"></a> | ||
<a href="https://travis-ci.org/transloadit/uppy"><img src="https://img.shields.io/travis/transloadit/uppy/master.svg?style=flat-square" alt="Build Status"></a> | ||
<img src="https://github.com/transloadit/uppy/workflows/Tests/badge.svg" alt="CI status for Uppy tests"> <img src="https://github.com/transloadit/uppy/workflows/Companion/badge.svg" alt="CI status for Companion tests"> <img src="https://github.com/transloadit/uppy/workflows/End-to-end%20tests/badge.svg" alt="CI status for browser tests"> | ||
@@ -9,0 +9,0 @@ Client library for communication with Companion. Intended for use in Uppy plugins. |
'use strict' | ||
const qsStringify = require('qs-stringify') | ||
const RequestClient = require('./RequestClient') | ||
@@ -18,2 +19,4 @@ const tokenStorage = require('./tokenStorage') | ||
this.tokenKey = `companion-${this.pluginId}-auth-token` | ||
this.companionKeysParams = this.opts.companionKeysParams | ||
this.preAuthToken = null | ||
} | ||
@@ -23,5 +26,15 @@ | ||
return Promise.all([super.headers(), this.getAuthToken()]) | ||
.then(([headers, token]) => | ||
Object.assign({}, headers, { 'uppy-auth-token': token }) | ||
) | ||
.then(([headers, token]) => { | ||
const authHeaders = {} | ||
if (token) { | ||
authHeaders['uppy-auth-token'] = token | ||
} | ||
if (this.companionKeysParams) { | ||
authHeaders['uppy-credentials-params'] = btoa( | ||
JSON.stringify({ params: this.companionKeysParams }) | ||
) | ||
} | ||
return Object.assign({}, headers, authHeaders) | ||
}) | ||
} | ||
@@ -47,4 +60,10 @@ | ||
authUrl () { | ||
return `${this.hostname}/${this.id}/connect` | ||
authUrl (queries = {}) { | ||
if (this.preAuthToken) { | ||
queries.uppyPreAuthToken = this.preAuthToken | ||
} | ||
let strigifiedQueries = qsStringify(queries) | ||
strigifiedQueries = strigifiedQueries ? `?${strigifiedQueries}` : strigifiedQueries | ||
return `${this.hostname}/${this.id}/connect${strigifiedQueries}` | ||
} | ||
@@ -56,2 +75,15 @@ | ||
fetchPreAuthToken () { | ||
if (!this.companionKeysParams) { | ||
return Promise.resolve() | ||
} | ||
return this.post(`${this.id}/preauth/`, { params: this.companionKeysParams }) | ||
.then((res) => { | ||
this.preAuthToken = res.token | ||
}).catch((err) => { | ||
this.uppy.log(`[CompanionClient] unable to fetch preAuthToken ${err}`, 'warning') | ||
}) | ||
} | ||
list (directory) { | ||
@@ -58,0 +90,0 @@ return this.get(`${this.id}/list/${directory || ''}`) |
@@ -138,3 +138,3 @@ 'use strict' | ||
headers: headers, | ||
credentials: 'same-origin' | ||
credentials: this.opts.companionCookiesRule || 'same-origin' | ||
})) | ||
@@ -155,3 +155,3 @@ .then(this._getPostResponseFunc(skipPostResponse)) | ||
headers: headers, | ||
credentials: 'same-origin', | ||
credentials: this.opts.companionCookiesRule || 'same-origin', | ||
body: JSON.stringify(data) | ||
@@ -173,3 +173,3 @@ })) | ||
headers: headers, | ||
credentials: 'same-origin', | ||
credentials: this.opts.companionCookiesRule || 'same-origin', | ||
body: data ? JSON.stringify(data) : null | ||
@@ -176,0 +176,0 @@ })) |
@@ -14,2 +14,3 @@ /** | ||
companionHeaders?: object | ||
companionCookiesRule?: RequestCredentials | ||
/** | ||
@@ -16,0 +17,0 @@ * Deprecated, use `companionHeaders` instead. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
75707
1062
3
+ Addedqs-stringify@^1.1.0
+ Addedqs-stringify@1.2.1(transitive)
Updated@uppy/utils@^3.3.1