├── .circleci └── config.yml ├── .editorconfig ├── .eslintignore ├── .eslintrc.yaml ├── .gitignore ├── LICENSE ├── README.md ├── docs ├── guide │ ├── README.md │ └── index.html ├── images │ └── logo.svg ├── index.html └── intro │ ├── README.md │ └── index.html ├── examples ├── restc-example-express │ ├── index.js │ ├── package-lock.json │ └── package.json ├── restc-example-hapi │ ├── latest │ │ ├── index.js │ │ └── package.json │ └── legacy │ │ ├── index.js │ │ └── package.json ├── restc-example-koa │ ├── index.js │ ├── package-lock.json │ └── package.json ├── restc-example-koa2-inline │ ├── gateway.js │ ├── index.js │ ├── package-lock.json │ └── package.json └── restc-example-koa2 │ ├── gateway.js │ ├── index.js │ ├── package-lock.json │ └── package.json ├── index.d.ts ├── index.js ├── lib ├── express.js ├── hapi.js ├── hapiLegacy.js ├── index.js ├── koa.js ├── koa2.js └── utils │ └── gateway.js ├── package-lock.json ├── package.json ├── scripts └── inline.php ├── src ├── index.html └── index.txt └── test └── gateway.js /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | jobs: 3 | build: 4 | docker: 5 | - image: circleci/node:10 6 | steps: 7 | - checkout 8 | - run: 9 | name: install-dep 10 | command: npm i 11 | - save_cache: 12 | key: dependency-cache-{{ checksum "package-lock.json" }} 13 | paths: 14 | - ./node_modules 15 | - run: 16 | name: lint 17 | command: npm run lint 18 | - run: 19 | name: test 20 | command: npm t 21 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 2 6 | trim_trailing_whitespace = true 7 | insert_final_newline = true 8 | 9 | [Makefile] 10 | indent_style = tab 11 | 12 | [*.md] 13 | indent_size = 4 14 | trim_trailing_whitespace = false 15 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.eslintrc.yaml: -------------------------------------------------------------------------------- 1 | --- 2 | env: 3 | node: true 4 | extends: standard 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .* 2 | node_modules 3 | dist 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 ElemeFE 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 |

2 | restc 3 |

4 | 5 |

6 | CircleCI (all branches) 7 |

8 | 9 |

10 | 中文文档 11 |

