@mihari/logger-koa 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Mihari 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.
package/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # @mihari/logger-koa
2
+
3
+ Koa request logging middleware for the mihari log collection library.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @mihari/logger-koa koa
9
+ ```
10
+
11
+ ## Usage
12
+
13
+ ```typescript
14
+ import Koa from "koa";
15
+ import { mihariMiddleware } from "@mihari/logger-koa";
16
+
17
+ const app = new Koa();
18
+
19
+ app.use(mihariMiddleware({
20
+ token: "your-api-token",
21
+ endpoint: "https://logs.example.com",
22
+ }));
23
+
24
+ app.use(async (ctx) => {
25
+ ctx.body = "Hello World";
26
+ });
27
+
28
+ app.listen(3000);
29
+ ```
30
+
31
+ ## Logged Fields
32
+
33
+ | Field | Description |
34
+ |-------|-------------|
35
+ | method | HTTP method (GET, POST, etc.) |
36
+ | url | Request URL path |
37
+ | status | Response status code |
38
+ | responseTimeMs | Response time in milliseconds |
39
+ | userAgent | User-Agent header value |
40
+ | contentLength | Response content length |
41
+ | ip | Client IP address |
42
+
43
+ ## Log Level Selection
44
+
45
+ - 5xx responses: error
46
+ - 4xx responses: warn
47
+ - All other responses: info
48
+
49
+ ## License
50
+
51
+ MIT
@@ -0,0 +1,53 @@
1
+ import { MihariConfig } from "@mihari/logger-types";
2
+ /**
3
+ * Minimal Koa context type to avoid requiring koa as a direct dependency.
4
+ */
5
+ interface KoaContext {
6
+ readonly method: string;
7
+ readonly url: string;
8
+ readonly status: number;
9
+ readonly ip: string;
10
+ readonly request: {
11
+ readonly length?: number;
12
+ readonly get: (header: string) => string;
13
+ };
14
+ readonly response: {
15
+ readonly length?: number;
16
+ };
17
+ }
18
+ type KoaNext = () => Promise<void>;
19
+ type KoaMiddleware = (ctx: KoaContext, next: KoaNext) => Promise<void>;
20
+ /**
21
+ * Creates Koa middleware that logs every HTTP request to mihari.
22
+ *
23
+ * Captured fields:
24
+ * - method: HTTP method (GET, POST, etc.)
25
+ * - url: Request URL
26
+ * - status: Response status code
27
+ * - responseTimeMs: Time to handle the request in milliseconds
28
+ * - userAgent: User-Agent header
29
+ * - contentLength: Response content length
30
+ * - ip: Client IP address
31
+ *
32
+ * Usage:
33
+ * ```typescript
34
+ * import Koa from "koa";
35
+ * import { mihariMiddleware } from "@mihari/logger-koa";
36
+ *
37
+ * const app = new Koa();
38
+ *
39
+ * app.use(mihariMiddleware({
40
+ * token: "your-token",
41
+ * endpoint: "https://logs.example.com",
42
+ * }));
43
+ *
44
+ * app.use(async (ctx) => {
45
+ * ctx.body = "Hello World";
46
+ * });
47
+ *
48
+ * app.listen(3000);
49
+ * ```
50
+ */
51
+ export declare function mihariMiddleware(config: MihariConfig): KoaMiddleware;
52
+ export default mihariMiddleware;
53
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAE9D;;GAEG;AACH,UAAU,UAAU;IAClB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,OAAO,EAAE;QAChB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;QACzB,QAAQ,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,MAAM,KAAK,MAAM,CAAC;KAC1C,CAAC;IACF,QAAQ,CAAC,QAAQ,EAAE;QACjB,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;KAC1B,CAAC;CACH;AAED,KAAK,OAAO,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;AACnC,KAAK,aAAa,GAAG,CAAC,GAAG,EAAE,UAAU,EAAE,IAAI,EAAE,OAAO,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;AAEvE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,YAAY,GAAG,aAAa,CA6BpE;AAED,eAAe,gBAAgB,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.mihariMiddleware = mihariMiddleware;
4
+ const logger_core_1 = require("@mihari/logger-core");
5
+ const logger_types_1 = require("@mihari/logger-types");
6
+ /**
7
+ * Creates Koa middleware that logs every HTTP request to mihari.
8
+ *
9
+ * Captured fields:
10
+ * - method: HTTP method (GET, POST, etc.)
11
+ * - url: Request URL
12
+ * - status: Response status code
13
+ * - responseTimeMs: Time to handle the request in milliseconds
14
+ * - userAgent: User-Agent header
15
+ * - contentLength: Response content length
16
+ * - ip: Client IP address
17
+ *
18
+ * Usage:
19
+ * ```typescript
20
+ * import Koa from "koa";
21
+ * import { mihariMiddleware } from "@mihari/logger-koa";
22
+ *
23
+ * const app = new Koa();
24
+ *
25
+ * app.use(mihariMiddleware({
26
+ * token: "your-token",
27
+ * endpoint: "https://logs.example.com",
28
+ * }));
29
+ *
30
+ * app.use(async (ctx) => {
31
+ * ctx.body = "Hello World";
32
+ * });
33
+ *
34
+ * app.listen(3000);
35
+ * ```
36
+ */
37
+ function mihariMiddleware(config) {
38
+ const client = new logger_core_1.MihariClient(config);
39
+ return async (ctx, next) => {
40
+ const startTime = Date.now();
41
+ try {
42
+ await next();
43
+ }
44
+ finally {
45
+ const responseTimeMs = Date.now() - startTime;
46
+ const status = ctx.status;
47
+ const level = status >= 500
48
+ ? logger_types_1.LogLevel.Error
49
+ : status >= 400
50
+ ? logger_types_1.LogLevel.Warn
51
+ : logger_types_1.LogLevel.Info;
52
+ client.log(level, `${ctx.method} ${ctx.url} ${status}`, {
53
+ method: ctx.method,
54
+ url: ctx.url,
55
+ status,
56
+ responseTimeMs,
57
+ userAgent: ctx.request.get("user-agent"),
58
+ contentLength: ctx.response.length,
59
+ ip: ctx.ip,
60
+ });
61
+ }
62
+ };
63
+ }
64
+ exports.default = mihariMiddleware;
65
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAsDA,4CA6BC;AAnFD,qDAAmD;AACnD,uDAA8D;AAsB9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8BG;AACH,SAAgB,gBAAgB,CAAC,MAAoB;IACnD,MAAM,MAAM,GAAG,IAAI,0BAAY,CAAC,MAAM,CAAC,CAAC;IAExC,OAAO,KAAK,EAAE,GAAe,EAAE,IAAa,EAAiB,EAAE;QAC7D,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAE7B,IAAI,CAAC;YACH,MAAM,IAAI,EAAE,CAAC;QACf,CAAC;gBAAS,CAAC;YACT,MAAM,cAAc,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,SAAS,CAAC;YAC9C,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;YAE1B,MAAM,KAAK,GAAG,MAAM,IAAI,GAAG;gBACzB,CAAC,CAAC,uBAAQ,CAAC,KAAK;gBAChB,CAAC,CAAC,MAAM,IAAI,GAAG;oBACb,CAAC,CAAC,uBAAQ,CAAC,IAAI;oBACf,CAAC,CAAC,uBAAQ,CAAC,IAAI,CAAC;YAEpB,MAAM,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,GAAG,CAAC,MAAM,IAAI,GAAG,CAAC,GAAG,IAAI,MAAM,EAAE,EAAE;gBACtD,MAAM,EAAE,GAAG,CAAC,MAAM;gBAClB,GAAG,EAAE,GAAG,CAAC,GAAG;gBACZ,MAAM;gBACN,cAAc;gBACd,SAAS,EAAE,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;gBACxC,aAAa,EAAE,GAAG,CAAC,QAAQ,CAAC,MAAM;gBAClC,EAAE,EAAE,GAAG,CAAC,EAAE;aACX,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC;AACJ,CAAC;AAED,kBAAe,gBAAgB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@mihari/logger-koa",
3
+ "version": "0.1.0",
4
+ "description": "Koa request logging middleware for mihari",
5
+ "main": "src/index.ts",
6
+ "types": "src/index.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc",
12
+ "clean": "rm -rf dist",
13
+ "test": "cd ../.. && npx vitest run --config vitest.config.ts packages/koa"
14
+ },
15
+ "devDependencies": {
16
+ "vitest": "^3.0.0"
17
+ },
18
+ "dependencies": {
19
+ "@mihari/logger-core": "^0.1.0",
20
+ "@mihari/logger-types": "^0.1.0"
21
+ },
22
+ "peerDependencies": {
23
+ "koa": ">=2.0.0"
24
+ },
25
+ "publishConfig": {
26
+ "access": "public"
27
+ },
28
+ "license": "MIT",
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/mihari/mihari-js",
32
+ "directory": "packages/koa"
33
+ },
34
+ "gitHead": "dc10a3217caa819965eb3a1e2ff3901a16e510aa"
35
+ }
package/src/index.ts ADDED
@@ -0,0 +1,86 @@
1
+ import { MihariClient } from "@mihari/logger-core";
2
+ import { LogLevel, MihariConfig } from "@mihari/logger-types";
3
+
4
+ /**
5
+ * Minimal Koa context type to avoid requiring koa as a direct dependency.
6
+ */
7
+ interface KoaContext {
8
+ readonly method: string;
9
+ readonly url: string;
10
+ readonly status: number;
11
+ readonly ip: string;
12
+ readonly request: {
13
+ readonly length?: number;
14
+ readonly get: (header: string) => string;
15
+ };
16
+ readonly response: {
17
+ readonly length?: number;
18
+ };
19
+ }
20
+
21
+ type KoaNext = () => Promise<void>;
22
+ type KoaMiddleware = (ctx: KoaContext, next: KoaNext) => Promise<void>;
23
+
24
+ /**
25
+ * Creates Koa middleware that logs every HTTP request to mihari.
26
+ *
27
+ * Captured fields:
28
+ * - method: HTTP method (GET, POST, etc.)
29
+ * - url: Request URL
30
+ * - status: Response status code
31
+ * - responseTimeMs: Time to handle the request in milliseconds
32
+ * - userAgent: User-Agent header
33
+ * - contentLength: Response content length
34
+ * - ip: Client IP address
35
+ *
36
+ * Usage:
37
+ * ```typescript
38
+ * import Koa from "koa";
39
+ * import { mihariMiddleware } from "@mihari/logger-koa";
40
+ *
41
+ * const app = new Koa();
42
+ *
43
+ * app.use(mihariMiddleware({
44
+ * token: "your-token",
45
+ * endpoint: "https://logs.example.com",
46
+ * }));
47
+ *
48
+ * app.use(async (ctx) => {
49
+ * ctx.body = "Hello World";
50
+ * });
51
+ *
52
+ * app.listen(3000);
53
+ * ```
54
+ */
55
+ export function mihariMiddleware(config: MihariConfig): KoaMiddleware {
56
+ const client = new MihariClient(config);
57
+
58
+ return async (ctx: KoaContext, next: KoaNext): Promise<void> => {
59
+ const startTime = Date.now();
60
+
61
+ try {
62
+ await next();
63
+ } finally {
64
+ const responseTimeMs = Date.now() - startTime;
65
+ const status = ctx.status;
66
+
67
+ const level = status >= 500
68
+ ? LogLevel.Error
69
+ : status >= 400
70
+ ? LogLevel.Warn
71
+ : LogLevel.Info;
72
+
73
+ client.log(level, `${ctx.method} ${ctx.url} ${status}`, {
74
+ method: ctx.method,
75
+ url: ctx.url,
76
+ status,
77
+ responseTimeMs,
78
+ userAgent: ctx.request.get("user-agent"),
79
+ contentLength: ctx.response.length,
80
+ ip: ctx.ip,
81
+ });
82
+ }
83
+ };
84
+ }
85
+
86
+ export default mihariMiddleware;