Skip to content

Commit 19e4d50

Browse files
committed
fix pick format
allow for file array in form data requests
1 parent 5817a95 commit 19e4d50

File tree

5 files changed

+86
-33
lines changed

5 files changed

+86
-33
lines changed

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

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,36 @@ export class HttpClient<SecurityDataType = unknown> {
6666
};
6767
}
6868

69+
private stringifyItem(item: any): string {
70+
if (typeof item === "object" && item !== null) {
71+
return JSON.stringify(item);
72+
}
73+
74+
return `${item}`;
75+
}
76+
6977
private createFormData(input: Record<string, unknown>): FormData {
78+
return Object.keys(input || {}).reduce((formData, key) => {
79+
const property = input[key];
80+
const propertyContent: Iterable<any> = (property instanceof Array) ? property : [property]
81+
82+
for (const formItem of propertyContent) {
83+
const isFileType = formItem instanceof Blob || formItem instanceof File;
84+
formData.append(
85+
key,
86+
isFileType
87+
? formItem
88+
: this.stringifyItem(formItem),
89+
);
90+
}
91+
92+
return formData;
93+
}, new FormData());
7094
return Object.keys(input || {}).reduce((formData, key) => {
7195
const property = input[key];
7296
formData.append(
7397
key,
74-
property instanceof Blob ?
75-
property :
76-
typeof property === "object" && property !== null ?
77-
JSON.stringify(property) :
78-
`${property}`
98+
property instanceof Blob ? property : this.stringifyItem(property),
7999
);
80100
return formData;
81101
}, new FormData())
@@ -96,7 +116,7 @@ export class HttpClient<SecurityDataType = unknown> {
96116
<% } %>
97117
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
98118
const requestParams = this.mergeRequestParams(params, secureParams);
99-
const responseFormat = (format && this.format) || void 0;
119+
const responseFormat = (format || this.format) || undefined;
100120

101121
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
102122
requestParams.headers.common = { Accept: "*/*" };

tests/spec/axios/schema.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,17 +1510,29 @@ export class HttpClient<SecurityDataType = unknown> {
15101510
};
15111511
}
15121512

1513+
private stringifyItem(item: any): string {
1514+
if (typeof item === "object" && item !== null) {
1515+
return JSON.stringify(item);
1516+
}
1517+
1518+
return `${item}`;
1519+
}
1520+
15131521
private createFormData(input: Record<string, unknown>): FormData {
15141522
return Object.keys(input || {}).reduce((formData, key) => {
15151523
const property = input[key];
1516-
formData.append(
1517-
key,
1518-
property instanceof Blob
1519-
? property
1520-
: typeof property === "object" && property !== null
1521-
? JSON.stringify(property)
1522-
: `${property}`,
1523-
);
1524+
const propertyContent: Iterable<any> = property instanceof Array ? property : [property];
1525+
1526+
for (const formItem of propertyContent) {
1527+
const isFileType = formItem instanceof Blob || formItem instanceof File;
1528+
formData.append(key, isFileType ? formItem : this.stringifyItem(formItem));
1529+
}
1530+
1531+
return formData;
1532+
}, new FormData());
1533+
return Object.keys(input || {}).reduce((formData, key) => {
1534+
const property = input[key];
1535+
formData.append(key, property instanceof Blob ? property : this.stringifyItem(property));
15241536
return formData;
15251537
}, new FormData());
15261538
}
@@ -1540,7 +1552,7 @@ export class HttpClient<SecurityDataType = unknown> {
15401552
(await this.securityWorker(this.securityData))) ||
15411553
{};
15421554
const requestParams = this.mergeRequestParams(params, secureParams);
1543-
const responseFormat = (format && this.format) || void 0;
1555+
const responseFormat = format || this.format || undefined;
15441556

15451557
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
15461558
requestParams.headers.common = { Accept: "*/*" };

