Skip to content

Commit

Permalink
fix: add content type headers only if func args are set
Browse files Browse the repository at this point in the history
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
  • Loading branch information
inian committed Aug 15, 2022
1 parent ad25d80 commit 81fbb72
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/FunctionsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,28 @@ export class FunctionsClient {
try {
let _headers: Record<string, string> = {}
let body: any
if (
(typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
functionArgs instanceof ArrayBuffer
) {
// will work for File as File inherits Blob
// also works for ArrayBuffer as it is the same underlying structure as a Blob
_headers['Content-Type'] = 'application/octet-stream'
body = functionArgs
} else if (typeof functionArgs === 'string') {
// plain string
_headers['Content-Type'] = 'text/plain'
body = functionArgs
} else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
// don't set content-type headers
// Request will automatically add the right boundary value
body = functionArgs
} else {
// default, assume this is JSON
_headers['Content-Type'] = 'application/json'
body = JSON.stringify(functionArgs)
if (functionArgs && !Object.prototype.hasOwnProperty.call(headers, 'Content-Type')) {
if (
(typeof Blob !== 'undefined' && functionArgs instanceof Blob) ||
functionArgs instanceof ArrayBuffer
) {
// will work for File as File inherits Blob
// also works for ArrayBuffer as it is the same underlying structure as a Blob
_headers['Content-Type'] = 'application/octet-stream'
body = functionArgs
} else if (typeof functionArgs === 'string') {
// plain string
_headers['Content-Type'] = 'text/plain'
body = functionArgs
} else if (typeof FormData !== 'undefined' && functionArgs instanceof FormData) {
// don't set content-type headers
// Request will automatically add the right boundary value
body = functionArgs
} else {
// default, assume this is JSON
_headers['Content-Type'] = 'application/json'
body = JSON.stringify(functionArgs)
}
}

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

0 comments on commit 81fbb72

Please sign in to comment.