Angular2 rc5 (#3603)

* Changes due to changes in Angular's http module.

* Added link to issue re Headers clone. Removed use-strict since its emitted by default from TypeScript 1.8 on.

* Added missing import for ResponseContentType.

* Added null check fix for Angular http issue.

* Updated package.json and typings

Signed-off-by: Sebastian Haas <sebastian@haas.tech>
This commit is contained in:
Sebastian Haas 2016-08-28 12:30:11 +02:00 committed by wing328
parent 440b7211de
commit 4337f050f5
5 changed files with 117 additions and 47 deletions

View File

@ -3,10 +3,13 @@ package io.swagger.codegen.languages;
import java.io.File; import java.io.File;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
import java.util.List;
import java.util.Map;
import io.swagger.codegen.CliOption; import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenModel; import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenParameter; import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.SupportingFile; import io.swagger.codegen.SupportingFile;
import io.swagger.models.ModelImpl; import io.swagger.models.ModelImpl;
import io.swagger.models.properties.ArrayProperty; import io.swagger.models.properties.ArrayProperty;
@ -163,6 +166,46 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
parameter.dataType = addModelPrefix(parameter.dataType); parameter.dataType = addModelPrefix(parameter.dataType);
} }
@Override
public Map<String, Object> postProcessOperations(Map<String, Object> operations) {
Map<String, Object> objs = (Map<String, Object>) operations.get("operations");
List<CodegenOperation> ops = (List<CodegenOperation>) objs.get("operation");
for (CodegenOperation op : ops) {
// Convert httpMethod to Angular's RequestMethod enum
// https://angular.io/docs/ts/latest/api/http/index/RequestMethod-enum.html
switch (op.httpMethod) {
case "GET":
op.httpMethod = "RequestMethod.Get";
break;
case "POST":
op.httpMethod = "RequestMethod.Post";
break;
case "PUT":
op.httpMethod = "RequestMethod.Put";
break;
case "DELETE":
op.httpMethod = "RequestMethod.Delete";
break;
case "OPTIONS":
op.httpMethod = "RequestMethod.Options";
break;
case "HEAD":
op.httpMethod = "RequestMethod.Head";
break;
case "PATCH":
op.httpMethod = "RequestMethod.Patch";
break;
default:
throw new RuntimeException("Unknown HTTP Method " + op.httpMethod + " not allowed");
}
// Convert path to TypeScript template string
op.path = op.path.replaceAll("\\{(.*?)\\}", "\\$\\{$1\\}");
}
return operations;
}
public String getNpmName() { public String getNpmName() {
return npmName; return npmName;
} }

View File

