cgserver 14.0.0 → 14.0.1

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.
@@ -32,9 +32,12 @@ var __importStar = (this && this.__importStar) || (function () {
32
32
  return result;
33
33
  };
34
34
  })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
35
38
  Object.defineProperty(exports, "__esModule", { value: true });
36
39
  exports.gHttpTool = exports.HttpTool = void 0;
37
- const request = __importStar(require("request"));
40
+ const axios_1 = __importDefault(require("axios"));
38
41
  const qs = __importStar(require("querystring"));
39
42
  const Core_1 = require("../Core/Core");
40
43
  const Log_1 = require("./Log");
@@ -46,91 +49,129 @@ class HttpTool {
46
49
  set debug(value) {
47
50
  this._debug = value;
48
51
  }
49
- get(options_url) {
50
- let time = Date.now();
51
- let options = null;
52
- if (Core_1.core.isString(options_url)) {
53
- options = { url: options_url };
52
+ get(config) {
53
+ const time = Date.now();
54
+ config.method = 'GET';
55
+ // Support legacy qs/form style fields passed in config
56
+ if (config.qs && Core_1.core.isObject(config.qs)) {
57
+ const q = qs.stringify(config.qs);
58
+ config.url = config.url + (config.url.indexOf('?') === -1 ? '?' + q : '&' + q);
54
59
  }
55
- else {
56
- options = options_url;
60
+ if (config.form && Core_1.core.isObject(config.form)) {
61
+ config.headers = { ...(config.headers || {}), 'Content-Type': 'application/x-www-form-urlencoded' };
62
+ config.data = qs.stringify(config.form);
57
63
  }
58
64
  if (this._debug) {
59
- Log_1.gLog.info("prepare get:" + options.url);
65
+ Log_1.gLog.info("prepare get:" + config.url);
60
66
  }
61
- return new Promise((resolve, reject) => {
62
- request.get(options, (error, response, body) => {
63
- let originbody = body;
64
- if (error) {
65
- Log_1.gLog.error("get:" + options.url);
66
- Log_1.gLog.error(error);
67
- }
67
+ return (0, axios_1.default)(config).then(resp => {
68
+ let originbody = resp.data;
69
+ let body = originbody;
70
+ if (Core_1.core.isString(originbody)) {
68
71
  try {
69
- if (Core_1.core.isString(body)) {
70
- body = JSON.parse(body);
71
- }
72
+ body = JSON.parse(originbody);
72
73
  }
73
74
  catch (e) {
74
75
  try {
75
- body = qs.parse(body);
76
+ body = qs.parse(originbody);
76
77
  }
77
- catch (e) {
78
+ catch (e2) {
78
79
  body = originbody;
79
80
  }
80
81
  }
81
- if (this._debug) {
82
- Log_1.gLog.info({
83
- dttime: (Date.now() - time) + "ms",
84
- url: options.url,
85
- originbody: originbody
86
- });
82
+ }
83
+ if (this._debug) {
84
+ Log_1.gLog.info({ dttime: (Date.now() - time) + "ms", url: config.url, originbody });
85
+ }
86
+ return { error: null, response: resp, body, originbody };
87
+ }).catch(err => {
88
+ const resp = err.response || null;
89
+ let originbody = resp ? resp.data : null;
90
+ let body = originbody;
91
+ if (Core_1.core.isString(originbody)) {
92
+ try {
93
+ body = JSON.parse(originbody);
94
+ }
95
+ catch (e) {
96
+ try {
97
+ body = qs.parse(originbody);
98
+ }
99
+ catch (e2) {
100
+ body = originbody;
101
+ }
87
102
  }
88
- resolve({ error, response, body, originbody });
89
- });
103
+ }
104
+ Log_1.gLog.error("get:" + config.url);
105
+ Log_1.gLog.error(err);
106
+ if (this._debug) {
107
+ Log_1.gLog.info({ dttime: (Date.now() - time) + "ms", url: config.url, originbody });
108
+ }
109
+ return { error: err, response: resp, body, originbody };
90
110
  });
91
111
  }
92
- post(options_url) {
93
- let time = Date.now();
94
- let options = null;
95
- if (Core_1.core.isString(options_url)) {
96
- options = { url: options_url };
112
+ post(config) {
113
+ const time = Date.now();
114
+ config.method = 'POST';
115
+ // Legacy fields mapping
116
+ if (config.qs && Core_1.core.isObject(config.qs)) {
117
+ const q = qs.stringify(config.qs);
118
+ config.url = config.url + (config.url.indexOf('?') === -1 ? '?' + q : '&' + q);
119
+ }
120
+ if (config.formData && Core_1.core.isObject(config.formData)) {
121
+ // If user wants multipart they should provide FormData or appropriate headers; keep raw assignment
122
+ config.data = config.formData;
97
123
  }
98
- else {
99
- options = options_url;
124
+ else if (config.form && Core_1.core.isObject(config.form)) {
125
+ config.headers = { ...(config.headers || {}), 'Content-Type': 'application/x-www-form-urlencoded' };
126
+ config.data = qs.stringify(config.form);
100
127
  }
101
128
  if (this._debug) {
102
- Log_1.gLog.info("prepare post:" + options.url);
103
- Log_1.gLog.info("prepare post data:" + JSON.stringify(options.json || options.body || options.formData || options.form || options.qs));
129
+ Log_1.gLog.info("prepare post:" + config.url);
130
+ Log_1.gLog.info("prepare post data:" + JSON.stringify(config.data));
104
131
  }
105
- return new Promise((resolve, reject) => {
106
- request.post(options, (error, response, body) => {
107
- let originbody = body;
108
- if (error) {
109
- Log_1.gLog.error("post:" + options.url);
110
- Log_1.gLog.error(error);
111
- }
132
+ return (0, axios_1.default)(config).then(resp => {
133
+ let originbody = resp.data;
134
+ let body = originbody;
135
+ if (Core_1.core.isString(originbody)) {
112
136
  try {
113
- if (Core_1.core.isString(body)) {
114
- body = JSON.parse(body);
115
- }
137
+ body = JSON.parse(originbody);
116
138
  }
117
139
  catch (e) {
118
140
  try {
119
- body = qs.parse(body);
141
+ body = qs.parse(originbody);
120
142
  }
121
- catch (e) {
143
+ catch (e2) {
122
144
  body = originbody;
123
145
  }
124
146
  }
125
- if (this._debug) {
126
- Log_1.gLog.info({
127
- dttime: (Date.now() - time) + "ms",
128
- url: options.url,
129
- originbody: originbody
130
- });
147
+ }
148
+ if (this._debug) {
149
+ Log_1.gLog.info({ dttime: (Date.now() - time) + "ms", url: config.url, originbody });
150
+ }
151
+ return { error: null, response: resp, body, originbody };
152
+ }).catch(err => {
153
+ const resp = err.response || null;
154
+ let originbody = resp ? resp.data : null;
155
+ let body = originbody;
156
+ if (Core_1.core.isString(originbody)) {
157
+ try {
158
+ body = JSON.parse(originbody);
159
+ }
160
+ catch (e) {
161
+ try {
162
+ body = qs.parse(originbody);
163
+ }
164
+ catch (e2) {
165
+ body = originbody;
166
+ }
131
167
  }
132
- resolve({ error, response, body, originbody });
133
- });
168
+ }
169
+ Log_1.gLog.error("post:" + config.url);
170
+ Log_1.gLog.error(err);
171
+ if (this._debug) {
172
+ Log_1.gLog.info({ dttime: (Date.now() - time) + "ms", url: config.url, originbody });
173
+ }
174
+ return { error: err, response: resp, body, originbody };
134
175
  });
135
176
  }
136
177
  }
@@ -207,12 +207,12 @@ class AppleTool {
207
207
  reqb.password = password;
208
208
  reqb['exclude-old-transactions'] = exclude_old_transactions;
209
209
  //先验证生产环境
210
- var resb = (await HttpTool_1.gHttpTool.post({ url, form: JSON.stringify(reqb) })).body;
210
+ var resb = (await HttpTool_1.gHttpTool.post({ url, data: reqb })).body;
211
211
  Log_1.gLog.info("production end onVerify_Res============================status=" + (resb ? resb.status : "null"));
212
212
  //状态21007表示是沙盒环境
213
213
  if (resb && resb.status == 21007) {
214
214
  url = this._sandboxVerifyUrl;
215
- resb = (await HttpTool_1.gHttpTool.post({ url, form: JSON.stringify(reqb) })).body;
215
+ resb = (await HttpTool_1.gHttpTool.post({ url, data: reqb })).body;
216
216
  Log_1.gLog.info("sandbox end onVerify_Res============================status=" + (resb ? resb.status : "null"));
217
217
  }
218
218
  Log_1.gLog.info(resb);
@@ -46,7 +46,7 @@ class CgRankTool {
46
46
  timeout: timeout,
47
47
  password: this._password
48
48
  };
49
- await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
49
+ await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
50
50
  return;
51
51
  }
52
52
  async getTimeout(key) {
@@ -55,7 +55,7 @@ class CgRankTool {
55
55
  key: key,
56
56
  password: this._password
57
57
  };
58
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
58
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
59
59
  return rs.body;
60
60
  }
61
61
  /**
@@ -69,7 +69,7 @@ class CgRankTool {
69
69
  key: key,
70
70
  password: this._password
71
71
  };
72
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
72
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
73
73
  return rs.body;
74
74
  }
75
75
  async saveAllRank() {
@@ -77,7 +77,7 @@ class CgRankTool {
77
77
  cmd: "saveAllRank",
78
78
  password: this._password
79
79
  };
80
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
80
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
81
81
  return rs.body;
82
82
  }
83
83
  async getRankItem(key, id) {
@@ -87,7 +87,7 @@ class CgRankTool {
87
87
  id: id,
88
88
  password: this._password
89
89
  };
90
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
90
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
91
91
  return rs.body;
92
92
  }
93
93
  async getRankItems(key, ids) {
@@ -97,7 +97,7 @@ class CgRankTool {
97
97
  ids: ids,
98
98
  password: this._password
99
99
  };
100
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
100
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
101
101
  return rs.body;
102
102
  }
103
103
  /**
@@ -114,7 +114,7 @@ class CgRankTool {
114
114
  count: count,
115
115
  password: this._password
116
116
  };
117
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
117
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
118
118
  return rs.body;
119
119
  }
120
120
  async getRankCount(key) {
@@ -123,7 +123,7 @@ class CgRankTool {
123
123
  key: key,
124
124
  password: this._password
125
125
  };
126
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
126
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
127
127
  return rs.body;
128
128
  }
129
129
  async getRevRankList(key, start, count) {
@@ -134,7 +134,7 @@ class CgRankTool {
134
134
  count: count,
135
135
  password: this._password
136
136
  };
137
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
137
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
138
138
  return rs.body;
139
139
  }
140
140
  async addToRank(key, id, score, other, isreplace = false) {
@@ -147,7 +147,7 @@ class CgRankTool {
147
147
  isreplace: isreplace,
148
148
  password: this._password
149
149
  };
150
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
150
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
151
151
  return rs.body;
152
152
  }
153
153
  async addsToRank(key, datas, isreplace = false) {
@@ -158,7 +158,7 @@ class CgRankTool {
158
158
  isreplace: isreplace,
159
159
  password: this._password
160
160
  };
161
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
161
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
162
162
  return rs.body;
163
163
  }
164
164
  async removeFromRank(key, id) {
@@ -168,7 +168,7 @@ class CgRankTool {
168
168
  id: id,
169
169
  password: this._password
170
170
  };
171
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
171
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
172
172
  return rs.body;
173
173
  }
174
174
  async updateInRank(key, command) {
@@ -178,7 +178,7 @@ class CgRankTool {
178
178
  command: command,
179
179
  password: this._password
180
180
  };
181
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
181
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
182
182
  return rs.body;
183
183
  }
184
184
  async updatesInRank(key, commands) {
@@ -188,7 +188,7 @@ class CgRankTool {
188
188
  commands: commands,
189
189
  password: this._password
190
190
  };
191
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
191
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
192
192
  return rs.body;
193
193
  }
194
194
  async executeCommand(key, commands) {
@@ -198,7 +198,7 @@ class CgRankTool {
198
198
  commands: commands,
199
199
  password: this._password
200
200
  };
201
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
201
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
202
202
  return rs.body;
203
203
  }
204
204
  async anyCall(call, ...args) {
@@ -207,7 +207,7 @@ class CgRankTool {
207
207
  args: args,
208
208
  password: this._password
209
209
  };
210
- let rs = await HttpTool_1.gHttpTool.post({ url: this._url, json: msg });
210
+ let rs = await HttpTool_1.gHttpTool.post({ url: this._url, data: msg });
211
211
  return rs.body;
212
212
  }
213
213
  }
@@ -115,7 +115,7 @@ class QQTool {
115
115
  //必须 成功授权后的回调地址,必须是注册appid时填写的主域名下的地址,建议设置为网站首页或网站的用户中心
116
116
  let redirect_uri = URLEncode.encode(IServerConfig_1.gServerCfg.qq.redirect_uri);
117
117
  let url = "https://graph.qq.com/oauth2.0/token?code=" + auth_code + "&grant_type=" + grant_type + "&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri;
118
- let rs = await HttpTool_1.gHttpTool.get(url);
118
+ let rs = await HttpTool_1.gHttpTool.get({ url });
119
119
  if (rs.body && rs.body.access_token) {
120
120
  return rs.body.access_token;
121
121
  }
@@ -126,8 +126,8 @@ class QQTool {
126
126
  }
127
127
  async getOpenId(access_token) {
128
128
  let url = "https://graph.qq.com/oauth2.0/me?access_token=" + access_token;
129
- let rs = await HttpTool_1.gHttpTool.get(url);
130
- let body = rs.response ? rs.response.body : null;
129
+ let rs = await HttpTool_1.gHttpTool.get({ url });
130
+ let body = rs.response ? rs.response.data : null;
131
131
  if (body) {
132
132
  body = body.replace("callback( ", "");
133
133
  body = body.replace(" );\n", "");
@@ -136,7 +136,7 @@ class QQTool {
136
136
  }
137
137
  catch (e) { }
138
138
  if (!body.openid) {
139
- Log_1.gLog.error(rs.response.body);
139
+ Log_1.gLog.error(rs.response.data);
140
140
  }
141
141
  return body.openid;
142
142
  }
@@ -151,7 +151,7 @@ class QQTool {
151
151
  return null;
152
152
  }
153
153
  let url = "https://graph.qq.com/user/get_user_info?access_token=" + access_token + "&oauth_consumer_key=" + IServerConfig_1.gServerCfg.qq.app_id + "&openid=" + openid;
154
- let rs = await HttpTool_1.gHttpTool.get(url);
154
+ let rs = await HttpTool_1.gHttpTool.get({ url });
155
155
  if (rs.body) {
156
156
  return rs.body;
157
157
  }
@@ -89,7 +89,7 @@ class WechatTool {
89
89
  return null;
90
90
  }
91
91
  let url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + IServerConfig_1.gServerCfg.wechat.app_id + "&secret=" + IServerConfig_1.gServerCfg.wechat.app_key + "&code=" + auth_code + "&grant_type=authorization_code";
92
- let rs = await HttpTool_1.gHttpTool.get(url);
92
+ let rs = await HttpTool_1.gHttpTool.get({ url });
93
93
  /*
94
94
  {
95
95
  "access_token":"ACCESS_TOKEN",
@@ -111,7 +111,7 @@ class WechatTool {
111
111
  }
112
112
  async getUserInfo(access_token, openid) {
113
113
  let url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid;
114
- let rs = await HttpTool_1.gHttpTool.get(url);
114
+ let rs = await HttpTool_1.gHttpTool.get({ url });
115
115
  if (rs.body) {
116
116
  return rs.body;
117
117
  }
@@ -1,17 +1,22 @@
1
- import * as request from "request";
1
+ import { AxiosRequestConfig, AxiosResponse } from "axios";
2
2
  export declare class HttpTool {
3
3
  protected _debug: boolean;
4
4
  get debug(): boolean;
5
5
  set debug(value: boolean);
6
- get(options_url: request.OptionsWithUrl | string): Promise<{
6
+ get(config: AxiosRequestConfig & {
7
+ form?: any;
8
+ }): Promise<{
7
9
  error: any;
8
- response: any;
10
+ response: AxiosResponse | null;
9
11
  body: any;
10
12
  originbody: any;
11
13
  }>;
12
- post(options_url: request.OptionsWithUrl | string): Promise<{
14
+ post(config: AxiosRequestConfig & {
15
+ formData?: any;
16
+ form?: any;
17
+ }): Promise<{
13
18
  error: any;
14
- response: any;
19
+ response: AxiosResponse | null;
15
20
  body: any;
16
21
  originbody: any;
17
22
  }>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cgserver",
3
- "version": "14.0.0",
3
+ "version": "14.0.1",
4
4
  "author": "trojan",
5
5
  "type": "commonjs",
6
6
  "description": "free for all.Websocket or Http",
@@ -41,9 +41,9 @@
41
41
  "@types/mssql": "^9.1.8",
42
42
  "@types/node": "^24.10.1",
43
43
  "@types/nodemailer": "^7.0.4",
44
- "@types/request": "^2.48.13",
45
44
  "@types/websocket": "^1.0.10",
46
45
  "alipay-sdk": "^4.14.0",
46
+ "axios": "^1.13.2",
47
47
  "azgaar-fmg-parser": "^1.2.2",
48
48
  "colors": "^1.4.0",
49
49
  "cookie-parser": "^1.4.7",