@x-9lab/xlab 1.0.0-alpha.2 → 1.0.0-alpha.3

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.
@@ -6,6 +6,13 @@
6
6
  */
7
7
  declare function create(cacheName: string, conf: Record<string, XLab.JsonValue>): any;
8
8
  export { create };
9
+ /**
10
+ * 获取一个缓存实例
11
+ * @param name 缓存名称
12
+ * @param conf 缓存配置
13
+ */
14
+ declare function get(name: string, conf?: Record<string, XLab.JsonValue>): any;
15
+ export { get };
9
16
  /**
10
17
  * 获取当前缓存实例数量
11
18
  * @return 缓存数量
@@ -78,12 +78,28 @@ declare function date(format: string, ts: Date): string;
78
78
  export { date };
79
79
  /**
80
80
  * 生成操作结果数据对象
81
- * @param status 布尔,操作结果
81
+ * @param status 返回码
82
82
  * @param result 返回的数据,数据格式根据具体模块而定
83
83
  * @param msg 返回的提示性信息
84
84
  * @return 数据对象
85
85
  */
86
- declare function responseResult<T = any>(status: boolean, result?: T, msg?: string): XLab.IStdRes<any>;
86
+ declare function responseResult<T = any>(status: number, result?: T, msg?: string): XLab.IStdRes;
87
+ /**
88
+ * 生成操作结果数据对象
89
+ * @param status 操作结果
90
+ * @param result 返回的数据,数据格式根据具体模块而定
91
+ * @param msg 返回的提示性信息
92
+ * @return 数据对象
93
+ */
94
+ declare function responseResult<T = any>(status: boolean, result?: T, msg?: string): XLab.IStdRes;
95
+ /**
96
+ * 生成操作结果数据对象
97
+ * @param status 返回结果数据
98
+ * @param result 返回的数据,数据格式根据具体模块而定
99
+ * @param msg 返回的提示性信息
100
+ * @return 数据对象
101
+ */
102
+ declare function responseResult<T = any>(status: XLab.ICodeItem, result?: T, msg?: string): XLab.IStdRes;
87
103
  export { responseResult };
88
104
  /**
89
105
  * 根据传入的参数生成一个 MD5 值
@@ -42,13 +42,13 @@ declare global {
42
42
  * @param name model 名称
43
43
  * @return model 模块
44
44
  */
45
- function requireModel<T = any>(name: string): T;
45
+ function requireModel<T extends XLab.IModels[K], K extends keyof XLab.IModels>(name: K): T;
46
46
  /**
47
47
  * 全局获取 service 的方法
48
48
  * @param name service 名称
49
49
  * @return service 模块
50
50
  */
51
- function requireService<T = any>(name: string): T;
51
+ function requireService<T extends XLab.IServices[K], K extends keyof XLab.IServices>(name: K): T;
52
52
  /**
53
53
  * 获取系统配置
54
54
  */
@@ -85,6 +85,12 @@ declare global {
85
85
  interface CompositeObject {
86
86
  [key: string]: CompositeValue;
87
87
  }
88
+ /**业务服务 */
89
+ interface IServices {
90
+ }
91
+ /**业务数据模型 */
92
+ interface IModels {
93
+ }
88
94
  /**标准返回数据 */
89
95
  interface IStdRes<T = any> {
90
96
  /**业务执行状态码 */
@@ -166,12 +172,14 @@ declare global {
166
172
  /**静态资源缓存时间 */
167
173
  staticResourceCacheTime?: number;
168
174
  }
175
+ /**业务错误数据对象 */
169
176
  interface ICodeItem {
170
177
  /**错误码 */
171
178
  errorcode: number;
172
179
  /**错误信息 */
173
180
  msg: string;
174
181
  }
182
+ /**业务错误定义 */
175
183
  type ICodeDetail = Record<string, ICodeItem>;
176
184
  }
177
185
  }
@@ -10,8 +10,10 @@ function _export(target, all) {
10
10
  }
11
11
  _export(exports, {
12
12
  create: ()=>create,
13
+ get: ()=>get,
13
14
  len: ()=>len
14
15
  });
16
+ const _utils = require("@x-drive/utils");
15
17
  const _lruCache = /*#__PURE__*/ _interopRequireDefault(require("lru-cache"));
16
18
  function _interopRequireDefault(obj) {
17
19
  return obj && obj.__esModule ? obj : {
@@ -67,6 +69,22 @@ const CACHE_LIST = [];
67
69
  }
68
70
  return LRU_CACHES[cacheName];
69
71
  }
72
+ /**
73
+ * 获取一个缓存实例
74
+ * @param name 缓存名称
75
+ * @param conf 缓存配置
76
+ */ function get(name, conf) {
77
+ if (!(0, _utils.isString)(name)) {
78
+ throw new Error("必须指定缓存名称");
79
+ }
80
+ if (LRU_CACHES[name]) {
81
+ return LRU_CACHES[name];
82
+ }
83
+ if ((0, _utils.isObject)(conf)) {
84
+ return create(name, conf);
85
+ }
86
+ throw new Error(`找不到缓存 ${name}, 你可以先创建再调用本方法获取,或传入配置对象自动生成`);
87
+ }
70
88
  /**
71
89
  * 获取当前缓存实例数量
72
90
  * @return 缓存数量
@@ -29,6 +29,7 @@ _export(exports, {
29
29
  getRealDefaultMod: ()=>getRealDefaultMod
30
30
  });
31
31
  const _returnCode = require("./return-code");
32
+ const _utils = require("@x-drive/utils");
32
33
  const _querystring = /*#__PURE__*/ _interopRequireDefault(require("querystring"));
33
34
  const _request = /*#__PURE__*/ _interopRequireDefault(require("request"));
34
35
  const _crypto = /*#__PURE__*/ _interopRequireDefault(require("crypto"));
@@ -187,7 +188,7 @@ const format_exp = /[YymndjNwaAghGHis]/g;
187
188
  * @param msg 返回的提示性信息
188
189
  * @return 数据对象
189
190
  */ function responseResult(status, result, msg) {
190
- var code = (0, _returnCode.getReturnCode)(status);
191
+ var code = (0, _utils.isObject)(status) ? status : (0, _returnCode.getReturnCode)(status);
191
192
  var dat = {
192
193
  "success": code.errorcode === 0,
193
194
  "code": code.errorcode,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@x-9lab/xlab",
3
- "version": "1.0.0-alpha.2",
3
+ "version": "1.0.0-alpha.3",
4
4
  "description": "9lab 服务端模块",
5
5
  "versionDesc": "",
6
6
  "scripts": {