@aomex/common-middleware 3.9.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,8 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
+
6
+ # [3.9.0](https://github.com/aomex/aomex/compare/v3.8.1...v3.9.0) (2024-10-30)
7
+
8
+ **Note:** Version bump only for package @aomex/common-middleware
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023-2024 geekact
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.
@@ -0,0 +1,50 @@
1
+ import { NonReadonly } from '@aomex/internal-tools';
2
+
3
+ declare interface MiddlewarePlatform {
4
+ /**
5
+ * 没有特定上下文的纯中间件,支持所有应用
6
+ */
7
+ readonly mixin: {
8
+ <Props extends object = object>(fn: (ctx: NonReadonly<Props>, next: Next) => any): MixinMiddleware<Props>;
9
+ };
10
+ }
11
+ declare class MixinMiddleware<Props extends object = object> extends Middleware<Props> {
12
+ protected _mixin_middleware_: 'mixin-middleware';
13
+ }
14
+
15
+ interface MiddlewarePlatform {
16
+ }
17
+ declare namespace Middleware {
18
+ type Infer<T> = T extends Middleware<infer R> ? R : T extends (...args: any[]) => Middleware<infer R> ? R : never;
19
+ type Fn<Ctx = any> = (ctx: Ctx, next: Next) => any;
20
+ type CollectArrayType<T> = T extends Middleware<infer R> ? R : unknown;
21
+ }
22
+ /**
23
+ * 中间件基类
24
+ */
25
+ declare abstract class Middleware<Props extends object = object> {
26
+ protected readonly fn: Middleware.Fn;
27
+ _contextType: object;
28
+ protected _props_must_be_used_: Props;
29
+ constructor(fn: Middleware.Fn);
30
+ /**
31
+ * 跳过执行中间件。
32
+ *
33
+ * 返回的类型未使用Partial,因为skip的设计是在部分场景下才需要触发的。
34
+ */
35
+ skip(when: (ctx: this['_contextType']) => boolean | Promise<boolean>): MixinMiddleware<Props>;
36
+ /**
37
+ * 注册中间件
38
+ */
39
+ static register(platform: keyof MiddlewarePlatform, SubMiddleware: new (...args: any[]) => Middleware): void;
40
+ }
41
+ declare const middleware: MiddlewarePlatform;
42
+
43
+ type Next = () => Promise<any>;
44
+ type ComposeFn = (ctx: any, next?: Next) => Promise<void>;
45
+ /**
46
+ * 组合中间件
47
+ */
48
+ declare const compose: (middlewareList: Middleware[]) => ComposeFn;
49
+
50
+ export { type ComposeFn, Middleware, type MiddlewarePlatform, MixinMiddleware, type Next, compose, middleware };
package/dist/index.js ADDED
@@ -0,0 +1,93 @@
1
+ // src/i18n/index.ts
2
+ import { I18n as I18n3 } from "@aomex/internal-tools";
3
+
4
+ // src/i18n/locales/zh-cn.ts
5
+ import { I18n } from "@aomex/internal-tools";
6
+ var zh = I18n.define({
7
+ middleware: {
8
+ call_next_multiple: "\u591A\u6B21\u6267\u884C\u4E2D\u95F4\u4EF6next()\u51FD\u6570"
9
+ }
10
+ });
11
+
12
+ // src/i18n/locales/en-us.ts
13
+ import { I18n as I18n2 } from "@aomex/internal-tools";
14
+ var en = I18n2.satisfies(zh).define({
15
+ middleware: {
16
+ call_next_multiple: "call next() multiple times"
17
+ }
18
+ });
19
+
20
+ // src/i18n/locales/index.ts
21
+ var locales = {
22
+ zh_CN: zh,
23
+ en_US: en
24
+ };
25
+
26
+ // src/i18n/index.ts
27
+ var i18n = new I18n3({
28
+ resources: locales,
29
+ defaultLanguage: "zh_CN"
30
+ });
31
+
32
+ // src/compose.ts
33
+ var noop = () => {
34
+ };
35
+ var compose = (middlewareList) => {
36
+ const total = middlewareList.length;
37
+ const fns = middlewareList.map((md) => md["fn"]);
38
+ return (ctx, next) => {
39
+ let lastIndex = -1;
40
+ const dispatch = async (i) => {
41
+ if (i <= lastIndex) {
42
+ throw new Error(i18n.t("middleware.call_next_multiple"));
43
+ }
44
+ const finished = (lastIndex = i) === total;
45
+ const fn = finished ? next : fns[lastIndex];
46
+ if (fn) {
47
+ await fn(ctx, finished ? noop : dispatch.bind(null, i + 1));
48
+ }
49
+ };
50
+ return dispatch(0);
51
+ };
52
+ };
53
+
54
+ // src/middleware.ts
55
+ var Middleware = class {
56
+ constructor(fn) {
57
+ this.fn = fn;
58
+ }
59
+ /**
60
+ * 跳过执行中间件。
61
+ *
62
+ * 返回的类型未使用Partial,因为skip的设计是在部分场景下才需要触发的。
63
+ */
64
+ skip(when) {
65
+ return middleware.mixin(async (ctx, next) => {
66
+ const skipped = await when(ctx);
67
+ return skipped ? next() : this.fn(ctx, next);
68
+ });
69
+ }
70
+ /**
71
+ * 注册中间件
72
+ */
73
+ static register(platform, SubMiddleware) {
74
+ Object.defineProperty(middleware, platform, {
75
+ get() {
76
+ return (...args) => new SubMiddleware(...args);
77
+ }
78
+ });
79
+ }
80
+ };
81
+ var middleware = {};
82
+
83
+ // src/mixin-middleware.ts
84
+ var MixinMiddleware = class extends Middleware {
85
+ };
86
+ Middleware.register("mixin", MixinMiddleware);
87
+ export {
88
+ Middleware,
89
+ MixinMiddleware,
90
+ compose,
91
+ middleware
92
+ };
93
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/i18n/index.ts","../src/i18n/locales/zh-cn.ts","../src/i18n/locales/en-us.ts","../src/i18n/locales/index.ts","../src/compose.ts","../src/middleware.ts","../src/mixin-middleware.ts"],"sourcesContent":["import { I18n } from '@aomex/internal-tools';\nimport { locales } from './locales';\n\nexport const i18n = new I18n({\n resources: locales,\n defaultLanguage: 'zh_CN',\n});\n","import { I18n } from '@aomex/internal-tools';\n\nexport const zh = I18n.define({\n middleware: {\n call_next_multiple: '多次执行中间件next()函数',\n },\n});\n","import { I18n } from '@aomex/internal-tools';\nimport { zh } from './zh-cn';\n\nexport const en = I18n.satisfies(zh).define({\n middleware: {\n call_next_multiple: 'call next() multiple times',\n },\n});\n","import { zh } from './zh-cn';\nimport { en } from './en-us';\n\nexport const locales = {\n zh_CN: zh,\n en_US: en,\n};\n","import { i18n } from './i18n';\nimport type { Middleware } from './middleware';\n\nexport type Next = () => Promise<any>;\nexport type ComposeFn = (ctx: any, next?: Next) => Promise<void>;\nconst noop = (): any => {};\n\n/**\n * 组合中间件\n */\nexport const compose = (middlewareList: Middleware[]): ComposeFn => {\n const total = middlewareList.length;\n const fns = middlewareList.map((md) => md['fn']);\n\n return (ctx, next) => {\n let lastIndex = -1;\n const dispatch = async (i: number): Promise<void> => {\n if (i <= lastIndex) {\n throw new Error(i18n.t('middleware.call_next_multiple'));\n }\n const finished = (lastIndex = i) === total;\n const fn = finished ? next : fns[lastIndex];\n if (fn) {\n await fn(ctx, finished ? noop : dispatch.bind(null, i + 1));\n }\n };\n return dispatch(0);\n };\n};\n","import type { Next } from './compose';\nimport type { MixinMiddleware } from './mixin-middleware';\n\nexport declare namespace Middleware {\n export type Infer<T> =\n T extends Middleware<infer R>\n ? R\n : T extends (...args: any[]) => Middleware<infer R>\n ? R\n : never;\n\n export type Fn<Ctx = any> = (ctx: Ctx, next: Next) => any;\n\n export type CollectArrayType<T> = T extends Middleware<infer R> ? R : unknown;\n}\n\nexport interface MiddlewarePlatform {}\n\n/**\n * 中间件基类\n */\nexport abstract class Middleware<Props extends object = object> {\n public declare _contextType: object;\n\n protected declare _props_must_be_used_: Props;\n\n constructor(protected readonly fn: Middleware.Fn) {}\n\n /**\n * 跳过执行中间件。\n *\n * 返回的类型未使用Partial,因为skip的设计是在部分场景下才需要触发的。\n */\n public skip(\n when: (ctx: this['_contextType']) => boolean | Promise<boolean>,\n ): MixinMiddleware<Props> {\n return middleware.mixin(async (ctx, next) => {\n const skipped = await when(ctx);\n return skipped ? next() : this.fn(ctx, next);\n });\n }\n\n /**\n * 注册中间件\n */\n static register(\n platform: keyof MiddlewarePlatform,\n SubMiddleware: new (...args: any[]) => Middleware,\n ) {\n Object.defineProperty(middleware, platform, {\n get() {\n return (...args: any[]) => new SubMiddleware(...args);\n },\n });\n }\n}\n\nexport const middleware: MiddlewarePlatform = {} as any;\n","import type { NonReadonly } from '@aomex/internal-tools';\nimport type { Next } from './compose';\nimport { Middleware } from './middleware';\n\ndeclare module './middleware' {\n export interface MiddlewarePlatform {\n /**\n * 没有特定上下文的纯中间件,支持所有应用\n */\n readonly mixin: {\n <Props extends object = object>(\n fn: (ctx: NonReadonly<Props>, next: Next) => any,\n ): MixinMiddleware<Props>;\n };\n }\n}\n\nexport class MixinMiddleware<Props extends object = object> extends Middleware<Props> {\n protected declare _mixin_middleware_: 'mixin-middleware';\n}\n\nMiddleware.register('mixin', MixinMiddleware);\n"],"mappings":";AAAA,SAAS,QAAAA,aAAY;;;ACArB,SAAS,YAAY;AAEd,IAAM,KAAK,KAAK,OAAO;AAAA,EAC5B,YAAY;AAAA,IACV,oBAAoB;AAAA,EACtB;AACF,CAAC;;;ACND,SAAS,QAAAC,aAAY;AAGd,IAAM,KAAKC,MAAK,UAAU,EAAE,EAAE,OAAO;AAAA,EAC1C,Y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AM,aAAiC,CAAC;;;ACxCxC,IAAM,kBAAN,cAA6D,WAAkB;AAEtF;AAEA,WAAW,SAAS,SAAS,eAAe;","names":["I18n","I18n","I18n","I18n"]}
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@aomex/common-middleware",
3
+ "version": "3.9.0",
4
+ "description": "aomex中间件",
5
+ "type": "module",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ },
12
+ "./package.json": "./package.json"
13
+ },
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "LICENSE",
20
+ "package.json",
21
+ "README.md",
22
+ "CHANGELOG.md"
23
+ ],
24
+ "repository": "git@github.com:aomex/aomex.git",
25
+ "homepage": "https://aomex.js.org",
26
+ "license": "MIT",
27
+ "engines": {
28
+ "node": "^20.13.0 || ^22"
29
+ },
30
+ "dependencies": {
31
+ "@aomex/internal-tools": "^3.9.0"
32
+ },
33
+ "scripts": {}
34
+ }