# egg-common-modules

> This is a collection for some common modules.

Latest version **1.1.62** (published 2018-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install egg-common-modules
pnpm add egg-common-modules
yarn add egg-common-modules
bun add egg-common-modules
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.62 |
| Published | 2018-09-12 |
| First published | 2018-05-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 6 |
| Unpacked size | 52.3 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| Author | Chris |
| Maintainers | xxddqq123 |
| Keywords | egg, eggPlugin, egg-plugin |

## Links

- npm: https://www.npmjs.com/package/egg-common-modules
- Repository: https://github.com/eggjs/egg-common-modules
- Homepage: https://github.com/eggjs/egg-common-modules#readme
- Issues: https://github.com/eggjs/egg/issues
- npm.io page: https://npm.io/package/egg-common-modules

## Dependencies (6)

- [ip](https://npm.io/package/ip.md) ^1.1.5
- [uuid](https://npm.io/package/uuid.md) ^3.3.2
- [consul](https://npm.io/package/consul.md) ^0.34.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.10
- [parameter](https://npm.io/package/parameter.md) 2.2.3
- [memory-cache](https://npm.io/package/memory-cache.md) ^0.2.0

## Recent versions

- 1.1.62 (latest) — 2018-09-12
- 1.1.61 — 2018-09-12
- 1.0.33 — 2018-05-03

## README

# egg-common-modules

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/egg-common-modules.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-common-modules
[travis-image]: https://img.shields.io/travis/eggjs/egg-common-modules.svg?style=flat-square
[travis-url]: https://travis-ci.org/eggjs/egg-common-modules
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-common-modules.svg?style=flat-square
[codecov-url]: https://codecov.io/github/eggjs/egg-common-modules?branch=master
[david-image]: https://img.shields.io/david/eggjs/egg-common-modules.svg?style=flat-square
[david-url]: https://david-dm.org/eggjs/egg-common-modules
[snyk-image]: https://snyk.io/test/npm/egg-common-modules/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-common-modules
[download-image]: https://img.shields.io/npm/dm/egg-common-modules.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-common-modules

## Introduction

It includes middlewares, utils, basis for service/controller, etc.

##### Middlewares:

1. errorHandler: It will handle all errors thrown from the application and convert to a common http response.
2. retrieveCookie: It will retrieve the cookie from the request header and set it to field - cookie. <br/><br/>e.g. `const cookie = ctx.request.cookie;`

##### Utils:

1. sign: It includes md5 and suffix function.
2. decorator function for egg-mysql transaction mode.

##### Exception:

This a common error type.<br/><br/>`Error { code: 1, msg: "通用错误" }`

##### Basis:

1. BasicService: It is a basic service for the application.
2. BasicController: It is a basic controller for the application.

## Install

```bash
$ npm i egg-common-modules --save
```

## Usage

```js
// {app_root}/config/plugin.js
exports.commonModules = {
    enable: true,
    package: 'egg-common-modules'
};
```

## Configuration

```js
// {app_root}/config/config.default.js
exports.commonModules = {};
```

see [config/config.default.js](config/config.default.js) for more detail.

## Example

```js
// {app_root}/config/config.default.js
exports.commonModules = {};

// {app_root}/app/controller/mainController.js
const { BasicController } = require('egg-common-modules').basis;

class mainController extends BasicController {
    async checkIfGotThePrize() {
        const { ctx } = this;
        const result = await ctx.service.mainService.checkIfGotThePrize();
        this.success(result);
    }
}

module.exports = mainController;

// {app_root}/app/service/mainService.js
const { BasicService } = require('egg-common-modules').basis;
const Exception = require('egg-common-modules').exception;

class mainService extends BasicService {
    async checkIfGotThePrize() {
        const result = await this.calPercentage();
        return result;
    }

    @transaction // this will make the database operations be in transaction mode
    async calPercentage() {
        const { ctx } = this;
        if (!ctx.userid) throw new Exception({ code: 1, msg: '未登录' });
        const user = await ctx.service.userService.getUser(ctx.userid);
        if (user.isPrime) {
            const rn = Math.random();
            if (rn > 0.5) return true;
        }
        return false;
    }
}

module.exports = mainService;
```

## Questions & Suggestions

Please open an issue [here](https://github.com/eggjs/egg/issues).

## Author

Chris

## License

[MIT](LICENSE)

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