├── examples └── test.js ├── README.md ├── .gitattributes ├── .idea └── koa2-request.iml ├── package.json ├── .gitignore └── index.js /examples/test.js: -------------------------------------------------------------------------------- 1 | var koa = require('koa'); 2 | var request = require('../index'); //koa2-request 3 | 4 | var app = new koa(); 5 | 6 | app.use(async(ctx, next) => { 7 | // request选项 8 | var res = await request('http://www.baidu.com'); 9 | ctx.body = res.body; 10 | }); 11 | 12 | app.listen(8080, () => { 13 | console.log('server is listener on port 8080') 14 | }); -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | koa2-request 2 | =========== 3 | 4 | > koa2的request库封装,支持async和await写法 5 | 6 | ## 安装 7 | 8 | ```bash 9 | npm install koa2-request 10 | ``` 11 | 12 | ## 使用方法 13 | 14 | ```javascript 15 | 16 | var koa2Req = require('koa2-request'); 17 | 18 | app.use(async(ctx, next) => { 19 | // request选项 20 | var res = await koa2Req('http://www.baidu.com'); 21 | ctx.body = res.body; 22 | }); 23 | ``` -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | 7 | # Standard to msysgit 8 | *.doc diff=astextplain 9 | *.DOC diff=astextplain 10 | *.docx diff=astextplain 11 | *.DOCX diff=astextplain 12 | *.dot diff=astextplain 13 | *.DOT diff=astextplain 14 | *.pdf diff=astextplain 15 | *.PDF diff=astextplain 16 | *.rtf diff=astextplain 17 | *.RTF diff=astextplain 18 | -------------------------------------------------------------------------------- /.idea/koa2-request.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "koa2-request", 3 | "version": "1.0.4", 4 | "description": "support es7 async await", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "./example/example.js" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/KELEN/koa2-request.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/KELEN/koa2-request/issues" 15 | }, 16 | "homepage": "https://github.com/KELEN/koa2-request#readme", 17 | "keywords": [ 18 | "koa2-request", 19 | "request", 20 | "es7", 21 | "async", 22 | "await" 23 | ], 24 | "author": { 25 | "name": "kelen", 26 | "email": "340443366@qq.com" 27 | }, 28 | "license": "ISC", 29 | "dependencies": { 30 | "request": "^2.83.0" 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Windows image file caches 2 | Thumbs.db 3 | ehthumbs.db 4 | 5 | # Folder config file 6 | Desktop.ini 7 | 8 | # Recycle Bin used on file shares 9 | $RECYCLE.BIN/ 10 | 11 | # Windows Installer files 12 | *.cab 13 | *.msi 14 | *.msm 15 | *.msp 16 | 17 | # Windows shortcuts 18 | *.lnk 19 | 20 | # ========================= 21 | # Operating System Files 22 | # ========================= 23 | 24 | # OSX 25 | # ========================= 26 | 27 | .DS_Store 28 | .AppleDouble 29 | .LSOverride 30 | 31 | # Thumbnails 32 | ._* 33 | 34 | # Files that might appear in the root of a volume 35 | .DocumentRevisions-V100 36 | .fseventsd 37 | .Spotlight-V100 38 | .TemporaryItems 39 | .Trashes 40 | .VolumeIcon.icns 41 | 42 | # Directories potentially created on remote AFP share 43 | .AppleDB 44 | .AppleDesktop 45 | Network Trash Folder 46 | Temporary Items 47 | .apdisk 48 | 49 | .idea/*.xml 50 | node_modules 51 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* 2 | Simple wrapper to request library 3 | http://github.com/mikeal/request, depend on request library 4 | For use in Koa2. 5 | */ 6 | 7 | var _request = require('request'); 8 | 9 | function request(uri, options) { 10 | return new Promise(function (resolve, reject) { 11 | _request(uri, options, function (error, response, body) { 12 | error && reject(error); 13 | resolve(response, body); 14 | }) 15 | }) 16 | } 17 | 18 | // 覆盖request请求方法 19 | for (var attr in _request) { 20 | if (_request.hasOwnProperty(attr)) { 21 | if (['get', 'post', 'put', 'patch', 'head', 'del'].indexOf(attr) > -1) { 22 | request[attr] = (function (attr) { 23 | return function (uri, options) { 24 | return new Promise(function (resolve, reject) { 25 | _request(uri, options, function (error, response, body) { 26 | error && reject(error); 27 | resolve(response, body); 28 | }) 29 | }) 30 | } 31 | })(attr); 32 | } else { 33 | request[attr] = _request[attr]; 34 | } 35 | } 36 | } 37 | 38 | module.exports = request; --------------------------------------------------------------------------------