@ -1,15 +1,18 @@
{{>licenseInfo}} {{>licenseInfo}}
import {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from '@angular/http'; import { Inject, Injectable, Optional } from '@angular/core';
import {Inject, Injectable, Optional} from '@angular/core'; import { Http, Headers, URLSearchParams } from '@angular/http';
import {Observable} from 'rxjs/Observable'; import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
import * as models from '../model/models'; import { Response, ResponseContentType } from '@angular/http';
import {BASE_PATH} from '../variables';
import 'rxjs/Rx'; import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import * as models from '../model/models';
import { BASE_PATH } from '../variables';
/* tslint:disable:no-unused-variable member-ordering */ /* tslint:disable:no-unused-variable member-ordering */
{{#operations}} {{#operations}}
'use strict';
{{#description}} {{#description}}
/** /**
@ -19,7 +22,7 @@ import 'rxjs/Rx';
@Injectable() @Injectable()
export class {{classname}} { export class {{classname}} {
protected basePath = '{{basePath}}'; protected basePath = '{{basePath}}';
public defaultHeaders : Headers = new Headers(); public defaultHeaders: Headers = new Headers();
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) { constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
if (basePath) { if (basePath) {
@ -33,12 +36,11 @@ export class {{classname}} {
* {{notes}} * {{notes}}
{{#allParams}}* @param {{paramName}} {{description}} {{#allParams}}* @param {{paramName}} {{description}}
{{/allParams}}*/ {{/allParams}}*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> { public {{nickname}}({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}extraHttpRequestParams?: any): Observable<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}{}{{/returnType}}> {
const path = this.basePath + '{{path}}'{{#pathParams}} const path = this.basePath + `{{path}}`;
.replace('{' + '{{baseName}}' + '}', String({{paramName}})){{/pathParams}};
let queryParameters = new URLSearchParams(); let queryParameters = new URLSearchParams();
let headerParams = this.defaultHeaders; let headers = new Headers(this.defaultHeaders.values()); // https://github.com/angular/angular/issues/6845
{{#hasFormParams}} {{#hasFormParams}}
let formParams = new URLSearchParams(); let formParams = new URLSearchParams();
@ -57,29 +59,49 @@ export class {{classname}} {
} }
{{/queryParams}} {{/queryParams}}
{{#headerParams}} {{#headers}}
headerParams.set('{{baseName}}', String({{paramName}})); headers.set('{{baseName}}', String({{paramName}}));
{{/headers}}
// to determine the Content-Type header
let consumes: string[] = [
{{#consumes}}
'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}
{{/consumes}}
];
// to determine the Accept header
let produces: string[] = [
{{#produces}}
'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}
{{/produces}}
];
{{/headerParams}}
{{#hasFormParams}} {{#hasFormParams}}
headerParams.set('Content-Type', 'application/x-www-form-urlencoded'); headers.set('Content-Type', 'application/x-www-form-urlencoded');
{{/hasFormParams}} {{/hasFormParams}}
{{#bodyParam}}
headers.set('Content-Type', 'application/json');
{{/bodyParam}}
{{#formParams}} {{#formParams}}
formParams['{{baseName}}'] = {{paramName}}; formParams['{{baseName}}'] = {{paramName}};
{{/formParams}} {{/formParams}}
let requestOptions: RequestOptionsArgs = { let requestOptions: RequestOptionsArgs = new RequestOptions({
method: '{{httpMethod}}', method: {{httpMethod}},
headers: headerParams, headers: headers,
search: queryParameters {{#bodyParam}}
}; body: {{paramName}} == null ? '' : JSON.stringify({{paramName}}), // https://github.com/angular/angular/issues/10612
{{#bodyParam}} {{/bodyParam}}
requestOptions.body = JSON.stringify({{paramName}}); {{#hasFormParams}}
{{/bodyParam}} body: formParams.toString(),
{{#hasFormParams}} {{/hasFormParams}}
requestOptions.body = formParams.toString(); search: queryParameters,
{{/hasFormParams}} responseType: ResponseContentType.Json
});
return this.http.request(path, requestOptions) return this.http.request(path, requestOptions)
.map((response: Response) => { .map((response: Response) => {

View File

@ -1,7 +1,6 @@
{{>licenseInfo}} {{>licenseInfo}}
{{#models}} {{#models}}
{{#model}} {{#model}}
'use strict';
import * as models from './models'; import * as models from './models';
{{#description}} {{#description}}

View File

@ -13,27 +13,33 @@
"main": "./lib/index.js", "main": "./lib/index.js",
"typings": "./lib/index.d.ts", "typings": "./lib/index.d.ts",
"scripts": { "scripts": {
"build": "typings install && tsc" "build": "typings install && tsc",
"postinstall": "npm run build"
}, },
"peerDependencies": { "peerDependencies": {
"@angular/core": "^2.0.0-rc.1", "@angular/core": "^2.0.0-rc.5",
"@angular/http": "^2.0.0-rc.1" "@angular/http": "^2.0.0-rc.5",
"@angular/common": "^2.0.0-rc.5",
"@angular/compiler": "^2.0.0-rc.5",
"core-js": "^2.4.0",
"reflect-metadata": "^0.1.3",
"rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.17"
}, },
"devDependencies": { "devDependencies": {
"@angular/common": "^2.0.0-rc.1", "@angular/core": "^2.0.0-rc.5",
"@angular/compiler": "^2.0.0-rc.1", "@angular/http": "^2.0.0-rc.5",
"@angular/core": "^2.0.0-rc.1", "@angular/common": "^2.0.0-rc.5",
"@angular/http": "^2.0.0-rc.1", "@angular/compiler": "^2.0.0-rc.5",
"@angular/platform-browser": "^2.0.0-rc.1", "@angular/platform-browser": "^2.0.0-rc.5",
"@angular/platform-browser-dynamic": "^2.0.0-rc.1", "core-js": "^2.4.0",
"core-js": "^2.3.0", "reflect-metadata": "^0.1.3",
"rxjs": "^5.0.0-beta.6", "rxjs": "5.0.0-beta.6",
"zone.js": "^0.6.12", "zone.js": "^0.6.17",
"typescript": "^1.8.10", "typescript": "^1.8.10",
"typings": "^0.8.1", "typings": "^1.3.2"
"es6-shim": "^0.35.0", }{{#npmRepository}},{{/npmRepository}}
"es7-reflect-metadata": "^1.6.0" {{#npmRepository}}
}{{#npmRepository}},
"publishConfig":{ "publishConfig":{
"registry":"{{npmRepository}}" "registry":"{{npmRepository}}"
} }

View File

@ -1,5 +1,5 @@
{ {
"ambientDependencies": { "globalDependencies": {
"core-js": "registry:dt/core-js#0.0.0+20160317120654" "core-js": "registry:dt/core-js#0.0.0+20160725163759"
} }
} }