node-easywechat 3.0.0-alpha.1 → 3.0.0-beta.2

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.
Files changed (60) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +156 -19
  3. package/dist/Core/Config.d.ts +1 -1
  4. package/dist/Core/Config.js +2 -2
  5. package/dist/Core/Contracts/AccessTokenInterface.d.ts +1 -1
  6. package/dist/Core/Contracts/CacheInterface.d.ts +3 -0
  7. package/dist/Core/Contracts/CacheInterface.js +3 -0
  8. package/dist/Core/Contracts/ServerInterface.d.ts +6 -0
  9. package/dist/Core/Contracts/ServerInterface.js +12 -0
  10. package/dist/Core/Encryptor.d.ts +3 -3
  11. package/dist/Core/Encryptor.js +6 -7
  12. package/dist/Core/Http/ServerRequest.d.ts +3 -0
  13. package/dist/Core/Http/ServerRequest.js +3 -0
  14. package/dist/Core/HttpClient/AccessTokenAwareClient.d.ts +5 -3
  15. package/dist/Core/HttpClient/AccessTokenAwareClient.js +9 -3
  16. package/dist/Core/HttpClient/Contracts/HttpClientInterface.d.ts +5 -0
  17. package/dist/Core/HttpClient/Contracts/HttpClientInterface.js +4 -0
  18. package/dist/Core/HttpClient/HttpClient.d.ts +10 -1
  19. package/dist/Core/HttpClient/HttpClient.js +20 -3
  20. package/dist/Core/HttpClient/HttpClientResponse.d.ts +9 -3
  21. package/dist/Core/HttpClient/HttpClientResponse.js +41 -8
  22. package/dist/Core/HttpClient/Mixins/PresetMixin.d.ts +79 -0
  23. package/dist/Core/HttpClient/Mixins/PresetMixin.js +155 -0
  24. package/dist/Core/Message.d.ts +14 -0
  25. package/dist/Core/Message.js +14 -0
  26. package/dist/Core/Mixins/ClientMixin.d.ts +6 -1
  27. package/dist/Core/Mixins/ClientMixin.js +3 -0
  28. package/dist/Core/Mixins/ServerRequestMixin.js +3 -0
  29. package/dist/Core/Support/Utils.d.ts +6 -0
  30. package/dist/Core/Support/Utils.js +10 -1
  31. package/dist/MiniApp/AccessToken.d.ts +9 -0
  32. package/dist/MiniApp/AccessToken.js +18 -0
  33. package/dist/MiniApp/Account.d.ts +4 -0
  34. package/dist/MiniApp/Account.js +8 -0
  35. package/dist/MiniApp/Application.d.ts +62 -0
  36. package/dist/MiniApp/Application.js +126 -0
  37. package/dist/MiniApp/Contracts/AccountInterface.d.ts +23 -0
  38. package/dist/MiniApp/Contracts/AccountInterface.js +25 -0
  39. package/dist/MiniApp/Contracts/ApplicationInterface.d.ts +68 -0
  40. package/dist/MiniApp/Contracts/ApplicationInterface.js +60 -0
  41. package/dist/MiniApp/Decryptor.d.ts +4 -0
  42. package/dist/MiniApp/Decryptor.js +20 -0
  43. package/dist/MiniApp/Server.d.ts +4 -0
  44. package/dist/MiniApp/Server.js +9 -0
  45. package/dist/MiniApp/Utils.d.ts +22 -0
  46. package/dist/MiniApp/Utils.js +56 -0
  47. package/dist/OfficialAccount/AccessToken.js +10 -9
  48. package/dist/OfficialAccount/Account.js +0 -4
  49. package/dist/OfficialAccount/Application.d.ts +3 -0
  50. package/dist/OfficialAccount/Application.js +5 -2
  51. package/dist/OfficialAccount/Server.d.ts +1 -6
  52. package/dist/OfficialAccount/Server.js +3 -10
  53. package/dist/OfficialAccount/Utils.js +0 -1
  54. package/dist/Types/global.d.ts +45 -66
  55. package/dist/index.d.ts +14 -0
  56. package/dist/index.js +18 -0
  57. package/mini_app.access_token.mock-access_token-appid.cache +1 -0
  58. package/official_account.access_token.mock-access_token-appid.cache +1 -1
  59. package/official_account.jsapi_ticket.mock-ticket-appid.cache +1 -1
  60. package/package.json +1 -1
@@ -16,9 +16,6 @@ const merge_1 = __importDefault(require("merge"));
16
16
  const Utils_1 = require("../Support/Utils");
17
17
  class HttpClientResponse {
18
18
  constructor(response, failureJudge = null, throwError = true) {
19
- this.response = null;
20
- this.failureJudge = null;
21
- this.throwError = false;
22
19
  this.response = response;
23
20
  this.failureJudge = failureJudge;
24
21
  this.throwError = throwError;
@@ -47,7 +44,7 @@ class HttpClientResponse {
47
44
  * @returns
48
45
  */
49
46
  isFailed() {
50
- if (this.failureJudge) {
47
+ if (this.is('text') && this.failureJudge) {
51
48
  return this.failureJudge(this);
52
49
  }
53
50
  try {
@@ -76,9 +73,10 @@ class HttpClientResponse {
76
73
  if (!content) {
77
74
  throw new Error('Response body is empty.');
78
75
  }
79
- let contentType = this.getHeader('content-type') || '';
80
- if (contentType && (contentType.indexOf('text/xml') > -1 || contentType.indexOf('application/xml') > -1 || content.indexOf('<xml>') > -1)) {
81
- return (0, Utils_1.parseXml)(content);
76
+ if (typeof content === 'string') {
77
+ if (this.is('xml') && content.indexOf('<xml>') > -1) {
78
+ return (0, Utils_1.parseXml)(content);
79
+ }
82
80
  }
83
81
  return content;
84
82
  });
@@ -119,7 +117,42 @@ class HttpClientResponse {
119
117
  * @returns
120
118
  */
121
119
  toDataUrl() {
122
- return 'data:' + this.getHeader('content-type') + ';base64,' + this.response.data;
120
+ return 'data:' + this.getHeader('content-type') + ';base64,' + this.response.data.toString('base64');
121
+ }
122
+ /**
123
+ * 判断 content-type 是否指定类型
124
+ * @param type
125
+ * @returns
126
+ */
127
+ is(type) {
128
+ let contentType = this.getHeader('content-type');
129
+ let res = false;
130
+ switch (type.toLowerCase()) {
131
+ case 'json':
132
+ res = contentType.indexOf('/json') > -1;
133
+ break;
134
+ case 'xml':
135
+ res = contentType.indexOf('/xml') > -1;
136
+ break;
137
+ case 'html':
138
+ res = contentType.indexOf('/html') > -1;
139
+ break;
140
+ case 'image':
141
+ res = contentType.indexOf('image/') > -1;
142
+ break;
143
+ case 'audio':
144
+ res = contentType.indexOf('audio/') > -1;
145
+ break;
146
+ case 'video':
147
+ res = contentType.indexOf('video/') > -1;
148
+ break;
149
+ case 'text':
150
+ res = contentType.indexOf('text/') > -1
151
+ || contentType.indexOf('/json') > -1
152
+ || contentType.indexOf('/xml') > -1;
153
+ break;
154
+ }
155
+ return res;
123
156
  }
124
157
  /**
125
158
  * 判断header是否存在
@@ -0,0 +1,79 @@
1
+ import { AxiosRequestConfig, Method } from "axios";
2
+ declare class PresetMixin {
3
+ /**
4
+ * 存储预置参数
5
+ */
6
+ protected presets: Record<string, any>;
7
+ /**
8
+ * 存储预置headers数据
9
+ */
10
+ protected prependHeaders: Record<string, any>;
11
+ /**
12
+ * 存储预置数据
13
+ */
14
+ protected prependData: Record<string, any>;
15
+ /**
16
+ * 设置预置参数
17
+ * @param presets
18
+ * @returns
19
+ */
20
+ setPresets(presets: Record<string, any>): this;
21
+ /**
22
+ * 设置单个预置header
23
+ * @param key
24
+ * @param value
25
+ * @returns
26
+ */
27
+ withHeader(key: string, value: any): this;
28
+ /**
29
+ * 批量设置预置headers
30
+ * @param headers
31
+ * @returns
32
+ */
33
+ withHeaders(headers: Record<string, any>): this;
34
+ /**
35
+ * 设置预置数据
36
+ * @param key 参数名
37
+ * @param value 参数值,不设置则尝试从预置参数中获取
38
+ * @returns
39
+ */
40
+ with(key: string | string[] | Record<string, any>, value?: string): this;
41
+ /**
42
+ * 预设置app_id(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
43
+ * @param new_appid
44
+ * @returns
45
+ */
46
+ withAppId(new_appid?: string): this;
47
+ /**
48
+ * 预设置app_id的别名(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
49
+ * @param new_alias
50
+ * @returns
51
+ */
52
+ withAppIdAs(new_alias: string): this;
53
+ /**
54
+ * 预设置secret(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
55
+ * @param new_secret
56
+ * @returns
57
+ */
58
+ withSecret(new_secret?: string): this;
59
+ /**
60
+ * 预设置mch_id(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
61
+ * @param new_mch_id
62
+ * @returns
63
+ */
64
+ withMchId(new_mch_id?: string): this;
65
+ /**
66
+ * 预设置mch_id别名(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
67
+ * @param new_alias
68
+ * @returns
69
+ */
70
+ withMchIdAs(new_alias?: string): this;
71
+ /**
72
+ * 合并预置参数并清空预置数据
73
+ * @param payload
74
+ * @param method
75
+ * @returns
76
+ */
77
+ mergeThenResetPrepends(payload: AxiosRequestConfig, method?: Method): any;
78
+ }
79
+ export = PresetMixin;
@@ -0,0 +1,155 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const merge_1 = __importDefault(require("merge"));
6
+ class PresetMixin {
7
+ constructor() {
8
+ /**
9
+ * 存储预置参数
10
+ */
11
+ this.presets = {};
12
+ /**
13
+ * 存储预置headers数据
14
+ */
15
+ this.prependHeaders = {};
16
+ /**
17
+ * 存储预置数据
18
+ */
19
+ this.prependData = {};
20
+ }
21
+ /**
22
+ * 设置预置参数
23
+ * @param presets
24
+ * @returns
25
+ */
26
+ setPresets(presets) {
27
+ this.presets = presets;
28
+ return this;
29
+ }
30
+ /**
31
+ * 设置单个预置header
32
+ * @param key
33
+ * @param value
34
+ * @returns
35
+ */
36
+ withHeader(key, value) {
37
+ if (typeof this.prependHeaders !== 'object') {
38
+ this.prependHeaders = {};
39
+ }
40
+ this.prependHeaders[key] = value;
41
+ return this;
42
+ }
43
+ /**
44
+ * 批量设置预置headers
45
+ * @param headers
46
+ * @returns
47
+ */
48
+ withHeaders(headers) {
49
+ headers.map((value, key) => {
50
+ this.withHeader(key, value);
51
+ });
52
+ return this;
53
+ }
54
+ /**
55
+ * 设置预置数据
56
+ * @param key 参数名
57
+ * @param value 参数值,不设置则尝试从预置参数中获取
58
+ * @returns
59
+ */
60
+ with(key, value = null) {
61
+ if (Array.isArray(key)) {
62
+ key.map((k) => {
63
+ var _a;
64
+ this.with(k, (_a = this.presets[k]) !== null && _a !== void 0 ? _a : null);
65
+ });
66
+ return this;
67
+ }
68
+ else if (typeof key === 'object') {
69
+ key.map((v, k) => {
70
+ var _a;
71
+ this.with(k, (_a = v !== null && v !== void 0 ? v : this.presets[k]) !== null && _a !== void 0 ? _a : null);
72
+ });
73
+ return this;
74
+ }
75
+ if (typeof this.prependData !== 'object') {
76
+ this.prependData = {};
77
+ }
78
+ this.prependData[key] = value || this.presets[key] || null;
79
+ return this;
80
+ }
81
+ /**
82
+ * 预设置app_id(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
83
+ * @param new_appid
84
+ * @returns
85
+ */
86
+ withAppId(new_appid = null) {
87
+ this.with('app_id', new_appid);
88
+ return this;
89
+ }
90
+ /**
91
+ * 预设置app_id的别名(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
92
+ * @param new_alias
93
+ * @returns
94
+ */
95
+ withAppIdAs(new_alias) {
96
+ this.with(new_alias, this.presets['app_id']);
97
+ return this;
98
+ }
99
+ /**
100
+ * 预设置secret(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
101
+ * @param new_secret
102
+ * @returns
103
+ */
104
+ withSecret(new_secret = null) {
105
+ this.with('secret', new_secret);
106
+ return this;
107
+ }
108
+ /**
109
+ * 预设置mch_id(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
110
+ * @param new_mch_id
111
+ * @returns
112
+ */
113
+ withMchId(new_mch_id = null) {
114
+ this.with('mch_id', new_mch_id);
115
+ return this;
116
+ }
117
+ /**
118
+ * 预设置mch_id别名(因nodejs不支持魔术方法,只好预先设置几个常用的方法)
119
+ * @param new_alias
120
+ * @returns
121
+ */
122
+ withMchIdAs(new_alias = null) {
123
+ this.with(new_alias, this.presets['mch_id']);
124
+ return this;
125
+ }
126
+ /**
127
+ * 合并预置参数并清空预置数据
128
+ * @param payload
129
+ * @param method
130
+ * @returns
131
+ */
132
+ mergeThenResetPrepends(payload, method = 'get') {
133
+ var _a, _b, _c, _d;
134
+ let field = method.toLowerCase() === 'get' ? 'params' : 'data';
135
+ let options = merge_1.default.recursive(true, payload);
136
+ if (!options.headers)
137
+ options.headers = {};
138
+ if (((_b = (_a = options.headers['Content-Type']) !== null && _a !== void 0 ? _a : options.headers['content-type']) !== null && _b !== void 0 ? _b : null) === 'application/json' || !!options.json) {
139
+ field = 'json';
140
+ }
141
+ if (((_d = (_c = options.headers['Content-Type']) !== null && _c !== void 0 ? _c : options.headers['content-type']) !== null && _d !== void 0 ? _d : null) === 'text/xml' || !!options.xml) {
142
+ field = 'xml';
143
+ }
144
+ if (this.prependData && Object.keys(this.prependData).length > 0) {
145
+ options[field] = Object.assign(Object.assign({}, this.prependData), options[field]);
146
+ }
147
+ if (this.prependHeaders && Object.keys(this.prependHeaders).length > 0) {
148
+ options.headers = Object.assign(Object.assign({}, this.prependHeaders), options.headers);
149
+ }
150
+ this.prependData = {};
151
+ this.prependHeaders = {};
152
+ return options;
153
+ }
154
+ }
155
+ module.exports = PresetMixin;
@@ -1,6 +1,12 @@
1
1
  import ServerRequestInterface from "./Http/Contracts/ServerRequestInterface";
2
2
  import HasAttributesMixin from "./Mixins/HasAttributesMixin";
3
+ /**
4
+ * 消息对象
5
+ */
3
6
  declare class Message {
7
+ /**
8
+ * 原始消息内容
9
+ */
4
10
  protected originContent: string;
5
11
  constructor(attributes: Record<string, any>, originContent?: string);
6
12
  /**
@@ -9,7 +15,15 @@ declare class Message {
9
15
  * @returns
10
16
  */
11
17
  static createFromRequest(request: ServerRequestInterface): Promise<Message>;
18
+ /**
19
+ * 获取原始消息内容
20
+ * @returns
21
+ */
12
22
  getOriginalContents(): string;
23
+ /**
24
+ * 转为字符串
25
+ * @returns
26
+ */
13
27
  toString(): string;
14
28
  }
15
29
  interface Message extends HasAttributesMixin {
@@ -13,8 +13,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  const HasAttributesMixin_1 = __importDefault(require("./Mixins/HasAttributesMixin"));
15
15
  const Utils_1 = require("./Support/Utils");
16
+ /**
17
+ * 消息对象
18
+ */
16
19
  class Message {
17
20
  constructor(attributes, originContent = '') {
21
+ /**
22
+ * 原始消息内容
23
+ */
18
24
  this.originContent = '';
19
25
  this.attributes = attributes;
20
26
  this.originContent = originContent;
@@ -61,9 +67,17 @@ class Message {
61
67
  return new Message(attributes, originContent);
62
68
  });
63
69
  }
70
+ /**
71
+ * 获取原始消息内容
72
+ * @returns
73
+ */
64
74
  getOriginalContents() {
65
75
  return this.originContent;
66
76
  }
77
+ /**
78
+ * 转为字符串
79
+ * @returns
80
+ */
67
81
  toString() {
68
82
  return this.toJson();
69
83
  }
@@ -1,6 +1,11 @@
1
1
  import AccessTokenAwareClient from "../HttpClient/AccessTokenAwareClient";
2
- declare class ClientMixin {
2
+ declare abstract class ClientMixin {
3
3
  protected client: AccessTokenAwareClient;
4
+ /**
5
+ * 创建客户端实例
6
+ * @returns
7
+ */
8
+ abstract createClient(): AccessTokenAwareClient;
4
9
  /**
5
10
  * 获取客户端实例
6
11
  * @returns
@@ -5,6 +5,9 @@ class ClientMixin {
5
5
  * @returns
6
6
  */
7
7
  getClient() {
8
+ if (!this.client) {
9
+ this.client = this.createClient();
10
+ }
8
11
  return this.client;
9
12
  }
10
13
  /**
@@ -5,6 +5,9 @@ class ServerRequestMixin {
5
5
  * @returns
6
6
  */
7
7
  getRequest() {
8
+ if (!this.request) {
9
+ throw new Error('Please set request instance before use.');
10
+ }
8
11
  return this.request;
9
12
  }
10
13
  /**
@@ -68,6 +68,12 @@ export declare const strStudly: (value: string) => string;
68
68
  * @returns
69
69
  */
70
70
  export declare const strCamel: (value: string) => string;
71
+ /**
72
+ * 蛇形(下划线分隔,全小写),'helloWorld' => 'hello_world'
73
+ * @param value
74
+ * @returns
75
+ */
76
+ export declare const strSnake: (value: string) => string;
71
77
  /**
72
78
  * 如果只有一个同名、同级节点,则不当作数组
73
79
  * @param obj
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.buildXml = exports.parseXml = exports.singleItem = exports.strCamel = exports.strStudly = exports.strLcwords = exports.strUcwords = exports.rtrim = exports.ltrim = exports.trim = exports.applyMixins = exports.inArray = exports.isIp = exports.isIpv6 = exports.isIpv4 = exports.isFunction = exports.isObject = exports.isNumber = exports.isArray = exports.isString = exports.makeSignature = exports.randomString = exports.parseQueryString = exports.buildQueryString = exports.getTimestamp = exports.md5File = exports.createHmac = exports.createHash = void 0;
15
+ exports.buildXml = exports.parseXml = exports.singleItem = exports.strSnake = exports.strCamel = exports.strStudly = exports.strLcwords = exports.strUcwords = exports.rtrim = exports.ltrim = exports.trim = exports.applyMixins = exports.inArray = exports.isIp = exports.isIpv6 = exports.isIpv4 = exports.isFunction = exports.isObject = exports.isNumber = exports.isArray = exports.isString = exports.makeSignature = exports.randomString = exports.parseQueryString = exports.buildQueryString = exports.getTimestamp = exports.md5File = exports.createHmac = exports.createHash = void 0;
16
16
  const crypto_1 = __importDefault(require("crypto"));
17
17
  const qs_1 = __importDefault(require("qs"));
18
18
  const xml2js_1 = __importDefault(require("xml2js"));
@@ -252,6 +252,15 @@ const strCamel = function (value) {
252
252
  return (0, exports.strLcwords)((0, exports.strStudly)(value));
253
253
  };
254
254
  exports.strCamel = strCamel;
255
+ /**
256
+ * 蛇形(下划线分隔,全小写),'helloWorld' => 'hello_world'
257
+ * @param value
258
+ * @returns
259
+ */
260
+ const strSnake = function (value) {
261
+ return value.replace(/([A-Z])/g, "_$1").toLowerCase().substring(1);
262
+ };
263
+ exports.strSnake = strSnake;
255
264
  /**
256
265
  * 如果只有一个同名、同级节点,则不当作数组
257
266
  * @param obj
@@ -0,0 +1,9 @@
1
+ import BaseAccessToken from "../OfficialAccount/AccessToken";
2
+ declare class AccessToken extends BaseAccessToken {
3
+ /**
4
+ * 获取access_token的缓存名称
5
+ * @returns
6
+ */
7
+ getKey(): string;
8
+ }
9
+ export = AccessToken;
@@ -0,0 +1,18 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const AccessToken_1 = __importDefault(require("../OfficialAccount/AccessToken"));
6
+ class AccessToken extends AccessToken_1.default {
7
+ /**
8
+ * 获取access_token的缓存名称
9
+ * @returns
10
+ */
11
+ getKey() {
12
+ if (!this.key) {
13
+ this.key = `mini_app.access_token.${this.appId}`;
14
+ }
15
+ return this.key;
16
+ }
17
+ }
18
+ module.exports = AccessToken;
@@ -0,0 +1,4 @@
1
+ import BaseAccount from "../OfficialAccount/Account";
2
+ declare class Account extends BaseAccount {
3
+ }
4
+ export = Account;
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const Account_1 = __importDefault(require("../OfficialAccount/Account"));
6
+ class Account extends Account_1.default {
7
+ }
8
+ module.exports = Account;
@@ -0,0 +1,62 @@
1
+ import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
2
+ import ConfigInterface from '../Core/Contracts/ConfigInterface';
3
+ import ServerInterface from '../Core/Contracts/ServerInterface';
4
+ import Encryptor from '../Core/Encryptor';
5
+ import AccessTokenAwareClient from '../Core/HttpClient/AccessTokenAwareClient';
6
+ import CacheMixin from '../Core/Mixins/CacheMixin';
7
+ import ClientMixin from '../Core/Mixins/ClientMixin';
8
+ import ConfigMixin from '../Core/Mixins/ConfigMixin';
9
+ import HttpClientMixin from '../Core/Mixins/HttpClientMixin';
10
+ import ServerRequestMixin from '../Core/Mixins/ServerRequestMixin';
11
+ import { MiniAppConfig } from '../Types/global';
12
+ import AccountInterface from './Contracts/AccountInterface';
13
+ import ApplicationInterface from './Contracts/ApplicationInterface';
14
+ import Utils from './Utils';
15
+ /**
16
+ * 小程序应用
17
+ */
18
+ declare class Application implements ApplicationInterface {
19
+ constructor(config: ConfigInterface | MiniAppConfig);
20
+ protected account: AccountInterface;
21
+ protected encryptor: Encryptor;
22
+ protected server: ServerInterface;
23
+ protected accessToken: AccessTokenInterface;
24
+ getAccount(): AccountInterface;
25
+ /**
26
+ * 设置当前账户实例
27
+ * @param account
28
+ * @returns
29
+ */
30
+ setAccount(account: AccountInterface): this;
31
+ getEncryptor(): Encryptor;
32
+ /**
33
+ * 设置加密机实例
34
+ * @param encryptor
35
+ * @returns
36
+ */
37
+ setEncryptor(encryptor: Encryptor): this;
38
+ getServer(): ServerInterface;
39
+ /**
40
+ * 设置服务端实例
41
+ * @param server
42
+ * @returns
43
+ */
44
+ setServer(server: ServerInterface): this;
45
+ getAccessToken(): AccessTokenInterface;
46
+ /**
47
+ * 设置AccessToken实例
48
+ * @param accessToken
49
+ * @returns
50
+ */
51
+ setAccessToken(accessToken: AccessTokenInterface): this;
52
+ getUtils(): Utils;
53
+ createClient(): AccessTokenAwareClient;
54
+ /**
55
+ * 获取请求默认配置
56
+ * @returns
57
+ */
58
+ protected getHttpClientDefaultOptions(): Record<string, any>;
59
+ }
60
+ interface Application extends ConfigMixin, CacheMixin, ClientMixin, ServerRequestMixin, HttpClientMixin {
61
+ }
62
+ export = Application;
@@ -0,0 +1,126 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const merge_1 = __importDefault(require("merge"));
6
+ const ConfigInterface_1 = __importDefault(require("../Core/Contracts/ConfigInterface"));
7
+ const Encryptor_1 = __importDefault(require("../Core/Encryptor"));
8
+ const AccessTokenAwareClient_1 = __importDefault(require("../Core/HttpClient/AccessTokenAwareClient"));
9
+ const CacheMixin_1 = __importDefault(require("../Core/Mixins/CacheMixin"));
10
+ const ClientMixin_1 = __importDefault(require("../Core/Mixins/ClientMixin"));
11
+ const ConfigMixin_1 = __importDefault(require("../Core/Mixins/ConfigMixin"));
12
+ const HttpClientMixin_1 = __importDefault(require("../Core/Mixins/HttpClientMixin"));
13
+ const ServerRequestMixin_1 = __importDefault(require("../Core/Mixins/ServerRequestMixin"));
14
+ const Utils_1 = require("../Core/Support/Utils");
15
+ const AccessToken_1 = __importDefault(require("./AccessToken"));
16
+ const Account_1 = __importDefault(require("./Account"));
17
+ const Server_1 = __importDefault(require("./Server"));
18
+ const Utils_2 = __importDefault(require("./Utils"));
19
+ const Config_1 = __importDefault(require("../OfficialAccount/Config"));
20
+ /**
21
+ * 小程序应用
22
+ */
23
+ class Application {
24
+ constructor(config) {
25
+ this.account = null;
26
+ this.encryptor = null;
27
+ this.server = null;
28
+ this.accessToken = null;
29
+ if (config instanceof ConfigInterface_1.default) {
30
+ this.setConfig(config);
31
+ }
32
+ else {
33
+ this.setConfig(new Config_1.default(config));
34
+ }
35
+ }
36
+ getAccount() {
37
+ if (!this.account) {
38
+ this.account = new Account_1.default(this.config.get('app_id'), this.config.get('secret'), this.config.get('token'), this.config.get('aes_key'));
39
+ }
40
+ return this.account;
41
+ }
42
+ /**
43
+ * 设置当前账户实例
44
+ * @param account
45
+ * @returns
46
+ */
47
+ setAccount(account) {
48
+ this.account = account;
49
+ return this;
50
+ }
51
+ getEncryptor() {
52
+ if (!this.encryptor) {
53
+ let token = this.getAccount().getToken();
54
+ let aesKey = this.getAccount().getAesKey();
55
+ if (!token || !aesKey) {
56
+ throw new Error('token or aes_key cannot be empty.');
57
+ }
58
+ this.encryptor = new Encryptor_1.default(this.getAccount().getAppId(), token, aesKey, this.getAccount().getAppId());
59
+ }
60
+ return this.encryptor;
61
+ }
62
+ /**
63
+ * 设置加密机实例
64
+ * @param encryptor
65
+ * @returns
66
+ */
67
+ setEncryptor(encryptor) {
68
+ this.encryptor = encryptor;
69
+ return this;
70
+ }
71
+ getServer() {
72
+ if (!this.server) {
73
+ let token = this.getAccount().getToken();
74
+ let aesKey = this.getAccount().getAesKey();
75
+ if (!token || !aesKey) {
76
+ throw new Error('token or aes_key cannot be empty.');
77
+ }
78
+ this.server = new Server_1.default(this.getRequest(), this.getAccount().getAesKey() ? this.getEncryptor() : null);
79
+ }
80
+ return this.server;
81
+ }
82
+ /**
83
+ * 设置服务端实例
84
+ * @param server
85
+ * @returns
86
+ */
87
+ setServer(server) {
88
+ this.server = server;
89
+ return this;
90
+ }
91
+ getAccessToken() {
92
+ if (!this.accessToken) {
93
+ this.accessToken = new AccessToken_1.default(this.getAccount().getAppId(), this.getAccount().getSecret(), null, this.getCache(), this.getHttpClient());
94
+ }
95
+ return this.accessToken;
96
+ }
97
+ /**
98
+ * 设置AccessToken实例
99
+ * @param accessToken
100
+ * @returns
101
+ */
102
+ setAccessToken(accessToken) {
103
+ this.accessToken = accessToken;
104
+ return this;
105
+ }
106
+ getUtils() {
107
+ return new Utils_2.default(this);
108
+ }
109
+ createClient() {
110
+ return (new AccessTokenAwareClient_1.default(this.getHttpClient(), this.getAccessToken()))
111
+ .setPresets(this.getConfig().all());
112
+ }
113
+ /**
114
+ * 获取请求默认配置
115
+ * @returns
116
+ */
117
+ getHttpClientDefaultOptions() {
118
+ return (0, merge_1.default)(true, {
119
+ baseURL: 'https://api.weixin.qq.com/',
120
+ }, this.getConfig().get('http'));
121
+ }
122
+ }
123
+ ;
124
+ ;
125
+ (0, Utils_1.applyMixins)(Application, [ConfigMixin_1.default, CacheMixin_1.default, ClientMixin_1.default, ServerRequestMixin_1.default, HttpClientMixin_1.default]);
126
+ module.exports = Application;