wok-server 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/LICENSE +21 -0
- package/README.md +47 -0
- package/dist/cache/cache.js +94 -0
- package/dist/cache/config.js +19 -0
- package/dist/cache/index.js +27 -0
- package/dist/cache/purge-task.js +56 -0
- package/dist/cache/stat.js +47 -0
- package/dist/config/convert.js +36 -0
- package/dist/config/exception.js +14 -0
- package/dist/config/index.js +67 -0
- package/dist/http-client/index.js +132 -0
- package/dist/i18n/ar.js +17 -0
- package/dist/i18n/de.js +17 -0
- package/dist/i18n/en-us.js +17 -0
- package/dist/i18n/es.js +17 -0
- package/dist/i18n/fr.js +17 -0
- package/dist/i18n/i18n.js +231 -0
- package/dist/i18n/index.js +52 -0
- package/dist/i18n/ja.js +17 -0
- package/dist/i18n/ko.js +17 -0
- package/dist/i18n/msg.js +2 -0
- package/dist/i18n/pt.js +17 -0
- package/dist/i18n/ru.js +17 -0
- package/dist/i18n/tag.js +18 -0
- package/dist/i18n/zh-HK.js +17 -0
- package/dist/i18n/zh-TW.js +17 -0
- package/dist/i18n/zh-cn.js +17 -0
- package/dist/index.js +13 -0
- package/dist/log/config.js +28 -0
- package/dist/log/date.js +21 -0
- package/dist/log/file.js +79 -0
- package/dist/log/index.js +109 -0
- package/dist/log/level.js +39 -0
- package/dist/log/store.js +16 -0
- package/dist/mongodb/collection.js +2 -0
- package/dist/mongodb/config.js +34 -0
- package/dist/mongodb/doc.js +2 -0
- package/dist/mongodb/exception.js +14 -0
- package/dist/mongodb/index.js +58 -0
- package/dist/mongodb/manager/base.js +563 -0
- package/dist/mongodb/manager/index.js +63 -0
- package/dist/mongodb/manager/tx-strict.js +84 -0
- package/dist/mongodb/manager/tx.js +30 -0
- package/dist/mongodb/migration.js +52 -0
- package/dist/mvc/access-log.js +31 -0
- package/dist/mvc/config.js +20 -0
- package/dist/mvc/exchange.js +113 -0
- package/dist/mvc/handler/index.js +6 -0
- package/dist/mvc/handler/json.js +33 -0
- package/dist/mvc/handler/restful.js +35 -0
- package/dist/mvc/handler/upload.js +33 -0
- package/dist/mvc/index.js +316 -0
- package/dist/mvc/interceptor.js +2 -0
- package/dist/mvc/query.js +43 -0
- package/dist/mvc/render/file.js +177 -0
- package/dist/mvc/render/html/html.js +90 -0
- package/dist/mvc/render/html/index.js +18 -0
- package/dist/mvc/render/html/style.js +2 -0
- package/dist/mvc/render/index.js +7 -0
- package/dist/mvc/render/json.js +26 -0
- package/dist/mvc/render/text.js +16 -0
- package/dist/mvc/router.js +2 -0
- package/dist/mysql/config.js +49 -0
- package/dist/mysql/exception.js +14 -0
- package/dist/mysql/index.js +85 -0
- package/dist/mysql/manager/base.js +233 -0
- package/dist/mysql/manager/index.js +107 -0
- package/dist/mysql/manager/ops/count.js +20 -0
- package/dist/mysql/manager/ops/criteria.js +326 -0
- package/dist/mysql/manager/ops/delete.js +65 -0
- package/dist/mysql/manager/ops/exist.js +26 -0
- package/dist/mysql/manager/ops/find.js +111 -0
- package/dist/mysql/manager/ops/index.js +14 -0
- package/dist/mysql/manager/ops/insert.js +101 -0
- package/dist/mysql/manager/ops/modify.js +10 -0
- package/dist/mysql/manager/ops/paginate.js +23 -0
- package/dist/mysql/manager/ops/query.js +9 -0
- package/dist/mysql/manager/ops/update.js +201 -0
- package/dist/mysql/manager/tx-strict.js +98 -0
- package/dist/mysql/manager/tx.js +30 -0
- package/dist/mysql/manager/utils.js +56 -0
- package/dist/mysql/migration.js +136 -0
- package/dist/mysql/table-info.js +8 -0
- package/dist/task/daily.js +58 -0
- package/dist/task/fixed-delay.js +33 -0
- package/dist/task/fixed-rate.js +37 -0
- package/dist/task/index.js +9 -0
- package/dist/task/task.js +39 -0
- package/dist/validation/exception.js +44 -0
- package/dist/validation/index.js +29 -0
- package/dist/validation/validator/array.js +38 -0
- package/dist/validation/validator/enum.js +28 -0
- package/dist/validation/validator/index.js +14 -0
- package/dist/validation/validator/length.js +40 -0
- package/dist/validation/validator/max-length.js +35 -0
- package/dist/validation/validator/max.js +29 -0
- package/dist/validation/validator/min-length.js +33 -0
- package/dist/validation/validator/min.js +29 -0
- package/dist/validation/validator/not-blank.js +33 -0
- package/dist/validation/validator/not-null.js +21 -0
- package/dist/validation/validator/plain-obj.js +32 -0
- package/dist/validation/validator/regexp.js +30 -0
- package/documentation/en/index.md +1 -0
- package/documentation/zh-cn/cache.md +59 -0
- package/documentation/zh-cn/config.md +68 -0
- package/documentation/zh-cn/http-client.md +33 -0
- package/documentation/zh-cn/i18n.md +154 -0
- package/documentation/zh-cn/index.md +25 -0
- package/documentation/zh-cn/log.md +40 -0
- package/documentation/zh-cn/mongodb.md +262 -0
- package/documentation/zh-cn/mvc.md +430 -0
- package/documentation/zh-cn/mysql.md +389 -0
- package/documentation/zh-cn/task.md +50 -0
- package/documentation/zh-cn/test.md +57 -0
- package/documentation/zh-cn/validate.md +125 -0
- package/package.json +46 -0
- package/types/cache/cache.d.ts +52 -0
- package/types/cache/config.d.ts +32 -0
- package/types/cache/index.d.ts +2 -0
- package/types/cache/purge-task.d.ts +11 -0
- package/types/cache/stat.d.ts +26 -0
- package/types/config/convert.d.ts +6 -0
- package/types/config/exception.d.ts +7 -0
- package/types/config/index.d.ts +15 -0
- package/types/http-client/index.d.ts +71 -0
- package/types/i18n/ar.d.ts +2 -0
- package/types/i18n/de.d.ts +2 -0
- package/types/i18n/en-us.d.ts +2 -0
- package/types/i18n/es.d.ts +2 -0
- package/types/i18n/fr.d.ts +2 -0
- package/types/i18n/i18n.d.ts +102 -0
- package/types/i18n/index.d.ts +9 -0
- package/types/i18n/ja.d.ts +2 -0
- package/types/i18n/ko.d.ts +2 -0
- package/types/i18n/msg.d.ts +50 -0
- package/types/i18n/pt.d.ts +2 -0
- package/types/i18n/ru.d.ts +2 -0
- package/types/i18n/tag.d.ts +11 -0
- package/types/i18n/zh-HK.d.ts +2 -0
- package/types/i18n/zh-TW.d.ts +2 -0
- package/types/i18n/zh-cn.d.ts +2 -0
- package/types/index.d.ts +10 -0
- package/types/log/config.d.ts +27 -0
- package/types/log/date.d.ts +2 -0
- package/types/log/file.d.ts +5 -0
- package/types/log/index.d.ts +34 -0
- package/types/log/level.d.ts +15 -0
- package/types/log/store.d.ts +12 -0
- package/types/mongodb/collection.d.ts +25 -0
- package/types/mongodb/config.d.ts +45 -0
- package/types/mongodb/doc.d.ts +11 -0
- package/types/mongodb/exception.d.ts +7 -0
- package/types/mongodb/index.d.ts +29 -0
- package/types/mongodb/manager/base.d.ts +188 -0
- package/types/mongodb/manager/index.d.ts +38 -0
- package/types/mongodb/manager/tx-strict.d.ts +41 -0
- package/types/mongodb/manager/tx.d.ts +21 -0
- package/types/mongodb/migration.d.ts +12 -0
- package/types/mvc/access-log.d.ts +7 -0
- package/types/mvc/config.d.ts +30 -0
- package/types/mvc/exchange.d.ts +72 -0
- package/types/mvc/handler/index.d.ts +3 -0
- package/types/mvc/handler/json.d.ts +23 -0
- package/types/mvc/handler/restful.d.ts +11 -0
- package/types/mvc/handler/upload.d.ts +40 -0
- package/types/mvc/index.d.ts +49 -0
- package/types/mvc/interceptor.d.ts +11 -0
- package/types/mvc/query.d.ts +13 -0
- package/types/mvc/render/file.d.ts +10 -0
- package/types/mvc/render/html/html.d.ts +98 -0
- package/types/mvc/render/html/index.d.ts +11 -0
- package/types/mvc/render/html/style.d.ts +1201 -0
- package/types/mvc/render/index.d.ts +4 -0
- package/types/mvc/render/json.d.ts +17 -0
- package/types/mvc/render/text.d.ts +10 -0
- package/types/mvc/router.d.ts +11 -0
- package/types/mysql/config.d.ts +86 -0
- package/types/mysql/exception.d.ts +7 -0
- package/types/mysql/index.d.ts +16 -0
- package/types/mysql/manager/base.d.ts +158 -0
- package/types/mysql/manager/index.d.ts +36 -0
- package/types/mysql/manager/ops/count.d.ts +13 -0
- package/types/mysql/manager/ops/criteria.d.ts +120 -0
- package/types/mysql/manager/ops/delete.d.ts +46 -0
- package/types/mysql/manager/ops/exist.d.ts +6 -0
- package/types/mysql/manager/ops/find.d.ts +66 -0
- package/types/mysql/manager/ops/index.d.ts +10 -0
- package/types/mysql/manager/ops/insert.d.ts +18 -0
- package/types/mysql/manager/ops/modify.d.ts +3 -0
- package/types/mysql/manager/ops/paginate.d.ts +36 -0
- package/types/mysql/manager/ops/query.d.ts +3 -0
- package/types/mysql/manager/ops/update.d.ts +70 -0
- package/types/mysql/manager/tx-strict.d.ts +34 -0
- package/types/mysql/manager/tx.d.ts +15 -0
- package/types/mysql/manager/utils.d.ts +17 -0
- package/types/mysql/migration.d.ts +8 -0
- package/types/mysql/table-info.d.ts +36 -0
- package/types/task/daily.d.ts +15 -0
- package/types/task/fixed-delay.d.ts +8 -0
- package/types/task/fixed-rate.d.ts +8 -0
- package/types/task/index.d.ts +4 -0
- package/types/task/task.d.ts +33 -0
- package/types/validation/exception.d.ts +43 -0
- package/types/validation/index.d.ts +32 -0
- package/types/validation/validator/array.d.ts +5 -0
- package/types/validation/validator/enum.d.ts +8 -0
- package/types/validation/validator/index.d.ts +11 -0
- package/types/validation/validator/length.d.ts +10 -0
- package/types/validation/validator/max-length.d.ts +8 -0
- package/types/validation/validator/max.d.ts +7 -0
- package/types/validation/validator/min-length.d.ts +6 -0
- package/types/validation/validator/min.d.ts +7 -0
- package/types/validation/validator/not-blank.d.ts +7 -0
- package/types/validation/validator/not-null.d.ts +6 -0
- package/types/validation/validator/plain-obj.d.ts +7 -0
- package/types/validation/validator/regexp.d.ts +8 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CacheStat } from './stat';
|
|
2
|
+
/**
|
|
3
|
+
* 缓存内容
|
|
4
|
+
*/
|
|
5
|
+
export interface CacheContent {
|
|
6
|
+
/**
|
|
7
|
+
* 保存的值
|
|
8
|
+
*/
|
|
9
|
+
val: any;
|
|
10
|
+
/**
|
|
11
|
+
* 过期时间
|
|
12
|
+
*/
|
|
13
|
+
expireAt: number;
|
|
14
|
+
}
|
|
15
|
+
export declare class Cache {
|
|
16
|
+
private readonly valueMap;
|
|
17
|
+
private readonly stat;
|
|
18
|
+
/**
|
|
19
|
+
* promise 表,作用是处理异步并发问题,如果有相同的 key 同时请求,保证异步的 provider 只执行一次
|
|
20
|
+
*/
|
|
21
|
+
private promiseMap;
|
|
22
|
+
constructor(valueMap: Map<string, CacheContent>, stat: CacheStat);
|
|
23
|
+
/**
|
|
24
|
+
* 放入缓存
|
|
25
|
+
* @param key
|
|
26
|
+
* @param val
|
|
27
|
+
* @param expiresInSeconds
|
|
28
|
+
*/
|
|
29
|
+
put(key: string, val: any, expiresInSeconds?: number): void;
|
|
30
|
+
/**
|
|
31
|
+
* 获取缓存值
|
|
32
|
+
* @param key
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
get<T>(key: string): T | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* 清除所有缓存
|
|
38
|
+
*/
|
|
39
|
+
clear(): void;
|
|
40
|
+
/**
|
|
41
|
+
* 删除.
|
|
42
|
+
* @param key
|
|
43
|
+
*/
|
|
44
|
+
remove(key: string): void;
|
|
45
|
+
/**
|
|
46
|
+
* 在缓存值不存在时计算缓存的值,然后放入并返回.
|
|
47
|
+
* @param key
|
|
48
|
+
* @param provider
|
|
49
|
+
* @param expiresInSeconds
|
|
50
|
+
*/
|
|
51
|
+
computeIfAbsent<T>(key: string, provider: () => Promise<T> | T, expiresInSeconds?: number): Promise<T>;
|
|
52
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 缓存的配置
|
|
3
|
+
*/
|
|
4
|
+
interface CacheConfig {
|
|
5
|
+
/**
|
|
6
|
+
* 默认的存活时长,取值:1-3600,默认 60。存活时间应该尽可能的短,尤其是当缓存的内容比较大时,
|
|
7
|
+
* 否则缓存的内容长时间占用内存,会对 gc 造成影响。
|
|
8
|
+
*/
|
|
9
|
+
defaultExpireInSeconds: number;
|
|
10
|
+
/**
|
|
11
|
+
* 是否启用统计任务,启用后每隔指定的时间打印一次缓存统计信息,用于了解缓存的情况.
|
|
12
|
+
*/
|
|
13
|
+
statTaskEnabled: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* 统计任务执行的周期,单位秒,取值 60-3600。统计周期也即是时间窗口,
|
|
16
|
+
* 每个周期都是独立的,只统计这个周期内的缓存使用情况。
|
|
17
|
+
*/
|
|
18
|
+
statInterval: number;
|
|
19
|
+
/**
|
|
20
|
+
* 最大元素数量,为了控制缓存占用的内存空间,可以将 maxElements 设置的低一些。
|
|
21
|
+
*/
|
|
22
|
+
maxElements: number;
|
|
23
|
+
/**
|
|
24
|
+
* 数据清理任务执行周期,单位秒.取值 1-3600,清理任务完成两件事:删除过期数据和驱逐多余记录。
|
|
25
|
+
* 如果项目规模小,缓存的内容非常少,可以将间隔设置长一些,降低频率.
|
|
26
|
+
* 如果缓存的内容很多,频率需要高一些,及时将无效数据清理掉。
|
|
27
|
+
* 否则缓存会长时间占用很高的内存无法被回收,回收器完成检查却需要很长时间,影响gc效率.
|
|
28
|
+
*/
|
|
29
|
+
cleaningInterval: number;
|
|
30
|
+
}
|
|
31
|
+
export declare const config: CacheConfig;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Task } from '../task';
|
|
2
|
+
import { CacheContent } from './cache';
|
|
3
|
+
/**
|
|
4
|
+
* 缓存清理任务
|
|
5
|
+
*/
|
|
6
|
+
export declare class PurgeTask implements Task {
|
|
7
|
+
private readonly valueMap;
|
|
8
|
+
readonly name = "cache purge";
|
|
9
|
+
constructor(valueMap: Map<string, CacheContent>);
|
|
10
|
+
run(): Promise<void>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 缓存统计.
|
|
3
|
+
*/
|
|
4
|
+
export declare class CacheStat {
|
|
5
|
+
private readonly valueMap;
|
|
6
|
+
/**
|
|
7
|
+
* 开始统计时间
|
|
8
|
+
*/
|
|
9
|
+
private start;
|
|
10
|
+
private totalGet;
|
|
11
|
+
private totalHit;
|
|
12
|
+
constructor(valueMap: Map<string, any>);
|
|
13
|
+
/**
|
|
14
|
+
* 添加 get 次数
|
|
15
|
+
* @param hit 是否命中
|
|
16
|
+
*/
|
|
17
|
+
addGet(hit: boolean): void;
|
|
18
|
+
/**
|
|
19
|
+
* 清空统计信息,重新开始统计.
|
|
20
|
+
*/
|
|
21
|
+
clear(): void;
|
|
22
|
+
/**
|
|
23
|
+
* 输出日志
|
|
24
|
+
*/
|
|
25
|
+
log(): void;
|
|
26
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ValidationOpts } from '../validation';
|
|
2
|
+
/**
|
|
3
|
+
* 获取配置
|
|
4
|
+
* @param envPrefix
|
|
5
|
+
*/
|
|
6
|
+
export declare function getConfig<T>(envPrefix: string): T | undefined;
|
|
7
|
+
/**
|
|
8
|
+
* 注册配置信息.
|
|
9
|
+
* @param defaultConfig 默认值, 模块会根据默认值来映射环境变量,默认值所有的属性都不能是 undefined,否则无法映射.
|
|
10
|
+
* @param envPrefix 环境变量前缀.
|
|
11
|
+
* @param validation 校验规则,如果有值,初始化的时候会对配置进行校验
|
|
12
|
+
* @returns 映射了环境变量后的配置
|
|
13
|
+
*/
|
|
14
|
+
export declare function registerConfig<T extends {}>(defaultConfig: T, envPrefix: string, validation?: ValidationOpts<T>): T;
|
|
15
|
+
export * from './exception';
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
/// <reference types="node" />
|
|
3
|
+
import { IncomingHttpHeaders } from 'http';
|
|
4
|
+
/**
|
|
5
|
+
* 请求选项.
|
|
6
|
+
*/
|
|
7
|
+
export interface HttpRequestOpts {
|
|
8
|
+
/**
|
|
9
|
+
* 链接地址.
|
|
10
|
+
*/
|
|
11
|
+
url: string;
|
|
12
|
+
/**
|
|
13
|
+
* 查询参数,附加在链接地址上
|
|
14
|
+
*/
|
|
15
|
+
query?: Record<string, string[] | string>;
|
|
16
|
+
/**
|
|
17
|
+
* 正文,可以是序列化好的字符串或二进制内容.
|
|
18
|
+
*/
|
|
19
|
+
body?: string | Buffer;
|
|
20
|
+
/**
|
|
21
|
+
* 消息头
|
|
22
|
+
*/
|
|
23
|
+
headers?: IncomingHttpHeaders;
|
|
24
|
+
/**
|
|
25
|
+
* 请求方法
|
|
26
|
+
*/
|
|
27
|
+
method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH';
|
|
28
|
+
/**
|
|
29
|
+
* 超时时间,单位毫秒,默认 5000
|
|
30
|
+
*/
|
|
31
|
+
timeout?: number;
|
|
32
|
+
/**
|
|
33
|
+
* 是否跟随重定向
|
|
34
|
+
*/
|
|
35
|
+
followRedirect?: boolean;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 响应信息.
|
|
39
|
+
*/
|
|
40
|
+
export interface HttpResponseInfo {
|
|
41
|
+
/**
|
|
42
|
+
* 状态码
|
|
43
|
+
*/
|
|
44
|
+
status: number;
|
|
45
|
+
/**
|
|
46
|
+
* 消息头.
|
|
47
|
+
*/
|
|
48
|
+
headers: IncomingHttpHeaders;
|
|
49
|
+
/**
|
|
50
|
+
* 响应正文
|
|
51
|
+
*/
|
|
52
|
+
body: Buffer;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 发送 http 请求.
|
|
56
|
+
* @param opts
|
|
57
|
+
* @returns
|
|
58
|
+
*/
|
|
59
|
+
export declare function doRequest(opts: HttpRequestOpts): Promise<HttpResponseInfo>;
|
|
60
|
+
/**
|
|
61
|
+
* 发送 json 格式的 post 请求,body 是未序列化的普通对象,表示要发送的请求数据.
|
|
62
|
+
* @param opts
|
|
63
|
+
*/
|
|
64
|
+
export declare function postJson<T>(opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'> & {
|
|
65
|
+
body: any;
|
|
66
|
+
}): Promise<T>;
|
|
67
|
+
/**
|
|
68
|
+
* get 请求获取 json 格式数据.
|
|
69
|
+
* @param opts
|
|
70
|
+
*/
|
|
71
|
+
export declare function getJson<T>(opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'>): Promise<T>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { IncomingHttpHeaders } from 'http';
|
|
3
|
+
/**
|
|
4
|
+
* 绑定语言的 i18n 类型,不可切换语言.
|
|
5
|
+
*/
|
|
6
|
+
export declare class BoundI18n<T> {
|
|
7
|
+
private readonly lang;
|
|
8
|
+
private readonly msgs;
|
|
9
|
+
constructor(lang: string, msgs: T);
|
|
10
|
+
/**
|
|
11
|
+
* 获取当前语言
|
|
12
|
+
*/
|
|
13
|
+
getLang(): string;
|
|
14
|
+
/**
|
|
15
|
+
* 构建国际化消息.
|
|
16
|
+
* @param key 消息模板的 key
|
|
17
|
+
* @param args 参数,如果 key 对应的消息模板有占位符号({})可以使用参数来填充
|
|
18
|
+
*/
|
|
19
|
+
buildMsg(key: keyof T, ...args: string[]): string;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 国际化,提供不同语言消息构建的功能,默认英文.
|
|
23
|
+
*/
|
|
24
|
+
export declare class I18n<T> {
|
|
25
|
+
readonly enMsgs: T;
|
|
26
|
+
/**
|
|
27
|
+
* 当前语言
|
|
28
|
+
*/
|
|
29
|
+
private lang;
|
|
30
|
+
/**
|
|
31
|
+
* 消息模板表,与当前语言是对应的
|
|
32
|
+
*/
|
|
33
|
+
private msgs;
|
|
34
|
+
/**
|
|
35
|
+
* 消息表,key 是语言,value 是一个 key 为地区,value 为 消息模板的 map,两层 map 为的是能够快速匹配.
|
|
36
|
+
* 地区为空的情况下,缺省值为 - 。语言和地区的 key 全转成小写存储。
|
|
37
|
+
*/
|
|
38
|
+
private msgsMap;
|
|
39
|
+
/**
|
|
40
|
+
* 构造 i18n 对象,必须指定英文的消息模板作为默认。
|
|
41
|
+
* @param enMsgs 英文的消息模板
|
|
42
|
+
*/
|
|
43
|
+
constructor(enMsgs: T);
|
|
44
|
+
protected findMsgsByLang(langTag: string): T | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* 设置一种语言对应的消息模板,支持异步,可以做动态加载
|
|
47
|
+
* @param langTag
|
|
48
|
+
* @param msgs
|
|
49
|
+
*/
|
|
50
|
+
setMsgs(langTag: string, msgs: T): void;
|
|
51
|
+
/**
|
|
52
|
+
* 给定一个语言标签列表,获得可以被支持的标签列表
|
|
53
|
+
* @param tags
|
|
54
|
+
*/
|
|
55
|
+
getSupportedLanguageTags(...tags: string[]): string[];
|
|
56
|
+
/**
|
|
57
|
+
* 设置当前语言,如果指定的语言不被支持,则修改无效
|
|
58
|
+
* @param lang 语言本标签,如 en-US
|
|
59
|
+
* @returns 是否成功,如果语言不支持则返回 false,不会改变现有语言
|
|
60
|
+
*/
|
|
61
|
+
setLang(lang: string): boolean;
|
|
62
|
+
/**
|
|
63
|
+
* 获取当前语言
|
|
64
|
+
*/
|
|
65
|
+
getLang(): string;
|
|
66
|
+
/**
|
|
67
|
+
* 构建国际化消息.
|
|
68
|
+
* @param key 消息模板的 key
|
|
69
|
+
* @param args 参数,如果 key 对应的消息模板有占位符号({})可以使用参数来填充
|
|
70
|
+
*/
|
|
71
|
+
buildMsg(key: keyof T, ...args: string[]): string;
|
|
72
|
+
/**
|
|
73
|
+
* 绑定一种语言,生成新的 i18n 对象,该对象仅能使用某一种语言.
|
|
74
|
+
* @param langTag 语言标签,无值则表示绑定当前语言
|
|
75
|
+
* @returns 返回一个语言受限的 i18n 对象.
|
|
76
|
+
*/
|
|
77
|
+
bindLang(langTag?: string): BoundI18n<T>;
|
|
78
|
+
/**
|
|
79
|
+
* 根据请求进行语言切换,解析消息头 'accept-language' ,如果 'accept-language' 中没有包含能够支持的语言则使用默认的英文.
|
|
80
|
+
* @param headers 消息头
|
|
81
|
+
*/
|
|
82
|
+
switchByRequest(headers: IncomingHttpHeaders): void;
|
|
83
|
+
private parseAcceptLanguage;
|
|
84
|
+
/**
|
|
85
|
+
* 根据请求进行语言绑定,解析消息头 'accept-language',绑定受支持的第一个语言,如果没有可以被支持的语言,则使用默认的英文.
|
|
86
|
+
* @param headers 消息头
|
|
87
|
+
*/
|
|
88
|
+
bindByRequest(headers: IncomingHttpHeaders): BoundI18n<T>;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 可扩展的 i18n ,增加扩展新 i18n 对象的功能,记录扩展的新对象,同步 setLang 操作
|
|
92
|
+
*/
|
|
93
|
+
export declare class ExtensibleI18n<T extends object> extends I18n<T> {
|
|
94
|
+
private extendedI18ns;
|
|
95
|
+
setLang(lang: string): boolean;
|
|
96
|
+
/**
|
|
97
|
+
* 扩展国际化内容,生成一个新的 i18n 对象,与当前的 i18n 对象语言保持一致,可在新对象中设置扩展部分的语言。
|
|
98
|
+
* <K> 新的消息模板类型
|
|
99
|
+
* @param enMsgs
|
|
100
|
+
*/
|
|
101
|
+
extend<K extends object>(enMsgs: K): I18n<K>;
|
|
102
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
export interface I18nMsgs {
|
|
2
|
+
/**
|
|
3
|
+
* 校验错误,不是数组
|
|
4
|
+
*/
|
|
5
|
+
'validate-err-array': string;
|
|
6
|
+
/**
|
|
7
|
+
* 校验错误:不是数字
|
|
8
|
+
*/
|
|
9
|
+
'validate-err-numer': string;
|
|
10
|
+
/**
|
|
11
|
+
* 校验错误:不得大于
|
|
12
|
+
*/
|
|
13
|
+
'validate-err-max': string;
|
|
14
|
+
/**
|
|
15
|
+
* 校验错误:不得小于
|
|
16
|
+
*/
|
|
17
|
+
'validate-err-min': string;
|
|
18
|
+
/**
|
|
19
|
+
* 枚举校验错误,值不在范围内
|
|
20
|
+
*/
|
|
21
|
+
'validate-err-enum': string;
|
|
22
|
+
/**
|
|
23
|
+
* 校验错误:不能为空
|
|
24
|
+
*/
|
|
25
|
+
'validate-err-empty': string;
|
|
26
|
+
/**
|
|
27
|
+
* 校验错误:不是字符串
|
|
28
|
+
*/
|
|
29
|
+
'validate-err-string': string;
|
|
30
|
+
/**
|
|
31
|
+
* 校验错误:格式不正确
|
|
32
|
+
*/
|
|
33
|
+
'validate-err-incorrect-format': string;
|
|
34
|
+
/**
|
|
35
|
+
* 校验错误,没有 length 属性
|
|
36
|
+
*/
|
|
37
|
+
'validate-err-no-length': string;
|
|
38
|
+
/**
|
|
39
|
+
* 校验错误,length 不是数字
|
|
40
|
+
*/
|
|
41
|
+
'validate-err-length-not-number': string;
|
|
42
|
+
/**
|
|
43
|
+
* 校验错误:最小长度
|
|
44
|
+
*/
|
|
45
|
+
'validate-err-min-length': string;
|
|
46
|
+
/**
|
|
47
|
+
* 校验错误:最大长度
|
|
48
|
+
*/
|
|
49
|
+
'validate-err-max-length': string;
|
|
50
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface LangTag {
|
|
2
|
+
lang: string;
|
|
3
|
+
region?: string;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* 解析语言标签,语言标签格式非常多,这里仅支持语言加地区的形式,如 zh-CN。
|
|
7
|
+
* 更多信息可以查看 https://www.rfc-editor.org/rfc/bcp/bcp47.txt 。
|
|
8
|
+
* @param tag
|
|
9
|
+
* @returns 语言和地区信息,为了方便匹配,全转小写
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseLangTag(tag: string): LangTag;
|
package/types/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './log';
|
|
2
|
+
export * from './i18n';
|
|
3
|
+
export * from './validation';
|
|
4
|
+
export * from './config';
|
|
5
|
+
export * from './cache';
|
|
6
|
+
export * from './task';
|
|
7
|
+
export * from './mvc';
|
|
8
|
+
export * from './mysql';
|
|
9
|
+
export * from './http-client';
|
|
10
|
+
export * from './mongodb';
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { LogLevel } from './level';
|
|
2
|
+
/**
|
|
3
|
+
* 日志配置的定义
|
|
4
|
+
*/
|
|
5
|
+
export interface LogConfig {
|
|
6
|
+
/**
|
|
7
|
+
* 是否输出到文件,如果为 true ,则会在根目录下生成 logs 目录存放日志文件.
|
|
8
|
+
*/
|
|
9
|
+
file: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* 文件目录
|
|
12
|
+
*/
|
|
13
|
+
fileDir: string;
|
|
14
|
+
/**
|
|
15
|
+
* 文件最大保存天数.需要开启文件才有效.
|
|
16
|
+
*/
|
|
17
|
+
fileMaxDays: number;
|
|
18
|
+
/**
|
|
19
|
+
* 要输出的日志级别.
|
|
20
|
+
*/
|
|
21
|
+
level: LogLevel;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 默认配置信息.
|
|
25
|
+
*/
|
|
26
|
+
declare const config: LogConfig;
|
|
27
|
+
export { config };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 获取日志对象.
|
|
3
|
+
* @returns
|
|
4
|
+
*/
|
|
5
|
+
export declare function getLogger(): Readonly<{
|
|
6
|
+
/**
|
|
7
|
+
* debug 日志
|
|
8
|
+
*/
|
|
9
|
+
debug(message: string): void;
|
|
10
|
+
isDebugEnabled(): boolean;
|
|
11
|
+
/**
|
|
12
|
+
* info 日志
|
|
13
|
+
* @param message
|
|
14
|
+
*/
|
|
15
|
+
info(message: string): void;
|
|
16
|
+
isInfoEnabled(): boolean;
|
|
17
|
+
/**
|
|
18
|
+
* 警告日志
|
|
19
|
+
* @param message
|
|
20
|
+
* @param error
|
|
21
|
+
*/
|
|
22
|
+
warn(message: string, error?: any): void;
|
|
23
|
+
isWarnEnabled(): boolean;
|
|
24
|
+
/**
|
|
25
|
+
* 错误日志
|
|
26
|
+
* @param message
|
|
27
|
+
* @param error
|
|
28
|
+
*/
|
|
29
|
+
error(message: string, error?: any): void;
|
|
30
|
+
isErrorEnabled(): boolean;
|
|
31
|
+
}>;
|
|
32
|
+
export * from './config';
|
|
33
|
+
export * from './level';
|
|
34
|
+
export { setLogStore } from './store';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 日志级别.
|
|
3
|
+
*/
|
|
4
|
+
export declare enum LogLevel {
|
|
5
|
+
DEBUG = "DEBUG",
|
|
6
|
+
INFO = "INFO",
|
|
7
|
+
WARN = "WARN",
|
|
8
|
+
ERROR = "ERROR"
|
|
9
|
+
}
|
|
10
|
+
export declare const LogLevelVals: Record<LogLevel, number>;
|
|
11
|
+
/**
|
|
12
|
+
* 解析日志级别
|
|
13
|
+
* @param level
|
|
14
|
+
*/
|
|
15
|
+
export declare function parseLogLevel(level: string): LogLevel;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* mogodb 集合信息
|
|
3
|
+
*
|
|
4
|
+
* <T> 数据类型,不能包含有 _id 字段,主键字段名称固定,不可以设置
|
|
5
|
+
*/
|
|
6
|
+
export interface MongoCollection<T> {
|
|
7
|
+
/**
|
|
8
|
+
* 集合名称.
|
|
9
|
+
*/
|
|
10
|
+
collectionName: string;
|
|
11
|
+
/**
|
|
12
|
+
* 创建时间字段信息,设置后在创建时会自动更新该字段
|
|
13
|
+
*/
|
|
14
|
+
createdDate?: {
|
|
15
|
+
field: keyof T;
|
|
16
|
+
type: 'number' | 'date';
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* 更新时间字段信息,设置后在更新记录时会自动维护该字段
|
|
20
|
+
*/
|
|
21
|
+
updatedDate?: {
|
|
22
|
+
field: keyof T;
|
|
23
|
+
type: 'number' | 'date';
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ValidationOpts } from '../validation';
|
|
2
|
+
/**
|
|
3
|
+
* MongoDB 配置.
|
|
4
|
+
*/
|
|
5
|
+
export interface MongoDBConfig {
|
|
6
|
+
/**
|
|
7
|
+
* 连接地址.
|
|
8
|
+
* 示例:mongodb+srv://<user>:<password>@<cluster-url>?retryWrites=true&writeConcern=majority
|
|
9
|
+
*/
|
|
10
|
+
uri: string;
|
|
11
|
+
/** The maximum number of connections in the connection pool. */
|
|
12
|
+
maxPoolSize: number;
|
|
13
|
+
/** The minimum number of connections in the connection pool. */
|
|
14
|
+
minPoolSize: number;
|
|
15
|
+
/** The maximum number of connections that may be in the process of being established concurrently by the connection pool. */
|
|
16
|
+
maxConnecting: number;
|
|
17
|
+
/** The maximum number of milliseconds that a connection can remain idle in the pool before being removed and closed. */
|
|
18
|
+
maxIdleTimeMS: number;
|
|
19
|
+
/** The maximum time in milliseconds that a thread can wait for a connection to become available. */
|
|
20
|
+
waitQueueTimeoutMS: number;
|
|
21
|
+
/**
|
|
22
|
+
* 开启慢查询警告,默认开启
|
|
23
|
+
*/
|
|
24
|
+
slowQueryWarn: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* 慢查询毫秒数,默认 200
|
|
27
|
+
*/
|
|
28
|
+
slowQueryMs: number;
|
|
29
|
+
/**
|
|
30
|
+
*事务超时时间,单位毫秒,默认 5000,设置为0的值表示不限制
|
|
31
|
+
*/
|
|
32
|
+
transactionTimeout: number;
|
|
33
|
+
/**
|
|
34
|
+
* 事务严格模式
|
|
35
|
+
*/
|
|
36
|
+
transactionStrict: boolean;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 默认配置.
|
|
40
|
+
*/
|
|
41
|
+
export declare const defaultConfig: MongoDBConfig;
|
|
42
|
+
/**
|
|
43
|
+
* 配置校验
|
|
44
|
+
*/
|
|
45
|
+
export declare const configValidation: ValidationOpts<MongoDBConfig>;
|