# medigo-server-kit

> Server Kit for Medigo App

Latest version **0.1.10** (published 2023-07-05) · ISC license · 0 weekly downloads

## Install

```sh
npm install medigo-server-kit
pnpm add medigo-server-kit
yarn add medigo-server-kit
bun add medigo-server-kit
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.10 |
| Published | 2023-07-05 |
| First published | 2018-11-02 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 24 |
| Unpacked size | 67.8 KB |
| Known vulnerabilities | 0 (+35 in 5 direct dependencies) |
| Install scripts | no |
| Author | denmasyarikin |
| Maintainers | denmasyarikin, agungroyat, aqid |
| Keywords | medigo, server, kit |

## Links

- npm: https://www.npmjs.com/package/medigo-server-kit
- Repository: https://dedensaka@bitbucket.org/medigoid/medigo-server-kit
- Homepage: https://bitbucket.org/medigoid/medigo-server-kit#readme
- Issues: https://bitbucket.org/medigoid/medigo-server-kit/issues
- npm.io page: https://npm.io/package/medigo-server-kit

## Dependencies (24)

- [pg](https://npm.io/package/pg.md) ^7.14.0
- [npm](https://npm.io/package/npm.md) ^6.13.3
- [cors](https://npm.io/package/cors.md) ^2.8.5
- [uuid](https://npm.io/package/uuid.md) ^3.3.3
- [axios](https://npm.io/package/axios.md) ^0.19.0
- [debug](https://npm.io/package/debug.md) ^4.3.4
- [redis](https://npm.io/package/redis.md) ^2.8.0
- [agenda](https://npm.io/package/agenda.md) ^2.2.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [moment](https://npm.io/package/moment.md) ^2.24.0
- [morgan](https://npm.io/package/morgan.md) ^1.10.0
- [express](https://npm.io/package/express.md) ^4.18.2
- [hashids](https://npm.io/package/hashids.md) ^2.2.10
- [agendash](https://npm.io/package/agendash.md) ^1.0.0
- [bcryptjs](https://npm.io/package/bcryptjs.md) ^2.4.3
- [mongoose](https://npm.io/package/mongoose.md) ^5.8.0
- [@hapi/joi](https://npm.io/package/@hapi/joi.md) ^15.1.1
- [sequelize](https://npm.io/package/sequelize.md) ^5.21.2
- [body-parser](https://npm.io/package/body-parser.md) ^1.20.1
- [@sentry/node](https://npm.io/package/@sentry/node.md) ^7.19.0
- [@sentry/tracing](https://npm.io/package/@sentry/tracing.md) ^7.19.0
- [@godaddy/terminus](https://npm.io/package/@godaddy/terminus.md) ^4.11.2
- [express-redis-cache](https://npm.io/package/express-redis-cache.md) ^1.1.3
- [express-bearer-token](https://npm.io/package/express-bearer-token.md) ^2.4.0

## Alternatives

- [@sveltejs/kit](https://npm.io/package/@sveltejs/kit.md) — 2.2M weekly downloads
- [@atlaskit/theme](https://npm.io/package/@atlaskit/theme.md) — 402.0K weekly downloads
- [@tangle-network/brand](https://npm.io/package/@tangle-network/brand.md) — 10.0K weekly downloads
- [seneca](https://npm.io/package/seneca.md) — 7.4K weekly downloads
- [@bsb/base](https://npm.io/package/@bsb/base.md) — 7.2K weekly downloads

## Recent versions

- 0.1.10 (latest) — 2023-07-05
- 1.0.0 — 2023-05-04
- 0.1.9 — 2022-11-15
- 0.1.7 — 2021-03-30
- 0.1.6 — 2021-03-29
- 0.1.5 — 2021-03-29
- 0.1.3 — 2019-09-10
- 0.1.2 — 2019-09-10
- 0.1.1 — 2019-08-28
- 0.1.0 — 2019-08-28
- 0.0.1-alpha.1 — 2019-08-22
- 0.0.1-alpha — 2019-08-14
- 0.0.1-rc.65 — 2019-07-17
- 0.0.1-rc.64 — 2019-06-29
- 0.0.1-rc.62 — 2019-06-25
- … 62 more at https://npm.io/package/medigo-server-kit/versions

## README

# [MEDIGO] Server KIT

[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) [![jest](https://jestjs.io/img/jest-badge.svg)](https://github.com/facebook/jest)

----------

## Getting started

----------

### Dependencies

- [Express JS](https://www.npmjs.com/package/express) wrapper for Medigo service that using NodeJS environment.
- [Mongoose](https://www.npmjs.com/package/mongoose)
- [Sequelize](https://www.npmjs.com/package/sequelize)
- [jest](https://github.com/facebook/jest) - For testing the application 

### Installation
install via NPM Package Manager
```
npm install medigo-server-kit
```
### Features

- Default Middleware
- Router
- Database (Mongoose, Sequelize)
- Authentication And Authorization
- Service Connection
- Error And Handler

### Usege
``` javascript
const ServerKit = require('medigo-server-kit')

const server = new ServerKit({
  name: 'Medigo Service Name',
  port: 8080
})

server.run()
```

----------

### Default Middleware Dependencies
This package comes with several middleware installed, but you can also add another middleware to the app. The default middlewares used are:

- [Body Parser](https://www.npmjs.com/package/body-parser)
- [CORS](https://www.npmjs.com/package/cors)
- [Express Bearer Token](https://www.npmjs.com/package/express-bearer-token) 
- [Morgan](https://www.npmjs.com/package/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)
```

----------

### Router
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()
```
----------

### Database (Mongoose, Sequelize)
This package comes with [Mongoose](https://www.npmjs.com/package/mongoose) and [Sequelize](https://www.npmjs.com/package/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-id`

Example 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
  }
}
```

----------

### Testing Directory

- `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

### Testing Application

Available tests command:

```
npm run test
```

----------

---
_Source: https://npm.io/package/medigo-server-kit · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
