├── .autod.conf.js ├── .eslintignore ├── .eslintrc ├── .github └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── History.md ├── LICENSE ├── README.md ├── app.js ├── app ├── extend │ ├── application.js │ └── context.js └── middleware │ └── leancloud.js ├── appveyor.yml ├── config └── config.default.js ├── package.json └── test ├── fixtures └── apps │ └── leancloud-test │ ├── app │ └── router.js │ ├── config │ └── config.default.js │ └── package.json └── leancloud.test.js /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | test: [ 7 | 'test', 8 | 'benchmark', 9 | ], 10 | devdep: [ 11 | 'egg', 12 | 'egg-ci', 13 | 'egg-bin', 14 | 'autod', 15 | 'eslint', 16 | 'eslint-config-egg', 17 | 'webstorm-disable-index', 18 | ], 19 | exclude: [ 20 | './test/fixtures', 21 | './docs', 22 | './coverage', 23 | ], 24 | }; 25 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg" 3 | } 4 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | ##### Checklist 12 | 13 | 14 | - [ ] `npm test` passes 15 | - [ ] tests and/or benchmarks are included 16 | - [ ] documentation is changed or added 17 | - [ ] commit message follows commit guidelines 18 | 19 | ##### Affected core subsystem(s) 20 | 21 | 22 | 23 | ##### Description of change 24 | 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run/ 7 | .DS_Store 8 | *.swp 9 | 10 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '6' 5 | - '7' 6 | install: 7 | - npm i npminstall && npminstall 8 | script: 9 | - npm run ci 10 | after_script: 11 | - npminstall codecov && codecov 12 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.0 / 2017-04-16 3 | ================== 4 | 5 | * feat: first impl (#1) 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-leancloud 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-leancloud.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-leancloud 12 | [travis-image]: https://img.shields.io/travis/eggjs/egg-leancloud.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/eggjs/egg-leancloud 14 | [codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-leancloud.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/eggjs/egg-leancloud?branch=master 16 | [david-image]: https://img.shields.io/david/eggjs/egg-leancloud.svg?style=flat-square 17 | [david-url]: https://david-dm.org/eggjs/egg-leancloud 18 | [snyk-image]: https://snyk.io/test/npm/egg-leancloud/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-leancloud 20 | [download-image]: https://img.shields.io/npm/dm/egg-leancloud.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-leancloud 22 | 23 | [LeanCloud](https://leancloud.cn/) plugin for [Egg.js](https://eggjs.org) 24 | 25 | > Maintainer Required. 26 | > If you are interested in picking up maintenance, file an issue at [eggjs/egg](https://github.com/eggjs/egg). 27 | 28 | ## Install 29 | 30 | ```bash 31 | $ npm i egg-leancloud --save 32 | ``` 33 | 34 | ## Usage 35 | 36 | ```js 37 | // {app_root}/config/plugin.js 38 | exports.leancloud = { 39 | enable: true, 40 | package: 'egg-leancloud', 41 | }; 42 | ``` 43 | 44 | ## Configuration 45 | 46 | ```js 47 | // {app_root}/config/config.default.js 48 | exports.leancloud = { 49 | appId: '', 50 | appKey: '', 51 | masterKey: '', 52 | }; 53 | ``` 54 | 55 | You can config this at `config.local.js` and leave `config.default.js` empty, then will auto read config from `process.env`. 56 | 57 | see [config/config.default.js](config/config.default.js) for more detail. 58 | 59 | ## Example 60 | 61 | app start point: 62 | 63 | ```js 64 | // {app_root}/app.js 65 | 66 | // adjust egg env by LeanCloud env 67 | if (!process.env.EGG_SERVER_ENV) { 68 | switch (process.env.LEANCLOUD_APP_ENV) { 69 | case 'production' : 70 | process.env.EGG_SERVER_ENV = 'prod'; 71 | break; 72 | case 'stage': 73 | process.env.EGG_SERVER_ENV = 'stage'; 74 | break; 75 | default: 76 | break; 77 | } 78 | } 79 | 80 | // start app 81 | require('egg').startCluster({ 82 | baseDir: __dirname, 83 | workers: process.env.LEANCLOUD_AVAILABLE_CPUS, 84 | port: process.env.LEANCLOUD_APP_PORT || process.env.PORT || 7001, 85 | }); 86 | ``` 87 | 88 | use LeanCloud api at controller: 89 | 90 | ```js 91 | // {app_root}/app/controller/news.js 92 | exports.list = function* (ctx) { 93 | const avQuery = new ctx.AV.Query('News'); 94 | ctx.body = yield avQuery.find(); 95 | }; 96 | ``` 97 | 98 | use LeanCloud api at start: 99 | 100 | ```js 101 | // {app_root}/app.js 102 | module.exports = app => { 103 | app.beforeStart(function* () { 104 | const avQuery = new ctx.AV.Query('User'); 105 | app.users = yield avQuery.find(); 106 | }); 107 | }; 108 | ``` 109 | 110 | ## Questions & Suggestions 111 | 112 | Please open an issue [here](https://github.com/eggjs/egg/issues). 113 | 114 | ## License 115 | 116 | [MIT](LICENSE) 117 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | app.config.coreMiddleware.push('leancloud'); 5 | }; 6 | -------------------------------------------------------------------------------- /app/extend/application.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const LEANCLOUD = Symbol('app#AV'); 4 | const AV = require('leanengine'); 5 | 6 | module.exports = { 7 | /** 8 | * LeanCloud Storage 9 | * @member {Object} Application#AV 10 | */ 11 | get AV() { 12 | if (!this[LEANCLOUD]) { 13 | AV.init(this.config.leancloud); 14 | this[LEANCLOUD] = AV; 15 | } 16 | return this[LEANCLOUD]; 17 | }, 18 | }; 19 | -------------------------------------------------------------------------------- /app/extend/context.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | /** 5 | * LeanCloud Storage 6 | * @member {Object} Context#AV 7 | */ 8 | get AV() { 9 | return this.app.AV; 10 | }, 11 | }; 12 | -------------------------------------------------------------------------------- /app/middleware/leancloud.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = (options, app) => { 4 | return app.AV.koa(); 5 | }; 6 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '6' 4 | - nodejs_version: '7' 5 | 6 | install: 7 | - ps: Install-Product node $env:nodejs_version 8 | - npm i npminstall && node_modules\.bin\npminstall 9 | 10 | test_script: 11 | - node --version 12 | - npm --version 13 | - npm run ci 14 | 15 | build: off 16 | -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * LeanCloud default config, will read from ENV as default. 5 | * @member Config#leancloud 6 | * @property {String} appId - application id 7 | * @property {String} appKey - application keyd 8 | * @property {String} masterKey - application master key 9 | * @property {String} region - default to china 10 | */ 11 | exports.leancloud = { 12 | appId: process.env.LEANCLOUD_APP_ID, 13 | appKey: process.env.LEANCLOUD_APP_KEY, 14 | masterKey: process.env.LEANCLOUD_APP_MASTER_KEY, 15 | }; 16 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-leancloud", 3 | "version": "1.0.0", 4 | "description": "leancloud plugin for eggjs", 5 | "eggPlugin": { 6 | "name": "leancloud" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "eggPlugin", 11 | "egg-plugin", 12 | "leancloud" 13 | ], 14 | "dependencies": { 15 | "leanengine": "^2.0.1" 16 | }, 17 | "devDependencies": { 18 | "autod": "^2.7.1", 19 | "egg": "^1.0.0", 20 | "egg-bin": "^3.1.0", 21 | "egg-ci": "^1.5.0", 22 | "egg-mock": "^3.2.0", 23 | "eslint": "^3.18.0", 24 | "eslint-config-egg": "^3.2.0", 25 | "webstorm-disable-index": "^1.1.2" 26 | }, 27 | "engines": { 28 | "node": ">=6.0.0" 29 | }, 30 | "scripts": { 31 | "test": "npm run lint -- --fix && npm run test-local", 32 | "test-local": "egg-bin test", 33 | "cov": "egg-bin cov", 34 | "lint": "eslint .", 35 | "ci": "npm run lint && npm run cov", 36 | "autod": "autod" 37 | }, 38 | "files": [ 39 | "app.js", 40 | "config", 41 | "app" 42 | ], 43 | "ci": { 44 | "version": "6, 7" 45 | }, 46 | "repository": { 47 | "type": "git", 48 | "url": "git+https://github.com/eggjs/egg-leancloud.git" 49 | }, 50 | "bugs": { 51 | "url": "https://github.com/eggjs/egg/issues" 52 | }, 53 | "homepage": "https://github.com/eggjs/egg-leancloud#readme", 54 | "author": "TZ ", 55 | "license": "MIT" 56 | } 57 | -------------------------------------------------------------------------------- /test/fixtures/apps/leancloud-test/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | app.get('/news', function* () { 5 | const avQuery = new this.AV.Query('News'); 6 | this.body = yield avQuery.get(this.query.id); 7 | }); 8 | 9 | app.get('/news/create', function* () { 10 | const Device = app.AV.Object.extend('News'); 11 | const device = new Device(); 12 | device.set('topic', this.query.topic); 13 | this.body = yield device.save(); 14 | }); 15 | }; 16 | -------------------------------------------------------------------------------- /test/fixtures/apps/leancloud-test/config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.leancloud = { 6 | appId: 'OO3ToHOVTR6kOVaEIPS1EpMC-gzGzoHsz', 7 | appKey: 'CPTPqmvQnLRw5gTm9yBPD7d7', 8 | masterKey: 'WMRMrgeuplGYJJqhx5cwDlvM', 9 | }; 10 | -------------------------------------------------------------------------------- /test/fixtures/apps/leancloud-test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "leancloud-test", 3 | "version": "0.0.1" 4 | } -------------------------------------------------------------------------------- /test/leancloud.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const mm = require('egg-mock'); 4 | const assert = require('assert'); 5 | 6 | describe('test/leancloud.test.js', () => { 7 | let app; 8 | before(() => { 9 | app = mm.app({ 10 | baseDir: 'apps/leancloud-test', 11 | }); 12 | return app.ready(); 13 | }); 14 | 15 | after(() => app.close()); 16 | afterEach(mm.restore); 17 | 18 | it('should GET /news', function* () { 19 | const topic = `topic_${Date.now()}`; 20 | const queryResponse = yield app.httpRequest() 21 | .get(`/news/create?topic=${topic}`) 22 | .accept('json') 23 | .expect(200); 24 | 25 | assert(queryResponse.body.topic === topic); 26 | assert(queryResponse.body.objectId); 27 | 28 | const response = yield app.httpRequest() 29 | .get(`/news?id=${queryResponse.body.objectId}`) 30 | .accept('json') 31 | .expect(200); 32 | assert(response.body.topic === topic); 33 | }); 34 | }); 35 | --------------------------------------------------------------------------------