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.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import io.swagger.codegen.CliOption;
import io.swagger.codegen.CodegenModel;
import io.swagger.codegen.CodegenParameter;
import io.swagger.codegen.CodegenOperation;
import io.swagger.codegen.SupportingFile;
import io.swagger.models.ModelImpl;
import io.swagger.models.properties.ArrayProperty;
@ -163,6 +166,46 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
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() {
return npmName;
}

View File

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

View File

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

View File

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

View File

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