12 | 13 | ## Introduction 14 | 15 | restc is an HTTP server middleware, aiming to enhance debugging experience on RESTful APIs. 16 | 17 | It can be easily integrated with popular HTTP frameworks. You will see: 18 | 19 | - formatted JSON responses in the browser 20 | - a debug panel with which you can send GET, POST, PUT, PATCH and DELETE requests directly in the browser 21 | 22 | You can even share a request by sharing its URL directly to others and everything will be automatically filled in the panel. 23 | 24 | ## Getting Started 25 | 26 | npm install --save restc 27 | 28 | Use the middleware 29 | 30 | ```js 31 | const restc = require('restc'); 32 | // for express 33 | app.use(restc.express()); 34 | // for koa 35 | app.use(restc.koa()); 36 | // ...and koa2 37 | app.use(restc.koa2()); 38 | // for hapi 39 | server.register(restc.hapi) 40 | // for hapi of legacy version 41 | server.register([{ 42 | register: restc.hapiLegacy 43 | }], (err) => { 44 | if (err) { 45 | throw err 46 | } 47 | }) 48 | ``` 49 | -------------------------------------------------------------------------------- /docs/guide/README.md: -------------------------------------------------------------------------------- 1 | ## 1. 在 HTTP 框架中引入 2 | 3 | 我们为 Node.js 中主流的 HTTP 框架——Express 和 Koa 编写了中间件,只需要添加两行代码即可引入 restc。 4 | 5 | ### 1.1. 安装 restc 6 | 7 | 如果你选择通过配置 Nginx 来引入 restc,那么这一步是可选的,请直接跳到 [在 Nginx 中引入][import-with-nginx]。 8 | 9 | 切换到项目目录,执行 10 | 11 | ```bash 12 | npm install --save restc 13 | ``` 14 | 15 | ### 1.2. Express 16 | 17 | 使用 `restc.express()` 中间件。 18 | 19 | ```javascript 20 | // import restc 21 | const restc = require('restc'); 22 | // use restc middleware 23 | app.use(restc.express()); 24 | ``` 25 | 26 | 也可以参考我们提供的例子 [restc-example-express][restc-example-express]。 27 | 28 | ### 1.3. Koa 1.x 29 | 30 | 使用 `restc.koa()` 中间件。 31 | 32 | ```javascript 33 | // import restc 34 | const restc = require('restc'); 35 | // use restc middleware 36 | app.use(restc.koa()); 37 | ``` 38 | 39 | 也可以参考我们提供的例子 [restc-example-koa][restc-example-koa]。 40 | 41 | ### 1.4. Koa 2.x 42 | 43 | 使用 `restc.koa2()` 中间件。 44 | 45 | ```javascript 46 | // import restc 47 | const restc = require('restc'); 48 | // use restc middleware 49 | app.use(restc.koa2()); 50 | ``` 51 | 52 | 也可以参考我们提供的例子 [restc-example-koa2][restc-example-koa2]。 53 | 54 | ### 1.4. Hapi 18.x 和 17.x 55 | 56 | 使用 `restc.hapi` 中间件。 57 | 58 | ```javascript 59 | // import restc 60 | const restc = require('restc'); 61 | // use restc middleware 62 | server.register(restc.hapi); 63 | ``` 64 | 65 | 也可以参考我们提供的例子 [restc-example-hapi][restc-example-hapi]。 66 | 67 | ### 1.5. Hapi 16.x 68 | 69 | 使用 `restc.hapiLegacy` 中间件。 70 | 71 | ```javascript 72 | // import restc 73 | const restc = require('restc'); 74 | // use restc middleware 75 | server.register([{ 76 | register: restc.hapiLegacy 77 | }], (err) => { 78 | if (err) { 79 | throw err 80 | } 81 | }) 82 | ``` 83 | 84 | 也可以参考我们提供的例子 [restc-example-hapi-legacy][restc-example-hapi-legacy]。 85 | 86 | ## 2. 在 Nginx 中引入 87 | 88 | 增加对 `$http_accept` 的判断逻辑,当客户端接受 HTML 时,重写并反向代理至 restc 页面。 89 | 90 | 假设原先 location 块为 91 | 92 | ```nginx 93 | location / { 94 | proxy_pass $scheme://myupstream; 95 | } 96 | ``` 97 | 98 | 在 location 块的开始处加上 99 | 100 | ```nginx 101 | add_header Vary Accept; 102 | if ($http_accept ~* "text/html") { 103 | rewrite ^.*$ / break; 104 | proxy_pass https://restc.faas.ele.me; 105 | } 106 | ``` 107 | 108 | 最终的 location 块变为 109 | 110 | ```nginx 111 | location / { 112 | add_header Vary Accept; 113 | if ($http_accept ~* "text/html") { 114 | rewrite ^.*$ / break; 115 | proxy_pass https://restc.faas.ele.me; 116 | } 117 | proxy_pass $scheme://myupstream; 118 | } 119 | ``` 120 | 121 | 建议配置 Nginx 缓存,加快访问速度。具体请看 [NGINX Content Caching][nginx-content-caching]。 122 | 123 | [import-with-nginx]: #2.%20%E5%9C%A8%20Nginx%20%E4%B8%AD%E5%BC%95%E5%85%A5 124 | [restc-example-express]: https://github.com/ElemeFE/restc/tree/master/examples/restc-example-express 125 | [restc-example-koa]: https://github.com/ElemeFE/restc/tree/master/examples/restc-example-koa 126 | [restc-example-koa2]: https://github.com/ElemeFE/restc/tree/master/examples/restc-example-koa2 127 | [nginx-content-caching]: https://www.nginx.com/resources/admin-guide/content-caching/ 128 | [restc-example-hapi]: https://github.com/ElemeFE/restc/tree/master/examples/restc-example-hapi/latest 129 | [restc-example-hapi-legacy]: https://github.com/ElemeFE/restc/tree/master/examples/restc-example-hapi/legacy 130 | -------------------------------------------------------------------------------- /docs/guide/index.html: -------------------------------------------------------------------------------- 1 | ../index.html -------------------------------------------------------------------------------- /docs/images/logo.svg: -------------------------------------------------------------------------------- 1 | 4 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 13 | 32 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /docs/intro/README.md: -------------------------------------------------------------------------------- 1 | ## 1. restc 2 | 3 | restc(发音同 rest-see /'restˌsi/)是一个 **HTTP 服务器中间件**。 4 | 5 | ## 2. 功能 6 | 7 | restc 用于可视化展示请求,调试 RESTful 接口。 8 | 9 | 挂载 restc 中间件后,访问者无需安装浏览器插件,直接通过浏览器访问接口地址,即可以可视化的方式对该接口发起各种请求。 10 | 11 | ### 2.1. 可视化展示请求 12 | 13 | 如果用浏览器直接打开一个 API 地址通常会得到一坨神奇的 JSON。 14 | 15 | 使用 restc 之后不仅**格式化**并**高亮**了结果,而且还可以看到 **HTTP 响应头**。 16 | 17 | | 使用前 | 使用后 | 18 | | ------ | ------ | 19 | | | | 20 | 21 | ### 2.2. 调试 RESTful 接口 22 | 23 | 如果用浏览器直接打开一个 API 地址只能是 GET 方法请求,RESTful 中的其他方法将无法测试。 24 | 25 | 使用 restc 之后不仅可以发送**多种 HTTP 方法**的请求,还是可以**在线编辑请求参数**。 26 | 27 | 28 | 29 | ## 3. 原理 30 | 31 | restc 的原理是根据请求头中的 accept 字段来判断请求是来自浏览器页面打开还是 ajax 请求(直接用页面打开时浏览器的 accept 会包含 text/html)。 32 | 33 | 对于浏览器页面打开的请求将对其渲染测试工具的 UI 界面。 34 | 35 | ## 4. 特点 36 | 37 | 1. 引入成本低:可以低成本地引入到绝大多数现有的 Web API 项目中。 38 | 2. 使用方便:客户端无需下载额外的软件或插件,在任何现代浏览器中直接访问 API endpoint 即可调试该 API。 39 | 3. 便于分享:由于是直接访问 API endpoint,因此可以很方便地分享某个请求。 40 | 41 | ## 5. 用法 42 | 43 | 请参见 [指南](../guide/)。 44 | -------------------------------------------------------------------------------- /docs/intro/index.html: -------------------------------------------------------------------------------- 1 | ../index.html -------------------------------------------------------------------------------- /examples/restc-example-express/index.js: -------------------------------------------------------------------------------- 1 | const express = require('express') 2 | const app = express() 3 | const restc = require('../..') 4 | 5 | app.use(restc.express()) 6 | 7 | app.get('/', (req, res) => { 8 | res.send({ message: 'Hello world!' }) 9 | }) 10 | 11 | app.get('/binary', (req, res) => { 12 | res.set('Content-Type', 'application/octet-stream') 13 | res.set('Content-Disposition', 'attachment; filename="hello.txt"') 14 | res.send('Hello world!') 15 | }) 16 | 17 | app.listen(3000) 18 | -------------------------------------------------------------------------------- /examples/restc-example-express/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-express", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "accepts": { 8 | "version": "1.3.7", 9 | "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", 10 | "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", 11 | "requires": { 12 | "mime-types": "~2.1.24", 13 | "negotiator": "0.6.2" 14 | } 15 | }, 16 | "array-flatten": { 17 | "version": "1.1.1", 18 | "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", 19 | "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" 20 | }, 21 | "body-parser": { 22 | "version": "1.18.3", 23 | "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.18.3.tgz", 24 | "integrity": "sha1-WykhmP/dVTs6DyDe0FkrlWlVyLQ=", 25 | "requires": { 26 | "bytes": "3.0.0", 27 | "content-type": "~1.0.4", 28 | "debug": "2.6.9", 29 | "depd": "~1.1.2", 30 | "http-errors": "~1.6.3", 31 | "iconv-lite": "0.4.23", 32 | "on-finished": "~2.3.0", 33 | "qs": "6.5.2", 34 | "raw-body": "2.3.3", 35 | "type-is": "~1.6.16" 36 | } 37 | }, 38 | "bytes": { 39 | "version": "3.0.0", 40 | "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", 41 | "integrity": "sha1-0ygVQE1olpn4Wk6k+odV3ROpYEg=" 42 | }, 43 | "content-disposition": { 44 | "version": "0.5.2", 45 | "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz", 46 | "integrity": "sha1-DPaLud318r55YcOoUXjLhdunjLQ=" 47 | }, 48 | "content-type": { 49 | "version": "1.0.4", 50 | "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", 51 | "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" 52 | }, 53 | "cookie": { 54 | "version": "0.3.1", 55 | "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", 56 | "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" 57 | }, 58 | "cookie-signature": { 59 | "version": "1.0.6", 60 | "resolved": "https://registry.npmjs.org/cookie-signature/-/cookie-signature-1.0.6.tgz", 61 | "integrity": "sha1-4wOogrNCzD7oylE6eZmXNNqzriw=" 62 | }, 63 | "debug": { 64 | "version": "2.6.9", 65 | "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", 66 | "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", 67 | "requires": { 68 | "ms": "2.0.0" 69 | } 70 | }, 71 | "depd": { 72 | "version": "1.1.2", 73 | "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", 74 | "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" 75 | }, 76 | "destroy": { 77 | "version": "1.0.4", 78 | "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", 79 | "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" 80 | }, 81 | "ee-first": { 82 | "version": "1.1.1", 83 | "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", 84 | "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" 85 | }, 86 | "encodeurl": { 87 | "version": "1.0.2", 88 | "resolved": "https://registry.npmjs.org/encodeurl/-/encodeurl-1.0.2.tgz", 89 | "integrity": "sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=" 90 | }, 91 | "escape-html": { 92 | "version": "1.0.3", 93 | "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", 94 | "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" 95 | }, 96 | "etag": { 97 | "version": "1.8.1", 98 | "resolved": "https://registry.npmjs.org/etag/-/etag-1.8.1.tgz", 99 | "integrity": "sha1-Qa4u62XvpiJorr/qg6x9eSmbCIc=" 100 | }, 101 | "express": { 102 | "version": "4.16.4", 103 | "resolved": "https://registry.npmjs.org/express/-/express-4.16.4.tgz", 104 | "integrity": "sha512-j12Uuyb4FMrd/qQAm6uCHAkPtO8FDTRJZBDd5D2KOL2eLaz1yUNdUB/NOIyq0iU4q4cFarsUCrnFDPBcnksuOg==", 105 | "requires": { 106 | "accepts": "~1.3.5", 107 | "array-flatten": "1.1.1", 108 | "body-parser": "1.18.3", 109 | "content-disposition": "0.5.2", 110 | "content-type": "~1.0.4", 111 | "cookie": "0.3.1", 112 | "cookie-signature": "1.0.6", 113 | "debug": "2.6.9", 114 | "depd": "~1.1.2", 115 | "encodeurl": "~1.0.2", 116 | "escape-html": "~1.0.3", 117 | "etag": "~1.8.1", 118 | "finalhandler": "1.1.1", 119 | "fresh": "0.5.2", 120 | "merge-descriptors": "1.0.1", 121 | "methods": "~1.1.2", 122 | "on-finished": "~2.3.0", 123 | "parseurl": "~1.3.2", 124 | "path-to-regexp": "0.1.7", 125 | "proxy-addr": "~2.0.4", 126 | "qs": "6.5.2", 127 | "range-parser": "~1.2.0", 128 | "safe-buffer": "5.1.2", 129 | "send": "0.16.2", 130 | "serve-static": "1.13.2", 131 | "setprototypeof": "1.1.0", 132 | "statuses": "~1.4.0", 133 | "type-is": "~1.6.16", 134 | "utils-merge": "1.0.1", 135 | "vary": "~1.1.2" 136 | } 137 | }, 138 | "finalhandler": { 139 | "version": "1.1.1", 140 | "resolved": "https://registry.npmjs.org/finalhandler/-/finalhandler-1.1.1.tgz", 141 | "integrity": "sha512-Y1GUDo39ez4aHAw7MysnUD5JzYX+WaIj8I57kO3aEPT1fFRL4sr7mjei97FgnwhAyyzRYmQZaTHb2+9uZ1dPtg==", 142 | "requires": { 143 | "debug": "2.6.9", 144 | "encodeurl": "~1.0.2", 145 | "escape-html": "~1.0.3", 146 | "on-finished": "~2.3.0", 147 | "parseurl": "~1.3.2", 148 | "statuses": "~1.4.0", 149 | "unpipe": "~1.0.0" 150 | } 151 | }, 152 | "forwarded": { 153 | "version": "0.1.2", 154 | "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.1.2.tgz", 155 | "integrity": "sha1-mMI9qxF1ZXuMBXPozszZGw/xjIQ=" 156 | }, 157 | "fresh": { 158 | "version": "0.5.2", 159 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", 160 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" 161 | }, 162 | "http-errors": { 163 | "version": "1.6.3", 164 | "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.6.3.tgz", 165 | "integrity": "sha1-i1VoC7S+KDoLW/TqLjhYC+HZMg0=", 166 | "requires": { 167 | "depd": "~1.1.2", 168 | "inherits": "2.0.3", 169 | "setprototypeof": "1.1.0", 170 | "statuses": ">= 1.4.0 < 2" 171 | } 172 | }, 173 | "iconv-lite": { 174 | "version": "0.4.23", 175 | "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.23.tgz", 176 | "integrity": "sha512-neyTUVFtahjf0mB3dZT77u+8O0QB89jFdnBkd5P1JgYPbPaia3gXXOVL2fq8VyU2gMMD7SaN7QukTB/pmXYvDA==", 177 | "requires": { 178 | "safer-buffer": ">= 2.1.2 < 3" 179 | } 180 | }, 181 | "inherits": { 182 | "version": "2.0.3", 183 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 184 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" 185 | }, 186 | "ipaddr.js": { 187 | "version": "1.9.0", 188 | "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz", 189 | "integrity": "sha512-M4Sjn6N/+O6/IXSJseKqHoFc+5FdGJ22sXqnjTpdZweHK64MzEPAyQZyEU3R/KRv2GLoa7nNtg/C2Ev6m7z+eA==" 190 | }, 191 | "media-typer": { 192 | "version": "0.3.0", 193 | "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", 194 | "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" 195 | }, 196 | "merge-descriptors": { 197 | "version": "1.0.1", 198 | "resolved": "https://registry.npmjs.org/merge-descriptors/-/merge-descriptors-1.0.1.tgz", 199 | "integrity": "sha1-sAqqVW3YtEVoFQ7J0blT8/kMu2E=" 200 | }, 201 | "methods": { 202 | "version": "1.1.2", 203 | "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", 204 | "integrity": "sha1-VSmk1nZUE07cxSZmVoNbD4Ua/O4=" 205 | }, 206 | "mime": { 207 | "version": "1.4.1", 208 | "resolved": "https://registry.npmjs.org/mime/-/mime-1.4.1.tgz", 209 | "integrity": "sha512-KI1+qOZu5DcW6wayYHSzR/tXKCDC5Om4s1z2QJjDULzLcmf3DvzS7oluY4HCTrc+9FiKmWUgeNLg7W3uIQvxtQ==" 210 | }, 211 | "mime-db": { 212 | "version": "1.40.0", 213 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", 214 | "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" 215 | }, 216 | "mime-types": { 217 | "version": "2.1.24", 218 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", 219 | "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", 220 | "requires": { 221 | "mime-db": "1.40.0" 222 | } 223 | }, 224 | "ms": { 225 | "version": "2.0.0", 226 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 227 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 228 | }, 229 | "negotiator": { 230 | "version": "0.6.2", 231 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", 232 | "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" 233 | }, 234 | "on-finished": { 235 | "version": "2.3.0", 236 | "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", 237 | "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", 238 | "requires": { 239 | "ee-first": "1.1.1" 240 | } 241 | }, 242 | "parseurl": { 243 | "version": "1.3.3", 244 | "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", 245 | "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" 246 | }, 247 | "path-to-regexp": { 248 | "version": "0.1.7", 249 | "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-0.1.7.tgz", 250 | "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" 251 | }, 252 | "proxy-addr": { 253 | "version": "2.0.5", 254 | "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.5.tgz", 255 | "integrity": "sha512-t/7RxHXPH6cJtP0pRG6smSr9QJidhB+3kXu0KgXnbGYMgzEnUxRQ4/LDdfOwZEMyIh3/xHb8PX3t+lfL9z+YVQ==", 256 | "requires": { 257 | "forwarded": "~0.1.2", 258 | "ipaddr.js": "1.9.0" 259 | } 260 | }, 261 | "qs": { 262 | "version": "6.5.2", 263 | "resolved": "https://registry.npmjs.org/qs/-/qs-6.5.2.tgz", 264 | "integrity": "sha512-N5ZAX4/LxJmF+7wN74pUD6qAh9/wnvdQcjq9TZjevvXzSUo7bfmw91saqMjzGS2xq91/odN2dW/WOl7qQHNDGA==" 265 | }, 266 | "range-parser": { 267 | "version": "1.2.1", 268 | "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.1.tgz", 269 | "integrity": "sha512-Hrgsx+orqoygnmhFbKaHE6c296J+HTAQXoxEF6gNupROmmGJRoyzfG3ccAveqCBrwr/2yxQ5BVd/GTl5agOwSg==" 270 | }, 271 | "raw-body": { 272 | "version": "2.3.3", 273 | "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.3.3.tgz", 274 | "integrity": "sha512-9esiElv1BrZoI3rCDuOuKCBRbuApGGaDPQfjSflGxdy4oyzqghxu6klEkkVIvBje+FF0BX9coEv8KqW6X/7njw==", 275 | "requires": { 276 | "bytes": "3.0.0", 277 | "http-errors": "1.6.3", 278 | "iconv-lite": "0.4.23", 279 | "unpipe": "1.0.0" 280 | } 281 | }, 282 | "safe-buffer": { 283 | "version": "5.1.2", 284 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 285 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" 286 | }, 287 | "safer-buffer": { 288 | "version": "2.1.2", 289 | "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", 290 | "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" 291 | }, 292 | "send": { 293 | "version": "0.16.2", 294 | "resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz", 295 | "integrity": "sha512-E64YFPUssFHEFBvpbbjr44NCLtI1AohxQ8ZSiJjQLskAdKuriYEP6VyGEsRDH8ScozGpkaX1BGvhanqCwkcEZw==", 296 | "requires": { 297 | "debug": "2.6.9", 298 | "depd": "~1.1.2", 299 | "destroy": "~1.0.4", 300 | "encodeurl": "~1.0.2", 301 | "escape-html": "~1.0.3", 302 | "etag": "~1.8.1", 303 | "fresh": "0.5.2", 304 | "http-errors": "~1.6.2", 305 | "mime": "1.4.1", 306 | "ms": "2.0.0", 307 | "on-finished": "~2.3.0", 308 | "range-parser": "~1.2.0", 309 | "statuses": "~1.4.0" 310 | } 311 | }, 312 | "serve-static": { 313 | "version": "1.13.2", 314 | "resolved": "https://registry.npmjs.org/serve-static/-/serve-static-1.13.2.tgz", 315 | "integrity": "sha512-p/tdJrO4U387R9oMjb1oj7qSMaMfmOyd4j9hOFoxZe2baQszgHcSWjuya/CiT5kgZZKRudHNOA0pYXOl8rQ5nw==", 316 | "requires": { 317 | "encodeurl": "~1.0.2", 318 | "escape-html": "~1.0.3", 319 | "parseurl": "~1.3.2", 320 | "send": "0.16.2" 321 | } 322 | }, 323 | "setprototypeof": { 324 | "version": "1.1.0", 325 | "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", 326 | "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" 327 | }, 328 | "statuses": { 329 | "version": "1.4.0", 330 | "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", 331 | "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" 332 | }, 333 | "type-is": { 334 | "version": "1.6.18", 335 | "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", 336 | "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", 337 | "requires": { 338 | "media-typer": "0.3.0", 339 | "mime-types": "~2.1.24" 340 | } 341 | }, 342 | "unpipe": { 343 | "version": "1.0.0", 344 | "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", 345 | "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" 346 | }, 347 | "utils-merge": { 348 | "version": "1.0.1", 349 | "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", 350 | "integrity": "sha1-n5VxD1CiZ5R7LMwSR0HBAoQn5xM=" 351 | }, 352 | "vary": { 353 | "version": "1.1.2", 354 | "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", 355 | "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" 356 | } 357 | } 358 | } 359 | -------------------------------------------------------------------------------- /examples/restc-example-express/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-express", 3 | "version": "1.0.0", 4 | "description": "An example to demostrate how to use restc in express.", 5 | "private": true, 6 | "main": "index.js", 7 | "author": "lujjjh ", 8 | "license": "MIT", 9 | "dependencies": { 10 | "express": "^4.14.0" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /examples/restc-example-hapi/latest/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const restc = require('../../..') 3 | const Hapi = require('hapi') 4 | 5 | const server = Hapi.server({ 6 | port: 3003 7 | }) 8 | 9 | server.route({ 10 | method: 'GET', 11 | path: '/', 12 | handler: (request, h) => { 13 | return 'Hello, world!' 14 | } 15 | }) 16 | server.route({ 17 | method: 'GET', 18 | path: '/json', 19 | handler: (request, h) => { 20 | return { 21 | message: 'Hello, world!' 22 | } 23 | } 24 | }) 25 | 26 | const init = async () => { 27 | await server.register(restc.hapi) 28 | await server.start() 29 | console.log(`Server running at: ${server.info.uri}`) 30 | } 31 | 32 | process.on('unhandledRejection', (err) => { 33 | console.log(err) 34 | process.exit(1) 35 | }) 36 | init() 37 | -------------------------------------------------------------------------------- /examples/restc-example-hapi/latest/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-hapi", 3 | "version": "1.0.0", 4 | "description": "An example to demostrate how to use restc in hapi", 5 | "main": "index.js", 6 | "author": "mayxiaoyu1223@gmail.com", 7 | "license": "MIT", 8 | "dependencies": { 9 | "hapi": "^18.1.0" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /examples/restc-example-hapi/legacy/index.js: -------------------------------------------------------------------------------- 1 | const restc = require('../../..') 2 | const Hapi = require('hapi') 3 | const server = new Hapi.Server() 4 | server.connection({ 5 | port: 3333 6 | }) 7 | let registerConfig = [{ 8 | register: restc.hapiLegacy 9 | }] 10 | server.register(registerConfig, (err) => { 11 | if (err) { 12 | throw err // something bad happened loading the plugin 13 | } 14 | // add route 15 | server.route({ 16 | method: 'GET', 17 | path: '/', 18 | handler: (request, reply) => { 19 | reply('Hello world!') 20 | } 21 | }) 22 | server.route({ 23 | method: 'GET', 24 | path: '/json', 25 | handler: (request, reply) => { 26 | reply({ 27 | message: 'Hello world' 28 | }) 29 | } 30 | }) 31 | // Start the server 32 | server.start((error) => { 33 | if (error) { 34 | throw error 35 | } 36 | 37 | console.log('info', `Server running at:${server.info.uri}`) 38 | }) 39 | }) 40 | -------------------------------------------------------------------------------- /examples/restc-example-hapi/legacy/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-hapi-16.x.x", 3 | "version": "1.0.0", 4 | "description": "An example to demostrate how to use restc in hapi 16.x.x", 5 | "main": "index.js", 6 | "author": "mayxiaoyu1223@gmail.com", 7 | "license": "MIT", 8 | "dependencies": { 9 | "hapi": "^16.7.0" 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /examples/restc-example-koa/index.js: -------------------------------------------------------------------------------- 1 | const app = require('koa')() 2 | const restc = require('../..') 3 | 4 | app.use(restc.koa()) 5 | 6 | app.use(function * (next) { 7 | this.body = { message: 'Hello world!' } 8 | }) 9 | 10 | app.listen(3000) 11 | -------------------------------------------------------------------------------- /examples/restc-example-koa/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-koa", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "accepts": { 8 | "version": "1.3.7", 9 | "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", 10 | "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", 11 | "requires": { 12 | "mime-types": "~2.1.24", 13 | "negotiator": "0.6.2" 14 | } 15 | }, 16 | "any-promise": { 17 | "version": "1.3.0", 18 | "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", 19 | "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" 20 | }, 21 | "co": { 22 | "version": "4.6.0", 23 | "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", 24 | "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=" 25 | }, 26 | "composition": { 27 | "version": "2.3.0", 28 | "resolved": "https://registry.npmjs.org/composition/-/composition-2.3.0.tgz", 29 | "integrity": "sha1-dCgFN0yrVQxSCjNmL1pzLgII1vI=", 30 | "requires": { 31 | "any-promise": "^1.1.0", 32 | "co": "^4.0.2" 33 | } 34 | }, 35 | "content-disposition": { 36 | "version": "0.5.3", 37 | "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", 38 | "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", 39 | "requires": { 40 | "safe-buffer": "5.1.2" 41 | } 42 | }, 43 | "content-type": { 44 | "version": "1.0.4", 45 | "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", 46 | "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" 47 | }, 48 | "cookies": { 49 | "version": "0.7.3", 50 | "resolved": "https://registry.npmjs.org/cookies/-/cookies-0.7.3.tgz", 51 | "integrity": "sha512-+gixgxYSgQLTaTIilDHAdlNPZDENDQernEMiIcZpYYP14zgHsCt4Ce1FEjFtcp6GefhozebB6orvhAAWx/IS0A==", 52 | "requires": { 53 | "depd": "~1.1.2", 54 | "keygrip": "~1.0.3" 55 | } 56 | }, 57 | "debug": { 58 | "version": "2.6.9", 59 | "resolved": "https://registry.npmjs.org/debug/-/debug-2.6.9.tgz", 60 | "integrity": "sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==", 61 | "requires": { 62 | "ms": "2.0.0" 63 | } 64 | }, 65 | "deep-equal": { 66 | "version": "1.0.1", 67 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", 68 | "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=" 69 | }, 70 | "delegates": { 71 | "version": "1.0.0", 72 | "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", 73 | "integrity": "sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=" 74 | }, 75 | "depd": { 76 | "version": "1.1.2", 77 | "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", 78 | "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" 79 | }, 80 | "destroy": { 81 | "version": "1.0.4", 82 | "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", 83 | "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" 84 | }, 85 | "ee-first": { 86 | "version": "1.1.1", 87 | "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", 88 | "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" 89 | }, 90 | "error-inject": { 91 | "version": "1.0.0", 92 | "resolved": "https://registry.npmjs.org/error-inject/-/error-inject-1.0.0.tgz", 93 | "integrity": "sha1-4rPZG1Su1nLzCdlQ0VSFD6EdTzc=" 94 | }, 95 | "escape-html": { 96 | "version": "1.0.3", 97 | "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", 98 | "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" 99 | }, 100 | "fresh": { 101 | "version": "0.5.2", 102 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", 103 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" 104 | }, 105 | "http-assert": { 106 | "version": "1.4.1", 107 | "resolved": "https://registry.npmjs.org/http-assert/-/http-assert-1.4.1.tgz", 108 | "integrity": "sha512-rdw7q6GTlibqVVbXr0CKelfV5iY8G2HqEUkhSk297BMbSpSL8crXC+9rjKoMcZZEsksX30le6f/4ul4E28gegw==", 109 | "requires": { 110 | "deep-equal": "~1.0.1", 111 | "http-errors": "~1.7.2" 112 | } 113 | }, 114 | "http-errors": { 115 | "version": "1.7.2", 116 | "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", 117 | "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", 118 | "requires": { 119 | "depd": "~1.1.2", 120 | "inherits": "2.0.3", 121 | "setprototypeof": "1.1.1", 122 | "statuses": ">= 1.5.0 < 2", 123 | "toidentifier": "1.0.0" 124 | } 125 | }, 126 | "inherits": { 127 | "version": "2.0.3", 128 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 129 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" 130 | }, 131 | "keygrip": { 132 | "version": "1.0.3", 133 | "resolved": "https://registry.npmjs.org/keygrip/-/keygrip-1.0.3.tgz", 134 | "integrity": "sha512-/PpesirAIfaklxUzp4Yb7xBper9MwP6hNRA6BGGUFCgbJ+BM5CKBtsoxinNXkLHAr+GXS1/lSlF2rP7cv5Fl+g==" 135 | }, 136 | "koa": { 137 | "version": "1.6.2", 138 | "resolved": "https://registry.npmjs.org/koa/-/koa-1.6.2.tgz", 139 | "integrity": "sha512-oeH9b78oNQeDzmgXSmjzHIw7eT9584Lhp9h+r7zcXnzNf+2vJf021GEJsIQ5owj8Wu2x5pArrYjURnp37rv+5Q==", 140 | "requires": { 141 | "accepts": "^1.2.2", 142 | "co": "^4.4.0", 143 | "composition": "^2.1.1", 144 | "content-disposition": "~0.5.0", 145 | "content-type": "^1.0.0", 146 | "cookies": "~0.7.0", 147 | "debug": "^2.6.9", 148 | "delegates": "^1.0.0", 149 | "destroy": "^1.0.3", 150 | "error-inject": "~1.0.0", 151 | "escape-html": "~1.0.1", 152 | "fresh": "^0.5.2", 153 | "http-assert": "^1.1.0", 154 | "http-errors": "^1.2.8", 155 | "koa-compose": "^2.3.0", 156 | "koa-is-json": "^1.0.0", 157 | "mime-types": "^2.0.7", 158 | "on-finished": "^2.1.0", 159 | "only": "0.0.2", 160 | "parseurl": "^1.3.0", 161 | "statuses": "^1.2.0", 162 | "type-is": "^1.5.5", 163 | "vary": "^1.0.0" 164 | } 165 | }, 166 | "koa-compose": { 167 | "version": "2.5.1", 168 | "resolved": "https://registry.npmjs.org/koa-compose/-/koa-compose-2.5.1.tgz", 169 | "integrity": "sha1-cmz7F2lN5cufvwPArfFyMD+D8VY=" 170 | }, 171 | "koa-is-json": { 172 | "version": "1.0.0", 173 | "resolved": "https://registry.npmjs.org/koa-is-json/-/koa-is-json-1.0.0.tgz", 174 | "integrity": "sha1-JzwH7c3Ljfaiwat9We52SRRR7BQ=" 175 | }, 176 | "media-typer": { 177 | "version": "0.3.0", 178 | "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", 179 | "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" 180 | }, 181 | "mime-db": { 182 | "version": "1.40.0", 183 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", 184 | "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" 185 | }, 186 | "mime-types": { 187 | "version": "2.1.24", 188 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", 189 | "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", 190 | "requires": { 191 | "mime-db": "1.40.0" 192 | } 193 | }, 194 | "ms": { 195 | "version": "2.0.0", 196 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 197 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 198 | }, 199 | "negotiator": { 200 | "version": "0.6.2", 201 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", 202 | "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" 203 | }, 204 | "on-finished": { 205 | "version": "2.3.0", 206 | "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", 207 | "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", 208 | "requires": { 209 | "ee-first": "1.1.1" 210 | } 211 | }, 212 | "only": { 213 | "version": "0.0.2", 214 | "resolved": "https://registry.npmjs.org/only/-/only-0.0.2.tgz", 215 | "integrity": "sha1-Kv3oTQPlC5qO3EROMGEKcCle37Q=" 216 | }, 217 | "parseurl": { 218 | "version": "1.3.3", 219 | "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", 220 | "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" 221 | }, 222 | "safe-buffer": { 223 | "version": "5.1.2", 224 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 225 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" 226 | }, 227 | "setprototypeof": { 228 | "version": "1.1.1", 229 | "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", 230 | "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" 231 | }, 232 | "statuses": { 233 | "version": "1.5.0", 234 | "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", 235 | "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" 236 | }, 237 | "toidentifier": { 238 | "version": "1.0.0", 239 | "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", 240 | "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" 241 | }, 242 | "type-is": { 243 | "version": "1.6.18", 244 | "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", 245 | "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", 246 | "requires": { 247 | "media-typer": "0.3.0", 248 | "mime-types": "~2.1.24" 249 | } 250 | }, 251 | "vary": { 252 | "version": "1.1.2", 253 | "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", 254 | "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" 255 | } 256 | } 257 | } 258 | -------------------------------------------------------------------------------- /examples/restc-example-koa/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-koa", 3 | "version": "1.0.0", 4 | "description": "An example to demostrate how to use restc in koa 1.x.", 5 | "private": true, 6 | "main": "index.js", 7 | "author": "lujjjh ", 8 | "license": "MIT", 9 | "dependencies": { 10 | "koa": "^1.2.4" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /examples/restc-example-koa2-inline/gateway.js: -------------------------------------------------------------------------------- 1 | const Koa = require('koa') 2 | const restc = require('../..') 3 | const app = new Koa() 4 | 5 | app.use(restc.koa2({ 6 | includes: ['/api', /^\/foo/], 7 | excludes: '/foobar' 8 | })) 9 | 10 | app.listen(3000) 11 | -------------------------------------------------------------------------------- /examples/restc-example-koa2-inline/index.js: -------------------------------------------------------------------------------- 1 | const Koa = require('koa') 2 | const restc = require('../..') 3 | const app = new Koa() 4 | 5 | app.use(restc.koa2({ inline: true })) 6 | 7 | app.use((ctx, next) => { 8 | ctx.body = { message: 'Hello world!' } 9 | return next() 10 | }) 11 | 12 | app.listen(3000) 13 | -------------------------------------------------------------------------------- /examples/restc-example-koa2-inline/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-koa2", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "accepts": { 8 | "version": "1.3.7", 9 | "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", 10 | "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", 11 | "requires": { 12 | "mime-types": "~2.1.24", 13 | "negotiator": "0.6.2" 14 | } 15 | }, 16 | "any-promise": { 17 | "version": "1.3.0", 18 | "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", 19 | "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" 20 | }, 21 | "cache-content-type": { 22 | "version": "1.0.1", 23 | "resolved": "https://registry.npmjs.org/cache-content-type/-/cache-content-type-1.0.1.tgz", 24 | "integrity": "sha512-IKufZ1o4Ut42YUrZSo8+qnMTrFuKkvyoLXUywKz9GJ5BrhOFGhLdkx9sG4KAnVvbY6kEcSFjLQul+DVmBm2bgA==", 25 | "requires": { 26 | "mime-types": "^2.1.18", 27 | "ylru": "^1.2.0" 28 | } 29 | }, 30 | "co": { 31 | "version": "4.6.0", 32 | "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", 33 | "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=" 34 | }, 35 | "content-disposition": { 36 | "version": "0.5.3", 37 | "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", 38 | "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", 39 | "requires": { 40 | "safe-buffer": "5.1.2" 41 | } 42 | }, 43 | "content-type": { 44 | "version": "1.0.4", 45 | "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", 46 | "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" 47 | }, 48 | "cookies": { 49 | "version": "0.7.3", 50 | "resolved": "https://registry.npmjs.org/cookies/-/cookies-0.7.3.tgz", 51 | "integrity": "sha512-+gixgxYSgQLTaTIilDHAdlNPZDENDQernEMiIcZpYYP14zgHsCt4Ce1FEjFtcp6GefhozebB6orvhAAWx/IS0A==", 52 | "requires": { 53 | "depd": "~1.1.2", 54 | "keygrip": "~1.0.3" 55 | } 56 | }, 57 | "debug": { 58 | "version": "3.1.0", 59 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", 60 | "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", 61 | "requires": { 62 | "ms": "2.0.0" 63 | } 64 | }, 65 | "deep-equal": { 66 | "version": "1.0.1", 67 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", 68 | "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=" 69 | }, 70 | "delegates": { 71 | "version": "1.0.0", 72 | "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", 73 | "integrity": "sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=" 74 | }, 75 | "depd": { 76 | "version": "1.1.2", 77 | "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", 78 | "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" 79 | }, 80 | "destroy": { 81 | "version": "1.0.4", 82 | "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", 83 | "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" 84 | }, 85 | "ee-first": { 86 | "version": "1.1.1", 87 | "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", 88 | "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" 89 | }, 90 | "error-inject": { 91 | "version": "1.0.0", 92 | "resolved": "https://registry.npmjs.org/error-inject/-/error-inject-1.0.0.tgz", 93 | "integrity": "sha1-4rPZG1Su1nLzCdlQ0VSFD6EdTzc=" 94 | }, 95 | "escape-html": { 96 | "version": "1.0.3", 97 | "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", 98 | "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" 99 | }, 100 | "fresh": { 101 | "version": "0.5.2", 102 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", 103 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" 104 | }, 105 | "http-assert": { 106 | "version": "1.4.1", 107 | "resolved": "https://registry.npmjs.org/http-assert/-/http-assert-1.4.1.tgz", 108 | "integrity": "sha512-rdw7q6GTlibqVVbXr0CKelfV5iY8G2HqEUkhSk297BMbSpSL8crXC+9rjKoMcZZEsksX30le6f/4ul4E28gegw==", 109 | "requires": { 110 | "deep-equal": "~1.0.1", 111 | "http-errors": "~1.7.2" 112 | } 113 | }, 114 | "http-errors": { 115 | "version": "1.7.2", 116 | "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", 117 | "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", 118 | "requires": { 119 | "depd": "~1.1.2", 120 | "inherits": "2.0.3", 121 | "setprototypeof": "1.1.1", 122 | "statuses": ">= 1.5.0 < 2", 123 | "toidentifier": "1.0.0" 124 | } 125 | }, 126 | "inherits": { 127 | "version": "2.0.3", 128 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 129 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" 130 | }, 131 | "is-generator-function": { 132 | "version": "1.0.7", 133 | "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.7.tgz", 134 | "integrity": "sha512-YZc5EwyO4f2kWCax7oegfuSr9mFz1ZvieNYBEjmukLxgXfBUbxAWGVF7GZf0zidYtoBl3WvC07YK0wT76a+Rtw==" 135 | }, 136 | "keygrip": { 137 | "version": "1.0.3", 138 | "resolved": "https://registry.npmjs.org/keygrip/-/keygrip-1.0.3.tgz", 139 | "integrity": "sha512-/PpesirAIfaklxUzp4Yb7xBper9MwP6hNRA6BGGUFCgbJ+BM5CKBtsoxinNXkLHAr+GXS1/lSlF2rP7cv5Fl+g==" 140 | }, 141 | "koa": { 142 | "version": "2.7.0", 143 | "resolved": "https://registry.npmjs.org/koa/-/koa-2.7.0.tgz", 144 | "integrity": "sha512-7ojD05s2Q+hFudF8tDLZ1CpCdVZw8JQELWSkcfG9bdtoTDzMmkRF6BQBU7JzIzCCOY3xd3tftiy/loHBUYaY2Q==", 145 | "requires": { 146 | "accepts": "^1.3.5", 147 | "cache-content-type": "^1.0.0", 148 | "content-disposition": "~0.5.2", 149 | "content-type": "^1.0.4", 150 | "cookies": "~0.7.1", 151 | "debug": "~3.1.0", 152 | "delegates": "^1.0.0", 153 | "depd": "^1.1.2", 154 | "destroy": "^1.0.4", 155 | "error-inject": "^1.0.0", 156 | "escape-html": "^1.0.3", 157 | "fresh": "~0.5.2", 158 | "http-assert": "^1.3.0", 159 | "http-errors": "^1.6.3", 160 | "is-generator-function": "^1.0.7", 161 | "koa-compose": "^4.1.0", 162 | "koa-convert": "^1.2.0", 163 | "koa-is-json": "^1.0.0", 164 | "on-finished": "^2.3.0", 165 | "only": "~0.0.2", 166 | "parseurl": "^1.3.2", 167 | "statuses": "^1.5.0", 168 | "type-is": "^1.6.16", 169 | "vary": "^1.1.2" 170 | } 171 | }, 172 | "koa-compose": { 173 | "version": "4.1.0", 174 | "resolved": "https://registry.npmjs.org/koa-compose/-/koa-compose-4.1.0.tgz", 175 | "integrity": "sha512-8ODW8TrDuMYvXRwra/Kh7/rJo9BtOfPc6qO8eAfC80CnCvSjSl0bkRM24X6/XBBEyj0v1nRUQ1LyOy3dbqOWXw==" 176 | }, 177 | "koa-convert": { 178 | "version": "1.2.0", 179 | "resolved": "https://registry.npmjs.org/koa-convert/-/koa-convert-1.2.0.tgz", 180 | "integrity": "sha1-2kCHXfSd4FOQmNFwC1CCDOvNIdA=", 181 | "requires": { 182 | "co": "^4.6.0", 183 | "koa-compose": "^3.0.0" 184 | }, 185 | "dependencies": { 186 | "koa-compose": { 187 | "version": "3.2.1", 188 | "resolved": "https://registry.npmjs.org/koa-compose/-/koa-compose-3.2.1.tgz", 189 | "integrity": "sha1-qFzLQLfZhtjlo0Wzoazo6rz1Tec=", 190 | "requires": { 191 | "any-promise": "^1.1.0" 192 | } 193 | } 194 | } 195 | }, 196 | "koa-is-json": { 197 | "version": "1.0.0", 198 | "resolved": "https://registry.npmjs.org/koa-is-json/-/koa-is-json-1.0.0.tgz", 199 | "integrity": "sha1-JzwH7c3Ljfaiwat9We52SRRR7BQ=" 200 | }, 201 | "media-typer": { 202 | "version": "0.3.0", 203 | "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", 204 | "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" 205 | }, 206 | "mime-db": { 207 | "version": "1.40.0", 208 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", 209 | "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" 210 | }, 211 | "mime-types": { 212 | "version": "2.1.24", 213 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", 214 | "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", 215 | "requires": { 216 | "mime-db": "1.40.0" 217 | } 218 | }, 219 | "ms": { 220 | "version": "2.0.0", 221 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 222 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 223 | }, 224 | "negotiator": { 225 | "version": "0.6.2", 226 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", 227 | "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" 228 | }, 229 | "on-finished": { 230 | "version": "2.3.0", 231 | "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", 232 | "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", 233 | "requires": { 234 | "ee-first": "1.1.1" 235 | } 236 | }, 237 | "only": { 238 | "version": "0.0.2", 239 | "resolved": "https://registry.npmjs.org/only/-/only-0.0.2.tgz", 240 | "integrity": "sha1-Kv3oTQPlC5qO3EROMGEKcCle37Q=" 241 | }, 242 | "parseurl": { 243 | "version": "1.3.3", 244 | "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", 245 | "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" 246 | }, 247 | "safe-buffer": { 248 | "version": "5.1.2", 249 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 250 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" 251 | }, 252 | "setprototypeof": { 253 | "version": "1.1.1", 254 | "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", 255 | "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" 256 | }, 257 | "statuses": { 258 | "version": "1.5.0", 259 | "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", 260 | "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" 261 | }, 262 | "toidentifier": { 263 | "version": "1.0.0", 264 | "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", 265 | "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" 266 | }, 267 | "type-is": { 268 | "version": "1.6.18", 269 | "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", 270 | "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", 271 | "requires": { 272 | "media-typer": "0.3.0", 273 | "mime-types": "~2.1.24" 274 | } 275 | }, 276 | "vary": { 277 | "version": "1.1.2", 278 | "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", 279 | "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" 280 | }, 281 | "ylru": { 282 | "version": "1.2.1", 283 | "resolved": "https://registry.npmjs.org/ylru/-/ylru-1.2.1.tgz", 284 | "integrity": "sha512-faQrqNMzcPCHGVC2aaOINk13K+aaBDUPjGWl0teOXywElLjyVAB6Oe2jj62jHYtwsU49jXhScYbvPENK+6zAvQ==" 285 | } 286 | } 287 | } 288 | -------------------------------------------------------------------------------- /examples/restc-example-koa2-inline/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-koa2", 3 | "version": "1.0.0", 4 | "description": "An example to demostrate how to use restc in koa 2.x.", 5 | "private": true, 6 | "main": "index.js", 7 | "author": "lujjjh ", 8 | "license": "MIT", 9 | "dependencies": { 10 | "koa": "^2.0.0" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /examples/restc-example-koa2/gateway.js: -------------------------------------------------------------------------------- 1 | const Koa = require('koa') 2 | const restc = require('../..') 3 | const app = new Koa() 4 | 5 | app.use(restc.koa2({ 6 | includes: ['/api', /^\/foo/], 7 | excludes: '/foobar' 8 | })) 9 | 10 | app.listen(3000) 11 | -------------------------------------------------------------------------------- /examples/restc-example-koa2/index.js: -------------------------------------------------------------------------------- 1 | const Koa = require('koa') 2 | const restc = require('../..') 3 | const app = new Koa() 4 | 5 | app.use(restc.koa2()) 6 | 7 | app.use((ctx, next) => { 8 | ctx.body = { message: 'Hello world!' } 9 | return next() 10 | }) 11 | 12 | app.listen(3000) 13 | -------------------------------------------------------------------------------- /examples/restc-example-koa2/package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-koa2", 3 | "version": "1.0.0", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "accepts": { 8 | "version": "1.3.7", 9 | "resolved": "https://registry.npmjs.org/accepts/-/accepts-1.3.7.tgz", 10 | "integrity": "sha512-Il80Qs2WjYlJIBNzNkK6KYqlVMTbZLXgHx2oT0pU/fjRHyEp+PEfEPY0R3WCwAGVOtauxh1hOxNgIf5bv7dQpA==", 11 | "requires": { 12 | "mime-types": "~2.1.24", 13 | "negotiator": "0.6.2" 14 | } 15 | }, 16 | "any-promise": { 17 | "version": "1.3.0", 18 | "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", 19 | "integrity": "sha1-q8av7tzqUugJzcA3au0845Y10X8=" 20 | }, 21 | "cache-content-type": { 22 | "version": "1.0.1", 23 | "resolved": "https://registry.npmjs.org/cache-content-type/-/cache-content-type-1.0.1.tgz", 24 | "integrity": "sha512-IKufZ1o4Ut42YUrZSo8+qnMTrFuKkvyoLXUywKz9GJ5BrhOFGhLdkx9sG4KAnVvbY6kEcSFjLQul+DVmBm2bgA==", 25 | "requires": { 26 | "mime-types": "^2.1.18", 27 | "ylru": "^1.2.0" 28 | } 29 | }, 30 | "co": { 31 | "version": "4.6.0", 32 | "resolved": "https://registry.npmjs.org/co/-/co-4.6.0.tgz", 33 | "integrity": "sha1-bqa989hTrlTMuOR7+gvz+QMfsYQ=" 34 | }, 35 | "content-disposition": { 36 | "version": "0.5.3", 37 | "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.3.tgz", 38 | "integrity": "sha512-ExO0774ikEObIAEV9kDo50o+79VCUdEB6n6lzKgGwupcVeRlhrj3qGAfwq8G6uBJjkqLrhT0qEYFcWng8z1z0g==", 39 | "requires": { 40 | "safe-buffer": "5.1.2" 41 | } 42 | }, 43 | "content-type": { 44 | "version": "1.0.4", 45 | "resolved": "https://registry.npmjs.org/content-type/-/content-type-1.0.4.tgz", 46 | "integrity": "sha512-hIP3EEPs8tB9AT1L+NUqtwOAps4mk2Zob89MWXMHjHWg9milF/j4osnnQLXBCBFBk/tvIG/tUc9mOUJiPBhPXA==" 47 | }, 48 | "cookies": { 49 | "version": "0.7.3", 50 | "resolved": "https://registry.npmjs.org/cookies/-/cookies-0.7.3.tgz", 51 | "integrity": "sha512-+gixgxYSgQLTaTIilDHAdlNPZDENDQernEMiIcZpYYP14zgHsCt4Ce1FEjFtcp6GefhozebB6orvhAAWx/IS0A==", 52 | "requires": { 53 | "depd": "~1.1.2", 54 | "keygrip": "~1.0.3" 55 | } 56 | }, 57 | "debug": { 58 | "version": "3.1.0", 59 | "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", 60 | "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", 61 | "requires": { 62 | "ms": "2.0.0" 63 | } 64 | }, 65 | "deep-equal": { 66 | "version": "1.0.1", 67 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", 68 | "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=" 69 | }, 70 | "delegates": { 71 | "version": "1.0.0", 72 | "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", 73 | "integrity": "sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=" 74 | }, 75 | "depd": { 76 | "version": "1.1.2", 77 | "resolved": "https://registry.npmjs.org/depd/-/depd-1.1.2.tgz", 78 | "integrity": "sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=" 79 | }, 80 | "destroy": { 81 | "version": "1.0.4", 82 | "resolved": "https://registry.npmjs.org/destroy/-/destroy-1.0.4.tgz", 83 | "integrity": "sha1-l4hXRCxEdJ5CBmE+N5RiBYJqvYA=" 84 | }, 85 | "ee-first": { 86 | "version": "1.1.1", 87 | "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", 88 | "integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0=" 89 | }, 90 | "error-inject": { 91 | "version": "1.0.0", 92 | "resolved": "https://registry.npmjs.org/error-inject/-/error-inject-1.0.0.tgz", 93 | "integrity": "sha1-4rPZG1Su1nLzCdlQ0VSFD6EdTzc=" 94 | }, 95 | "escape-html": { 96 | "version": "1.0.3", 97 | "resolved": "https://registry.npmjs.org/escape-html/-/escape-html-1.0.3.tgz", 98 | "integrity": "sha1-Aljq5NPQwJdN4cFpGI7wBR0dGYg=" 99 | }, 100 | "fresh": { 101 | "version": "0.5.2", 102 | "resolved": "https://registry.npmjs.org/fresh/-/fresh-0.5.2.tgz", 103 | "integrity": "sha1-PYyt2Q2XZWn6g1qx+OSyOhBWBac=" 104 | }, 105 | "http-assert": { 106 | "version": "1.4.1", 107 | "resolved": "https://registry.npmjs.org/http-assert/-/http-assert-1.4.1.tgz", 108 | "integrity": "sha512-rdw7q6GTlibqVVbXr0CKelfV5iY8G2HqEUkhSk297BMbSpSL8crXC+9rjKoMcZZEsksX30le6f/4ul4E28gegw==", 109 | "requires": { 110 | "deep-equal": "~1.0.1", 111 | "http-errors": "~1.7.2" 112 | } 113 | }, 114 | "http-errors": { 115 | "version": "1.7.2", 116 | "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.7.2.tgz", 117 | "integrity": "sha512-uUQBt3H/cSIVfch6i1EuPNy/YsRSOUBXTVfZ+yR7Zjez3qjBz6i9+i4zjNaoqcoFVI4lQJ5plg63TvGfRSDCRg==", 118 | "requires": { 119 | "depd": "~1.1.2", 120 | "inherits": "2.0.3", 121 | "setprototypeof": "1.1.1", 122 | "statuses": ">= 1.5.0 < 2", 123 | "toidentifier": "1.0.0" 124 | } 125 | }, 126 | "inherits": { 127 | "version": "2.0.3", 128 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", 129 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" 130 | }, 131 | "is-generator-function": { 132 | "version": "1.0.7", 133 | "resolved": "https://registry.npmjs.org/is-generator-function/-/is-generator-function-1.0.7.tgz", 134 | "integrity": "sha512-YZc5EwyO4f2kWCax7oegfuSr9mFz1ZvieNYBEjmukLxgXfBUbxAWGVF7GZf0zidYtoBl3WvC07YK0wT76a+Rtw==" 135 | }, 136 | "keygrip": { 137 | "version": "1.0.3", 138 | "resolved": "https://registry.npmjs.org/keygrip/-/keygrip-1.0.3.tgz", 139 | "integrity": "sha512-/PpesirAIfaklxUzp4Yb7xBper9MwP6hNRA6BGGUFCgbJ+BM5CKBtsoxinNXkLHAr+GXS1/lSlF2rP7cv5Fl+g==" 140 | }, 141 | "koa": { 142 | "version": "2.7.0", 143 | "resolved": "https://registry.npmjs.org/koa/-/koa-2.7.0.tgz", 144 | "integrity": "sha512-7ojD05s2Q+hFudF8tDLZ1CpCdVZw8JQELWSkcfG9bdtoTDzMmkRF6BQBU7JzIzCCOY3xd3tftiy/loHBUYaY2Q==", 145 | "requires": { 146 | "accepts": "^1.3.5", 147 | "cache-content-type": "^1.0.0", 148 | "content-disposition": "~0.5.2", 149 | "content-type": "^1.0.4", 150 | "cookies": "~0.7.1", 151 | "debug": "~3.1.0", 152 | "delegates": "^1.0.0", 153 | "depd": "^1.1.2", 154 | "destroy": "^1.0.4", 155 | "error-inject": "^1.0.0", 156 | "escape-html": "^1.0.3", 157 | "fresh": "~0.5.2", 158 | "http-assert": "^1.3.0", 159 | "http-errors": "^1.6.3", 160 | "is-generator-function": "^1.0.7", 161 | "koa-compose": "^4.1.0", 162 | "koa-convert": "^1.2.0", 163 | "koa-is-json": "^1.0.0", 164 | "on-finished": "^2.3.0", 165 | "only": "~0.0.2", 166 | "parseurl": "^1.3.2", 167 | "statuses": "^1.5.0", 168 | "type-is": "^1.6.16", 169 | "vary": "^1.1.2" 170 | } 171 | }, 172 | "koa-compose": { 173 | "version": "4.1.0", 174 | "resolved": "https://registry.npmjs.org/koa-compose/-/koa-compose-4.1.0.tgz", 175 | "integrity": "sha512-8ODW8TrDuMYvXRwra/Kh7/rJo9BtOfPc6qO8eAfC80CnCvSjSl0bkRM24X6/XBBEyj0v1nRUQ1LyOy3dbqOWXw==" 176 | }, 177 | "koa-convert": { 178 | "version": "1.2.0", 179 | "resolved": "https://registry.npmjs.org/koa-convert/-/koa-convert-1.2.0.tgz", 180 | "integrity": "sha1-2kCHXfSd4FOQmNFwC1CCDOvNIdA=", 181 | "requires": { 182 | "co": "^4.6.0", 183 | "koa-compose": "^3.0.0" 184 | }, 185 | "dependencies": { 186 | "koa-compose": { 187 | "version": "3.2.1", 188 | "resolved": "https://registry.npmjs.org/koa-compose/-/koa-compose-3.2.1.tgz", 189 | "integrity": "sha1-qFzLQLfZhtjlo0Wzoazo6rz1Tec=", 190 | "requires": { 191 | "any-promise": "^1.1.0" 192 | } 193 | } 194 | } 195 | }, 196 | "koa-is-json": { 197 | "version": "1.0.0", 198 | "resolved": "https://registry.npmjs.org/koa-is-json/-/koa-is-json-1.0.0.tgz", 199 | "integrity": "sha1-JzwH7c3Ljfaiwat9We52SRRR7BQ=" 200 | }, 201 | "media-typer": { 202 | "version": "0.3.0", 203 | "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", 204 | "integrity": "sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=" 205 | }, 206 | "mime-db": { 207 | "version": "1.40.0", 208 | "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.40.0.tgz", 209 | "integrity": "sha512-jYdeOMPy9vnxEqFRRo6ZvTZ8d9oPb+k18PKoYNYUe2stVEBPPwsln/qWzdbmaIvnhZ9v2P+CuecK+fpUfsV2mA==" 210 | }, 211 | "mime-types": { 212 | "version": "2.1.24", 213 | "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.24.tgz", 214 | "integrity": "sha512-WaFHS3MCl5fapm3oLxU4eYDw77IQM2ACcxQ9RIxfaC3ooc6PFuBMGZZsYpvoXS5D5QTWPieo1jjLdAm3TBP3cQ==", 215 | "requires": { 216 | "mime-db": "1.40.0" 217 | } 218 | }, 219 | "ms": { 220 | "version": "2.0.0", 221 | "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", 222 | "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" 223 | }, 224 | "negotiator": { 225 | "version": "0.6.2", 226 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", 227 | "integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw==" 228 | }, 229 | "on-finished": { 230 | "version": "2.3.0", 231 | "resolved": "https://registry.npmjs.org/on-finished/-/on-finished-2.3.0.tgz", 232 | "integrity": "sha1-IPEzZIGwg811M3mSoWlxqi2QaUc=", 233 | "requires": { 234 | "ee-first": "1.1.1" 235 | } 236 | }, 237 | "only": { 238 | "version": "0.0.2", 239 | "resolved": "https://registry.npmjs.org/only/-/only-0.0.2.tgz", 240 | "integrity": "sha1-Kv3oTQPlC5qO3EROMGEKcCle37Q=" 241 | }, 242 | "parseurl": { 243 | "version": "1.3.3", 244 | "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.3.tgz", 245 | "integrity": "sha512-CiyeOxFT/JZyN5m0z9PfXw4SCBJ6Sygz1Dpl0wqjlhDEGGBP1GnsUVEL0p63hoG1fcj3fHynXi9NYO4nWOL+qQ==" 246 | }, 247 | "safe-buffer": { 248 | "version": "5.1.2", 249 | "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", 250 | "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" 251 | }, 252 | "setprototypeof": { 253 | "version": "1.1.1", 254 | "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz", 255 | "integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw==" 256 | }, 257 | "statuses": { 258 | "version": "1.5.0", 259 | "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz", 260 | "integrity": "sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=" 261 | }, 262 | "toidentifier": { 263 | "version": "1.0.0", 264 | "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", 265 | "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==" 266 | }, 267 | "type-is": { 268 | "version": "1.6.18", 269 | "resolved": "https://registry.npmjs.org/type-is/-/type-is-1.6.18.tgz", 270 | "integrity": "sha512-TkRKr9sUTxEH8MdfuCSP7VizJyzRNMjj2J2do2Jr3Kym598JVdEksuzPQCnlFPW4ky9Q+iA+ma9BGm06XQBy8g==", 271 | "requires": { 272 | "media-typer": "0.3.0", 273 | "mime-types": "~2.1.24" 274 | } 275 | }, 276 | "vary": { 277 | "version": "1.1.2", 278 | "resolved": "https://registry.npmjs.org/vary/-/vary-1.1.2.tgz", 279 | "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" 280 | }, 281 | "ylru": { 282 | "version": "1.2.1", 283 | "resolved": "https://registry.npmjs.org/ylru/-/ylru-1.2.1.tgz", 284 | "integrity": "sha512-faQrqNMzcPCHGVC2aaOINk13K+aaBDUPjGWl0teOXywElLjyVAB6Oe2jj62jHYtwsU49jXhScYbvPENK+6zAvQ==" 285 | } 286 | } 287 | } 288 | -------------------------------------------------------------------------------- /examples/restc-example-koa2/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc-example-koa2", 3 | "version": "1.0.0", 4 | "description": "An example to demostrate how to use restc in koa 2.x.", 5 | "private": true, 6 | "main": "index.js", 7 | "author": "lujjjh ", 8 | "license": "MIT", 9 | "dependencies": { 10 | "koa": "^2.0.0" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | declare module "restc" { 2 | export function koa2(): () => any; 3 | export function koa(): () => any; 4 | export function express(): () => any; 5 | export function hapi(): () => any; 6 | export function hapiLegacy(): () => any; 7 | } 8 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const express = require('./lib/express') 4 | const koa = require('./lib/koa') 5 | const koa2 = require('./lib/koa2') 6 | const hapi = require('./lib/hapi') 7 | const hapiLegacy = require('./lib/hapiLegacy') 8 | 9 | module.exports = { express, koa, koa2, hapi, hapiLegacy } 10 | -------------------------------------------------------------------------------- /lib/express.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const serve = require('.') 4 | 5 | module.exports = options => (req, res, next) => { 6 | serve(options)(req, res) || next() 7 | } 8 | -------------------------------------------------------------------------------- /lib/hapi.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const serve = require('.') 3 | 4 | const restcPlugin = { 5 | name: 'restcPlugin', 6 | version: '1.0.0', 7 | register: async function (server, options) { 8 | server.ext('onPreResponse', (request, reply) => { 9 | return new Promise((resolve, reject) => { 10 | if (!serve(options)(request.raw.req, request.raw.res)) { 11 | resolve(reply.continue) 12 | } 13 | }) 14 | }) 15 | } 16 | } 17 | exports.plugin = restcPlugin 18 | -------------------------------------------------------------------------------- /lib/hapiLegacy.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | const serve = require('.') 3 | 4 | exports.register = function (server, options, next) { 5 | server.ext('onPreResponse', (request, reply) => { 6 | if (!serve(options)(request.raw.req, request.raw.res)) { 7 | reply.continue() 8 | } 9 | }) 10 | return next() 11 | } 12 | exports.register.attributes = { 13 | name: 'restcPlugin', 14 | version: '1.0.0' 15 | } 16 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const fs = require('fs') 4 | const path = require('path') 5 | const Gateway = require('./utils/gateway.js') 6 | 7 | const cache = new WeakMap() 8 | 9 | module.exports = function (options = {}) { 10 | if (cache.has(options)) { 11 | return cache.get(options) 12 | } 13 | 14 | const { inline, includes, excludes, shouldHandle } = options 15 | 16 | const content = fs.readFileSync( 17 | path.join(__dirname, '..', 'dist', inline ? 'index.inline.html' : 'index.html'), 18 | 'utf-8' 19 | ) 20 | 21 | const gateway = new Gateway({ includes, excludes, shouldHandle }) 22 | const handler = function (req, res) { 23 | // set vary header for every response 24 | res.setHeader('Vary', 'Accept, Origin') 25 | 26 | // return if it is a cross-origin request 27 | if ('origin' in req.headers) { 28 | return false 29 | } 30 | 31 | // return if the client does not prefer text/html 32 | let accept = req.headers.accept || '' 33 | if (!/^text\/html(?:,|$)/.test(accept)) { 34 | return false 35 | } 36 | 37 | // feed the request to the gateway 38 | if (!gateway.shouldHandle(req)) { 39 | return false 40 | } 41 | 42 | res.statusCode = 200 43 | res.setHeader('Content-Type', 'text/html') 44 | // serve restc 45 | res.end(content) 46 | 47 | return true 48 | } 49 | 50 | cache.set(options, handler) 51 | return handler 52 | } 53 | -------------------------------------------------------------------------------- /lib/koa.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const serve = require('.') 4 | 5 | module.exports = options => function * (next) { 6 | if (!serve(options)(this.req, this.res)) { 7 | yield next 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /lib/koa2.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const serve = require('.') 4 | 5 | module.exports = options => (ctx, next) => { 6 | if (!serve(options)(ctx.req, ctx.res)) { 7 | return next() 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /lib/utils/gateway.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | function Gateway (options) { 4 | options = options || {} 5 | let includes = options.includes 6 | let excludes = options.excludes 7 | const shouldHandle = options.shouldHandle 8 | 9 | if (typeof shouldHandle === 'function') { 10 | Object.defineProperty(this, 'shouldHandle', { 11 | value: shouldHandle, 12 | configurable: true 13 | }) 14 | } else { 15 | if (includes && !(includes instanceof Array)) { 16 | includes = [includes] 17 | } 18 | if (excludes && !(excludes instanceof Array)) { 19 | excludes = [excludes] 20 | } 21 | this.includes = includes 22 | this.excludes = excludes 23 | } 24 | } 25 | 26 | Gateway.prototype.shouldHandle = function (req) { 27 | const test = (url, rule) => { 28 | switch (true) { 29 | case typeof rule === 'string': 30 | rule = rule.replace(/^\/*/, '/') 31 | return url.indexOf(rule) === 0 32 | case rule instanceof RegExp: 33 | return rule.test(url) 34 | } 35 | return false 36 | } 37 | 38 | const url = req.url 39 | 40 | if (this.excludes && this.excludes.some(rule => test(url, rule))) { 41 | return false 42 | } 43 | 44 | if (this.includes && this.includes.every(rule => !test(url, rule))) { 45 | return false 46 | } 47 | 48 | return true 49 | } 50 | 51 | module.exports = Gateway 52 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "restc", 3 | "version": "0.5.0", 4 | "description": "A server-side middleware to visualize REST requests.", 5 | "main": "index.js", 6 | "repository": "https://github.com/ElemeFE/restc.git", 7 | "license": "MIT", 8 | "typings": "index.d.ts", 9 | "files": [ 10 | "lib", 11 | "dist", 12 | "index.js", 13 | "index.d.ts" 14 | ], 15 | "scripts": { 16 | "build": "mkdir -p dist && cp src/index.html dist/ && ./scripts/inline.php", 17 | "lint": "eslint .", 18 | "lint-fix": "eslint --fix .", 19 | "test": "ava", 20 | "prepack": "npm run build" 21 | }, 22 | "devDependencies": { 23 | "ava": "^3.13.0", 24 | "eslint": "^4.2.0", 25 | "eslint-config-standard": "^10.2.1", 26 | "eslint-plugin-import": "^2.7.0", 27 | "eslint-plugin-node": "^5.1.0", 28 | "eslint-plugin-promise": "^3.5.0", 29 | "eslint-plugin-standard": "^3.0.1" 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /scripts/inline.php: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env php 2 | 2 | 3 | 4 | 5 | restc 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 1128 | 1129 | 1130 | -------------------------------------------------------------------------------- /src/index.txt: -------------------------------------------------------------------------------- 1 | Frame 2 | Header 3 | Logo 4 | SideBarToggleButton 5 | Main 6 | ResultView 7 | ResponseView 8 | HighlightBlock 9 | RequestView 10 | HighlightBlock 11 | SideBar 12 | SideBarMethod 13 | SideBarMethodSelect 14 | SideBarQueryStringInput : SideBarInput 15 | SideBarSendButton 16 | SideBarKeyValueCollection 17 | SideBarBodyEditor 18 | SideBarHeaderCollection : SideBarKeyValueCollection 19 | 20 | SideBarKeyValueCollection 21 | SideBarKeyValueAdd 22 | SideBarKeyValuePair 23 | SideBarKeyValuePairCheckBox 24 | SideBarKeyValuePairInput : SideBarInput 25 | SideBarKeyValuePairInput : SideBarInput 26 | 27 | Configuration 28 | 29 | QueryString 30 | 31 | loader 32 | -------------------------------------------------------------------------------- /test/gateway.js: -------------------------------------------------------------------------------- 1 | 'use strict' 2 | 3 | const test = require('ava') 4 | const Gateway = require('../lib/utils/gateway') 5 | 6 | test('empty gateway should let the requests pass', t => { 7 | const gateway = new Gateway() 8 | t.is(gateway.shouldHandle({ url: '/' }), true) 9 | t.is(gateway.shouldHandle({ url: '/foo' }), true) 10 | t.is(gateway.shouldHandle({ url: '/foo/bar?baz' }), true) 11 | }) 12 | 13 | test('gateway should only let requests that matches includes pass', t => { 14 | const gateway = new Gateway({ 15 | includes: [ 16 | '/foo', 17 | /^\/bar/, 18 | /baz$/ 19 | ] 20 | }) 21 | t.is(gateway.shouldHandle({ url: '/' }), false) 22 | t.is(gateway.shouldHandle({ url: '/foo' }), true) 23 | t.is(gateway.shouldHandle({ url: '/bar' }), true) 24 | t.is(gateway.shouldHandle({ url: '/baz/' }), false) 25 | t.is(gateway.shouldHandle({ url: '/?baz' }), true) 26 | }) 27 | 28 | test('gateway must prevent requests that matches excludes', t => { 29 | const gateway = new Gateway({ 30 | excludes: [ 31 | '/foo', 32 | /^\/bar/, 33 | /baz$/ 34 | ] 35 | }) 36 | t.is(gateway.shouldHandle({ url: '/' }), true) 37 | t.is(gateway.shouldHandle({ url: '/foo' }), false) 38 | t.is(gateway.shouldHandle({ url: '/bar' }), false) 39 | t.is(gateway.shouldHandle({ url: '/baz/' }), true) 40 | t.is(gateway.shouldHandle({ url: '/?baz' }), false) 41 | }) 42 | 43 | test('excludes should have higher priority over includes', t => { 44 | const gateway = new Gateway({ 45 | includes: [ 46 | '/foo', 47 | /^\/bar/ 48 | ], 49 | excludes: [ 50 | '/bar/baz' 51 | ] 52 | }) 53 | t.is(gateway.shouldHandle({ url: '/' }), false) 54 | t.is(gateway.shouldHandle({ url: '/foo' }), true) 55 | t.is(gateway.shouldHandle({ url: '/bar' }), true) 56 | t.is(gateway.shouldHandle({ url: '/bar/baz' }), false) 57 | }) 58 | 59 | test('includes and excludes should process non-array properly', t => { 60 | const gateway = new Gateway({ 61 | includes: '/foo', 62 | excludes: '/foo/bar' 63 | }) 64 | t.is(gateway.shouldHandle({ url: '/' }), false) 65 | t.is(gateway.shouldHandle({ url: '/foo' }), true) 66 | t.is(gateway.shouldHandle({ url: '/foo/42' }), true) 67 | t.is(gateway.shouldHandle({ url: '/foo/bar' }), false) 68 | }) 69 | 70 | test('gateway should directly call shouldHandle if provided and ignore includes and excludes', t => { 71 | const gateway = new Gateway({ 72 | includes: '/foo', 73 | excludes: [ 74 | '/foo/bar', 75 | '/42' 76 | ], 77 | shouldHandle: req => req.url === '/42' 78 | }) 79 | t.is(gateway.shouldHandle({ url: '/' }), false) 80 | t.is(gateway.shouldHandle({ url: '/foo' }), false) 81 | t.is(gateway.shouldHandle({ url: '/foo/42' }), false) 82 | t.is(gateway.shouldHandle({ url: '/42' }), true) 83 | }) 84 | --------------------------------------------------------------------------------