tests/spec/axiosSingleHttpClient/schema.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,17 +1510,29 @@ export class HttpClient<SecurityDataType = unknown> {
15101510
};
15111511
}
15121512

1513+
private stringifyItem(item: any): string {
1514+
if (typeof item === "object" && item !== null) {
1515+
return JSON.stringify(item);
1516+
}
1517+
1518+
return `${item}`;
1519+
}
1520+
15131521
private createFormData(input: Record<string, unknown>): FormData {
15141522
return Object.keys(input || {}).reduce((formData, key) => {
15151523
const property = input[key];
1516-
formData.append(
1517-
key,
1518-
property instanceof Blob
1519-
? property
1520-
: typeof property === "object" && property !== null
1521-
? JSON.stringify(property)
1522-
: `${property}`,
1523-
);
1524+
const propertyContent: Iterable<any> = property instanceof Array ? property : [property];
1525+
1526+
for (const formItem of propertyContent) {
1527+
const isFileType = formItem instanceof Blob || formItem instanceof File;
1528+
formData.append(key, isFileType ? formItem : this.stringifyItem(formItem));
1529+
}
1530+
1531+
return formData;
1532+
}, new FormData());
1533+
return Object.keys(input || {}).reduce((formData, key) => {
1534+
const property = input[key];
1535+
formData.append(key, property instanceof Blob ? property : this.stringifyItem(property));
15241536
return formData;
15251537
}, new FormData());
15261538
}
@@ -1540,7 +1552,7 @@ export class HttpClient<SecurityDataType = unknown> {
15401552
(await this.securityWorker(this.securityData))) ||
15411553
{};
15421554
const requestParams = this.mergeRequestParams(params, secureParams);
1543-
const responseFormat = (format && this.format) || void 0;
1555+
const responseFormat = format || this.format || undefined;
15441556

15451557
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
15461558
requestParams.headers.common = { Accept: "*/*" };

tests/spec/jsAxios/schema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,7 @@ export declare class HttpClient<SecurityDataType = unknown> {
17401740
constructor({ securityWorker, secure, format, ...axiosConfig }?: ApiConfig<SecurityDataType>);
17411741
setSecurityData: (data: SecurityDataType | null) => void;
17421742
private mergeRequestParams;
1743+
private stringifyItem;
17431744
private createFormData;
17441745
request: <T = any, _E = any>({
17451746
secure,

tests/spec/jsAxios/schema.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class HttpClient {
2929
(await this.securityWorker(this.securityData))) ||
3030
{};
3131
const requestParams = this.mergeRequestParams(params, secureParams);
32-
const responseFormat = (format && this.format) || void 0;
32+
const responseFormat = format || this.format || undefined;
3333
if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
3434
requestParams.headers.common = { Accept: "*/*" };
3535
requestParams.headers.post = {};
@@ -65,17 +65,25 @@ export class HttpClient {
6565
},
6666
};
6767
}
68+
stringifyItem(item) {
69+
if (typeof item === "object" && item !== null) {
70+
return JSON.stringify(item);
71+
}
72+
return `${item}`;
73+
}
6874
createFormData(input) {
6975
return Object.keys(input || {}).reduce((formData, key) => {
7076
const property = input[key];
71-
formData.append(
72-
key,
73-
property instanceof Blob
74-
? property
75-
: typeof property === "object" && property !== null
76-
? JSON.stringify(property)
77-
: `${property}`,
78-
);
77+
const propertyContent = property instanceof Array ? property : [property];
78+
for (const formItem of propertyContent) {
79+
const isFileType = formItem instanceof Blob || formItem instanceof File;
80+
formData.append(key, isFileType ? formItem : this.stringifyItem(formItem));
81+
}
82+
return formData;
83+
}, new FormData());
84+
return Object.keys(input || {}).reduce((formData, key) => {
85+
const property = input[key];
86+
formData.append(key, property instanceof Blob ? property : this.stringifyItem(property));
7987
return formData;
8088
}, new FormData());
8189
}

0 commit comments

Comments
 (0)