wok-server 0.5.0 → 0.7.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/README.en.md +61 -0
- package/README.md +44 -29
- package/dist/cache/cache.js +98 -98
- package/dist/cache/config.js +19 -19
- package/dist/cache/index.js +27 -27
- package/dist/cache/purge-task.js +46 -46
- package/dist/cache/stat.js +47 -47
- package/dist/config/convert.js +36 -36
- package/dist/config/exception.js +14 -14
- package/dist/config/index.js +81 -81
- package/dist/http-client/index.js +136 -136
- package/dist/i18n/ar.js +17 -17
- package/dist/i18n/de.js +17 -17
- package/dist/i18n/en-us.js +17 -17
- package/dist/i18n/es.js +17 -17
- package/dist/i18n/fr.js +17 -17
- package/dist/i18n/i18n.js +231 -231
- package/dist/i18n/index.js +52 -52
- package/dist/i18n/ja.js +17 -17
- package/dist/i18n/ko.js +17 -17
- package/dist/i18n/msg.js +2 -2
- package/dist/i18n/pt.js +17 -17
- package/dist/i18n/ru.js +17 -17
- package/dist/i18n/tag.js +18 -18
- package/dist/i18n/zh-HK.js +17 -17
- package/dist/i18n/zh-TW.js +17 -17
- package/dist/i18n/zh-cn.js +17 -17
- package/dist/index.js +14 -14
- package/dist/lock/index.js +114 -114
- package/dist/log/config.js +35 -35
- package/dist/log/date.js +21 -21
- package/dist/log/file.js +198 -198
- package/dist/log/index.js +135 -135
- package/dist/log/level.js +33 -33
- package/dist/log/log.js +56 -56
- package/dist/log/store.js +19 -19
- package/dist/mongodb/collection.js +2 -2
- package/dist/mongodb/config.js +34 -34
- package/dist/mongodb/doc.js +2 -2
- package/dist/mongodb/exception.js +14 -14
- package/dist/mongodb/index.js +58 -58
- package/dist/mongodb/manager/base.js +563 -563
- package/dist/mongodb/manager/index.js +63 -63
- package/dist/mongodb/manager/tx-strict.js +84 -84
- package/dist/mongodb/manager/tx.js +30 -30
- package/dist/mongodb/migration.js +52 -52
- package/dist/mvc/access-log.js +33 -33
- package/dist/mvc/config.js +27 -27
- package/dist/mvc/exchange.js +113 -113
- package/dist/mvc/handler/index.js +7 -6
- package/dist/mvc/handler/json.js +65 -65
- package/dist/mvc/handler/restful.js +35 -35
- package/dist/mvc/handler/sse.js +65 -0
- package/dist/mvc/handler/upload.js +31 -31
- package/dist/mvc/index.js +50 -50
- package/dist/mvc/interceptor.js +2 -2
- package/dist/mvc/query.js +43 -43
- package/dist/mvc/render/file.js +132 -132
- package/dist/mvc/render/html/html.js +90 -90
- package/dist/mvc/render/html/index.js +18 -18
- package/dist/mvc/render/html/style.js +2 -2
- package/dist/mvc/render/index.js +7 -7
- package/dist/mvc/render/json.js +26 -26
- package/dist/mvc/render/text.js +16 -16
- package/dist/mvc/router.js +2 -2
- package/dist/mvc/server.js +272 -272
- package/dist/mvc/static/header.js +67 -67
- package/dist/mvc/static/index.js +6 -6
- package/dist/mvc/static/mime-type.js +84 -84
- package/dist/mvc/static/server-cache-config.js +66 -66
- package/dist/mvc/static/server-cache.js +133 -133
- package/dist/mvc/static/static-handler.js +372 -372
- package/dist/mysql/config.js +51 -51
- package/dist/mysql/exception.js +14 -14
- package/dist/mysql/index.js +87 -87
- package/dist/mysql/manager/base.js +278 -239
- package/dist/mysql/manager/index.js +107 -107
- package/dist/mysql/manager/ops/count.js +20 -20
- package/dist/mysql/manager/ops/criteria.js +381 -356
- package/dist/mysql/manager/ops/delete.js +59 -65
- package/dist/mysql/manager/ops/exist.js +26 -26
- package/dist/mysql/manager/ops/find.js +149 -169
- package/dist/mysql/manager/ops/index.js +16 -14
- package/dist/mysql/manager/ops/insert.js +132 -106
- package/dist/mysql/manager/ops/modify.js +10 -10
- package/dist/mysql/manager/ops/order-by.js +28 -0
- package/dist/mysql/manager/ops/paginate.js +48 -23
- package/dist/mysql/manager/ops/query.js +9 -9
- package/dist/mysql/manager/ops/update.js +222 -216
- package/dist/mysql/manager/ops/upsert.js +178 -0
- package/dist/mysql/manager/ops/utils.js +28 -24
- package/dist/mysql/manager/tx-strict.js +103 -103
- package/dist/mysql/manager/tx.js +30 -30
- package/dist/mysql/manager/utils.js +56 -56
- package/dist/mysql/migration.js +136 -136
- package/dist/mysql/table-info.js +8 -8
- package/dist/task/daily.js +59 -59
- package/dist/task/fixed-delay.js +38 -38
- package/dist/task/fixed-rate.js +42 -42
- package/dist/task/index.js +9 -9
- package/dist/task/task.js +56 -56
- package/dist/validation/exception.js +36 -36
- package/dist/validation/index.js +40 -40
- package/dist/validation/validator/array.js +34 -34
- package/dist/validation/validator/enum.js +28 -28
- package/dist/validation/validator/index.js +14 -14
- package/dist/validation/validator/length.js +40 -40
- package/dist/validation/validator/max-length.js +35 -35
- package/dist/validation/validator/max.js +29 -29
- package/dist/validation/validator/min-length.js +33 -33
- package/dist/validation/validator/min.js +29 -29
- package/dist/validation/validator/not-blank.js +33 -33
- package/dist/validation/validator/not-null.js +21 -21
- package/dist/validation/validator/plain-obj.js +32 -32
- package/dist/validation/validator/regexp.js +34 -34
- package/documentation/en/cache.md +56 -0
- package/documentation/en/config.md +96 -0
- package/documentation/en/engineering.md +256 -0
- package/documentation/en/http-client.md +32 -0
- package/documentation/en/i18n.md +143 -0
- package/documentation/en/index.md +24 -0
- package/documentation/en/lock.md +51 -0
- package/documentation/en/log.md +109 -0
- package/documentation/en/mongodb.md +256 -0
- package/documentation/en/mvc.md +688 -0
- package/documentation/en/mysql.md +682 -0
- package/documentation/en/task.md +45 -0
- package/documentation/en/test.md +56 -0
- package/documentation/en/validate.md +130 -0
- package/documentation/zh-cn/mvc.md +66 -24
- package/documentation/zh-cn/mysql.md +146 -17
- package/package.json +4 -1
- package/skills/wok-server-api-rules/SKILL.md +350 -0
- package/skills/wok-server-cache/SKILL.md +216 -0
- package/skills/wok-server-code-navigation/SKILL.md +153 -0
- package/skills/wok-server-config/SKILL.md +200 -0
- package/skills/wok-server-getting-started/SKILL.md +123 -0
- package/skills/wok-server-getting-started/references/engineering.md +169 -0
- package/skills/wok-server-http-client/SKILL.md +164 -0
- package/skills/wok-server-i18n/SKILL.md +214 -0
- package/skills/wok-server-lock/SKILL.md +144 -0
- package/skills/wok-server-log/SKILL.md +218 -0
- package/skills/wok-server-mongodb/SKILL.md +235 -0
- package/skills/wok-server-mvc/SKILL.md +251 -0
- package/skills/wok-server-mvc/references/respond-html.md +157 -0
- package/skills/wok-server-mvc/references/sse.md +121 -0
- package/skills/wok-server-mvc/references/static-files.md +47 -0
- package/skills/wok-server-mvc/references/upload.md +62 -0
- package/skills/wok-server-mvc/references/websocket.md +30 -0
- package/skills/wok-server-mysql/SKILL.md +388 -0
- package/skills/wok-server-mysql/references/multi-datasource.md +76 -0
- package/skills/wok-server-mysql/references/version-control.md +22 -0
- package/skills/wok-server-task/SKILL.md +158 -0
- package/skills/wok-server-validate/SKILL.md +167 -0
- package/src/cache/cache.ts +118 -0
- package/src/cache/config.ts +53 -0
- package/src/cache/index.ts +27 -0
- package/src/cache/purge-task.ts +53 -0
- package/src/cache/stat.ts +47 -0
- package/src/config/convert.ts +27 -0
- package/src/config/exception.ts +8 -0
- package/src/config/index.ts +92 -0
- package/src/http-client/index.ts +202 -0
- package/src/i18n/ar.ts +16 -0
- package/src/i18n/de.ts +16 -0
- package/src/i18n/en-us.ts +16 -0
- package/src/i18n/es.ts +16 -0
- package/src/i18n/fr.ts +16 -0
- package/src/i18n/i18n.ts +230 -0
- package/src/i18n/index.ts +50 -0
- package/src/i18n/ja.ts +16 -0
- package/src/i18n/ko.ts +16 -0
- package/src/i18n/msg.ts +50 -0
- package/src/i18n/pt.ts +16 -0
- package/src/i18n/ru.ts +16 -0
- package/src/i18n/tag.ts +18 -0
- package/src/i18n/zh-HK.ts +16 -0
- package/src/i18n/zh-TW.ts +16 -0
- package/src/i18n/zh-cn.ts +16 -0
- package/src/index.ts +11 -0
- package/src/lock/index.ts +164 -0
- package/src/log/config.ts +71 -0
- package/src/log/date.ts +19 -0
- package/src/log/file.ts +215 -0
- package/src/log/index.ts +136 -0
- package/src/log/level.ts +29 -0
- package/src/log/log.ts +77 -0
- package/src/log/store.ts +31 -0
- package/src/mongodb/collection.ts +25 -0
- package/src/mongodb/config.ts +69 -0
- package/src/mongodb/doc.ts +12 -0
- package/src/mongodb/exception.ts +8 -0
- package/src/mongodb/index.ts +71 -0
- package/src/mongodb/manager/base.ts +674 -0
- package/src/mongodb/manager/index.ts +80 -0
- package/src/mongodb/manager/tx-strict.ts +153 -0
- package/src/mongodb/manager/tx.ts +34 -0
- package/src/mongodb/migration.ts +66 -0
- package/src/mvc/access-log.ts +33 -0
- package/src/mvc/config.ts +70 -0
- package/src/mvc/exchange.ts +126 -0
- package/src/mvc/handler/index.ts +4 -0
- package/src/mvc/handler/json.ts +96 -0
- package/src/mvc/handler/restful.ts +39 -0
- package/src/mvc/handler/sse.ts +90 -0
- package/src/mvc/handler/upload.ts +54 -0
- package/src/mvc/index.ts +48 -0
- package/src/mvc/interceptor.ts +12 -0
- package/src/mvc/query.ts +36 -0
- package/src/mvc/render/file.ts +148 -0
- package/src/mvc/render/html/html.ts +187 -0
- package/src/mvc/render/html/index.ts +16 -0
- package/src/mvc/render/html/style.ts +1201 -0
- package/src/mvc/render/index.ts +4 -0
- package/src/mvc/render/json.ts +24 -0
- package/src/mvc/render/text.ts +14 -0
- package/src/mvc/router.ts +13 -0
- package/src/mvc/server.ts +315 -0
- package/src/mvc/static/header.ts +86 -0
- package/src/mvc/static/index.ts +3 -0
- package/src/mvc/static/mime-type.ts +81 -0
- package/src/mvc/static/server-cache-config.ts +92 -0
- package/src/mvc/static/server-cache.ts +171 -0
- package/src/mvc/static/static-handler.ts +445 -0
- package/src/mysql/config.ts +130 -0
- package/src/mysql/exception.ts +8 -0
- package/src/mysql/index.ts +88 -0
- package/src/mysql/manager/base.ts +332 -0
- package/src/mysql/manager/index.ts +112 -0
- package/src/mysql/manager/ops/count.ts +30 -0
- package/src/mysql/manager/ops/criteria.ts +446 -0
- package/src/mysql/manager/ops/delete.ts +91 -0
- package/src/mysql/manager/ops/exist.ts +41 -0
- package/src/mysql/manager/ops/find.ts +209 -0
- package/src/mysql/manager/ops/index.ts +13 -0
- package/src/mysql/manager/ops/insert.ts +158 -0
- package/src/mysql/manager/ops/modify.ts +14 -0
- package/src/mysql/manager/ops/order-by.ts +58 -0
- package/src/mysql/manager/ops/paginate.ts +100 -0
- package/src/mysql/manager/ops/query.ts +13 -0
- package/src/mysql/manager/ops/update.ts +318 -0
- package/src/mysql/manager/ops/upsert.ts +224 -0
- package/src/mysql/manager/ops/utils.ts +24 -0
- package/src/mysql/manager/tx-strict.ts +138 -0
- package/src/mysql/manager/tx.ts +31 -0
- package/src/mysql/manager/utils.ts +75 -0
- package/src/mysql/migration.ts +149 -0
- package/src/mysql/table-info.ts +41 -0
- package/src/task/daily.ts +70 -0
- package/src/task/fixed-delay.ts +45 -0
- package/src/task/fixed-rate.ts +49 -0
- package/src/task/index.ts +4 -0
- package/src/task/task.ts +70 -0
- package/src/validation/exception.ts +27 -0
- package/src/validation/index.ts +61 -0
- package/src/validation/validator/array.ts +32 -0
- package/src/validation/validator/enum.ts +25 -0
- package/src/validation/validator/index.ts +11 -0
- package/src/validation/validator/length.ts +42 -0
- package/src/validation/validator/max-length.ts +33 -0
- package/src/validation/validator/max.ts +26 -0
- package/src/validation/validator/min-length.ts +31 -0
- package/src/validation/validator/min.ts +26 -0
- package/src/validation/validator/not-blank.ts +31 -0
- package/src/validation/validator/not-null.ts +19 -0
- package/src/validation/validator/plain-obj.ts +30 -0
- package/src/validation/validator/regexp.ts +32 -0
- package/types/cache/cache.d.ts +52 -52
- package/types/cache/config.d.ts +32 -32
- package/types/cache/index.d.ts +2 -2
- package/types/cache/purge-task.d.ts +11 -11
- package/types/cache/stat.d.ts +26 -26
- package/types/config/convert.d.ts +6 -6
- package/types/config/exception.d.ts +7 -7
- package/types/config/index.d.ts +25 -25
- package/types/http-client/index.d.ts +71 -71
- package/types/i18n/ar.d.ts +2 -2
- package/types/i18n/de.d.ts +2 -2
- package/types/i18n/en-us.d.ts +2 -2
- package/types/i18n/es.d.ts +2 -2
- package/types/i18n/fr.d.ts +2 -2
- package/types/i18n/i18n.d.ts +102 -102
- package/types/i18n/index.d.ts +9 -9
- package/types/i18n/ja.d.ts +2 -2
- package/types/i18n/ko.d.ts +2 -2
- package/types/i18n/msg.d.ts +50 -50
- package/types/i18n/pt.d.ts +2 -2
- package/types/i18n/ru.d.ts +2 -2
- package/types/i18n/tag.d.ts +11 -11
- package/types/i18n/zh-HK.d.ts +2 -2
- package/types/i18n/zh-TW.d.ts +2 -2
- package/types/i18n/zh-cn.d.ts +2 -2
- package/types/index.d.ts +11 -11
- package/types/lock/index.d.ts +64 -64
- package/types/log/config.d.ts +35 -35
- package/types/log/date.d.ts +2 -2
- package/types/log/file.d.ts +13 -13
- package/types/log/index.d.ts +53 -53
- package/types/log/level.d.ts +14 -14
- package/types/log/log.d.ts +40 -40
- package/types/log/store.d.ts +19 -19
- package/types/mongodb/collection.d.ts +25 -25
- package/types/mongodb/config.d.ts +45 -45
- package/types/mongodb/doc.d.ts +11 -11
- package/types/mongodb/exception.d.ts +7 -7
- package/types/mongodb/index.d.ts +29 -29
- package/types/mongodb/manager/base.d.ts +188 -188
- package/types/mongodb/manager/index.d.ts +38 -38
- package/types/mongodb/manager/tx-strict.d.ts +41 -41
- package/types/mongodb/manager/tx.d.ts +21 -21
- package/types/mongodb/migration.d.ts +12 -12
- package/types/mvc/access-log.d.ts +7 -7
- package/types/mvc/config.d.ts +42 -42
- package/types/mvc/exchange.d.ts +72 -72
- package/types/mvc/handler/index.d.ts +4 -3
- package/types/mvc/handler/json.d.ts +44 -44
- package/types/mvc/handler/restful.d.ts +11 -11
- package/types/mvc/handler/sse.d.ts +34 -0
- package/types/mvc/handler/upload.d.ts +36 -36
- package/types/mvc/index.d.ts +22 -22
- package/types/mvc/interceptor.d.ts +11 -11
- package/types/mvc/query.d.ts +13 -13
- package/types/mvc/render/file.d.ts +10 -10
- package/types/mvc/render/html/html.d.ts +98 -98
- package/types/mvc/render/html/index.d.ts +11 -11
- package/types/mvc/render/html/style.d.ts +1201 -1201
- package/types/mvc/render/index.d.ts +4 -4
- package/types/mvc/render/json.d.ts +17 -17
- package/types/mvc/render/text.d.ts +10 -10
- package/types/mvc/router.d.ts +11 -11
- package/types/mvc/server.d.ts +90 -90
- package/types/mvc/static/header.d.ts +27 -27
- package/types/mvc/static/index.d.ts +3 -3
- package/types/mvc/static/mime-type.d.ts +2 -2
- package/types/mvc/static/server-cache-config.d.ts +30 -30
- package/types/mvc/static/server-cache.d.ts +76 -76
- package/types/mvc/static/static-handler.d.ts +77 -77
- package/types/mysql/config.d.ts +90 -90
- package/types/mysql/exception.d.ts +7 -7
- package/types/mysql/index.d.ts +16 -16
- package/types/mysql/manager/base.d.ts +196 -165
- package/types/mysql/manager/index.d.ts +36 -36
- package/types/mysql/manager/ops/count.d.ts +13 -13
- package/types/mysql/manager/ops/criteria.d.ts +144 -134
- package/types/mysql/manager/ops/delete.d.ts +47 -46
- package/types/mysql/manager/ops/exist.d.ts +6 -6
- package/types/mysql/manager/ops/find.d.ts +87 -86
- package/types/mysql/manager/ops/index.d.ts +12 -10
- package/types/mysql/manager/ops/insert.d.ts +32 -18
- package/types/mysql/manager/ops/modify.d.ts +3 -3
- package/types/mysql/manager/ops/order-by.d.ts +38 -0
- package/types/mysql/manager/ops/paginate.d.ts +53 -36
- package/types/mysql/manager/ops/query.d.ts +3 -3
- package/types/mysql/manager/ops/update.d.ts +99 -76
- package/types/mysql/manager/ops/upsert.d.ts +36 -0
- package/types/mysql/manager/ops/utils.d.ts +5 -5
- package/types/mysql/manager/tx-strict.d.ts +36 -36
- package/types/mysql/manager/tx.d.ts +15 -15
- package/types/mysql/manager/utils.d.ts +17 -17
- package/types/mysql/migration.d.ts +8 -8
- package/types/mysql/table-info.d.ts +36 -36
- package/types/task/daily.d.ts +16 -16
- package/types/task/fixed-delay.d.ts +9 -9
- package/types/task/fixed-rate.d.ts +9 -9
- package/types/task/index.d.ts +4 -4
- package/types/task/task.d.ts +34 -34
- package/types/validation/exception.d.ts +38 -38
- package/types/validation/index.d.ts +32 -32
- package/types/validation/validator/array.d.ts +5 -5
- package/types/validation/validator/enum.d.ts +8 -8
- package/types/validation/validator/index.d.ts +11 -11
- package/types/validation/validator/length.d.ts +10 -10
- package/types/validation/validator/max-length.d.ts +8 -8
- package/types/validation/validator/max.d.ts +7 -7
- package/types/validation/validator/min-length.d.ts +6 -6
- package/types/validation/validator/min.d.ts +7 -7
- package/types/validation/validator/not-blank.d.ts +7 -7
- package/types/validation/validator/not-null.d.ts +6 -6
- package/types/validation/validator/plain-obj.d.ts +7 -7
- package/types/validation/validator/regexp.d.ts +8 -8
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import { IncomingHttpHeaders, request as requestHttp } from 'http'
|
|
2
|
+
import { request as requestHttps } from 'https'
|
|
3
|
+
import { URL } from 'url'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 请求选项.
|
|
7
|
+
*/
|
|
8
|
+
export interface HttpRequestOpts {
|
|
9
|
+
/**
|
|
10
|
+
* 链接地址.
|
|
11
|
+
*/
|
|
12
|
+
url: string
|
|
13
|
+
/**
|
|
14
|
+
* 查询参数,附加在链接地址上
|
|
15
|
+
*/
|
|
16
|
+
query?: Record<string, string[] | string>
|
|
17
|
+
/**
|
|
18
|
+
* 正文,可以是序列化好的字符串或二进制内容.
|
|
19
|
+
*/
|
|
20
|
+
body?: string | Buffer
|
|
21
|
+
/**
|
|
22
|
+
* 消息头
|
|
23
|
+
*/
|
|
24
|
+
headers?: IncomingHttpHeaders
|
|
25
|
+
/**
|
|
26
|
+
* 请求方法
|
|
27
|
+
*/
|
|
28
|
+
method: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'CONNECT' | 'OPTIONS' | 'TRACE' | 'PATCH'
|
|
29
|
+
/**
|
|
30
|
+
* 超时时间,单位毫秒,默认 5000
|
|
31
|
+
*/
|
|
32
|
+
timeout?: number
|
|
33
|
+
/**
|
|
34
|
+
* 是否跟随重定向
|
|
35
|
+
*/
|
|
36
|
+
followRedirect?: boolean
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 响应信息.
|
|
40
|
+
*/
|
|
41
|
+
export interface HttpResponseInfo {
|
|
42
|
+
/**
|
|
43
|
+
* 状态码
|
|
44
|
+
*/
|
|
45
|
+
status: number
|
|
46
|
+
/**
|
|
47
|
+
* 消息头.
|
|
48
|
+
*/
|
|
49
|
+
headers: IncomingHttpHeaders
|
|
50
|
+
/**
|
|
51
|
+
* 响应正文
|
|
52
|
+
*/
|
|
53
|
+
body: Buffer
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 发送 http 请求.
|
|
58
|
+
* @param opts
|
|
59
|
+
* @returns
|
|
60
|
+
*/
|
|
61
|
+
export function doRequest(opts: HttpRequestOpts): Promise<HttpResponseInfo> {
|
|
62
|
+
return new Promise<HttpResponseInfo>((resolve, reject) => {
|
|
63
|
+
const url = new URL(opts.url)
|
|
64
|
+
// query
|
|
65
|
+
if (opts.query) {
|
|
66
|
+
Object.entries(opts.query).forEach(entry => {
|
|
67
|
+
const [key, val] = entry
|
|
68
|
+
// 忽略空值
|
|
69
|
+
if (val === undefined || val === null) {
|
|
70
|
+
return
|
|
71
|
+
} else if (typeof val === 'string') {
|
|
72
|
+
url.searchParams.append(key, val)
|
|
73
|
+
} else if (Array.isArray(val)) {
|
|
74
|
+
val.forEach(v => url.searchParams.append(key, v))
|
|
75
|
+
} else {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`The request parameter value is neither of string nor string[] type:${val} ,name: ${key}`
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
const request = url.protocol === 'https:' ? requestHttps : requestHttp
|
|
83
|
+
const req = request(
|
|
84
|
+
url,
|
|
85
|
+
{
|
|
86
|
+
method: opts.method,
|
|
87
|
+
headers: opts.headers,
|
|
88
|
+
timeout: opts.timeout && opts.timeout > 0 ? opts.timeout : 5000,
|
|
89
|
+
rejectUnauthorized: false
|
|
90
|
+
},
|
|
91
|
+
res => {
|
|
92
|
+
const chunks: any[] = []
|
|
93
|
+
res.on('error', reject)
|
|
94
|
+
res.on('data', chunk => chunks.push(chunk))
|
|
95
|
+
res.on('end', () => {
|
|
96
|
+
// 重定向支持
|
|
97
|
+
if (opts.followRedirect) {
|
|
98
|
+
// 301、302、303、307、308
|
|
99
|
+
if (
|
|
100
|
+
res.statusCode === 301 ||
|
|
101
|
+
res.statusCode === 302 ||
|
|
102
|
+
res.statusCode === 303 ||
|
|
103
|
+
res.statusCode === 307 ||
|
|
104
|
+
res.statusCode === 308
|
|
105
|
+
) {
|
|
106
|
+
if (res.headers.location) {
|
|
107
|
+
// 重新请求,但是重定向标记改为 false ,只重定向一次,防止无限重定向造成死循环
|
|
108
|
+
resolve(
|
|
109
|
+
doRequest(
|
|
110
|
+
Object.assign({}, opts, { url: res.headers.location, followRedirect: false })
|
|
111
|
+
)
|
|
112
|
+
)
|
|
113
|
+
return
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
const buffer = Buffer.concat(chunks)
|
|
118
|
+
resolve({
|
|
119
|
+
status: res.statusCode || 0,
|
|
120
|
+
body: buffer,
|
|
121
|
+
headers: res.headers
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
)
|
|
126
|
+
req.on('error', reject)
|
|
127
|
+
req.on('timeout', () => reject('Request timeout'))
|
|
128
|
+
if (opts.body) {
|
|
129
|
+
req.write(opts.body)
|
|
130
|
+
}
|
|
131
|
+
req.end()
|
|
132
|
+
})
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* 发送 json 格式的 post 请求,body 是未序列化的普通对象,表示要发送的请求数据.
|
|
136
|
+
* @param opts
|
|
137
|
+
*/
|
|
138
|
+
export async function postJson<T>(
|
|
139
|
+
opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'> & { body: any }
|
|
140
|
+
): Promise<T> {
|
|
141
|
+
const headers = Object.assign({}, opts.headers || {}, {
|
|
142
|
+
'Content-Type': 'application/json; charset=utf-8'
|
|
143
|
+
})
|
|
144
|
+
const res = await doRequest({
|
|
145
|
+
url: opts.url,
|
|
146
|
+
query: opts.query,
|
|
147
|
+
headers: headers,
|
|
148
|
+
timeout: opts.timeout,
|
|
149
|
+
method: 'POST',
|
|
150
|
+
body: JSON.stringify(opts.body),
|
|
151
|
+
followRedirect: false
|
|
152
|
+
})
|
|
153
|
+
if (res.status !== 200) {
|
|
154
|
+
if (res.body.byteLength < 1024) {
|
|
155
|
+
throw new Error(
|
|
156
|
+
`Request failed,status code:${res.status},url:${opts.url},body:${res.body.toString(
|
|
157
|
+
'utf-8'
|
|
158
|
+
)}`
|
|
159
|
+
)
|
|
160
|
+
} else {
|
|
161
|
+
throw new Error(`Request failed,status code:${res.status},url:${opts.url}`)
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const bodyText = res.body.toString('utf8')
|
|
165
|
+
if (!bodyText) {
|
|
166
|
+
return {} as T
|
|
167
|
+
}
|
|
168
|
+
return JSON.parse(bodyText)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* get 请求获取 json 格式数据.
|
|
173
|
+
* @param opts
|
|
174
|
+
*/
|
|
175
|
+
export async function getJson<T>(
|
|
176
|
+
opts: Pick<HttpRequestOpts, 'url' | 'query' | 'headers' | 'timeout'>
|
|
177
|
+
): Promise<T> {
|
|
178
|
+
const res = await doRequest({
|
|
179
|
+
url: opts.url,
|
|
180
|
+
query: opts.query,
|
|
181
|
+
headers: opts.headers,
|
|
182
|
+
timeout: opts.timeout,
|
|
183
|
+
method: 'GET',
|
|
184
|
+
followRedirect: true
|
|
185
|
+
})
|
|
186
|
+
if (res.status !== 200) {
|
|
187
|
+
if (res.body.byteLength < 1024) {
|
|
188
|
+
throw new Error(
|
|
189
|
+
`Request failed,status code:${res.status},url:${opts.url},body:${res.body.toString(
|
|
190
|
+
'utf-8'
|
|
191
|
+
)}`
|
|
192
|
+
)
|
|
193
|
+
} else {
|
|
194
|
+
throw new Error(`Request failed,status code:${res.status},url:${opts.url}`)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
const bodyText = res.body.toString('utf8')
|
|
198
|
+
if (!bodyText) {
|
|
199
|
+
return {} as T
|
|
200
|
+
}
|
|
201
|
+
return JSON.parse(bodyText)
|
|
202
|
+
}
|
package/src/i18n/ar.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const ar: I18nMsgs = {
|
|
4
|
+
'validate-err-array': 'القيمة لست مصفوفة',
|
|
5
|
+
'validate-err-enum': 'يجب أن تكون القيمة واحدة من {}',
|
|
6
|
+
'validate-err-numer': 'القيمة ليست رقمًا',
|
|
7
|
+
'validate-err-max': 'يجب ألا تكون أكبر من {}',
|
|
8
|
+
'validate-err-min': 'يجب ألا تكون أصغر من {}',
|
|
9
|
+
'validate-err-empty': 'لا يمكن أن يكونارغًا',
|
|
10
|
+
'validate-err-string': 'القيمة ليست من نوع سلسلة الحروف',
|
|
11
|
+
'validate-err-incorrect-format': 'التنسيق غير صحيح',
|
|
12
|
+
'validate-err-no-length': 'تعذر العثور على خاصية "length"',
|
|
13
|
+
'validate-err-length-not-number': 'خاصية "length" ليست رقما',
|
|
14
|
+
'validate-err-min-length': 'لا يجوز أن يكن الطول أقل من {}',
|
|
15
|
+
'validate-err-max-length': 'لا يجوز أن يكون الطول أكبر من {}'
|
|
16
|
+
}
|
package/src/i18n/de.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const de: I18nMsgs = {
|
|
4
|
+
'validate-err-array': 'Der Wert ist kein Array',
|
|
5
|
+
'validate-err-enum': 'Der Wert muss einer von sein',
|
|
6
|
+
'validate-err-numer': 'Der Wert ist keine Zahl',
|
|
7
|
+
'validate-err-max': 'Darf nicht größer als {} sein',
|
|
8
|
+
'validate-err-min': 'f nicht kleiner als {} sein',
|
|
9
|
+
'validate-err-empty': 'Darf nicht leer',
|
|
10
|
+
'validate-err-string': 'Der Wert ist kein Zeichenketten-Typ',
|
|
11
|
+
'validate-err-incorrect-format': 'Das Format ist nicht korrekt',
|
|
12
|
+
'validate-err-no-length': 'length kann nicht gefunden werden',
|
|
13
|
+
'validate-err-length-not-number': 'Eigenschaft length ist keine zahl',
|
|
14
|
+
'validate-err-min-length': 'Die L darf nicht kleiner als {} sein',
|
|
15
|
+
'validate-err-max-length': 'Die Länge nicht größer als {} sein'
|
|
16
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const enUS: I18nMsgs = {
|
|
4
|
+
'validate-err-array': 'Value is not a array type.',
|
|
5
|
+
'validate-err-enum': ' Value must be one of [{}].',
|
|
6
|
+
'validate-err-numer': 'Value is not a number type.',
|
|
7
|
+
'validate-err-max': 'Not greater than {}.',
|
|
8
|
+
'validate-err-min': 'Not less than {}.',
|
|
9
|
+
'validate-err-empty': 'Cannot be empty.',
|
|
10
|
+
'validate-err-string': 'Value is not a string type.',
|
|
11
|
+
'validate-err-incorrect-format': 'Incorrect format.',
|
|
12
|
+
'validate-err-no-length': 'Unable to find length field.',
|
|
13
|
+
'validate-err-length-not-number': 'Length field is not a number type.',
|
|
14
|
+
'validate-err-min-length': 'The length should not be less than {}.',
|
|
15
|
+
'validate-err-max-length': 'The length must not exceed {}.'
|
|
16
|
+
}
|
package/src/i18n/es.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const es: I18nMsgs = {
|
|
4
|
+
'validate-err-array': 'El valor no es un arreglo',
|
|
5
|
+
'validate-err-enum': 'El valor debe ser uno de {}',
|
|
6
|
+
'validate-err-numer': 'El valor no es un número',
|
|
7
|
+
'validate-err-max': 'No debe ser mayor que {}',
|
|
8
|
+
'validate-err-min': 'No debe ser menor que {}',
|
|
9
|
+
'validate-err-empty': 'No puede estar vacío',
|
|
10
|
+
'validate-err-string': 'El valor no es un tipo de cadena',
|
|
11
|
+
'validate-err-incorrect-format': 'El formato no es correcto',
|
|
12
|
+
'validate-err-no-length': 'No se encontró la propiedad de longitud',
|
|
13
|
+
'validate-err-length-not-number': ' propiedad de longitud no es un número',
|
|
14
|
+
'validate-err-min-length': 'La longitud no debe ser menor que {}',
|
|
15
|
+
'validate-err-max-length': 'La longitud no debe ser mayor que'
|
|
16
|
+
}
|
package/src/i18n/fr.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const fr: I18nMsgs = {
|
|
4
|
+
'validate-err-array': "La valeur n'est pas un tableau",
|
|
5
|
+
'validate-err-enum': "La valeur doit être l'un de {}",
|
|
6
|
+
'validate-err-numer': "La n'est pas un nombre",
|
|
7
|
+
'validate-err-max': 'Ne doit pas supérieur à {}',
|
|
8
|
+
'validate-err-min': 'Ne doit pas être inférieur à {}',
|
|
9
|
+
'validate-err-empty': 'Ne doit pas être vide',
|
|
10
|
+
'validate-err-string': "La valeur n'est pas de type chaîne de caractères",
|
|
11
|
+
'validate-err-incorrect-format': 'Format incorrect',
|
|
12
|
+
'validate-err-no-length': 'Impossible de trouver la propriété "length"',
|
|
13
|
+
'validate-err-length-not-number': `propriété "length" n'est pas un nombre`,
|
|
14
|
+
'validate-err-min-length': 'La longueur ne doit pas être infure {}',
|
|
15
|
+
'validate-err-max-length': 'La longueur ne doit pas être supérieure à {}'
|
|
16
|
+
}
|
package/src/i18n/i18n.ts
ADDED
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { IncomingHttpHeaders } from 'http'
|
|
2
|
+
import { parseLangTag } from './tag'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* 绑定语言的 i18n 类型,不可切换语言.
|
|
6
|
+
*/
|
|
7
|
+
export class BoundI18n<T> {
|
|
8
|
+
constructor(private readonly lang: string, private readonly msgs: T) {}
|
|
9
|
+
/**
|
|
10
|
+
* 获取当前语言
|
|
11
|
+
*/
|
|
12
|
+
getLang(): string {
|
|
13
|
+
return this.lang
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 构建国际化消息.
|
|
18
|
+
* @param key 消息模板的 key
|
|
19
|
+
* @param args 参数,如果 key 对应的消息模板有占位符号({})可以使用参数来填充
|
|
20
|
+
*/
|
|
21
|
+
buildMsg(key: keyof T, ...args: string[]): string {
|
|
22
|
+
const template = this.msgs[key] as unknown as string
|
|
23
|
+
if (!args || !args.length) {
|
|
24
|
+
return template
|
|
25
|
+
}
|
|
26
|
+
let idx = 0
|
|
27
|
+
return template.replace(/\{\}/g, () => `${args[idx++]}`)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* 国际化,提供不同语言消息构建的功能,默认英文.
|
|
32
|
+
*/
|
|
33
|
+
export class I18n<T> {
|
|
34
|
+
/**
|
|
35
|
+
* 当前语言
|
|
36
|
+
*/
|
|
37
|
+
private lang: string
|
|
38
|
+
/**
|
|
39
|
+
* 消息模板表,与当前语言是对应的
|
|
40
|
+
*/
|
|
41
|
+
private msgs: T
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 消息表,key 是语言,value 是一个 key 为地区,value 为 消息模板的 map,两层 map 为的是能够快速匹配.
|
|
45
|
+
* 地区为空的情况下,缺省值为 - 。语言和地区的 key 全转成小写存储。
|
|
46
|
+
*/
|
|
47
|
+
private msgsMap: Map<string, Map<string, T>>
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* 构造 i18n 对象,必须指定英文的消息模板作为默认。
|
|
51
|
+
* @param enMsgs 英文的消息模板
|
|
52
|
+
*/
|
|
53
|
+
constructor(readonly enMsgs: T) {
|
|
54
|
+
const regionMap = new Map<string, T>()
|
|
55
|
+
regionMap.set('-', enMsgs)
|
|
56
|
+
this.msgsMap = new Map<string, Map<string, T>>()
|
|
57
|
+
this.msgsMap.set('en', regionMap)
|
|
58
|
+
this.lang = 'en'
|
|
59
|
+
this.msgs = enMsgs
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
protected findMsgsByLang(langTag: string): T | undefined {
|
|
63
|
+
// 检查表中是否支持指定语言,如果不支持则使用默认语言
|
|
64
|
+
const tag = parseLangTag(langTag)
|
|
65
|
+
const regionMap = this.msgsMap.get(tag.lang)
|
|
66
|
+
if (!regionMap) {
|
|
67
|
+
return undefined
|
|
68
|
+
}
|
|
69
|
+
// 查找顺序:
|
|
70
|
+
// 1. 如果有地区信息,查找地区标签对应的消息
|
|
71
|
+
// 2. 上一步没有找到,则查找默认地区标签对应的消息
|
|
72
|
+
// 3. 前面都没有找到,则取语言下的第一个地区的消息
|
|
73
|
+
let msgs: T | undefined = undefined
|
|
74
|
+
if (tag.region) {
|
|
75
|
+
msgs = regionMap.get(tag.region)
|
|
76
|
+
}
|
|
77
|
+
if (!msgs) {
|
|
78
|
+
msgs = regionMap.get('-')
|
|
79
|
+
}
|
|
80
|
+
if (!msgs) {
|
|
81
|
+
msgs = regionMap.values().next().value
|
|
82
|
+
}
|
|
83
|
+
return msgs
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 设置一种语言对应的消息模板,支持异步,可以做动态加载
|
|
87
|
+
* @param langTag
|
|
88
|
+
* @param msgs
|
|
89
|
+
*/
|
|
90
|
+
setMsgs(langTag: string, msgs: T) {
|
|
91
|
+
const tag = parseLangTag(langTag)
|
|
92
|
+
let regionMap = this.msgsMap.get(tag.lang)
|
|
93
|
+
if (!regionMap) {
|
|
94
|
+
regionMap = new Map<string, T>()
|
|
95
|
+
this.msgsMap.set(tag.lang, regionMap)
|
|
96
|
+
}
|
|
97
|
+
regionMap.set(tag.region || '-', msgs)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 给定一个语言标签列表,获得可以被支持的标签列表
|
|
102
|
+
* @param tags
|
|
103
|
+
*/
|
|
104
|
+
getSupportedLanguageTags(...tags: string[]): string[] {
|
|
105
|
+
return tags.filter(tag => {
|
|
106
|
+
const langTag = parseLangTag(tag)
|
|
107
|
+
return this.msgsMap.get(langTag.lang)
|
|
108
|
+
})
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 设置当前语言,如果指定的语言不被支持,则修改无效
|
|
113
|
+
* @param lang 语言本标签,如 en-US
|
|
114
|
+
* @returns 是否成功,如果语言不支持则返回 false,不会改变现有语言
|
|
115
|
+
*/
|
|
116
|
+
setLang(lang: string): boolean {
|
|
117
|
+
const msgs = this.findMsgsByLang(lang)
|
|
118
|
+
if (!msgs) {
|
|
119
|
+
return false
|
|
120
|
+
}
|
|
121
|
+
this.lang = lang
|
|
122
|
+
this.msgs = msgs
|
|
123
|
+
return true
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 获取当前语言
|
|
128
|
+
*/
|
|
129
|
+
getLang(): string {
|
|
130
|
+
return this.lang
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 构建国际化消息.
|
|
135
|
+
* @param key 消息模板的 key
|
|
136
|
+
* @param args 参数,如果 key 对应的消息模板有占位符号({})可以使用参数来填充
|
|
137
|
+
*/
|
|
138
|
+
buildMsg(key: keyof T, ...args: string[]): string {
|
|
139
|
+
const template = this.msgs[key] as unknown as string
|
|
140
|
+
if (!args || !args.length) {
|
|
141
|
+
return template
|
|
142
|
+
}
|
|
143
|
+
let idx = 0
|
|
144
|
+
return template.replace(/\{\}/g, () => `${args[idx++]}`)
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* 绑定一种语言,生成新的 i18n 对象,该对象仅能使用某一种语言.
|
|
148
|
+
* @param langTag 语言标签,无值则表示绑定当前语言
|
|
149
|
+
* @returns 返回一个语言受限的 i18n 对象.
|
|
150
|
+
*/
|
|
151
|
+
bindLang(langTag?: string): BoundI18n<T> {
|
|
152
|
+
if (!langTag) {
|
|
153
|
+
return new BoundI18n(this.lang, this.msgs)
|
|
154
|
+
}
|
|
155
|
+
const msgs = this.findMsgsByLang(langTag)
|
|
156
|
+
if (!msgs) {
|
|
157
|
+
throw new Error(`Unsupported language : ${langTag}`)
|
|
158
|
+
}
|
|
159
|
+
return new BoundI18n(langTag, msgs)
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 根据请求进行语言切换,解析消息头 'accept-language' ,如果 'accept-language' 中没有包含能够支持的语言则使用默认的英文.
|
|
163
|
+
* @param headers 消息头
|
|
164
|
+
*/
|
|
165
|
+
switchByRequest(headers: IncomingHttpHeaders) {
|
|
166
|
+
const langs = this.parseAcceptLanguage(headers)
|
|
167
|
+
const tags = this.getSupportedLanguageTags(...langs)
|
|
168
|
+
if (tags.length) {
|
|
169
|
+
this.setLang(tags[0])
|
|
170
|
+
} else {
|
|
171
|
+
this.setLang('en')
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private parseAcceptLanguage(headers: IncomingHttpHeaders): string[] {
|
|
176
|
+
const acceptLang = headers['accept-language']
|
|
177
|
+
if (!acceptLang) {
|
|
178
|
+
return []
|
|
179
|
+
}
|
|
180
|
+
// zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
|
|
181
|
+
// 为了性能考虑,暂不支持 q 参数设定的优先级,否则还要排序
|
|
182
|
+
const tags = acceptLang.split(',')
|
|
183
|
+
const langs: string[] = []
|
|
184
|
+
for (const tag of tags) {
|
|
185
|
+
const [t, q] = tag.split(';')
|
|
186
|
+
if ('*' !== t) {
|
|
187
|
+
langs.push(t)
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
return langs
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* 根据请求进行语言绑定,解析消息头 'accept-language',绑定受支持的第一个语言,如果没有可以被支持的语言,则使用默认的英文.
|
|
194
|
+
* @param headers 消息头
|
|
195
|
+
*/
|
|
196
|
+
bindByRequest(headers: IncomingHttpHeaders): BoundI18n<T> {
|
|
197
|
+
const langs = this.parseAcceptLanguage(headers)
|
|
198
|
+
const tags = this.getSupportedLanguageTags(...langs)
|
|
199
|
+
if (tags.length) {
|
|
200
|
+
return this.bindLang(tags[0])
|
|
201
|
+
} else {
|
|
202
|
+
return this.bindLang('en')
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* 可扩展的 i18n ,增加扩展新 i18n 对象的功能,记录扩展的新对象,同步 setLang 操作
|
|
209
|
+
*/
|
|
210
|
+
export class ExtensibleI18n<T extends object> extends I18n<T> {
|
|
211
|
+
private extendedI18ns: I18n<any>[] = []
|
|
212
|
+
|
|
213
|
+
setLang(lang: string): boolean {
|
|
214
|
+
for (const ex of this.extendedI18ns) {
|
|
215
|
+
ex.setLang(lang)
|
|
216
|
+
}
|
|
217
|
+
return super.setLang(lang)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* 扩展国际化内容,生成一个新的 i18n 对象,与当前的 i18n 对象语言保持一致,可在新对象中设置扩展部分的语言。
|
|
222
|
+
* <K> 新的消息模板类型
|
|
223
|
+
* @param enMsgs
|
|
224
|
+
*/
|
|
225
|
+
extend<K extends object>(enMsgs: K): I18n<K> {
|
|
226
|
+
const extendedI18n = new I18n<K>(enMsgs)
|
|
227
|
+
this.extendedI18ns.push(extendedI18n)
|
|
228
|
+
return extendedI18n
|
|
229
|
+
}
|
|
230
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ar } from './ar'
|
|
2
|
+
import { de } from './de'
|
|
3
|
+
import { enUS } from './en-us'
|
|
4
|
+
import { es } from './es'
|
|
5
|
+
import { fr } from './fr'
|
|
6
|
+
import { ExtensibleI18n } from './i18n'
|
|
7
|
+
import { ja } from './ja'
|
|
8
|
+
import { ko } from './ko'
|
|
9
|
+
import { I18nMsgs } from './msg'
|
|
10
|
+
import { pt } from './pt'
|
|
11
|
+
import { ru } from './ru'
|
|
12
|
+
import { zhHK } from './zh-HK'
|
|
13
|
+
import { zhTW } from './zh-TW'
|
|
14
|
+
import { zhCN } from './zh-cn'
|
|
15
|
+
|
|
16
|
+
let I18N: ExtensibleI18n<I18nMsgs> | undefined
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 获取 i18n 对象
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
export function getI18n(): ExtensibleI18n<I18nMsgs> {
|
|
23
|
+
if (I18N) {
|
|
24
|
+
return I18N
|
|
25
|
+
}
|
|
26
|
+
// 如果 i18n 对象不存在,则创建,并进行初始化
|
|
27
|
+
I18N = new ExtensibleI18n(enUS)
|
|
28
|
+
I18N.setMsgs('zh-CN', zhCN)
|
|
29
|
+
I18N.setMsgs('zh-TW', zhTW)
|
|
30
|
+
I18N.setMsgs('zh-HK', zhHK)
|
|
31
|
+
I18N.setMsgs('ja', ja)
|
|
32
|
+
I18N.setMsgs('ko', ko)
|
|
33
|
+
I18N.setMsgs('es', es)
|
|
34
|
+
I18N.setMsgs('de', de)
|
|
35
|
+
I18N.setMsgs('fr', fr)
|
|
36
|
+
I18N.setMsgs('ar', ar)
|
|
37
|
+
I18N.setMsgs('pt', pt)
|
|
38
|
+
I18N.setMsgs('ru', ru)
|
|
39
|
+
// 当前语言
|
|
40
|
+
if (process.env.LANG) {
|
|
41
|
+
I18N.setLang(process.env.LANG)
|
|
42
|
+
} else if (process.env.LC_CTYPE) {
|
|
43
|
+
const [tag] = process.env.LC_CTYPE.split('.')
|
|
44
|
+
I18N.setLang(tag)
|
|
45
|
+
}
|
|
46
|
+
return I18N
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export * from './i18n'
|
|
50
|
+
export * from './msg'
|
package/src/i18n/ja.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const ja: I18nMsgs = {
|
|
4
|
+
'validate-err-array': '値が配列ではありません',
|
|
5
|
+
'validate-err-enum': '値は {} の中のいずれかでなければなりません',
|
|
6
|
+
'validate-err-numer': '値が数字ではありません',
|
|
7
|
+
'validate-err-max': '{} 以下である必要があります',
|
|
8
|
+
'validate-err-min': '{} 以上である必要があります',
|
|
9
|
+
'validate-err-empty': '空であってはいけません',
|
|
10
|
+
'validate-err-string': '値が文字列のタイプではありません',
|
|
11
|
+
'validate-err-incorrect-format': '形式が正しくありません',
|
|
12
|
+
'validate-err-no-length': 'length属性が見当たりません',
|
|
13
|
+
'validate-err-length-not-number': 'lengthプロパティは数値ではありません',
|
|
14
|
+
'validate-err-min-length': '長さは {} 以上でなければりません',
|
|
15
|
+
'validate-err-max-length': '長さは {} 以下でなければなりません'
|
|
16
|
+
}
|
package/src/i18n/ko.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const ko: I18nMsgs = {
|
|
4
|
+
'validate-err-array': '값은 배열이 아닙니다',
|
|
5
|
+
'validate-err-enum': '값은 {} 중 하나여야 합니다',
|
|
6
|
+
'validate-err-numer': '값은 숫자가 아닙니다',
|
|
7
|
+
'validate-err-max': '{}보 크면 안 됩니다',
|
|
8
|
+
'validate-err-min': '{}보다 작으면 안 됩니다',
|
|
9
|
+
'validate-err-empty': '비워둘 수 없습니다',
|
|
10
|
+
'validate-err-string': '값이 문자열 유형이 아닙니다',
|
|
11
|
+
'validate-err-incorrect-format': '형식이 올바르지 않습니다',
|
|
12
|
+
'validate-err-no-length': 'length 속성을 찾을 수 없습니다',
|
|
13
|
+
'validate-err-length-not-number': 'length 속성이 숫자가 아닙니다',
|
|
14
|
+
'validate-err-min-length': '길이는 {}보다 작을 수 없습니다',
|
|
15
|
+
'validate-err-max-length': '길이는 {}보다 클 수 없습니다'
|
|
16
|
+
}
|
package/src/i18n/msg.ts
ADDED
|
@@ -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
|
+
}
|
package/src/i18n/pt.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const pt: I18nMsgs = {
|
|
4
|
+
'validate-err-array': 'O valor não é um array',
|
|
5
|
+
'validate-err-enum': 'O valor deve ser um dos {}',
|
|
6
|
+
'validate-err-numer': 'O valor não é um número',
|
|
7
|
+
'validate-err-max': 'Não pode ser maior que {}',
|
|
8
|
+
'validate-err-min': 'Não pode ser menor que {}',
|
|
9
|
+
'validate-err-empty': 'Não pode ser vazio',
|
|
10
|
+
'validate-err-string': 'O valor não é do tipo string',
|
|
11
|
+
'validate-err-incorrect-format': 'Formato incorreto',
|
|
12
|
+
'validate-err-no-length': 'Não foi possível encontrar a propriedade "length"',
|
|
13
|
+
'validate-err-length-not-number': 'A propriedade "length" não é um número',
|
|
14
|
+
'validate-err-min-length': 'O comprimento não pode ser menor que {}',
|
|
15
|
+
'validate-err-max-length': 'O comprimento não pode ser maior que {}'
|
|
16
|
+
}
|
package/src/i18n/ru.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { I18nMsgs } from './msg'
|
|
2
|
+
|
|
3
|
+
export const ru: I18nMsgs = {
|
|
4
|
+
'validate-err-array': 'Значение не является массивом',
|
|
5
|
+
'validate-err-enum': 'Значение должно быть одним из {}',
|
|
6
|
+
'validate-err-numer': 'Значение не является числом',
|
|
7
|
+
'validate-err-max': 'Не должно быть больше {}',
|
|
8
|
+
'validate-err-min': 'Не должно быть меньше {}',
|
|
9
|
+
'validate-err-empty': 'не может быть пустым',
|
|
10
|
+
'validate-err-string': 'Значение не является строковым типом',
|
|
11
|
+
'validate-err-incorrect-format': 'Неправильный формат',
|
|
12
|
+
'validate-err-no-length': 'Никаких признаков length',
|
|
13
|
+
'validate-err-length-not-number': 'Атрибут длины не является числом',
|
|
14
|
+
'validate-err-min-length': 'Длина не должна быть меньше {}',
|
|
15
|
+
'validate-err-max-length': 'Длина не должна быть больше {}'
|
|
16
|
+
}
|