abler-api 0.1.1 → 0.1.6

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.
@@ -293,6 +293,31 @@ class apiUtil$1 {
293
293
  */
294
294
 
295
295
 
296
+ static sendResponse(response, promise) {
297
+ promise.then(function (data) {
298
+ if (!response.finished) {
299
+ try {
300
+ response.send(data);
301
+ } catch (e) {
302
+ console.log(`response.send 出错了,req.url: ${response.req.url}, response:`, response);
303
+ console.log("异常信息", e);
304
+ }
305
+ }
306
+ }, function (error) {
307
+ console.log('ERROR:', error);
308
+
309
+ if (!response.finished) {
310
+ response.send(error);
311
+ }
312
+ });
313
+ }
314
+ /**
315
+ * API 服务结束,包装数据并发送响应
316
+ * @param response
317
+ * @param promise
318
+ */
319
+
320
+
296
321
  static async responseOf(response, promise) {
297
322
  promise.then(function (data) {
298
323
  // console.log('RES:', apiSuccess(data));
@@ -967,6 +992,50 @@ class apiUtil$1 {
967
992
 
968
993
  static tokenDataExist(token) {
969
994
  return kvStorage.stored(token);
995
+ }
996
+
997
+ static parametersOK(params) {
998
+ configNeeded();
999
+ let paramSufix = params._param_sufix || "";
1000
+
1001
+ for (let i = 1; i < arguments.length; i++) {
1002
+ let paramName = arguments[i];
1003
+ let paramExists = false;
1004
+
1005
+ if (paramName.includes("/")) {
1006
+ let names = paramName.split("/"); // if (params[names[0]]) {
1007
+ // paramExists = true;
1008
+ // } else {
1009
+ // if (params[names[1]]) {
1010
+ // paramExists = true;
1011
+ // params[names[0]] = params[names[1]];
1012
+ // delete params[names[1]];
1013
+ // }
1014
+ // }
1015
+
1016
+ for (let ind in names) {
1017
+ let name = names[ind];
1018
+
1019
+ if (typeof params[name] != 'undefined') {
1020
+ paramExists = true;
1021
+ break;
1022
+ }
1023
+
1024
+ if (paramSufix != "" && typeof params[name + paramSufix] != 'undefined') {
1025
+ paramExists = true;
1026
+ break;
1027
+ }
1028
+ }
1029
+
1030
+ paramName = `${paramName} 之一`;
1031
+ } else {
1032
+ paramExists = typeof params[paramName] != 'undefined' || typeof params[paramName + paramSufix] != 'undefined';
1033
+ }
1034
+
1035
+ if (!paramExists) return ppUtil$1.errorPormise(err.ERROR(err.PARAMETER_NEEDED, paramName));
1036
+ }
1037
+
1038
+ return Promise.resolve(paramSufix);
970
1039
  } //#endregion
971
1040
  //region ====中间件
972
1041
 
@@ -285,6 +285,31 @@ class apiUtil$1 {
285
285
  */
286
286
 
287
287
 
288
+ static sendResponse(response, promise) {
289
+ promise.then(function (data) {
290
+ if (!response.finished) {
291
+ try {
292
+ response.send(data);
293
+ } catch (e) {
294
+ console.log(`response.send 出错了,req.url: ${response.req.url}, response:`, response);
295
+ console.log("异常信息", e);
296
+ }
297
+ }
298
+ }, function (error) {
299
+ console.log('ERROR:', error);
300
+
301
+ if (!response.finished) {
302
+ response.send(error);
303
+ }
304
+ });
305
+ }
306
+ /**
307
+ * API 服务结束,包装数据并发送响应
308
+ * @param response
309
+ * @param promise
310
+ */
311
+
312
+
288
313
  static async responseOf(response, promise) {
289
314
  promise.then(function (data) {
290
315
  // console.log('RES:', apiSuccess(data));
@@ -959,6 +984,50 @@ class apiUtil$1 {
959
984
 
960
985
  static tokenDataExist(token) {
961
986
  return kvStorage.stored(token);
987
+ }
988
+
989
+ static parametersOK(params) {
990
+ configNeeded();
991
+ let paramSufix = params._param_sufix || "";
992
+
993
+ for (let i = 1; i < arguments.length; i++) {
994
+ let paramName = arguments[i];
995
+ let paramExists = false;
996
+
997
+ if (paramName.includes("/")) {
998
+ let names = paramName.split("/"); // if (params[names[0]]) {
999
+ // paramExists = true;
1000
+ // } else {
1001
+ // if (params[names[1]]) {
1002
+ // paramExists = true;
1003
+ // params[names[0]] = params[names[1]];
1004
+ // delete params[names[1]];
1005
+ // }
1006
+ // }
1007
+
1008
+ for (let ind in names) {
1009
+ let name = names[ind];
1010
+
1011
+ if (typeof params[name] != 'undefined') {
1012
+ paramExists = true;
1013
+ break;
1014
+ }
1015
+
1016
+ if (paramSufix != "" && typeof params[name + paramSufix] != 'undefined') {
1017
+ paramExists = true;
1018
+ break;
1019
+ }
1020
+ }
1021
+
1022
+ paramName = `${paramName} 之一`;
1023
+ } else {
1024
+ paramExists = typeof params[paramName] != 'undefined' || typeof params[paramName + paramSufix] != 'undefined';
1025
+ }
1026
+
1027
+ if (!paramExists) return ppUtil$1.errorPormise(err.ERROR(err.PARAMETER_NEEDED, paramName));
1028
+ }
1029
+
1030
+ return Promise.resolve(paramSufix);
962
1031
  } //#endregion
963
1032
  //region ====中间件
964
1033
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "abler-api",
3
- "version": "0.1.1",
3
+ "version": "0.1.6",
4
4
  "description": "API服务相关工具",
5
5
  "main": "./dist/cjs/pp-util.js",
6
6
  "module": "./dist/es/pp-util.js",
@@ -15,8 +15,8 @@
15
15
  "license": "ISC",
16
16
  "dependencies": {
17
17
  "q": "^1.5.1",
18
- "abler-util": "^0.1.6",
19
- "abler-db": "^0.1.3"
18
+ "abler-util": "~0.1.6",
19
+ "abler-db": "~0.1.3"
20
20
  },
21
21
  "devDependencies": {
22
22
  "@babel/core": "^7.18.5",