Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor nodejs generated code structure #4909

Merged
merged 4 commits into from
Mar 5, 2017
Merged

Refactor nodejs generated code structure #4909

merged 4 commits into from
Mar 5, 2017

Conversation

fehguy
Copy link
Contributor

@fehguy fehguy commented Mar 3, 2017

This is a breaking change in the generated code structure for nodejs.

In all previous versions of the nodejs generated code, there was a sort of silly abstraction layer--there were controllers for receiving calls and service classes, designed to hold business logic. The challenge with the existing structure was that the service class was interacting directly with the response object, and thus there was a mix of business logic and routing. Way too convoluted for most nodejs developers.

This change breaks up the responsibilities. There is a controller and service class separation of concerns still, but they are:

  1. separated into different folders. This makes it easier to ignore changes in generated code in the service folder to avoid overwriting files.

  2. Almost completely abstracting away the request/response cycle from the service class

  3. Use of promises for better code structure

The Almost is because in the service classes, there are scenarios where you need to indicate the response code that goes back over the wire. To do this, a utility class is required, and you can then specify what code you want to send to the caller.

@fehguy fehguy added this to the v2.3.0 milestone Mar 3, 2017
@@ -26,6 +26,6 @@ fi

# if you've executed sbt assembly previously it will use that instead.
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
ags="$@ generate -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l nodejs-server -o samples/server/petstore/nodejs"
ags="$@ generate -t modules/swagger-codegen/src/main/resources/nodejs -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l nodejs-server -o samples/server/petstore/nodejs"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make it easier to test template changes, the bin script is now loading templates directly from the file system instead of the compiled jar

@@ -1,67 +0,0 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 I think this file was added on accident? Please let me know and I can restore them if needed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file was added on accident. Thanks for removing it.

@@ -1,30 +0,0 @@
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@wing328 same here...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file was added on accident. Thanks for removing it.


public class NodeJSServerCodegen extends DefaultCodegen implements CodegenConfig {

private static final Logger LOGGER = LoggerFactory.getLogger(NodeJSServerCodegen.class);

protected String implFolder = "service";
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will adjust PR to make this configurable.

@@ -1,13 +1,21 @@
'use strict';

var url = require('url');
require('../utils/writer.js');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pull in utility functions for writeJson

{{/returnType}}
**/
exports.{{{operationId}}} = function({{#allParams}}{{paramName}}{{#hasMore}},{{/hasMore}}{{/allParams}}) {
return new Promise(function(resolve, reject) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No more req, res, only using Promise lib.

writeJson(res, response);
})
.catch(function (response) {
writeJson(res, response);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to send specific response codes, use the respondWithCode function:

writeJson(respondWithCode(500, { message: 'it broked'}));

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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note, all input values are extracted in generated code to make it easier to implement the service class.

@fehguy
Copy link
Contributor Author

fehguy commented Mar 4, 2017

@wing328 if you have any feedback please let me know, otherwise we can merge this.

@wing328
Copy link
Contributor

wing328 commented Mar 5, 2017

@fehguy I did some tests and the result is good.

@wing328 wing328 merged commit 15a0bf5 into 2.3.0 Mar 5, 2017
@wing328 wing328 deleted the issue-3515 branch March 22, 2017 08:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants