node-easywechat 3.0.0-beta.5 → 3.0.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.
Files changed (55) hide show
  1. package/CHANGELOG.md +21 -0
  2. package/README.md +36 -1
  3. package/dist/Core/Contracts/RefreshableAccessTokenInterface.d.ts +2 -2
  4. package/dist/Core/Contracts/RefreshableAccessTokenInterface.js +2 -2
  5. package/dist/Core/HttpClient/HttpClient.js +3 -1
  6. package/dist/Core/HttpClient/HttpClientResponse.d.ts +16 -5
  7. package/dist/Core/HttpClient/HttpClientResponse.js +64 -22
  8. package/dist/Core/Message.d.ts +0 -127
  9. package/dist/Core/Mixins/DecryptXmlMessageMixin.js +1 -1
  10. package/dist/Core/Mixins/ResponseXmlMessageMixin.js +2 -2
  11. package/dist/Core/Support/PrivateKey.d.ts +20 -0
  12. package/dist/Core/Support/PrivateKey.js +38 -0
  13. package/dist/Core/Support/PublicKey.d.ts +14 -0
  14. package/dist/Core/Support/PublicKey.js +36 -0
  15. package/dist/Core/Support/RSA.d.ts +2 -2
  16. package/dist/Core/Support/RSA.js +4 -4
  17. package/dist/Core/Support/Utils.d.ts +6 -0
  18. package/dist/Core/Support/Utils.js +15 -1
  19. package/dist/MiniApp/Application.d.ts +4 -4
  20. package/dist/MiniApp/Contracts/ApplicationInterface.d.ts +2 -2
  21. package/dist/OfficialAccount/AccessToken.d.ts +2 -2
  22. package/dist/OfficialAccount/AccessToken.js +2 -2
  23. package/dist/OfficialAccount/Application.d.ts +4 -4
  24. package/dist/OfficialAccount/Contracts/ApplicationInterface.d.ts +2 -2
  25. package/dist/OfficialAccount/JsApiTicket.js +2 -2
  26. package/dist/OfficialAccount/Message.d.ts +139 -0
  27. package/dist/OfficialAccount/Message.js +1 -0
  28. package/dist/Pay/Application.d.ts +52 -0
  29. package/dist/Pay/Application.js +96 -0
  30. package/dist/Pay/Client.d.ts +42 -0
  31. package/dist/Pay/Client.js +125 -0
  32. package/dist/Pay/Config.d.ts +5 -0
  33. package/dist/Pay/Config.js +17 -0
  34. package/dist/Pay/Contracts/ApplicationInterface.d.ts +50 -0
  35. package/dist/Pay/Contracts/ApplicationInterface.js +45 -0
  36. package/dist/Pay/Contracts/MerchantInterface.d.ts +35 -0
  37. package/dist/Pay/Contracts/MerchantInterface.js +35 -0
  38. package/dist/Pay/Contracts/ResponseValidatorInterface.d.ts +9 -0
  39. package/dist/Pay/Contracts/ResponseValidatorInterface.js +10 -0
  40. package/dist/Pay/LegacySignature.d.ts +12 -0
  41. package/dist/Pay/LegacySignature.js +59 -0
  42. package/dist/Pay/Merchant.d.ts +25 -0
  43. package/dist/Pay/Merchant.js +53 -0
  44. package/dist/Pay/Message.d.ts +175 -0
  45. package/dist/Pay/Message.js +24 -0
  46. package/dist/Pay/Server.d.ts +41 -0
  47. package/dist/Pay/Server.js +122 -0
  48. package/dist/Pay/Signature.d.ts +15 -0
  49. package/dist/Pay/Signature.js +44 -0
  50. package/dist/Pay/Utils.d.ts +54 -0
  51. package/dist/Pay/Utils.js +142 -0
  52. package/dist/Types/global.d.ts +110 -1
  53. package/dist/index.d.ts +3 -3
  54. package/dist/index.js +3 -3
  55. package/package.json +1 -1
