@guanghechen/middleware 1.0.6 → 2.0.1

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) 2023-present guanghechen (https://github.com/guanghechen)
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,131 @@
1
+ <header>
2
+ <h1 align="center">
3
+ <a href="https://github.com/guanghechen/sora/tree/@guanghechen/middleware@2.0.0/packages/middleware#readme">@guanghechen/middleware</a>
4
+ </h1>
5
+ <div align="center">
6
+ <a href="https://www.npmjs.com/package/@guanghechen/middleware">
7
+ <img
8
+ alt="Npm Version"
9
+ src="https://img.shields.io/npm/v/@guanghechen/middleware.svg"
10
+ />
11
+ </a>
12
+ <a href="https://www.npmjs.com/package/@guanghechen/middleware">
13
+ <img
14
+ alt="Npm Download"
15
+ src="https://img.shields.io/npm/dm/@guanghechen/middleware.svg"
16
+ />
17
+ </a>
18
+ <a href="https://www.npmjs.com/package/@guanghechen/middleware">
19
+ <img
20
+ alt="Npm License"
21
+ src="https://img.shields.io/npm/l/@guanghechen/middleware.svg"
22
+ />
23
+ </a>
24
+ <a href="#install">
25
+ <img
26
+ alt="Module Formats: cjs, esm"
27
+ src="https://img.shields.io/badge/module_formats-cjs%2C%20esm-green.svg"
28
+ />
29
+ </a>
30
+ <a href="https://github.com/nodejs/node">
31
+ <img
32
+ alt="Node.js Version"
33
+ src="https://img.shields.io/node/v/@guanghechen/middleware"
34
+ />
35
+ </a>
36
+ <a href="https://github.com/facebook/jest">
37
+ <img
38
+ alt="Tested with Jest"
39
+ src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
40
+ />
41
+ </a>
42
+ <a href="https://github.com/prettier/prettier">
43
+ <img
44
+ alt="Code Style: prettier"
45
+ src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square"
46
+ />
47
+ </a>
48
+ </div>
49
+ </header>
50
+ <br/>
51
+
52
+ Middleware pattern implementation for both synchronous and asynchronous processing pipelines.
53
+
54
+ ## Install
55
+
56
+ - npm
57
+
58
+ ```bash
59
+ npm install --save @guanghechen/middleware
60
+ ```
61
+
62
+ - yarn
63
+
64
+ ```bash
65
+ yarn add @guanghechen/middleware
66
+ ```
67
+
68
+ ## Usage
69
+
70
+ | Name | Description |
71
+ | :-----------------: | :-------------------------------------------------------: |
72
+ | `AsyncMiddlewares` | Asynchronous middleware chain for processing data |
73
+ | `Middlewares` | Synchronous middleware chain for processing data |
74
+
75
+ ## Example
76
+
77
+ - Async middleware:
78
+
79
+ ```typescript
80
+ import { AsyncMiddlewares } from '@guanghechen/middleware'
81
+
82
+ const middlewares = new AsyncMiddlewares<string, string, { prefix: string }>()
83
+
84
+ // Add middlewares (executed in registration order)
85
+ middlewares.use(async (input, embryo, api, next) => {
86
+ // Transform input to uppercase, pass to next middleware
87
+ return next(input.toUpperCase())
88
+ })
89
+
90
+ middlewares.use(async (input, embryo, api, next) => {
91
+ // Wrap with brackets, embryo contains result from previous middleware
92
+ return next(embryo ? `[${embryo}]` : null)
93
+ })
94
+
95
+ middlewares.use(async (input, embryo, api, next) => {
96
+ // Add prefix from api
97
+ return embryo ? `${api.prefix}${embryo}` : null
98
+ })
99
+
100
+ // Execute middleware chain
101
+ const reducer = middlewares.reducer('hello', { prefix: '> ' })
102
+ const result = await reducer(null) // Result: "> [HELLO]"
103
+ ```
104
+
105
+ - Sync middleware:
106
+
107
+ ```typescript
108
+ import { Middlewares } from '@guanghechen/middleware'
109
+
110
+ const middlewares = new Middlewares<number, number, { multiplier: number }>()
111
+
112
+ middlewares.use((input, embryo, api, next) => {
113
+ // First middleware: multiply input
114
+ return next(input * api.multiplier)
115
+ })
116
+
117
+ middlewares.use((input, embryo, api, next) => {
118
+ // Second middleware: add 1 to embryo (result from previous)
119
+ return embryo !== null ? embryo + 1 : null
120
+ })
121
+
122
+ const reducer = middlewares.reducer(5, { multiplier: 2 })
123
+ const result = reducer(null) // Result: 11 (5 * 2 + 1)
124
+ ```
125
+
126
+ ## Reference
127
+
128
+ - [homepage][homepage]
129
+
130
+ [homepage]:
131
+ https://github.com/guanghechen/sora/tree/@guanghechen/middleware@2.0.0/packages/middleware#readme
@@ -1,9 +1,5 @@
1
- interface IAsyncMiddlewareNext<Output> {
2
- (embryo: Readonly<Output | null>): Promise<Output | null>;
3
- }
4
- interface IAsyncMiddleware<Input, Output, Api> {
5
- (input: Readonly<Input>, embryo: Readonly<Output> | null, api: Readonly<Api>, next: IAsyncMiddlewareNext<Output>): Promise<Output | null>;
6
- }
1
+ type IAsyncMiddlewareNext<Output> = (embryo: Readonly<Output | null>) => Promise<Output | null>;
2
+ type IAsyncMiddleware<Input, Output, Api> = (input: Readonly<Input>, embryo: Readonly<Output> | null, api: Readonly<Api>, next: IAsyncMiddlewareNext<Output>) => Promise<Output | null>;
7
3
  interface IAsyncMiddlewares<Input, Output, Api> {
8
4
  /**
9
5
  * Use a middleware.
@@ -24,12 +20,8 @@ declare class AsyncMiddlewares<Input, Output, Api> implements IAsyncMiddlewares<
24
20
  reducer(input: Readonly<Input>, api: Readonly<Api>): IAsyncMiddlewareNext<Output>;
25
21
  }
26
22
 
27
- interface IMiddlewareNext<Output> {
28
- (embryo: Readonly<Output | null>): Output | null;
29
- }
30
- interface IMiddleware<Input, Output, Api> {
31
- (input: Readonly<Input>, embryo: Readonly<Output> | null, api: Readonly<Api>, next: IMiddlewareNext<Output>): Output | null;
32
- }
23
+ type IMiddlewareNext<Output> = (embryo: Readonly<Output | null>) => Output | null;
24
+ type IMiddleware<Input, Output, Api> = (input: Readonly<Input>, embryo: Readonly<Output> | null, api: Readonly<Api>, next: IMiddlewareNext<Output>) => Output | null;
33
25
  interface IMiddlewares<Input, Output, Api> {
34
26
  /**
35
27
  * Use a middleware.
@@ -50,4 +42,5 @@ declare class Middlewares<Input, Output, Api> implements IMiddlewares<Input, Out
50
42
  reducer(input: Readonly<Input>, api: Readonly<Api>): IMiddlewareNext<Output>;
51
43
  }
52
44
 
53
- export { AsyncMiddlewares, type IAsyncMiddleware, type IAsyncMiddlewareNext, type IAsyncMiddlewares, type IMiddleware, type IMiddlewareNext, type IMiddlewares, Middlewares };
45
+ export { AsyncMiddlewares, Middlewares };
46
+ export type { IAsyncMiddleware, IAsyncMiddlewareNext, IAsyncMiddlewares, IMiddleware, IMiddlewareNext, IMiddlewares };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guanghechen/middleware",
3
- "version": "1.0.6",
3
+ "version": "2.0.1",
4
4
  "description": "types of @guanghechen/middleware",
5
5
  "author": {
6
6
  "name": "guanghechen",
@@ -8,17 +8,17 @@
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "https://github.com/guanghechen/sora/tree/@guanghechen/middleware@1.0.5",
11
+ "url": "https://github.com/guanghechen/sora/tree/@guanghechen/middleware@2.0.0",
12
12
  "directory": "packages/middleware"
13
13
  },
14
- "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/middleware@1.0.5/packages/middleware#readme",
14
+ "homepage": "https://github.com/guanghechen/sora/tree/@guanghechen/middleware@2.0.0/packages/middleware#readme",
15
15
  "type": "module",
16
16
  "exports": {
17
17
  ".": {
18
+ "types": "./lib/types/index.d.ts",
18
19
  "source": "./src/index.ts",
19
20
  "import": "./lib/esm/index.mjs",
20
- "require": "./lib/cjs/index.cjs",
21
- "types": "./lib/types/index.d.ts"
21
+ "require": "./lib/cjs/index.cjs"
22
22
  }
23
23
  },
24
24
  "source": "./src/index.ts",
@@ -34,5 +34,11 @@
34
34
  "LICENSE",
35
35
  "README.md"
36
36
  ],
37
- "gitHead": "c46c7cc64c02c61047fbc7dff2ce8f6b21d4da2c"
38
- }
37
+ "scripts": {
38
+ "build": "rollup -c ../../rollup.config.mjs",
39
+ "clean": "rimraf lib",
40
+ "test": "vitest run --config ../../vitest.config.ts",
41
+ "test:coverage": "vitest run --config ../../vitest.config.ts --coverage",
42
+ "test:update": "vitest run --config ../../vitest.config.ts -u"
43
+ }
44
+ }