Skip to content

Commit f5d6aae

Browse files
silasbwwing328
authored andcommitted
[TypeScript-Node] support setting the content-type header per-call (#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 <silasbw@gmail.com> * Update modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache Co-Authored-By: silasbw <silasbw@gmail.com> * Update Petstore sample * Fix types * update "npm" petstore example * Rename
1 parent df8137c commit f5d6aae

File tree

8 files changed

+84
-42
lines changed

8 files changed

+84
-42
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class {{classname}} {
127127
* @param {{paramName}} {{description}}
128128
{{/allParams}}
129129
*/
130-
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}} }> {
130+
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}} }> {
131131
const localVarPath = this.basePath + '{{{path}}}'{{#pathParams}}
132132
.replace('{' + '{{baseName}}' + '}', encodeURIComponent(String({{paramName}}))){{/pathParams}};
133133
let localVarQueryParameters: any = {};
@@ -152,6 +152,7 @@ export class {{classname}} {
152152
{{#headerParams}}
153153
localVarHeaderParams['{{baseName}}'] = ObjectSerializer.serialize({{paramName}}, "{{{dataType}}}");
154154
{{/headerParams}}
155+
(<any>Object).assign(localVarHeaderParams, options.headers);
155156

156157
let localVarUseFormData = false;
157158

samples/client/petstore-security-test/typescript-node/api/fakeApi.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,13 @@ export class FakeApi {
7474
* @summary To test code injection *_/ ' \" =end -- \\r\\n \\n \\r
7575
* @param testCodeInjectEndRnNR To test code injection *_/ &#39; \\\&quot; &#x3D;end -- \\\\r\\\\n \\\\n \\\\r
7676
*/
77-
public testCodeInjectEndRnNR (testCodeInjectEndRnNR?: string) : Promise<{ response: http.ClientResponse; body?: any; }> {
77+
public testCodeInjectEndRnNR (testCodeInjectEndRnNR?: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
7878
const localVarPath = this.basePath + '/fake';
7979
let localVarQueryParameters: any = {};
8080
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
8181
let localVarFormParams: any = {};
8282

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

8485
let localVarUseFormData = false;
8586

samples/client/petstore/typescript-node/default/api/petApi.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export class PetApi {
8383
* @summary Add a new pet to the store
8484
* @param body Pet object that needs to be added to the store
8585
*/
86-
public addPet (body: Pet) : Promise<{ response: http.ClientResponse; body?: any; }> {
86+
public addPet (body: Pet, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
8787
const localVarPath = this.basePath + '/pet';
8888
let localVarQueryParameters: any = {};
8989
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
@@ -94,6 +94,7 @@ export class PetApi {
9494
throw new Error('Required parameter body was null or undefined when calling addPet.');
9595
}
9696

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

9899
let localVarUseFormData = false;
99100

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

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

155157
let localVarUseFormData = false;
156158

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

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

212215
let localVarUseFormData = false;
213216

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

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

270274
let localVarUseFormData = false;
271275

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

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

325330
let localVarUseFormData = false;
326331

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

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

379385
let localVarUseFormData = false;
380386

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

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

436443
let localVarUseFormData = false;
437444

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

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

500508
let localVarUseFormData = false;
501509

samples/client/petstore/typescript-node/default/api/storeApi.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class StoreApi {
7777
* @summary Delete purchase order by ID
7878
* @param orderId ID of the order that needs to be deleted
7979
*/
80-
public deleteOrder (orderId: string) : Promise<{ response: http.ClientResponse; body?: any; }> {
80+
public deleteOrder (orderId: string, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
8181
const localVarPath = this.basePath + '/store/order/{orderId}'
8282
.replace('{' + 'orderId' + '}', encodeURIComponent(String(orderId)));
8383
let localVarQueryParameters: any = {};
@@ -89,6 +89,7 @@ export class StoreApi {
8989
throw new Error('Required parameter orderId was null or undefined when calling deleteOrder.');
9090
}
9191

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

9394
let localVarUseFormData = false;
9495

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

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

138140
let localVarUseFormData = false;
139141

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

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

193196
let localVarUseFormData = false;
194197

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

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

245249
let localVarUseFormData = false;
246250

samples/client/petstore/typescript-node/default/api/userApi.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class UserApi {
7575
* @summary Create user
7676
* @param body Created user object
7777
*/
78-
public createUser (body: User) : Promise<{ response: http.ClientResponse; body?: any; }> {
78+
public createUser (body: User, options: {headers: {[name: string]: string}} = {headers: {}}) : Promise<{ response: http.ClientResponse; body?: any; }> {
7979
const localVarPath = this.basePath + '/user';
8080
let localVarQueryParameters: any = {};
8181
let localVarHeaderParams: any = (<any>Object).assign({}, this.defaultHeaders);
@@ -86,6 +86,7 @@ export class UserApi {
8686
throw new Error('Required parameter body was null or undefined when calling createUser.');
8787
}
8888

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

9091
let localVarUseFormData = false;
9192

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

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

142144
let localVarUseFormData = false;
143145

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

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

194197
let localVarUseFormData = false;
195198

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

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

247251
let localVarUseFormData = false;
248252

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

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

299304
let localVarUseFormData = false;
300305

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

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

365371
let localVarUseFormData = false;
366372

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

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

411418
let localVarUseFormData = false;
412419

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

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

469477
let localVarUseFormData = false;
470478

0 commit comments

Comments
 (0)