Skip to content

Commit

Permalink
[TypeScript-Node] support setting the content-type header per-call (#…
Browse files Browse the repository at this point in the history
…1868)

* [TypeScript-Node] support setting the content-type header per-call

This approach is inspired by the typescript-fetch implementation in
swagger-codegen.

Fixes #1867

* Update modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache

Co-Authored-By: silasbw <[email protected]>

* Update modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache

Co-Authored-By: silasbw <[email protected]>

* Update Petstore sample

* Fix types

* update "npm" petstore example

* Rename
  • Loading branch information
silasbw authored and wing328 committed Jan 16, 2019
1 parent df8137c commit f5d6aae
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export class {{classname}} {
* @param {{paramName}} {{description}}
{{/allParams}}
*/
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) : Promise<{ response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
public {{nickname}} ({{#allParams}}{{paramName}}{{^required}}?{{/required}}: {{{dataType}}}, {{/allParams}}options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.{{#supportsES6}}IncomingMessage{{/supportsES6}}{{^supportsES6}}ClientResponse{{/supportsES6}}; {{#returnType}}body: {{{returnType}}}; {{/returnType}}{{^returnType}}body?: any; {{/returnType}} }> {
const localVarPath = this.basePath + '{{{path}}}'{{#pathParams}}
.replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}};
let localVarQueryParameters: any = {};
Expand All @@ -152,6 +152,7 @@ export class {{classname}} {
{{#headerParams}}
localVarHeaderParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
{{/headerParams}}
(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ export class FakeApi {
* @summary To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
* @param testCodeInjectEndRnNR To test code injection *_/ &#39; \\\&quot; &#x3D;end -- \\\\r\\\\n \\\\n \\\\r
*/
public testCodeInjectEndRnNR (testCodeInjectEndRnNR?: string) : Promise<{ response: http.ClientResponse; body?: any; }> {
public testCodeInjectEndRnNR (testCodeInjectEndRnNR?: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/fake';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
let localVarFormParams: any = {};
(<any>Object).assign(localVarHeaderParams, options.headers);
let localVarUseFormData = false;
Expand Down
24 changes: 16 additions & 8 deletions samples/client/petstore/typescript-node/default/api/petApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class PetApi {
* @summary Add a new pet to the store
* @param body Pet object that needs to be added to the store
*/
public addPet (body: Pet) : Promise<{ response: http.ClientResponse; body?: any; }> {
public addPet (body: Pet, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/pet';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -94,6 +94,7 @@ export class PetApi {
throw new Error('Required parameter body was null or undefined when calling addPet.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -138,7 +139,7 @@ export class PetApi {
* @param petId Pet id to delete
* @param apiKey
*/
public deletePet (petId: number, apiKey?: string) : Promise<{ response: http.ClientResponse; body?: any; }> {
public deletePet (petId: number, apiKey?: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/pet/{petId}'
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
let localVarQueryParameters: any = {};
Expand All @@ -151,6 +152,7 @@ export class PetApi {
}

localVarHeaderParams['api_key'] = ObjectSerializer.serialize(apiKey, "string");
(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -193,7 +195,7 @@ export class PetApi {
* @summary Finds Pets by status
* @param status Status values that need to be considered for filter
*/
public findPetsByStatus (status: Array<'available' | 'pending' | 'sold'>) : Promise<{ response: http.ClientResponse; body: Array<Pet>; }> {
public findPetsByStatus (status: Array<'available' | 'pending' | 'sold'>, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: Array<Pet>; }> {
const localVarPath = this.basePath + '/pet/findByStatus';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -208,6 +210,7 @@ export class PetApi {
localVarQueryParameters['status'] = ObjectSerializer.serialize(status, "Array<'available' | 'pending' | 'sold'>");
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -251,7 +254,7 @@ export class PetApi {
* @summary Finds Pets by tags
* @param tags Tags to filter by
*/
public findPetsByTags (tags: Array<string>) : Promise<{ response: http.ClientResponse; body: Array<Pet>; }> {
public findPetsByTags (tags: Array<string>, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: Array<Pet>; }> {
const localVarPath = this.basePath + '/pet/findByTags';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -266,6 +269,7 @@ export class PetApi {
localVarQueryParameters['tags'] = ObjectSerializer.serialize(tags, "Array<string>");
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -309,7 +313,7 @@ export class PetApi {
* @summary Find pet by ID
* @param petId ID of pet to return
*/
public getPetById (petId: number) : Promise<{ response: http.ClientResponse; body: Pet; }> {
public getPetById (petId: number, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: Pet; }> {
const localVarPath = this.basePath + '/pet/{petId}'
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
let localVarQueryParameters: any = {};
Expand All @@ -321,6 +325,7 @@ export class PetApi {
throw new Error('Required parameter petId was null or undefined when calling getPetById.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -364,7 +369,7 @@ export class PetApi {
* @summary Update an existing pet
* @param body Pet object that needs to be added to the store
*/
public updatePet (body: Pet) : Promise<{ response: http.ClientResponse; body?: any; }> {
public updatePet (body: Pet, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/pet';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -375,6 +380,7 @@ export class PetApi {
throw new Error('Required parameter body was null or undefined when calling updatePet.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -420,7 +426,7 @@ export class PetApi {
* @param name Updated name of the pet
* @param status Updated status of the pet
*/
public updatePetWithForm (petId: number, name?: string, status?: string) : Promise<{ response: http.ClientResponse; body?: any; }> {
public updatePetWithForm (petId: number, name?: string, status?: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/pet/{petId}'
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
let localVarQueryParameters: any = {};
Expand All @@ -432,6 +438,7 @@ export class PetApi {
throw new Error('Required parameter petId was null or undefined when calling updatePetWithForm.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -484,7 +491,7 @@ export class PetApi {
* @param additionalMetadata Additional data to pass to server
* @param file file to upload
*/
public uploadFile (petId: number, additionalMetadata?: string, file?: Buffer) : Promise<{ response: http.ClientResponse; body: ApiResponse; }> {
public uploadFile (petId: number, additionalMetadata?: string, file?: Buffer, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: ApiResponse; }> {
const localVarPath = this.basePath + '/pet/{petId}/uploadImage'
.replace('{' + 'petId' + '}', encodeURIComponent(String(petId)));
let localVarQueryParameters: any = {};
Expand All @@ -496,6 +503,7 @@ export class PetApi {
throw new Error('Required parameter petId was null or undefined when calling uploadFile.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down
12 changes: 8 additions & 4 deletions samples/client/petstore/typescript-node/default/api/storeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class StoreApi {
* @summary Delete purchase order by ID
* @param orderId ID of the order that needs to be deleted
*/
public deleteOrder (orderId: string) : Promise<{ response: http.ClientResponse; body?: any; }> {
public deleteOrder (orderId: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/store/order/{orderId}'
.replace('{' + 'orderId' + '}', encodeURIComponent(String(orderId)));
let localVarQueryParameters: any = {};
Expand All @@ -89,6 +89,7 @@ export class StoreApi {
throw new Error('Required parameter orderId was null or undefined when calling deleteOrder.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -128,12 +129,13 @@ export class StoreApi {
* Returns a map of status codes to quantities
* @summary Returns pet inventories by status
*/
public getInventory () : Promise<{ response: http.ClientResponse; body: { [key: string]: number; }; }> {
public getInventory (options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: { [key: string]: number; }; }> {
const localVarPath = this.basePath + '/store/inventory';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
let localVarFormParams: any = {};

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -177,7 +179,7 @@ export class StoreApi {
* @summary Find purchase order by ID
* @param orderId ID of pet that needs to be fetched
*/
public getOrderById (orderId: number) : Promise<{ response: http.ClientResponse; body: Order; }> {
public getOrderById (orderId: number, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: Order; }> {
const localVarPath = this.basePath + '/store/order/{orderId}'
.replace('{' + 'orderId' + '}', encodeURIComponent(String(orderId)));
let localVarQueryParameters: any = {};
Expand All @@ -189,6 +191,7 @@ export class StoreApi {
throw new Error('Required parameter orderId was null or undefined when calling getOrderById.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -230,7 +233,7 @@ export class StoreApi {
* @summary Place an order for a pet
* @param body order placed for purchasing the pet
*/
public placeOrder (body: Order) : Promise<{ response: http.ClientResponse; body: Order; }> {
public placeOrder (body: Order, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: Order; }> {
const localVarPath = this.basePath + '/store/order';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -241,6 +244,7 @@ export class StoreApi {
throw new Error('Required parameter body was null or undefined when calling placeOrder.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down
24 changes: 16 additions & 8 deletions samples/client/petstore/typescript-node/default/api/userApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class UserApi {
* @summary Create user
* @param body Created user object
*/
public createUser (body: User) : Promise<{ response: http.ClientResponse; body?: any; }> {
public createUser (body: User, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/user';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -86,6 +86,7 @@ export class UserApi {
throw new Error('Required parameter body was null or undefined when calling createUser.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -127,7 +128,7 @@ export class UserApi {
* @summary Creates list of users with given input array
* @param body List of user object
*/
public createUsersWithArrayInput (body: Array<User>) : Promise<{ response: http.ClientResponse; body?: any; }> {
public createUsersWithArrayInput (body: Array<User>, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/user/createWithArray';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -138,6 +139,7 @@ export class UserApi {
throw new Error('Required parameter body was null or undefined when calling createUsersWithArrayInput.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -179,7 +181,7 @@ export class UserApi {
* @summary Creates list of users with given input array
* @param body List of user object
*/
public createUsersWithListInput (body: Array<User>) : Promise<{ response: http.ClientResponse; body?: any; }> {
public createUsersWithListInput (body: Array<User>, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/user/createWithList';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -190,6 +192,7 @@ export class UserApi {
throw new Error('Required parameter body was null or undefined when calling createUsersWithListInput.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -231,7 +234,7 @@ export class UserApi {
* @summary Delete user
* @param username The name that needs to be deleted
*/
public deleteUser (username: string) : Promise<{ response: http.ClientResponse; body?: any; }> {
public deleteUser (username: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/user/{username}'
.replace('{' + 'username' + '}', encodeURIComponent(String(username)));
let localVarQueryParameters: any = {};
Expand All @@ -243,6 +246,7 @@ export class UserApi {
throw new Error('Required parameter username was null or undefined when calling deleteUser.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -283,7 +287,7 @@ export class UserApi {
* @summary Get user by user name
* @param username The name that needs to be fetched. Use user1 for testing.
*/
public getUserByName (username: string) : Promise<{ response: http.ClientResponse; body: User; }> {
public getUserByName (username: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: User; }> {
const localVarPath = this.basePath + '/user/{username}'
.replace('{' + 'username' + '}', encodeURIComponent(String(username)));
let localVarQueryParameters: any = {};
Expand All @@ -295,6 +299,7 @@ export class UserApi {
throw new Error('Required parameter username was null or undefined when calling getUserByName.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -337,7 +342,7 @@ export class UserApi {
* @param username The user name for login
* @param password The password for login in clear text
*/
public loginUser (username: string, password: string) : Promise<{ response: http.ClientResponse; body: string; }> {
public loginUser (username: string, password: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body: string; }> {
const localVarPath = this.basePath + '/user/login';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
Expand All @@ -361,6 +366,7 @@ export class UserApi {
localVarQueryParameters['password'] = ObjectSerializer.serialize(password, "string");
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -401,12 +407,13 @@ export class UserApi {
*
* @summary Logs out current logged in user session
*/
public logoutUser () : Promise<{ response: http.ClientResponse; body?: any; }> {
public logoutUser (options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/user/logout';
let localVarQueryParameters: any = {};
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
let localVarFormParams: any = {};

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down Expand Up @@ -448,7 +455,7 @@ export class UserApi {
* @param username name that need to be deleted
* @param body Updated user object
*/
public updateUser (username: string, body: User) : Promise<{ response: http.ClientResponse; body?: any; }> {
public updateUser (username: string, body: User, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
const localVarPath = this.basePath + '/user/{username}'
.replace('{' + 'username' + '}', encodeURIComponent(String(username)));
let localVarQueryParameters: any = {};
Expand All @@ -465,6 +472,7 @@ export class UserApi {
throw new Error('Required parameter body was null or undefined when calling updateUser.');
}

(<any>Object).assign(localVarHeaderParams, options.headers);

let localVarUseFormData = false;

Expand Down
Loading

0 comments on commit f5d6aae

Please sign in to comment.