forked from loafle/openapi-generator-original
add authentication to typescript-angular2 (#3781)
* add authentication to typescript-angular2 * updated typescript-angular2 petstore example * update typescript-angular2 with simplified conditionals
This commit is contained in:
+1
@@ -71,6 +71,7 @@ public class TypeScriptAngular2ClientCodegen extends AbstractTypeScriptClientCod
|
||||
supportingFiles.add(new SupportingFile("models.mustache", modelPackage().replace('.', File.separatorChar), "models.ts"));
|
||||
supportingFiles.add(new SupportingFile("apis.mustache", apiPackage().replace('.', File.separatorChar), "api.ts"));
|
||||
supportingFiles.add(new SupportingFile("index.mustache", getIndexDirectory(), "index.ts"));
|
||||
supportingFiles.add(new SupportingFile("configuration.mustache", getIndexDirectory(), "configuration.ts"));
|
||||
supportingFiles.add(new SupportingFile("variables.mustache", getIndexDirectory(), "variables.ts"));
|
||||
supportingFiles.add(new SupportingFile("gitignore", "", ".gitignore"));
|
||||
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
|
||||
|
||||
@@ -9,6 +9,7 @@ import 'rxjs/add/operator/map';
|
||||
|
||||
import * as models from '../model/models';
|
||||
import { BASE_PATH } from '../variables';
|
||||
import { Configuration } from '../configuration';
|
||||
|
||||
/* tslint:disable:no-unused-variable member-ordering */
|
||||
|
||||
@@ -23,11 +24,15 @@ import { BASE_PATH } from '../variables'
|
||||
export class {{classname}} {
|
||||
protected basePath = '{{basePath}}';
|
||||
public defaultHeaders: Headers = new Headers();
|
||||
public configuration: Configuration = new Configuration();
|
||||
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
|
||||
if (basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
if (configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
}
|
||||
|
||||
{{#operation}}
|
||||
@@ -96,7 +101,39 @@ export class {{classname}} {
|
||||
'{{{mediaType}}}'{{#hasMore}}, {{/hasMore}}
|
||||
{{/produces}}
|
||||
];
|
||||
|
||||
|
||||
{{#authMethods}}
|
||||
// authentication ({{name}}) required
|
||||
{{#isApiKey}}
|
||||
{{#isKeyInHeader}}
|
||||
if (this.configuration.apiKey)
|
||||
{
|
||||
headers.set('{{keyParamName}}', this.configuration.apiKey);
|
||||
}
|
||||
{{/isKeyInHeader}}
|
||||
{{#isKeyInQuery}}
|
||||
if (this.configuration.apiKey)
|
||||
{
|
||||
formParams.set('{{keyParamName}}', this.configuration.apiKey);
|
||||
}
|
||||
{{/isKeyInQuery}}
|
||||
{{/isApiKey}}
|
||||
{{#isBasic}}
|
||||
// http basic authentication required
|
||||
if (this.configuration.username || this.configuration.password)
|
||||
{
|
||||
headers.set('Authorization', 'Basic ' + btoa(this.configuration.username + ':' + this.configuration.password));
|
||||
}
|
||||
{{/isBasic}}
|
||||
{{#isOAuth}}
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
{{/isOAuth}}
|
||||
{{/authMethods}}
|
||||
|
||||
{{#hasFormParams}}
|
||||
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
||||
{{/hasFormParams}}
|
||||
@@ -107,7 +144,7 @@ export class {{classname}} {
|
||||
|
||||
{{#formParams}}
|
||||
if ({{paramName}} !== undefined) {
|
||||
formParams.set('{{baseName}}', <any>{{paramName}});
|
||||
formParams.set('{{baseName}}', <any>{{paramName}});
|
||||
}
|
||||
{{/formParams}}
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export class Configuration {
|
||||
apiKey: string;
|
||||
username: string;
|
||||
password: string;
|
||||
accessToken: string;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './api/api';
|
||||
export * from './model/models';
|
||||
export * from './variables';
|
||||
export * from './variables';
|
||||
export * from './configuration';
|
||||
@@ -32,6 +32,7 @@ import 'rxjs/add/operator/map';
|
||||
|
||||
import * as models from '../model/models';
|
||||
import { BASE_PATH } from '../variables';
|
||||
import { Configuration } from '../configuration';
|
||||
|
||||
/* tslint:disable:no-unused-variable member-ordering */
|
||||
|
||||
@@ -40,11 +41,15 @@ import { BASE_PATH } from '../variables'
|
||||
export class PetApi {
|
||||
protected basePath = 'http://petstore.swagger.io/v2';
|
||||
public defaultHeaders: Headers = new Headers();
|
||||
public configuration: Configuration = new Configuration();
|
||||
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
|
||||
if (basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
if (configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,7 +209,14 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
|
||||
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
@@ -246,7 +258,14 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -284,7 +303,14 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -322,7 +348,14 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -361,7 +394,19 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
// authentication (api_key) required
|
||||
if (this.configuration.apiKey)
|
||||
{
|
||||
headers.set('api_key', this.configuration.apiKey);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -398,7 +443,14 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
|
||||
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
@@ -444,15 +496,22 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
|
||||
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
||||
|
||||
|
||||
if (name !== undefined) {
|
||||
formParams.set('name', <any>name);
|
||||
formParams.set('name', <any>name);
|
||||
}
|
||||
if (status !== undefined) {
|
||||
formParams.set('status', <any>status);
|
||||
formParams.set('status', <any>status);
|
||||
}
|
||||
|
||||
let requestOptions: RequestOptionsArgs = new RequestOptions({
|
||||
@@ -496,15 +555,22 @@ export class PetApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (petstore_auth) required
|
||||
// oauth required
|
||||
if (this.configuration.accessToken)
|
||||
{
|
||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
||||
}
|
||||
|
||||
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
||||
|
||||
|
||||
if (additionalMetadata !== undefined) {
|
||||
formParams.set('additionalMetadata', <any>additionalMetadata);
|
||||
formParams.set('additionalMetadata', <any>additionalMetadata);
|
||||
}
|
||||
if (file !== undefined) {
|
||||
formParams.set('file', <any>file);
|
||||
formParams.set('file', <any>file);
|
||||
}
|
||||
|
||||
let requestOptions: RequestOptionsArgs = new RequestOptions({
|
||||
|
||||
@@ -32,6 +32,7 @@ import 'rxjs/add/operator/map';
|
||||
|
||||
import * as models from '../model/models';
|
||||
import { BASE_PATH } from '../variables';
|
||||
import { Configuration } from '../configuration';
|
||||
|
||||
/* tslint:disable:no-unused-variable member-ordering */
|
||||
|
||||
@@ -40,11 +41,15 @@ import { BASE_PATH } from '../variables'
|
||||
export class StoreApi {
|
||||
protected basePath = 'http://petstore.swagger.io/v2';
|
||||
public defaultHeaders: Headers = new Headers();
|
||||
public configuration: Configuration = new Configuration();
|
||||
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
|
||||
if (basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
if (configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -136,7 +141,8 @@ export class StoreApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -170,7 +176,13 @@ export class StoreApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
// authentication (api_key) required
|
||||
if (this.configuration.apiKey)
|
||||
{
|
||||
headers.set('api_key', this.configuration.apiKey);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -209,7 +221,8 @@ export class StoreApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -244,7 +257,8 @@ export class StoreApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
|
||||
@@ -32,6 +32,7 @@ import 'rxjs/add/operator/map';
|
||||
|
||||
import * as models from '../model/models';
|
||||
import { BASE_PATH } from '../variables';
|
||||
import { Configuration } from '../configuration';
|
||||
|
||||
/* tslint:disable:no-unused-variable member-ordering */
|
||||
|
||||
@@ -40,11 +41,15 @@ import { BASE_PATH } from '../variables'
|
||||
export class UserApi {
|
||||
protected basePath = 'http://petstore.swagger.io/v2';
|
||||
public defaultHeaders: Headers = new Headers();
|
||||
public configuration: Configuration = new Configuration();
|
||||
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string) {
|
||||
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
|
||||
if (basePath) {
|
||||
this.basePath = basePath;
|
||||
}
|
||||
if (configuration) {
|
||||
this.configuration = configuration;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -198,7 +203,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
@@ -235,7 +241,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
@@ -272,7 +279,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
@@ -313,7 +321,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -352,7 +361,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -394,7 +404,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -428,7 +439,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -468,7 +480,8 @@ export class UserApi {
|
||||
'application/json',
|
||||
'application/xml'
|
||||
];
|
||||
|
||||
|
||||
|
||||
|
||||
headers.set('Content-Type', 'application/json');
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
export class Configuration {
|
||||
apiKey: string;
|
||||
username: string;
|
||||
password: string;
|
||||
accessToken: string;
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
export * from './api/api';
|
||||
export * from './model/models';
|
||||
export * from './variables';
|
||||
export * from './variables';
|
||||
export * from './configuration';
|
||||
@@ -0,0 +1,3 @@
|
||||
import { OpaqueToken } from '@angular/core';
|
||||
|
||||
export const BASE_PATH = new OpaqueToken('basePath');
|
||||
Reference in New Issue
Block a user