@hoajs/request-id 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/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ## v0.1.0 / 2025-10-09
2
+
3
+ - init
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 - present, Hoa 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,39 @@
1
+ ## @hoajs/request-id
2
+
3
+ Request ID middleware for Hoa.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ $ npm i @hoajs/request-id --save
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```js
14
+ import { Hoa } from 'hoa'
15
+ import { requestId } from '@hoajs/request-id'
16
+
17
+ const app = new Hoa()
18
+ app.use(requestId())
19
+
20
+ app.use(async (ctx) => {
21
+ ctx.res.body = `Hello, ${ctx.state.requestId}!`
22
+ })
23
+
24
+ export default app
25
+ ```
26
+
27
+ ## Documentation
28
+
29
+ The documentation is available on [hoa-js.com](https://hoa-js.com/middleware/request-id.html)
30
+
31
+ ## Test (100% coverage)
32
+
33
+ ```sh
34
+ $ npm test
35
+ ```
36
+
37
+ ## License
38
+
39
+ MIT
@@ -0,0 +1,56 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
9
+ var __copyProps = (to, from, except, desc) => {
10
+ if (from && typeof from === "object" || typeof from === "function") {
11
+ for (let key of __getOwnPropNames(from))
12
+ if (!__hasOwnProp.call(to, key) && key !== except)
13
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
14
+ }
15
+ return to;
16
+ };
17
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
18
+ var request_id_exports = {};
19
+ __export(request_id_exports, {
20
+ default: () => request_id_default,
21
+ requestId: () => requestId
22
+ });
23
+ module.exports = __toCommonJS(request_id_exports);
24
+ function requestId(options = {}) {
25
+ const limitLength = options.limitLength ?? 255;
26
+ const headerName = options.headerName ?? "X-Request-Id";
27
+ const generator = options.generator ?? (() => crypto.randomUUID());
28
+ return async function hoaRequestId(ctx, next) {
29
+ let reqId = headerName ? ctx.req.get(headerName) : void 0;
30
+ if (!reqId || reqId.length > limitLength || /[^\w-]/.test(reqId)) {
31
+ reqId = generator(ctx);
32
+ }
33
+ ctx.state.requestId = reqId;
34
+ try {
35
+ await next();
36
+ } catch (err) {
37
+ const errHeaders = err.headers ? Object.fromEntries(new Headers(err.headers).entries()) : {};
38
+ if (headerName) {
39
+ errHeaders[headerName] = reqId;
40
+ err.headers = errHeaders;
41
+ } else if (err.headers) {
42
+ err.headers = errHeaders;
43
+ }
44
+ throw err;
45
+ } finally {
46
+ if (headerName) {
47
+ ctx.res.set(headerName, reqId);
48
+ }
49
+ }
50
+ };
51
+ }
52
+ var request_id_default = requestId;
53
+ // Annotate the CommonJS export names for ESM import in node:
54
+ 0 && (module.exports = {
55
+ requestId
56
+ });
@@ -0,0 +1,33 @@
1
+ function requestId(options = {}) {
2
+ const limitLength = options.limitLength ?? 255;
3
+ const headerName = options.headerName ?? "X-Request-Id";
4
+ const generator = options.generator ?? (() => crypto.randomUUID());
5
+ return async function hoaRequestId(ctx, next) {
6
+ let reqId = headerName ? ctx.req.get(headerName) : void 0;
7
+ if (!reqId || reqId.length > limitLength || /[^\w-]/.test(reqId)) {
8
+ reqId = generator(ctx);
9
+ }
10
+ ctx.state.requestId = reqId;
11
+ try {
12
+ await next();
13
+ } catch (err) {
14
+ const errHeaders = err.headers ? Object.fromEntries(new Headers(err.headers).entries()) : {};
15
+ if (headerName) {
16
+ errHeaders[headerName] = reqId;
17
+ err.headers = errHeaders;
18
+ } else if (err.headers) {
19
+ err.headers = errHeaders;
20
+ }
21
+ throw err;
22
+ } finally {
23
+ if (headerName) {
24
+ ctx.res.set(headerName, reqId);
25
+ }
26
+ }
27
+ };
28
+ }
29
+ var request_id_default = requestId;
30
+ export {
31
+ request_id_default as default,
32
+ requestId
33
+ };
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@hoajs/request-id",
3
+ "version": "0.1.0",
4
+ "description": "Request ID middleware for Hoa.",
5
+ "main": "./dist/cjs/request-id.js",
6
+ "type": "module",
7
+ "module": "./dist/esm/request-id.js",
8
+ "types": "./types/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./types/index.d.ts",
12
+ "import": "./dist/esm/request-id.js",
13
+ "require": "./dist/cjs/request-id.js",
14
+ "default": "./dist/esm/request-id.js"
15
+ }
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "types",
20
+ "CHANGELOG.md"
21
+ ],
22
+ "scripts": {
23
+ "lint": "eslint .",
24
+ "build": "tsup",
25
+ "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js",
26
+ "prepublishOnly": "npm run lint && npm run test && npm run build",
27
+ "prepare": "husky"
28
+ },
29
+ "author": "nswbmw",
30
+ "license": "MIT",
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "git+https://github.com/hoa-js/request-id.git"
34
+ },
35
+ "keywords": [
36
+ "hoa",
37
+ "middleware",
38
+ "requestId",
39
+ "request-id",
40
+ "x-request-id"
41
+ ],
42
+ "devDependencies": {
43
+ "@commitlint/cli": "20.1.0",
44
+ "@commitlint/config-conventional": "20.0.0",
45
+ "eslint": "9.37.0",
46
+ "globals": "16.4.0",
47
+ "hoa": "0.1.0",
48
+ "husky": "9.1.7",
49
+ "jest": "30.2.0",
50
+ "neostandard": "0.12.2",
51
+ "tsup": "8.5.0"
52
+ },
53
+ "peerDependencies": {
54
+ "hoa": "*"
55
+ },
56
+ "engines": {
57
+ "node": ">=20"
58
+ }
59
+ }
@@ -0,0 +1,15 @@
1
+ import type { HoaContext } from 'hoa'
2
+
3
+ export type RequestIdGenerator = (ctx: HoaContext) => string
4
+
5
+ export interface RequestIdOptions {
6
+ limitLength?: number
7
+ headerName?: string
8
+ generator?: RequestIdGenerator
9
+ }
10
+
11
+ export type RequestIdMiddleware = (ctx: HoaContext, next: () => Promise<void>) => Promise<void>
12
+
13
+ export function requestId(options?: RequestIdOptions): RequestIdMiddleware
14
+
15
+ export default requestId