@guanghechen/middleware 2.0.0 → 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 +21 -0
- package/README.md +23 -12
- package/lib/types/index.d.ts +4 -12
- package/package.json +13 -11
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
CHANGED
|
@@ -79,22 +79,27 @@ Middleware pattern implementation for both synchronous and asynchronous processi
|
|
|
79
79
|
```typescript
|
|
80
80
|
import { AsyncMiddlewares } from '@guanghechen/middleware'
|
|
81
81
|
|
|
82
|
-
const middlewares = new AsyncMiddlewares<string, string,
|
|
82
|
+
const middlewares = new AsyncMiddlewares<string, string, { prefix: string }>()
|
|
83
83
|
|
|
84
|
-
// Add middlewares
|
|
84
|
+
// Add middlewares (executed in registration order)
|
|
85
85
|
middlewares.use(async (input, embryo, api, next) => {
|
|
86
|
-
|
|
87
|
-
return
|
|
86
|
+
// Transform input to uppercase, pass to next middleware
|
|
87
|
+
return next(input.toUpperCase())
|
|
88
88
|
})
|
|
89
89
|
|
|
90
90
|
middlewares.use(async (input, embryo, api, next) => {
|
|
91
|
-
|
|
92
|
-
return
|
|
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
|
|
93
98
|
})
|
|
94
99
|
|
|
95
100
|
// Execute middleware chain
|
|
96
|
-
const reducer = middlewares.reducer('hello', {})
|
|
97
|
-
const result = await reducer(null) // Result: "[HELLO]"
|
|
101
|
+
const reducer = middlewares.reducer('hello', { prefix: '> ' })
|
|
102
|
+
const result = await reducer(null) // Result: "> [HELLO]"
|
|
98
103
|
```
|
|
99
104
|
|
|
100
105
|
- Sync middleware:
|
|
@@ -102,19 +107,25 @@ Middleware pattern implementation for both synchronous and asynchronous processi
|
|
|
102
107
|
```typescript
|
|
103
108
|
import { Middlewares } from '@guanghechen/middleware'
|
|
104
109
|
|
|
105
|
-
const middlewares = new Middlewares<number, number,
|
|
110
|
+
const middlewares = new Middlewares<number, number, { multiplier: number }>()
|
|
106
111
|
|
|
107
112
|
middlewares.use((input, embryo, api, next) => {
|
|
108
|
-
|
|
113
|
+
// First middleware: multiply input
|
|
114
|
+
return next(input * api.multiplier)
|
|
109
115
|
})
|
|
110
116
|
|
|
111
117
|
middlewares.use((input, embryo, api, next) => {
|
|
112
|
-
|
|
118
|
+
// Second middleware: add 1 to embryo (result from previous)
|
|
119
|
+
return embryo !== null ? embryo + 1 : null
|
|
113
120
|
})
|
|
114
121
|
|
|
115
|
-
const reducer = middlewares.reducer(5, {})
|
|
122
|
+
const reducer = middlewares.reducer(5, { multiplier: 2 })
|
|
116
123
|
const result = reducer(null) // Result: 11 (5 * 2 + 1)
|
|
117
124
|
```
|
|
118
125
|
|
|
126
|
+
## Reference
|
|
127
|
+
|
|
128
|
+
- [homepage][homepage]
|
|
129
|
+
|
|
119
130
|
[homepage]:
|
|
120
131
|
https://github.com/guanghechen/sora/tree/@guanghechen/middleware@2.0.0/packages/middleware#readme
|
package/lib/types/index.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
|
|
28
|
-
|
|
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.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guanghechen/middleware",
|
|
3
|
-
"version": "2.0.
|
|
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@
|
|
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@
|
|
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",
|
|
@@ -26,10 +26,6 @@
|
|
|
26
26
|
"module": "./lib/esm/index.mjs",
|
|
27
27
|
"types": "./lib/types/index.d.ts",
|
|
28
28
|
"license": "MIT",
|
|
29
|
-
"scripts": {
|
|
30
|
-
"build": "rollup -c ../../rollup.config.mjs",
|
|
31
|
-
"clean": "rimraf lib"
|
|
32
|
-
},
|
|
33
29
|
"files": [
|
|
34
30
|
"lib/",
|
|
35
31
|
"!lib/**/*.map",
|
|
@@ -38,5 +34,11 @@
|
|
|
38
34
|
"LICENSE",
|
|
39
35
|
"README.md"
|
|
40
36
|
],
|
|
41
|
-
"
|
|
42
|
-
|
|
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
|
+
}
|