From 4337f050f56c5e1e8c0c147a14e3f8f938dbf84d Mon Sep 17 00:00:00 2001 From: Sebastian Haas Date: Sun, 28 Aug 2016 12:30:11 +0200 Subject: [PATCH] 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 --- .../TypeScriptAngular2ClientCodegen.java | 43 +++++++++++ .../typescript-angular2/api.mustache | 76 ++++++++++++------- .../typescript-angular2/model.mustache | 1 - .../typescript-angular2/package.mustache | 38 ++++++---- .../typescript-angular2/typings.mustache | 6 +- 5 files changed, 117 insertions(+), 47 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java index b8a9f772ad1..1020375356a 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/TypeScriptAngular2ClientCodegen.java @@ -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 postProcessOperations(Map operations) { + Map objs = (Map) operations.get("operations"); + List ops = (List) 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; } diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache index 8565dee4c5a..aadf38be181 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/api.mustache @@ -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) => { diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache index 4e7a0d87a48..09320aa89d3 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/model.mustache @@ -1,7 +1,6 @@ {{>licenseInfo}} {{#models}} {{#model}} -'use strict'; import * as models from './models'; {{#description}} diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache index 70cb98cce7e..6527831e62d 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/package.mustache @@ -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}}" } diff --git a/modules/swagger-codegen/src/main/resources/typescript-angular2/typings.mustache b/modules/swagger-codegen/src/main/resources/typescript-angular2/typings.mustache index 0848dcffe31..507c40e5cbe 100644 --- a/modules/swagger-codegen/src/main/resources/typescript-angular2/typings.mustache +++ b/modules/swagger-codegen/src/main/resources/typescript-angular2/typings.mustache @@ -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" } -} \ No newline at end of file +}