forked from loafle/openapi-generator-original
[typescript-angular2] access token function (#4361)
* allow function so access token can be derived for each api call * update tests * update type for accessToken to be string or function that returns string
This commit is contained in:
parent
77b92d7d11
commit
0b5a6f25da
@ -143,7 +143,10 @@ export class {{classname}} {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
{{/isOAuth}}
|
{{/isOAuth}}
|
||||||
{{/authMethods}}
|
{{/authMethods}}
|
||||||
|
@ -2,5 +2,5 @@ export class Configuration {
|
|||||||
apiKey: string;
|
apiKey: string;
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
accessToken: string;
|
accessToken: string | () => string;
|
||||||
}
|
}
|
@ -1,72 +1,67 @@
|
|||||||
/**
|
/**
|
||||||
* Swagger Petstore *_/ ' \" =end \\r\\n \\n \\r
|
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end
|
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
|
||||||
*
|
*
|
||||||
* OpenAPI spec version: 1.0.0 *_/ ' \" =end \\r\\n \\n \\r
|
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
* Contact: apiteam@swagger.io *_/ ' \" =end \\r\\n \\n \\r
|
* Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
*
|
*
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
* NOTE: This class is auto generated by the swagger code generator program.
|
||||||
* https://github.com/swagger-api/swagger-codegen.git
|
* https://github.com/swagger-api/swagger-codegen.git
|
||||||
* Do not edit the class manually.
|
* 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 {Http, Headers, RequestOptionsArgs, Response, URLSearchParams} from '@angular/http';
|
import { Inject, Injectable, Optional } from '@angular/core';
|
||||||
import {Injectable, Optional} from '@angular/core';
|
import { Http, Headers, URLSearchParams } from '@angular/http';
|
||||||
import {Observable} from 'rxjs/Observable';
|
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
|
||||||
import * as models from '../model/models';
|
import { Response, ResponseContentType } from '@angular/http';
|
||||||
import 'rxjs/Rx';
|
|
||||||
|
import { Observable } from 'rxjs/Observable';
|
||||||
|
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 */
|
/* tslint:disable:no-unused-variable member-ordering */
|
||||||
|
|
||||||
'use strict';
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class FakeApi {
|
export class FakeApi {
|
||||||
protected basePath = 'https://petstore.swagger.io *_/ ' \" =end \\r\\n \\n \\r/v2 *_/ ' \" =end \\r\\n \\n \\r';
|
protected basePath = 'https://petstore.swagger.io *_/ ' \" =end -- \\r\\n \\n \\r/v2 *_/ ' \" =end -- \\r\\n \\n \\r';
|
||||||
public defaultHeaders : Headers = new Headers();
|
public defaultHeaders: Headers = new Headers();
|
||||||
|
public configuration: Configuration = new Configuration();
|
||||||
|
|
||||||
constructor(protected http: Http, @Optional() basePath: string) {
|
constructor(protected http: Http, @Optional()@Inject(BASE_PATH) basePath: string, @Optional() configuration: Configuration) {
|
||||||
if (basePath) {
|
if (basePath) {
|
||||||
this.basePath = basePath;
|
this.basePath = basePath;
|
||||||
}
|
}
|
||||||
|
if (configuration) {
|
||||||
|
this.configuration = configuration;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* To test code injection *_/ ' \" =end \\r\\n \\n \\r
|
|
||||||
*
|
*
|
||||||
* @param test code inject * ' " =end rn n r To test code injection *_/ ' \" =end \\r\\n \\n \\r
|
* Extends object by coping non-existing properties.
|
||||||
|
* @param objA object to be extended
|
||||||
|
* @param objB source object
|
||||||
*/
|
*/
|
||||||
public testCodeInjectEndRnNR (test code inject * ' " =end rn n r?: string, extraHttpRequestParams?: any ) : Observable<{}> {
|
private extendObj<T1,T2>(objA: T1, objB: T2) {
|
||||||
const path = this.basePath + '/fake';
|
for(let key in objB){
|
||||||
|
if(objB.hasOwnProperty(key)){
|
||||||
|
(objA as any)[key] = (objB as any)[key];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return <T1&T2>objA;
|
||||||
|
}
|
||||||
|
|
||||||
let queryParameters = new URLSearchParams();
|
/**
|
||||||
let headerParams = this.defaultHeaders;
|
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
let formParams = new URLSearchParams();
|
*
|
||||||
|
* @param test code inject * ' " =end rn n r To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
headerParams.set('Content-Type', 'application/x-www-form-urlencoded');
|
*/
|
||||||
|
public testCodeInjectEndRnNR(test code inject * ' " =end rn n r?: string, extraHttpRequestParams?: any): Observable<{}> {
|
||||||
formParams['test code inject */ ' " =end \r\n \n \r'] = test code inject * ' " =end rn n r;
|
return this.testCodeInjectEndRnNRWithHttpInfo(test code inject * ' " =end rn n r, extraHttpRequestParams)
|
||||||
|
|
||||||
let requestOptions: RequestOptionsArgs = {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: headerParams,
|
|
||||||
search: queryParameters
|
|
||||||
};
|
|
||||||
requestOptions.body = formParams.toString();
|
|
||||||
|
|
||||||
return this.http.request(path, requestOptions)
|
|
||||||
.map((response: Response) => {
|
.map((response: Response) => {
|
||||||
if (response.status === 204) {
|
if (response.status === 204) {
|
||||||
return undefined;
|
return undefined;
|
||||||
@ -76,4 +71,54 @@ export class FakeApi {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
|
*
|
||||||
|
* @param test code inject * ' " =end rn n r To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
|
*/
|
||||||
|
public testCodeInjectEndRnNRWithHttpInfo(test code inject * ' " =end rn n r?: string, extraHttpRequestParams?: any): Observable<Response> {
|
||||||
|
const path = this.basePath + `/fake`;
|
||||||
|
|
||||||
|
let queryParameters = new URLSearchParams();
|
||||||
|
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
|
||||||
|
let formParams = new URLSearchParams();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// to determine the Content-Type header
|
||||||
|
let consumes: string[] = [
|
||||||
|
'application/json',
|
||||||
|
'*_/ =end -- '
|
||||||
|
];
|
||||||
|
|
||||||
|
// to determine the Accept header
|
||||||
|
let produces: string[] = [
|
||||||
|
'application/json',
|
||||||
|
'*_/ =end -- '
|
||||||
|
];
|
||||||
|
|
||||||
|
|
||||||
|
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
||||||
|
|
||||||
|
|
||||||
|
if (test code inject * ' " =end rn n r !== undefined) {
|
||||||
|
formParams.set('test code inject */ ' " =end -- \r\n \n \r', <any>test code inject * ' " =end rn n r);
|
||||||
|
}
|
||||||
|
|
||||||
|
let requestOptions: RequestOptionsArgs = new RequestOptions({
|
||||||
|
method: RequestMethod.Put,
|
||||||
|
headers: headers,
|
||||||
|
body: formParams.toString(),
|
||||||
|
search: queryParameters
|
||||||
|
});
|
||||||
|
|
||||||
|
// https://github.com/swagger-api/swagger-codegen/issues/4037
|
||||||
|
if (extraHttpRequestParams) {
|
||||||
|
requestOptions = this.extendObj(requestOptions, extraHttpRequestParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.http.request(path, requestOptions);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,6 @@
|
|||||||
|
export class Configuration {
|
||||||
|
apiKey: string;
|
||||||
|
username: string;
|
||||||
|
password: string;
|
||||||
|
accessToken: string | () => string;
|
||||||
|
}
|
@ -1,2 +1,4 @@
|
|||||||
export * from './api/api';
|
export * from './api/api';
|
||||||
export * from './model/models';
|
export * from './model/models';
|
||||||
|
export * from './variables';
|
||||||
|
export * from './configuration';
|
@ -1,38 +1,24 @@
|
|||||||
/**
|
/**
|
||||||
* Swagger Petstore *_/ ' \" =end \\r\\n \\n \\r
|
* Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end
|
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end --
|
||||||
*
|
*
|
||||||
* OpenAPI spec version: 1.0.0 *_/ ' \" =end \\r\\n \\n \\r
|
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
* Contact: apiteam@swagger.io *_/ ' \" =end \\r\\n \\n \\r
|
* Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
*
|
*
|
||||||
* NOTE: This class is auto generated by the swagger code generator program.
|
* NOTE: This class is auto generated by the swagger code generator program.
|
||||||
* https://github.com/swagger-api/swagger-codegen.git
|
* https://github.com/swagger-api/swagger-codegen.git
|
||||||
* Do not edit the class manually.
|
* 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.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
'use strict';
|
|
||||||
import * as models from './models';
|
import * as models from './models';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Model for testing reserved words *_/ ' \" =end \\r\\n \\n \\r
|
* Model for testing reserved words *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
*/
|
*/
|
||||||
export interface ModelReturn {
|
export interface ModelReturn {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* property description *_/ ' \" =end \\r\\n \\n \\r
|
* property description *_/ ' \" =end -- \\r\\n \\n \\r
|
||||||
*/
|
*/
|
||||||
return?: number;
|
return?: number;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
import { OpaqueToken } from '@angular/core';
|
||||||
|
|
||||||
|
export const BASE_PATH = new OpaqueToken('basePath');
|
@ -217,7 +217,10 @@ export class PetApi {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -271,7 +274,10 @@ export class PetApi {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -320,7 +326,10 @@ export class PetApi {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -369,7 +378,10 @@ export class PetApi {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -415,16 +427,19 @@ export class PetApi {
|
|||||||
'application/xml'
|
'application/xml'
|
||||||
];
|
];
|
||||||
|
|
||||||
// authentication (api_key) required
|
|
||||||
if (this.configuration.apiKey)
|
|
||||||
{
|
|
||||||
headers.set('api_key', this.configuration.apiKey);
|
|
||||||
}
|
|
||||||
// authentication (petstore_auth) required
|
// authentication (petstore_auth) required
|
||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
|
}
|
||||||
|
// authentication (api_key) required
|
||||||
|
if (this.configuration.apiKey)
|
||||||
|
{
|
||||||
|
headers.set('api_key', this.configuration.apiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -472,7 +487,10 @@ export class PetApi {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -529,7 +547,10 @@ export class PetApi {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
||||||
@ -592,7 +613,10 @@ export class PetApi {
|
|||||||
// oauth required
|
// oauth required
|
||||||
if (this.configuration.accessToken)
|
if (this.configuration.accessToken)
|
||||||
{
|
{
|
||||||
headers.set('Authorization', 'Bearer ' + this.configuration.accessToken);
|
let accessToken = typeof this.configuration.accessToken === 'function'
|
||||||
|
? this.configuration.accessToken()
|
||||||
|
: this.configuration.accessToken;
|
||||||
|
headers.set('Authorization', 'Bearer ' + accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
headers.set('Content-Type', 'application/x-www-form-urlencoded');
|
||||||
|
@ -2,5 +2,5 @@ export class Configuration {
|
|||||||
apiKey: string;
|
apiKey: string;
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
accessToken: string;
|
accessToken: string | () => string;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user