Skip to content

Commit 7a5fe8c

Browse files
committed
feat: middleware koa-health and koa-openapi
1 parent f77d5d8 commit 7a5fe8c

File tree

19 files changed

+311
-3
lines changed

19 files changed

+311
-3
lines changed

packages/fastman/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "0.3.1",
44
"description": "A tool to export and import postman collection file",
55
"repository": {
6-
"url": "undefined/undefined",
6+
"url": "36node/sketch",
77
"type": "git"
88
},
99
"license": "MIT",

packages/fetch/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"browser": "dist/browser.mjs",
2121
"main": "dist/index.js",
2222
"module": "dist/index.mjs",
23+
"typings": "typings/index.d.ts",
2324
"scripts": {
2425
"build": "sketch build src/{browser,index}.js",
2526
"test": "NODE_ENV=test sketch test --env=node"

packages/koa-health/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 36node <zzswang@36node.com> (https://github.com/36node)
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
13+
all 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
21+
THE SOFTWARE.

packages/koa-health/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# @36node/koa-health
2+
3+
[![version][0]][1] [![downloads][2]][3]
4+
5+
## Install
6+
7+
```bash
8+
yarn add module
9+
```
10+
11+
## Usage
12+
13+
```js
14+
import module from "module";
15+
16+
module();
17+
//=> foo
18+
```
19+
20+
## Contributing
21+
22+
1. Fork it!
23+
2. Create your feature branch: `git checkout -b my-new-feature`
24+
3. Commit your changes: `git commit -am 'Add some feature'`
25+
4. Push to the branch: `git push origin my-new-feature`
26+
5. Submit a pull request :D
27+
28+
## Author
29+
30+
**module** © [36node](https://github.com/36node), Released under the [MIT](./LICENSE) License.
31+
32+
Authored and maintained by 36node with help from contributors ([list](https://github.com/36node/module/contributors)).
33+
34+
> [github.com/zzswang](https://github.com/zzswang) · GitHub [@36node](https://github.com/36node)
35+
36+
[0]: https://img.shields.io/npm/v/@36node/koa-health.svg?style=flat
37+
[1]: https://npmjs.com/package/@36node/koa-health
38+
[2]: https://img.shields.io/npm/dm/@36node/koa-health.svg?style=flat
39+
[3]: https://npmjs.com/package/@36node/koa-health

packages/koa-health/package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "@36node/koa-health",
3+
"version": "0.0.0",
4+
"description": "koa health middleware for 36node team.",
5+
"repository": {
6+
"url": "36node/sketch",
7+
"type": "git"
8+
},
9+
"license": "MIT",
10+
"publishConfig": {
11+
"access": "public"
12+
},
13+
"template": "module",
14+
"files": [
15+
"bin",
16+
"dist",
17+
"typings"
18+
],
19+
"config-overrides-path": "../cli/config-overrides",
20+
"browser": "dist/browser.mjs",
21+
"main": "dist/index.js",
22+
"module": "dist/index.mjs",
23+
"typings": "typings/index.d.ts",
24+
"scripts": {
25+
"build": "sketch build",
26+
"test": "sketch test --env=node"
27+
},
28+
"dependencies": {},
29+
"gitHead": "9c3f6d6834be075aa49460dd9c5e277f2a7573ad"
30+
}

packages/koa-health/src/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { escape } from "./lib/regex";
2+
3+
/**
4+
* health 中间件
5+
* @param {Object} options 生成 health 中间件的参数
6+
* @param {String} options.url 路径,默认 /health
7+
* @param {String} options.version the version of api
8+
*/
9+
export default function({ url = "/health", version }) {
10+
const regex = new RegExp(`${escape(url)}$`);
11+
return (ctx, next) => {
12+
if (ctx.url.match(regex)) {
13+
ctx.body = {
14+
version,
15+
status: "OK",
16+
load: 0.9,
17+
check: [
18+
{
19+
name: "mysql",
20+
status: "WARNING",
21+
reason:
22+
"db schema version (0.9.0) conflict with service api version (1.0.0)",
23+
},
24+
{
25+
name: "mongo",
26+
status: "OK",
27+
},
28+
{
29+
name: "sms-service",
30+
status: "ERROR",
31+
reason: "connection timeout",
32+
},
33+
],
34+
};
35+
} else {
36+
return next();
37+
}
38+
};
39+
}

packages/koa-health/src/index.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import health from "./index";
2+
3+
test("health", () => {
4+
expect(health()).toHaveBeenCalledWith();
5+
});

packages/koa-health/src/lib/regex.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const escape = function(string) {
2+
return string.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
3+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Koa = require("koa");
2+
3+
declare module "@36node/koa-health" {
4+
interface Options {
5+
url: string;
6+
version: string;
7+
}
8+
9+
/**
10+
* health 中间件
11+
* @param {Object} options 生成 health 中间件的参数
12+
* @param {String} options.url 路径,默认 /health
13+
* @param {String} options.version the version of api
14+
*/
15+
export function health(options: Options): Function;
16+
}

packages/koa-openapi/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 36node <zzswang@36node.com> (https://github.com/36node)
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
13+
all 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
21+
THE SOFTWARE.

0 commit comments

Comments
 (0)