Refactor nodejs generated code structure (#4909)

* read directly from templates

* refactor nodejs structure

* dont inject into global scope

* move to 2 spaces consistently
This commit is contained in:
Tony Tam
2017-03-05 09:10:01 -08:00
committed by wing328
parent 7aebcfa7c7
commit 15a0bf5df5
17 changed files with 765 additions and 544 deletions

View File

@@ -1,37 +1,97 @@
'use strict';
var url = require('url');
var Pet = require('./PetService');
var utils = require('../utils/writer.js');
var Pet = require('../service/PetService');
module.exports.addPet = function addPet (req, res, next) {
Pet.addPet(req.swagger.params, res, next);
var body = req.swagger.params['body'].value;
Pet.addPet(body)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.deletePet = function deletePet (req, res, next) {
Pet.deletePet(req.swagger.params, res, next);
var petId = req.swagger.params['petId'].value;
var api_key = req.swagger.params['api_key'].value;
Pet.deletePet(petId,api_key)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.findPetsByStatus = function findPetsByStatus (req, res, next) {
Pet.findPetsByStatus(req.swagger.params, res, next);
var status = req.swagger.params['status'].value;
Pet.findPetsByStatus(status)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.findPetsByTags = function findPetsByTags (req, res, next) {
Pet.findPetsByTags(req.swagger.params, res, next);
var tags = req.swagger.params['tags'].value;
Pet.findPetsByTags(tags)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.getPetById = function getPetById (req, res, next) {
Pet.getPetById(req.swagger.params, res, next);
var petId = req.swagger.params['petId'].value;
Pet.getPetById(petId)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.updatePet = function updatePet (req, res, next) {
Pet.updatePet(req.swagger.params, res, next);
var body = req.swagger.params['body'].value;
Pet.updatePet(body)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.updatePetWithForm = function updatePetWithForm (req, res, next) {
Pet.updatePetWithForm(req.swagger.params, res, next);
var petId = req.swagger.params['petId'].value;
var name = req.swagger.params['name'].value;
var status = req.swagger.params['status'].value;
Pet.updatePetWithForm(petId,name,status)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.uploadFile = function uploadFile (req, res, next) {
Pet.uploadFile(req.swagger.params, res, next);
var petId = req.swagger.params['petId'].value;
var additionalMetadata = req.swagger.params['additionalMetadata'].value;
var file = req.swagger.params['file'].value;
Pet.uploadFile(petId,additionalMetadata,file)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};

View File

@@ -1,166 +0,0 @@
'use strict';
exports.addPet = function(args, res, next) {
/**
* Add a new pet to the store
*
*
* body Pet Pet object that needs to be added to the store
* no response value expected for this operation
**/
res.end();
}
exports.deletePet = function(args, res, next) {
/**
* Deletes a pet
*
*
* petId Long Pet id to delete
* api_key String (optional)
* no response value expected for this operation
**/
res.end();
}
exports.findPetsByStatus = function(args, res, next) {
/**
* Finds Pets by status
* Multiple status values can be provided with comma separated strings
*
* status List Status values that need to be considered for filter
* returns List
**/
var examples = {};
examples['application/json'] = [ {
"tags" : [ {
"id" : 123456789,
"name" : "aeiou"
} ],
"id" : 123456789,
"category" : {
"id" : 123456789,
"name" : "aeiou"
},
"status" : "aeiou",
"name" : "doggie",
"photoUrls" : [ "aeiou" ]
} ];
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}
exports.findPetsByTags = function(args, res, next) {
/**
* Finds Pets by tags
* Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
*
* tags List Tags to filter by
* returns List
**/
var examples = {};
examples['application/json'] = [ {
"tags" : [ {
"id" : 123456789,
"name" : "aeiou"
} ],
"id" : 123456789,
"category" : {
"id" : 123456789,
"name" : "aeiou"
},
"status" : "aeiou",
"name" : "doggie",
"photoUrls" : [ "aeiou" ]
} ];
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}
exports.getPetById = function(args, res, next) {
/**
* Find pet by ID
* Returns a single pet
*
* petId Long ID of pet to return
* returns Pet
**/
var examples = {};
examples['application/json'] = {
"tags" : [ {
"id" : 123456789,
"name" : "aeiou"
} ],
"id" : 123456789,
"category" : {
"id" : 123456789,
"name" : "aeiou"
},
"status" : "aeiou",
"name" : "doggie",
"photoUrls" : [ "aeiou" ]
};
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}
exports.updatePet = function(args, res, next) {
/**
* Update an existing pet
*
*
* body Pet Pet object that needs to be added to the store
* no response value expected for this operation
**/
res.end();
}
exports.updatePetWithForm = function(args, res, next) {
/**
* Updates a pet in the store with form data
*
*
* petId Long ID of pet that needs to be updated
* name String Updated name of the pet (optional)
* status String Updated status of the pet (optional)
* no response value expected for this operation
**/
res.end();
}
exports.uploadFile = function(args, res, next) {
/**
* uploads an image
*
*
* petId Long ID of pet to update
* additionalMetadata String Additional data to pass to server (optional)
* file File file to upload (optional)
* returns ApiResponse
**/
var examples = {};
examples['application/json'] = {
"message" : "aeiou",
"code" : 123,
"type" : "aeiou"
};
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}

View File

@@ -1,21 +1,47 @@
'use strict';
var url = require('url');
var Store = require('./StoreService');
var utils = require('../utils/writer.js');
var Store = require('../service/StoreService');
module.exports.deleteOrder = function deleteOrder (req, res, next) {
Store.deleteOrder(req.swagger.params, res, next);
var orderId = req.swagger.params['orderId'].value;
Store.deleteOrder(orderId)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.getInventory = function getInventory (req, res, next) {
Store.getInventory(req.swagger.params, res, next);
Store.getInventory()
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.getOrderById = function getOrderById (req, res, next) {
Store.getOrderById(req.swagger.params, res, next);
var orderId = req.swagger.params['orderId'].value;
Store.getOrderById(orderId)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.placeOrder = function placeOrder (req, res, next) {
Store.placeOrder(req.swagger.params, res, next);
var body = req.swagger.params['body'].value;
Store.placeOrder(body)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};

View File

@@ -1,82 +0,0 @@
'use strict';
exports.deleteOrder = function(args, res, next) {
/**
* Delete purchase order by ID
* For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
*
* orderId String ID of the order that needs to be deleted
* no response value expected for this operation
**/
res.end();
}
exports.getInventory = function(args, res, next) {
/**
* Returns pet inventories by status
* Returns a map of status codes to quantities
*
* returns Map
**/
var examples = {};
examples['application/json'] = {
"key" : 123
};
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}
exports.getOrderById = function(args, res, next) {
/**
* Find purchase order by ID
* For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
*
* orderId Long ID of pet that needs to be fetched
* returns Order
**/
var examples = {};
examples['application/json'] = {
"id" : 123456789,
"petId" : 123456789,
"complete" : true,
"status" : "aeiou",
"quantity" : 123,
"shipDate" : "2000-01-23T04:56:07.000+00:00"
};
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}
exports.placeOrder = function(args, res, next) {
/**
* Place an order for a pet
*
*
* body Order order placed for purchasing the pet
* returns Order
**/
var examples = {};
examples['application/json'] = {
"id" : 123456789,
"petId" : 123456789,
"complete" : true,
"status" : "aeiou",
"quantity" : 123,
"shipDate" : "2000-01-23T04:56:07.000+00:00"
};
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}

View File

@@ -1,37 +1,93 @@
'use strict';
var url = require('url');
var User = require('./UserService');
var utils = require('../utils/writer.js');
var User = require('../service/UserService');
module.exports.createUser = function createUser (req, res, next) {
User.createUser(req.swagger.params, res, next);
var body = req.swagger.params['body'].value;
User.createUser(body)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.createUsersWithArrayInput = function createUsersWithArrayInput (req, res, next) {
User.createUsersWithArrayInput(req.swagger.params, res, next);
var body = req.swagger.params['body'].value;
User.createUsersWithArrayInput(body)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.createUsersWithListInput = function createUsersWithListInput (req, res, next) {
User.createUsersWithListInput(req.swagger.params, res, next);
var body = req.swagger.params['body'].value;
User.createUsersWithListInput(body)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.deleteUser = function deleteUser (req, res, next) {
User.deleteUser(req.swagger.params, res, next);
var username = req.swagger.params['username'].value;
User.deleteUser(username)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.getUserByName = function getUserByName (req, res, next) {
User.getUserByName(req.swagger.params, res, next);
var username = req.swagger.params['username'].value;
User.getUserByName(username)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.loginUser = function loginUser (req, res, next) {
User.loginUser(req.swagger.params, res, next);
var username = req.swagger.params['username'].value;
var password = req.swagger.params['password'].value;
User.loginUser(username,password)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.logoutUser = function logoutUser (req, res, next) {
User.logoutUser(req.swagger.params, res, next);
User.logoutUser()
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};
module.exports.updateUser = function updateUser (req, res, next) {
User.updateUser(req.swagger.params, res, next);
var username = req.swagger.params['username'].value;
var body = req.swagger.params['body'].value;
User.updateUser(username,body)
.then(function (response) {
utils.writeJson(res, response);
})
.catch(function (response) {
utils.writeJson(res, response);
});
};

View File

@@ -1,114 +0,0 @@
'use strict';
exports.createUser = function(args, res, next) {
/**
* Create user
* This can only be done by the logged in user.
*
* body User Created user object
* no response value expected for this operation
**/
res.end();
}
exports.createUsersWithArrayInput = function(args, res, next) {
/**
* Creates list of users with given input array
*
*
* body List List of user object
* no response value expected for this operation
**/
res.end();
}
exports.createUsersWithListInput = function(args, res, next) {
/**
* Creates list of users with given input array
*
*
* body List List of user object
* no response value expected for this operation
**/
res.end();
}
exports.deleteUser = function(args, res, next) {
/**
* Delete user
* This can only be done by the logged in user.
*
* username String The name that needs to be deleted
* no response value expected for this operation
**/
res.end();
}
exports.getUserByName = function(args, res, next) {
/**
* Get user by user name
*
*
* username String The name that needs to be fetched. Use user1 for testing.
* returns User
**/
var examples = {};
examples['application/json'] = {
"id" : 123456789,
"lastName" : "aeiou",
"phone" : "aeiou",
"username" : "aeiou",
"email" : "aeiou",
"userStatus" : 123,
"firstName" : "aeiou",
"password" : "aeiou"
};
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}
exports.loginUser = function(args, res, next) {
/**
* Logs user into the system
*
*
* username String The user name for login
* password String The password for login in clear text
* returns String
**/
var examples = {};
examples['application/json'] = "aeiou";
if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} else {
res.end();
}
}
exports.logoutUser = function(args, res, next) {
/**
* Logs out current logged in user session
*
*
* no response value expected for this operation
**/
res.end();
}
exports.updateUser = function(args, res, next) {
/**
* Updated user
* This can only be done by the logged in user.
*
* username String name that need to be deleted
* body User Updated user object
* no response value expected for this operation
**/
res.end();
}