aws-s3: Use RequestClient.

This commit is contained in:
Renée Kooi 2018-09-03 12:02:53 +02:00
parent 8777591fb2
commit cd71bc48b3
No known key found for this signature in database
GPG key ID: 8CDD5F0BC448F040

View file

@ -2,6 +2,7 @@ const resolveUrl = require('resolve-url')
const { Plugin } = require('@uppy/core')
const Translator = require('@uppy/utils/lib/Translator')
const limitPromises = require('@uppy/utils/lib/limitPromises')
const { RequestClient } = require('@uppy/companion-client')
const XHRUpload = require('@uppy/xhr-upload')
function isXml (xhr) {
@ -9,6 +10,15 @@ function isXml (xhr) {
return typeof contentType === 'string' && contentType.toLowerCase() === 'application/xml'
}
function assertServerError (res) {
if (res && res.error) {
const error = new Error(res.message)
Object.assign(error, res.error)
throw error
}
return res
}
module.exports = class AwsS3 extends Plugin {
constructor (uppy, opts) {
super(uppy, opts)
@ -36,6 +46,8 @@ module.exports = class AwsS3 extends Plugin {
this.translator = new Translator({ locale: this.locale })
this.i18n = this.translator.translate.bind(this.translator)
this.client = new RequestClient(uppy, opts)
this.prepareUpload = this.prepareUpload.bind(this)
if (typeof this.opts.limit === 'number' && this.opts.limit !== 0) {
@ -52,10 +64,8 @@ module.exports = class AwsS3 extends Plugin {
const filename = encodeURIComponent(file.name)
const type = encodeURIComponent(file.type)
return fetch(`${this.opts.serverUrl}/s3/params?filename=${filename}&type=${type}`, {
method: 'get',
headers: { accept: 'application/json' }
}).then((response) => response.json())
return this.client.get(`s3/params?filename=${filename}&type=${type}`)
.then(assertServerError)
}
validateParameters (file, params) {