@@ -8,6 +8,7 @@ import tcpp from 'tcp-ping';
88import filesizeParser from 'filesize-parser' ;
99import { pricingPageUrl } from './utils' ;
1010import { Session } from 'types' ;
11+ import FormData from 'form-data' ;
1112
1213const tcpPing = util . promisify ( tcpp . ping ) ;
1314
@@ -75,12 +76,7 @@ async function query(url: string, options: fetch.RequestInit) {
7576 }
7677
7778 if ( resp . status !== 200 ) {
78- throw Object . assign (
79- new Error ( json . message || json . error || resp . statusText ) ,
80- {
81- status : resp . status ,
82- } ,
83- ) ;
79+ throw new Error ( `${ resp . status } : ${ resp . statusText } ` ) ;
8480 }
8581 return json ;
8682}
@@ -116,7 +112,7 @@ export const post = queryWithBody('POST');
116112export const put = queryWithBody ( 'PUT' ) ;
117113export const doDelete = queryWithBody ( 'DELETE' ) ;
118114
119- export async function uploadFile ( fn : string , key : string ) {
115+ export async function uploadFile ( fn : string , key ? : string ) {
120116 const { url, backupUrl, formData, maxSize } = await post ( '/upload' , {
121117 ext : path . extname ( fn ) ,
122118 } ) ;
@@ -156,27 +152,27 @@ export async function uploadFile(fn: string, key: string) {
156152 const form = new FormData ( ) ;
157153
158154 Object . entries ( formData ) . forEach ( ( [ k , v ] ) => {
159- form . set ( k , v ) ;
155+ form . append ( k , v ) ;
160156 } ) ;
161157 const fileStream = fs . createReadStream ( fn ) ;
162158 fileStream . on ( 'data' , function ( data ) {
163159 bar . tick ( data . length ) ;
164160 } ) ;
165161
166162 if ( key ) {
167- form . set ( 'key' , key ) ;
163+ form . append ( 'key' , key ) ;
168164 }
169- form . set ( 'file' , fileStream ) ;
165+ form . append ( 'file' , fileStream ) ;
170166
171- const res = await fetch ( url , {
167+ const res = await fetch ( realUrl , {
172168 method : 'POST' ,
173169 body : form ,
174170 } ) ;
175171
176172 if ( res . status > 299 ) {
177- throw new Error ( `${ res . status } : ${ await res . text ( ) } ` ) ;
173+ throw new Error ( `${ res . status } : ${ res . statusText } ` ) ;
178174 }
179175
180176 // const body = await response.json();
181- return { hash : key } ;
177+ return { hash : key || formData . key } ;
182178}
0 commit comments