Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,25 @@ import { Configuration } from '../configurat
import { {{classname}}Interface } from './{{classFilename}}Interface';
{{/withInterfaces}}

{{#useHttpClient}}
import { HttpClient, HttpHeaders, HttpParams,
HttpResponse, HttpEvent } from '@angular/common/http';
import { CustomHttpUrlEncodingCodec } from '../encoder';
{{/useHttpClient}}
{{^useHttpClient}}
import { Http, Headers, URLSearchParams } from '@angular/http';
import { RequestMethod, RequestOptions, RequestOptionsArgs } from '@angular/http';
import { Response, ResponseContentType } from '@angular/http';
import { CustomQueryEncoderHelper } from '../encoder';
{{/useHttpClient}}


type FormParams = URLSearchParams | FormData{{#useHttpClient}} | HttpParams{{/useHttpClient}};

function append(formParams: FormParams, param: string, value: any) {
return {{#useHttpClient}}formParams.append(param, value){{/useHttpClient}}{{^useHttpClient}}formParams.append(param, value), formParams{{/useHttpClient}}
};

{{#operations}}

{{#description}}
Expand Down Expand Up @@ -262,7 +281,7 @@ export class {{classname}} {
{{#hasFormParams}}
const canConsumeForm = this.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): void; };
let formParams: FormParams;
Copy link
Copy Markdown

@emmanuelgautier emmanuelgautier Feb 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

formParams could be a HttpParams type and need to not use the return as the same way.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emmanuelgautier FormParams type is defined here as a union type that includes HttpParams, so I think that would work. What am I missing?
What do you mean by "and need not use the return as the same way"?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FormData and HttpParams append function does not have the same signature :

One return FormParams and the other not. When you use formParams.append, you have to check what is the exact variable type. Here, you do not make this check and could be a potential error when formParams is a HttpParams and you use HttpClient

It is almost the same thing that I done in this lines.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emmanuelgautier: They differ on the return type, that is the reason why in my method, I implicitly check the type (by checking the useReturnValue ) -
If it's HttpParams I return the return value of append(), if it isn't, I append, and return the same instance (see the comma , there).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, if you use HttpClient, you do not use useReturnValue but formParams could be a HttpParams and return value.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could formParams be HttpParams? the only line that assigns an HttpParams instance to formParam is this one, which is then always followed by a new assignment to the appender here

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. My bad 👍

let useForm = false;
let convertFormParamsToString = false;
{{#formParams}}
Expand All @@ -286,23 +305,22 @@ export class {{classname}} {
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
{{/useHttpClient}}
}

{{#formParams}}
{{#isListContainer}}
if ({{paramName}}) {
{{#isCollectionFormatMulti}}
{{paramName}}.forEach((element) => {
{{#useHttpClient}}formParams = {{/useHttpClient}}formParams.append('{{baseName}}', <any>element){{#useHttpClient}} || formParams{{/useHttpClient}};
{{#useHttpClient}}formParams = {{/useHttpClient}}append(formParams, '{{baseName}}', <any>element);
})
{{/isCollectionFormatMulti}}
{{^isCollectionFormatMulti}}
{{#useHttpClient}}formParams = {{/useHttpClient}}formParams.append('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}'])){{#useHttpClient}} || formParams{{/useHttpClient}};
{{#useHttpClient}}formParams = {{/useHttpClient}}append(formParams, '{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
{{/isCollectionFormatMulti}}
}
{{/isListContainer}}
{{^isListContainer}}
if ({{paramName}} !== undefined) {
{{#useHttpClient}}formParams = {{/useHttpClient}}formParams.append('{{baseName}}', <any>{{paramName}}){{#useHttpClient}} || formParams{{/useHttpClient}};
{{#useHttpClient}}formParams = {{/useHttpClient}}append(formParams, '{{baseName}}', <any>{{paramName}});
}
{{/isListContainer}}
{{/formParams}}
Expand Down Expand Up @@ -353,4 +371,4 @@ export class {{classname}} {
}

{{/operation}}}
{{/operations}}
{{/operations}}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.0-SNAPSHOT
2.4.3-SNAPSHOT
15 changes: 11 additions & 4 deletions samples/client/petstore-security-test/typescript-angular/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Building

To build an compile the typescript sources to javascript use:
To install the required dependencies and to build the typescript sources run:
```
npm install
npm run build
Expand All @@ -14,15 +14,15 @@ First build the package than run ```npm publish```

### consuming

navigate to the folder of your consuming project and run one of next commando's.
Navigate to the folder of your consuming project and run one of next commands.

_published:_

```
npm install @ --save
```

_unPublished (not recommended):_
_without publishing (not recommended):_

```
npm install PATH_TO_GENERATED_PACKAGE --save
Expand All @@ -37,9 +37,16 @@ npm link

In your project:
```
npm link @
npm link
```

__Note for Windows users:__ The Angular CLI has troubles to use linked npm packages.
Please refer to this issue https://github.com/angular/angular-cli/issues/8284 for a solution / workaround.
Published packages are not effected by this issue.


#### General usage

In your Angular project:


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class ApiModule {
return {
ngModule: ApiModule,
providers: [ { provide: Configuration, useFactory: configurationFactory } ]
}
};
}

constructor( @Optional() @SkipSelf() parentModule: ApiModule,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ import { Observable } from 'rxjs/Observab
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';

import { HttpClient, HttpHeaders, HttpParams,
HttpResponse, HttpEvent } from '@angular/common/http';
import { CustomHttpUrlEncodingCodec } from '../encoder';


type FormParams = URLSearchParams | FormData | HttpParams;

function append(formParams: FormParams, param: string, value: any) {
return formParams.append(param, value)
};


@Injectable()
export class FakeService {
Expand All @@ -46,7 +57,7 @@ export class FakeService {
*/
private canConsumeForm(consumes: string[]): boolean {
const form = 'multipart/form-data';
for (let consume of consumes) {
for (const consume of consumes) {
if (form === consume) {
return true;
}
Expand All @@ -67,37 +78,37 @@ export class FakeService {
public testCodeInjectEndRnNR(testCodeInjectEndRnNR?: string, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<any>>;
public testCodeInjectEndRnNR(testCodeInjectEndRnNR?: string, observe: any = 'body', reportProgress: boolean = false ): Observable<any> {


let headers = this.defaultHeaders;

// to determine the Accept header
let httpHeaderAccepts: string[] = [
'application/json',
'*_/ =end -- '
];
let httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAcceptSelected != undefined) {
headers = headers.set("Accept", httpHeaderAcceptSelected);
headers = headers.set('Accept', httpHeaderAcceptSelected);
}

// to determine the Content-Type header
let consumes: string[] = [
const consumes: string[] = [
'application/json',
'*_/ =end -- '
];

const canConsumeForm = this.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): void; };
let formParams: FormParams;
let useForm = false;
let convertFormParamsToString = false;
if (useForm) {
formParams = new FormData();
} else {
formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
}

if (testCodeInjectEndRnNR !== undefined) {
formParams = formParams.append('test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r', <any>testCodeInjectEndRnNR) || formParams;
formParams = append(formParams, 'test code inject */ &#39; &quot; &#x3D;end -- \r\n \n \r', <any>testCodeInjectEndRnNR);
}

return this.httpClient.put<any>(`${this.basePath}/fake`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export class Configuration {
* Select the correct content-type to use for a request.
* Uses {@link Configuration#isJsonMime} to determine the correct content-type.
* If no content type is found return the first found type if the contentTypes is not empty
* @param {string[]} contentTypes - the array of content types that are available for selection
* @returns {string} the selected content-type or <code>undefined</code> if no selection could be made.
* @param contentTypes - the array of content types that are available for selection
* @returns the selected content-type or <code>undefined</code> if no selection could be made.
*/
public selectHeaderContentType (contentTypes: string[]): string | undefined {
if (contentTypes.length == 0) {
Expand All @@ -47,8 +47,8 @@ export class Configuration {
* Select the correct accept content-type to use for a request.
* Uses {@link Configuration#isJsonMime} to determine the correct accept content-type.
* If no content type is found return the first found type if the contentTypes is not empty
* @param {string[]} accepts - the array of content types that are available for selection.
* @returns {string} the selected content-type or <code>undefined</code> if no selection could be made.
* @param accepts - the array of content types that are available for selection.
* @returns the selected content-type or <code>undefined</code> if no selection could be made.
*/
public selectHeaderAccept(accepts: string[]): string | undefined {
if (accepts.length == 0) {
Expand All @@ -69,8 +69,8 @@ export class Configuration {
* application/json; charset=UTF8
* APPLICATION/JSON
* application/vnd.company+json
* @param {string} mime - MIME (Multipurpose Internet Mail Extensions)
* @return {boolean} True if the given MIME is JSON, false otherwise.
* @param mime - MIME (Multipurpose Internet Mail Extensions)
* @return True if the given MIME is JSON, false otherwise.
*/
public isJsonMime(mime: string): boolean {
const jsonMime: RegExp = new RegExp('^(application\/json|[^;/ \t]+\/[^;/ \t]+[+]json)[ \t]*(;.*)?$', 'i');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import { Pet } from '../model/pet';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';

type FormParams = HttpParams | URLSearchParams | FormData;

function getAppender(useReturnValue = false) {
return (formParams: FormParams, param: string, value: any) =>
useReturnValue ? formParams.append(param, value) : formParams.append(param, value), formParams;
}

@Injectable()
export class PetService {
Expand Down Expand Up @@ -557,7 +563,8 @@ export class PetService {

const canConsumeForm = this.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): void; };
let formParams: FormParams;
let append = getAppender();
let useForm = false;
let convertFormParamsToString = false;
if (useForm) {
Expand All @@ -569,12 +576,11 @@ export class PetService {
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}

if (name !== undefined) {
formParams.append('name', <any>name);
append(formParams, 'name', <any>name);
}
if (status !== undefined) {
formParams.append('status', <any>status);
append(formParams, 'status', <any>status);
}

let requestOptions: RequestOptionsArgs = new RequestOptions({
Expand Down Expand Up @@ -633,7 +639,8 @@ export class PetService {

const canConsumeForm = this.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): void; };
let formParams: FormParams;
let append = getAppender();
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
Expand All @@ -648,12 +655,11 @@ export class PetService {
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}

if (additionalMetadata !== undefined) {
formParams.append('additionalMetadata', <any>additionalMetadata);
append(formParams, 'additionalMetadata', <any>additionalMetadata);
}
if (file !== undefined) {
formParams.append('file', <any>file);
append(formParams, 'file', <any>file);
}

let requestOptions: RequestOptionsArgs = new RequestOptions({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import { Order } from '../model/order';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';

type FormParams = HttpParams | URLSearchParams | FormData;

function getAppender(useReturnValue = false) {
return (formParams: FormParams, param: string, value: any) =>
useReturnValue ? formParams.append(param, value) : formParams.append(param, value), formParams;
}

@Injectable()
export class StoreService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import { User } from '../model/user';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';

type FormParams = HttpParams | URLSearchParams | FormData;

function getAppender(useReturnValue = false) {
return (formParams: FormParams, param: string, value: any) =>
useReturnValue ? formParams.append(param, value) : formParams.append(param, value), formParams;
}

@Injectable()
export class UserService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import { Pet } from '../model/pet';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';

type FormParams = HttpParams | URLSearchParams | FormData;

function getAppender(useReturnValue = false) {
return (formParams: FormParams, param: string, value: any) =>
useReturnValue ? formParams.append(param, value) : formParams.append(param, value), formParams;
}

@Injectable()
export class PetService {
Expand Down Expand Up @@ -557,7 +563,8 @@ export class PetService {

const canConsumeForm = this.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): void; };
let formParams: FormParams;
let append = getAppender();
let useForm = false;
let convertFormParamsToString = false;
if (useForm) {
Expand All @@ -569,12 +576,11 @@ export class PetService {
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}

if (name !== undefined) {
formParams.append('name', <any>name);
append(formParams, 'name', <any>name);
}
if (status !== undefined) {
formParams.append('status', <any>status);
append(formParams, 'status', <any>status);
}

let requestOptions: RequestOptionsArgs = new RequestOptions({
Expand Down Expand Up @@ -633,7 +639,8 @@ export class PetService {

const canConsumeForm = this.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): void; };
let formParams: FormParams;
let append = getAppender();
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
Expand All @@ -648,12 +655,11 @@ export class PetService {
// set the content-type explicitly to avoid having it set to 'text/plain'
headers.set('Content-Type', 'application/x-www-form-urlencoded;charset=UTF-8');
}

if (additionalMetadata !== undefined) {
formParams.append('additionalMetadata', <any>additionalMetadata);
append(formParams, 'additionalMetadata', <any>additionalMetadata);
}
if (file !== undefined) {
formParams.append('file', <any>file);
append(formParams, 'file', <any>file);
}

let requestOptions: RequestOptionsArgs = new RequestOptions({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import { Order } from '../model/order';
import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
import { Configuration } from '../configuration';

type FormParams = HttpParams | URLSearchParams | FormData;

function getAppender(useReturnValue = false) {
return (formParams: FormParams, param: string, value: any) =>
useReturnValue ? formParams.append(param, value) : formParams.append(param, value), formParams;
}

@Injectable()
export class StoreService {
Expand Down
Loading