Skip to content

Commit bb69900

Browse files
authored
fetch client handle formdata with array of file properly
1 parent 5d4fe0d commit bb69900

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

templates/base/http-clients/fetch-http-client.ejs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,28 @@ export class HttpClient<SecurityDataType = unknown> {
105105
[ContentType.Json]: (input:any) => input !== null && (typeof input === "object" || typeof input === "string") ? JSON.stringify(input) : input,
106106
[ContentType.Text]: (input:any) => input !== null && typeof input !== "string" ? JSON.stringify(input) : input,
107107
[ContentType.FormData]: (input: any) =>
108-
Object.keys(input || {}).reduce((formData, key) => {
109-
const property = input[key];
110-
formData.append(
111-
key,
112-
property instanceof Blob ?
113-
property :
114-
typeof property === "object" && property !== null ?
115-
JSON.stringify(property) :
116-
`${property}`
117-
);
118-
return formData;
119-
}, new FormData()),
108+
{
109+
const append = (formData: FormData, key: string, value: any) => formData.append(
110+
key,
111+
value instanceof Blob
112+
? value
113+
: typeof value === 'object' && value !== null
114+
? JSON.stringify(value)
115+
: `${value}`,
116+
)
117+
118+
return Object.keys(input || {}).reduce((formData, key) => {
119+
const property = input[key]
120+
if (Array.isArray(property) && property.length === property.filter(p => p instanceof Blob).length) {
121+
property.forEach(element => {
122+
append(formData, key, element)
123+
})
124+
} else {
125+
append(formData, key, property)
126+
}
127+
return formData
128+
}, new FormData())
129+
},
120130
[ContentType.UrlEncoded]: (input: any) => this.toQueryString(input),
121131
}
122132

0 commit comments

Comments
 (0)