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

[typescript-angular2] Feature/angular2 file post #4623

Merged
merged 6 commits into from
Jan 23, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public TypeScriptAngular2ClientCodegen() {
embeddedTemplateDir = templateDir = "typescript-angular2";
modelTemplateFiles.put("model.mustache", ".ts");
apiTemplateFiles.put("api.service.mustache", ".ts");
languageSpecificPrimitives.add("Blob");
typeMapping.put("Date","Date");
typeMapping.put("file","Blob");
apiPackage = "api";
modelPackage = "model";

Expand Down Expand Up @@ -116,6 +118,11 @@ private String getIndexDirectory() {
return indexPackage.replace('.', File.separatorChar);
}

@Override
public boolean isDataTypeFile(final String dataType) {
return dataType != null && dataType.equals("Blob");
}

@Override
public String getTypeDeclaration(Property p) {
Property inner;
Expand All @@ -127,7 +134,9 @@ public String getTypeDeclaration(Property p) {
MapProperty mp = (MapProperty)p;
inner = mp.getAdditionalProperties();
return "{ [key: string]: " + this.getTypeDeclaration(inner) + "; }";
} else if(p instanceof FileProperty || p instanceof ObjectProperty) {
} else if(p instanceof FileProperty) {
return "Blob";
} else if(p instanceof ObjectProperty) {
return "any";
} else {
return super.getTypeDeclaration(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ export class {{classname}} {
return <T1&T2>objA;
}

/**
* @param consumes string[] mime-types
* @return true: consumes contains 'multipart/form-data', false: otherwise
*/
private canConsumeForm(consumes: string[]): boolean {
const form = 'multipart/form-data';
for (let consume of consumes) {
if (form === consume) {
return true;
}
}
return false;
}

{{#operation}}
/**
* {{summary}}
Expand All @@ -66,7 +80,12 @@ export class {{classname}} {
if (response.status === 204) {
return undefined;
} else {
{{^isResponseFile}}
return response.json();
{{/isResponseFile}}
{{#isResponseFile}}
return response.blob();
{{/isResponseFile}}
}
});
}
Expand All @@ -84,10 +103,7 @@ export class {{classname}} {

let queryParameters = new URLSearchParams();
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
{{#hasFormParams}}
let formParams = new URLSearchParams();

{{/hasFormParams}}
{{#allParams}}
{{#required}}
// verify required parameter '{{paramName}}' is not null or undefined
Expand All @@ -106,12 +122,24 @@ export class {{classname}} {
headers.set('{{baseName}}', String({{paramName}}));
{{/headerParams}}

{{#hasFormParams}}
// to determine the Content-Type header
let consumes: string[] = [
{{#consumes}}
'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}
{{/consumes}}
];
let canConsumeForm = this.canConsumeForm(consumes);
let useForm = false;
{{#formParams}}
{{#isFile}}
useForm = canConsumeForm;
{{/isFile}}
{{/formParams}}
let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
set(param: string, value: any): void;
};
{{/hasFormParams}}

// to determine the Accept header
let produces: string[] = [
Expand Down Expand Up @@ -155,17 +183,13 @@ export class {{classname}} {
{{/isOAuth}}
{{/authMethods}}

{{#hasFormParams}}
headers.set('Content-Type', 'application/x-www-form-urlencoded');
{{/hasFormParams}}

{{#bodyParam}}
headers.set('Content-Type', 'application/json');
{{/bodyParam}}

{{#formParams}}
if ({{paramName}} !== undefined) {
formParams.set('{{baseName}}', <any>{{paramName}});
formParams.set('{{baseName}}', {{paramName}});
}
{{/formParams}}

Expand All @@ -176,8 +200,11 @@ export class {{classname}} {
body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
{{/bodyParam}}
{{#hasFormParams}}
body: formParams.toString(),
body: formParams,
{{/hasFormParams}}
{{#isResponseFile}}
responseType: ResponseContentType.Blob,
{{/isResponseFile}}
search: queryParameters
});

Expand Down
Loading