@@ -1,4 +1,3 @@
1
- import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
2
1
  import ConfigInterface from '../Core/Contracts/ConfigInterface';
3
2
  import ServerInterface from '../Core/Contracts/ServerInterface';
4
3
  import Encryptor from '../Core/Encryptor';
@@ -12,6 +11,7 @@ import { MiniAppConfig } from '../Types/global';
12
11
  import AccountInterface from './Contracts/AccountInterface';
13
12
  import ApplicationInterface from './Contracts/ApplicationInterface';
14
13
  import Utils from './Utils';
14
+ import RefreshableAccessTokenInterface from '../Core/Contracts/RefreshableAccessTokenInterface';
15
15
  /**
16
16
  * 小程序应用
17
17
  */
@@ -20,7 +20,7 @@ declare class Application implements ApplicationInterface {
20
20
  protected account: AccountInterface;
21
21
  protected encryptor: Encryptor;
22
22
  protected server: ServerInterface;
23
- protected accessToken: AccessTokenInterface;
23
+ protected accessToken: RefreshableAccessTokenInterface;
24
24
  getAccount(): AccountInterface;
25
25
  /**
26
26
  * 设置当前账户实例
@@ -42,13 +42,13 @@ declare class Application implements ApplicationInterface {
42
42
  * @returns
43
43
  */
44
44
  setServer(server: ServerInterface): this;
45
- getAccessToken(): AccessTokenInterface;
45
+ getAccessToken(): RefreshableAccessTokenInterface;
46
46
  /**
47
47
  * 设置AccessToken实例
48
48
  * @param accessToken
49
49
  * @returns
50
50
  */
51
- setAccessToken(accessToken: AccessTokenInterface): this;
51
+ setAccessToken(accessToken: RefreshableAccessTokenInterface): this;
52
52
  getUtils(): Utils;
53
53
  createClient(): AccessTokenAwareClient;
54
54
  /**
@@ -1,4 +1,3 @@
1
- import AccessTokenInterface from "../../Core/Contracts/AccessTokenInterface";
2
1
  import CacheInterface from "../../Core/Contracts/CacheInterface";
3
2
  import ConfigInterface from "../../Core/Contracts/ConfigInterface";
4
3
  import AccessTokenAwareClient from "../../Core/HttpClient/AccessTokenAwareClient";
@@ -8,6 +7,7 @@ import ServerRequestInterface from "../../Core/Http/Contracts/ServerRequestInter
8
7
  import Encryptor from "../../Core/Encryptor";
9
8
  import AccountInterface from "./AccountInterface";
10
9
  import Utils from "../Utils";
10
+ import RefreshableAccessTokenInterface from "../../Core/Contracts/RefreshableAccessTokenInterface";
11
11
  declare abstract class ApplicationInterface {
12
12
  /**
13
13
  * 获取当前账户实例
@@ -53,7 +53,7 @@ declare abstract class ApplicationInterface {
53
53
  * 获取AccessToken实例
54
54
  * @returns
55
55
  */
56
- getAccessToken(): AccessTokenInterface;
56
+ getAccessToken(): RefreshableAccessTokenInterface;
57
57
  /**
58
58
  * 获取缓存实例
59
59
  * @returns
@@ -1,7 +1,7 @@
1
1
  import CacheInterface from "../Core/Contracts/CacheInterface";
2
2
  import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
3
- import RefreshableAccessToken from "../Core/Contracts/RefreshableAccessTokenInterface";
4
- declare class AccessToken implements RefreshableAccessToken {
3
+ import RefreshableAccessTokenInterface from "../Core/Contracts/RefreshableAccessTokenInterface";
4
+ declare class AccessToken implements RefreshableAccessTokenInterface {
5
5
  protected appId: string;
6
6
  protected secret: string;
7
7
  protected key: string;
@@ -66,13 +66,13 @@ class AccessToken {
66
66
  }
67
67
  refresh() {
68
68
  return __awaiter(this, void 0, void 0, function* () {
69
- let response = yield (yield this.httpClient.request('get', 'cgi-bin/token', {
69
+ let response = (yield this.httpClient.request('get', 'cgi-bin/token', {
70
70
  params: {
71
71
  grant_type: 'client_credential',
72
72
  appid: this.appId,
73
73
  secret: this.secret,
74
74
  }
75
- })).toObject(false);
75
+ })).toObject();
76
76
  if (!response['access_token']) {
77
77
  throw new Error('Failed to get access_token: ' + JSON.stringify(response));
78
78
  }
@@ -1,5 +1,4 @@
1
1
  import ProviderInterface from 'node-socialite/dist/Core/ProviderInterface';
2
- import AccessTokenInterface from '../Core/Contracts/AccessTokenInterface';
3
2
  import ConfigInterface from '../Core/Contracts/ConfigInterface';
4
3
  import ServerInterface from '../Core/Contracts/ServerInterface';
5
4
  import Encryptor from '../Core/Encryptor';
@@ -14,6 +13,7 @@ import AccountInterface from './Contracts/AccountInterface';
14
13
  import ApplicationInterface from './Contracts/ApplicationInterface';
15
14
  import JsApiTicket from './JsApiTicket';
16
15
  import Utils from './Utils';
16
+ import RefreshableAccessTokenInterface from '../Core/Contracts/RefreshableAccessTokenInterface';
17
17
  /**
18
18
  * 公众号应用
19
19
  */
@@ -22,7 +22,7 @@ declare class Application implements ApplicationInterface {
22
22
  protected account: AccountInterface;
23
23
  protected encryptor: Encryptor;
24
24
  protected server: ServerInterface;
25
- protected accessToken: AccessTokenInterface;
25
+ protected accessToken: RefreshableAccessTokenInterface;
26
26
  protected oauthFactory: OfficialAccountOAuthFactory;
27
27
  protected ticket: JsApiTicket;
28
28
  getAccount(): AccountInterface;
@@ -46,13 +46,13 @@ declare class Application implements ApplicationInterface {
46
46
  * @returns
47
47
  */
48
48
  setServer(server: ServerInterface): this;
49
- getAccessToken(): AccessTokenInterface;
49
+ getAccessToken(): RefreshableAccessTokenInterface;
50
50
  /**
51
51
  * 设置AccessToken实例
52
52
  * @param accessToken
53
53
  * @returns
54
54
  */
55
- setAccessToken(accessToken: AccessTokenInterface): this;
55
+ setAccessToken(accessToken: RefreshableAccessTokenInterface): this;
56
56
  setOAuthFactory(oauthFactory: OfficialAccountOAuthFactory): this;
57
57
  getOAuth(): ProviderInterface;
58
58
  getTicket(): JsApiTicket;
@@ -1,5 +1,4 @@
1
1
  import ProviderInterface from "node-socialite/dist/Core/ProviderInterface";
2
- import AccessTokenInterface from "../../Core/Contracts/AccessTokenInterface";
3
2
  import CacheInterface from "../../Core/Contracts/CacheInterface";
4
3
  import ConfigInterface from "../../Core/Contracts/ConfigInterface";
5
4
  import AccessTokenAwareClient from "../../Core/HttpClient/AccessTokenAwareClient";
@@ -11,6 +10,7 @@ import AccountInterface from "./AccountInterface";
11
10
  import { OfficialAccountOAuthFactory } from "../../Types/global";
12
11
  import JsApiTicket from "../JsApiTicket";
13
12
  import Utils from "../Utils";
13
+ import RefreshableAccessTokenInterface from "../../Core/Contracts/RefreshableAccessTokenInterface";
14
14
  declare abstract class ApplicationInterface {
15
15
  /**
16
16
  * 获取当前账户实例
@@ -56,7 +56,7 @@ declare abstract class ApplicationInterface {
56
56
  * 获取AccessToken实例
57
57
  * @returns
58
58
  */
59
- getAccessToken(): AccessTokenInterface;
59
+ getAccessToken(): RefreshableAccessTokenInterface;
60
60
  /**
61
61
  * 获取缓存实例
62
62
  * @returns
@@ -35,11 +35,11 @@ class JsApiTicket extends AccessToken_1.default {
35
35
  if (!!ticket && typeof ticket === 'string') {
36
36
  return ticket;
37
37
  }
38
- let response = yield (yield this.httpClient.request('get', '/cgi-bin/ticket/getticket', {
38
+ let response = (yield this.httpClient.request('get', '/cgi-bin/ticket/getticket', {
39
39
  params: {
40
40
  type: 'jsapi',
41
41
  }
42
- })).toObject(false);
42
+ })).toObject();
43
43
  if (!response['ticket']) {
44
44
  throw new Error('Failed to get jssdk_ticket: ' + JSON.stringify(response));
45
45
  }
@@ -1,4 +1,143 @@
1
1
  import BaseMessage from '../Core/Message';
2
2
  declare class Message extends BaseMessage {
3
3
  }
4
+ interface Message {
5
+ /**
6
+ * 消息密文,兼容模式、安全模式才有
7
+ */
8
+ Encrypt?: string;
9
+ /**
10
+ * 开发者微信号
11
+ */
12
+ ToUserName?: string;
13
+ /**
14
+ * 发送方帐号 OpenId
15
+ */
16
+ FromUserName?: string;
17
+ /**
18
+ * 消息创建时间
19
+ */
20
+ CreateTime?: number;
21
+ /**
22
+ * 消息类型
23
+ * - `event` 事件
24
+ * - `text` 文本
25
+ * - `image` 图片
26
+ * - `voice` 语音
27
+ * - `video` 视频
28
+ * - `shortvideo` 小视频
29
+ * - `location` 地理位置
30
+ * - `link` 链接
31
+ */
32
+ MsgType?: string;
33
+ /**
34
+ * 消息id,64位整型
35
+ * @scope MsgType='text' | 'image' | 'voice' | 'video' | 'shortvideo' | 'location'
36
+ */
37
+ MsgId?: string;
38
+ /**
39
+ * 文本消息内容
40
+ * @scope MsgType='text'
41
+ */
42
+ Content?: string;
43
+ /**
44
+ * 图片链接
45
+ * @scope MsgType='image'
46
+ */
47
+ PicUrl?: string;
48
+ /**
49
+ * 媒体id,可以调用获取临时素材接口拉取数据
50
+ * @scope MsgType='image' | 'voice'
51
+ */
52
+ MediaId?: string;
53
+ /**
54
+ * 语音格式,如amr,speex等
55
+ * @scope MsgType='voice'
56
+ */
57
+ Format?: string;
58
+ /**
59
+ * 语音识别结果,UTF8编码
60
+ * @scope MsgType='voice'
61
+ */
62
+ Recognition?: string;
63
+ /**
64
+ * 缩略图的媒体id
65
+ * @scope MsgType='video' | 'shortvideo'
66
+ */
67
+ ThumbMediaId?: string;
68
+ /**
69
+ * 纬度
70
+ * @scope MsgType='location'
71
+ */
72
+ Location_X?: number;
73
+ /**
74
+ * 经度
75
+ * @scope MsgType='location'
76
+ */
77
+ Location_Y?: number;
78
+ /**
79
+ * 地图缩放大小
80
+ * @scope MsgType='location'
81
+ */
82
+ Scale?: number;
83
+ /**
84
+ * 地理位置信息
85
+ * @scope MsgType='location'
86
+ */
87
+ Label?: number;
88
+ /**
89
+ * 消息标题
90
+ * @scope MsgType='link'
91
+ */
92
+ Title?: string;
93
+ /**
94
+ * 消息描述
95
+ * @scope MsgType='link'
96
+ */
97
+ Description?: string;
98
+ /**
99
+ * 消息链接
100
+ * @scope MsgType='link'
101
+ */
102
+ Url?: string;
103
+ /**
104
+ * 事件类型
105
+ * - `subscribe` 关注
106
+ * - `unsubscribe` 取消关注
107
+ * - `SCAN` 扫描(用户已关注)
108
+ * - `LOCATION` 地理位置
109
+ * - `CLICK` 自定义菜单事件
110
+ * - `VIEW` 点击菜单跳转链接
111
+ * @scope MsgType='event'
112
+ */
113
+ Event?: string;
114
+ /**
115
+ * 事件KEY值
116
+ * - Event='subscribe' | 'SCAN'时:qrscene_为前缀,后面为二维码的参数值
117
+ * - Event='CLICK'时:自定义菜单接口中KEY值对应
118
+ * - Event='VIEW'时:设置的跳转URL
119
+ * @scope MsgType='event' && Event='subscribe' | 'SCAN' | 'CLICK' | 'VIEW'
120
+ */
121
+ EventKey?: string;
122
+ /**
123
+ * 二维码的ticket,可用来换取二维码图片
124
+ * @scope MsgType='event' && Event='subscribe' | 'SCAN'
125
+ */
126
+ Ticket?: string;
127
+ /**
128
+ * 纬度
129
+ * @scope MsgType='event' && Event='LOCATION'
130
+ */
131
+ Latitude?: number;
132
+ /**
133
+ * 经度
134
+ * @scope MsgType='event' && Event='LOCATION'
135
+ */
136
+ Longitude?: number;
137
+ /**
138
+ * 位置精度
139
+ * @scope MsgType='event' && Event='LOCATION'
140
+ */
141
+ Precision?: number;
142
+ }
4
143
  export = Message;
@@ -5,4 +5,5 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  const Message_1 = __importDefault(require("../Core/Message"));
6
6
  class Message extends Message_1.default {
7
7
  }
8
+ ;
8
9
  module.exports = Message;
@@ -0,0 +1,52 @@
1
+ import ConfigInterface from '../Core/Contracts/ConfigInterface';
2
+ import ServerInterface from '../Core/Contracts/ServerInterface';
3
+ import Encryptor from '../Core/Encryptor';
4
+ import CacheMixin from '../Core/Mixins/CacheMixin';
5
+ import ConfigMixin from '../Core/Mixins/ConfigMixin';
6
+ import HttpClientMixin from '../Core/Mixins/HttpClientMixin';
7
+ import ServerRequestMixin from '../Core/Mixins/ServerRequestMixin';
8
+ import { PayConfig } from '../Types/global';
9
+ import MerchantInterface from './Contracts/MerchantInterface';
10
+ import ApplicationInterface from './Contracts/ApplicationInterface';
11
+ import Utils from './Utils';
12
+ import HttpClientInterface from '../Core/HttpClient/Contracts/HttpClientInterface';
13
+ /**
14
+ * 微信支付应用
15
+ */
16
+ declare class Application implements ApplicationInterface {
17
+ constructor(config: ConfigInterface | PayConfig);
18
+ protected merchant: MerchantInterface;
19
+ protected encryptor: Encryptor;
20
+ protected server: ServerInterface;
21
+ protected client: HttpClientInterface;
22
+ getMerchant(): MerchantInterface;
23
+ /**
24
+ * 设置当前账户实例
25
+ * @param merchant
26
+ * @returns
27
+ */
28
+ setMerchant(merchant: MerchantInterface): this;
29
+ getServer(): ServerInterface;
30
+ /**
31
+ * 设置服务端实例
32
+ * @param server
33
+ * @returns
34
+ */
35
+ setServer(server: ServerInterface): this;
36
+ getUtils(): Utils;
37
+ getClient(): HttpClientInterface;
38
+ /**
39
+ * 设置客户端
40
+ * @param client
41
+ * @returns
42
+ */
43
+ setClient(client: HttpClientInterface): this;
44
+ /**
45
+ * 获取请求默认配置
46
+ * @returns
47
+ */
48
+ protected getHttpClientDefaultOptions(): Record<string, any>;
49
+ }
50
+ interface Application extends ConfigMixin, CacheMixin, ServerRequestMixin, HttpClientMixin {
51
+ }
52
+ export = Application;
@@ -0,0 +1,96 @@
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 CacheMixin_1 = __importDefault(require("../Core/Mixins/CacheMixin"));
8
+ const ConfigMixin_1 = __importDefault(require("../Core/Mixins/ConfigMixin"));
9
+ const HttpClientMixin_1 = __importDefault(require("../Core/Mixins/HttpClientMixin"));
10
+ const ServerRequestMixin_1 = __importDefault(require("../Core/Mixins/ServerRequestMixin"));
11
+ const Utils_1 = require("../Core/Support/Utils");
12
+ const Merchant_1 = __importDefault(require("./Merchant"));
13
+ const Server_1 = __importDefault(require("./Server"));
14
+ const Utils_2 = __importDefault(require("./Utils"));
15
+ const Config_1 = __importDefault(require("../OfficialAccount/Config"));
16
+ const PrivateKey_1 = require("../Core/Support/PrivateKey");
17
+ const PublicKey_1 = require("../Core/Support/PublicKey");
18
+ const Client_1 = __importDefault(require("./Client"));
19
+ /**
20
+ * 微信支付应用
21
+ */
22
+ class Application {
23
+ constructor(config) {
24
+ this.merchant = null;
25
+ this.encryptor = null;
26
+ this.server = null;
27
+ if (config instanceof ConfigInterface_1.default) {
28
+ this.setConfig(config);
29
+ }
30
+ else {
31
+ this.setConfig(new Config_1.default(config));
32
+ }
33
+ }
34
+ getMerchant() {
35
+ var _a;
36
+ if (!this.merchant) {
37
+ this.merchant = new Merchant_1.default(this.config.get('mch_id'), new PrivateKey_1.PrivateKey(this.config.get('private_key')), new PublicKey_1.PublicKey(this.config.get('certificate')), this.config.get('secret_key'), this.config.get('v2_secret_key'), (_a = this.config.get('platform_certs')) !== null && _a !== void 0 ? _a : []);
38
+ }
39
+ return this.merchant;
40
+ }
41
+ /**
42
+ * 设置当前账户实例
43
+ * @param merchant
44
+ * @returns
45
+ */
46
+ setMerchant(merchant) {
47
+ this.merchant = merchant;
48
+ return this;
49
+ }
50
+ getServer() {
51
+ if (!this.server) {
52
+ this.server = new Server_1.default(this.getMerchant(), this.getRequest());
53
+ }
54
+ return this.server;
55
+ }
56
+ /**
57
+ * 设置服务端实例
58
+ * @param server
59
+ * @returns
60
+ */
61
+ setServer(server) {
62
+ this.server = server;
63
+ return this;
64
+ }
65
+ getUtils() {
66
+ return new Utils_2.default(this.getMerchant());
67
+ }
68
+ getClient() {
69
+ if (!this.client) {
70
+ this.client = (new Client_1.default(this.getMerchant(), this.getHttpClient(), this.config.get('http', {}))).setPresets(this.config.all());
71
+ }
72
+ return this.client;
73
+ }
74
+ /**
75
+ * 设置客户端
76
+ * @param client
77
+ * @returns
78
+ */
79
+ setClient(client) {
80
+ this.client = client;
81
+ return this;
82
+ }
83
+ /**
84
+ * 获取请求默认配置
85
+ * @returns
86
+ */
87
+ getHttpClientDefaultOptions() {
88
+ return (0, merge_1.default)(true, {
89
+ baseURL: 'https://api.weixin.qq.com/',
90
+ }, this.getConfig().get('http'));
91
+ }
92
+ }
93
+ ;
94
+ ;
95
+ (0, Utils_1.applyMixins)(Application, [ConfigMixin_1.default, CacheMixin_1.default, ServerRequestMixin_1.default, HttpClientMixin_1.default]);
96
+ module.exports = Application;
@@ -0,0 +1,42 @@
1
+ import { Method, AxiosRequestConfig, AxiosInstance } from "axios";
2
+ import HttpClientInterface from "../Core/HttpClient/Contracts/HttpClientInterface";
3
+ import HttpClientMethodsMixin from '../Core/HttpClient/Mixins/HttpClientMethodsMixin';
4
+ import { LogHandler } from '../Types/global';
5
+ import HttpClientResponse from '../Core/HttpClient/HttpClientResponse';
6
+ import PresetMixin from '../Core/HttpClient/Mixins/PresetMixin';
7
+ import MerchantInterface from './Contracts/MerchantInterface';
8
+ declare class Client implements HttpClientInterface {
9
+ protected merchant: MerchantInterface;
10
+ protected throw: boolean;
11
+ protected client: HttpClientInterface;
12
+ protected defaultOptions: AxiosRequestConfig<any>;
13
+ V3_URI_PREFIXES: string[];
14
+ constructor(merchant: MerchantInterface, client: HttpClientInterface, defaultOptions?: Record<string, any>);
15
+ getInstance(): AxiosInstance;
16
+ setInstance(instance: AxiosInstance): this;
17
+ setLogger(logger: LogHandler): this;
18
+ request(method: Method, url: string, payload?: AxiosRequestConfig<any>): Promise<HttpClientResponse>;
19
+ /**
20
+ * 判断是否是V3请求
21
+ * @param url 请求地址
22
+ * @returns
23
+ */
24
+ protected isV3Request(url: string): boolean;
25
+ /**
26
+ * 创建签名(V3)
27
+ * @param method 请求方式
28
+ * @param url 请求地址
29
+ * @param payload 请求载荷
30
+ * @returns
31
+ */
32
+ protected createSignature(method: string, url: string, payload: AxiosRequestConfig<any>): string;
33
+ /**
34
+ * 创建签名(V2)
35
+ * @param body 请求参数
36
+ * @returns
37
+ */
38
+ protected attachLegacySignature(body: Record<string, any>): Record<string, string | number>;
39
+ }
40
+ interface Client extends HttpClientMethodsMixin, PresetMixin {
41
+ }
42
+ export = Client;
@@ -0,0 +1,125 @@
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ const merge_1 = __importDefault(require("merge"));
15
+ const Utils_1 = require("../Core/Support/Utils");
16
+ const HttpClient_1 = __importDefault(require("../Core/HttpClient/HttpClient"));
17
+ const HttpClientMethodsMixin_1 = __importDefault(require("../Core/HttpClient/Mixins/HttpClientMethodsMixin"));
18
+ const PresetMixin_1 = __importDefault(require("../Core/HttpClient/Mixins/PresetMixin"));
19
+ const url_1 = __importDefault(require("url"));
20
+ const Signature_1 = __importDefault(require("./Signature"));
21
+ const LegacySignature_1 = __importDefault(require("./LegacySignature"));
22
+ class Client {
23
+ constructor(merchant, client, defaultOptions = {}) {
24
+ var _a;
25
+ this.merchant = merchant;
26
+ this.throw = true;
27
+ this.client = null;
28
+ this.defaultOptions = {
29
+ baseURL: 'https://api.mch.weixin.qq.com',
30
+ headers: {
31
+ 'Content-Type': 'application/json',
32
+ 'Accept': 'application/json',
33
+ },
34
+ };
35
+ this.V3_URI_PREFIXES = [
36
+ '/v3/',
37
+ '/sandbox/v3/',
38
+ '/hk/v3/',
39
+ '/global/v3/',
40
+ ];
41
+ this.throw = !!((_a = defaultOptions['throw']) !== null && _a !== void 0 ? _a : true);
42
+ this.defaultOptions = merge_1.default.recursive(true, this.defaultOptions, defaultOptions);
43
+ this.client = client || HttpClient_1.default.create();
44
+ }
45
+ getInstance() {
46
+ return this.client.getInstance();
47
+ }
48
+ setInstance(instance) {
49
+ this.client.setInstance(instance);
50
+ return this;
51
+ }
52
+ setLogger(logger) {
53
+ this.client.setLogger(logger);
54
+ return this;
55
+ }
56
+ request(method, url, payload = {}) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ if (!payload.headers)
59
+ payload.headers = {};
60
+ if (!payload.headers['user-agent'] && !payload.headers['User-Agent']) {
61
+ payload.headers['user-agent'] = (0, Utils_1.createUserAgent)();
62
+ }
63
+ if (this.isV3Request(url)) {
64
+ payload.headers['authorization'] = this.createSignature(method, url, payload);
65
+ }
66
+ else {
67
+ if (payload.xml) {
68
+ if (Array.isArray(payload.xml)) {
69
+ payload.xml = (0, Utils_1.buildXml)(this.attachLegacySignature(payload.xml));
70
+ }
71
+ if (typeof payload.xml !== 'string') {
72
+ throw new Error('The `xml` option must be a string or object.');
73
+ }
74
+ payload.data = payload.xml;
75
+ delete payload.xml;
76
+ }
77
+ if (payload.data && typeof payload.data === 'object') {
78
+ payload.data = (0, Utils_1.buildXml)(this.attachLegacySignature(payload.data));
79
+ }
80
+ if (!payload.headers['content-type'] && !payload.headers['Content-Type']) {
81
+ payload.headers['content-type'] = 'text/xml';
82
+ }
83
+ }
84
+ if (this.prependHeaders && Object.keys(this.prependHeaders).length > 0) {
85
+ payload.headers = Object.assign(Object.assign({}, this.prependHeaders), payload.headers);
86
+ }
87
+ return this.client.request(method, (0, Utils_1.ltrim)(url, '\\/+'), payload);
88
+ });
89
+ }
90
+ /**
91
+ * 判断是否是V3请求
92
+ * @param url 请求地址
93
+ * @returns
94
+ */
95
+ isV3Request(url) {
96
+ let urlObj = url_1.default.parse(url);
97
+ for (let i = 0; i < this.V3_URI_PREFIXES.length; i++) {
98
+ if (urlObj.pathname.startsWith(this.V3_URI_PREFIXES[i])) {
99
+ return true;
100
+ }
101
+ }
102
+ return false;
103
+ }
104
+ /**
105
+ * 创建签名(V3)
106
+ * @param method 请求方式
107
+ * @param url 请求地址
108
+ * @param payload 请求载荷
109
+ * @returns
110
+ */
111
+ createSignature(method, url, payload) {
112
+ return (new Signature_1.default(this.merchant)).createHeader(method, url, payload);
113
+ }
114
+ /**
115
+ * 创建签名(V2)
116
+ * @param body 请求参数
117
+ * @returns
118
+ */
119
+ attachLegacySignature(body) {
120
+ return (new LegacySignature_1.default(this.merchant)).sign(body);
121
+ }
122
+ }
123
+ ;
124
+ (0, Utils_1.applyMixins)(Client, [HttpClientMethodsMixin_1.default, PresetMixin_1.default]);
125
+ module.exports = Client;
@@ -0,0 +1,5 @@
1
+ import BaseConfig from '../Core/Config';
2
+ declare class Config extends BaseConfig {
3
+ protected requiredKeys: string[];
4
+ }
5
+ export = Config;
@@ -0,0 +1,17 @@
1
+ 'use strict';
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ const Config_1 = __importDefault(require("../Core/Config"));
6
+ class Config extends Config_1.default {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.requiredKeys = [
10
+ 'mch_id',
11
+ 'secret_key',
12
+ 'private_key',
13
+ 'certificate',
14
+ ];
15
+ }
16
+ }
17
+ module.exports = Config;