klark-js-plugins
v1.0.32
Published
Plugin modules for KlarkJS
Downloads
14
Readme
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.
- Install plugins
- Example
- Plugins
- krkCrudGenerator
- krkCrudGeneratorUrls
- krkDbMongooseBinders
- krkDbMongooseConnector
- krkDbMongoosePluginsPassword
- krkErrors
- krkGeneratorsCreateUser
- krkGeneratorsLogin
- krkLogger
- krkMiddlewareCrudController
- krkMiddlewareInitiateResponseParams
- krkMiddlewareParameterValidator
- krkMiddlewarePermissionsAuthorizeStrategy
- krkMiddlewarePermissions
- krkMiddlewarePermissionsRoles
- krkMiddlewareResponse
- krkModelsApp
- krkModelsUser
- krkNotificationsEmail
- krkParameterValidator
- krkPromiseExtension
- krkRouter
- krkRoutesAuthorize
- krkRoutersAuthorizeVerifyAccountEmailTmpl
- krkRoutesMultimedia
- krkRoutesServerInfo
- krkRoutesUsers
- krkServer
- krkUtilitiesDate
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`;
// locate the klark plugins inside the node_modules folder
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`;
// locate only the necessary klark plugins inside the node_modules folder
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 = // express app
krkCrudGenerator(app, todoModel, {
apiUrlPrefix: 'v1'
});
});
Unauthorized creation
Request
POST http://.../v1/todo
{
"content": "hi"
}
Response
401
{
"code": 4001,
"msg": "unauthorized user"
}
Authorized creation, invalid arguments
Request
HEADER Authorization: JWT ...
POST http://.../v1/todo
{
"content": "hiiiiiiiiiiiiiiii"
}
Response
400
{
"code": 1003,
"msg": "invalid params, 'content' length"
}
Create a custom CRUD api
KlarkModule(module, 'createCustomTodoApi', function(krkCrudGenerator) {
var app = // express app
app.get(crudUrls.retrieveAll('Application'), [
// everybody can access this route
krkMiddlewarePermissions.check('FREE'),
// check and sanitize all the necessary arguments like page, count etc
krkMiddlewareParameterValidator.crud.retrieveAll(modelsApplication),
// retrieves the records from the MongoDB
middlewareRetrieveAllController,
// response
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
krkUtilitiesDate
Name: krkUtilitiesDate
Path: /plugins/utilities/date.module.js