Skip to content

Commit 81fbb72

Browse files
committed
fix: add content type headers only if func args are set
and if the user hasn't passed in a content-type header via the constructor arguments before we added application/json even if there are no function args set
1 parent ad25d80 commit 81fbb72

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/FunctionsClient.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -54,26 +54,28 @@ export class FunctionsClient {
5454
try {
5555
let _headers: Record<string, string> = {}
5656
let body: any
57-
if (
58-
(typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
59-
functionArgs instanceof ArrayBuffer
60-
) {
61-
// will work for File as File inherits Blob
62-
// also works for ArrayBuffer as it is the same underlying structure as a Blob
63-
_headers['Content-Type'] = 'application/octet-stream'
64-
body = functionArgs
65-
} else if (typeof functionArgs === 'string') {
66-
// plain string
67-
_headers['Content-Type'] = 'text/plain'
68-
body = functionArgs
69-
} else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
70-
// don't set content-type headers
71-
// Request will automatically add the right boundary value
72-
body = functionArgs
73-
} else {
74-
// default, assume this is JSON
75-
_headers['Content-Type'] = 'application/json'
76-
body = JSON.stringify(functionArgs)
57+
if (functionArgs && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) {
58+
if (
59+
(typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
60+
functionArgs instanceof ArrayBuffer
61+
) {
62+
// will work for File as File inherits Blob
63+
// also works for ArrayBuffer as it is the same underlying structure as a Blob
64+
_headers['Content-Type'] = 'application/octet-stream'
65+
body = functionArgs
66+
} else if (typeof functionArgs === 'string') {
67+
// plain string
68+
_headers['Content-Type'] = 'text/plain'
69+
body = functionArgs
70+
} else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
71+
// don't set content-type headers
72+
// Request will automatically add the right boundary value
73+
body = functionArgs
74+
} else {
75+
// default, assume this is JSON
76+
_headers['Content-Type'] = 'application/json'
77+
body = JSON.stringify(functionArgs)
78+
}
7779
}
7880

7981
const response = await this.fetch(`${this.url}/${functionName}`, {

0 commit comments

Comments
 (0)