@uppy/companion-client
Advanced tools
Comparing version 2.0.4 to 2.0.5
@@ -62,3 +62,18 @@ 'use strict'; | ||
} | ||
/** | ||
* Ensure we have a preauth token if necessary. Attempts to fetch one if we don't, | ||
* or rejects if loading one fails. | ||
*/ | ||
async ensurePreAuth() { | ||
if (this.companionKeysParams && !this.preAuthToken) { | ||
await this.fetchPreAuthToken(); | ||
if (!this.preAuthToken) { | ||
throw new Error('Could not load authentication data required for third-party login. Please try again later.'); | ||
} | ||
} | ||
} | ||
authUrl(queries) { | ||
@@ -69,7 +84,9 @@ if (queries === void 0) { | ||
const params = new URLSearchParams(queries); | ||
if (this.preAuthToken) { | ||
queries.uppyPreAuthToken = this.preAuthToken; | ||
params.set('uppyPreAuthToken', this.preAuthToken); | ||
} | ||
return `${this.hostname}/${this.id}/connect?${new URLSearchParams(queries)}`; | ||
return `${this.hostname}/${this.id}/connect?${params}`; | ||
} | ||
@@ -81,14 +98,15 @@ | ||
fetchPreAuthToken() { | ||
async fetchPreAuthToken() { | ||
if (!this.companionKeysParams) { | ||
return Promise.resolve(); | ||
return; | ||
} | ||
return this.post(`${this.id}/preauth/`, { | ||
params: this.companionKeysParams | ||
}).then(res => { | ||
try { | ||
const res = await this.post(`${this.id}/preauth/`, { | ||
params: this.companionKeysParams | ||
}); | ||
this.preAuthToken = res.token; | ||
}).catch(err => { | ||
} catch (err) { | ||
this.uppy.log(`[CompanionClient] unable to fetch preAuthToken ${err}`, 'warning'); | ||
}); | ||
} | ||
} | ||
@@ -95,0 +113,0 @@ |
@@ -161,3 +161,3 @@ 'use strict'; | ||
}), _class.VERSION = "2.0.4", _class.defaultHeaders = { | ||
}), _class.VERSION = "2.0.5", _class.defaultHeaders = { | ||
Accept: 'application/json', | ||
@@ -164,0 +164,0 @@ 'Content-Type': 'application/json', |
@@ -1,3 +0,1 @@ | ||
"use strict"; | ||
var _queued, _emitter, _isOpen, _socket, _handleMessage; | ||
@@ -4,0 +2,0 @@ |
{ | ||
"name": "@uppy/companion-client", | ||
"description": "Client library for communication with Companion. Intended for use in Uppy plugins.", | ||
"version": "2.0.4", | ||
"version": "2.0.5", | ||
"license": "MIT", | ||
@@ -24,5 +24,5 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@uppy/utils": "^4.0.4", | ||
"@uppy/utils": "^4.0.5", | ||
"namespace-emitter": "^2.0.1" | ||
} | ||
} |
@@ -56,8 +56,23 @@ 'use strict' | ||
/** | ||
* Ensure we have a preauth token if necessary. Attempts to fetch one if we don't, | ||
* or rejects if loading one fails. | ||
*/ | ||
async ensurePreAuth () { | ||
if (this.companionKeysParams && !this.preAuthToken) { | ||
await this.fetchPreAuthToken() | ||
if (!this.preAuthToken) { | ||
throw new Error('Could not load authentication data required for third-party login. Please try again later.') | ||
} | ||
} | ||
} | ||
authUrl (queries = {}) { | ||
const params = new URLSearchParams(queries) | ||
if (this.preAuthToken) { | ||
queries.uppyPreAuthToken = this.preAuthToken | ||
params.set('uppyPreAuthToken', this.preAuthToken) | ||
} | ||
return `${this.hostname}/${this.id}/connect?${new URLSearchParams(queries)}` | ||
return `${this.hostname}/${this.id}/connect?${params}` | ||
} | ||
@@ -69,13 +84,13 @@ | ||
fetchPreAuthToken () { | ||
async fetchPreAuthToken () { | ||
if (!this.companionKeysParams) { | ||
return Promise.resolve() | ||
return | ||
} | ||
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') | ||
}) | ||
try { | ||
const res = await this.post(`${this.id}/preauth/`, { params: this.companionKeysParams }) | ||
this.preAuthToken = res.token | ||
} catch (err) { | ||
this.uppy.log(`[CompanionClient] unable to fetch preAuthToken ${err}`, 'warning') | ||
} | ||
} | ||
@@ -82,0 +97,0 @@ |
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
71943
29
1036
Updated@uppy/utils@^4.0.5