diff --git a/README.md b/README.md index 5dfd7cc..93c8db7 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ ES6 yeoman generator for FAM troupe. ``` npm install -g generator-troupe -yo forbin-scudl ``` # Quality and Compatibility @@ -26,8 +25,28 @@ cd node_modules/generator-troupe gulp test-local ``` -# Getting Started -You can generate troupe controllers with cucumber features with this generator. +# How to generate a SCUDL for a provider like AWS through Conan +All of the generators will ask you for the model name, which is singular and camelCased. Eg: apple, account, accountType + +## 1. Generate the model +``` +yo troupe:model +``` +The models are generated with a default name property with a non empty validation. After the generation, is strongly recommended to add all the properties you need to validate, and all the relationships with other models. + +## 2. Generate the functions +``` +yo troupe:functions +``` +This generator will create all the lambda classes to handle the SCUDL operations, all the necessary steps to do that, and it will create also a specialized class that returns all the meta data that a deployer like Conan may need. Every file will have their spec file to test it's functionality. + +After this generation you will need to customize all the special steps, adding or removing everything that you may need, as well as the function metadata if necessary, like a timeout or anything. + +## 3. Generate the resources +``` +yo troupe:resources +``` +In this case, the generated class will provide the deployer all the metadata that he may need in order to create the api/http resources that calls the lambda functions. Remember to add here any meta information for the specific resource that you may need. # How to Contribute @@ -39,7 +58,7 @@ We always aim to be friendly and helpful. ## Running Tests -It's easy to run the test suite locally, and *highly recommended* if you're using Generator-troupe.js on a platform we aren't automatically testing for. +It's easy to run the test suite locally, and *highly recommended* if you're using Troupe Generator on a platform we aren't automatically testing for. ``` npm test diff --git a/generators/functions/templates/source/lib/lambdas/models/modelsCreate.js b/generators/functions/templates/source/lib/lambdas/models/modelsCreate.js index 0a3e175..7e1fa0f 100644 --- a/generators/functions/templates/source/lib/lambdas/models/modelsCreate.js +++ b/generators/functions/templates/source/lib/lambdas/models/modelsCreate.js @@ -8,6 +8,7 @@ import authorize from "../../steps/authorize.js"; import save<%= modelNamePluralPascal %> from "../../steps/<%= modelNamePlural %>/save<%= modelNamePluralPascal %>.js"; import <%= modelNamePascal %> from "../../models/<%= modelName %>.js"; import { local } from "../../../../environment.json"; +import { getBadRequestError } from "../../errors.js"; Model.database = new Database(local); @@ -16,14 +17,17 @@ export default class <%= modelNamePluralPascal %>Create { this.database = Model.database; this.actionContext = new ActionContext(input, context); this.actionContext.permission = "<%= modelNamePlural %>:create"; - this.actionContext.<%= modelName %>Parameters = jsonApiModelFormatter(input.data.data, <%= modelNamePascal %>); - delete this.actionContext.<%= modelName %>Parameters.id; - this.action = new Action(this.actionContext); - this.action.series( - authenticate, - authorize, - save<%= modelNamePluralPascal %> - ); + + if(input.data.data) { + this.actionContext.<%= modelName %>Parameters = jsonApiModelFormatter(input.data.data, <%= modelNamePascal %>); + delete this.actionContext.<%= modelName %>Parameters.id; + this.action = new Action(this.actionContext); + this.action.series( + authenticate, + authorize, + save<%= modelNamePluralPascal %> + ); + } } /** @@ -35,14 +39,18 @@ export default class <%= modelNamePluralPascal %>Create { * @return {undefined} Returns nothing */ handler(input, context) { - this.action - .results((errors) => { - if (errors) { - context.done(errors); - } else { - const data = jsonApiModelFormatter(this.actionContext.<%= modelName %>); - context.done(null, { data }); - } - }); + if(this.action) { + this.action + .results((errors) => { + if (errors) { + context.done(errors); + } else { + const data = jsonApiModelFormatter(this.actionContext.<%= modelName %>); + context.done(null, { data }); + } + }); + } else { + context.done(getBadRequestError()); + } } } diff --git a/generators/functions/templates/source/lib/lambdas/models/modelsUpdate.js b/generators/functions/templates/source/lib/lambdas/models/modelsUpdate.js index 602dc1f..28962a4 100644 --- a/generators/functions/templates/source/lib/lambdas/models/modelsUpdate.js +++ b/generators/functions/templates/source/lib/lambdas/models/modelsUpdate.js @@ -9,6 +9,7 @@ import fetch<%= modelNamePluralPascal %> from "../../steps/<%= modelNamePlural % import save<%= modelNamePluralPascal %> from "../../steps/<%= modelNamePlural %>/save<%= modelNamePluralPascal %>.js"; import <%= modelNamePascal %> from "../../models/<%= modelName %>.js"; import { local } from "../../../../environment.json"; +import { getBadRequestError } from "../../errors.js"; Model.database = new Database(local); @@ -18,14 +19,17 @@ export default class <%= modelNamePluralPascal %>Update { this.actionContext = new ActionContext(input, context); this.actionContext.permission = "<%= modelNamePlural %>:update"; this.actionContext.<%= modelName %>Id = input.params.path.id; - this.actionContext.<%= modelName %>Parameters = jsonApiModelFormatter(input.data.data, <%= modelNamePascal %>); - this.action = new Action(this.actionContext); - this.action.series( - authenticate, - authorize, - fetch<%= modelNamePluralPascal %>, - save<%= modelNamePluralPascal %> - ); + + if(input.data.data) { + this.actionContext.<%= modelName %>Parameters = jsonApiModelFormatter(input.data.data, <%= modelNamePascal %>); + this.action = new Action(this.actionContext); + this.action.series( + authenticate, + authorize, + fetch<%= modelNamePluralPascal %>, + save<%= modelNamePluralPascal %> + ); + } } /** @@ -37,13 +41,17 @@ export default class <%= modelNamePluralPascal %>Update { * @return {undefined} Returns nothing */ handler(input, context) { - this.action - .results((errors) => { - if (errors) { - context.done(errors); - } else { - context.done(null, { data: jsonApiModelFormatter(this.actionContext.<%= modelName %>) }); - } - }); + if(this.action) { + this.action + .results((errors) => { + if (errors) { + context.done(errors); + } else { + context.done(null, { data: jsonApiModelFormatter(this.actionContext.<%= modelName %>) }); + } + }); + } else { + context.done(getBadRequestError()); + } } } diff --git a/generators/functions/templates/spec/lambdas/models/modelsCreate.spec.js b/generators/functions/templates/spec/lambdas/models/modelsCreate.spec.js index ed11356..022e0e2 100644 --- a/generators/functions/templates/spec/lambdas/models/modelsCreate.spec.js +++ b/generators/functions/templates/spec/lambdas/models/modelsCreate.spec.js @@ -46,6 +46,16 @@ describe("lambdas/<%= modelName %>Create.js", () => { handlerClass.database.should.eql(Model.database); }); + it("should return an error if the input is not ok", done => { + callback = (handlerError) => { + handlerError.message.should.contain("BADREQUEST"); + done(); + }; + delete input.data.data; + const apple = new ApplesCreate(input, context); + apple.handler(input, context); + }); + describe("(permission)", () => { it("should set the permission needed", () => { handlerClass.actionContext.permission.should.equal("<%= modelNamePlural %>:create"); diff --git a/generators/functions/templates/spec/lambdas/models/modelsUpdate.spec.js b/generators/functions/templates/spec/lambdas/models/modelsUpdate.spec.js index cf0736c..8977294 100644 --- a/generators/functions/templates/spec/lambdas/models/modelsUpdate.spec.js +++ b/generators/functions/templates/spec/lambdas/models/modelsUpdate.spec.js @@ -5,7 +5,6 @@ import jsonApiModelFormatter from "jsonapi-model-formatter"; chai.should(); import <%= modelNamePascal %> from "../../../dist/lib/models/<%= modelName %>.js"; -import AccessToken from "../../../dist/lib/models/accessToken.js"; import <%= modelNamePluralPascal %>Update from "../../../dist/lib/lambdas/<%= modelNamePlural %>/<%= modelNamePlural %>Update.js"; import authenticate from "../../../dist/lib/steps/authenticate.js"; import authorize from "../../../dist/lib/steps/authorize.js"; @@ -17,6 +16,7 @@ describe("lambdas/<%= modelName %>Update.js", () => { let input; let handlerClass; let callback; + let context; let validAccessTokenParam; let salt; @@ -50,6 +50,16 @@ describe("lambdas/<%= modelName %>Update.js", () => { handlerClass.database.should.eql(Model.database); }); + it("should return an error if the input is not ok", done => { + callback = (handlerError) => { + handlerError.message.should.contain("BADREQUEST"); + done(); + }; + delete input.data.data; + const apple = new ApplesUpdate(input, context); + apple.handler(input, context); + }); + describe("(permission)", () => { it("should set the permission needed", () => { handlerClass.actionContext.permission.should.equal("<%= modelNamePlural %>:update"); diff --git a/index.js b/index.js deleted file mode 100644 index 620f2ce..0000000 --- a/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require("./es5/lib/app.index.js"); diff --git a/package.json b/package.json index 5a4a922..24c7f69 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "generator-troupe", "version": "0.1.2", "description": "Generator for FAM troupe.", - "main": "index.js", + "main": "./es5/lib/app.index.js", "scripts": { "test": "gulp test" },