@@ -13,6 +13,7 @@ import {
13
13
InvalidHeaderFormatException ,
14
14
InvalidRequestBodyFormatException ,
15
15
} from './exceptions' ;
16
+ import { MultipartDataSerializer } from './serializers' ;
16
17
17
18
export default class HttpClient {
18
19
/**
@@ -25,7 +26,7 @@ export default class HttpClient {
25
26
/**
26
27
* The request body data.
27
28
*
28
- * @var {object | string | undefined}
29
+ * @var { object | string | undefined}
29
30
*/
30
31
private body ?: object | string ;
31
32
@@ -41,7 +42,14 @@ export default class HttpClient {
41
42
*
42
43
* @var {MockedResponse}
43
44
*/
44
- private mockedResponses : MockedResponses ;
45
+ private readonly mockedResponses : MockedResponses ;
46
+
47
+ /**
48
+ * The Multipart Data Serializer instance.
49
+ *
50
+ * @var {MultipartDataSerializer}
51
+ */
52
+ private readonly multipartDataSerializer : MultipartDataSerializer ;
45
53
46
54
/**
47
55
* Number of attempts for the request.
@@ -65,18 +73,18 @@ export default class HttpClient {
65
73
private retries : number = 0 ;
66
74
67
75
/**
68
- * The number of milliseconds to wait between retries .
76
+ * The callback that will determine if the request should be retried .
69
77
*
70
- * @var {number }
78
+ * @var {function }
71
79
*/
72
- private retryDelay : number = 0 ;
80
+ private retryCallback ?: ( response : Response | undefined , request : this , error ?: unknown ) => boolean | null ;
73
81
74
82
/**
75
- * The callback that will determine if the request should be retried .
83
+ * The number of milliseconds to wait between retries .
76
84
*
77
- * @var {function }
85
+ * @var {number }
78
86
*/
79
- private retryCallback ?: ( response : Response | undefined , request : this , error ?: unknown ) => boolean | null ;
87
+ private retryDelay : number = 0 ;
80
88
81
89
/**
82
90
* The URL for the request.
@@ -100,6 +108,7 @@ export default class HttpClient {
100
108
*/
101
109
constructor ( baseUrl ?: string , options ?: object ) {
102
110
this . mockedResponses = new MockedResponses ( ) ;
111
+ this . multipartDataSerializer = new MultipartDataSerializer ( ) ;
103
112
104
113
this . setBaseUrl ( baseUrl ) ;
105
114
this . setOptions ( options ) ;
@@ -144,7 +153,7 @@ export default class HttpClient {
144
153
*/
145
154
public asForm ( ) : this {
146
155
this . setBodyFormat ( 'FormData' ) ;
147
- this . contentType ( ' multipart/form-data' ) ;
156
+ this . contentType ( ` multipart/form-data; boundary= ${ this . multipartDataSerializer . getBoundary ( ) } ` ) ;
148
157
149
158
return this ;
150
159
}
@@ -210,14 +219,11 @@ export default class HttpClient {
210
219
let url = this . url
211
220
. split ( '://' )
212
221
. map ( ( urlSlug ) => urlSlug . replace ( / \/ { 2 , } / g, '/' ) )
213
- . join ( '://' ) ;
214
-
215
- if ( ! url . endsWith ( '/' ) ) {
216
- url += '/' ;
217
- }
222
+ . join ( '://' )
223
+ . replace ( / \/ $ / , '' ) ;
218
224
219
225
if ( ! ( url . startsWith ( 'http://' ) || url . startsWith ( 'https://' ) ) ) {
220
- url = this . baseUrl . replace ( / \/ $ / , '' ) + url ;
226
+ url = this . baseUrl . replace ( / \/ $ / , '' ) + '/' + url . replace ( / ^ \/ / , '' ) ;
221
227
}
222
228
223
229
if ( this . urlQueryParameters !== undefined ) {
@@ -376,13 +382,19 @@ export default class HttpClient {
376
382
throw new InvalidRequestBodyFormatException ( 'Cannot parse a string as FormData.' ) ;
377
383
}
378
384
379
- const formData = new FormData ( ) ;
385
+ let formData : FormData ;
386
+
387
+ if ( this . body instanceof FormData ) {
388
+ formData = this . body ;
389
+ } else {
390
+ formData = new FormData ( ) ;
380
391
381
- for ( const [ key , value ] of Object . entries ( this . body ) ) {
382
- formData . append ( key , value ) ;
392
+ for ( const [ key , value ] of Object . entries ( this . body ) ) {
393
+ formData . append ( key , value ) ;
394
+ }
383
395
}
384
396
385
- return formData ;
397
+ return this . multipartDataSerializer . generateMultipartPayload ( formData ) ;
386
398
}
387
399
388
400
if ( this . bodyFormat === 'URLSearchParams' ) {
@@ -548,11 +560,11 @@ export default class HttpClient {
548
560
/**
549
561
* Specify the body data of the request.
550
562
*
551
- * @param {object | string } body
563
+ * @param {FormData | object | string } body
552
564
*
553
565
* @return {void }
554
566
*/
555
- private setBody ( body : object | string ) : void {
567
+ private setBody ( body : FormData | object | string ) : void {
556
568
this . body = body ;
557
569
}
558
570
0 commit comments