Skip to content

Commit

Permalink
[typescript-angular2] Feature/angular2 file post (#4623)
Browse files Browse the repository at this point in the history
* Merge branch 'www2k-feature/file-response'

* Merge pull request #4421 from wing328/fix_isfile_boolean

Fix `isPrimitiveType` for file property

* roll back to latest working version of swagger paresr for codegen

* enable typescript-angular2 to upload file

* update typescript-angular2 samples
  • Loading branch information
wing328 authored Jan 23, 2017
1 parent 4034ee0 commit 4fcef31
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 191 deletions.
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

0 comments on commit 4fcef31

Please sign in to comment.