diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java index b9913272c08..20bb097eaea 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/TypeScriptClientCodegen.java @@ -123,7 +123,8 @@ public class TypeScriptClientCodegen extends DefaultCodegen implements CodegenCo supportingFiles.add(new SupportingFile("configuration.mustache", "", "configuration.ts")); supportingFiles.add(new SupportingFile("middleware.mustache", "", "middleware.ts")); - + supportingFiles.add(new SupportingFile("auth" + File.separator + "auth.mustache", "auth", "auth.ts")); + // models this.modelPackage = ""; this.modelTemplateFiles.put("models/models.mustache", ".ts"); diff --git a/modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache b/modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache new file mode 100644 index 00000000000..7d88980180e --- /dev/null +++ b/modules/openapi-generator/src/main/resources/typescript/auth/auth.mustache @@ -0,0 +1,62 @@ +import {RequestContext} from '../http/http'; +// typings for btoa are incorrect +//@ts-ignore +import * as btoa from "btoa"; + +export abstract class SecurityAuthentication { + + public constructor(private name: string) { + + } + + /* + * + * @return returns the name of the security authentication as specified in OAI + */ + public getName(): string { + return this.name; + } + + public abstract applySecurityAuthentication(context: RequestContext): void; + +} + +export class NoAuthentication extends SecurityAuthentication { + + public constructor() { + super("_no_auth"); + } + + public applySecurityAuthentication(_context: RequestContext) { + + } +} + +export class APIKeyAuthentication extends SecurityAuthentication { + + public constructor(authName: string, private paramName: string, private apiKey: string, private keyLocation: "query" | "header" | "cookie") { + super(authName); + } + + public applySecurityAuthentication(context: RequestContext) { + if (this.keyLocation === "header") { + context.setHeaderParam(this.paramName, this.apiKey); + } else if (this.keyLocation === "cookie") { + context.addCookie(this.paramName, this.apiKey); + } else if (this.keyLocation === "query") { + context.setQueryParam(this.paramName, this.apiKey); + } + } +} + +export class HttpBasicAuthentication extends SecurityAuthentication { + + public constructor(authName: string, private username: string, private password: string) { + super(authName); + } + + public applySecurityAuthentication(context: RequestContext) { + let comb = this.username + ":" + this.password; + context.setHeaderParam("Authentication", "Basic " + btoa(comb)); + } +} \ No newline at end of file diff --git a/modules/openapi-generator/src/main/resources/typescript/http/http.mustache b/modules/openapi-generator/src/main/resources/typescript/http/http.mustache index 4bd5bdcfefb..e23d864203c 100644 --- a/modules/openapi-generator/src/main/resources/typescript/http/http.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/http/http.mustache @@ -1,5 +1,8 @@ // TODO: evaluate if we can easily get rid of this library import * as FormData from "form-data"; +// typings of url-parse are incorrect... +// @ts-ignore +import * as URLParse from "url-parse"; export enum HttpMethod { GET = "GET", @@ -28,18 +31,20 @@ export class HttpException extends Error { export class RequestContext { private headers: { [key: string]: string } = {}; private body: string | FormData = ""; - - public constructor(private url: string, private httpMethod: HttpMethod) { - + private url: URLParse; + + public constructor(url: string, private httpMethod: HttpMethod) { + this.url = URLParse(url, true); } public getUrl(): string { - return this.url; + return this.url.toString(); } public setUrl(url: string) { - this.url = url; + this.url = URLParse(url, true); } + public setBody(body: string | FormData) { // HTTP-Spec 1.1 Section 4.3 @@ -66,6 +71,12 @@ export class RequestContext { return this.body; } + public setQueryParam(name: string, value: string) { + let queryObj = this.url.query; + queryObj[name] = value; + this.url.set("query", queryObj); + } + public addCookie(name: string, value: string): void { if (!this.headers["Cookie"]) { this.headers["Cookie"] = ""; @@ -73,7 +84,7 @@ export class RequestContext { this.headers["Cookie"] += name + "=" + value + "; "; } - public setHeader(key: string, value: string): void { + public setHeaderParam(key: string, value: string): void { this.headers[key] = value; } } diff --git a/modules/openapi-generator/src/main/resources/typescript/package.mustache b/modules/openapi-generator/src/main/resources/typescript/package.mustache index cb3ab771c69..08be2ff1f3f 100644 --- a/modules/openapi-generator/src/main/resources/typescript/package.mustache +++ b/modules/openapi-generator/src/main/resources/typescript/package.mustache @@ -20,9 +20,11 @@ "dependencies": { "es6-promise": "^4.2.4", "isomorphic-fetch": "^2.2.1", - "@types/isomorphic-fetch": "0.0.34" + "btoa": "^1.2.1", + "@types/isomorphic-fetch": "0.0.34", "form-data": "^2.3.2", "@types/form-data": "^2.2.1", + "url-parse": "^1.4.3" }, "devDependencies": { "ts-node": "^7.0.0",