@aomex/common-service 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 +8 -0
- package/LICENSE +21 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
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-service
|
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.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 为了让服务层有提示,你需要手动处理这一步:
|
|
3
|
+
*
|
|
4
|
+
* ```typescript
|
|
5
|
+
export const services = await combineServices({...});
|
|
6
|
+
|
|
7
|
+
declare module '@aomex/common' {
|
|
8
|
+
type T = typeof services;
|
|
9
|
+
export interface CombinedServices extends T {}
|
|
10
|
+
}
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
interface CombinedServices {
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 把所有服务绑定在一起,然后导出使用
|
|
17
|
+
*/
|
|
18
|
+
declare const combineServices: <T extends Record<string, new (services: Record<string, Service>) => Service>>(classes: T) => Promise<{ readonly [K in keyof T]: InstanceType<T[K]>; }>;
|
|
19
|
+
|
|
20
|
+
declare abstract class Service {
|
|
21
|
+
private readonly _services;
|
|
22
|
+
protected get displayName(): string;
|
|
23
|
+
constructor(_services: Record<string, Service>);
|
|
24
|
+
protected get services(): CombinedServices;
|
|
25
|
+
/**
|
|
26
|
+
* 会自动触发的初始化方法
|
|
27
|
+
*/
|
|
28
|
+
protected init(): void | Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { type CombinedServices, Service, combineServices };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/service.ts
|
|
2
|
+
var Service = class {
|
|
3
|
+
constructor(_services) {
|
|
4
|
+
this._services = _services;
|
|
5
|
+
}
|
|
6
|
+
get displayName() {
|
|
7
|
+
const className = this.constructor.name;
|
|
8
|
+
if (className.endsWith("Service")) {
|
|
9
|
+
return className.slice(0, -7);
|
|
10
|
+
}
|
|
11
|
+
return className;
|
|
12
|
+
}
|
|
13
|
+
get services() {
|
|
14
|
+
return this._services;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 会自动触发的初始化方法
|
|
18
|
+
*/
|
|
19
|
+
init() {
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// src/combine-services.ts
|
|
24
|
+
var combineServices = async (classes) => {
|
|
25
|
+
const services = {};
|
|
26
|
+
const caches = {};
|
|
27
|
+
await Promise.all(
|
|
28
|
+
Object.entries(classes).map(async ([key, Class]) => {
|
|
29
|
+
const instance = new Class(services);
|
|
30
|
+
await instance["init"]();
|
|
31
|
+
caches[key] = instance;
|
|
32
|
+
})
|
|
33
|
+
);
|
|
34
|
+
Object.entries(caches).forEach(([key, service]) => {
|
|
35
|
+
services[key] = service;
|
|
36
|
+
});
|
|
37
|
+
Object.freeze(services);
|
|
38
|
+
return services;
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
Service,
|
|
42
|
+
combineServices
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/service.ts","../src/combine-services.ts"],"sourcesContent":["import type { CombinedServices } from './combine-services';\n\nexport abstract class Service {\n protected get displayName() {\n const className = this.constructor.name;\n if (className.endsWith('Service')) {\n return className.slice(0, -7);\n }\n return className;\n }\n\n constructor(private readonly _services: Record<string, Service>) {}\n\n protected get services(): CombinedServices {\n return this._services as CombinedServices;\n }\n\n /**\n * 会自动触发的初始化方法\n */\n protected init(): void | Promise<void> {}\n}\n","import { Service } from './service';\n/**\n * 为了让服务层有提示,你需要手动处理这一步:\n * \n * ```typescript\n export const services = await combineServices({...});\n \n declare module '@aomex/common' {\n type T = typeof services;\n export interface CombinedServices extends T {}\n }\n * ```\n */\nexport interface CombinedServices {}\n\n/**\n * 把所有服务绑定在一起,然后导出使用\n */\nexport const combineServices = async <\n T extends Record<string, new (services: Record<string, Service>) => Service>,\n>(\n classes: T,\n): Promise<{ readonly [K in keyof T]: InstanceType<T[K]> }> => {\n const services: Record<string, Service> = {};\n const caches: Record<string, Service> = {};\n\n await Promise.all(\n Object.entries(classes).map(async ([key, Class]) => {\n const instance = new Class(services);\n await instance['init']();\n caches[key] = instance;\n }),\n );\n\n Object.entries(caches).forEach(([key, service]) => {\n services[key] = service;\n });\n\n Object.freeze(services);\n\n return services as any;\n};\n"],"mappings":";AAEO,IAAe,UAAf,MAAuB;AAAA,EAS5B,YAA6B,WAAoC;AAApC;AAAA,EAAqC;AAAA,EARlE,IAAc,cAAc;AAC1B,UAAM,YAAY,KAAK,YAAY;AACnC,QAAI,UAAU,SAAS,SAAS,GAAG;AACjC,aAAO,UAAU,MAAM,GAAG,EAAE;AAAA,IAC9B;AACA,WAAO;AAAA,EACT;AAAA,EAIA,IAAc,WAA6B;AACzC,WAAO,KAAK;AAAA,EACd;AAAA;AAAA;AAAA;AAAA,EAKU,OAA6B;AAAA,EAAC;AAC1C;;;ACHO,IAAM,kBAAkB,OAG7B,YAC6D;AAC7D,QAAM,WAAoC,CAAC;AAC3C,QAAM,SAAkC,CAAC;AAEzC,QAAM,QAAQ;AAAA,IACZ,OAAO,QAAQ,OAAO,EAAE,IAAI,OAAO,CAAC,KAAK,KAAK,MAAM;AAClD,YAAM,WAAW,IAAI,MAAM,QAAQ;AACnC,YAAM,SAAS,MAAM,EAAE;AACvB,aAAO,GAAG,IAAI;AAAA,IAChB,CAAC;AAAA,EACH;AAEA,SAAO,QAAQ,MAAM,EAAE,QAAQ,CAAC,CAAC,KAAK,OAAO,MAAM;AACjD,aAAS,GAAG,IAAI;AAAA,EAClB,CAAC;AAED,SAAO,OAAO,QAAQ;AAEtB,SAAO;AACT;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@aomex/common-service",
|
|
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
|
+
"scripts": {}
|
|
31
|
+
}
|