klark plugins
Plugin modules for KlarkJS
We are trying to create an ecosystem of utilities and functionalities that improve dramatically the automation of creating a robust API in NodeJS, ExpressJS and KlarkJS.
The main architecture that came up when we integrate a NodeJS API is the following:
- Receive request
- Check permissions
- Check Parameters
- Execute the controller
- Response
Hopefully, express js is organized in middlewares.
We created a collection of middlewares and utilities in order to automate the CRUD functionality. The CRUD model is based on the Mongoose models.
Install plugins
- In the root project folder install klark-js-plugins
npm install --save klark-js-plugins
- Open the file that contains the Klark registration code.
var modules = `plugins/**/index.js`;
var subModules = `plugins/**/*.module.js`;
var klarkPlugins = `node_modules/klark-js-plugins/plugins/**/*.js`;
klark.run({
predicateFilePicker: function() {
return [
modules,
subModules,
klarkPlugins
]
}
});
Notice
If you want to include a subset of the plugins, you should include only the corresponding internal dependencies.
Let's assume that we only want to use the generators/create-user.module.js
. Our configuration should look like this:
var modules = `plugins/**/index.js`;
var subModules = `plugins/**/*.module.js`;
var klarkPlugins = [
`node_modules/klark-js-plugins/plugins/generators/create-user.module.js`,
`node_modules/klark-js-plugins/plugins/models/user/index.js`
];
klark.run({
predicateFilePicker: function() {
return [
modules,
subModules,
klarkPlugins
]
}
});
Example
Considering the following mongoose model:
var todoSchema = new $mongoose.Schema({
content: {type: String, maxlength: [16], required: true}
});
var todoModel = $mongoose.model('Todo', schema);
Create a simple CRUD api
krkCrudGenerator creates and registers the CRUD functionality for a mongoose model.
KlarkModule(module, 'createSimpleTodoApi', function(krkCrudGenerator) {
var app =
krkCrudGenerator(app, todoModel, {
apiUrlPrefix: 'v1'
});
});
Unauthorized creation
Request
POST http:
{
"content": "hi"
}
Response
401
{
"code": 4001,
"msg": "unauthorized user"
}
Authorized creation, invalid arguments
Request
HEADER Authorization: JWT ...
POST http:
{
"content": "hiiiiiiiiiiiiiiii"
}
Response
400
{
"code": 1003,
"msg": "invalid params, 'content' length"
}
Create a custom CRUD api
KlarkModule(module, 'createCustomTodoApi', function(krkCrudGenerator) {
var app =
app.get(crudUrls.retrieveAll('Application'), [
krkMiddlewarePermissions.check('FREE'),
krkMiddlewareParameterValidator.crud.retrieveAll(modelsApplication),
middlewareRetrieveAllController,
krkMiddlewareResponse.success
]);
});
Plugins
krkCrudGenerator
Name: krkCrudGenerator
Path: /plugins/crud-generator/index.js
Dependencies: lodash, krkMiddlewareParameterValidator, krkMiddlewarePermissions, krkMiddlewareCrudController, krkMiddlewareResponse, krkCrudGeneratorUrls
krkCrudGeneratorUrls
Name: krkCrudGeneratorUrls
Path: /plugins/crud-generator/urls.module.js
krkDbMongooseBinders
Name: krkDbMongooseBinders
Path: /plugins/db/mongoose-binders/index.js
Dependencies: lodash, mongoose, krkLogger, krkModelsApp
krkDbMongooseConnector
Name: krkDbMongooseConnector
Path: /plugins/db/mongoose-connector/index.js
Dependencies: q, mongoose, krkLogger
krkDbMongoosePluginsPassword
Name: krkDbMongoosePluginsPassword
Path: /plugins/db/mongoose-plugins/password.module.js
Dependencies: lodash, q, bcrypt, krkLogger
krkErrors
Name: krkErrors
Path: /plugins/errors/index.js
Dependencies: lodash, krkLogger
krkGeneratorsCreateUser
Name: krkGeneratorsCreateUser
Path: /plugins/generators/create-user.module.js
Dependencies: mongoose, krkModelsUser
krkGeneratorsLogin
Name: krkGeneratorsLogin
Path: /plugins/generators/login.module.js
Dependencies: krkMiddlewarePermissions
krkLogger
Name: krkLogger
Path: /plugins/logger/index.js
krkMiddlewareCrudController
Name: krkMiddlewareCrudController
Path: /plugins/middleware/crud-controller/index.js
Dependencies: lodash, q, krkDbMongooseBinders
krkMiddlewareInitiateResponseParams
Name: krkMiddlewareInitiateResponseParams
Path: /plugins/middleware/initiate-response-params/index.js
Dependencies: lodash, krkLogger, krkErrors
krkMiddlewareParameterValidator
Name: krkMiddlewareParameterValidator
Path: /plugins/middleware/parameter-validator/index.js
Dependencies: q, lodash, krkParameterValidator
krkMiddlewarePermissionsAuthorizeStrategy
Name: krkMiddlewarePermissionsAuthorizeStrategy
Path: /plugins/middleware/permissions/authorize-strategy.module.js
Dependencies: lodash, passport-jwt, krkModelsUser
krkMiddlewarePermissions
Name: krkMiddlewarePermissions
Path: /plugins/middleware/permissions/index.js
Dependencies: lodash, passport, jwt-simple, krkLogger, krkMiddlewarePermissionsRoles
krkMiddlewarePermissionsRoles
Name: krkMiddlewarePermissionsRoles
Path: /plugins/middleware/permissions/roles.module.js
krkMiddlewareResponse
Name: krkMiddlewareResponse
Path: /plugins/middleware/response/index.js
Dependencies: lodash
krkModelsApp
Name: krkModelsApp
Path: /plugins/models/app/index.js
Dependencies: mongoose
krkModelsUser
Name: krkModelsUser
Path: /plugins/models/user/index.js
Dependencies: lodash, q, mongoose, mongoose-type-email, mongoose-createdmodified, krkMiddlewarePermissionsRoles, krkDbMongoosePluginsPassword
krkNotificationsEmail
Name: krkNotificationsEmail
Path: /plugins/notifications/email.module.js
Dependencies: lodash, q, nodemailer
krkParameterValidator
Name: krkParameterValidator
Path: /plugins/parameter-validator/index.js
Dependencies: q, lodash, express-validator
krkPromiseExtension
Name: krkPromiseExtension
Path: /plugins/promise-extension/index.js
Dependencies: lodash
krkRouter
Name: krkRouter
Path: /plugins/router/index.js
Dependencies: express
krkRoutesAuthorize
Name: krkRoutesAuthorize
Path: /plugins/routers/authorize/index.js
Dependencies: lodash, q, crypto, krkLogger, krkDbMongooseBinders, krkRoutersAuthorizeVerifyAccountEmailTmpl, krkNotificationsEmail, krkMiddlewareResponse, krkParameterValidator, krkMiddlewarePermissions, krkModelsUser
krkRoutersAuthorizeVerifyAccountEmailTmpl
Name: krkRoutersAuthorizeVerifyAccountEmailTmpl
Path: /plugins/routers/authorize/verify-account-email-tmpl.module.js
Dependencies: config
krkRoutesMultimedia
Name: krkRoutesMultimedia
Path: /plugins/routers/multimedia/index.js
Dependencies: q, lodash, fs, multer, crypto, mkdirp, krkMiddlewarePermissions, krkMiddlewareResponse
krkRoutesServerInfo
Name: krkRoutesServerInfo
Path: /plugins/routers/server-info/index.js
Dependencies: krkMiddlewareResponse
krkRoutesUsers
Name: krkRoutesUsers
Path: /plugins/routers/users/index.js
Dependencies: crypto, q, lodash, krkModelsUser, krkRoutersAuthorizeVerifyAccountEmailTmpl, krkParameterValidator, krkNotificationsEmail, krkMiddlewareResponse, krkMiddlewareCrudController, krkMiddlewarePermissions
krkServer
Name: krkServer
Path: /plugins/server/index.js
Dependencies: http, krkLogger
krkUtilitiesDate
Name: krkUtilitiesDate
Path: /plugins/utilities/date.module.js
Dependencies: lodash, moment