Update sample with small test script

This commit is contained in:
Mads Mætzke Tandrup 2015-07-26 08:22:32 +02:00
parent e41a475335
commit 02da80e837
No known key found for this signature in database
GPG Key ID: 67056F406C84B14B
7 changed files with 311 additions and 131 deletions

View File

@ -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) {
(<any>requestOptions).formData = formParams;
} else {
requestOptions.form = formParams;
}
}
request(requestOptions, (error, response, body) => {

View File

@ -1,3 +1,3 @@
/node_modules
/typings
/api.js
/*.js

View File

@ -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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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<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) {
(<any>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<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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>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) {
(<any>requestOptions).formData = formParams;
} else {
requestOptions.form = formParams;
}
}
request(requestOptions, (error, response, body) => {

View File

@ -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 = <api.Pet>(<any>res.response).body;
petId = (<any>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);
//});

View File

@ -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",

View File

@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"noImplicitAny": true
},
"files": [
"api.ts",
"client.ts",
"typings/tsd.d.ts"
]
}

View File

@ -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"
}
}
}