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

Feature/multipart formdata #18

Open
wants to merge 2 commits into
base: typescript-axios
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,45 @@ export class {{classname}}Resource {
* {{notes}}
{{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/
public {{nickname}}({{#allParams}}{{^isQueryParam}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/isQueryParam}}{{/allParams}}{{#hasQueryParams}}query?: { {{#queryParams}}{{paramName}}{{^required}}?{{/required}}: {{dataType}}{{^-last}},{{/-last}} {{/queryParams}}}, {{/hasQueryParams}}axiosConfig?: AxiosRequestConfig): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
public {{nickname}}({{#allParams}}{{^isQueryParam}}{{paramName}}{{^required}}?{{/required}}: Partial<{{{dataType}}}>, {{/isQueryParam}}{{/allParams}}{{#hasQueryParams}}query?: { {{#queryParams}}{{paramName}}{{^required}}?{{/required}}: {{dataType}}{{^-last}},{{/-last}} {{/queryParams}}}, {{/hasQueryParams}}axiosConfig?: AxiosRequestConfig): Promise<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
const reqPath = '{{path}}'{{#pathParams}}
.replace('{' + '{{baseName}}}', String({{paramName}}{{#isDateTime}}.toISOString(){{/isDateTime}})){{/pathParams}};
{{#hasFormParams}}
{{#hasFormParams}}
let hasFile = false;
{{#formParams}}
{{#isFile}}hasFile = true;
{{/isFile}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a ternary?

{{/formParams}}

let reqFormParams = {
{{#formParams}}
{{#formParams}}
{{baseName}}: {{paramName}}{{^-last}},{{/-last}}
{{/formParams}}
{{/formParams}}
};
{{/hasFormParams}}

let headers: any;
let data: any;

if (hasFile) {
headers = { 'Content-Type': 'multipart/form-data' };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we learned, I think you want to leave this header off and let FormData apply the header itself.

data = new FormData();
Object.keys(reqFormParams).forEach((key) => {
data.append(key, reqFormParams[key]);
});
} else {
headers = { 'Content-Type': 'application/x-www-form-urlencoded' };
data = stringify(reqFormParams);
}
{{/hasFormParams}}
let reqConfig = {
...axiosConfig,
method: '{{httpMethod}}',
url: reqPath{{#hasQueryParams}},
params: query{{/hasQueryParams}}{{#bodyParam}},
data: {{paramName}}{{/bodyParam}}{{#hasFormParams}},
headers: { 'Content-Type': 'application/x-www-form-urlencoded'},
data: stringify(reqFormParams)
{{/hasFormParams}}
headers: headers,
data: data
{{/hasFormParams}}

};
return axios.request(reqConfig)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
{{#tsImports}}
import { {{classname}} } from './{{filename}}';
{{/tsImports}}{{#description}}
/**
* {{{description}}}
*/{{/description}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelGeneric}}{{/isEnum}}
/* {{{description}}} */{{/description}}{{#isEnum}}{{>modelEnum}}{{/isEnum}}{{^isEnum}}{{>modelGeneric}}{{/isEnum}}
{{/model}}
{{/models}}