Skip to content

Commit 5440ce4

Browse files
committed
normalize modified options body
1 parent 7488abc commit 5440ce4

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

src/Client.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,15 @@ export default class Client {
206206
async send<T = any>(path: string, reqOptions: SendOptions): Promise<T> {
207207
let options = Object.assign({ method: 'GET' } as SendOptions, reqOptions);
208208

209-
// JSON serialize the body if needed and set the correct content type
209+
// add the json header, if not already
210210
// (for FormData body the Content-Type header should be skipped since the boundary is autogenerated)
211-
if (!this.isFormData(options.body)) {
212-
if (options.body && typeof options.body !== 'string') {
213-
options.body = JSON.stringify(options.body);
214-
}
215-
216-
// add the json header (if not already)
217-
if (typeof options?.headers?.['Content-Type'] === 'undefined') {
218-
options.headers = Object.assign({}, options.headers, {
219-
'Content-Type': 'application/json',
220-
});
221-
}
211+
if (
212+
!this.isFormData(options.body) &&
213+
typeof options?.headers?.['Content-Type'] === 'undefined'
214+
) {
215+
options.headers = Object.assign({}, options.headers, {
216+
'Content-Type': 'application/json',
217+
});
222218
}
223219

224220
// add Accept-Language header (if not already)
@@ -279,6 +275,15 @@ export default class Client {
279275
}
280276
}
281277

278+
// ensures that the json body is serialized
279+
if (
280+
options?.headers?.['Content-Type'] == 'application/json' &&
281+
options.body &&
282+
typeof options.body !== 'string'
283+
) {
284+
options.body = JSON.stringify(options.body);
285+
}
286+
282287
// send the request
283288
return fetch(url, options)
284289
.then(async (response) => {

0 commit comments

Comments
 (0)