Skip to content

Commit

Permalink
[Typescript/Fetch] Fix tslint issues in generated code (#4313)
Browse files Browse the repository at this point in the history
* [Typescript/Fetch] Fix tslint issues in generated code

* Add security generated files.

* Use tslint version that doesn't require typescript 2

* Run build scripts
  • Loading branch information
ChrisJamesC authored and wing328 committed Dec 8, 2016
1 parent a0c4b58 commit a3d3082
Show file tree
Hide file tree
Showing 16 changed files with 553 additions and 47 deletions.
Expand Up @@ -40,6 +40,7 @@ public void processOpts() {
supportingFiles.add(new SupportingFile("package.json.mustache", "", "package.json"));
supportingFiles.add(new SupportingFile("typings.json.mustache", "", "typings.json"));
supportingFiles.add(new SupportingFile("tsconfig.json.mustache", "", "tsconfig.json"));
supportingFiles.add(new SupportingFile("tslint.json.mustache", "", "tslint.json"));
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));

if(additionalProperties.containsKey(NPM_NAME)) {
Expand Down
Expand Up @@ -10,7 +10,7 @@ import * as assign from "core-js/library/fn/object/assign";
interface Dictionary<T> { [index: string]: T; }
export interface FetchAPI { (url: string, init?: any): Promise<any>; }

const BASE_PATH = "{{{basePath}}}".replace(/\/+$/, '');
const BASE_PATH = "{{{basePath}}}".replace(/\/+$/, "");

export interface FetchArgs {
url: string;
Expand Down Expand Up @@ -83,8 +83,10 @@ export const {{classname}}FetchParamCreactor = {
.replace(`{${"{{baseName}}"}}`, `${ params["{{paramName}}"] }`){{/pathParams}};
let urlObj = url.parse(baseUrl, true);
{{#hasQueryParams}}
urlObj.query = {{#supportsES6}}Object.{{/supportsES6}}assign({}, urlObj.query, { {{#queryParams}}
"{{baseName}}": params["{{paramName}}"],{{/queryParams}}
urlObj.query = {{#supportsES6}}Object.{{/supportsES6}}assign({}, urlObj.query, {
{{#queryParams}}
"{{baseName}}": params["{{paramName}}"],
{{/queryParams}}
});
{{/hasQueryParams}}
let fetchOptions: RequestInit = {{#supportsES6}}Object.{{/supportsES6}}assign({}, { method: "{{httpMethod}}" }, options);
Expand All @@ -103,8 +105,8 @@ export const {{classname}}FetchParamCreactor = {
}{{/bodyParam}}
{{/hasBodyParam}}
{{#hasHeaderParams}}
fetchOptions.headers = {{#supportsES6}}Object.{{/supportsES6}}assign({ {{#headerParams}}
"{{baseName}}": params["{{paramName}}"],{{/headerParams}}
fetchOptions.headers = {{#supportsES6}}Object.{{/supportsES6}}assign({
{{#headerParams}}"{{baseName}}": params["{{paramName}}"],{{/headerParams}}
}, contentTypeHeader);
{{/hasHeaderParams}}
{{^hasHeaderParams}}
Expand Down
Expand Up @@ -10,9 +10,11 @@
{{/supportsES6}}"isomorphic-fetch": "^2.2.1"
},
"scripts" : {
"prepublish" : "typings install && tsc"
"prepublish" : "typings install && tsc",
"test": "tslint api.ts"
},
"devDependencies": {
"tslint": "^3.15.1",
"typescript": "^1.8.10",
"typings": "^1.0.4"
}
Expand Down
@@ -0,0 +1,101 @@
{
"jsRules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
],
"no-duplicate-variable": true,
"no-eval": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"double"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
},
"rules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
],
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"no-var-keyword": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"double"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}
30 changes: 9 additions & 21 deletions samples/client/petstore-security-test/typescript-fetch/api.ts
Expand Up @@ -8,18 +8,6 @@
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as querystring from "querystring";
Expand All @@ -31,7 +19,7 @@ import * as assign from "core-js/library/fn/object/assign";
interface Dictionary<T> { [index: string]: T; }
export interface FetchAPI { (url: string, init?: any): Promise<any>; }

const BASE_PATH = "https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r".replace(/\/+$/, '');
const BASE_PATH = "https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r".replace(/\/+$/, "");

export interface FetchArgs {
url: string;
Expand All @@ -46,7 +34,7 @@ export class BaseAPI {
this.basePath = basePath;
this.fetch = fetch;
}
}
};

/**
* Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r
Expand All @@ -68,15 +56,15 @@ export const FakeApiFetchParamCreactor = {
* To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
* @param test code inject * &#39; &quot; &#x3D;end rn n r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*/
testCodeInjectEndRnNR(params: { test code inject * &#39; &quot; &#x3D;end rn n r?: string; }, options?: any): FetchArgs {
testCodeInjectEndRnNR(params: { "test code inject * &#39; &quot; &#x3D;end rn n r"?: string; }, options?: any): FetchArgs {
const baseUrl = `/fake`;
let urlObj = url.parse(baseUrl, true);
let fetchOptions: RequestInit = assign({}, { method: "PUT" }, options);

let contentTypeHeader: Dictionary<string>;
contentTypeHeader = { "Content-Type": "application/x-www-form-urlencoded" };
fetchOptions.body = querystring.stringify({
"test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r": params.test code inject * &#39; &quot; &#x3D;end rn n r,
"test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r": params["test code inject * &#39; &quot; &#x3D;end rn n r"],
});
if (contentTypeHeader) {
fetchOptions.headers = contentTypeHeader;
Expand All @@ -86,7 +74,7 @@ export const FakeApiFetchParamCreactor = {
options: fetchOptions,
};
},
}
};

/**
* FakeApi - functional programming interface
Expand All @@ -96,7 +84,7 @@ export const FakeApiFp = {
* To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
* @param test code inject * &#39; &quot; &#x3D;end rn n r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*/
testCodeInjectEndRnNR(params: { test code inject * &#39; &quot; &#x3D;end rn n r?: string; }, options?: any): (fetch: FetchAPI, basePath?: string) => Promise<any> {
testCodeInjectEndRnNR(params: { "test code inject * &#39; &quot; &#x3D;end rn n r"?: string; }, options?: any): (fetch: FetchAPI, basePath?: string) => Promise<any> {
const fetchArgs = FakeApiFetchParamCreactor.testCodeInjectEndRnNR(params, options);
return (fetch: FetchAPI = isomorphicFetch, basePath: string = BASE_PATH) => {
return fetch(basePath + fetchArgs.url, fetchArgs.options).then((response) => {
Expand All @@ -118,7 +106,7 @@ export class FakeApi extends BaseAPI {
* To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
* @param test code inject * &#39; &quot; &#x3D;end rn n r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*/
testCodeInjectEndRnNR(params: { test code inject * &#39; &quot; &#x3D;end rn n r?: string; }, options?: any) {
testCodeInjectEndRnNR(params: { "test code inject * &#39; &quot; &#x3D;end rn n r"?: string; }, options?: any) {
return FakeApiFp.testCodeInjectEndRnNR(params, options)(this.fetch, this.basePath);
}
};
Expand All @@ -132,9 +120,9 @@ export const FakeApiFactory = function (fetch?: FetchAPI, basePath?: string) {
* To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
* @param test code inject * &#39; &quot; &#x3D;end rn n r To test code injection *_/ &#39; \&quot; &#x3D;end -- \\r\\n \\n \\r
*/
testCodeInjectEndRnNR(params: { test code inject * &#39; &quot; &#x3D;end rn n r?: string; }, options?: any) {
testCodeInjectEndRnNR(params: { "test code inject * &#39; &quot; &#x3D;end rn n r"?: string; }, options?: any) {
return FakeApiFp.testCodeInjectEndRnNR(params, options)(fetch, basePath);
},
}
};
};

@@ -1,7 +1,7 @@
{
"name": "typescript-fetch-api",
"version": "0.0.0",
"license": "Apache-2.0",
"license": "Unlicense",
"main": "./dist/api.js",
"browser": "./dist/api.js",
"typings": "./dist/api.d.ts",
Expand All @@ -10,9 +10,11 @@
"isomorphic-fetch": "^2.2.1"
},
"scripts" : {
"prepublish" : "typings install && tsc"
"prepublish" : "typings install && tsc",
"test": "tslint api.ts"
},
"devDependencies": {
"tslint": "^3.15.1",
"typescript": "^1.8.10",
"typings": "^1.0.4"
}
Expand Down
101 changes: 101 additions & 0 deletions samples/client/petstore-security-test/typescript-fetch/tslint.json
@@ -0,0 +1,101 @@
{
"jsRules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
],
"no-duplicate-variable": true,
"no-eval": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"double"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
},
"rules": {
"class-name": true,
"comment-format": [
true,
"check-space"
],
"indent": [
true,
"spaces"
],
"no-eval": true,
"no-internal-module": true,
"no-trailing-whitespace": true,
"no-unsafe-finally": true,
"no-var-keyword": true,
"one-line": [
true,
"check-open-brace",
"check-whitespace"
],
"quotemark": [
true,
"double"
],
"semicolon": [
true,
"always"
],
"triple-equals": [
true,
"allow-null-check"
],
"typedef-whitespace": [
true,
{
"call-signature": "nospace",
"index-signature": "nospace",
"parameter": "nospace",
"property-declaration": "nospace",
"variable-declaration": "nospace"
}
],
"variable-name": [
true,
"ban-keywords"
],
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator",
"check-type"
]
}
}

0 comments on commit a3d3082

Please sign in to comment.