Pārlūkot izejas kodu

aws-s3: Use RequestClient.

Renée Kooi 6 gadi atpakaļ
vecāks
revīzija
cd71bc48b3
1 mainītis faili ar 14 papildinājumiem un 4 dzēšanām
  1. 14 4
      packages/@uppy/aws-s3/src/index.js

+ 14 - 4
packages/@uppy/aws-s3/src/index.js

@@ -2,6 +2,7 @@ const resolveUrl = require('resolve-url')
 const { Plugin } = require('@uppy/core')
 const { Plugin } = require('@uppy/core')
 const Translator = require('@uppy/utils/lib/Translator')
 const Translator = require('@uppy/utils/lib/Translator')
 const limitPromises = require('@uppy/utils/lib/limitPromises')
 const limitPromises = require('@uppy/utils/lib/limitPromises')
+const { RequestClient } = require('@uppy/companion-client')
 const XHRUpload = require('@uppy/xhr-upload')
 const XHRUpload = require('@uppy/xhr-upload')
 
 
 function isXml (xhr) {
 function isXml (xhr) {
@@ -9,6 +10,15 @@ function isXml (xhr) {
   return typeof contentType === 'string' && contentType.toLowerCase() === 'application/xml'
   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 {
 module.exports = class AwsS3 extends Plugin {
   constructor (uppy, opts) {
   constructor (uppy, opts) {
     super(uppy, opts)
     super(uppy, opts)
@@ -36,6 +46,8 @@ module.exports = class AwsS3 extends Plugin {
     this.translator = new Translator({ locale: this.locale })
     this.translator = new Translator({ locale: this.locale })
     this.i18n = this.translator.translate.bind(this.translator)
     this.i18n = this.translator.translate.bind(this.translator)
 
 
+    this.client = new RequestClient(uppy, opts)
+
     this.prepareUpload = this.prepareUpload.bind(this)
     this.prepareUpload = this.prepareUpload.bind(this)
 
 
     if (typeof this.opts.limit === 'number' && this.opts.limit !== 0) {
     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 filename = encodeURIComponent(file.name)
     const type = encodeURIComponent(file.type)
     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) {
   validateParameters (file, params) {