better code format for nodejs server (#4411)

This commit is contained in:
wing328
2016-12-23 02:05:10 +08:00
committed by GitHub
parent 27f1b6ee98
commit aed21bba73
9 changed files with 178 additions and 141 deletions

View File

@@ -1,15 +1,13 @@
'use strict'; 'use strict';
var url = require('url'); var url = require('url');
{{#operations}} {{#operations}}
var {{classname}} = require('./{{classname}}Service'); var {{classname}} = require('./{{classname}}Service');
{{#operation}} {{#operation}}
module.exports.{{nickname}} = function {{nickname}} (req, res, next) { module.exports.{{nickname}} = function {{nickname}} (req, res, next) {
{{classname}}.{{nickname}}(req.swagger.params, res, next); {{classname}}.{{nickname}}(req.swagger.params, res, next);
}; };
{{/operation}} {{/operation}}
{{/operations}} {{/operations}}

View File

@@ -2,26 +2,40 @@
{{#operations}} {{#operations}}
{{#operation}} {{#operation}}
exports.{{nickname}} = function(args, res, next) { exports.{{{operationId}}} = function(args, res, next) {
/** /**
* parameters expected in the args: {{#summary}}
{{#allParams}}* {{paramName}} ({{dataType}}) * {{{summary}}}
{{/allParams}}**/ {{/summary}}
{{^returnType}}// no response value expected for this operation {{#notes}}
* {{{notes}}}
{{/notes}}
*
{{#allParams}}
* {{paramName}} {{{dataType}}} {{{description}}}{{^required}} (optional){{/required}}
{{/allParams}}
{{^returnType}}
* no response value expected for this operation
{{/returnType}} {{/returnType}}
{{#returnType}}
* returns {{{returnType}}}
{{/returnType}}
**/
{{#returnType}} {{#returnType}}
var examples = {}; var examples = {};
{{#examples}}examples['{{contentType}}'] = {{{example}}}; {{#examples}}
examples['{{contentType}}'] = {{{example}}};
{{/examples}} {{/examples}}
if(Object.keys(examples).length > 0) { if (Object.keys(examples).length > 0) {
res.setHeader('Content-Type', 'application/json'); res.setHeader('Content-Type', 'application/json');
res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2)); res.end(JSON.stringify(examples[Object.keys(examples)[0]] || {}, null, 2));
} } else {
else {
res.end(); res.end();
} }
{{/returnType}} {{/returnType}}
{{^returnType}}res.end();{{/returnType}} {{^returnType}}
res.end();
{{/returnType}}
} }
{{/operation}} {{/operation}}

View File

@@ -2,10 +2,8 @@
var url = require('url'); var url = require('url');
var Pet = require('./PetService'); var Pet = require('./PetService');
module.exports.addPet = function addPet (req, res, next) { module.exports.addPet = function addPet (req, res, next) {
Pet.addPet(req.swagger.params, res, next); Pet.addPet(req.swagger.params, res, next);
}; };

View File

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

View File

@@ -2,10 +2,8 @@
var url = require('url'); var url = require('url');
var Store = require('./StoreService'); var Store = require('./StoreService');
module.exports.deleteOrder = function deleteOrder (req, res, next) { module.exports.deleteOrder = function deleteOrder (req, res, next) {
Store.deleteOrder(req.swagger.params, res, next); Store.deleteOrder(req.swagger.params, res, next);
}; };

View File

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

View File

@@ -2,10 +2,8 @@
var url = require('url'); var url = require('url');
var User = require('./UserService'); var User = require('./UserService');
module.exports.createUser = function createUser (req, res, next) { module.exports.createUser = function createUser (req, res, next) {
User.createUser(req.swagger.params, res, next); User.createUser(req.swagger.params, res, next);
}; };

View File

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

View File

@@ -6,7 +6,7 @@
"scripts": { "scripts": {
"prestart": "npm install", "prestart": "npm install",
"start": "node index.js" "start": "node index.js"
}, },
"keywords": [ "keywords": [
"swagger" "swagger"
], ],