Skip to content

Commit f6c9046

Browse files
MarcusDunnkuamanetjs2me
authored
Fix request format and allow for files in request - With code review changes (#350)
* fix pick format allow for file array in form data requests * fixed code review suggestions Co-authored-by: Daniele De Matteo <[email protected]> Co-authored-by: Sergey S. Volkov <[email protected]>
1 parent 3334480 commit f6c9046

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

templates/base/http-clients/axios-http-client.eta

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,20 @@ export class HttpClient<SecurityDataType = unknown> {
6969
}
7070

7171
protected createFormData(input: Record<string, unknown>): FormData {
72+
return Object.keys(input || {}).reduce((formData, key) => {
73+
const property = input[key];
74+
const propertyContent: Iterable<any> = (property instanceof Array) ? property : [property]
75+
76+
for (const formItem of propertyContent) {
77+
const isFileType = formItem instanceof Blob || formItem instanceof File;
78+
formData.append(
79+
key,
80+
isFileType ? formItem : this.stringifyFormItem(formItem)
81+
);
82+
}
83+
84+
return formData;
85+
}, new FormData());
7286
return Object.keys(input || {}).reduce((formData, key) => {
7387
const property = input[key];
7488
formData.append(
@@ -98,7 +112,7 @@ export class HttpClient<SecurityDataType = unknown> {
98112
<% } %>
99113
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
100114
const requestParams = this.mergeRequestParams(params, secureParams);
101-
const responseFormat = (format && this.format) || void 0;
115+
const responseFormat = (format || this.format) || undefined;
102116

103117
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
104118
body = this.createFormData(body as Record<string, unknown>);

tests/spec/axios/schema.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,17 @@ export class HttpClient<SecurityDataType = unknown> {
15131513
}
15141514

15151515
protected createFormData(input: Record<string, unknown>): FormData {
1516+
return Object.keys(input || {}).reduce((formData, key) => {
1517+
const property = input[key];
1518+
const propertyContent: Iterable<any> = property instanceof Array ? property : [property];
1519+
1520+
for (const formItem of propertyContent) {
1521+
const isFileType = formItem instanceof Blob || formItem instanceof File;
1522+
formData.append(key, isFileType ? formItem : this.stringifyFormItem(formItem));
1523+
}
1524+
1525+
return formData;
1526+
}, new FormData());
15161527
return Object.keys(input || {}).reduce((formData, key) => {
15171528
const property = input[key];
15181529
formData.append(
@@ -1542,7 +1553,7 @@ export class HttpClient<SecurityDataType = unknown> {
15421553
(await this.securityWorker(this.securityData))) ||
15431554
{};
15441555
const requestParams = this.mergeRequestParams(params, secureParams);
1545-
const responseFormat = (format && this.format) || void 0;
1556+
const responseFormat = format || this.format || undefined;
15461557

15471558
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
15481559
body = this.createFormData(body as Record<string, unknown>);

tests/spec/axiosSingleHttpClient/schema.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,23 @@ export class HttpClient<SecurityDataType = unknown> {
15131513
}
15141514

15151515
protected createFormData(input: Record<string, unknown>): FormData {
1516+
return Object.keys(input || {}).reduce((formData, key) => {
1517+
let property = input[key];
1518+
let propertyContent: Iterable<any> = property instanceof Array ? property : [property];
1519+
1520+
for (const formItem of propertyContent) {
1521+
formData.append(
1522+
key,
1523+
formItem instanceof Blob || formItem instanceof File
1524+
? formItem
1525+
: typeof formItem === "object" && formItem !== null
1526+
? JSON.stringify(formItem)
1527+
: `${formItem}`,
1528+
);
1529+
}
1530+
1531+
return formData;
1532+
}, new FormData());
15161533
return Object.keys(input || {}).reduce((formData, key) => {
15171534
const property = input[key];
15181535
formData.append(
@@ -1542,7 +1559,7 @@ export class HttpClient<SecurityDataType = unknown> {
15421559
(await this.securityWorker(this.securityData))) ||
15431560
{};
15441561
const requestParams = this.mergeRequestParams(params, secureParams);
1545-
const responseFormat = (format && this.format) || void 0;
1562+
const responseFormat = format || this.format || void 0;
15461563

15471564
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
15481565
body = this.createFormData(body as Record<string, unknown>);

tests/spec/jsAxios/schema.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,23 @@ export class HttpClient {
4444
},
4545
};
4646
}
47+
stringifyFormItem(formItem) {
48+
if (typeof formItem === "object" && formItem !== null) {
49+
return JSON.stringify(formItem);
50+
} else {
51+
return `${formItem}`;
52+
}
53+
}
4754
createFormData(input) {
55+
return Object.keys(input || {}).reduce((formData, key) => {
56+
const property = input[key];
57+
const propertyContent = property instanceof Array ? property : [property];
58+
for (const formItem of propertyContent) {
59+
const isFileType = formItem instanceof Blob || formItem instanceof File;
60+
formData.append(key, isFileType ? formItem : stringifyFormItem(formItem));
61+
}
62+
return formData;
63+
}, new FormData());
4864
return Object.keys(input || {}).reduce((formData, key) => {
4965
const property = input[key];
5066
formData.append(

0 commit comments

Comments
 (0)