Skip to content

Commit

Permalink
fetch client handle formdata with array of file properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Untaek authored May 24, 2024
1 parent 5d4fe0d commit bb69900
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions templates/base/http-clients/fetch-http-client.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,28 @@ export class HttpClient<SecurityDataType = unknown> {
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
[ContentType.FormData]: (input: any) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
key,
property instanceof Blob ?
property :
typeof property === "object" && property !== null ?
JSON.stringify(property) :
`${property}`
);
return formData;
}, new FormData()),
{
const append = (formData: FormData, key: string, value: any) => formData.append(
key,
value instanceof Blob
? value
: typeof value === 'object' && value !== null
? JSON.stringify(value)
: `${value}`,
)

return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key]
if (Array.isArray(property) && property.length === property.filter(p => p instanceof Blob).length) {
property.forEach(element => {
append(formData, key, element)
})
} else {
append(formData, key, property)
}
return formData
}, new FormData())
},
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
}

Expand Down

0 comments on commit bb69900

Please sign in to comment.