From 02da80e83798e2d39e8f92dbcbb0d9770e8ee658 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mads=20M=C3=A6tzke=20Tandrup?= Date: Sun, 26 Jul 2015 08:22:32 +0200 Subject: [PATCH] Update sample with small test script --- .../resources/TypeScript-node/api.mustache | 17 +- .../petstore/typescript-node/.gitignore | 2 +- .../client/petstore/typescript-node/api.ts | 360 ++++++++++++------ .../client/petstore/typescript-node/client.ts | 44 +++ .../petstore/typescript-node/package.json | 2 +- .../petstore/typescript-node/tsconfig.json | 11 + .../client/petstore/typescript-node/tsd.json | 6 +- 7 files changed, 311 insertions(+), 131 deletions(-) create mode 100644 samples/client/petstore/typescript-node/client.ts create mode 100644 samples/client/petstore/typescript-node/tsconfig.json diff --git a/modules/swagger-codegen/src/main/resources/TypeScript-node/api.mustache b/modules/swagger-codegen/src/main/resources/TypeScript-node/api.mustache index 098cd8bc1d..2d3de0411b 100644 --- a/modules/swagger-codegen/src/main/resources/TypeScript-node/api.mustache +++ b/modules/swagger-codegen/src/main/resources/TypeScript-node/api.mustache @@ -101,7 +101,7 @@ export class {{classname}} { {{/formParams}} var deferred = promise.defer<{ response: http.ClientResponse; {{#returnType}}body: {{{returnType}}}; {{/returnType}} }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: '{{httpMethod}}', qs: queryParameters, uri: path, @@ -109,15 +109,20 @@ export class {{classname}} { {{#bodyParam}} body: {{paramName}}, {{/bodyParam}} - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { diff --git a/samples/client/petstore/typescript-node/.gitignore b/samples/client/petstore/typescript-node/.gitignore index ea683e8b6f..5c06ad7bc2 100644 --- a/samples/client/petstore/typescript-node/.gitignore +++ b/samples/client/petstore/typescript-node/.gitignore @@ -1,3 +1,3 @@ /node_modules /typings -/api.js \ No newline at end of file +/*.js diff --git a/samples/client/petstore/typescript-node/api.ts b/samples/client/petstore/typescript-node/api.ts index bef650a81e..cf556042fa 100644 --- a/samples/client/petstore/typescript-node/api.ts +++ b/samples/client/petstore/typescript-node/api.ts @@ -88,23 +88,29 @@ export class UserApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'POST', qs: queryParameters, uri: path, json: true, body: body, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -130,23 +136,29 @@ export class UserApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'POST', qs: queryParameters, uri: path, json: true, body: body, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -172,23 +184,29 @@ export class UserApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'POST', qs: queryParameters, uri: path, json: true, body: body, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -222,22 +240,28 @@ export class UserApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: string; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -263,22 +287,28 @@ export class UserApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -311,22 +341,28 @@ export class UserApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: User; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -359,23 +395,29 @@ export class UserApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'PUT', qs: queryParameters, uri: path, json: true, body: body, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -408,22 +450,28 @@ export class UserApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'DELETE', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -458,23 +506,29 @@ export class PetApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'PUT', qs: queryParameters, uri: path, json: true, body: body, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -500,23 +554,29 @@ export class PetApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'POST', qs: queryParameters, uri: path, json: true, body: body, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -546,22 +606,28 @@ export class PetApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: Array; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -591,22 +657,28 @@ export class PetApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: Array; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -639,22 +711,28 @@ export class PetApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: Pet; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -687,6 +765,7 @@ export class PetApi { } var useFormData = false; + if (name !== undefined) { formParams['name'] = name; } @@ -697,20 +776,25 @@ export class PetApi { var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'POST', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -745,22 +829,28 @@ export class PetApi { headerParams['apiKey'] = apiKey; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'DELETE', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -793,6 +883,7 @@ export class PetApi { } var useFormData = false; + if (additionalMetadata !== undefined) { formParams['additionalMetadata'] = additionalMetadata; } @@ -804,20 +895,25 @@ export class PetApi { var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'POST', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -852,22 +948,28 @@ export class StoreApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: { [key: string]: number; }; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -893,23 +995,29 @@ export class StoreApi { var formParams: any = {}; var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: Order; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'POST', qs: queryParameters, uri: path, json: true, body: body, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -942,22 +1050,28 @@ export class StoreApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; body: Order; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'GET', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { @@ -990,22 +1104,28 @@ export class StoreApi { } var useFormData = false; + var deferred = promise.defer<{ response: http.ClientResponse; }>(); - var requestOptions: any = { + var requestOptions: request.Options = { method: 'DELETE', qs: queryParameters, uri: path, json: true, - auth: { + } + + if (this.username !== undefined) { + requestOptions.auth = { username: this.username, password: this.password } } - if (useFormData) { - requestOptions.formData = formParams; - } else { - requestOptions.form = formParams; + if (Object.keys(formParams).length) { + if (useFormData) { + (requestOptions).formData = formParams; + } else { + requestOptions.form = formParams; + } } request(requestOptions, (error, response, body) => { diff --git a/samples/client/petstore/typescript-node/client.ts b/samples/client/petstore/typescript-node/client.ts new file mode 100644 index 0000000000..1a99ad67d7 --- /dev/null +++ b/samples/client/petstore/typescript-node/client.ts @@ -0,0 +1,44 @@ +import api = require('./api'); + +var petApi = new api.PetApi('http://petstore.swagger.io', undefined, undefined); + +var pet = new api.Pet(); +pet.name = 'TypeScriptDoggie'; + +var petId: any; + +petApi.addPet(pet) +.then((res) => { + var newPet = (res.response).body; + petId = (res.response).body.id; + console.log(`Created pet with ID ${petId}`); + newPet.status = api.Pet.StatusEnum.available; + return petApi.updatePet(newPet); +}) +.then((res) => { + console.log('Updated pet using POST body'); + return petApi.updatePetWithForm(petId, undefined, "pending"); +}) +.then((res) => { + console.log('Updated pet using POST form'); + return petApi.getPetById(petId); +}) +.then((res) => { + console.log('Got pet by ID: ' + JSON.stringify(res.body)); + if (res.body.status != api.Pet.StatusEnum.pending) { + throw new Error("Unexpected pet status"); + } + return petApi.deletePet(petId); +}) +.then((res) => { + console.log('Deleted pet'); +}) +.catch((err:any) => { + console.error(err); +}); + +//var pets = petApi.findPetsByStatus(['available']); + +//pets.then((data:any) => { +//console.log(data); +//}); diff --git a/samples/client/petstore/typescript-node/package.json b/samples/client/petstore/typescript-node/package.json index e0c2a74a47..8cca4d0484 100644 --- a/samples/client/petstore/typescript-node/package.json +++ b/samples/client/petstore/typescript-node/package.json @@ -5,7 +5,7 @@ "main": "api.js", "scripts": { "postinstall": "tsd reinstall", - "test": "tsc --module commonjs api.ts typings/tsd.d.ts" + "test": "tsc && node client.js" }, "author": "Mads M. Tandrup", "license": "Apache 2.0", diff --git a/samples/client/petstore/typescript-node/tsconfig.json b/samples/client/petstore/typescript-node/tsconfig.json new file mode 100644 index 0000000000..de28deb89c --- /dev/null +++ b/samples/client/petstore/typescript-node/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "module": "commonjs", + "noImplicitAny": true + }, + "files": [ + "api.ts", + "client.ts", + "typings/tsd.d.ts" + ] +} diff --git a/samples/client/petstore/typescript-node/tsd.json b/samples/client/petstore/typescript-node/tsd.json index d72b84c317..89e4861f94 100644 --- a/samples/client/petstore/typescript-node/tsd.json +++ b/samples/client/petstore/typescript-node/tsd.json @@ -9,13 +9,13 @@ "commit": "f6c8ca47193fb67947944a3170912672ac3e908e" }, "request/request.d.ts": { - "commit": "2cc4331067a81e2ae6bc4590e7b33bf286c8f28f" + "commit": "f6c8ca47193fb67947944a3170912672ac3e908e" }, "form-data/form-data.d.ts": { - "commit": "2cc4331067a81e2ae6bc4590e7b33bf286c8f28f" + "commit": "f6c8ca47193fb67947944a3170912672ac3e908e" }, "node/node.d.ts": { - "commit": "48b4cb54af76f02b33dccaa0fdd2ae97db7cdd1b" + "commit": "f6c8ca47193fb67947944a3170912672ac3e908e" } } }