Server Kit for Medigo App
 
----------
----------
- Express JS wrapper for Medigo service that using NodeJS environment.
- Mongoose
- Sequelize
- jest - For testing the application
npm install medigo-server-kit
`
$3
- Default Middleware
- Router
- Database (Mongoose, Sequelize)
- Authentication And Authorization
- Service Connection
- Error And Handler
$3
` javascript
const ServerKit = require('medigo-server-kit')const server = new ServerKit({
name: 'Medigo Service Name',
port: 8080
})
server.run()
`----------
$3
This package comes with several middleware installed, but you can also add another middleware to the app. The default middlewares used are:- Body Parser
- CORS
- Express Bearer Token
- Morgan
You can add middleware to the app with following:
` javascript
...
const log = (req, res, next) => {
console.log('hello middleware.')
next()
}server.middleware(log)
`
Or you can access directly to the express app
` javascript
server.app.use(log)
`----------
$3
You can use express router `express.Router` as example below:
` javascript
// router.js
module.exports = router => {
router.get('/hello-medigo', (req, res, next) => {
res.json({ message: 'hello too.' })
})
return router
}// server.js
server.router(require('./router.js'))
server.run()
`
----------$3
This package comes with Mongoose and Sequelize ORM. Both databases are using URI Connection in configuration.
` javascript
const server = new ServerKit({
name: 'Medigo Service Name',
port: 8080,
database: 'mongoose',
connection: 'mongodb://{username}:{password}@localhost:27017/{name}'
})// We recommend to setup database first before running the server
server.setUpDatabase().then(() => {
server.run()
})
`Both database ORM should use our db instance
#### - Mongoose
`javascript
// user.js model of user
const mongoose = require('medigo-server-kit/database/mongoose').db
const Schema = require('medigo-server-kit/database/MongooseSchema')// When using our schema, it will add timestamp, transform _id to id,
// and force _id field type to string instead of ObjectId
let userSchema = new Schema({
username: { type: String, required: true },
password: { type: String, required: true }
})
let UserModel = mongoose.model('User', userSchema)
UserModel.find({})
`
#### - Sequelize
If the service uses sequelize, you should install sequelize via npm to the app, because currently we don't provide schema for sequelize (will be provided later) and using this is still complicated. We will also update this documentation later.----------
Authentication And Authorization
This feature is currently under development, for a while, we provide simple Authorization like below:
`javascript
const c = require('controllers')
const authorize = require('medigo-server-kit/security/authorize')module.exports = router => {
router.get('/doctor', authorize('adminGroupHC', 'adminHC'), c.doctor.getListDoctor)
return router
}
`
Service Connection
To connect to another service around medigo services, this package also provide client connector for request. The service connectors available are:- Auth
- User
- HealthCenter
- Doctor
- SuperAdmin
- Reservation
- Schedule
- Payment
- Notification
- Location
- Storage
- Middleware
- MasterData
#### - Service Client
Example of usage
`javascript
...
const UserService = require('medigo-server-kit/services/User')async getUserList (req, res, next) {
// req param is important to know where the request comes from,
// who is the requesting user, and many more.
let userClient = new UserService(req)
let data = await userClient.get('/user')
res.json(data)
}
...
`
#### - Service Connection
You can get access to current state of the request (such as authenticated user, healthCenter of the user, medigo-client and more by using Connection Class, Available informations are: - getClient
object of id and name of medigo-client
- getClientId x-medigo-client-id
- getServiceName which service that is requesting
- getClientName x-medigo-client-name
- getUser object of current authenticated user
- getHealthCenter current healthcenter of user
- getUserId x-medigo-user-id
- getHealthCenterId x-medigo-healthcenter-idExample of usage
` javascript
const ServiceConnection = require('medigo-server-kit/services/Connection')module.exports = {
getCurrentUser (req, res, next) {
let SC = new ServiceConnection(req)
// will return null if not exists
res.json({ data: SC.getUser() })
}
}
`----------
Error and Handler
We also provide error Classes that are automatically handled by the app.- Http
- Server
- Service
- BadRequest
- Forbidden
- NotFound
- Unauthentication
- Validation
- Validations
Example of usage
`javascript
const Error = require('medigo-server-kit/error')async getUserList (req, res, next) {
try {
if (false) {
throw new Error.BadRequest('Something\'s wrong in your request.')
}
} catch (error) {
next(error) // important to next for error handler
}
}
`----------
$3
-
tests - Contains all the application tests
- tests/__mocks__ - Subdirectory for mocks module are defined immediately adjacent to the module
- tests/__tests__ - Contains all the scenario tests$3
Available tests command:
`
npm run test
``----------