Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix request format and allow for files in request - With code review changes #350

Merged
merged 3 commits into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion templates/base/http-clients/axios-http-client.eta
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,20 @@ export class HttpClient<SecurityDataType = unknown> {
}

protected createFormData(input: Record<string, unknown>): FormData {
return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
const propertyContent: Iterable<any> = (property instanceof Array) ? property : [property]

for (const formItem of propertyContent) {
const isFileType = formItem instanceof Blob || formItem instanceof File;
formData.append(
key,
isFileType ? formItem : this.stringifyFormItem(formItem)
);
}

return formData;
}, new FormData());
return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
Expand Down Expand Up @@ -98,7 +112,7 @@ export class HttpClient<SecurityDataType = unknown> {
<% } %>
const secureParams = ((typeof secure === 'boolean' ? secure : this.secure) && this.securityWorker && (await this.securityWorker(this.securityData))) || {};
const requestParams = this.mergeRequestParams(params, secureParams);
const responseFormat = (format && this.format) || void 0;
const responseFormat = (format || this.format) || undefined;

if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
body = this.createFormData(body as Record<string, unknown>);
Expand Down
13 changes: 12 additions & 1 deletion tests/spec/axios/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,17 @@ export class HttpClient<SecurityDataType = unknown> {
}

protected createFormData(input: Record<string, unknown>): FormData {
return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
const propertyContent: Iterable<any> = property instanceof Array ? property : [property];

for (const formItem of propertyContent) {
const isFileType = formItem instanceof Blob || formItem instanceof File;
formData.append(key, isFileType ? formItem : this.stringifyFormItem(formItem));
}

return formData;
}, new FormData());
return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
Expand Down Expand Up @@ -1542,7 +1553,7 @@ export class HttpClient<SecurityDataType = unknown> {
(await this.securityWorker(this.securityData))) ||
{};
const requestParams = this.mergeRequestParams(params, secureParams);
const responseFormat = (format && this.format) || void 0;
const responseFormat = format || this.format || undefined;

if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
body = this.createFormData(body as Record<string, unknown>);
Expand Down
19 changes: 18 additions & 1 deletion tests/spec/axiosSingleHttpClient/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1513,6 +1513,23 @@ export class HttpClient<SecurityDataType = unknown> {
}

protected createFormData(input: Record<string, unknown>): FormData {
return Object.keys(input || {}).reduce((formData, key) => {
let property = input[key];
let propertyContent: Iterable<any> = property instanceof Array ? property : [property];

for (const formItem of propertyContent) {
formData.append(
key,
formItem instanceof Blob || formItem instanceof File
? formItem
: typeof formItem === "object" && formItem !== null
? JSON.stringify(formItem)
: `${formItem}`,
);
}

return formData;
}, new FormData());
return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
Expand Down Expand Up @@ -1542,7 +1559,7 @@ export class HttpClient<SecurityDataType = unknown> {
(await this.securityWorker(this.securityData))) ||
{};
const requestParams = this.mergeRequestParams(params, secureParams);
const responseFormat = (format && this.format) || void 0;
const responseFormat = format || this.format || void 0;

if (type === ContentType.FormData && body && body !== null && typeof body === "object") {
body = this.createFormData(body as Record<string, unknown>);
Expand Down
16 changes: 16 additions & 0 deletions tests/spec/jsAxios/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,23 @@ export class HttpClient {
},
};
}
stringifyFormItem(formItem) {
if (typeof formItem === "object" && formItem !== null) {
return JSON.stringify(formItem);
} else {
return `${formItem}`;
}
}
createFormData(input) {
return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
const propertyContent = property instanceof Array ? property : [property];
for (const formItem of propertyContent) {
const isFileType = formItem instanceof Blob || formItem instanceof File;
formData.append(key, isFileType ? formItem : stringifyFormItem(formItem));
}
return formData;
}, new FormData());
return Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
Expand Down